-
Notifications
You must be signed in to change notification settings - Fork 19
/
Curve.cpp
1327 lines (1172 loc) · 30.5 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
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
// Curve.cpp
// Copyright 2011, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "Curve.h"
#include "Circle.h"
#include "Arc.h"
#include "Area.h"
#include "kurve/geometry.h"
const Point operator*(const double &d, const Point &p){ return p * d;}
double Point::tolerance = 0.001;
//static const double PI = 3.1415926535897932; duplicated in kurve/geometry.h
double Point::length()const
{
return sqrt( x*x + y*y );
}
double Point::normalize()
{
double len = length();
if(fabs(len)> 0.000000000000001)
*this = (*this) / len;
return len;
}
Line2d::Line2d(const Point& P0, const Point& V) :p0(P0), v(V)
{
}
double Line2d::Dist(const Point& p)const
{
Point vn = v;
vn.normalize();
double d1 = p0 * vn;
double d2 = p * vn;
Point pn = p0 + vn * (d2 - d1);
return pn.dist(p);
}
CVertex::CVertex(int type, const Point& p, const Point& c, int user_data):m_type(type), m_p(p), m_c(c), m_user_data(user_data)
{
}
CVertex::CVertex(const Point& p, int user_data):m_type(0), m_p(p), m_c(0.0, 0.0), m_user_data(user_data)
{
}
void CCurve::append(const CVertex& vertex)
{
m_vertices.push_back(vertex);
}
class CArcOrLine
{
public:
CArc m_arc;
bool m_is_a_line;
CArcOrLine(const CArc& arc, bool is_a_line) :m_arc(arc), m_is_a_line(is_a_line){}
};
bool CCurve::CheckForArc(const CVertex& prev_vt, std::list<const CVertex*>& might_be_an_arc, CArcOrLine &arc_or_line_returned)
{
// this examines the vertices in might_be_an_arc
// if they do fit an arc, set arc to be the arc that they fit and return true
// returns true, if arc added
if(might_be_an_arc.size() < 2)return false;
// find middle point
unsigned int num = (unsigned int)might_be_an_arc.size();
int i = 0;
const CVertex* mid_vt = NULL;
int mid_i = (num-1)/2;
for(std::list<const CVertex*>::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++, i++)
{
if(i == mid_i)
{
mid_vt = *It;
break;
}
}
// create a circle to test
Point p0(prev_vt.m_p);
Point p1(mid_vt->m_p);
Point p2(might_be_an_arc.back()->m_p);
CircleOrLine c(p0, p1, p2);
const CVertex* current_vt = &prev_vt;
for(std::list<const CVertex*>::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++)
{
const CVertex* vt = *It;
if(!c.PointIsOn(vt->m_p, CArea::m_accuracy / CArea::m_units * 0.1))
return false;
if(!c.LineIsOn(current_vt->m_p, vt->m_p, CArea::m_accuracy * 2.0 / CArea::m_units))
return false;
current_vt = vt;
}
CArc arc;
arc.m_s = prev_vt.m_p;
arc.m_e = might_be_an_arc.back()->m_p;
if (c.m_is_a_line)
{
arc_or_line_returned = CArcOrLine(arc, true);
return true;
}
arc.m_c = c.m_c;
arc.SetDirWithPoint(might_be_an_arc.front()->m_p);
arc.m_user_data = might_be_an_arc.back()->m_user_data;
double angs = atan2(arc.m_s.y - arc.m_c.y, arc.m_s.x - arc.m_c.x);
double ange = atan2(arc.m_e.y - arc.m_c.y, arc.m_e.x - arc.m_c.x);
if(arc.m_dir)
{
// make sure ange > angs
if(ange < angs)ange += 6.2831853071795864;
}
else
{
// make sure angs > ange
if(angs < ange)angs += 6.2831853071795864;
}
if(arc.IncludedAngle() >= 3.15)return false; // We don't want full arcs, so limit to about 180 degrees
for(std::list<const CVertex*>::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++)
{
const CVertex* vt = *It;
double angp = atan2(vt->m_p.y - arc.m_c.y, vt->m_p.x - arc.m_c.x);
if(arc.m_dir)
{
// make sure angp > angs
if(angp < angs)angp += 6.2831853071795864;
if(angp > ange)return false;
}
else
{
// make sure angp > ange
if(angp < ange)angp += 6.2831853071795864;
if(angp > angs)return false;
}
}
arc_or_line_returned = CArcOrLine(arc, false);
return true;
}
bool CheckAddedRadii(const std::list<CVertex> &new_vertices)
{
if (new_vertices.size() > 1)
{
std::list<CVertex>::const_iterator It = new_vertices.end();
It--;
const CVertex& v = *It;
if (v.m_type != 0)
{
It--;
const CVertex& p = *It;
double r1 = p.m_p.dist(v.m_c);
double r2 = v.m_p.dist(v.m_c);
if (fabs(r1 - r2) > 0.0001)
{
return false;
}
}
}
return true;
}
void CCurve::AddArcOrLines(bool check_for_arc, std::list<CVertex> &new_vertices, std::list<const CVertex*>& might_be_an_arc, CArcOrLine &arc_or_line, bool &arc_found, bool &arc_added)
{
if (check_for_arc && CheckForArc(new_vertices.back(), might_be_an_arc, arc_or_line))
{
arc_found = true;
}
else
{
if (arc_found)
{
if (arc_or_line.m_is_a_line || arc_or_line.m_arc.AlmostALine(CArea::m_accuracy))
{
new_vertices.push_back(CVertex(arc_or_line.m_arc.m_e, arc_or_line.m_arc.m_user_data));
}
else
{
new_vertices.push_back(CVertex(arc_or_line.m_arc.m_dir ? 1 : -1, arc_or_line.m_arc.m_e, arc_or_line.m_arc.m_c, arc_or_line.m_arc.m_user_data));
CheckAddedRadii(new_vertices);
}
arc_added = true;
arc_found = false;
const CVertex* back_vt = might_be_an_arc.back();
might_be_an_arc.clear();
if (check_for_arc)might_be_an_arc.push_back(back_vt);
}
else
{
const CVertex* back_vt = might_be_an_arc.back();
if (check_for_arc)might_be_an_arc.pop_back();
for (std::list<const CVertex*>::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++)
{
const CVertex* v = *It;
if (It != might_be_an_arc.begin() || (new_vertices.size() == 0) || (new_vertices.back().m_p != v->m_p))
{
new_vertices.push_back(*v);
CheckAddedRadii(new_vertices);
}
}
might_be_an_arc.clear();
if (check_for_arc)might_be_an_arc.push_back(back_vt);
}
}
}
void CCurve::FitArcs()
{
std::list<CVertex> new_vertices;
std::list<const CVertex*> might_be_an_arc;
CArcOrLine arc_or_line(CArc(), false);
bool arc_found = false;
bool arc_added = false;
int i = 0;
const CVertex* prev_vt = NULL;
for (std::list<CVertex>::iterator It = m_vertices.begin(); It != m_vertices.end(); It++, i++)
{
CVertex& vt = *It;
if (vt.m_type || i == 0)
{
if (i != 0)
{
AddArcOrLines(false, new_vertices, might_be_an_arc, arc_or_line, arc_found, arc_added);
}
new_vertices.push_back(vt);
}
else
{
if (vt.m_p != prev_vt->m_p)
{
might_be_an_arc.push_back(&vt);
if (might_be_an_arc.size() == 1)
{
}
else
{
AddArcOrLines(true, new_vertices, might_be_an_arc, arc_or_line, arc_found, arc_added);
}
}
}
prev_vt = &vt;
}
if (might_be_an_arc.size() > 0)AddArcOrLines(false, new_vertices, might_be_an_arc, arc_or_line, arc_found, arc_added);
if (arc_added)
{
m_vertices.clear();
for (std::list<CVertex>::iterator It = new_vertices.begin(); It != new_vertices.end(); It++)m_vertices.push_back(*It);
for (std::list<const CVertex*>::iterator It = might_be_an_arc.begin(); It != might_be_an_arc.end(); It++)m_vertices.push_back(*(*It));
}
}
void CCurve::UnFitArcs()
{
std::list<Point> new_pts;
const CVertex* prev_vertex = NULL;
for(std::list<CVertex>::const_iterator It2 = m_vertices.begin(); It2 != m_vertices.end(); It2++)
{
const CVertex& vertex = *It2;
if(vertex.m_type == 0 || prev_vertex == NULL)
{
new_pts.push_back(vertex.m_p * CArea::m_units);
}
else
{
if(vertex.m_p != prev_vertex->m_p)
{
double phi,dphi,dx,dy;
int Segments;
int i;
double ang1,ang2,phit;
dx = (prev_vertex->m_p.x - vertex.m_c.x) * CArea::m_units;
dy = (prev_vertex->m_p.y - vertex.m_c.y) * CArea::m_units;
ang1=atan2(dy,dx);
if (ang1<0) ang1+=2.0*PI;
dx = (vertex.m_p.x - vertex.m_c.x) * CArea::m_units;
dy = (vertex.m_p.y - vertex.m_c.y) * CArea::m_units;
ang2=atan2(dy,dx);
if (ang2<0) ang2+=2.0*PI;
if (vertex.m_type == -1)
{ //clockwise
if (ang2 > ang1)
phit=2.0*PI-ang2+ ang1;
else
phit=ang1-ang2;
}
else
{ //counter_clockwise
if (ang1 > ang2)
phit=-(2.0*PI-ang1+ ang2);
else
phit=-(ang2-ang1);
}
//what is the delta phi to get an accurancy of aber
double radius = sqrt(dx*dx + dy*dy);
dphi=2*acos((radius-CArea::m_accuracy)/radius);
//set the number of segments
if (phit > 0)
Segments=(int)ceil(phit/dphi);
else
Segments=(int)ceil(-phit/dphi);
if (Segments < 1)
Segments=1;
if (Segments > 5000)
Segments=5000;
dphi=phit/(Segments);
double px = prev_vertex->m_p.x * CArea::m_units;
double py = prev_vertex->m_p.y * CArea::m_units;
for (i=1; i<=Segments; i++)
{
dx = px - vertex.m_c.x * CArea::m_units;
dy = py - vertex.m_c.y * CArea::m_units;
phi=atan2(dy,dx);
double nx = vertex.m_c.x * CArea::m_units + radius * cos(phi-dphi);
double ny = vertex.m_c.y * CArea::m_units + radius * sin(phi-dphi);
new_pts.push_back(Point(nx, ny));
px = nx;
py = ny;
}
}
}
prev_vertex = &vertex;
}
m_vertices.clear();
for(std::list<Point>::iterator It = new_pts.begin(); It != new_pts.end(); It++)
{
Point &pt = *It;
CVertex vertex(0, pt / CArea::m_units, Point(0.0, 0.0));
m_vertices.push_back(vertex);
}
}
Point CCurve::NearestPoint(const Point& p)const
{
double best_dist = 0.0;
Point best_point = Point(0, 0);
bool best_point_valid = false;
Point prev_p = Point(0, 0);
bool prev_p_valid = false;
bool first_span = true;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p_valid)
{
Point near_point = Span(prev_p, vertex, first_span).NearestPoint(p);
first_span = false;
double dist = near_point.dist(p);
if(!best_point_valid || dist < best_dist)
{
best_dist = dist;
best_point = near_point;
best_point_valid = true;
}
}
prev_p = vertex.m_p;
prev_p_valid = true;
}
return best_point;
}
Point CCurve::NearestPoint(const CCurve& c, double *d)const
{
double best_dist = 0.0;
Point best_point = Point(0, 0);
bool best_point_valid = false;
Point prev_p = Point(0, 0);
bool prev_p_valid = false;
bool first_span = true;
for(std::list<CVertex>::const_iterator It = c.m_vertices.begin(); It != c.m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p_valid)
{
double dist;
Point near_point = NearestPoint(Span(prev_p, vertex, first_span), &dist);
first_span = false;
if(!best_point_valid || dist < best_dist)
{
best_dist = dist;
best_point = near_point;
best_point_valid = true;
}
}
prev_p = vertex.m_p;
prev_p_valid = true;
}
if(d)*d = best_dist;
return best_point;
}
void CCurve::GetBox(CBox2D &box)
{
Point prev_p = Point(0, 0);
bool prev_p_valid = false;
for(std::list<CVertex>::iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
CVertex& vertex = *It;
if(prev_p_valid)
{
Span(prev_p, vertex).GetBox(box);
}
prev_p = vertex.m_p;
prev_p_valid = true;
}
}
void CCurve::Reverse()
{
std::list<CVertex> new_vertices;
CVertex* prev_v = NULL;
for(std::list<CVertex>::reverse_iterator It = m_vertices.rbegin(); It != m_vertices.rend(); It++)
{
CVertex &v = *It;
int type = 0;
Point cp(0.0, 0.0);
if(prev_v)
{
type = -prev_v->m_type;
cp = prev_v->m_c;
}
CVertex new_v(type, v.m_p, cp);
new_vertices.push_back(new_v);
prev_v = &v;
}
m_vertices = new_vertices;
}
double CCurve::GetArea()const
{
double area = 0.0;
Point prev_p = Point(0, 0);
bool prev_p_valid = false;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p_valid)
{
area += Span(prev_p, vertex).GetArea();
}
prev_p = vertex.m_p;
prev_p_valid = true;
}
return area;
}
bool CCurve::IsClosed()const
{
if(m_vertices.size() == 0)return false;
return m_vertices.front().m_p == m_vertices.back().m_p;
}
void CCurve::ChangeStart(const Point &p) {
if (m_vertices.size() > 0)
{
if (p == m_vertices.front().m_p)
{
// no need to change start if it's already the start
return;
}
}
CCurve new_curve;
bool started = false;
bool finished = false;
int start_span = 0;
bool closed = IsClosed();
for(int i = 0; i < (closed ? 2:1); i++)
{
const Point *prev_p = NULL;
int span_index = 0;
for(std::list<CVertex>::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end() && !finished; VIt++)
{
const CVertex& vertex = *VIt;
if(prev_p)
{
Span span(*prev_p, vertex);
if(span.On(p))
{
if(started)
{
if(p == *prev_p || span_index != start_span)
{
new_curve.m_vertices.push_back(vertex);
}
else
{
if(p == vertex.m_p)new_curve.m_vertices.push_back(vertex);
else
{
CVertex v(vertex);
v.m_p = p;
new_curve.m_vertices.push_back(v);
}
finished = true;
}
}
else
{
new_curve.m_vertices.push_back(CVertex(p));
started = true;
start_span = span_index;
if(p != vertex.m_p)new_curve.m_vertices.push_back(vertex);
}
}
else
{
if(started)
{
new_curve.m_vertices.push_back(vertex);
}
}
span_index++;
}
prev_p = &(vertex.m_p);
}
}
if(started)
{
*this = new_curve;
}
}
void CCurve::Break(const Point &p) {
// inserts a point, if it lies on the curve
const Point *prev_p = NULL;
for(std::list<CVertex>::iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++)
{
CVertex& vertex = *VIt;
if(p == vertex.m_p)break; // point is already on a vertex
if(prev_p)
{
Span span(*prev_p, vertex);
if(span.On(p))
{
CVertex v(vertex);
v.m_p = p;
m_vertices.insert(VIt, v);
break;
}
}
prev_p = &(vertex.m_p);
}
}
void CCurve::ExtractSeparateCurves(const std::list<Point> &ordered_points, std::list<CCurve> &separate_curves)const
{
// returns separate curves for this curve split at points
// the points must be in order along this curve, already, and lie on this curve
const Point *prev_p = NULL;
if(ordered_points.size() == 0)
{
separate_curves.push_back(*this);
return;
}
CCurve current_curve;
std::list<Point>::const_iterator PIt = ordered_points.begin();
Point point = *PIt;
for(std::list<CVertex>::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++)
{
const CVertex& vertex = *VIt;
if(prev_p)// not the first vertex
{
Span span(*prev_p, vertex);
while((PIt != ordered_points.end()) && span.On(point))
{
CVertex v(vertex);
v.m_p = point;
current_curve.m_vertices.push_back(v);
if(current_curve.m_vertices.size() > 1)// don't add single point curves
separate_curves.push_back(current_curve); // add the curve
current_curve = CCurve();// make a new curve
current_curve.m_vertices.push_back(v); // add it's first point
PIt++;
if(PIt != ordered_points.end())point = *PIt; // increment the point
}
// add the end of span
if(current_curve.m_vertices.back().m_p != vertex.m_p)
current_curve.m_vertices.push_back(vertex);
}
if((current_curve.m_vertices.size() == 0) || (current_curve.m_vertices.back().m_p != vertex.m_p))
{
// very first vertex, start the current curve
current_curve.m_vertices.push_back(vertex);
}
prev_p = &(vertex.m_p);
}
// add whatever is left
if(current_curve.m_vertices.size() > 1)// don't add single point curves
separate_curves.push_back(current_curve); // add the curve
}
void CCurve::RemoveTinySpans() {
CCurve new_curve;
std::list<CVertex>::const_iterator VIt = m_vertices.begin();
new_curve.m_vertices.push_back(*VIt);
VIt++;
for(; VIt != m_vertices.end(); VIt++)
{
const CVertex& vertex = *VIt;
if(vertex.m_type != 0 || new_curve.m_vertices.back().m_p.dist(vertex.m_p) > Point::tolerance)
{
new_curve.m_vertices.push_back(vertex);
}
}
*this = new_curve;
}
void CCurve::ChangeEnd(const Point &p) {
// changes the end position of the Kurve, doesn't keep closed kurves closed
CCurve new_curve;
const Point *prev_p = NULL;
for(std::list<CVertex>::const_iterator VIt = m_vertices.begin(); VIt != m_vertices.end(); VIt++)
{
const CVertex& vertex = *VIt;
if(prev_p)
{
Span span(*prev_p, vertex);
if(span.On(p))
{
CVertex v(vertex);
v.m_p = p;
new_curve.m_vertices.push_back(v);
break;
}
else
{
if(p != vertex.m_p)new_curve.m_vertices.push_back(vertex);
}
}
else
{
new_curve.m_vertices.push_back(vertex);
}
prev_p = &(vertex.m_p);
}
*this = new_curve;
}
Point CCurve::NearestPoint(const Span& p, double *d)const
{
double best_dist = 0.0;
Point best_point = Point(0, 0);
bool best_point_valid = false;
Point prev_p = Point(0, 0);
bool prev_p_valid = false;
bool first_span = true;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p_valid)
{
double dist;
Point near_point = Span(prev_p, vertex, first_span).NearestPoint(p, &dist);
first_span = false;
if(!best_point_valid || dist < best_dist)
{
best_dist = dist;
best_point = near_point;
best_point_valid = true;
}
}
prev_p = vertex.m_p;
prev_p_valid = true;
}
if(d)*d = best_dist;
return best_point;
}
static geoff_geometry::Kurve MakeKurve(const CCurve& curve)
{
geoff_geometry::Kurve k;
for(std::list<CVertex>::const_iterator It = curve.m_vertices.begin(); It != curve.m_vertices.end(); It++)
{
const CVertex& v = *It;
k.Add(geoff_geometry::spVertex(v.m_type, geoff_geometry::Point(v.m_p.x, v.m_p.y), geoff_geometry::Point(v.m_c.x, v.m_c.y)));
}
return k;
}
static CCurve MakeCCurve(const geoff_geometry::Kurve& k)
{
CCurve c;
int n = k.nSpans();
for(int i = 0; i<= n; i++)
{
geoff_geometry::spVertex spv;
k.Get(i, spv);
c.append(CVertex(spv.type, Point(spv.p.x, spv.p.y), Point(spv.pc.x, spv.pc.y)));
}
return c;
}
static geoff_geometry::Span MakeSpan(const Span& span)
{
return geoff_geometry::Span(span.m_v.m_type, geoff_geometry::Point(span.m_p.x, span.m_p.y), geoff_geometry::Point(span.m_v.m_p.x, span.m_v.m_p.y), geoff_geometry::Point(span.m_v.m_c.x, span.m_v.m_c.y));
}
bool CCurve::Offset(double leftwards_value)
{
// use the kurve code donated by Geoff Hawkesford, to offset the curve as an open curve
// returns true for success, false for failure
bool success = true;
CCurve save_curve = *this;
try
{
geoff_geometry::Kurve k = MakeKurve(*this);
geoff_geometry::Kurve kOffset;
int ret = 0;
k.OffsetMethod1(kOffset, fabs(leftwards_value), (leftwards_value > 0) ? 1:-1, 1, ret);
success = (ret == 0);
if(success)*this = MakeCCurve(kOffset);
}
catch(...)
{
success = false;
}
if(success == false)
{
if(this->IsClosed())
{
double inwards_offset = leftwards_value;
bool cw = false;
if(this->IsClockwise())
{
inwards_offset = -inwards_offset;
cw = true;
}
CArea a;
a.append(*this);
a.Offset(inwards_offset);
if(a.m_curves.size() == 1)
{
Span* start_span = NULL;
if(this->m_vertices.size() > 1)
{
std::list<CVertex>::iterator It = m_vertices.begin();
CVertex &v0 = *It;
It++;
CVertex &v1 = *It;
start_span = new Span(v0.m_p, v1, true);
}
*this = a.m_curves.front();
if(this->IsClockwise() != cw)this->Reverse();
if(start_span)
{
Point forward = start_span->GetVector(0.0);
Point left(-forward.y, forward.x);
Point offset_start = start_span->m_p + left * leftwards_value;
this->ChangeStart(this->NearestPoint(offset_start));
delete start_span;
}
success = true;
}
}
}
return success;
}
void CCurve::GetSpans(std::list<Span> &spans)const
{
const Point *prev_p = NULL;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p)
{
spans.push_back(Span(*prev_p, vertex));
}
prev_p = &(vertex.m_p);
}
}
void CCurve::OffsetForward(double forwards_value, bool refit_arcs)
{
// for drag-knife compensation
// replace arcs with lines
UnFitArcs();
std::list<Span> spans;
GetSpans(spans);
m_vertices.clear();
// shift all the spans
for(std::list<Span>::iterator It = spans.begin(); It != spans.end(); It++)
{
Span &span = *It;
Point v = span.GetVector(0.0);
v.normalize();
Point shift = v * forwards_value;
span.m_p = span.m_p + shift;
span.m_v.m_p = span.m_v.m_p + shift;
}
// loop through the shifted spans
for(std::list<Span>::iterator It = spans.begin(); It != spans.end();)
{
Span &span = *It;
Point v = span.GetVector(0.0);
v.normalize();
// add the span
if(It == spans.begin())m_vertices.push_back(span.m_p);
m_vertices.push_back(span.m_v.m_p);
It++;
if(It != spans.end())
{
Span &next_span = *It;
Point nv = next_span.GetVector(0.0);
nv.normalize();
double sin_angle = v ^ nv;
bool sharp_corner = ( fabs(sin_angle) > 0.5 ); // angle > 30 degrees
if(sharp_corner)
{
// add an arc to the start of the next span
int arc_type = ((sin_angle > 0) ? 1 : (-1));
Point centre = span.m_v.m_p - v * forwards_value;
m_vertices.push_back(CVertex(arc_type, next_span.m_p, centre));
}
}
}
if(refit_arcs)
FitArcs(); // find the arcs again
else
UnFitArcs(); // convert those little arcs added to lines
}
double CCurve::Perim()const
{
const Point *prev_p = NULL;
double perim = 0.0;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p)
{
Span span(*prev_p, vertex);
perim += span.Length();
}
prev_p = &(vertex.m_p);
}
return perim;
}
Point CCurve::PerimToPoint(double perim)const
{
if(m_vertices.size() == 0)return Point(0, 0);
const Point *prev_p = NULL;
double kperim = 0.0;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p)
{
Span span(*prev_p, vertex);
double length = span.Length();
if(perim < kperim + length)
{
Point p = span.MidPerim(perim - kperim);
return p;
}
kperim += length;
}
prev_p = &(vertex.m_p);
}
return m_vertices.back().m_p;
}
double CCurve::PointToPerim(const Point& p)const
{
double best_dist = 0.0;
double perim_at_best_dist = 0.0;
Point best_point = Point(0, 0);
bool best_dist_found = false;
double perim = 0.0;
const Point *prev_p = NULL;
bool first_span = true;
for(std::list<CVertex>::const_iterator It = m_vertices.begin(); It != m_vertices.end(); It++)
{
const CVertex& vertex = *It;
if(prev_p)
{
Span span(*prev_p, vertex, first_span);
Point near_point = span.NearestPoint(p);
first_span = false;
double dist = near_point.dist(p);
if(!best_dist_found || dist < best_dist)
{
best_dist = dist;
Span span_to_point(*prev_p, CVertex(span.m_v.m_type, near_point, span.m_v.m_c));
perim_at_best_dist = perim + span_to_point.Length();
best_dist_found = true;
}
perim += span.Length();
}
prev_p = &(vertex.m_p);
}
return perim_at_best_dist;
}
void CCurve::operator+=(const CCurve& curve)
{
for(std::list<CVertex>::const_iterator It = curve.m_vertices.begin(); It != curve.m_vertices.end(); It++)
{
const CVertex &vt = *It;
if(It == curve.m_vertices.begin())
{
if((m_vertices.size() == 0) || (It->m_p != m_vertices.back().m_p))
{
m_vertices.push_back(CVertex(It->m_p));
}
}
else
{
m_vertices.push_back(vt);
}
}
}
void CCurve::CurveIntersections(const CCurve& c, std::list<Point> &pts)const
{
CArea a;
a.append(*this);
a.CurveIntersections(c, pts);