[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 |
---|
[3311] | 13 | co-programmer: Patrick Boenzli |
---|
[3023] | 14 | |
---|
[3311] | 15 | ADD: Patrick Boenzli B-Spline |
---|
| 16 | |
---|
| 17 | |
---|
[3023] | 18 | TODO: |
---|
| 19 | local-Time implementation |
---|
| 20 | NURBS |
---|
| 21 | |
---|
[3018] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include "curve.h" |
---|
[3327] | 25 | #include "matrix.h" |
---|
[3018] | 26 | |
---|
[3320] | 27 | #include <math.h> |
---|
| 28 | #include <stdio.h> |
---|
[3019] | 29 | |
---|
[3018] | 30 | /** |
---|
[3019] | 31 | \brief adds a new Node to the bezier Curve |
---|
| 32 | \param newNode a Vector to the position of the new node |
---|
| 33 | */ |
---|
| 34 | void Curve::addNode(const Vector& newNode) |
---|
| 35 | { |
---|
| 36 | if (nodeCount != 0 ) |
---|
| 37 | { |
---|
| 38 | currentNode = currentNode->next = new PathNode; |
---|
| 39 | } |
---|
| 40 | currentNode->position = newNode; |
---|
| 41 | currentNode->next = 0; // not sure if this really points to NULL!! |
---|
| 42 | currentNode->number = (++nodeCount); |
---|
[3320] | 43 | this->rebuild(); |
---|
[3019] | 44 | return; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
[3327] | 48 | /////////////////////////////////// |
---|
| 49 | /// Bezier Curve ////////////////// |
---|
| 50 | /////////////////////////////////// |
---|
| 51 | |
---|
[3019] | 52 | /** |
---|
[3018] | 53 | \brief Creates a new BezierCurve |
---|
| 54 | */ |
---|
| 55 | BezierCurve::BezierCurve (void) |
---|
| 56 | { |
---|
[3321] | 57 | this->derivation = 0; |
---|
| 58 | dirCurve = new BezierCurve(1); |
---|
| 59 | this->init(); |
---|
| 60 | } |
---|
[3018] | 61 | |
---|
[3321] | 62 | /** |
---|
| 63 | \brief Creates a new BezierCurve-Derivation-Curve |
---|
| 64 | */ |
---|
| 65 | BezierCurve::BezierCurve (int derivation) |
---|
| 66 | { |
---|
| 67 | this->derivation = derivation; |
---|
| 68 | dirCurve=NULL; |
---|
| 69 | this->init(); |
---|
[3018] | 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | \brief Deletes a BezierCurve. |
---|
[3217] | 74 | |
---|
[3018] | 75 | It does this by freeing all the space taken over from the nodes |
---|
| 76 | */ |
---|
[3327] | 77 | BezierCurve::~BezierCurve(void) |
---|
[3018] | 78 | { |
---|
| 79 | PathNode* tmpNode; |
---|
| 80 | currentNode = firstNode; |
---|
| 81 | while (tmpNode != 0) |
---|
| 82 | { |
---|
| 83 | tmpNode = currentNode; |
---|
| 84 | currentNode = currentNode->next; |
---|
| 85 | delete tmpNode; |
---|
| 86 | } |
---|
[3321] | 87 | if (dirCurve) |
---|
| 88 | delete dirCurve; |
---|
[3018] | 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
[3321] | 92 | \brief Initializes a BezierCurve |
---|
| 93 | */ |
---|
| 94 | void BezierCurve::init(void) |
---|
| 95 | { |
---|
| 96 | nodeCount = 0; |
---|
| 97 | firstNode = new PathNode; |
---|
| 98 | currentNode = firstNode; |
---|
| 99 | |
---|
| 100 | firstNode->position = Vector (.0, .0, .0); |
---|
| 101 | firstNode->number = 0; |
---|
| 102 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
| 103 | |
---|
| 104 | return; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
[3320] | 108 | \brief Rebuilds a Curve |
---|
| 109 | */ |
---|
| 110 | void BezierCurve::rebuild(void) |
---|
| 111 | { |
---|
| 112 | PathNode* tmpNode = firstNode; |
---|
| 113 | |
---|
[3321] | 114 | // rebuilding the Curve itself |
---|
[3320] | 115 | int k=0; |
---|
| 116 | int binCoef = 1; |
---|
| 117 | while(tmpNode) |
---|
| 118 | { |
---|
| 119 | if (k+1 < nodeCount-k) |
---|
| 120 | binCoef *=(nodeCount-k)/(k+1); |
---|
| 121 | else |
---|
| 122 | binCoef /= (k+1)/(nodeCount-k); |
---|
| 123 | ++k; |
---|
| 124 | tmpNode->factor = binCoef; |
---|
| 125 | tmpNode = tmpNode->next; |
---|
| 126 | } |
---|
[3321] | 127 | |
---|
| 128 | // rebuilding the Derivation curve |
---|
| 129 | if(this->derivation == 0) |
---|
| 130 | { |
---|
| 131 | tmpNode = firstNode; |
---|
| 132 | delete dirCurve; |
---|
| 133 | dirCurve = new BezierCurve(1); |
---|
| 134 | while(tmpNode->next) |
---|
| 135 | { |
---|
| 136 | Vector tmpVector = (tmpNode->next->position)- (tmpNode->position); |
---|
| 137 | tmpVector.x*=(float)nodeCount; |
---|
| 138 | tmpVector.y*=(float)nodeCount; |
---|
| 139 | tmpVector.z*=(float)nodeCount; |
---|
| 140 | tmpVector.normalize(); |
---|
| 141 | this->dirCurve->addNode(tmpVector); |
---|
| 142 | tmpNode = tmpNode->next; |
---|
| 143 | } |
---|
| 144 | } |
---|
[3320] | 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
[3018] | 148 | \brief calculates the Position on the curve |
---|
| 149 | \param t The position on the Curve (0<=t<=1) |
---|
| 150 | \return the Position on the Path |
---|
| 151 | */ |
---|
| 152 | Vector BezierCurve::calcPos(float t) |
---|
| 153 | { |
---|
| 154 | if (nodeCount <=4) |
---|
| 155 | { |
---|
[3320] | 156 | // if (verbose >= 1) |
---|
[3018] | 157 | // printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount); |
---|
| 158 | return Vector(0,0,0); |
---|
| 159 | } |
---|
| 160 | PathNode* tmpNode = firstNode; |
---|
| 161 | Vector ret = Vector(0.0,0.0,0.0); |
---|
[3320] | 162 | float factor = 1.0*pow(1.0-t,nodeCount); |
---|
| 163 | while(tmpNode) |
---|
[3018] | 164 | { |
---|
[3320] | 165 | factor *= t/(1.0-t); // same as pow but much faster. |
---|
| 166 | ret.x += tmpNode->factor * factor * tmpNode->position.x; |
---|
| 167 | ret.y += tmpNode->factor * factor * tmpNode->position.y; |
---|
| 168 | ret.z += tmpNode->factor * factor * tmpNode->position.z; |
---|
| 169 | |
---|
[3018] | 170 | tmpNode = tmpNode->next; |
---|
| 171 | } |
---|
| 172 | return ret; |
---|
| 173 | } |
---|
| 174 | |
---|
[3217] | 175 | /** |
---|
| 176 | \brief Calulates the direction of the Curve at time t. |
---|
| 177 | \param The time at which to evaluate the curve. |
---|
| 178 | \returns The vvaluated Vector. |
---|
| 179 | */ |
---|
[3018] | 180 | Vector BezierCurve::calcDir (float t) |
---|
| 181 | { |
---|
[3322] | 182 | return dirCurve->calcPos(t); |
---|
[3018] | 183 | } |
---|
| 184 | |
---|
[3217] | 185 | /** |
---|
| 186 | \brief Calculates the Quaternion needed for our rotations |
---|
| 187 | \param t The time at which to evaluate the cuve. |
---|
| 188 | \returns The evaluated Quaternion. |
---|
| 189 | */ |
---|
[3028] | 190 | Quaternion BezierCurve::calcQuat (float t) |
---|
| 191 | { |
---|
| 192 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
[3018] | 196 | /** |
---|
| 197 | \brief returns the Position of the point calculated on the Curve |
---|
| 198 | \return a Vector to the calculated position |
---|
| 199 | */ |
---|
[3319] | 200 | Vector BezierCurve::getPos(void) const |
---|
[3018] | 201 | { |
---|
| 202 | return curvePoint; |
---|
| 203 | } |
---|
[3327] | 204 | |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | /////////////////////////////////// |
---|
| 208 | //// Uniform Point curve ///////// |
---|
| 209 | /////////////////////////////////// |
---|
| 210 | /** |
---|
| 211 | \brief Creates a new UPointCurve |
---|
| 212 | */ |
---|
| 213 | UPointCurve::UPointCurve (void) |
---|
| 214 | { |
---|
| 215 | this->derivation = 0; |
---|
| 216 | this->init(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | \brief Creates a new UPointCurve-Derivation-Curve of deriavation'th degree |
---|
| 221 | */ |
---|
| 222 | UPointCurve::UPointCurve (int derivation) |
---|
| 223 | { |
---|
| 224 | this->derivation = derivation; |
---|
| 225 | dirCurve=NULL; |
---|
| 226 | this->init(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | \brief Deletes a UPointCurve. |
---|
| 231 | |
---|
| 232 | It does this by freeing all the space taken over from the nodes |
---|
| 233 | */ |
---|
| 234 | UPointCurve::~UPointCurve(void) |
---|
| 235 | { |
---|
| 236 | PathNode* tmpNode; |
---|
| 237 | currentNode = firstNode; |
---|
| 238 | while (tmpNode != 0) |
---|
| 239 | { |
---|
| 240 | tmpNode = currentNode; |
---|
| 241 | currentNode = currentNode->next; |
---|
| 242 | delete tmpNode; |
---|
| 243 | } |
---|
| 244 | if (dirCurve) |
---|
| 245 | delete dirCurve; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | \brief Initializes a UPointCurve |
---|
| 250 | */ |
---|
| 251 | void UPointCurve::init(void) |
---|
| 252 | { |
---|
| 253 | nodeCount = 0; |
---|
| 254 | firstNode = new PathNode; |
---|
| 255 | currentNode = firstNode; |
---|
| 256 | |
---|
| 257 | firstNode->position = Vector (.0, .0, .0); |
---|
| 258 | firstNode->number = 0; |
---|
| 259 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
| 260 | |
---|
| 261 | return; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | \brief Rebuilds a UPointCurve |
---|
| 266 | |
---|
| 267 | \todo very bad algorithm |
---|
| 268 | */ |
---|
| 269 | void UPointCurve::rebuild(void) |
---|
| 270 | { |
---|
| 271 | // rebuilding the Curve itself |
---|
| 272 | PathNode* tmpNode = this->firstNode; |
---|
| 273 | int i=0; |
---|
| 274 | Matrix xTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 275 | Matrix yTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 276 | Matrix zTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 277 | Matrix xValMat = Matrix(this->nodeCount, 3); |
---|
| 278 | Matrix yValMat = Matrix(this->nodeCount, 3); |
---|
| 279 | Matrix zValMat = Matrix(this->nodeCount, 3); |
---|
| 280 | while(tmpNode) |
---|
| 281 | { |
---|
| 282 | Vector fac = Vector(1,1,1); |
---|
| 283 | for (int j = 0; j < this->nodeCount; j++) |
---|
| 284 | { |
---|
| 285 | xTmpMat(i,j) = fac.x; fac.x *= (float)i/(float)this->nodeCount;//tmpNode->position.x; |
---|
| 286 | yTmpMat(i,j) = fac.y; fac.y *= (float)i/(float)this->nodeCount;//tmpNode->position.y; |
---|
| 287 | zTmpMat(i,j) = fac.z; fac.z *= (float)i/(float)this->nodeCount;//tmpNode->position.z; |
---|
| 288 | } |
---|
| 289 | xValMat(i,0) = tmpNode->position.x; |
---|
| 290 | yValMat(i,0) = tmpNode->position.y; |
---|
| 291 | zValMat(i,0) = tmpNode->position.z; |
---|
| 292 | ++i; |
---|
| 293 | tmpNode = tmpNode->next; |
---|
| 294 | } |
---|
| 295 | tmpNode = this->firstNode; |
---|
| 296 | xValMat = xTmpMat.Inv() *= xValMat; |
---|
| 297 | yValMat = yTmpMat.Inv() *= yValMat; |
---|
| 298 | zValMat = zTmpMat.Inv() *= zValMat; |
---|
| 299 | i = 0; |
---|
| 300 | while(tmpNode) |
---|
| 301 | { |
---|
| 302 | tmpNode->vFactor.x = xValMat(i,0); |
---|
| 303 | tmpNode->vFactor.y = yValMat(i,0); |
---|
| 304 | tmpNode->vFactor.z = zValMat(i,0); |
---|
| 305 | |
---|
| 306 | i++; |
---|
| 307 | tmpNode = tmpNode->next; |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | \brief calculates the Position on the curve |
---|
| 313 | \param t The position on the Curve (0<=t<=1) |
---|
| 314 | \return the Position on the Path |
---|
| 315 | */ |
---|
| 316 | Vector UPointCurve::calcPos(float t) |
---|
| 317 | { |
---|
| 318 | PathNode* tmpNode = firstNode; |
---|
| 319 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 320 | float factor = 1.0; |
---|
| 321 | while(tmpNode) |
---|
| 322 | { |
---|
| 323 | ret.x += tmpNode->vFactor.x * factor; |
---|
| 324 | ret.y += tmpNode->vFactor.y * factor; |
---|
| 325 | ret.z += tmpNode->vFactor.z * factor; |
---|
| 326 | factor *= t; |
---|
| 327 | |
---|
| 328 | tmpNode = tmpNode->next; |
---|
| 329 | } |
---|
| 330 | return ret; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | \brief Calulates the direction of the Curve at time t. |
---|
| 335 | \param The time at which to evaluate the curve. |
---|
| 336 | \returns The vvaluated Vector. |
---|
| 337 | */ |
---|
| 338 | Vector UPointCurve::calcDir (float t) |
---|
| 339 | { |
---|
[3328] | 340 | PathNode* tmpNode = firstNode; |
---|
| 341 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 342 | float factor = 1.0/t; |
---|
| 343 | int k=0; |
---|
| 344 | while(tmpNode) |
---|
| 345 | { |
---|
| 346 | ret.x += tmpNode->vFactor.x * factor *k; |
---|
| 347 | ret.y += tmpNode->vFactor.y * factor *k; |
---|
| 348 | ret.z += tmpNode->vFactor.z * factor *k; |
---|
| 349 | factor *= t; |
---|
| 350 | k++; |
---|
| 351 | tmpNode = tmpNode->next; |
---|
| 352 | } |
---|
| 353 | ret.normalize(); |
---|
| 354 | return ret; |
---|
[3327] | 355 | } |
---|
| 356 | |
---|
| 357 | /** |
---|
| 358 | \brief Calculates the Quaternion needed for our rotations |
---|
| 359 | \param t The time at which to evaluate the cuve. |
---|
| 360 | \returns The evaluated Quaternion. |
---|
| 361 | */ |
---|
| 362 | Quaternion UPointCurve::calcQuat (float t) |
---|
| 363 | { |
---|
| 364 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | \brief returns the Position of the point calculated on the Curve |
---|
| 370 | \return a Vector to the calculated position |
---|
| 371 | */ |
---|
| 372 | Vector UPointCurve::getPos(void) const |
---|
| 373 | { |
---|
| 374 | return curvePoint; |
---|
| 375 | } |
---|