Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/old.bezierTrack/src/curve.cc @ 3500

Last change on this file since 3500 was 3028, checked in by bensch, 20 years ago

orxonox/branches/bezierTrack: now Camera follows Path. heavy cleanUp of not used stuff like elyptical Camera and so on…

File size: 3.3 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: ...
14
15   TODO:
16     local-Time implementation
17     closed Bezier Curve
18     NURBS
19     
20*/
21
22#include "curve.h"
23
24
25
26/**
27   \brief adds a new Node to the bezier Curve
28   \param newNode a Vector to the position of the new node
29*/
30void Curve::addNode(const Vector& newNode)
31{
32  if (nodeCount != 0 )
33    {
34      currentNode = currentNode->next = new PathNode;
35    }
36  currentNode->position = newNode;
37  currentNode->next = 0; // not sure if this really points to NULL!!
38  currentNode->number = (++nodeCount);
39  return;
40}
41
42
43/**
44   \brief Creates a new BezierCurve
45*/
46BezierCurve::BezierCurve (void)
47{
48  nodeCount = 0;
49  firstNode = new PathNode;
50  currentNode = firstNode;
51
52  firstNode->position = Vector (.0, .0, .0);
53  firstNode->number = 0;
54  firstNode->next = 0; // not sure if this really points to NULL!!
55
56  return;
57}
58
59/**
60   \brief Deletes a BezierCurve.
61   It does this by freeing all the space taken over from the nodes
62*/
63BezierCurve::~BezierCurve (void)
64{
65  PathNode* tmpNode;
66  currentNode = firstNode;
67  while (tmpNode != 0)
68    {
69      tmpNode = currentNode;
70      currentNode = currentNode->next;
71      delete tmpNode;
72    }
73}
74
75/**
76   \brief calculates the Position on the curve
77   \param t The position on the Curve (0<=t<=1)
78   \return the Position on the Path
79*/
80Vector BezierCurve::calcPos(float t) 
81{
82  if (nodeCount <=4)
83    {
84      //      if (verbose >= 1)
85      //      printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount);
86      return Vector(0,0,0);
87    }
88  PathNode* tmpNode = firstNode;
89   
90  Vector ret = Vector(0.0,0.0,0.0);
91  double factor;
92  int k=0;
93  while(tmpNode!=0)
94    {
95      k++;
96      factor = ncr (nodeCount, k);
97     
98      for (int j=0; j<nodeCount-k; j++)
99        factor*=(1-t);
100      for (int j=0; j<k; j++)
101        factor*=t;
102      ret.x += factor * tmpNode->position.x;
103      ret.y += factor * tmpNode->position.y;
104      ret.z += factor * tmpNode->position.z;
105     
106      tmpNode = tmpNode->next;
107
108    }
109  return ret;
110}
111
112Vector BezierCurve::calcDir (float t)
113{
114  PathNode* tmpNode = firstNode;
115  BezierCurve* tmpCurve = new BezierCurve();
116  Vector ret;
117  Vector tmpVector;
118
119  while (tmpNode->next != 0)
120    {
121      tmpVector = (tmpNode->next->position)- (tmpNode->position);
122      tmpVector.x*=(float)nodeCount;
123      tmpVector.y*=(float)nodeCount;
124      tmpVector.z*=(float)nodeCount;
125
126      tmpCurve->addNode(tmpVector);
127      tmpNode = tmpNode->next;
128    }
129  ret = tmpCurve->calcPos(t);
130  ret.normalize();
131
132  return ret;
133}
134
135Quaternion BezierCurve::calcQuat (float t)
136{
137  return Quaternion (calcDir(t), Vector(0,0,1));
138}
139
140
141/**
142  \brief returns the Position of the point calculated on the Curve
143  \return a Vector to the calculated position
144*/
145Vector BezierCurve::getPos() const
146{
147  return curvePoint;
148}
149
150
151int ncr(int n, int i)
152{
153  int ret = 1;
154  for (int k=1; k<=n; k++)
155    ret*=k;
156  for (int k=1; k<=i; k++)
157    ret/=k;
158  for (int k=1; k<=n-i; k++)
159    ret/=k;
160
161  return ret;
162}
163
Note: See TracBrowser for help on using the repository browser.