[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 |
---|
| 21 | |
---|
[3018] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include "curve.h" |
---|
[3365] | 25 | #include "matrix.h" |
---|
| 26 | #include "debug.h" |
---|
[3018] | 27 | |
---|
[3365] | 28 | #include <math.h> |
---|
| 29 | #include <stdio.h> |
---|
[3019] | 30 | |
---|
[3588] | 31 | |
---|
| 32 | /** |
---|
| 33 | \brief default constructor for a Curve |
---|
| 34 | */ |
---|
| 35 | Curve::Curve(void) |
---|
| 36 | { |
---|
| 37 | nodeCount = 0; |
---|
| 38 | firstNode = new PathNode; |
---|
| 39 | currentNode = firstNode; |
---|
| 40 | |
---|
| 41 | firstNode->position = Vector (.0, .0, .0); |
---|
| 42 | firstNode->number = 0; |
---|
| 43 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
| 44 | } |
---|
| 45 | |
---|
[3018] | 46 | /** |
---|
[3019] | 47 | \brief adds a new Node to the bezier Curve |
---|
| 48 | \param newNode a Vector to the position of the new node |
---|
| 49 | */ |
---|
| 50 | void Curve::addNode(const Vector& newNode) |
---|
| 51 | { |
---|
| 52 | if (nodeCount != 0 ) |
---|
| 53 | { |
---|
| 54 | currentNode = currentNode->next = new PathNode; |
---|
| 55 | } |
---|
| 56 | currentNode->position = newNode; |
---|
[3433] | 57 | currentNode->next = 0; |
---|
[3019] | 58 | currentNode->number = (++nodeCount); |
---|
[3365] | 59 | this->rebuild(); |
---|
[3019] | 60 | return; |
---|
| 61 | } |
---|
| 62 | |
---|
[3433] | 63 | void Curve::addNode(const Vector& newNode, unsigned int insertPosition) |
---|
| 64 | { |
---|
| 65 | |
---|
| 66 | if (this->nodeCount == 0 || insertPosition > this->nodeCount) |
---|
| 67 | return addNode(newNode); |
---|
| 68 | |
---|
| 69 | if (insertPosition == 0) |
---|
| 70 | insertPosition = 1; |
---|
| 71 | |
---|
| 72 | PathNode* insNode = new PathNode; |
---|
| 73 | |
---|
| 74 | // relinking |
---|
| 75 | PathNode* tmpNode = this->firstNode; |
---|
| 76 | if (insertPosition > 1) |
---|
| 77 | { |
---|
| 78 | while (tmpNode->next->number != insertPosition) |
---|
| 79 | tmpNode= tmpNode->next; |
---|
| 80 | insNode->next = tmpNode->next; |
---|
| 81 | tmpNode->next = insNode; |
---|
| 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | insNode->next = this->firstNode; |
---|
| 86 | this->firstNode = insNode; |
---|
| 87 | } |
---|
| 88 | // renumbering |
---|
| 89 | insNode->number = insertPosition; |
---|
| 90 | tmpNode = insNode->next; |
---|
| 91 | while (tmpNode) |
---|
| 92 | { |
---|
| 93 | tmpNode->number++; |
---|
| 94 | tmpNode = tmpNode->next; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // finished |
---|
| 98 | insNode->position = newNode; |
---|
| 99 | ++nodeCount; |
---|
| 100 | this->rebuild(); |
---|
| 101 | return; |
---|
| 102 | } |
---|
| 103 | |
---|
[3365] | 104 | /** |
---|
| 105 | \brief Finds a Node by its Number, and returns its Position |
---|
| 106 | \param nodeToFind the n'th node in the List of nodes |
---|
| 107 | \returns A Vector to the Position of the Node. |
---|
| 108 | */ |
---|
| 109 | Vector Curve::getNode(unsigned int nodeToFind) |
---|
| 110 | { |
---|
[3433] | 111 | if (nodeToFind > this->nodeCount || nodeToFind < 0) |
---|
[3365] | 112 | return Vector(0,0,0); |
---|
| 113 | PathNode* tmpNode = this->firstNode; |
---|
| 114 | for (int i = 1; i < nodeToFind; i++) |
---|
| 115 | tmpNode = tmpNode->next; |
---|
| 116 | return tmpNode->position; |
---|
| 117 | } |
---|
[3019] | 118 | |
---|
[3433] | 119 | /** |
---|
| 120 | \brief Outputs information about the state of this Curve |
---|
| 121 | */ |
---|
| 122 | void Curve::debug(void) |
---|
| 123 | { |
---|
| 124 | printf("<<-------------------------------\n"); |
---|
| 125 | printf("Curve Information:\n"); |
---|
| 126 | printf("NodeCount: %d\n", this->nodeCount); |
---|
| 127 | PathNode* tmpNode = this->firstNode; |
---|
| 128 | while (tmpNode) |
---|
| 129 | { |
---|
| 130 | printf("node #%d: %f, %f, %f\n", tmpNode->number, tmpNode->position.x, tmpNode->position.y, tmpNode->position.z); |
---|
| 131 | tmpNode = tmpNode->next; |
---|
| 132 | } |
---|
| 133 | printf("------------------------------->>\n"); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
[3365] | 137 | /////////////////////////////////// |
---|
| 138 | /// Bezier Curve ////////////////// |
---|
| 139 | /////////////////////////////////// |
---|
| 140 | |
---|
[3019] | 141 | /** |
---|
[3018] | 142 | \brief Creates a new BezierCurve |
---|
| 143 | */ |
---|
| 144 | BezierCurve::BezierCurve (void) |
---|
| 145 | { |
---|
[3365] | 146 | this->derivation = 0; |
---|
| 147 | dirCurve = new BezierCurve(1); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | \brief Creates a new BezierCurve-Derivation-Curve |
---|
| 152 | */ |
---|
| 153 | BezierCurve::BezierCurve (int derivation) |
---|
| 154 | { |
---|
| 155 | this->derivation = derivation; |
---|
| 156 | dirCurve=NULL; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | \brief Deletes a BezierCurve. |
---|
| 161 | |
---|
| 162 | It does this by freeing all the space taken over from the nodes |
---|
| 163 | */ |
---|
| 164 | BezierCurve::~BezierCurve(void) |
---|
| 165 | { |
---|
| 166 | PathNode* tmpNode; |
---|
| 167 | currentNode = firstNode; |
---|
| 168 | while (tmpNode != 0) |
---|
| 169 | { |
---|
| 170 | tmpNode = currentNode; |
---|
| 171 | currentNode = currentNode->next; |
---|
| 172 | delete tmpNode; |
---|
| 173 | } |
---|
| 174 | if (dirCurve) |
---|
| 175 | delete dirCurve; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | \brief Rebuilds a Curve |
---|
| 180 | */ |
---|
| 181 | void BezierCurve::rebuild(void) |
---|
| 182 | { |
---|
| 183 | PathNode* tmpNode = firstNode; |
---|
[3217] | 184 | |
---|
[3365] | 185 | // rebuilding the Curve itself |
---|
| 186 | float k=0; |
---|
| 187 | float n = nodeCount -1; |
---|
| 188 | float binCoef = 1; |
---|
| 189 | while(tmpNode) |
---|
| 190 | { |
---|
| 191 | tmpNode->factor = binCoef; |
---|
| 192 | if (tmpNode =tmpNode->next) |
---|
| 193 | { |
---|
| 194 | binCoef *=(n-k)/(k+1); |
---|
| 195 | ++k; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // rebuilding the Derivation curve |
---|
[3433] | 200 | if(this->derivation <= 1) |
---|
[3365] | 201 | { |
---|
| 202 | tmpNode = firstNode; |
---|
| 203 | delete dirCurve; |
---|
| 204 | dirCurve = new BezierCurve(1); |
---|
| 205 | while(tmpNode->next) |
---|
| 206 | { |
---|
| 207 | Vector tmpVector = (tmpNode->next->position)- (tmpNode->position); |
---|
| 208 | tmpVector.x*=(float)nodeCount; |
---|
| 209 | tmpVector.y*=(float)nodeCount; |
---|
| 210 | tmpVector.z*=(float)nodeCount; |
---|
| 211 | tmpVector.normalize(); |
---|
| 212 | this->dirCurve->addNode(tmpVector); |
---|
| 213 | tmpNode = tmpNode->next; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | \brief calculates the Position on the curve |
---|
| 220 | \param t The position on the Curve (0<=t<=1) |
---|
| 221 | \return the Position on the Path |
---|
| 222 | */ |
---|
| 223 | Vector BezierCurve::calcPos(float t) |
---|
| 224 | { |
---|
| 225 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 226 | if (this->nodeCount >= 3) |
---|
| 227 | { |
---|
| 228 | PathNode* tmpNode = this->firstNode; |
---|
| 229 | double factor = pow(1.0-t,nodeCount-1); |
---|
| 230 | while(tmpNode) |
---|
| 231 | { |
---|
| 232 | ret.x += tmpNode->factor * factor * tmpNode->position.x; |
---|
| 233 | ret.y += tmpNode->factor * factor * tmpNode->position.y; |
---|
| 234 | ret.z += tmpNode->factor * factor * tmpNode->position.z; |
---|
| 235 | factor *= t/(1.0-t); // same as pow but much faster. |
---|
| 236 | |
---|
| 237 | tmpNode = tmpNode->next; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | else if (nodeCount == 2) |
---|
| 241 | { |
---|
| 242 | ret = this->firstNode->position *(1.0-t); |
---|
| 243 | ret = ret + this->firstNode->next->position * t; |
---|
| 244 | } |
---|
| 245 | else if (nodeCount == 1) |
---|
| 246 | ret = this->firstNode->position; |
---|
| 247 | return ret; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | \brief Calulates the direction of the Curve at time t. |
---|
| 252 | \param The time at which to evaluate the curve. |
---|
| 253 | \returns The vvaluated Vector. |
---|
| 254 | */ |
---|
| 255 | Vector BezierCurve::calcDir (float t) |
---|
| 256 | { |
---|
| 257 | return dirCurve->calcPos(t); |
---|
| 258 | } |
---|
| 259 | |
---|
[3433] | 260 | Vector BezierCurve::calcAcc (float t) |
---|
| 261 | { |
---|
| 262 | return dirCurve->dirCurve->calcPos(t); |
---|
| 263 | } |
---|
| 264 | |
---|
[3365] | 265 | /** |
---|
| 266 | \brief Calculates the Quaternion needed for our rotations |
---|
| 267 | \param t The time at which to evaluate the cuve. |
---|
| 268 | \returns The evaluated Quaternion. |
---|
| 269 | */ |
---|
| 270 | Quaternion BezierCurve::calcQuat (float t) |
---|
| 271 | { |
---|
| 272 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | \brief returns the Position of the point calculated on the Curve |
---|
| 278 | \return a Vector to the calculated position |
---|
| 279 | */ |
---|
| 280 | Vector BezierCurve::getPos(void) const |
---|
| 281 | { |
---|
| 282 | return curvePoint; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /////////////////////////////////// |
---|
| 288 | //// Uniform Point curve ///////// |
---|
| 289 | /////////////////////////////////// |
---|
| 290 | /** |
---|
| 291 | \brief Creates a new UPointCurve |
---|
| 292 | */ |
---|
| 293 | UPointCurve::UPointCurve (void) |
---|
| 294 | { |
---|
| 295 | this->derivation = 0; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /** |
---|
| 299 | \brief Creates a new UPointCurve-Derivation-Curve of deriavation'th degree |
---|
| 300 | */ |
---|
| 301 | UPointCurve::UPointCurve (int derivation) |
---|
| 302 | { |
---|
| 303 | this->derivation = derivation; |
---|
| 304 | dirCurve=NULL; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | \brief Deletes a UPointCurve. |
---|
| 309 | |
---|
[3018] | 310 | It does this by freeing all the space taken over from the nodes |
---|
| 311 | */ |
---|
[3365] | 312 | UPointCurve::~UPointCurve(void) |
---|
[3018] | 313 | { |
---|
| 314 | PathNode* tmpNode; |
---|
| 315 | currentNode = firstNode; |
---|
| 316 | while (tmpNode != 0) |
---|
| 317 | { |
---|
| 318 | tmpNode = currentNode; |
---|
| 319 | currentNode = currentNode->next; |
---|
| 320 | delete tmpNode; |
---|
| 321 | } |
---|
[3365] | 322 | if (dirCurve) |
---|
| 323 | delete dirCurve; |
---|
[3018] | 324 | } |
---|
| 325 | |
---|
| 326 | /** |
---|
[3365] | 327 | \brief Rebuilds a UPointCurve |
---|
| 328 | |
---|
| 329 | \todo very bad algorithm |
---|
| 330 | */ |
---|
| 331 | void UPointCurve::rebuild(void) |
---|
| 332 | { |
---|
| 333 | // rebuilding the Curve itself |
---|
| 334 | PathNode* tmpNode = this->firstNode; |
---|
| 335 | int i=0; |
---|
| 336 | Matrix xTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 337 | Matrix yTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 338 | Matrix zTmpMat = Matrix(this->nodeCount, this->nodeCount); |
---|
| 339 | Matrix xValMat = Matrix(this->nodeCount, 3); |
---|
| 340 | Matrix yValMat = Matrix(this->nodeCount, 3); |
---|
| 341 | Matrix zValMat = Matrix(this->nodeCount, 3); |
---|
| 342 | while(tmpNode) |
---|
| 343 | { |
---|
| 344 | Vector fac = Vector(1,1,1); |
---|
| 345 | for (int j = 0; j < this->nodeCount; j++) |
---|
| 346 | { |
---|
| 347 | xTmpMat(i,j) = fac.x; fac.x *= (float)i/(float)this->nodeCount;//tmpNode->position.x; |
---|
| 348 | yTmpMat(i,j) = fac.y; fac.y *= (float)i/(float)this->nodeCount;//tmpNode->position.y; |
---|
| 349 | zTmpMat(i,j) = fac.z; fac.z *= (float)i/(float)this->nodeCount;//tmpNode->position.z; |
---|
| 350 | } |
---|
| 351 | xValMat(i,0) = tmpNode->position.x; |
---|
| 352 | yValMat(i,0) = tmpNode->position.y; |
---|
| 353 | zValMat(i,0) = tmpNode->position.z; |
---|
| 354 | ++i; |
---|
| 355 | tmpNode = tmpNode->next; |
---|
| 356 | } |
---|
| 357 | tmpNode = this->firstNode; |
---|
| 358 | xValMat = xTmpMat.Inv() *= xValMat; |
---|
| 359 | yValMat = yTmpMat.Inv() *= yValMat; |
---|
| 360 | zValMat = zTmpMat.Inv() *= zValMat; |
---|
| 361 | i = 0; |
---|
| 362 | while(tmpNode) |
---|
| 363 | { |
---|
| 364 | tmpNode->vFactor.x = xValMat(i,0); |
---|
| 365 | tmpNode->vFactor.y = yValMat(i,0); |
---|
| 366 | tmpNode->vFactor.z = zValMat(i,0); |
---|
| 367 | |
---|
| 368 | i++; |
---|
| 369 | tmpNode = tmpNode->next; |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | /** |
---|
[3018] | 374 | \brief calculates the Position on the curve |
---|
| 375 | \param t The position on the Curve (0<=t<=1) |
---|
| 376 | \return the Position on the Path |
---|
| 377 | */ |
---|
[3365] | 378 | Vector UPointCurve::calcPos(float t) |
---|
[3018] | 379 | { |
---|
| 380 | PathNode* tmpNode = firstNode; |
---|
| 381 | Vector ret = Vector(0.0,0.0,0.0); |
---|
[3365] | 382 | float factor = 1.0; |
---|
| 383 | while(tmpNode) |
---|
[3018] | 384 | { |
---|
[3365] | 385 | ret.x += tmpNode->vFactor.x * factor; |
---|
| 386 | ret.y += tmpNode->vFactor.y * factor; |
---|
| 387 | ret.z += tmpNode->vFactor.z * factor; |
---|
| 388 | factor *= t; |
---|
| 389 | |
---|
[3018] | 390 | tmpNode = tmpNode->next; |
---|
| 391 | } |
---|
| 392 | return ret; |
---|
| 393 | } |
---|
| 394 | |
---|
[3217] | 395 | /** |
---|
| 396 | \brief Calulates the direction of the Curve at time t. |
---|
| 397 | \param The time at which to evaluate the curve. |
---|
| 398 | \returns The vvaluated Vector. |
---|
| 399 | */ |
---|
[3365] | 400 | Vector UPointCurve::calcDir (float t) |
---|
[3018] | 401 | { |
---|
| 402 | PathNode* tmpNode = firstNode; |
---|
[3365] | 403 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 404 | float factor = 1.0/t; |
---|
| 405 | int k=0; |
---|
| 406 | while(tmpNode) |
---|
[3018] | 407 | { |
---|
[3365] | 408 | ret.x += tmpNode->vFactor.x * factor *k; |
---|
| 409 | ret.y += tmpNode->vFactor.y * factor *k; |
---|
| 410 | ret.z += tmpNode->vFactor.z * factor *k; |
---|
| 411 | factor *= t; |
---|
| 412 | k++; |
---|
[3018] | 413 | tmpNode = tmpNode->next; |
---|
| 414 | } |
---|
| 415 | ret.normalize(); |
---|
[3028] | 416 | return ret; |
---|
[3018] | 417 | } |
---|
| 418 | |
---|
[3433] | 419 | Vector UPointCurve::calcAcc (float t) |
---|
| 420 | { |
---|
| 421 | } |
---|
| 422 | |
---|
[3217] | 423 | /** |
---|
| 424 | \brief Calculates the Quaternion needed for our rotations |
---|
| 425 | \param t The time at which to evaluate the cuve. |
---|
| 426 | \returns The evaluated Quaternion. |
---|
| 427 | */ |
---|
[3365] | 428 | Quaternion UPointCurve::calcQuat (float t) |
---|
[3028] | 429 | { |
---|
| 430 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | |
---|
[3018] | 434 | /** |
---|
| 435 | \brief returns the Position of the point calculated on the Curve |
---|
| 436 | \return a Vector to the calculated position |
---|
| 437 | */ |
---|
[3365] | 438 | Vector UPointCurve::getPos(void) const |
---|
[3018] | 439 | { |
---|
| 440 | return curvePoint; |
---|
| 441 | } |
---|