File: BezierCurve.cpp

package info (click to toggle)
criticalmass 1%3A1.0.0-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 17,180 kB
  • ctags: 10,844
  • sloc: ansic: 47,628; cpp: 25,173; sh: 11,803; xml: 3,532; perl: 3,271; makefile: 610; python: 66; awk: 40; lisp: 33
file content (417 lines) | stat: -rw-r--r-- 9,720 bytes parent folder | download | duplicates (11)
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
// Description:
//   Bezier curve template consisting of Bezier segments.
//
// Copyright (C) 2001 Frank Becker
//
// 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
//
#include <stdio.h>
#include <math.h>

#include <Trace.hpp>
#include <Bezier.hpp>
#include <BezierCurve.hpp>

#include <ResourceManager.hpp>

//Contruct Bezier curve with a given number of points per Bezier segment
template< class POINT >
BezierCurve<POINT>::BezierCurve( const int pps):
    _controlPoints(0),
    _numSegments(-1),
    _pointsPerSegment( pps)
{
    XTRACE();
//    LOG_INFO << _pointsPerSegment << " points per bezier segment" << endl;
}

//Cleanup
template< class POINT>
BezierCurve<POINT>::~BezierCurve()
{
    XTRACE();
    if( _controlPoints)
	delete[] _controlPoints;
}

//Change control point at given index
template< class POINT>
void BezierCurve<POINT>::SetControlPoint( int idx, const POINT &np)
{
    XTRACE();
    if( (idx>=0) && (idx<(_numSegments*3+1)))
    {
	_controlPoints[ idx] = np;
    }
}

//Get control point info
template< class POINT>
POINT BezierCurve<POINT>::GetControlPoint( int idx)
{
    XTRACE();
    if( (idx>=0) && (idx<(_numSegments*3+1)))
    {
	return( _controlPoints[ idx]);
    }

    POINT p;
    return( p);
}

//Add a segment to Bezier curve
template< class POINT>
void BezierCurve<POINT>::AddSegment( const POINT &p)
{
    XTRACE();
    if( _numSegments == -1)
    {
	_controlPoints = new POINT[1];
	_controlPoints[ 0] = p;
	_numSegments = 0;
    }
    else
    {
	_numSegments++;
	POINT *newControlPoints = new POINT[ 3*_numSegments+1];
	memcpy( newControlPoints, _controlPoints, 
	    sizeof( POINT) * (3*(_numSegments-1)+1));

	delete[] _controlPoints;
	_controlPoints = newControlPoints;

        //first control point of this segment is the last of the bezier curve
	POINT p1 = _controlPoints[ 3*_numSegments-3];
	POINT dp = (p-p1)/3.0;

        //place the middle 2 control points on line segment 
	//(i.e. new bezier segment is/straight line).
	_controlPoints[ 3*_numSegments-2] = p1+dp;
	_controlPoints[ 3*_numSegments-1] = p-dp;

	//last control point is the point that was passed in
	_controlPoints[ 3*_numSegments  ] = p;
    }
}

//Delete Bezier segments from-to.
template< class POINT>
void BezierCurve<POINT>::DeleteSegments( int fidx, int tidx)
{
    XTRACE();
    if( fidx==-1)
	fidx=0;

    fidx = fidx - (fidx%3);

    tidx = tidx + (3-(tidx%3));

    if( (tidx>3) && (tidx>(_numSegments*3)))
	tidx = _numSegments*3;

    if( (fidx<0) || (fidx>(_numSegments*3)) ||
	    (tidx<0) || (tidx>(_numSegments*3)))
    {
	return;
    }

    int nSegments = (tidx-fidx)/3;

    _numSegments -= nSegments;

    POINT *newControlPoints = new POINT[ 3*_numSegments+1];

    int i;
    for( i=0; i<=fidx; i++)
    {
	newControlPoints[ i] = _controlPoints[ i];
    }
    int j;
    for( j=1; i<(_numSegments*3+1); j++, i++)
    {
	newControlPoints[ i] = _controlPoints[ tidx+j];
    }

    delete[] _controlPoints;
    _controlPoints = newControlPoints;
}

