/src/ogre/OgreMain/src/OgreSimpleSpline.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | |
30 | | namespace Ogre { |
31 | | |
32 | | //--------------------------------------------------------------------- |
33 | | SimpleSpline::SimpleSpline() |
34 | 0 | { |
35 | 0 | mAutoCalc = true; |
36 | 0 | } |
37 | | //--------------------------------------------------------------------- |
38 | | SimpleSpline::~SimpleSpline() |
39 | 0 | { |
40 | 0 | } |
41 | | //--------------------------------------------------------------------- |
42 | | void SimpleSpline::addPoint(const Vector3& p) |
43 | 0 | { |
44 | 0 | mPoints.push_back(p); |
45 | 0 | if (mAutoCalc) |
46 | 0 | { |
47 | 0 | recalcTangents(); |
48 | 0 | } |
49 | 0 | } |
50 | | //--------------------------------------------------------------------- |
51 | | Vector3 SimpleSpline::interpolate(Real t) const |
52 | 0 | { |
53 | | // Currently assumes points are evenly spaced, will cause velocity |
54 | | // change where this is not the case |
55 | | // TODO: base on arclength? |
56 | | |
57 | | |
58 | | // Work out which segment this is in |
59 | 0 | Real fSeg = t * (mPoints.size() - 1); |
60 | 0 | unsigned int segIdx = (unsigned int)fSeg; |
61 | | // Apportion t |
62 | 0 | t = fSeg - segIdx; |
63 | |
|
64 | 0 | return interpolate(segIdx, t); |
65 | |
|
66 | 0 | } |
67 | | //--------------------------------------------------------------------- |
68 | | Vector3 SimpleSpline::interpolate(unsigned int fromIndex, Real t) const |
69 | 0 | { |
70 | | // Bounds check |
71 | 0 | assert (fromIndex < mPoints.size() && |
72 | 0 | "fromIndex out of bounds"); |
73 | |
|
74 | 0 | if ((fromIndex + 1) == mPoints.size()) |
75 | 0 | { |
76 | | // Duff request, cannot blend to nothing |
77 | | // Just return source |
78 | 0 | return mPoints[fromIndex]; |
79 | |
|
80 | 0 | } |
81 | | |
82 | | // Fast special cases |
83 | 0 | if (t == 0.0f) |
84 | 0 | { |
85 | 0 | return mPoints[fromIndex]; |
86 | 0 | } |
87 | 0 | else if(t == 1.0f) |
88 | 0 | { |
89 | 0 | return mPoints[fromIndex + 1]; |
90 | 0 | } |
91 | | |
92 | | // Set up matrix |
93 | | // Hermite polynomial |
94 | 0 | static Matrix4 hCoeffs = { |
95 | 0 | 2, -2, 1, 1, |
96 | 0 | -3, 3, -2, -1, |
97 | 0 | 0, 0, 1, 0, |
98 | 0 | 1, 0, 0, 0 |
99 | 0 | }; |
100 | | |
101 | | // Real interpolation |
102 | | // Form a vector of powers of t |
103 | 0 | Real t2, t3; |
104 | 0 | t2 = t * t; |
105 | 0 | t3 = t2 * t; |
106 | 0 | Vector4 powers(t3, t2, t, 1); |
107 | | |
108 | | |
109 | | // Algorithm is ret = powers * hCoeffs * Matrix4(point1, point2, tangent1, tangent2) |
110 | 0 | const Vector3& point1 = mPoints[fromIndex]; |
111 | 0 | const Vector3& point2 = mPoints[fromIndex+1]; |
112 | 0 | const Vector3& tan1 = mTangents[fromIndex]; |
113 | 0 | const Vector3& tan2 = mTangents[fromIndex+1]; |
114 | 0 | Matrix4 pt; |
115 | |
|
116 | 0 | pt[0][0] = point1.x; |
117 | 0 | pt[0][1] = point1.y; |
118 | 0 | pt[0][2] = point1.z; |
119 | 0 | pt[0][3] = 1.0f; |
120 | 0 | pt[1][0] = point2.x; |
121 | 0 | pt[1][1] = point2.y; |
122 | 0 | pt[1][2] = point2.z; |
123 | 0 | pt[1][3] = 1.0f; |
124 | 0 | pt[2][0] = tan1.x; |
125 | 0 | pt[2][1] = tan1.y; |
126 | 0 | pt[2][2] = tan1.z; |
127 | 0 | pt[2][3] = 1.0f; |
128 | 0 | pt[3][0] = tan2.x; |
129 | 0 | pt[3][1] = tan2.y; |
130 | 0 | pt[3][2] = tan2.z; |
131 | 0 | pt[3][3] = 1.0f; |
132 | |
|
133 | 0 | Vector4 ret = powers * hCoeffs * pt; |
134 | | |
135 | |
|
136 | 0 | return Vector3(ret.x, ret.y, ret.z); |
137 | | |
138 | | |
139 | | |
140 | |
|
141 | 0 | } |
142 | | //--------------------------------------------------------------------- |
143 | | void SimpleSpline::recalcTangents(void) |
144 | 0 | { |
145 | | // Catmull-Rom approach |
146 | | // |
147 | | // tangent[i] = 0.5 * (point[i+1] - point[i-1]) |
148 | | // |
149 | | // Assume endpoint tangents are parallel with line with neighbour |
150 | |
|
151 | 0 | size_t i, numPoints; |
152 | 0 | bool isClosed; |
153 | |
|
154 | 0 | numPoints = mPoints.size(); |
155 | 0 | if (numPoints < 2) |
156 | 0 | { |
157 | | // Can't do anything yet |
158 | 0 | return; |
159 | 0 | } |
160 | | |
161 | | // Closed or open? |
162 | 0 | if (mPoints[0] == mPoints[numPoints-1]) |
163 | 0 | { |
164 | 0 | isClosed = true; |
165 | 0 | } |
166 | 0 | else |
167 | 0 | { |
168 | 0 | isClosed = false; |
169 | 0 | } |
170 | |
|
171 | 0 | mTangents.resize(numPoints); |
172 | | |
173 | | |
174 | |
|
175 | 0 | for(i = 0; i < numPoints; ++i) |
176 | 0 | { |
177 | 0 | if (i ==0) |
178 | 0 | { |
179 | | // Special case start |
180 | 0 | if (isClosed) |
181 | 0 | { |
182 | | // Use numPoints-2 since numPoints-1 is the last point and == [0] |
183 | 0 | mTangents[i] = 0.5 * (mPoints[1] - mPoints[numPoints-2]); |
184 | 0 | } |
185 | 0 | else |
186 | 0 | { |
187 | 0 | mTangents[i] = 0.5 * (mPoints[1] - mPoints[0]); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | else if (i == numPoints-1) |
191 | 0 | { |
192 | | // Special case end |
193 | 0 | if (isClosed) |
194 | 0 | { |
195 | | // Use same tangent as already calculated for [0] |
196 | 0 | mTangents[i] = mTangents[0]; |
197 | 0 | } |
198 | 0 | else |
199 | 0 | { |
200 | 0 | mTangents[i] = 0.5 * (mPoints[i] - mPoints[i-1]); |
201 | 0 | } |
202 | 0 | } |
203 | 0 | else |
204 | 0 | { |
205 | 0 | mTangents[i] = 0.5 * (mPoints[i+1] - mPoints[i-1]); |
206 | 0 | } |
207 | | |
208 | 0 | } |
209 | | |
210 | | |
211 | |
|
212 | 0 | } |
213 | | //--------------------------------------------------------------------- |
214 | | const Vector3& SimpleSpline::getPoint(unsigned short index) const |
215 | 0 | { |
216 | 0 | assert (index < mPoints.size() && "Point index is out of bounds!!"); |
217 | |
|
218 | 0 | return mPoints[index]; |
219 | 0 | } |
220 | | //--------------------------------------------------------------------- |
221 | | unsigned short SimpleSpline::getNumPoints(void) const |
222 | 0 | { |
223 | 0 | return (unsigned short)mPoints.size(); |
224 | 0 | } |
225 | | //--------------------------------------------------------------------- |
226 | | void SimpleSpline::clear(void) |
227 | 0 | { |
228 | 0 | mPoints.clear(); |
229 | 0 | mTangents.clear(); |
230 | 0 | } |
231 | | //--------------------------------------------------------------------- |
232 | | void SimpleSpline::updatePoint(unsigned short index, const Vector3& value) |
233 | 0 | { |
234 | 0 | assert (index < mPoints.size() && "Point index is out of bounds!!"); |
235 | |
|
236 | 0 | mPoints[index] = value; |
237 | 0 | if (mAutoCalc) |
238 | 0 | { |
239 | 0 | recalcTangents(); |
240 | 0 | } |
241 | 0 | } |
242 | | //--------------------------------------------------------------------- |
243 | | void SimpleSpline::setAutoCalculate(bool autoCalc) |
244 | 0 | { |
245 | 0 | mAutoCalc = autoCalc; |
246 | 0 | } |
247 | | |
248 | | |
249 | | |
250 | | |
251 | | } |
252 | | |
253 | | |
254 | | |
255 | | |