[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" |
---|
| 25 | |
---|
[3320] | 26 | #include <math.h> |
---|
| 27 | #include <stdio.h> |
---|
[3019] | 28 | |
---|
[3018] | 29 | /** |
---|
[3019] | 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); |
---|
[3320] | 42 | this->rebuild(); |
---|
[3019] | 43 | return; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /** |
---|
[3018] | 48 | \brief Creates a new BezierCurve |
---|
| 49 | */ |
---|
| 50 | BezierCurve::BezierCurve (void) |
---|
| 51 | { |
---|
[3321] | 52 | this->derivation = 0; |
---|
| 53 | dirCurve = new BezierCurve(1); |
---|
| 54 | this->init(); |
---|
| 55 | } |
---|
[3018] | 56 | |
---|
[3321] | 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(); |
---|
[3018] | 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | \brief Deletes a BezierCurve. |
---|
[3217] | 69 | |
---|
[3018] | 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 | } |
---|
[3321] | 82 | if (dirCurve) |
---|
| 83 | delete dirCurve; |
---|
[3018] | 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
[3321] | 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 | /** |
---|
[3320] | 103 | \brief Rebuilds a Curve |
---|
| 104 | */ |
---|
| 105 | void BezierCurve::rebuild(void) |
---|
| 106 | { |
---|
| 107 | PathNode* tmpNode = firstNode; |
---|
| 108 | |
---|
[3321] | 109 | // rebuilding the Curve itself |
---|
[3320] | 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 | } |
---|
[3321] | 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 | } |
---|
[3320] | 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
[3018] | 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 | { |
---|
[3320] | 151 | // if (verbose >= 1) |
---|
[3018] | 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); |
---|
[3320] | 157 | float factor = 1.0*pow(1.0-t,nodeCount); |
---|
| 158 | while(tmpNode) |
---|
[3018] | 159 | { |
---|
[3320] | 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 | |
---|
[3018] | 165 | tmpNode = tmpNode->next; |
---|
| 166 | } |
---|
| 167 | return ret; |
---|
| 168 | } |
---|
| 169 | |
---|
[3217] | 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 | */ |
---|
[3018] | 175 | Vector BezierCurve::calcDir (float t) |
---|
| 176 | { |
---|
[3322] | 177 | return dirCurve->calcPos(t); |
---|
[3018] | 178 | } |
---|
| 179 | |
---|
[3217] | 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 | */ |
---|
[3028] | 185 | Quaternion BezierCurve::calcQuat (float t) |
---|
| 186 | { |
---|
| 187 | return Quaternion (calcDir(t), Vector(0,0,1)); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
[3018] | 191 | /** |
---|
| 192 | \brief returns the Position of the point calculated on the Curve |
---|
| 193 | \return a Vector to the calculated position |
---|
| 194 | */ |
---|
[3319] | 195 | Vector BezierCurve::getPos(void) const |
---|
[3018] | 196 | { |
---|
| 197 | return curvePoint; |
---|
| 198 | } |
---|