-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.cpp
447 lines (355 loc) · 9.85 KB
/
curve.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include "Curve.h"
#include <algorithm>
#include <functional>
#ifdef _DEBUG
#include <assert.h>
#endif // _DEBUG
#include <math.h>
#ifdef WIN32
#include <windows.h>
#endif // WIN32
#include <GL/gl.h>
#include <float.h>
#include "Curve.h"
#include "CurveEvaluator.h"
float Curve::s_fCtrlPtXEpsilon = 0.0001f;
Curve::Curve() :
m_pceEvaluator(NULL),
m_bWrap(false),
m_bDirty(true),
m_fMaxX(1.0f)
{
init();
}
Curve::Curve(const float fMaxX, const Point& point) :
m_pceEvaluator(NULL),
m_bWrap(false),
m_bDirty(true),
m_fMaxX(fMaxX)
{
addControlPoint(point);
}
Curve::Curve(const float fMaxX, const float fStartYValue) :
m_pceEvaluator(NULL),
m_bWrap(false),
m_bDirty(true),
m_fMaxX(fMaxX)
{
init(fStartYValue);
}
void Curve::init(const float fStartYValue /* = 0.0f */)
{
m_ptvCtrlPts.push_back(Point(m_fMaxX * (1.0f / 3.0f), fStartYValue));
m_ptvCtrlPts.push_back(Point(m_fMaxX * (2.0f / 3.0f), fStartYValue));
}
void Curve::maxX(const float fNewMaxX)
{
m_fMaxX = fNewMaxX;
for (int i = 0; i < m_ptvCtrlPts.size(); ++i) {
if (m_ptvCtrlPts[i].x > m_fMaxX) {
m_ptvCtrlPts[i].x = m_fMaxX;
}
}
m_bDirty = true;
}
Curve::Curve(std::istream& isInputStream)
{
fromStream(isInputStream);
}
void Curve::toStream(std::ostream & output_stream) const
{
output_stream << m_ptvCtrlPts.size() << std::endl;
for (std::vector<Point>::const_iterator control_point_iterator = m_ptvCtrlPts.begin(); control_point_iterator != m_ptvCtrlPts.end(); ++control_point_iterator) {
output_stream << *control_point_iterator;
}
output_stream << m_fMaxX << std::endl;
output_stream << m_bWrap << std::endl;
}
void Curve::fromStream(std::istream& isInputStream)
{
int iCtrlPtCount;
isInputStream >> iCtrlPtCount;
m_ptvCtrlPts.resize(iCtrlPtCount);
for (int iCtrlPt = 0; iCtrlPt < iCtrlPtCount; ++iCtrlPt) {
isInputStream >> m_ptvCtrlPts[iCtrlPt];
}
isInputStream >> m_fMaxX;
isInputStream >> m_bWrap;
m_bDirty = true;
}
void Curve::wrap(bool bWrap)
{
m_bWrap = bWrap;
m_bDirty = true;
}
bool Curve::wrap() const
{
return m_bWrap;
}
float Curve::evaluateCurveAt(const float x) const
{
reevaluate();
float value = 0.0f;
if (m_ptvEvaluatedCurvePts.size() == 1)
return m_ptvEvaluatedCurvePts[0].y;
if (m_ptvEvaluatedCurvePts.size() > 1) {
std::vector<Point>::iterator first_point = m_ptvEvaluatedCurvePts.begin();
std::vector<Point>::iterator last_point = m_ptvEvaluatedCurvePts.end() - 1;
bool evaluate_point_to_left_of_range = (first_point->x > x);
bool evaluate_point_to_right_of_range = (last_point->x < x);
if (evaluate_point_to_left_of_range) {
value = first_point->y;
}
else if (evaluate_point_to_right_of_range) {
value = last_point->y;
}
else {
std::vector<Point>::iterator point_one_iterator = first_point;
while ((point_one_iterator + 1)->x < x) {
++point_one_iterator;
#ifdef _DEBUG
assert(point_one_iterator != last_point);
#endif // _DEBUG
}
std::vector<Point>::iterator point_two_iterator = point_one_iterator + 1;
#ifdef _DEBUG
assert(point_one_iterator != m_ptvEvaluatedCurvePts.end());
assert(point_two_iterator != m_ptvEvaluatedCurvePts.end());
#endif // _DEBUG
Point point_one = *point_one_iterator;
Point point_two = *point_two_iterator;
if (point_one.x == point_two.x)
value = point_one.y;
else {
float slope = (point_two.y - point_one.y) / (point_two.x - point_one.x);
value = (x - point_one.x) * slope + point_one.y;
}
}
}
return value;
}
void Curve::scaleX(const float fScale)
{
for (std::vector<Point>::iterator control_point_iterator = m_ptvCtrlPts.begin();
control_point_iterator != m_ptvCtrlPts.end();
++control_point_iterator) {
control_point_iterator->x *= fScale;
}
m_fMaxX *= fScale;
m_bDirty = true;
}
void Curve::addControlPoint(const Point& point)
{
m_ptvCtrlPts.push_back(point);
sortControlPoints();
m_bDirty = true;
}
void Curve::removeControlPoint(const int iCtrlPt)
{
if (iCtrlPt < m_ptvCtrlPts.size() && m_ptvCtrlPts.size() > 2) {
m_ptvCtrlPts.erase(m_ptvCtrlPts.begin() + iCtrlPt);
m_bDirty = true;
}
}
/** This fxn allows removal of all ctrl points (not just down to 2) **/
void Curve::removeControlPoint2(const int iCtrlPt)
{
if (iCtrlPt < m_ptvCtrlPts.size()) {
m_ptvCtrlPts.erase(m_ptvCtrlPts.begin() + iCtrlPt);
m_bDirty = true;
}
}
int Curve::getClosestControlPoint(const Point& point, Point& ptCtrlPt) const
{
reevaluate();
int iMinDistPt = 0;
float fMinDistSquared = FLT_MAX;
for (int i = 0; i < m_ptvCtrlPts.size(); ++i) {
float delta_x = (m_ptvCtrlPts[i].x - point.x);
float delta_y = (m_ptvCtrlPts[i].y - point.y);
float fDistSquared = delta_x * delta_x + delta_y * delta_y;
if (fDistSquared < fMinDistSquared) {
iMinDistPt = i;
fMinDistSquared = fDistSquared;
ptCtrlPt = m_ptvCtrlPts[i];
}
}
return iMinDistPt;
}
void Curve::getClosestPoint(const Point& pt, Point& ptClosestPt) const
{
reevaluate();
ptClosestPt = Point(pt.x, evaluateCurveAt(pt.x));
}
float Curve::getDistanceToCurve(const Point& point) const
{
reevaluate();
Point ptEvaluatedPt(point.x, evaluateCurveAt(point.x));
float delta_x = point.x - ptEvaluatedPt.x;
float delta_y = point.y - ptEvaluatedPt.y;
float distance = sqrt(delta_x * delta_x + delta_y * delta_y);
return distance;
}
int Curve::controlPointCount() const
{
return m_ptvCtrlPts.size();
}
int Curve::segmentCount() const
{
reevaluate();
int iEvaluatedPtCount = m_ptvEvaluatedCurvePts.size();
if (iEvaluatedPtCount == 0) {
return 0;
}
else {
return iEvaluatedPtCount - 1;
}
}
void Curve::moveControlPoint(const int iCtrlPt, const Point& ptNewPt)
{
int iCtrlPtCount = m_ptvCtrlPts.size();
#ifdef _DEBUG
assert(iCtrlPt < iCtrlPtCount);
#endif // _DEBUG
if (iCtrlPt < iCtrlPtCount) {
m_ptvCtrlPts[iCtrlPt] = ptNewPt;
// adjust the x value so that it falls within the
// points right before and after it
if (iCtrlPt > 0) {
if (m_ptvCtrlPts[iCtrlPt].x < m_ptvCtrlPts[iCtrlPt - 1].x + s_fCtrlPtXEpsilon)
m_ptvCtrlPts[iCtrlPt].x = m_ptvCtrlPts[iCtrlPt - 1].x + s_fCtrlPtXEpsilon;
}
if (iCtrlPt < iCtrlPtCount - 1) {
if (m_ptvCtrlPts[iCtrlPt].x > m_ptvCtrlPts[iCtrlPt + 1].x - s_fCtrlPtXEpsilon)
m_ptvCtrlPts[iCtrlPt].x = m_ptvCtrlPts[iCtrlPt + 1].x - s_fCtrlPtXEpsilon;
}
}
m_bDirty = true;
}
void Curve::moveControlPoints(const std::vector<int>& ivCtrlPts, const Point& ptOffset,
const float fMinY, const float fMaxY)
{
int iCtrlPtCount = m_ptvCtrlPts.size();
#ifdef _DEBUG
for (int iCheck = 0; iCheck < ivCtrlPts.size(); ++iCheck) {
assert(ivCtrlPts[iCheck] < iCtrlPtCount);
}
#endif // _DEBUG
Point ptActualOffset = ptOffset;
int i;
// make sure the will be moved points will not run over other
// static control points. Also limit the y value.
for (i = 0; i < ivCtrlPts.size(); ++i) {
int iCtrlPt = ivCtrlPts[i];
if (m_ptvCtrlPts[iCtrlPt].y + ptActualOffset.y > fMaxY)
ptActualOffset.y = fMaxY - m_ptvCtrlPts[iCtrlPt].y;
if (m_ptvCtrlPts[iCtrlPt].y + ptActualOffset.y < fMinY)
ptActualOffset.y = fMinY - m_ptvCtrlPts[iCtrlPt].y;
if (iCtrlPt > 0) {
if (std::find(ivCtrlPts.begin(), ivCtrlPts.end(), iCtrlPt - 1) == ivCtrlPts.end()) {
if (m_ptvCtrlPts[iCtrlPt].x + ptActualOffset.x < m_ptvCtrlPts[iCtrlPt - 1].x + s_fCtrlPtXEpsilon)
ptActualOffset.x = m_ptvCtrlPts[iCtrlPt - 1].x + s_fCtrlPtXEpsilon - m_ptvCtrlPts[iCtrlPt].x;
}
}
else { // iCtrlPt == 0
if (m_ptvCtrlPts[iCtrlPt].x + ptActualOffset.x < 0.0f)
ptActualOffset.x = -m_ptvCtrlPts[iCtrlPt].x;
}
if (iCtrlPt < iCtrlPtCount - 1) {
if (std::find(ivCtrlPts.begin(), ivCtrlPts.end(), iCtrlPt + 1) == ivCtrlPts.end()) {
if (m_ptvCtrlPts[iCtrlPt].x + ptActualOffset.x > m_ptvCtrlPts[iCtrlPt + 1].x - s_fCtrlPtXEpsilon)
ptActualOffset.x = m_ptvCtrlPts[iCtrlPt + 1].x - s_fCtrlPtXEpsilon - m_ptvCtrlPts[iCtrlPt].x;
}
}
else { // iCtrlPt == iCtrlPtCount - 1
if (m_ptvCtrlPts[iCtrlPt].x + ptActualOffset.x > m_fMaxX)
ptActualOffset.x = m_fMaxX - m_ptvCtrlPts[iCtrlPt].x;
}
}
// move the control points
for (i = 0; i < ivCtrlPts.size(); ++i) {
int iCtrlPt = ivCtrlPts[i];
m_ptvCtrlPts[iCtrlPt].x += ptActualOffset.x;
m_ptvCtrlPts[iCtrlPt].y += ptActualOffset.y;
}
m_bDirty = true;
}
void Curve::drawCurve() const
{
reevaluate();
drawEvaluatedCurveSegments();
}
void Curve::drawEvaluatedCurveSegments() const
{
reevaluate();
glBegin(GL_LINE_STRIP);
for (std::vector<Point>::const_iterator it = m_ptvEvaluatedCurvePts.begin();
it != m_ptvEvaluatedCurvePts.end();
++it) {
glVertex2f(it->x, it->y);
}
glEnd();
}
void Curve::drawControlPoint(int iCtrlPt) const
{
reevaluate();
double fPointSize;
glGetDoublev(GL_POINT_SIZE, &fPointSize);
glPointSize(7.0);
glColor3d(1,0,0);
glBegin(GL_POINTS);
glVertex2f(m_ptvCtrlPts[iCtrlPt].x, m_ptvCtrlPts[iCtrlPt].y);
glEnd();
glPointSize(fPointSize);
}
void Curve::drawControlPoints() const
{
reevaluate();
double fPointSize;
glGetDoublev(GL_POINT_SIZE, &fPointSize);
glPointSize(7.0);
glColor3d(1,1,1);
glBegin(GL_POINTS);
for (std::vector<Point>::const_iterator kit = m_ptvCtrlPts.begin();
kit != m_ptvCtrlPts.end();
++kit) {
glVertex2f(kit->x, kit->y);
}
glEnd();
glPointSize(fPointSize);
}
void Curve::sortControlPoints() const
{
std::sort(m_ptvCtrlPts.begin(),
m_ptvCtrlPts.end(),
PointSmallerXCompare());
}
void Curve::reevaluate() const
{
if (m_bDirty) {
if (m_pceEvaluator) {
m_pceEvaluator->evaluateCurve(m_ptvCtrlPts,
m_ptvEvaluatedCurvePts,
m_fMaxX,
m_bWrap);
std::sort(m_ptvEvaluatedCurvePts.begin(),
m_ptvEvaluatedCurvePts.end(),
PointSmallerXCompare());
m_bDirty = false;
}
}
}
void Curve::invalidate() const
{
m_bDirty = true;
}
std::ostream& operator<<(std::ostream& output_stream, const Curve & curve_data)
{
curve_data.toStream(output_stream);
return output_stream;
}
std::istream& operator>>(std::istream& isInputStream, Curve & curve_data)
{
curve_data.fromStream(isInputStream);
return isInputStream;
}