-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspline.cpp
More file actions
339 lines (273 loc) · 10.7 KB
/
Copy pathspline.cpp
File metadata and controls
339 lines (273 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "spline.hpp"
#include "deform.hpp"
#include "globals.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/norm.hpp"
#include <glm/gtx/compatibility.hpp>
#include <glm/gtx/normalize_dot.hpp>
#include <glm/gtx/orthonormalize.hpp>
void Spline::addControlPoint(const glm::vec3 &point) {
controlPoints.push_back(point);
}
void Spline::clear() { controlPoints.clear(); }
std::vector<glm::vec3> Spline::getControlPoints() const {
return controlPoints;
}
float Spline::getLength() const {
return static_cast<float>(controlPoints.size());
}
glm::vec3 Spline::evaluate(float t) const {
if (controlPoints.size() < 4)
return glm::vec3(0.0f);
// Clamp t to valid range
t = glm::clamp(t, 0.0f, getLength() - 1.0f);
int seg = static_cast<int>(t);
float lt = t - static_cast<float>(seg);
int p0, p1, p2, p3;
p1 = glm::clamp(seg, 0, (int)controlPoints.size() - 1);
p0 = glm::clamp(p1 - 1, 0, (int)controlPoints.size() - 1);
p2 = glm::clamp(p1 + 1, 0, (int)controlPoints.size() - 1);
p3 = glm::clamp(p2 + 1, 0, (int)controlPoints.size() - 1);
return catmullRomGlobal(controlPoints[p0], controlPoints[p1],
controlPoints[p2], controlPoints[p3], lt);
}
// Catmull-Rom interpolation with uniform parameterization
glm::vec3 Spline::catmullRom(float t, int i) const {
const glm::vec3 &p0 = controlPoints[i];
const glm::vec3 &p1 = controlPoints[i + 1];
const glm::vec3 &p2 = controlPoints[i + 2];
const glm::vec3 &p3 = controlPoints[i + 3];
float t2 = t * t;
float t3 = t2 * t;
return 0.5f * ((2.0f * p1) + (-p0 + p2) * t +
(2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 +
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
}
glm::vec3 Spline::computeTangent(float t) const {
// Use central difference for more accurate tangent calculation
const float h = 0.01f;
// Handle empty spline case
if (controlPoints.size() < 4)
return glm::vec3(1.0f, 0.0f, 0.0f);
// Calculate points slightly before and after t
float t1 = glm::max(0.0f, t - h);
float t2 = glm::min(getLength(), t + h);
// If t1 and t2 are the same, we're at an endpoint
if (fabs(t2 - t1) < 1e-5f) {
// If at the start, use forward difference
if (t < h) {
t1 = 0.0f;
t2 = h;
}
// If at the end, use backward difference
else {
t1 = getLength() - h;
t2 = getLength();
}
}
glm::vec3 p1 = evaluate(t1);
glm::vec3 p2 = evaluate(t2);
// Calculate tangent as the difference
glm::vec3 tangent = p2 - p1;
// If the tangent is too small, try with a larger step
if (glm::length2(tangent) < 1e-5f) {
t1 = glm::max(0.0f, t - 0.1f);
t2 = glm::min(getLength(), t + 0.1f);
p1 = evaluate(t1);
p2 = evaluate(t2);
tangent = p2 - p1;
// If still too small, use a default direction
if (glm::length2(tangent) < 1e-5f) {
// Try to infer direction from control points
if (!controlPoints.empty() && controlPoints.size() >= 2) {
int mid = controlPoints.size() / 2;
tangent = controlPoints[0] - controlPoints[mid];
if (glm::length2(tangent) < 1e-5f) {
return glm::vec3(1.0f, 0.0f, 0.0f);
}
} else {
return glm::vec3(1.0f, 0.0f, 0.0f);
}
}
}
return glm::normalize(tangent);
}
SplineFrame Spline::computeFrame(float t) const {
SplineFrame frame;
// Handle empty spline case
if (controlPoints.size() < 4) {
frame.position = glm::vec3(0.0f);
frame.tangent = glm::vec3(1.0f, 0.0f, 0.0f); // Forward direction
frame.normal = glm::vec3(0.0f, 1.0f, 0.0f); // Up direction
frame.binormal = glm::vec3(0.0f, 0.0f, 1.0f); // Right direction
return frame;
}
// Get position by evaluating spline
frame.position = evaluate(t);
// Calculate tangent (direction of movement)
frame.tangent = computeTangent(t);
// Choose a consistent up vector for initial frame calculation
glm::vec3 worldUp(0.0f, 1.0f, 0.0f); // World up is Y-axis
// If tangent is too close to world up, choose a different reference vector
if (glm::abs(glm::dot(worldUp, frame.tangent)) > 0.99f) {
worldUp = glm::vec3(1.0f, 0.0f, 0.0f); // Use X-axis instead
}
// Calculate binormal (perpendicular to tangent and world up)
// This gives us the "right" direction of the frame
frame.binormal = glm::normalize(glm::cross(frame.tangent, worldUp));
// Calculate normal (perpendicular to tangent and binormal)
// This gives us the "up" direction of the frame
frame.normal = glm::normalize(glm::cross(frame.binormal, frame.tangent));
return frame;
}
SplineFrame Spline::evaluateFrame(float t,
const SplineFrame &previousFrame) const {
// If we don't have enough control points, return previous frame
if (controlPoints.size() < 4)
return previousFrame;
// Get current position and calculate tangent
glm::vec3 position = evaluate(t);
glm::vec3 tangent = computeTangent(t);
// Create rotation matrix from previous frame's basis vectors
// Column 0 = normal, Column 1 = binormal, Column 2 = tangent
glm::mat3 prevBasis(previousFrame.normal, previousFrame.binormal,
previousFrame.tangent);
// Use parallel transport to rotate the frame
// This minimizes twisting along the spline
glm::mat3 currentBasis =
parallelTransport(prevBasis, previousFrame.tangent, tangent);
// Create new frame
SplineFrame frame;
frame.position = position;
frame.tangent = tangent;
// Extract normal and binormal from rotated basis
frame.normal = currentBasis[0]; // First column
frame.binormal = currentBasis[1]; // Second column
// Ensure orthogonality
frame.binormal = glm::normalize(glm::cross(frame.tangent, frame.normal));
frame.normal = glm::normalize(glm::cross(frame.binormal, frame.tangent));
return frame;
}
SplineFrame Spline::evaluateFrame(float distance) const {
if (controlPoints.empty()) {
// Return default frame if no control points
SplineFrame frame;
frame.position = glm::vec3(0.0f);
frame.tangent = glm::vec3(0.0f, 0.0f, 1.0f);
frame.normal = glm::vec3(0.0f, 1.0f, 0.0f);
frame.binormal = glm::vec3(1.0f, 0.0f, 0.0f);
return frame;
}
if (controlPoints.size() == 1) {
// If only one control point, use it for position
SplineFrame frame;
frame.position = controlPoints[0];
frame.tangent = glm::vec3(0.0f, 0.0f, 1.0f);
frame.normal = glm::vec3(0.0f, 1.0f, 0.0f);
frame.binormal = glm::vec3(1.0f, 0.0f, 0.0f);
return frame;
}
// For multiple control points, compute initial frame
float segmentLength = gridSize;
int segmentIndex = static_cast<int>(distance / segmentLength);
float localT = (distance - (segmentIndex * segmentLength)) / segmentLength;
// Clamp indices for safety
segmentIndex = glm::clamp(segmentIndex, 0, (int)controlPoints.size() - 2);
// Ensure we have enough points for Catmull-Rom
if (controlPoints.size() >= 4) {
// Full Catmull-Rom calculation
int p0_idx = glm::max(0, segmentIndex - 1);
int p1_idx = segmentIndex;
int p2_idx = glm::min((int)controlPoints.size() - 1, segmentIndex + 1);
int p3_idx = glm::min((int)controlPoints.size() - 1, segmentIndex + 2);
glm::vec3 p0 = controlPoints[p0_idx];
glm::vec3 p1 = controlPoints[p1_idx];
glm::vec3 p2 = controlPoints[p2_idx];
glm::vec3 p3 = controlPoints[p3_idx];
SplineFrame frame;
frame.position = catmullRomGlobal(p0, p1, p2, p3, localT);
// Calculate tangent
glm::vec3 nextPos = catmullRomGlobal(p0, p1, p2, p3, localT + 0.01f);
frame.tangent = glm::normalize(nextPos - frame.position);
// Calculate normal and binormal
glm::vec3 worldUp(0.0f, 1.0f, 0.0f);
if (glm::abs(glm::dot(worldUp, frame.tangent)) > 0.99f) {
worldUp = glm::vec3(1.0f, 0.0f, 0.0f);
}
frame.normal = glm::normalize(
glm::cross(glm::cross(frame.tangent, worldUp), frame.tangent));
frame.binormal = glm::normalize(glm::cross(frame.tangent, frame.normal));
return frame;
} else {
// Linear interpolation for 2-3 control points
int p1_idx = glm::min(segmentIndex, (int)controlPoints.size() - 2);
int p2_idx = p1_idx + 1;
SplineFrame frame;
frame.position =
glm::mix(controlPoints[p1_idx], controlPoints[p2_idx], localT);
frame.tangent =
glm::normalize(controlPoints[p2_idx] - controlPoints[p1_idx]);
// Set normal and binormal
glm::vec3 worldUp(0.0f, 1.0f, 0.0f);
if (glm::abs(glm::dot(worldUp, frame.tangent)) > 0.99f) {
worldUp = glm::vec3(1.0f, 0.0f, 0.0f);
}
frame.normal = glm::normalize(
glm::cross(glm::cross(frame.tangent, worldUp), frame.tangent));
frame.binormal = glm::normalize(glm::cross(frame.tangent, frame.normal));
return frame;
}
}
glm::vec3 catmullRomGlobal(const glm::vec3 &p0, const glm::vec3 &p1,
const glm::vec3 &p2, const glm::vec3 &p3, float t) {
// Precompute basis functions once
const float t2 = t * t;
const float t3 = t2 * t;
// Basis functions - compute once and reuse
const float b0 = 0.5f * (-t3 + 2.0f * t2 - t); // -0.5*t^3 + t^2 - 0.5*t
const float b1 =
0.5f * (3.0f * t3 - 5.0f * t2 + 2.0f); // 1.5*t^3 - 2.5*t^2 + 1
const float b2 =
0.5f * (-3.0f * t3 + 4.0f * t2 + t); // -1.5*t^3 + 2*t^2 + 0.5*t
const float b3 = 0.5f * (t3 - t2); // 0.5*t^3 - 0.5*t^2
// Direct calculation using basis functions
return b0 * p0 + b1 * p1 + b2 * p2 + b3 * p3;
}
void bindVerticesToSpine(const Spline &spine,
const std::vector<glm::vec3> &vertices) {
// Create frames along the spine
const int numFrames = 50;
std::vector<SplineFrame> frames(numFrames);
// Compute frames
frames[0] = spine.computeFrame(0.0f);
for (int i = 1; i < numFrames; ++i) {
float t = static_cast<float>(i) / (numFrames - 1) * spine.getLength();
frames[i] = spine.evaluateFrame(t, frames[i - 1]);
}
// Resize binding array
vertexBindings.resize(vertices.size());
// Bind each vertex to closest frame
for (size_t i = 0; i < vertices.size(); ++i) {
const glm::vec3 &vertex = vertices[i];
float minDist = FLT_MAX;
int closestFrameIndex = 0;
// Find closest frame
for (size_t j = 0; j < frames.size(); ++j) {
const SplineFrame &frame = frames[j];
float dist = glm::distance(vertex, frame.position);
if (dist < minDist) {
minDist = dist;
closestFrameIndex = j;
}
}
// Calculate local coordinates in frame space
const SplineFrame &frame = frames[closestFrameIndex];
glm::vec3 toVertex = vertex - frame.position;
float x = glm::dot(toVertex, frame.tangent);
float y = glm::dot(toVertex, frame.normal);
float z = glm::dot(toVertex, frame.binormal);
// Store binding
vertexBindings[i].frame_index = closestFrameIndex;
vertexBindings[i].local_coords = glm::vec3(x, y, z);
}
}