Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3018 in orxonox.OLD for orxonox/branches/bezierTrack/src/vector.cc


Ignore:
Timestamp:
Nov 28, 2004, 1:31:21 PM (21 years ago)
Author:
bensch
Message:

orxonox/branches/bezierTrack: moved Curve to its own file. I expect this class to be bigger than I thought at the beginning:

  1. Class Curve and its subclasses: a) BezierCurve b) closedBezierCurve c) Nurbs d) any other curve that might occure

this will take some time and many hours of thinking so please help

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/bezierTrack/src/vector.cc

    r3017 r3018  
    845845
    846846
    847 /**
    848    \brief Creates a new BezierCurve
    849 */
    850 BezierCurve::BezierCurve (void)
    851 {
    852   nodeCount = 0;
    853   firstNode = new PathNode;
    854   currentNode = firstNode;
    855 
    856   firstNode->position = Vector (.0, .0, .0);
    857   firstNode->number = 0;
    858   firstNode->next = 0; // not sure if this really points to NULL!!
    859 
    860   return;
    861 }
    862 
    863 /**
    864    \brief Deletes a BezierCurve.
    865    It does this by freeing all the space taken over from the nodes
    866 */
    867 BezierCurve::~BezierCurve (void)
    868 {
    869   PathNode* tmpNode;
    870   currentNode = firstNode;
    871   while (tmpNode != 0)
    872     {
    873       tmpNode = currentNode;
    874       currentNode = currentNode->next;
    875       delete tmpNode;
    876     }
    877 }
    878 
    879 /**
    880    \brief adds a new Node to the bezier Curve
    881    \param newNode a Vector to the position of the new node
    882 */
    883 void BezierCurve::addNode(const Vector& newNode)
    884 {
    885   if (nodeCount != 0 )
    886     {
    887       currentNode = currentNode->next = new PathNode;
    888     }
    889   currentNode->position = newNode;
    890   currentNode->next = 0; // not sure if this really points to NULL!!
    891   currentNode->number = (++nodeCount);
    892   return;
    893 }
    894 
    895 /**
    896    \brief calculates the Position on the curve
    897    \param t The position on the Curve (0<=t<=1)
    898    \return the Position on the Path
    899 */
    900 Vector BezierCurve::calcPos(float t)
    901 {
    902   if (nodeCount <=4)
    903     {
    904       //      if (verbose >= 1)
    905       //      printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount);
    906       return Vector(0,0,0);
    907     }
    908   PathNode* tmpNode = firstNode;
    909    
    910   Vector ret = Vector(0.0,0.0,0.0);
    911   double factor;
    912   int k=0;
    913   while(tmpNode!=0)
    914     {
    915       k++;
    916       factor = ncr (nodeCount, k);
    917      
    918       for (int j=0; j<nodeCount-k; j++)
    919         factor*=(1-t);
    920       for (int j=0; j<k; j++)
    921         factor*=t;
    922       ret.x += factor * tmpNode->position.x;
    923       ret.y += factor * tmpNode->position.y;
    924       ret.z += factor * tmpNode->position.z;
    925      
    926       tmpNode = tmpNode->next;
    927 
    928     }
    929   return ret;
    930 }
    931 
    932 Vector BezierCurve::calcDir (float t)
    933 {
    934   PathNode* tmpNode = firstNode;
    935   BezierCurve* tmpCurve = new BezierCurve();
    936   Vector ret;
    937   Vector tmpVector;
    938 
    939   while (tmpNode->next != 0)
    940     {
    941       tmpVector = (tmpNode->next->position)- (tmpNode->position);
    942       tmpVector.x*=(float)nodeCount;
    943       tmpVector.y*=(float)nodeCount;
    944       tmpVector.z*=(float)nodeCount;
    945 
    946       tmpCurve->addNode(tmpVector);
    947       tmpNode = tmpNode->next;
    948     }
    949   ret = tmpCurve->calcPos(t);
    950   ret.normalize();
    951 
    952   return Vector (0,0,0);//ret;
    953 }
    954 
    955 /**
    956   \brief returns the Position of the point calculated on the Curve
    957   \return a Vector to the calculated position
    958 */
    959 Vector BezierCurve::getPos() const
    960 {
    961   return curvePoint;
    962 }
    963 
    964 
    965 int BezierCurve::ncr(int n, int i)
    966 {
    967   int ret = 1;
    968   for (int k=1; k<=n; k++)
    969     ret*=k;
    970   for (int k=1; k<=i; k++)
    971     ret/=k;
    972   for (int k=1; k<=n-i; k++)
    973     ret/=k;
    974 
    975   return ret;
    976 }
Note: See TracChangeset for help on using the changeset viewer.