//Move curve by vector p
template< class POINT>
void BezierCurve<POINT>::MoveCurve( const POINT &p)
{
    XTRACE();
    for( int i=0; i<(3*_numSegments+1); i++)
    {
	_controlPoints[ i] = _controlPoints[i] + p;
    }
}

//Scale curve by s
template< class POINT>
void BezierCurve<POINT>::ScaleCurve( const float s)
{
    XTRACE();
    for( int i=0; i<(3*_numSegments+1); i++)
    {
	_controlPoints[ i] = _controlPoints[ i] * s;
    }
}

//Get point on curve at time 0<=t<=_numSegments
template< class POINT>
void BezierCurve<POINT>::GetPos( float t, POINT &pos)
{
    XTRACE();
    if( (t<0.0) || (t>(float)_numSegments)) return;

    float seg = floor(t);
    float dt = t-seg;
    Bezier<POINT> bez( &_controlPoints[ (int)(seg*3)]);
    bez.Pos( dt, pos);
}

//Get tangent for point on curve at time 0<=t<=_numSegments
template< class POINT>
void BezierCurve<POINT>::GetTangent( float t, POINT &tangent)
{
    XTRACE();
    if( (t<0.0) || (t>(float)_numSegments)) return;

    float seg = floor(t);
    float dt = t-seg;
    Bezier<POINT> bez( &_controlPoints[ (int)(seg*3)]);
    bez.TVec( dt, tangent);
}

//Convert Bezier curve to an outline. Outline has to be deleted[] by client
template< class POINT>
void BezierCurve<POINT>::ConvertToOutline( POINT *&outline, int &numPoints)
{
    XTRACE();
    if( _numSegments <= 0)
    {
	outline = 0;
	numPoints = 0;
	return;
    }

    numPoints = _pointsPerSegment * _numSegments + 1;
    outline = new POINT[ numPoints];

    float dt = 1.0/_pointsPerSegment;

    int outlineIndex = 0;
    for( int i=0; i<_numSegments; i++)
    {
	Bezier<POINT> bez( &_controlPoints[ i*3]);
	float t=0;
	for( int tt=0; tt<_pointsPerSegment; tt++)
	{
	    bez.Pos( t, outline[ outlineIndex]);
	    outlineIndex++;
	    t+=dt;
	}
    }

    outline[ outlineIndex] = _controlPoints[ _numSegments*3];
}

//Convert partial Bezier curve to an outline. 
//Outline has to be deleted[] by client.
template< class POINT>
void BezierCurve<POINT>::ConvertToOutline(
	int fidx, int tidx, POINT *&outline, int &numPoints)
{
    XTRACE();
    if( fidx==-1)
	fidx=0;

    fidx = fidx - (fidx%3);

    tidx = tidx + (3-(tidx%3));

    if( (tidx>3) && (tidx>(_numSegments*3)))
	tidx = _numSegments*3;

    if( (fidx<0) || (fidx>(_numSegments*3)) ||
	    (tidx<0) || (tidx>(_numSegments*3)))
    {
	numPoints = 0;
	outline = 0;
	return;
    }

    int nSegments = (tidx-fidx)/3;

    numPoints = _pointsPerSegment * nSegments + 1;
    outline = new POINT[ numPoints];

    float dt = 1.0/_pointsPerSegment;

    int outlineIndex = 0;
    for( int i=0; i<nSegments; i++)
    {
	Bezier<POINT> bez( &_controlPoints[ fidx + i*3]);
	float t=0;
	for( int tt=0; tt<_pointsPerSegment; tt++)
	{
	    bez.Pos( t, outline[ outlineIndex]);
	    outlineIndex++;
	    t+=dt;
	}
    }

    outline[ outlineIndex] = _controlPoints[ fidx + nSegments*3];
}

