// virtual int Spline2f::insertControlPoint( const Vector2f& p ) { float t; closestPointOnSpline( p, &t ); int controlPointIndex; if( t >= ( 1.f - delta() ) ) { controlPointIndex = numControlPoints(); } else if( t <= delta() ) { controlPointIndex = 0; } else { controlPointIndex = Arithmetic::roundToInt( t * numControlPoints() ); } controlPointIndex = MathUtils::clampToRangeInt( controlPointIndex, 0, numControlPoints() ); printf( "closest t = %f, controlPointIndex = %d\n", t, controlPointIndex ); m_xSpline.insertControlPoint( controlPointIndex, p.x ); m_ySpline.insertControlPoint( controlPointIndex, p.y ); m_bCacheIsDirty = true; return controlPointIndex; }
// virtual int Spline2f::insertControlPoint( const Vector2f& p ) { float t; closestPointOnSpline( p, &t ); int controlPointIndex; if( t >= ( 1.f - delta() ) ) { controlPointIndex = numControlPoints(); } else if( t <= delta() ) { controlPointIndex = 0; } else { controlPointIndex = libcgt::core::math::roundToInt( t * numControlPoints() ); } controlPointIndex = libcgt::core::math::clampToRangeExclusive( controlPointIndex, 0, numControlPoints()); m_xSpline.insertControlPoint( controlPointIndex, p.x ); m_ySpline.insertControlPoint( controlPointIndex, p.y ); m_bCacheIsDirty = true; return controlPointIndex; }
void BezierSpline::write_text(std::ostream &os, int num_points) { if (num_points == 0) num_points = (closed())?numControlPoints()+1:numControlPoints(); for (int j = 0; j < num_points; ++j) { Point point = getControlPoint(j % numControlPoints()); os << point[0] << " " << point[1] << " " << point[2] << " "; } os << std::endl; }
void BezierSpline::displaceHeight(float hdiff) { for (unsigned int i = 0; i < numControlPoints(); ++i) { control_points_[i][2] += hdiff; } }
void BezierSpline::simplify(float min_distance, float max_distance) { if (numControlPoints() <= 2) return; std::vector<Point> sample_points; ::simplify(control_points_, sample_points, min_distance, max_distance); control_points_ = sample_points; }
void BezierSpline::subSample(float tol) { if (numControlPoints() <= 2) return; std::vector<Point> new_points; ::subSample(control_points_, new_points, tol); control_points_ = new_points; }
void BezierSpline::cleanup() { int n = numControlPoints(); if (n > 1) { for (int i = n-1; i >= 1; --i) if ((control_points_[i] - control_points_[i-1]).norm() < FLT_EPSILON) control_points_.erase(control_points_.begin()+i); } }
void BezierSpline::updateKnots() { //assert(numControlPoints() >= order()); knots_.resize(numControlPoints() + order()); for (unsigned int i = 0; i < order(); ++i) knots_[i] = 0.0f; for (unsigned int i = order(); i < numKnots()-order(); ++i) knots_[i] = (i-order() + 1) / ((float) numKnots()-2*order() + 1); for (unsigned int i = numKnots()-order(); i < numKnots(); ++i) knots_[i] = 1.0f; }
Spline2f Spline2f::offsetPath( float distance ) { Spline2f offsetSpline; int nControlPoints = numControlPoints(); float delta = 1.f / ( nControlPoints - 1 ); for( int i = 0; i < nControlPoints; ++i ) { float t = i * delta; Vector2f controlPoint = getControlPoint( i ); Vector2f normalAtControlPoint = normalAt( t ).normalized(); offsetSpline.appendControlPoint( controlPoint + distance * normalAtControlPoint ); } return offsetSpline; }
int Spline2f::closestControlPoint( const Vector2f& p, float* distanceSquared ) { int minIndex = -1; float minDistanceSquared = FLT_MAX; for( int i = 0; i < numControlPoints(); ++i ) { Vector2f controlPoint = getControlPoint( i ); float currentDistanceSquared = ( p - controlPoint ).normSquared(); if( currentDistanceSquared < minDistanceSquared ) { minDistanceSquared = currentDistanceSquared; minIndex = i; } } if( distanceSquared != NULL ) { *distanceSquared = minDistanceSquared; } return minIndex; }
int Spline2f::numPointsToEvaluate() { return numControlPoints() * m_nPointsToEvaluateFactor; }
bool OpenNaturalCubicSpline::isValid() { return( numControlPoints() >= 4 ); }
BezierSpline::Point BezierSpline::getControlPoint(int idx) const { assert(idx >= 0 && idx < numControlPoints()); return control_points_[idx]; }
void BezierSpline::setControlPoint(int idx, const Point &point) { assert(idx >= 0 && idx < numControlPoints()); control_points_[idx] = point; }