Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/math/curve.cc @ 5053

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

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

File size: 6.4 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     tList implementation
22     
23*/
24
25#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
26
27#include "curve.h"
28
29#include "debug.h"
30
31#include <math.h>
32#include <stdio.h>
33
34
35/**
36  *  default constructor for a Curve
37*/
38Curve::Curve()
39{
40  nodeCount = 0;
41  firstNode = new PathNode;
42  currentNode = firstNode;
43
44  firstNode->position = Vector (.0, .0, .0);
45  firstNode->number = 0;
46  firstNode->next = 0; // not sure if this really points to NULL!!
47}
48
49/**
50 *  adds a new Node to the bezier Curve
51 * @param newNode a Vector to the position of the new node
52*/
53void Curve::addNode(const Vector& newNode)
54{
55  if (nodeCount != 0 )
56    {
57      currentNode = currentNode->next = new PathNode;
58    }
59  currentNode->position = newNode;
60  currentNode->next = 0;
61  currentNode->number = (++nodeCount);
62  this->rebuild();
63  return;
64}
65
66/**
67 *  adds a new Node to the bezier Curve
68 * @param newNode a Vector to the position of the new node
69 * @param insertPosition after the n-th node the new node will be inserted
70*/
71void Curve::addNode(const Vector& newNode, unsigned int insertPosition)
72{
73
74  if (this->nodeCount == 0 || insertPosition > this->nodeCount)
75    return addNode(newNode);
76
77  if (insertPosition == 0)
78    insertPosition = 1;
79
80  PathNode* insNode = new PathNode;
81
82  // relinking
83  PathNode* tmpNode = this->firstNode;
84  if (insertPosition > 1)
85    {
86      while (tmpNode->next->number != insertPosition)
87        tmpNode= tmpNode->next;
88      insNode->next = tmpNode->next;
89      tmpNode->next = insNode;
90    }
91  else
92    {
93      insNode->next = this->firstNode;
94      this->firstNode = insNode;
95    }
96  // renumbering
97  insNode->number = insertPosition;
98  tmpNode = insNode->next;
99  while (tmpNode)
100    {
101      tmpNode->number++;
102      tmpNode = tmpNode->next;
103    }
104
105    // finished
106  insNode->position = newNode;
107  ++nodeCount;
108  this->rebuild();
109  return;
110}
111
112/**
113 *  Finds a Node by its Number, and returns its Position
114 * @param nodeToFind the n'th node in the List of nodes
115 * @returns A Vector to the Position of the Node.
116*/
117Vector Curve::getNode(unsigned int nodeToFind)
118{
119  if (nodeToFind > this->nodeCount || nodeToFind < 0)
120    return Vector(0,0,0);
121  PathNode* tmpNode = this->firstNode;
122  for (int i = 1; i < nodeToFind; i++)
123    tmpNode = tmpNode->next;
124  return tmpNode->position;
125}
126
127/**
128 *  Outputs information about the state of this Curve
129*/
130void Curve::debug()
131{
132  printf("<<-------------------------------\n");
133  printf("Curve Information:\n");
134  printf("NodeCount: %d\n", this->nodeCount);
135  PathNode* tmpNode = this->firstNode;
136  while (tmpNode)
137    {
138      printf("node #%d: %f, %f, %f\n", tmpNode->number, tmpNode->position.x, tmpNode->position.y, tmpNode->position.z);
139      tmpNode = tmpNode->next;
140    }
141  printf("------------------------------->>\n");
142}
143
144
145///////////////////////////////////
146/// Bezier Curve //////////////////
147///////////////////////////////////
148
149/**
150 *  Creates a new BezierCurve
151*/
152BezierCurve::BezierCurve ()
153{
154  this->derivation = 0;
155  dirCurve = new BezierCurve(1);
156}
157
158/**
159 *  Creates a new BezierCurve-Derivation-Curve
160*/
161BezierCurve::BezierCurve (int derivation)
162{
163  this->derivation = derivation;
164  dirCurve=NULL;
165}
166
167/**
168 *  Deletes a BezierCurve.
169
170   It does this by freeing all the space taken over from the nodes
171*/
172BezierCurve::~BezierCurve()
173{
174  PathNode* tmpNode;
175  currentNode = firstNode;
176  while (tmpNode != 0)
177    {
178      tmpNode = currentNode;
179      currentNode = currentNode->next;
180      delete tmpNode;
181    }
182  if (dirCurve)
183    delete dirCurve;
184}
185
186/**
187 *  Rebuilds a Curve
188*/
189void BezierCurve::rebuild()
190{
191  PathNode* tmpNode = firstNode;
192
193  // rebuilding the Curve itself
194  float k=0;
195  float n = nodeCount -1;
196  float binCoef = 1;
197  while(tmpNode)
198    {
199      tmpNode->factor = binCoef;
200      if (tmpNode =tmpNode->next)
201        {
202          binCoef *=(n-k)/(k+1);
203          ++k;
204        }
205    }
206
207  // rebuilding the Derivation curve
208  if(this->derivation <= 1)
209    {
210      tmpNode = firstNode;
211      delete dirCurve;
212      dirCurve = new BezierCurve(1);
213      while(tmpNode->next)
214        {
215          Vector tmpVector = (tmpNode->next->position)- (tmpNode->position);
216          tmpVector.x*=(float)nodeCount;
217          tmpVector.y*=(float)nodeCount;
218          tmpVector.z*=(float)nodeCount;
219          tmpVector.normalize();
220          this->dirCurve->addNode(tmpVector);
221          tmpNode = tmpNode->next;
222        }
223    }
224}
225
226/**
227 *  calculates the Position on the curve
228 * @param t The position on the Curve (0<=t<=1)
229 * @return the Position on the Path
230*/
231Vector BezierCurve::calcPos(float t) 
232{
233  Vector ret = Vector(0.0,0.0,0.0);
234  if (this->nodeCount >= 3)
235    {
236      PathNode* tmpNode = this->firstNode;
237      double factor = pow(1.0-t,nodeCount-1);
238      while(tmpNode)
239        {
240          ret.x += tmpNode->factor * factor * tmpNode->position.x;
241          ret.y += tmpNode->factor * factor * tmpNode->position.y;
242          ret.z += tmpNode->factor * factor * tmpNode->position.z;
243          factor *= t/(1.0-t); // same as pow but much faster.
244         
245          tmpNode = tmpNode->next;
246        }
247    }
248  else if (nodeCount == 2)
249    {
250      ret = this->firstNode->position *(1.0-t);
251      ret = ret + this->firstNode->next->position * t;
252    }
253  else if (nodeCount == 1)
254    ret = this->firstNode->position;
255  return ret;
256}
257
258/**
259 *  Calulates the direction of the Curve at time t.
260 * @param t The time at which to evaluate the curve.
261 * @returns The valuated Vector.
262*/
263Vector BezierCurve::calcDir (float t)
264{
265  return this->dirCurve->calcPos(t);
266}
267
268/**
269 *  Calulates the acceleration (second derivate) of the Curve at time t.
270 * @param t The time at which to evaluate the curve.
271 * @returns The valuated Vector.
272*/
273Vector BezierCurve::calcAcc (float t)
274{
275  return this->dirCurve->getDirCurve()->calcPos(t);
276}
277
278/**
279 *  Calculates the Quaternion needed for our rotations
280 * @param t The time at which to evaluate the cuve.
281 * @returns The evaluated Quaternion.
282*/
283Quaternion BezierCurve::calcQuat (float t)
284{
285  return Quaternion (calcDir(t), Vector(0,0,1));
286}
287
288
289/**
290  \brief returns the Position of the point calculated on the Curve
291  \return a Vector to the calculated position
292*/
293Vector BezierCurve::getPos() const
294{
295  return curvePoint;
296}
Note: See TracBrowser for help on using the repository browser.