dMatrix clSpline::GetCoordinates(const dMatrix &uSpec) { dMatrix coord; if(uSpec.GetNumberColumns() != 1 || uSpec.GetNumberRows() == 0) { throw clException("clSpline", "GetCoordinates", "Invalid dimensions of matrix uSpec."); } else { coord.SetNumberRows(uSpec.GetNumberRows()); coord.SetNumberColumns(3); if(!initialised) { Initialise(); } // Do for all specified u's for(int i=1; i<=uSpec.GetNumberRows(); i++) { if(uSpec(i, 1) < 0 || uSpec(i, 1) > 1) { throw clException("clSpline", "GetCoordinates", "Invalid value for uSpec."); } else { // Now find the position of uSpec double uu = uSpec(i, 1)*GetSplineLength(); int j = 1; while((uu - u(j+1, 1) > 0) && (j<u.GetNumberRows()-1)) { j++; } // Now calculate the coefficients double A = (u(j+1, 1)-uu)/(u(j+1, 1)-u(j,1)); double B = 1-A; double C = (A*A*A-A)/6 * (u(j+1, 1)-u(j, 1))*(u(j+1, 1)-u(j, 1)); double D = (B*B*B-B)/6 * (u(j+1, 1)-u(j, 1))*(u(j+1, 1)-u(j, 1)); // Finally calculate the coordinates coord.SetElement(i, 1, A*X(j, 1) + B*X(j+1, 1) + C*X2(j, 1) + D*X2(j+1, 1)); coord.SetElement(i, 2, A*Y(j, 1) + B*Y(j+1, 1) + C*Y2(j, 1) + D*Y2(j+1, 1)); coord.SetElement(i, 3, A*Z(j, 1) + B*Z(j+1, 1) + C*Z2(j, 1) + D*Z2(j+1, 1)); } } } return (coord); }
void clSpline::SetControlPoints(const dMatrix &cPointsX, const dMatrix &cPointsY, const dMatrix &cPointsZ) { // Check if dimensions are correct if((cPointsX.GetNumberColumns() != 1 || cPointsY.GetNumberColumns() != 1 || cPointsZ.GetNumberColumns() != 1) || (cPointsX.GetNumberRows() != cPointsY.GetNumberRows()) || (cPointsX.GetNumberRows() != cPointsZ.GetNumberRows())) { throw clException("clSpline", "SetControlPoints", "Dimensions of matrices do not match."); } else { X = cPointsX; Y = cPointsY; Z = cPointsZ; } }