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 | #include <math.h> |
---|
27 | #include <stdio.h> |
---|
28 | |
---|
29 | /** |
---|
30 | \brief adds a new Node to the bezier Curve |
---|
31 | \param newNode a Vector to the position of the new node |
---|
32 | */ |
---|
33 | void Curve::addNode(const Vector& newNode) |
---|
34 | { |
---|
35 | if (nodeCount != 0 ) |
---|
36 | { |
---|
37 | currentNode = currentNode->next = new PathNode; |
---|
38 | } |
---|
39 | currentNode->position = newNode; |
---|
40 | currentNode->next = 0; // not sure if this really points to NULL!! |
---|
41 | currentNode->number = (++nodeCount); |
---|
42 | this->rebuild(); |
---|
43 | return; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | \brief Creates a new BezierCurve |
---|
49 | */ |
---|
50 | BezierCurve::BezierCurve (void) |
---|
51 | { |
---|
52 | this->derivation = 0; |
---|
53 | dirCurve = new BezierCurve(1); |
---|
54 | this->init(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | \brief Creates a new BezierCurve-Derivation-Curve |
---|
59 | */ |
---|
60 | BezierCurve::BezierCurve (int derivation) |
---|
61 | { |
---|
62 | this->derivation = derivation; |
---|
63 | dirCurve=NULL; |
---|
64 | this->init(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | \brief Deletes a BezierCurve. |
---|
69 | |
---|
70 | It does this by freeing all the space taken over from the nodes |
---|
71 | */ |
---|
72 | BezierCurve::~BezierCurve (void) |
---|
73 | { |
---|
74 | PathNode* tmpNode; |
---|
75 | currentNode = firstNode; |
---|
76 | while (tmpNode != 0) |
---|
77 | { |
---|
78 | tmpNode = currentNode; |
---|
79 | currentNode = currentNode->next; |
---|
80 | delete tmpNode; |
---|
81 | } |
---|
82 | if (dirCurve) |
---|
83 | delete dirCurve; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | \brief Initializes a BezierCurve |
---|
88 | */ |
---|
89 | void BezierCurve::init(void) |
---|
90 | { |
---|
91 | nodeCount = 0; |
---|
92 | firstNode = new PathNode; |
---|
93 | currentNode = firstNode; |
---|
94 | |
---|
95 | firstNode->position = Vector (.0, .0, .0); |
---|
96 | firstNode->number = 0; |
---|
97 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
98 | |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | \brief Rebuilds a Curve |
---|
104 | */ |
---|
105 | void BezierCurve::rebuild(void) |
---|
106 | { |
---|
107 | PathNode* tmpNode = firstNode; |
---|
108 | |
---|
109 | // rebuilding the Curve itself |
---|
110 | int k=0; |
---|
111 | int binCoef = 1; |
---|
112 | while(tmpNode) |
---|
113 | { |
---|
114 | if (k+1 < nodeCount-k) |
---|
115 | binCoef *=(nodeCount-k)/(k+1); |
---|
116 | else |
---|
117 | binCoef /= (k+1)/(nodeCount-k); |
---|
118 | ++k; |
---|
119 | tmpNode->factor = binCoef; |
---|
120 | tmpNode = tmpNode->next; |
---|
121 | } |
---|
122 | |
---|
123 | // rebuilding the Derivation curve |
---|
124 | if(this->derivation == 0) |
---|
125 | { |
---|
126 | tmpNode = firstNode; |
---|
127 | delete dirCurve; |
---|
128 | dirCurve = new BezierCurve(1); |
---|
129 | while(tmpNode->next) |
---|
130 | { |
---|
131 | Vector tmpVector = (tmpNode->next->position)- (tmpNode->position); |
---|
132 | tmpVector.x*=(float)nodeCount; |
---|
133 | tmpVector.y*=(float)nodeCount; |
---|
134 | tmpVector.z*=(float)nodeCount; |
---|
135 | tmpVector.normalize(); |
---|
136 | this->dirCurve->addNode(tmpVector); |
---|
137 | tmpNode = tmpNode->next; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief calculates the Position on the curve |
---|
144 | \param t The position on the Curve (0<=t<=1) |
---|
145 | \return the Position on the Path |
---|
146 | */ |
---|
147 | Vector BezierCurve::calcPos(float t) |
---|
148 | { |
---|
149 | if (nodeCount <=4) |
---|
150 | { |
---|
151 | // if (verbose >= 1) |
---|
152 | // printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount); |
---|
153 | return Vector(0,0,0); |
---|
154 | } |
---|
155 | PathNode* tmpNode = firstNode; |
---|
156 | Vector ret = Vector(0.0,0.0,0.0); |
---|
157 | float factor = 1.0*pow(1.0-t,nodeCount); |
---|
158 | while(tmpNode) |
---|
159 | { |
---|
160 | factor *= t/(1.0-t); // same as pow but much faster. |
---|
161 | ret.x += tmpNode->factor * factor * tmpNode->position.x; |
---|
162 | ret.y += tmpNode->factor * factor * tmpNode->position.y; |
---|
163 | ret.z += tmpNode->factor * factor * tmpNode->position.z; |
---|
164 | |
---|
165 | tmpNode = tmpNode->next; |
---|
166 | } |
---|
167 | return ret; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | \brief Calulates the direction of the Curve at time t. |
---|
172 | \param The time at which to evaluate the curve. |
---|
173 | \returns The vvaluated Vector. |
---|
174 | */ |
---|
175 | Vector BezierCurve::calcDir (float t) |
---|
176 | { |
---|
177 | return dirCurve->calcPos(t); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | \brief Calculates the Quaternion needed for our rotations |
---|
182 | \param t The time at which to evaluate the cuve. |
---|
183 | \returns The evaluated Quaternion. |
---|
184 | */ |
---|
185 | Quaternion BezierCurve::calcQuat (float t) |
---|
186 | { |
---|
187 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /** |
---|
192 | \brief returns the Position of the point calculated on the Curve |
---|
193 | \return a Vector to the calculated position |
---|
194 | */ |
---|
195 | Vector BezierCurve::getPos(void) const |
---|
196 | { |
---|
197 | return curvePoint; |
---|
198 | } |
---|