Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/curve.cc @ 3319

Last change on this file since 3319 was 3319, checked in by bensch, 19 years ago

orxonox/branches/parenting: added a testCurve to world.cc, and removed curve from the vector-class.

File size: 3.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: Patrick Boenzli
14
15   ADD: Patrick Boenzli           B-Spline
16
17
18   TODO:
19     local-Time implementation
20     NURBS
21     
22*/
23
24#include "curve.h"
25
26
27
28/**
29   \brief adds a new Node to the bezier Curve
30   \param newNode a Vector to the position of the new node
31*/
32void Curve::addNode(const Vector& newNode)
33{
34  if (nodeCount != 0 )
35    {
36      currentNode = currentNode->next = new PathNode;
37    }
38  currentNode->position = newNode;
39  currentNode->next = 0; // not sure if this really points to NULL!!
40  currentNode->number = (++nodeCount);
41  return;
42}
43
44
45/**
46   \brief Creates a new BezierCurve
47*/
48BezierCurve::BezierCurve (void)
49{
50  nodeCount = 0;
51  firstNode = new PathNode;
52  currentNode = firstNode;
53
54  firstNode->position = Vector (.0, .0, .0);
55  firstNode->number = 0;
56  firstNode->next = 0; // not sure if this really points to NULL!!
57
58  return;
59}
60
61/**
62   \brief Deletes a BezierCurve.
63
64   It does this by freeing all the space taken over from the nodes
65*/
66BezierCurve::~BezierCurve (void)
67{
68  PathNode* tmpNode;
69  currentNode = firstNode;
70  while (tmpNode != 0)
71    {
72      tmpNode = currentNode;
73      currentNode = currentNode->next;
74      delete tmpNode;
75    }
76}
77
78/**
79   \brief calculates the Position on the curve
80   \param t The position on the Curve (0<=t<=1)
81   \return the Position on the Path
82*/
83Vector BezierCurve::calcPos(float t) 
84{
85  if (nodeCount <=4)
86    {
87      //      if (verbose >= 1)
88      //      printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount);
89      return Vector(0,0,0);
90    }
91  PathNode* tmpNode = firstNode;
92   
93  Vector ret = Vector(0.0,0.0,0.0);
94  double factor;
95  int k=0;
96  while(tmpNode!=0)
97    {
98      k++;
99      factor = ncr (nodeCount, k);
100     
101      for (int j=0; j<nodeCount-k; j++)
102        factor*=(1-t);
103      for (int j=0; j<k; j++)
104        factor*=t;
105      ret.x += factor * tmpNode->position.x;
106      ret.y += factor * tmpNode->position.y;
107      ret.z += factor * tmpNode->position.z;
108     
109      tmpNode = tmpNode->next;
110
111    }
112  return ret;
113}
114
115/**
116   \brief Calulates the direction of the Curve at time t.
117   \param The time at which to evaluate the curve.
118   \returns The vvaluated Vector.
119*/
120Vector BezierCurve::calcDir (float t)
121{
122  PathNode* tmpNode = firstNode;
123  BezierCurve* tmpCurve = new BezierCurve();
124  Vector ret;
125  Vector tmpVector;
126
127  while (tmpNode->next != 0)
128    {
129      tmpVector = (tmpNode->next->position)- (tmpNode->position);
130      tmpVector.x*=(float)nodeCount;
131      tmpVector.y*=(float)nodeCount;
132      tmpVector.z*=(float)nodeCount;
133
134      tmpCurve->addNode(tmpVector);
135      tmpNode = tmpNode->next;
136    }
137  ret = tmpCurve->calcPos(t);
138  ret.normalize();
139
140  return ret;
141}
142
143/**
144   \brief Calculates the Quaternion needed for our rotations
145   \param t The time at which to evaluate the cuve.
146   \returns The evaluated Quaternion.
147*/
148Quaternion BezierCurve::calcQuat (float t)
149{
150  return Quaternion (calcDir(t), Vector(0,0,1));
151}
152
153
154/**
155  \brief returns the Position of the point calculated on the Curve
156  \return a Vector to the calculated position
157*/
158Vector BezierCurve::getPos(void) const
159{
160  return curvePoint;
161}
162
163/**
164   \brief ncr-calculator, did not find an other method
165   \todo a c++ variante to do this
166*/
167int ncr(int n, int i)
168{
169  int ret = 1;
170  for (int k=1; k<=n; k++)
171    ret*=k;
172  for (int k=1; k<=i; k++)
173    ret/=k;
174  for (int k=1; k<=n-i; k++)
175    ret/=k;
176
177  return ret;
178}
179
Note: See TracBrowser for help on using the repository browser.