Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4878 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
RevLine 
[3018]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
[3365]13   co-programmer: Patrick Boenzli
[3023]14
[3365]15   ADD: Patrick Boenzli           B-Spline
16
17
[3023]18   TODO:
19     local-Time implementation
20     NURBS
[3593]21     tList implementation
[3023]22     
[3018]23*/
24
[3593]25#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
26
[3018]27#include "curve.h"
[3595]28
[3365]29#include "debug.h"
[3018]30
[3365]31#include <math.h>
32#include <stdio.h>
[3019]33
[3588]34
35/**
[4836]36  *  default constructor for a Curve
[3588]37*/
[4746]38Curve::Curve()
[3588]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
[3018]49/**
[4836]50 *  adds a new Node to the bezier Curve
51 * @param newNode a Vector to the position of the new node
[3019]52*/
53void Curve::addNode(const Vector& newNode)
54{
55  if (nodeCount != 0 )
56    {
57      currentNode = currentNode->next = new PathNode;
58    }
59  currentNode->position = newNode;
[3433]60  currentNode->next = 0;
[3019]61  currentNode->number = (++nodeCount);
[3365]62  this->rebuild();
[3019]63  return;
64}
65
[4472]66/**
[4836]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
[4472]70*/
[3433]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
[3365]112/**
[4836]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.
[3365]116*/
117Vector Curve::getNode(unsigned int nodeToFind)
118{
[3433]119  if (nodeToFind > this->nodeCount || nodeToFind < 0)
[3365]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}
[3019]126
[3433]127/**
[4836]128 *  Outputs information about the state of this Curve
[3433]129*/
[4746]130void Curve::debug()
[3433]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
[3365]145///////////////////////////////////
146/// Bezier Curve //////////////////
147///////////////////////////////////
148
[3019]149/**
[4836]150 *  Creates a new BezierCurve
[3018]151*/
[4746]152BezierCurve::BezierCurve ()
[3018]153{
[3365]154  this->derivation = 0;
155  dirCurve = new BezierCurve(1);
156}
157
158/**
[4836]159 *  Creates a new BezierCurve-Derivation-Curve
[3365]160*/
161BezierCurve::BezierCurve (int derivation)
162{
163  this->derivation = derivation;
164  dirCurve=NULL;
165}
166
167/**
[4836]168 *  Deletes a BezierCurve.
[3365]169
170   It does this by freeing all the space taken over from the nodes
171*/
[4746]172BezierCurve::~BezierCurve()
[3365]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/**
[4836]187 *  Rebuilds a Curve
[3365]188*/
[4746]189void BezierCurve::rebuild()
[3365]190{
191  PathNode* tmpNode = firstNode;
[3217]192
[3365]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
[3433]208  if(this->derivation <= 1)
[3365]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/**
[4836]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
[3365]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/**
[4836]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.
[3365]262*/
263Vector BezierCurve::calcDir (float t)
264{
[4472]265  return this->dirCurve->calcPos(t);
[3365]266}
267
[4472]268/**
[4836]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.
[4472]272*/
[3433]273Vector BezierCurve::calcAcc (float t)
274{
[4472]275  return this->dirCurve->getDirCurve()->calcPos(t);
[3433]276}
277
[3365]278/**
[4836]279 *  Calculates the Quaternion needed for our rotations
280 * @param t The time at which to evaluate the cuve.
281 * @returns The evaluated Quaternion.
[3365]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*/
[4746]293Vector BezierCurve::getPos() const
[3365]294{
295  return curvePoint;
296}
Note: See TracBrowser for help on using the repository browser.