| 19 | #include <algorithm> |
| 20 | |
| 21 | LNLib::LN_NurbsCurve LNLib::NurbsVolume::GetIsoCurve(const LN_NurbsVolume& volume, UVW uvw) |
| 22 | { |
| 23 | LN_NurbsCurve isoCurve; |
| 24 | |
| 25 | int numU = volume.ControlPoints.size(); |
| 26 | if (numU == 0) return isoCurve; |
| 27 | int numV = volume.ControlPoints[0].size(); |
| 28 | if (numV == 0) return isoCurve; |
| 29 | int numW = volume.ControlPoints[0][0].size(); |
| 30 | |
| 31 | auto clampParam = [](double param, const std::vector<double>& kv, int degree) { |
| 32 | double minVal = kv[degree]; |
| 33 | double maxVal = kv[kv.size() - degree - 1]; |
| 34 | return std::clamp(param, minVal, maxVal); |
| 35 | }; |
| 36 | |
| 37 | if (MathUtils::IsAlmostEqualTo(uvw.U(),-1.0)) |
| 38 | { |
| 39 | isoCurve.Degree = volume.DegreeU; |
| 40 | isoCurve.KnotVector = volume.KnotVectorU; |
| 41 | isoCurve.ControlPoints.reserve(numU); |
| 42 | |
| 43 | double fixedV = clampParam(uvw.V(), volume.KnotVectorV, volume.DegreeV); |
| 44 | double fixedW = clampParam(uvw.W(), volume.KnotVectorW, volume.DegreeW); |
| 45 | |
| 46 | int vSpan = Polynomials::GetKnotSpanIndex(volume.DegreeV, volume.KnotVectorV, fixedV); |
| 47 | int wSpan = Polynomials::GetKnotSpanIndex(volume.DegreeW, volume.KnotVectorW, fixedW); |
| 48 | double Nv[Constants::NURBSMaxDegree + 1]; |
| 49 | Polynomials::BasisFunctions(vSpan, volume.DegreeV, volume.KnotVectorV, fixedV, Nv); |
| 50 | double Nw[Constants::NURBSMaxDegree + 1]; |
| 51 | Polynomials::BasisFunctions(wSpan, volume.DegreeW, volume.KnotVectorW, fixedW, Nw); |
| 52 | |
| 53 | for (int u = 0; u < numU; ++u) |
| 54 | { |
| 55 | XYZW combinedPoint(0.0, 0.0, 0.0, 0.0); |
| 56 | for (int j = 0; j <= volume.DegreeV; ++j) |
| 57 | { |
| 58 | int vIndex = vSpan - volume.DegreeV + j; |
| 59 | for (int k = 0; k <= volume.DegreeW; ++k) |
| 60 | { |
| 61 | int wIndex = wSpan - volume.DegreeW + k; |
| 62 | combinedPoint = combinedPoint + (Nv[j] * Nw[k] * volume.ControlPoints[u][vIndex][wIndex]); |
| 63 | } |
| 64 | } |
| 65 | isoCurve.ControlPoints.emplace_back(std::move(combinedPoint)); |
| 66 | } |
| 67 | } |
| 68 | else if (MathUtils::IsAlmostEqualTo(uvw.V(), -1.0)) |
| 69 | { |
| 70 | isoCurve.Degree = volume.DegreeV; |
| 71 | isoCurve.KnotVector = volume.KnotVectorV; |
| 72 | isoCurve.ControlPoints.reserve(numV); |
| 73 | |
| 74 | double fixedU = clampParam(uvw.U(), volume.KnotVectorU, volume.DegreeU); |
| 75 | double fixedW = clampParam(uvw.W(), volume.KnotVectorW, volume.DegreeW); |
| 76 | |
| 77 | int uSpan = Polynomials::GetKnotSpanIndex(volume.DegreeU, volume.KnotVectorU, fixedU); |
| 78 | int wSpan = Polynomials::GetKnotSpanIndex(volume.DegreeW, volume.KnotVectorW, fixedW); |