| [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 |
|---|
| 13 | co-programmer: ... |
|---|
| [3023] | 14 | |
|---|
| 15 | TODO: |
|---|
| 16 | local-Time implementation |
|---|
| 17 | closed Bezier Curve |
|---|
| 18 | NURBS |
|---|
| 19 | |
|---|
| [3018] | 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include "curve.h" |
|---|
| 23 | |
|---|
| [3019] | 24 | |
|---|
| 25 | |
|---|
| [3018] | 26 | /** |
|---|
| [3019] | 27 | \brief adds a new Node to the bezier Curve |
|---|
| 28 | \param newNode a Vector to the position of the new node |
|---|
| 29 | */ |
|---|
| 30 | void Curve::addNode(const Vector& newNode) |
|---|
| 31 | { |
|---|
| 32 | if (nodeCount != 0 ) |
|---|
| 33 | { |
|---|
| 34 | currentNode = currentNode->next = new PathNode; |
|---|
| 35 | } |
|---|
| 36 | currentNode->position = newNode; |
|---|
| 37 | currentNode->next = 0; // not sure if this really points to NULL!! |
|---|
| 38 | currentNode->number = (++nodeCount); |
|---|
| 39 | return; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| [3018] | 44 | \brief Creates a new BezierCurve |
|---|
| 45 | */ |
|---|
| 46 | BezierCurve::BezierCurve (void) |
|---|
| 47 | { |
|---|
| 48 | nodeCount = 0; |
|---|
| 49 | firstNode = new PathNode; |
|---|
| 50 | currentNode = firstNode; |
|---|
| 51 | |
|---|
| 52 | firstNode->position = Vector (.0, .0, .0); |
|---|
| 53 | firstNode->number = 0; |
|---|
| 54 | firstNode->next = 0; // not sure if this really points to NULL!! |
|---|
| 55 | |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | \brief Deletes a BezierCurve. |
|---|
| [3217] | 61 | |
|---|
| [3018] | 62 | It does this by freeing all the space taken over from the nodes |
|---|
| 63 | */ |
|---|
| 64 | BezierCurve::~BezierCurve (void) |
|---|
| 65 | { |
|---|
| 66 | PathNode* tmpNode; |
|---|
| 67 | currentNode = firstNode; |
|---|
| 68 | while (tmpNode != 0) |
|---|
| 69 | { |
|---|
| 70 | tmpNode = currentNode; |
|---|
| 71 | currentNode = currentNode->next; |
|---|
| 72 | delete tmpNode; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | \brief calculates the Position on the curve |
|---|
| 78 | \param t The position on the Curve (0<=t<=1) |
|---|
| 79 | \return the Position on the Path |
|---|
| 80 | */ |
|---|
| 81 | Vector BezierCurve::calcPos(float t) |
|---|
| 82 | { |
|---|
| 83 | if (nodeCount <=4) |
|---|
| 84 | { |
|---|
| 85 | // if (verbose >= 1) |
|---|
| 86 | // printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount); |
|---|
| 87 | return Vector(0,0,0); |
|---|
| 88 | } |
|---|
| 89 | PathNode* tmpNode = firstNode; |
|---|
| 90 | |
|---|
| 91 | Vector ret = Vector(0.0,0.0,0.0); |
|---|
| 92 | double factor; |
|---|
| 93 | int k=0; |
|---|
| 94 | while(tmpNode!=0) |
|---|
| 95 | { |
|---|
| 96 | k++; |
|---|
| 97 | factor = ncr (nodeCount, k); |
|---|
| 98 | |
|---|
| 99 | for (int j=0; j<nodeCount-k; j++) |
|---|
| 100 | factor*=(1-t); |
|---|
| 101 | for (int j=0; j<k; j++) |
|---|
| 102 | factor*=t; |
|---|
| 103 | ret.x += factor * tmpNode->position.x; |
|---|
| 104 | ret.y += factor * tmpNode->position.y; |
|---|
| 105 | ret.z += factor * tmpNode->position.z; |
|---|
| 106 | |
|---|
| 107 | tmpNode = tmpNode->next; |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | return ret; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| [3217] | 113 | /** |
|---|
| 114 | \brief Calulates the direction of the Curve at time t. |
|---|
| 115 | \param The time at which to evaluate the curve. |
|---|
| 116 | \returns The vvaluated Vector. |
|---|
| 117 | */ |
|---|
| [3018] | 118 | Vector BezierCurve::calcDir (float t) |
|---|
| 119 | { |
|---|
| 120 | PathNode* tmpNode = firstNode; |
|---|
| 121 | BezierCurve* tmpCurve = new BezierCurve(); |
|---|
| 122 | Vector ret; |
|---|
| 123 | Vector tmpVector; |
|---|
| 124 | |
|---|
| 125 | while (tmpNode->next != 0) |
|---|
| 126 | { |
|---|
| 127 | tmpVector = (tmpNode->next->position)- (tmpNode->position); |
|---|
| 128 | tmpVector.x*=(float)nodeCount; |
|---|
| 129 | tmpVector.y*=(float)nodeCount; |
|---|
| 130 | tmpVector.z*=(float)nodeCount; |
|---|
| 131 | |
|---|
| 132 | tmpCurve->addNode(tmpVector); |
|---|
| 133 | tmpNode = tmpNode->next; |
|---|
| 134 | } |
|---|
| 135 | ret = tmpCurve->calcPos(t); |
|---|
| 136 | ret.normalize(); |
|---|
| 137 | |
|---|
| [3028] | 138 | return ret; |
|---|
| [3018] | 139 | } |
|---|
| 140 | |
|---|
| [3217] | 141 | /** |
|---|
| 142 | \brief Calculates the Quaternion needed for our rotations |
|---|
| 143 | \param t The time at which to evaluate the cuve. |
|---|
| 144 | \returns The evaluated Quaternion. |
|---|
| 145 | */ |
|---|
| [3028] | 146 | Quaternion BezierCurve::calcQuat (float t) |
|---|
| 147 | { |
|---|
| 148 | return Quaternion (calcDir(t), Vector(0,0,1)); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| [3018] | 152 | /** |
|---|
| 153 | \brief returns the Position of the point calculated on the Curve |
|---|
| 154 | \return a Vector to the calculated position |
|---|
| 155 | */ |
|---|
| 156 | Vector BezierCurve::getPos() const |
|---|
| 157 | { |
|---|
| 158 | return curvePoint; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| [3217] | 161 | /** |
|---|
| 162 | \brief ncr-calculator, did not find an other method |
|---|
| 163 | \todo a c++ variante to do this |
|---|
| 164 | */ |
|---|
| [3019] | 165 | int ncr(int n, int i) |
|---|
| [3018] | 166 | { |
|---|
| 167 | int ret = 1; |
|---|
| 168 | for (int k=1; k<=n; k++) |
|---|
| 169 | ret*=k; |
|---|
| 170 | for (int k=1; k<=i; k++) |
|---|
| 171 | ret/=k; |
|---|
| 172 | for (int k=1; k<=n-i; k++) |
|---|
| 173 | ret/=k; |
|---|
| 174 | |
|---|
| 175 | return ret; |
|---|
| 176 | } |
|---|
| [3028] | 177 | |
|---|