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