//Convert Bezier curve to an outline with perpendicular vectors. 
//Outline and pvecs have to be deleted[] by client.
template< class POINT>
void BezierCurve<POINT>::ConvertToOutlineWithPVec(
	POINT *&outline, POINT *&pvecs, int &numPoints)
{
    XTRACE();

    if( _numSegments <= 0)
    {
	outline = 0;
	numPoints = 0;
	return;
    }

    numPoints = _pointsPerSegment * _numSegments + 1;
    outline = new POINT[ numPoints];
    pvecs   = new POINT[ numPoints];

    float dt = 1.0/_pointsPerSegment;

    int outlineIndex = 0;
    for( int i=0; i<_numSegments; i++)
    {
	Bezier<POINT> bez( &_controlPoints[ i*3]);
	float t=0;
	for( int tt=0; tt<_pointsPerSegment; tt++)
	{
	    bez.Pos( t, outline[ outlineIndex]);
	    bez.PVec( t, pvecs[ outlineIndex]);
	    outlineIndex++;
	    t+=dt;
	}
	if( i==(_numSegments-1))
	{
	    bez.Pos( t, outline[ outlineIndex]);
	    bez.PVec( t, pvecs[ outlineIndex]);
	}
    }
    //  outline[ outlineIndex] = _controlPoints[ _numSegments*3];
}

//Save Bezier curve to file.
template< class POINT>
bool BezierCurve<POINT>::Save( const char *filename)
{
    XTRACE();
    FILE *fp;
    if( (fp=fopen(filename,"w")) != NULL)
    {
        int dim = POINT::dimension();
	fprintf( fp, "%d\n", _pointsPerSegment); 
	fprintf( fp, "%d\n", _numSegments); 
	for( int idx=0; idx<(_numSegments*3+1); idx++)
	{
            for( int d=0; d<dim; d++)
            {
		fprintf( fp, "%f ", (_controlPoints[ idx])[d]);
            }
	    fprintf( fp, "\n");
	}
	fclose( fp);
    } 
    else
    {
        LOG_ERROR << "Could not create " << filename << endl;
	return false;
    }
    return true;
}

//Load Bezier curve from file.
template< class POINT>
bool BezierCurve<POINT>::Load( const char *filename)
{
    XTRACE();

    if( !ResourceManagerS::instance()->selectResource( string(filename)))
    {
	LOG_ERROR << "Curve [" << filename << "] not found." << endl;
        return false;
    }
    ziStream &infile = ResourceManagerS::instance()->getInputStream();

    string line;

    int oldPointsPerSegment = _pointsPerSegment;
    int oldNumSegments = _numSegments;
    getline( infile, line);
    if( sscanf( line.c_str(), "%d\n", &_pointsPerSegment) != 1)
    {
	LOG_ERROR << "Expected points per segment" << endl;
	return false;
    }

    getline( infile, line);
    if( sscanf( line.c_str(), "%d\n", &_numSegments) != 1)
    {
	_pointsPerSegment = oldPointsPerSegment;
	LOG_ERROR << "Expected number of segments" << endl;
	return false;
    }

    if( oldNumSegments != _numSegments)
    {
	delete[] _controlPoints;
	_controlPoints = new POINT[ 3*_numSegments+1];
    }

    for( int idx=0; idx<(_numSegments*3+1); idx++)
    {
//	LOG_INFO << "Loading control point #" << idx << endl;
//FIXME: only support 1d, 2d and 3d bezier curves...
	float val[3];
	getline( infile, line);
	int dim = sscanf( line.c_str(), "%f %f %f\n", &val[0], &val[1], &val[2]);

	if( dim != POINT::dimension())
	{
	    for( int i=dim; i<POINT::dimension(); i++)
	    {
		//fill missing dimensions with 0
		val[i] = 0.0;
	    }
#if 0
	    delete[] _controlPoints;
	    _controlPoints = 0;
	    _pointsPerSegment = oldPointsPerSegment;
	    _numSegments = 0;
	    LOG_ERROR << "Expected another " 
		      << POINT::dimension() << "-tuple" 
		      << "in [" << filename << "]" << endl;
	    return false;
#endif
	}
	for( int i=0; i<dim; i++)
	{
	    _controlPoints[ idx].set( i, val[i]);
	}
    }

    return true;
}