-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstractPath.C
355 lines (293 loc) · 8.13 KB
/
abstractPath.C
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
/*
Copyright (C) 2004, Aseem Agarwala, roto@agarwala.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
#include <float.h>
#include "abstractPath.h"
//#include "GraphicsGems.h"
AbstractPath::AbstractPath() {
_tangents = NULL;
_inMotion = false;
//_centroidCalculated = false;
}
AbstractPath::AbstractPath(const AbstractPath& other) : DynArray<Vec2f,100>(other) {
_tangents = NULL;
_inMotion = false;
//_centroidCalculated = false;
}
AbstractPath::~AbstractPath() {
if (_tangents)
delete[] _tangents;
}
#define PHI 0.25f
#define MU -0.2564f
void AbstractPath::smooth() {
int ul = getNumElements()-1;
Vec2f offset, newVal;
for (int i=1; i<ul; i++) {
filterOffset(i,offset);
offset *= PHI;
newVal = getElement(i);
newVal += offset;
setElement(i,newVal);
}
}
void AbstractPath::sharpen() {
int ul = getNumElements()-1;
Vec2f offset, newVal;
for (int i=1; i<ul; i++) {
filterOffset(i,offset);
offset *= MU;
newVal = getElement(i);
newVal += offset;
setElement(i,newVal);
}
}
inline void AbstractPath::filterOffset(const int i, Vec2f& here) const {
Vec2f P1(getElement(i-1), getElement(i));
Vec2f P2(getElement(i+1), getElement(i));
float p1len = P1.Len(), p2len = P2.Len(), sum = p1len+p2len;
P1 *= p2len / sum;
P2 *= p1len / sum;
here = P1;
here += P2;
}
// CORRECTNESS: not calculating tangents across closed rotoPaths
Vec2f AbstractPath::tangent(const int i) const {
assert(i>=0 && i<getNumElements());
if (_tangents)
return _tangents[i];
Vec2f U;
if (i==0)
Vec2f_Sub(U,getElement(1), getElement(0));
else if (i==getNumElements()-1)
Vec2f_Sub(U,getElement(getNumElements()-1),
getElement(getNumElements()-2));
else {
Vec2f_Sub(U,getElement(i+1), getElement(i-1));
}
U.Normalize();
return U;
}
void AbstractPath::renderPS(FILE* fp) const {
Vec2f location;
for (int i=0; i<getNumElements(); i++) {
location = getElement(i);
fprintf(fp,"%f %f .5 .5 rectfill\n",location.x()-.25,
_globalh - location.y()-.25);
}
}
double AbstractPath::distanceToEnd(const Vec2f pt) const {
return getPointerToElement(getNumElements()-1)->distanceTo2(pt);
}
double AbstractPath::distanceToStart(const Vec2f pt) const {
return getPointerToElement(0)->distanceTo2(pt);
}
double AbstractPath::distanceTo2(const Vec2f pt, int& index) const {
int i, max = getNumElements();
float res=FLT_MAX,tmp;
for (i=0; i<max; i++) {
if ((tmp=getPointerToElement(i)->distanceTo2(pt)) < res) {
res = tmp;
index = i;
}
}
return ((double)res);
}
double AbstractPath::distanceTo2(const Vec2f pt) const {
int shit;
return distanceTo2(pt,shit);
}
void AbstractPath::writePtr(FILE* fp) const {
fwrite(_ptrDoc,sizeof(int),2,fp);
}
void AbstractPath::writePtr(QDataStream* fp) const {
*fp << _ptrDoc[0] << _ptrDoc[1];
}
void AbstractPath::writeNullPtr(FILE* fp) {
int dummy[2] = {-1,-1};
fwrite(&dummy,sizeof(int),2,fp);
}
void AbstractPath::writeNullPtr(QDataStream* fp) {
int dummy[2] = {-1,-1};
*fp << dummy[0] << dummy[1];
}
void AbstractPath::writeIntArray(FILE* fp, int* array, int size) {
assert(array && size>0);
fwrite(&size,sizeof(int),1,fp);
fwrite(array,sizeof(int),size,fp);
}
void AbstractPath::writeIntArray(QDataStream* fp, int* array, int size) {
assert(array && size>0);
*fp << size;
for (int i=0; i<size; ++i)
*fp << array[i];
}
void AbstractPath::readIntArray(QDataStream* fp, int* array, int size) {
if (array != NULL) {
for (int i=0; i<size; ++i)
*fp >> array[i];
}
else {
int j;
for (int i=0; i<size; ++i)
*fp >> j;
}
}
void AbstractPath::writeBool(QDataStream *fp, bool b) {
int i = b;
*fp << i;
}
void AbstractPath::readBool(QDataStream *fp, bool& b) {
int i;
*fp >> i;
assert(i==0 || i==1);
b = (bool)i;
}
void AbstractPath::print() const {
for (int i=0; i<getNumElements(); i++) {
Vec2f pt = getElement(i);
printf("%.4f %.4f\n",pt.x(),pt.y());
}
printf("\n");
}
#define _SSPACING_ 2.
void AbstractPath::resample(const int* numSamples, DynArray<float,100>* interpMe) {
int oldCount = getNumElements();
Vec2f* oldSamples = new Vec2f[oldCount];
float* oldVals = new float[oldCount];
memcpy(oldSamples, getData(), oldCount*sizeof(Vec2f));
memcpy(oldVals, interpMe->getData(), oldCount*sizeof(float));
resetElements();
interpMe->resetElements();
float m=0, totSegLen=0, L=0, n, s, segLen, st=0, rSegLen, tmpF;
int i;
Vec2f tmp;
for (i=1; i<oldCount; i++) {
Vec2f_Sub(tmp,oldSamples[i], oldSamples[i-1]);
L +=tmp.Len();
}
add(oldSamples[0]);
interpMe->add(oldVals[0]);
if (numSamples==NULL)
n = ceil(L/_SSPACING_);
else
n = float(*numSamples-1);
s = L / n; assert(finite(s));
for (i=1; i<oldCount; i++) {
Vec2f_Sub(tmp,oldSamples[i], oldSamples[i-1]);
segLen = tmp.Len();
totSegLen += segLen;
if (s-st <= segLen) {
m = (s-st)/segLen; //assert(m>=0 && m<=1);
Vec2f_LinInterp(tmp,oldSamples[i-1], oldSamples[i],m);
add(tmp);
tmpF = oldVals[i-1] + m*(oldVals[i]-oldVals[i-1]);
interpMe->add(tmpF);
rSegLen = segLen;
segLen -= (s-st);
while (segLen>s) {
m += s/rSegLen; //assert(m>=0 && m<=1);
//printf("in here\n");
Vec2f_LinInterp(tmp,oldSamples[i-1], oldSamples[i],m);
add(tmp);
tmpF = oldVals[i-1] + m*(oldVals[i]-oldVals[i-1]);
interpMe->add(tmpF);
segLen -= s;
} // endwhile
st = segLen;
}
else
st += segLen;
} // endfor
add(oldSamples[oldCount-1]);
interpMe->add(oldVals[oldCount-1]);
}
/*
void AbstractPath::centroidBbox(Vec2f* cent, Vec2f* dims) const {
cent->Set(0,0);
Vec2f curr;
Bboxf2D bbox;
for (int i=0; i<getNumElements(); i++) {
curr = getElement(i);
*cent += curr;
bbox.includePoint(curr);
}
*cent /= float(getNumElements());
_centroid = *cent;
_centroidCalculated = true;
dims->Set(bbox.width(), bbox.height()); // not completely right
// also, centroid may not be on curve
}
void AbstractPath::getCentroid(Vec2f& here) const {
if (!_centroidCalculated) {
here.Set(0,0);
Vec2f curr;
for (int i=0; i<getNumElements(); i++) {
curr = getElement(i);
here += curr;
}
here /= float(getNumElements());
_centroid = here;
_centroidCalculated = true;
}
else
here = _centroid;
}
*/
void AbstractPath::translate(const Vec2f& delta) {
for (int i=0; i<getNumElements(); i++)
(*getPointerToElement(i)) += delta;
_inMotion = true;
_motion += delta;
}
void AbstractPath::fixLoc() {
_motion.Set(0,0);
_inMotion = false;
}
void AbstractPath::renderDirection() const {
glBegin(GL_LINES);
for (int i=0; i<getNumElements(); i+=15) {
Vec2f tan = tangent(i);
Vec2f perp(-tan.y(), tan.x());
Vec2f ourLoc = getElement(i);
perp *= 5.f;
tan *= 5.f;
Vec2f left(ourLoc, perp);
left -= tan;
glVertex2f(left.x(), left.y());
glVertex2f(ourLoc.x(), ourLoc.y());
glVertex2f(ourLoc.x(), ourLoc.y());
ourLoc += perp;
ourLoc -= tan;
glVertex2f(ourLoc.x(), ourLoc.y());
}
glEnd();
}
void AbstractPath::nudge(const int i, const Vec2f& loc) {
printf("Nudge %d, %f %f\n",i,loc.x(), loc.y());
//int low = MIN(0,i-_nudgeRadius), high = MAX(size-1
}
void AbstractPath::modifyEnd(const int which, const Vec2f newloc) {
if (which==0)
setElement(0, newloc);
else if (which==1)
setElement(getNumElements()-1, newloc);
else
assert(0);
}
int AbstractPath::_globalh;
ImgSequence* AbstractPath::_is;
TalkFitCurve AbstractPath::_talkFit;
int AbstractPath::_nudgeRadius;