-
Notifications
You must be signed in to change notification settings - Fork 2
/
curven.hpp
1342 lines (1201 loc) · 47.8 KB
/
curven.hpp
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *****************************************************************************
** *****************************************************************************
**
** CurveNDimension Lib is a C++ library for handling n-dimensional curves, or,
** actually, n-dimensional polygonal lines.
**
** Copyright (C) 2011-2015 Emilio Vital Brazil - emilio.brazil@gmail.com and
** Claudio Esperanca - esperanc@cos.ufrj.br
**
** This file is part of CurveNDimension.
**
** CurveNDimension is free software: you can redistribute it and/or modify
** it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
** the Free Software Foundation, either version 2.1 of the License, or
** (at your option) any later version.
**
** CurveNDimension 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 CurveNDimension. If not, see <http://www.gnu.org/licenses/>.
**
** *****************************************************************************
** ****************************************************************************/
#ifndef __CURVEN_HPP__
#define __CURVEN_HPP__
#include <vector>
#include <queue>
#include <cassert>
#include <limits>
#include "vectorn.hpp"
#include "catmullrom.hpp"
/// Infinity constant
#ifndef INF
#define INF std::numeric_limits<real>::max()
#endif
/// Epsilon constant
#ifndef EPS
#define EPS std::numeric_limits<real>::epsilon()
#endif
/**
* \class PolygonalCurve \anchor PolygonalCurve
* \brief N-dimensional polygonal chain.
*
* This templated class represents an open or closed Polygonal Chain in n dimensions.
* It requires Point and Vector types for which a standard implementation is
* provided. If desired, the user may provide a custom implementation of these
* classes or, better yet, derive and customize them.
*
* \param real is the type used for storing each cartesian coordinate of each point.
* \param dim is the number of dimensions of each point.
* \param Point each vertex of the polygonal line is stored as an object of this class.
* \param Vector objects of this class are returned whenever a vector in dim dimensions is
* required.
*
**/
template < typename real , unsigned int dim , class Point = PointN<real,dim> ,
class Vector = VectorN<real,dim> >
class PolygonalCurve {
protected:
/// \brief a Smooth Step function (see smoothstep in wikipedia).
///
/// A simple smooth function that maps the unit interval to itself.
/// Smoothness is guaranteed by having the derivatives at 0 and 1 to
/// be zero. (see smoothstep in Wikipedia)
///
/// \param x a value that is constrained to be between 0 and 1
/// \return a value between 0 and 1
static inline double smoothStep (double x) {
if (x < 0) x = 0;
else if (x>1) x = 1;
return x*x*(3 - 2*x);
// alternatively
// return x*x*x*(x*(x*6 - 15) + 10);
}
/// \brief class used by the douglasPeuckerRank method.
///
/// This is used as an element in the priority queue needed
/// by the douglasPeuckerRank method.
struct DPitem {
unsigned first; ///< index of the first vertex of the range
unsigned last; ///< index of the last vertex of the range
unsigned farthest; ///< index between first and last which is farthest from
/// the line segment defined by the vertices first and last
double dist; ///< squared perpendicular distance from the line segment
/// \brief Constructor.
///
/// Builds an item corresponding to the a vertex range of a polygonal curve.
///
/// \param f index of first point in poly
/// \param l index of last point in poly
/// \param poly polygonal curve which is being generalized
DPitem (unsigned f, unsigned l, const PolygonalCurve<real,dim,Point,Vector>& poly)
: first(f), last(l)
{
assert (last-first > 1);
// Compute farthest point and its distance to line first-last
const Point& p0 = poly [first];
const Point& p1 = poly [last];
Vector v = p1-p0; // Direction vector from p0 to p1
unitize (v); // Make it a unit vector
farthest = first;
dist = -1;
for (unsigned i = first+1; i < last; i++) {
const Point& p = poly [i];
Point pr = p0+((p-p0)*v)*v; // p projected onto line p0-p1
double d = pr.dist2(p); // Squared distance
if (d > dist) { // Keep index of the farthest point
farthest = i;
dist = d;
}
}
}
/// Operator <
inline bool operator < (const DPitem& other) const { return dist < other.dist; }
};
public:
/// Empty constructor - builds an open polygonal line with 0 vertices.
PolygonalCurve( void ) { pIsClosed = false ; }
/// \brief Copy constructor.
///
/// Builds the curve as a copy of another polygonal curve.
/// \param curve: another polygonal curve.
PolygonalCurve( const PolygonalCurve<real,dim,Point,Vector> &curve ) {
this->copy(curve) ;
}
/// \brief Constructor from an array.
///
/// Constructor from an array (stl vector data type) of Points.
/// \param points an array of points.
PolygonalCurve( const std::vector<Point> &points ) {
pPoints = points;
if ( pPoints.size() > 1 && points[ pPoints.size()-1 ] == pPoints[ 0 ] )
{
pIsClosed = true ;
pPoints.pop_back();
}
else pIsClosed = false ;
}
/// \brief Copy helper method.
///
/// Replaces this object with a copy of another curve.
/// \param curve Makes this object a copy of curve.
virtual void copy( const PolygonalCurve<real,dim,Point,Vector> &curve ) {
pPoints = curve.pPoints;
pIsClosed = curve.pIsClosed;
}
/// \brief Assignment operator
///
/// Makes this object a copy of the given curve.
/// \param curve: another polygonal curve.
virtual PolygonalCurve &operator=( const PolygonalCurve<real,dim,Point,Vector> &curve ) {
this->copy(curve);
return (*this);
}
/// \brief Indexing operator.
///
/// Returns the reference to point at index i
/// \param i the index.
virtual Point& operator[](unsigned i) { assert(i<size()) ; return pPoints[i] ; }
/// \brief Indexing operator.
///
/// Returns the value of point at index i
/// \param i: the index.
virtual Point operator[](unsigned i) const { assert(i<size()) ; return pPoints[i] ; }
/// Number of points of the curve
virtual unsigned int size( void ) const { return (unsigned int)pPoints.size() ; }
/// \brief Resets line to an empty open curve.
virtual void clear( void ) { pPoints.clear() ; pIsClosed = false; }
//
// Set and gets
//
/// \brief Replaces the points of this curve by points in an array.
///
/// Changes the internal representation of the geometry to points in a given array
/// \param points: an array of points
virtual void setPoints( const std::vector<Point> &points ) { pPoints = points ; }
/// \brief Returns a copy of the geometry as an array of points
///
/// \return an array of points.
virtual std::vector<Point> getPoints( void ) const { return pPoints ; }
/// \brief Element assignment.
/// Alters the i'th point
/// \param index the index.
/// \param p new point.
virtual void setPoint( unsigned int index , const Point& p ){
assert(index<size()) ; pPoints[index] = p ;
}
/// \brief Returns a copy of the i'th point in the curve.
/// \param i: index.
virtual Point at( unsigned int i ) const {
if( isClosed() ) i = i%this->size();
assert(i<size()) ;
return pPoints[i] ;
}
/// \brief Returns a copy of the first point in the curve.
///
/// Requires a non-empty curve.
/// \return the first point.
virtual Point atBegin( void ) const { assert(size()>0) ; return pPoints[0] ; }
/// \brief Returns the last point in the curve.
///
/// Requires a non-empty curve.
/// \return the last point.
virtual Point atEnd( void ) const { assert(size()>0) ; return pPoints[ size()-1 ] ; }
/// \brief Curve trimming.
///
/// Removes the last point of the curve
virtual void pop_back(void) { pPoints.pop_back(); }
/// \brief Curve extension.
///
/// Appends another point to the end of the curve.
/// \param p the point to be appended.
virtual void push_back( const Point& p ) { pPoints.push_back(p) ; }
/// \brief Curve extension.
///
/// Adds another point to the curve.
/// \param p new point.
virtual void add( const Point& p ) {
pPoints.push_back(p) ;
}
/// \brief Point insertion.
///
/// Inserts a point at a given index.
/// \param index the index.
/// \param p point to be inserted.
virtual void insert( unsigned int index , const Point& p ) {
if( index < size()) pPoints.insert( pPoints.begin()+index , p ); else add(p) ;
}
/// \brief copy part of the curve between indexes idxStart and idxEnd
/// inclusive if idxStart < idxEnd returns a reverse curve version
/// \param idxStart the start index to be copied.
/// \param idxEnd the end index to be copied
/// \return a curve that contains the points between idxStart and idxEnd
virtual PolygonalCurve<real,dim,Point,Vector>
copyRange( unsigned int idxStart , unsigned int idxEnd ) const
{
unsigned int p0Idx = 0 , p1Idx = this->size() ;
bool revert = false ;
if( idxStart <= idxEnd )
{
p0Idx = idxStart ;
p1Idx = idxEnd ;
}
else
{
p0Idx = idxEnd ;
p1Idx = idxStart ;
revert = true ;
}
if ( p0Idx > this->size() -1 )
return PolygonalCurve<real,dim,Point,Vector> ();
p1Idx = ( p1Idx < this->size() ) ? p1Idx + 1 : this->size() ;
std::vector<Point> points( pPoints.begin()+p0Idx ,
pPoints.begin()+p1Idx ) ;
PolygonalCurve<real,dim,Point,Vector> c(points) ;
if(revert) c.reverse();
return c ;
}
/// \brief Closes or opens curve.
///
/// Makes this a closed/open curve.
/// \param b whether the curve must be closed (true) or not (false).
virtual void close( bool b = true ) { pIsClosed = b ; }
/// Makes this an open curve (polyline).
virtual void open( void ) { pIsClosed = false ; }
/// \brief tells whether the curve is closed.
/// \return true iff curve is closed.
virtual bool isClosed() const { return pIsClosed ; }
/// \brief tells whether the curve is closed.
/// \return true iff the size is 0.
virtual bool isEmpty() const { return this->size() == 0 ; }
/// Makes line follow the reverse circulation.
virtual void reverse( void ){
std::vector<Point> tmp;
for( unsigned int i = size() ; i > 0 ; --i )
{
tmp.push_back( pPoints[i-1] );
}
pPoints = tmp;
}
/// \brief Bounding box of the curve.
/// \param min (output) point with minimum coordinates.
/// \param max (output) point with maximum coordinates.
virtual void minMax( Point& min , Point& max ) const {
max = -INF, min = INF ;
for( unsigned int i = 0 ; i < size() ; i++ )
{
for( unsigned int j = 0 ; j < dim ; j++ )
{
max[j] = ( at(i)[j] > max[j] ) ? at(i)[j] : max[j] ;
min[j] = ( at(i)[j] < min[j] ) ? at(i)[j] : min[j] ;
}
}
}
/// Computes the arc length of the curve.
/// \return the length of the curve.
virtual real length( void ) const {
real len = 0;
Vector vtmp;
for( unsigned int i = 1 ; i < size() ; i++ )
{
vtmp = pPoints[i]-pPoints[i-1];
len += vtmp.norm();
}
if(isClosed()) vtmp = atBegin() - atEnd() , len += vtmp.norm() ;
return len;
}
/// \brief Computes the length of the curve up to a given vertex.
/// \param k index of the vertex (between 0 and size()-1)
/// \return length.
virtual real length( unsigned int k ) const {
real len = 0;
k = ( k < size() )? k+1 : size();
for( unsigned int i = 1 ; i < k ; i++ )
{
Vector vtmp = pPoints[i]-pPoints[i-1];
len += vtmp.norm();
}
return len;
}
/// Returns the centroid (barycenter) of the vertex points
virtual Point centroid( void ) const {
Vector barTmp;
for( unsigned int i = 0 ; i < size() ; i++ ) barTmp += at(i).toVector();
barTmp /= size() ;
return Point( barTmp );
}
/// \brief Computes the distance between a point and the curve.
/// \param p a point.
/// \return Euclidean distance between p and curve.
virtual real distanceTo( const Point& p ) const {
unsigned int index ; real u ; Point pr ;
return projectPoint( p , index , u , pr ) ;
}
/// \brief Computes distance between a point and the curve.
/// \param p input point.
/// \param index (output) index of the vertex just before the segment on
/// which the projection of p on the curve lies.
/// \param parameterU (output) relative length of the projected point with
/// respect to the total length of the curve.
/// \param pr (output) closest point on the curve to p.
/// \return distance between p and pr.
virtual real projectPoint( const Point& p , unsigned int& index ,
real& parameterU , Point& pr ) const {
real dist = INF, uTmp1 = 0.0, uTmp2 = 0.0 ;
parameterU = 0.0;
if( size() > 0 )
{
index = 0 ;
Point prTmp = at(0);
pr = prTmp;
dist = ( atBegin() - p ).norm() ;
real distTMP = INF , invL = 0.0 ;
if( length()>EPS) invL = 1.0/length();
for( unsigned int i = 1 ; i < size() ; i++ )
{
uTmp1 = uTmp2;
unsigned int iTmp = i;
Vector u = at(i) - at(i-1);
Vector v = p - at(i-1);
Vector w = p - at(i);
real normQ = u.norm2();
real alpha = 0.0;
real distU = u.norm();
real distV = v.norm();
real distW = w.norm();
if( normQ > EPS ) alpha = (u*v)/normQ ;
if( alpha > 0.0 && alpha < 1.0 )
{
prTmp = at(i-1) + alpha*u;
distTMP = (prTmp-p).norm();
uTmp1 += distU*invL*alpha;
}
else if ( distV < distW ) distTMP = distV, prTmp = at(i-1) ;
else distTMP = distW , ++iTmp ,
uTmp1 += distU*invL , prTmp = at(i) ;
if( distTMP < dist )
dist = distTMP , index = iTmp - 1 ,
parameterU = uTmp1 , pr = prTmp;
uTmp2 += distU*invL;
}
if(isClosed())
{
uTmp1 = uTmp2;
unsigned int iTmp = size();
Vector u = atBegin() - atEnd();
Vector v = p - atEnd();
Vector w = p - atBegin();
real normQ = u.norm2();
real alpha = 0.0;
real distU = u.norm();
real distV = v.norm();
real distW = w.norm();
if( normQ > EPS ) alpha = (u*v)/normQ ;
if( alpha > 0.0 && alpha < 1.0 )
{
prTmp = atEnd() + alpha*u;
distTMP = (prTmp-p).norm();
uTmp1 += distU*invL*alpha;
}
else if ( distV < distW ) distTMP = distV, prTmp = atEnd() ;
else distTMP = distW , iTmp = 0 ,
uTmp1 += distU*invL , prTmp = atBegin() ;
if( distTMP < dist )
dist = distTMP , index = iTmp - 1 ,
parameterU = uTmp1 , pr = prTmp;
}
}
return dist;
}
//
// Parameters
//
/// \brief Computes the relative distance between a vertex and the beginning of the curve.
/// \param i index of the vertex (between 0 and size()-1)
/// \return relative length (between 0 and 1)
virtual real getParameter( unsigned int i ) const { return length(i)/length() ; }
/// \brief Computes C(u) for u in [0,1].
///
/// Computes C(u), assuming the curve is parameterized by arc length,
/// i.e., C(0) is the first point, C(1) is the last point.
/// \param u a value between 0 and 1;
/// \param p (output) the point at C(u);
/// \param alpha (output) if C(u) is between vertex i and vertex i+1, returns
/// the interpolation ratio between them, such that alpha=0 means C(u) is P(i)
/// and alpha=1 means C(u) = P(i+1)
/// \return the index of the point just before u is reached.
virtual unsigned int eval( real u , Point& p , real& alpha ) const {
if( size() == 0 ) return 0;
real l = length();
if( u >= 1.0 )
{
p = at(size()-1);
return size()-1;
}
if( u <= 0.0 || l == 0 )
{
p = at(0);
alpha = 0.0;
return 0;
}
real il = 1.0/l;
real aux1 = 0 ;
real aux2 = 0 ;
real lenTmp = 0 ;
unsigned int index = 0 ;
Vector vtmp;
while( aux1 <= u )
{
lenTmp = aux1;
index++;
vtmp = pPoints[index]-pPoints[index-1];
aux2 = vtmp.norm()*il;
aux1 += aux2;
}
alpha = (u - lenTmp)/aux2;
index--;
p = at(index) + alpha*vtmp;
return index;
}
/// \brief Computes C(u) for u in [0,1].
///
/// Computes C(u), assuming the curve is parameterized by arc length,
/// i.e., C(0) is the first point, C(1) is the last point.
/// \param u a value between 0 and 1;
/// \return C(u).
virtual Point eval( real u ) const { Point p ; eval( u , p ) ; return p ; }
/// \brief Computes C(u) for u in [0,1].
///
/// Computes C(u), assuming the curve is parameterized by arc length,
/// i.e., C(0) is the first point, C(1) is the last point.
/// \param u a value between 0 and 1;
/// \param p (output) the point at C(u);
/// \return the index of the point just before u is reached.
virtual unsigned int eval( real u , Point& p ) const { real alpha ; return eval( u , p , alpha ); }
/// \brief Estimated tangent at a given vertex.
/// \param index: index between 0 and size()-1.
/// \return estimated tangent.
virtual Vector tangentAt( unsigned int index ) const {
Vector tan;
unsigned int nofp = this->size();
if( nofp > 1 )
{
if( ( index < nofp-1 ) && ( index > 0 ) )
tan = ( at(index+1) - at(index-1) )*.5 ;
else if( isClosed() )
tan = ( at( (index+1) % nofp ) - at( (int(index)-1) % nofp ) ) *.5;
else if ( index > 0 )
tan = at(index) - at(index-1);
else tan = at(1) - at(0);
}
return tan;
}
/// \brief Tangent at u.
///
/// Estimates a tangent vector at a given point C(u), where u in [0,1].
/// \param u: relative distance along the curve from the beginning of the curve.
/// \return : the estimated tangent at C(u).
virtual Vector tangentEval( real u ) const {
Vector tan;
if( size() > 1 )
{
Point p;
real alpha;
unsigned int index = eval( u , p , alpha );
if( alpha > EPS )
{
if( index != size()-1 ) tan = at(index+1) - at(index);
else if ( isClosed() ) tan = at(0) - at(index);
else tan = at(index-1) - at(index);
}
else tan = tangentAt(index);
}
return tan;
}
/// \brief Tangent at u.
///
/// Smoothly interpolated tangent at a point along the curve.
/// \param u: relative distance along the curve from the beginning of the curve.
/// \return : estimated tangent.
virtual Vector tangentEvalContinuous( real u ) const {
Vector tan;
if( size() > 1 )
{
Point p;
real alpha;
unsigned int index = eval( u , p , alpha );
if( index != size()-1 ) tan = alpha * tangentAt(index) + (1.0-alpha)* tangentAt(index+1);
else tan = tangentAt(index);
}
return tan;
}
//
// Filters
//
/// \brief Chaikin supersampling.
/// \param numberOfsimplifications number of subdivisions.
virtual void chaikinSubDivide( unsigned int numberOfsimplifications = 1 ){
if (size() == 0) return;
if( isClosed() )
{
for( unsigned int n = 0 ; n < numberOfsimplifications ; ++n )
{
std::vector<Point> tmp;
// step over every 2 points
for( unsigned int i = 1 ; i < size() ; ++i )
{
// get original points
const Vector p0 = pPoints[i-1].toVector();
const Vector p1 = pPoints[i ].toVector();
// calculate the original point
Vector Q = 0.75*p0 + 0.25*p1 ;
tmp.push_back( Point(Q) );
Vector R ;
R = 0.25*p0 + 0.75*p1 ;
tmp.push_back( Point(R) );
}
// get original points
const Vector p0 = atEnd().toVector();
const Vector p1 = atBegin().toVector();
// calculate the original point
Vector Q = 0.75*p0 + 0.25*p1 ;
tmp.push_back( Point(Q) );
Vector R ;
R = 0.25*p0 + 0.75*p1 ;
tmp.push_back( Point(R) );
// copy over pPoints
pPoints = tmp ;
}
}
else
{
for( unsigned int n = 0 ; n < numberOfsimplifications ; ++n )
{
std::vector<Point> tmp;
// keep the first point
tmp.push_back( pPoints[0] );
// step over every 2 points
for( unsigned int i = 1 ; i < size() ; ++i )
{
// get original points
const Vector p0 = pPoints[i-1].toVector();
const Vector p1 = pPoints[i ].toVector();
// calculate the original point
Vector Q = 0.75*p0 + 0.25*p1 ;
tmp.push_back( Point(Q) );
Vector R ;
R = 0.25*p0 + 0.75*p1 ;
tmp.push_back( Point(R) );
}
tmp.push_back(pPoints[ size()-1 ] );
// copy over pPoints
pPoints = tmp ;
}
}
}
/// \brief Chaikin simplification of the polygonal line.
/// \param numberOfSimplifications number of simplifications
virtual void chaikinFilter( unsigned int numberOfSimplifications = 1 ) {
if (size() == 0) return;
if( isClosed() )
{
for( unsigned int n = 0 ; n < numberOfSimplifications ; ++n )
{
std::vector<Point> tmp;
// make sure we do not lose any points!!
if ( size() <= 6 ) return;
// step over every 2 points
for( unsigned int i = 1 ; i < ( size() - 2 ) ; i += 2 )
{
// get original points
const Vector p0 = pPoints[i-1].toVector();
const Vector p1 = pPoints[i ].toVector();
const Vector p2 = pPoints[i+1].toVector();
const Vector p3 = pPoints[i+2].toVector();
// calculate the original point
Vector Q = -0.25*p0 + 0.75*p1 + 0.75*p2 - 0.25*p3;
// add to new curve
tmp.push_back( Point(Q) );
}
if( size()%2 != 0 )
{
unsigned int i = size() - 1 ;
// get original points
const Vector p0 = pPoints[i-1].toVector();
const Vector p1 = pPoints[i ].toVector();
const Vector p2 = pPoints[0].toVector();
const Vector p3 = pPoints[1].toVector();
// calculate the original point
Vector Q = -0.25*p0 + 0.75*p1 + 0.75*p2 - 0.25*p3;
// add to new curve
tmp.push_back( Point(Q) );
}
else
{
unsigned int i = size() - 2 ;
// get original points
const Vector p0 = pPoints[i-1].toVector();
const Vector p1 = pPoints[i ].toVector();
const Vector p2 = pPoints[i+1].toVector();
const Vector p3 = pPoints[0].toVector();
// calculate the original point
Vector Q = -0.25*p0 + 0.75*p1 + 0.75*p2 - 0.25*p3;
// add to new curve
tmp.push_back( Point(Q) );
}
// copy over pPoints
pPoints = tmp ;
}
}
else
{
for( unsigned int n = 0 ; n < numberOfSimplifications ; ++n )
{
std::vector<Point> tmp;
// make sure we do not loose any points!!
if (size() <= 8) return;
// keep the first point
tmp.push_back( pPoints[0] );
Point p2 = -0.5*pPoints[0].toVector() + pPoints[1].toVector() +
0.75*pPoints[2].toVector() - 0.25*pPoints[3].toVector();
tmp.push_back( Point(p2) );
// step over every 2 points
for( unsigned int i = 2 ; i < ( size() - 5 ) ; i += 2 )
{
// get original points
const Vector p0 = pPoints[i ].toVector();
const Vector p1 = pPoints[i + 1].toVector();
const Vector p2 = pPoints[i + 2].toVector();
const Vector p3 = pPoints[i + 3].toVector();
// calculate the original point
Vector Q = -0.25*p0 + 0.75*p1 + 0.75*p2 - 0.25*p3 ;
// add to new curve
tmp.push_back( Point(Q) );
}
unsigned int lastIndex = size() - 1 ;
Vector pL = -0.25*pPoints[lastIndex-3].toVector() +
0.75*pPoints[lastIndex-2].toVector() +
pPoints[lastIndex-1].toVector() - 0.50*pPoints[lastIndex].toVector();
tmp.push_back( Point(pL) );
tmp.push_back(pPoints[ lastIndex ]);
// copy over pPoints
pPoints = tmp ;
}
}
}
/// \brief Simple supersampling.
/// \param step makes sure each two consecutive vertices are separated by at least this
/// amount.
virtual void superSample( real step ){
if (size() == 0) return;
PolygonalCurve tmpLine;
tmpLine.add( pPoints[0] );
for( unsigned int i = 0 ; i < size() - 1 ; ++i )
{
Vector tmpV = pPoints[i+1] -pPoints[i] ;
real tmpNorm = tmpV.norm() ;
if( tmpNorm > step )
{
Vector base = pPoints[i].toVector() ;
real invNorm = 1/tmpNorm ;
Vector unitV = invNorm*tmpV ;
unsigned int j = 1 ;
while( (j*step) <= ( tmpNorm - step ) )
{
Vector newP = base + (j*step)*unitV ;
tmpLine.add( Point( newP ) );
++j;
}
}
tmpV = pPoints[i+1] -tmpLine.atEnd() ;
tmpNorm = tmpV.norm() ;
if( tmpNorm > 0.8*step )
{
tmpLine.add( pPoints[i+1] );
}
}
if(isClosed())
{
Vector tmpV = atBegin() - atEnd() ;
real tmpNorm = norm( tmpV ) ;
// real step = 0.5f;
if( tmpNorm > step )
{
Vector base = atEnd().toVector() ;
real invNorm = 1/tmpNorm ;
Vector unitV = invNorm*tmpV ;
unsigned int j = 1 ;
while( (j*step) <= ( tmpNorm - step ) )
{
Vector newP = base + (j*step)*unitV ;
tmpLine.add( Point( newP ) );
++j;
}
}
}
tmpLine.close( isClosed() );
*this = tmpLine;
}
/// \brief Applies the superSample(step) and chaikinFilter( numberOfInteractions ).
///
/// Useful to clean up hand-drawn input curves.
/// \param step: maximum distance between consecutive vertices in supersampling.
/// \param numberOfInteractions: how many simplification steps are performed.
virtual void lineFilter( real step = 0.5 , unsigned int numberOfInteractions = 5 ){
if (size() == 0) return;
this->superSample( step );
this->chaikinFilter( numberOfInteractions );
}
/// Mean filter, i.e, each vertex v(i) at i, is replaced by (v(i-1)+v(i)*3+v(i+1))/5.
virtual void meanFilter( void ){
if( size() > 1 )
{
Vector v;
std::vector<Point> tmp;
if(isClosed())
{
v = ( atEnd().toVector()+3.0*at(0).toVector()+at(1).toVector() )/5.0;
tmp.push_back( Point( v ) );
}
else tmp.push_back( atBegin() );
for( unsigned int i = 1 ; i < size()-1 ; ++i )
{
v = ( at(i-1).toVector()+3.0*at(i).toVector()+at(i+1).toVector() )/5.0;
tmp.push_back( Point( v ) );
}
if (isClosed())
{
v = ( at(size()-2).toVector()+3.0*atEnd().toVector()+at(0).toVector() )/5.0;
tmp.push_back( Point( v ) );
}
else tmp.push_back( atEnd() );
pPoints = tmp;
}
}
/// \brief edits curve by dragging a point to a new position.
///
/// Deforms the curve by displacing vertex p[k] to a new position pk.
/// Neighboring vertices whose distance along the curve to p[k] are less than
/// | pk-p[k] | * factor are also displaced smoothly.
/// \param k index of the anchor point which is the basis for the deformation.
/// \param pk New position of point k.
/// \param factor proportionality constant for determining the neighborhood of
/// the anchor point - larger values mean a bigger neighborhood.
inline void smoothDeform (unsigned k, const Point& pk, real factor = 1.0) {
Vector v = pk - at(k);
real vlen = v.norm();
real maxLen = vlen * factor;
real di = 0;
real dj = 0;
Point p0 = (*this) [k];
(*this) [k] = pk;
Point previ = p0;
Point prevj = p0;
int i = k ;
int j = k ;
bool iIsValid = true ;
bool jIsValid = true ;
int nofp = this->size() ;
if( nofp > 1 )
{
while ( iIsValid || jIsValid )
{
if( i == 0 && !this->isClosed() ) iIsValid = false ;
if( j == nofp-1 && !this->isClosed() ) jIsValid = false ;
if( iIsValid )
{
i = (i==0) ? (nofp-1) : i-1 ;
Point pi = at(i);
di += (pi-previ).norm();
if ( di > maxLen) iIsValid = false;
if( iIsValid )
{
previ = pi;
pi += v * smoothStep((maxLen-di)/maxLen);
(*this) [i] = pi;
if( (previ-pi).norm() <= ZERO_VECTOR ) iIsValid = false ;
}
}
if( jIsValid )
{
j = ( j == nofp-1 ) ? 0 : j+1 ;
Point pj = at(j);
dj += (pj-prevj).norm();
if ( dj > maxLen ) jIsValid = false;
if( jIsValid )
{
prevj = pj;
pj += v * smoothStep((maxLen-dj)/maxLen);
(*this) [j] = pj;
if( (prevj-pj).norm() <= ZERO_VECTOR ) jIsValid = false ;
}
}
}
}
}
/// \brief Catmull-Rom spline interpolation.
///
/// Creates a Catmull-Rom interpolated curve with
/// the vertices of this Polygonal Curve as control points.
/// \param result (output): where the interpolated curve is to be stored - any previous
/// content is erased.
/// \param maxdist distance between interpolated points should be not greater than this.
inline void catmull(PolygonalCurve<real,dim,Point,Vector>& result, real maxdist = 1.0) {
result.clear(); // Make sure result has no vertices
double d2 = maxdist*maxdist; // Use quicker squared distance
// Catmullrom interpolator
CatmullRomBlend<real, dim> crb;
// How many segments?
int nsegments = isClosed()? size() : size()-1;
// Emit first point
result.add (at(0));
// Emit at least nsegments points
int i = 0;
while (i < nsegments) {
// The two middle control points
Point p1 = at(i % size());
Point p2 = at((i+1) % size());
if (p1.dist2(p2) > d2) {
// Perform subdivision
// The two extremity control points
Point p0 = i == 0 ? (isClosed() ? atEnd() : at(0)) : at (i-1);
Point p3 = i+2 < (int)size() || isClosed() ? at((i+2) % size()) : atEnd();
// The anchor point against which to measure distance
Point anchor = p1;
// The u value corresponding to the anchor
double uanchor = 0.0;
// A stack of u's (parameter values)
std::vector<real> ustack;
ustack.push_back (1.0);
ustack.push_back (0.5);
// Perform iterative subdivision
while (ustack.size()>0) {
real u = ustack.back();
ustack.pop_back();
Point p;
crb.blendPoint (u,&p0.x(),&p1.x(),&p2.x(),&p3.x(),&p.x());
if (p.dist2(anchor) <= d2) {
result.add (p);
anchor = p;
uanchor = u;
} else {
real unew = (u+uanchor) / 2;
ustack.push_back (u);
ustack.push_back (unew);
}
}
}
else {
// Add segment point
result.add (p2);
}
i++;
}
}