126 : values_(path)
127 {
128 if (path.size() < 3)
129 throw std::invalid_argument(
130 "Cartesian spline requires at least three path nodes");
131 knots_.resize(path.size());
132 knots_[0] = 0.;
133 for (std::size_t j = 1; j < path.size(); ++j) {
134 if (path[j].size() != path.front().size() || !path[j].allFinite())
135 throw std::invalid_argument(
136 "Finite consistent Cartesian path coordinates required");
137 const double length = (path[j] - path[j - 1]).norm();
138 if (!(length > 0) || !std::isfinite(length))
139 throw std::invalid_argument(
140 "Distinct Cartesian path nodes required");
141 knots_[j] = knots_[j - 1] + length;
142 }
143 const double total = knots_.back();
144 if (!(total > 0) || !std::isfinite(total))
145 throw std::invalid_argument(
146 "Finite Cartesian path length required");
147 for (double& knot: knots_)
148 knot /= total;
149 second_.assign(path.size(), V::Zero(path.front().size()));
150 const Eigen::Index interior =
151 static_cast<Eigen::Index>(path.size() - 2);
152 Eigen::MatrixXd matrix = Eigen::MatrixXd::Zero(interior, interior);
153 Eigen::MatrixXd rhs =
154 Eigen::MatrixXd::Zero(interior, path.front().size());
155 for (Eigen::Index i = 0; i < interior; ++i) {
156 const std::size_t j = static_cast<std::size_t>(i + 1);
157 const double left = knots_[j] - knots_[j - 1],
158 right = knots_[j + 1] - knots_[j];
159 matrix(i, i) = 2 * (left + right);
160 if (i) matrix(i, i - 1) = left;
161 if (i + 1 < interior) matrix(i, i + 1) = right;
162 rhs.row(i) = 6 * ((path[j + 1] - path[j]) / right -
163 (path[j] - path[j - 1]) / left)
164 .transpose();
165 }
166 const Eigen::MatrixXd curvature = matrix.ldlt().solve(rhs);
167 if (!curvature.allFinite())
168 throw std::runtime_error("Cartesian natural spline solve failed");
169 for (Eigen::Index i = 0; i < interior; ++i)
170 second_[static_cast<std::size_t>(i + 1)] =
171 curvature.row(i).transpose();
172 }