void idSplineList::buildSpline() { //int start = Sys_Milliseconds(); clearSpline(); for(int i = 3; i < controlPoints.Num(); i++) { for(float tension = 0.0f; tension < 1.001f; tension += granularity) { float x = 0; float y = 0; float z = 0; for(int j = 0; j < 4; j++) { x += controlPoints[i - (3 - j)]->x * calcSpline(j, tension); y += controlPoints[i - (3 - j)]->y * calcSpline(j, tension); z += controlPoints[i - (3 - j)]->z * calcSpline(j, tension); } splinePoints.Append(new idVec3(x, y, z)); } } dirty = false; //Com_Printf("Spline build took %f seconds\n", (float)(Sys_Milliseconds() - start) / 1000); }
void idSplineList::addToRenderer() { if (controlPoints.Num() == 0) { return; } idVec3_t mins, maxs; idVec3_t yellow(1.0, 1.0, 0); idVec3_t white(1.0, 1.0, 1.0); int i; for(i = 0; i < controlPoints.Num(); i++) { VectorCopy(*controlPoints[i], mins); VectorCopy(mins, maxs); mins[0] -= 8; mins[1] += 8; mins[2] -= 8; maxs[0] += 8; maxs[1] -= 8; maxs[2] += 8; debugLine( yellow, mins[0], mins[1], mins[2], maxs[0], mins[1], mins[2]); debugLine( yellow, maxs[0], mins[1], mins[2], maxs[0], maxs[1], mins[2]); debugLine( yellow, maxs[0], maxs[1], mins[2], mins[0], maxs[1], mins[2]); debugLine( yellow, mins[0], maxs[1], mins[2], mins[0], mins[1], mins[2]); debugLine( yellow, mins[0], mins[1], maxs[2], maxs[0], mins[1], maxs[2]); debugLine( yellow, maxs[0], mins[1], maxs[2], maxs[0], maxs[1], maxs[2]); debugLine( yellow, maxs[0], maxs[1], maxs[2], mins[0], maxs[1], maxs[2]); debugLine( yellow, mins[0], maxs[1], maxs[2], mins[0], mins[1], maxs[2]); } int step = 0; idVec3_t step1; for(i = 3; i < controlPoints.Num(); i++) { for (float tension = 0.0f; tension < 1.001f; tension += 0.1f) { float x = 0; float y = 0; float z = 0; for (int j = 0; j < 4; j++) { x += controlPoints[i - (3 - j)]->x * calcSpline(j, tension); y += controlPoints[i - (3 - j)]->y * calcSpline(j, tension); z += controlPoints[i - (3 - j)]->z * calcSpline(j, tension); } if (step == 0) { step1[0] = x; step1[1] = y; step1[2] = z; step = 1; } else { debugLine( white, step1[0], step1[1], step1[2], x, y, z); step = 0; } } } }
NaturalCubicSpline::NaturalCubicSpline( osg::ref_ptr<osg::Vec4Array> knots, int curveSteps, BaseCurve extrudeShape) : _knots(knots.get()), _curveSteps(curveSteps), _extrudeShape(extrudeShape) { _geometry = new osg::Geometry; calcSpline(); }
double Spline::spline(double xarg) const { if (numPoints()==0) return 0.0; if (numPoints()==1) return mPoints[0].y; if (mRecalc) calcSpline(); int klo=0; int khi=numPoints()-1; int k; while (khi-klo > 1) { k = khi+klo; if (k % 2) k = (k+1) / 2; else k = k/2; if (mPoints[k].x > xarg) khi=k; else klo=k; } double h = mPoints[khi].x - mPoints[klo].x; if (h==0) { // failed return 0.0; } double a = (mPoints[khi].x - xarg) / h; double b = (xarg - mPoints[klo].x) / h; return a * mPoints[klo].y + b*mPoints[khi].y + ((a*a*a-a) * mPoints[klo].y2 + (b*b*b-b) * mPoints[khi].y2) * (h*h) / 6.0; }