[5211] | 1 | /* |
---|
[3018] | 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 | |
---|
[5211] | 15 | ADD: Patrick Boenzli B-Spline |
---|
[3365] | 16 | |
---|
| 17 | |
---|
[5211] | 18 | TODO: |
---|
[3023] | 19 | local-Time implementation |
---|
| 20 | NURBS |
---|
[5218] | 21 | !!tList implementation!! |
---|
[5211] | 22 | |
---|
[3018] | 23 | */ |
---|
| 24 | |
---|
[3593] | 25 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH |
---|
| 26 | |
---|
[3018] | 27 | #include "curve.h" |
---|
[3595] | 28 | |
---|
[3365] | 29 | #include "debug.h" |
---|
[3018] | 30 | |
---|
[3365] | 31 | #include <math.h> |
---|
| 32 | #include <stdio.h> |
---|
[3019] | 33 | |
---|
[3588] | 34 | |
---|
[5211] | 35 | /** |
---|
[4836] | 36 | * default constructor for a Curve |
---|
[3588] | 37 | */ |
---|
[4746] | 38 | Curve::Curve() |
---|
[3588] | 39 | { |
---|
| 40 | nodeCount = 0; |
---|
| 41 | firstNode = new PathNode; |
---|
| 42 | currentNode = firstNode; |
---|
| 43 | |
---|
| 44 | firstNode->position = Vector (.0, .0, .0); |
---|
| 45 | firstNode->number = 0; |
---|
| 46 | firstNode->next = 0; // not sure if this really points to NULL!! |
---|
| 47 | } |
---|
| 48 | |
---|
[3018] | 49 | /** |
---|
[4836] | 50 | * adds a new Node to the bezier Curve |
---|
| 51 | * @param newNode a Vector to the position of the new node |
---|
[3019] | 52 | */ |
---|
| 53 | void Curve::addNode(const Vector& newNode) |
---|
| 54 | { |
---|
| 55 | if (nodeCount != 0 ) |
---|
| 56 | { |
---|
| 57 | currentNode = currentNode->next = new PathNode; |
---|
| 58 | } |
---|
| 59 | currentNode->position = newNode; |
---|
[3433] | 60 | currentNode->next = 0; |
---|
[3019] | 61 | currentNode->number = (++nodeCount); |
---|
[3365] | 62 | this->rebuild(); |
---|
[3019] | 63 | return; |
---|
| 64 | } |
---|
| 65 | |
---|
[4472] | 66 | /** |
---|
[4836] | 67 | * adds a new Node to the bezier Curve |
---|
| 68 | * @param newNode a Vector to the position of the new node |
---|
| 69 | * @param insertPosition after the n-th node the new node will be inserted |
---|
[4472] | 70 | */ |
---|
[3433] | 71 | void Curve::addNode(const Vector& newNode, unsigned int insertPosition) |
---|
| 72 | { |
---|
| 73 | if (this->nodeCount == 0 || insertPosition > this->nodeCount) |
---|
| 74 | return addNode(newNode); |
---|
| 75 | |
---|
| 76 | if (insertPosition == 0) |
---|
| 77 | insertPosition = 1; |
---|
| 78 | |
---|
| 79 | PathNode* insNode = new PathNode; |
---|
| 80 | |
---|
| 81 | // relinking |
---|
| 82 | PathNode* tmpNode = this->firstNode; |
---|
| 83 | if (insertPosition > 1) |
---|
| 84 | { |
---|
| 85 | while (tmpNode->next->number != insertPosition) |
---|
[5211] | 86 | tmpNode= tmpNode->next; |
---|
[3433] | 87 | insNode->next = tmpNode->next; |
---|
| 88 | tmpNode->next = insNode; |
---|
| 89 | } |
---|
| 90 | else |
---|
| 91 | { |
---|
| 92 | insNode->next = this->firstNode; |
---|
| 93 | this->firstNode = insNode; |
---|
| 94 | } |
---|
| 95 | // renumbering |
---|
| 96 | insNode->number = insertPosition; |
---|
| 97 | tmpNode = insNode->next; |
---|
| 98 | while (tmpNode) |
---|
| 99 | { |
---|
| 100 | tmpNode->number++; |
---|
| 101 | tmpNode = tmpNode->next; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // finished |
---|
| 105 | insNode->position = newNode; |
---|
| 106 | ++nodeCount; |
---|
| 107 | this->rebuild(); |
---|
| 108 | return; |
---|
| 109 | } |
---|
| 110 | |
---|
[3365] | 111 | /** |
---|
[4836] | 112 | * Finds a Node by its Number, and returns its Position |
---|
| 113 | * @param nodeToFind the n'th node in the List of nodes |
---|
| 114 | * @returns A Vector to the Position of the Node. |
---|
[3365] | 115 | */ |
---|
| 116 | Vector Curve::getNode(unsigned int nodeToFind) |
---|
| 117 | { |
---|
[3433] | 118 | if (nodeToFind > this->nodeCount || nodeToFind < 0) |
---|
[3365] | 119 | return Vector(0,0,0); |
---|
| 120 | PathNode* tmpNode = this->firstNode; |
---|
| 121 | for (int i = 1; i < nodeToFind; i++) |
---|
| 122 | tmpNode = tmpNode->next; |
---|
| 123 | return tmpNode->position; |
---|
| 124 | } |
---|
[3019] | 125 | |
---|
[3433] | 126 | /** |
---|
[4836] | 127 | * Outputs information about the state of this Curve |
---|
[3433] | 128 | */ |
---|
[4746] | 129 | void Curve::debug() |
---|
[3433] | 130 | { |
---|
| 131 | printf("<<-------------------------------\n"); |
---|
| 132 | printf("Curve Information:\n"); |
---|
| 133 | printf("NodeCount: %d\n", this->nodeCount); |
---|
| 134 | PathNode* tmpNode = this->firstNode; |
---|
| 135 | while (tmpNode) |
---|
| 136 | { |
---|
| 137 | printf("node #%d: %f, %f, %f\n", tmpNode->number, tmpNode->position.x, tmpNode->position.y, tmpNode->position.z); |
---|
| 138 | tmpNode = tmpNode->next; |
---|
| 139 | } |
---|
| 140 | printf("------------------------------->>\n"); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
[3365] | 144 | /////////////////////////////////// |
---|
| 145 | /// Bezier Curve ////////////////// |
---|
| 146 | /////////////////////////////////// |
---|
| 147 | |
---|
[3019] | 148 | /** |
---|
[4836] | 149 | * Creates a new BezierCurve |
---|
[3018] | 150 | */ |
---|
[4746] | 151 | BezierCurve::BezierCurve () |
---|
[3018] | 152 | { |
---|
[3365] | 153 | this->derivation = 0; |
---|
| 154 | dirCurve = new BezierCurve(1); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
[4836] | 158 | * Creates a new BezierCurve-Derivation-Curve |
---|
[3365] | 159 | */ |
---|
| 160 | BezierCurve::BezierCurve (int derivation) |
---|
| 161 | { |
---|
| 162 | this->derivation = derivation; |
---|
| 163 | dirCurve=NULL; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
[4836] | 167 | * Deletes a BezierCurve. |
---|
[3365] | 168 | |
---|
| 169 | It does this by freeing all the space taken over from the nodes |
---|
| 170 | */ |
---|
[4746] | 171 | BezierCurve::~BezierCurve() |
---|
[3365] | 172 | { |
---|
| 173 | PathNode* tmpNode; |
---|
| 174 | currentNode = firstNode; |
---|
| 175 | while (tmpNode != 0) |
---|
| 176 | { |
---|
| 177 | tmpNode = currentNode; |
---|
| 178 | currentNode = currentNode->next; |
---|
| 179 | delete tmpNode; |
---|
| 180 | } |
---|
| 181 | if (dirCurve) |
---|
| 182 | delete dirCurve; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
[4836] | 186 | * Rebuilds a Curve |
---|
[3365] | 187 | */ |
---|
[4746] | 188 | void BezierCurve::rebuild() |
---|
[3365] | 189 | { |
---|
| 190 | PathNode* tmpNode = firstNode; |
---|
[3217] | 191 | |
---|
[3365] | 192 | // rebuilding the Curve itself |
---|
| 193 | float k=0; |
---|
| 194 | float n = nodeCount -1; |
---|
| 195 | float binCoef = 1; |
---|
| 196 | while(tmpNode) |
---|
| 197 | { |
---|
| 198 | tmpNode->factor = binCoef; |
---|
| 199 | if (tmpNode =tmpNode->next) |
---|
[5211] | 200 | { |
---|
| 201 | binCoef *=(n-k)/(k+1); |
---|
| 202 | ++k; |
---|
| 203 | } |
---|
[3365] | 204 | } |
---|
| 205 | |
---|
| 206 | // rebuilding the Derivation curve |
---|
[3433] | 207 | if(this->derivation <= 1) |
---|
[3365] | 208 | { |
---|
| 209 | tmpNode = firstNode; |
---|
| 210 | delete dirCurve; |
---|
| 211 | dirCurve = new BezierCurve(1); |
---|
| 212 | while(tmpNode->next) |
---|
[5211] | 213 | { |
---|
| 214 | Vector tmpVector = (tmpNode->next->position)- (tmpNode->position); |
---|
| 215 | tmpVector.x*=(float)nodeCount; |
---|
| 216 | tmpVector.y*=(float)nodeCount; |
---|
| 217 | tmpVector.z*=(float)nodeCount; |
---|
| 218 | tmpVector.normalize(); |
---|
| 219 | this->dirCurve->addNode(tmpVector); |
---|
| 220 | tmpNode = tmpNode->next; |
---|
| 221 | } |
---|
[3365] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[4836] | 226 | * calculates the Position on the curve |
---|
| 227 | * @param t The position on the Curve (0<=t<=1) |
---|
| 228 | * @return the Position on the Path |
---|
[3365] | 229 | */ |
---|
[5211] | 230 | Vector BezierCurve::calcPos(float t) |
---|
[3365] | 231 | { |
---|
| 232 | Vector ret = Vector(0.0,0.0,0.0); |
---|
| 233 | if (this->nodeCount >= 3) |
---|
| 234 | { |
---|
| 235 | PathNode* tmpNode = this->firstNode; |
---|
| 236 | double factor = pow(1.0-t,nodeCount-1); |
---|
| 237 | while(tmpNode) |
---|
[5211] | 238 | { |
---|
| 239 | ret.x += tmpNode->factor * factor * tmpNode->position.x; |
---|
| 240 | ret.y += tmpNode->factor * factor * tmpNode->position.y; |
---|
| 241 | ret.z += tmpNode->factor * factor * tmpNode->position.z; |
---|
| 242 | factor *= t/(1.0-t); // same as pow but much faster. |
---|
| 243 | |
---|
| 244 | tmpNode = tmpNode->next; |
---|
| 245 | } |
---|
[3365] | 246 | } |
---|
| 247 | else if (nodeCount == 2) |
---|
| 248 | { |
---|
| 249 | ret = this->firstNode->position *(1.0-t); |
---|
| 250 | ret = ret + this->firstNode->next->position * t; |
---|
| 251 | } |
---|
| 252 | else if (nodeCount == 1) |
---|
| 253 | ret = this->firstNode->position; |
---|
| 254 | return ret; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /** |
---|
[4836] | 258 | * Calulates the direction of the Curve at time t. |
---|
| 259 | * @param t The time at which to evaluate the curve. |
---|
| 260 | * @returns The valuated Vector. |
---|
[3365] | 261 | */ |
---|
| 262 | Vector BezierCurve::calcDir (float t) |
---|
| 263 | { |
---|
[4472] | 264 | return this->dirCurve->calcPos(t); |
---|
[3365] | 265 | } |
---|
| 266 | |
---|
[4472] | 267 | /** |
---|
[4836] | 268 | * Calulates the acceleration (second derivate) of the Curve at time t. |
---|
| 269 | * @param t The time at which to evaluate the curve. |
---|
| 270 | * @returns The valuated Vector. |
---|
[4472] | 271 | */ |
---|
[3433] | 272 | Vector BezierCurve::calcAcc (float t) |
---|
| 273 | { |
---|
[4472] | 274 | return this->dirCurve->getDirCurve()->calcPos(t); |
---|
[3433] | 275 | } |
---|
| 276 | |
---|
[3365] | 277 | /** |
---|
[4836] | 278 | * Calculates the Quaternion needed for our rotations |
---|
| 279 | * @param t The time at which to evaluate the cuve. |
---|
| 280 | * @returns The evaluated Quaternion. |
---|
[3365] | 281 | */ |
---|
| 282 | Quaternion BezierCurve::calcQuat (float t) |
---|
| 283 | { |
---|
| 284 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | \brief returns the Position of the point calculated on the Curve |
---|
| 290 | \return a Vector to the calculated position |
---|
| 291 | */ |
---|
[4746] | 292 | Vector BezierCurve::getPos() const |
---|
[3365] | 293 | { |
---|
| 294 | return curvePoint; |
---|
| 295 | } |
---|