int main(int argc,char** argv) { jhm::position p0(0,1,2); jhm::position p1(5,2,7); jhm::position p2(-3,-7,3); jhm::vector v0(1,2.5,3); jhm::vector v1(-1,5,4.5); double c = 3.0; //(position)+(position) = (UNDEFINED) //std::cout<<p0<<" + "<<p1<<" = "<<p0+p1<<std::endl; //(vector)+-(vector) = (vector) std::cout<<"position, vector operation"<<std::endl; std::cout<<v0<<" + "<<v1<<" = "<<v0+v1<<std::endl; std::cout<<v0<<" - "<<v1<<" = "<<v0-v1<<std::endl; //(position)+-(vector) = (position) std::cout<<p0<<" + "<<v0<<" = "<<p0+v0<<std::endl; std::cout<<p0<<" - "<<v0<<" = "<<p0-v0<<std::endl; //(position)-(position) = (vector) std::cout<<p0<<" - "<<p1<<" = "<<p0-p1<<std::endl; //(scalar)*(vector) = (vector) std::cout<<c<<" * "<<v0<<" = "<<c*v0<<std::endl; std::cout<<std::endl; std::cout<<"Affine combination"<<std::endl; //sum(scalar)*(point) = (point) if sum(scalar) = 1 // = (vector) if sum(scalar) = 0 : meaningless // = (UNDEFINED) o.w. // p = 0.3*p0 + 0.7*p1 std::cout<<"0.3*"<<p0<<" + 0.7*"<<p1<<" = "<<affineCombination(p0,p1,0.3)<<std::endl; // p = 0.1*p0 + 0.4*p1 + 0.5*p2 std::cout<<"0.1*"<<p0<<" + 0.4*"<<p1<<" + 0.5*"<<p2<<" = "<<affineCombination(p0,p1,p2,0.1,0.4)<<std::endl; jhm::position p[3] = {p0,p1,p2}; double t[2] = {0.1,0.4}; std::cout<<"0.1*"<<p0<<" + 0.4*"<<p1<<" + 0.5*"<<p2<<" = "<<affineCombination(3,p,t)<<std::endl; std::cout<<std::endl; std::cout<<"operation"<<std::endl; std::cout<<"|p0| : "<<p0.norm()<<std::endl; std::cout<<"v0 (dot) v1 : "<<v0%v1<<std::endl; std::cout<<"v0 (cross) v1 : "<<v0*v1<<std::endl; std::cout<<"|p0 - p1| : "<<distance(p0,p1)<<std::endl; return 0; }
METHODPREFIX void PolygonBase<ScalarParam,dimensionParam>::doClip( const typename PolygonBase<ScalarParam,dimensionParam>::Plane& plane) { /* Create temporary array of vertices: */ int numClipVertices=0; Point* clipVertices=new Point[numVertices+1]; /* Process each edge of the polygon: */ Point* p1=&vertices[numVertices-1]; Scalar d1=plane.calcDistance(*p1); for(int i=0;i<numVertices;++i) { Point* p2=&vertices[i]; Scalar d2=plane.calcDistance(*p2); /* Check if the edge intersects the plane: */ if(d1*d2<Scalar(0)) { /* Add the intersection point to the clipped polygon: */ clipVertices[numClipVertices]=affineCombination(*p1,*p2,-d1/(d2-d1)); ++numClipVertices; } /* Check if the edge's end point is behind the plane: */ if(d2<=Scalar(0)) { /* Add the end point to the clipped polygon: */ clipVertices[numClipVertices]=*p2; ++numClipVertices; } /* Go to the next vertex: */ p1=p2; d1=d2; } /* Create the final polygon: */ if(numClipVertices!=numVertices) { delete[] vertices; numVertices=numClipVertices; vertices=new Point[numVertices]; } for(int i=0;i<numVertices;++i) vertices[i]=clipVertices[i]; delete[] clipVertices; }
METHODPREFIX SplineCurve<ScalarParam,dimensionParam>& SplineCurve<ScalarParam,dimensionParam>::insertKnot( typename SplineCurve<ScalarParam,dimensionParam>::Scalar newKnot) { /* Insert new knot into knot array: */ int numKnots=numPoints+degree-1; Scalar* newKnots=new Scalar[numKnots+1]; int i; for(i=0;i<numKnots&&knots[i]<=newKnot;++i) newKnots[i]=knots[i]; int iv=i-degree; newKnots[i]=newKnot; for(;i<numKnots;++i) newKnots[i+1]=knots[i]; /* Copy initial part of control point array: */ Point* newPoints=new Point[numPoints+1]; for(i=0;i<=iv;++i) newPoints[i]=points[i]; /* Calculate new control points influenced by new knot as affine combinations of old ones: */ for(;i<=iv+degree;++i) { Scalar alpha=(newKnot-knots[i-1])/(knots[i+degree-1]-knots[i-1]); newPoints[i]=affineCombination(points[i-1],points[i],alpha); } /* Copy rest of control point array: */ for(;i<=numPoints;++i) newPoints[i]=points[i-1]; /* Store new knot and control point arrays: */ ++numPoints; delete[] knots; knots=newKnots; delete[] points; points=newPoints; /* Return resulting spline curve: */ return *this; }