-
Notifications
You must be signed in to change notification settings - Fork 0
/
visilibity.hpp
2169 lines (2039 loc) · 79.2 KB
/
visilibity.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
/**
* \file visilibity.hpp
* \authors Karl J. Obermeyer
* \date March 20, 2008
*
VisiLibity: A Floating-Point Visibility Algorithms Library,
Copyright (C) 2008 Karl J. Obermeyer (karl.obermeyer [ at ] gmail.com)
This file is part of VisiLibity.
VisiLibity 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 3 of the License, or (at your
option) any later version.
VisiLibity 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with VisiLibity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \mainpage
* <center>
* see also the <a href="../index.html">VisiLibity Project Page</a>
* </center>
* <b>Authors</b>: Karl J. Obermeyer
* <hr>
* \section developers For Developers
* <a href="../VisiLibity.coding_standards.html">Coding Standards</a>
* <hr>
* \section release_notes Release Notes
* <b>Current Functionality</b>
* <ul>
* <li>visibility polygons in polygonal environments with holes</li>
* <li>visibility graphs</li>
* <li>shortest path planning for a point</li>
* </ul>
*/
#ifndef VISILIBITY_H
#define VISILIBITY_H
//Uncomment these lines when compiling under
//Microsoft Visual Studio
#include <limits>
#define NAN std::numeric_limits<double>::quiet_NaN()
#define INFINITY std::numeric_limits<double>::infinity()
#define M_PI 3.141592653589793238462643
#define and &&
#define or ||
#include <cmath> //math functions in std namespace
#include <vector>
#include <queue> //queue and priority_queue.
#include <set> //priority queues with iteration,
//integrated keys
#include <list>
#include <algorithm> //sorting, min, max, reverse
#include <cstdlib> //rand and srand
#include <ctime> //Unix time
#include <fstream> //file I/O
#include <iostream>
#include <cstring> //C-string manipulation
#include <string> //string class
#include <cassert> //assertions
/// VisiLibity's sole namespace
namespace VisiLibity
{
//Fwd declaration of all classes and structs serves as index.
struct Bounding_Box;
class Point;
class Line_Segment;
class Angle;
class Ray;
class Polar_Point;
class Polyline;
class Polygon;
class Environment;
class Guards;
class Visibility_Polygon;
class Visibility_Graph;
/** \brief floating-point display precision.
*
* This is the default precision with which floating point
* numbers are displayed or written to files for classes with a
* write_to_file() method.
*/
const int FIOS_PRECISION = 10;
/** \brief get a uniform random sample from an (inclusive) interval
* on the real line
*
* \author Karl J. Obermeyer
* \param lower_bound lower bound of the real interval
* \param upper_bound upper bound of the real interval
* \pre \a lower_bound <= \a upper_bound
* \return a random sample from a uniform probability distribution
* on the real interval [\a lower_bound, \a upper_bound]
* \remarks Uses the Standard Library's rand() function. rand()
* should be seeded (only necessary once at the beginning of the
* program) using the command
* std::srand( std::time( NULL ) ); rand();
* \warning performance degrades as upper_bound - lower_bound
* approaches RAND_MAX.
*/
double uniform_random_sample(double lower_bound, double upper_bound);
/** \brief rectangle with sides parallel to the x- and y-axes
*
* \author Karl J. Obermeyer
* Useful for enclosing other geometric objects.
*/
struct Bounding_Box { double x_min, x_max, y_min, y_max; };
/// Point in the plane represented by Cartesian coordinates
class Point
{
public:
//Constructors
/** \brief default
*
* \remarks Data defaults to NAN so that checking whether the
* data are numbers can be used as a precondition in functions.
*/
Point() : x_(NAN) , y_(NAN) { }
/// costruct from raw coordinates
Point(double x_temp, double y_temp)
{ x_=x_temp; y_=y_temp; }
//Accessors
/// get x coordinate
double x () const { return x_; }
/// get y coordinate
double y () const { return y_; }
/** \brief closest Point on \a line_segment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers
* and \a line_segment_temp is nonempty
* \return the Point on \a line_segment_temp which is the smallest
* Euclidean distance from the calling Point
*/
Point projection_onto(const Line_Segment& line_segment_temp) const;
/** \brief closest Point on \a ray_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point and \a ray_temp data are numbers
* \return the Point on \a ray_temp which is the smallest
* Euclidean distance from the calling Point
*/
Point projection_onto(const Ray& ray_temp) const;
/** \brief closest Point on \a polyline_temp
*
* \pre the calling Point data are numbers and \a polyline_temp
* is nonempty
* \return the Point on \a polyline_temp which is the smallest
* Euclidean distance from the calling Point
*/
Point projection_onto(const Polyline& polyline_temp) const;
/** \brief closest vertex of \a polygon_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a polygon_temp
* is nonempty
* \return the vertex of \a polygon_temp which is the
* smallest Euclidean distance from the calling Point
*/
Point projection_onto_vertices_of(const Polygon& polygon_temp) const;
/** \brief closest vertex of \a environment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty
* \return the vertex of \a environment_temp which is
* the smallest Euclidean distance from the calling Point
*/
Point projection_onto_vertices_of(const Environment&
enviroment_temp) const;
/** \brief closest Point on boundary of \a polygon_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a polygon_temp
* is nonempty
* \return the Point on the boundary of \a polygon_temp which is the
* smallest Euclidean distance from the calling Point
*/
Point projection_onto_boundary_of(const Polygon& polygon_temp) const;
/** \brief closest Point on boundary of \a environment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty
* \return the Point on the boundary of \a environment_temp which is
* the smalles Euclidean distance from the calling Point
*/
Point projection_onto_boundary_of(const Environment&
enviroment_temp) const;
/** \brief true iff w/in \a epsilon of boundary of \a polygon_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a polygon_temp
* is nonempty
* \return true iff the calling Point is within Euclidean distance
* \a epsilon of \a polygon_temp 's boundary
* \remarks O(n) time complexity, where n is the number
* of vertices of \a polygon_temp
*/
bool on_boundary_of(const Polygon& polygon_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of boundary of \a environment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty
* \return true iff the calling Point is within Euclidean distance
* \a epsilon of \a environment_temp 's boundary
* \remarks O(n) time complexity, where n is the number
* of vertices of \a environment_temp
*/
bool on_boundary_of(const Environment& environment_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of \a line_segment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a line_segment_temp
* is nonempty
* \return true iff the calling Point is within distance
* \a epsilon of the (closed) Line_Segment \a line_segment_temp
*/
bool in(const Line_Segment& line_segment_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of interior but greater than
* \a espilon away from endpoints of \a line_segment_temp
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a line_segment_temp
* is nonempty
* \return true iff the calling Point is within distance \a
* epsilon of \line_segment_temp, but distance (strictly) greater
* than epsilon from \a line_segment_temp 's endpoints.
*/
bool in_relative_interior_of(const Line_Segment& line_segment_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of \a polygon_temp
*
* \author Karl J. Obermeyer
*
* \pre the calling Point data are numbers and \a polygon_temp is
* \a epsilon -simple. Test simplicity with
* Polygon::is_simple(epsilon)
*
* \return true iff the calling Point is a Euclidean distance no greater
* than \a epsilon from the (closed) Polygon (with vertices listed
* either cw or ccw) \a polygon_temp.
* \remarks O(n) time complexity, where n is the number of vertices
* in \a polygon_temp
*/
bool in(const Polygon& polygon_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of \a environment_temp
*
* \author Karl J. Obermeyer
*
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty and \a epsilon -valid. Test validity with
* Enviroment::is_valid(epsilon)
*
* \return true iff the calling Point is a Euclidean distance no greater
* than \a epsilon from the in the (closed) Environment \a environment_temp
* \remarks O(n) time complexity, where n is the number of
* vertices in \a environment_temp
*/
bool in(const Environment& environment_temp,
double epsilon=0.0) const;
/** \brief true iff w/in \a epsilon of some endpoint
* of \a line_segment_temp
*
* \pre the calling Point data are numbers and \a line_segment_temp
* is nonempty
* \return true iff calling Point is a Euclidean distance no greater
* than \a epsilon from some endpoint of \a line_segment_temp
*/
bool is_endpoint_of(const Line_Segment& line_segment_temp,
double epsilon=0.0) const;
//Mutators
/// change x coordinate
void set_x(double x_temp) { x_ = x_temp;}
/// change y coordinate
void set_y(double y_temp) { y_ = y_temp;}
/** \brief relocate to closest vertex if w/in \a epsilon of some
* vertex (of \a polygon_temp)
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a polygon_temp
* is nonempty
* \post If the calling Point was a Euclidean distance no greater
* than \a epsilon from any vertex of \a polygon_temp, then it
* will be repositioned to coincide with the closest such vertex
* \remarks O(n) time complexity, where n is the number of
* vertices in \a polygon_temp.
*/
void snap_to_vertices_of(const Polygon& polygon_temp,
double epsilon=0.0);
/** \brief relocate to closest vertex if w/in \a epsilon of some
* vertex (of \a environment_temp)
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty
* \post If the calling Point was a Euclidean distance no greater
* than \a epsilon from any vertex of \a environment_temp, then it
* will be repositioned to coincide with the closest such vertex
* \remarks O(n) time complexity, where n is the number of
* vertices in \a environment_temp.
*/
void snap_to_vertices_of(const Environment& environment_temp,
double epsilon=0.0);
/** \brief relocate to closest Point on boundary if w/in \a epsilon
* of the boundary (of \a polygon_temp)
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a polygon_temp
* is nonempty
* \post if the calling Point was a Euclidean distance no greater
* than \a epsilon from the boundary of \a polygon_temp, then it
* will be repositioned to it's projection onto that boundary
* \remarks O(n) time complexity, where n is the number of
* vertices in \a polygon_temp.
*/
void snap_to_boundary_of(const Polygon& polygon_temp,
double epsilon=0.0);
/** \brief relocate to closest Point on boundary if w/in \a epsilon
* of the boundary (of \a environment_temp)
*
* \author Karl J. Obermeyer
* \pre the calling Point data are numbers and \a environment_temp
* is nonempty
* \post if the calling Point was a Euclidean distance no greater
* than \a epsilon from the boundary of \a environment_temp, then it
* will be repositioned to it's projection onto that boundary
* \remarks O(n) time complexity, where n is the number of
* vertices in \a environment_temp.
*/
void snap_to_boundary_of(const Environment& environment_temp,
double epsilon=0.0);
protected:
double x_;
double y_;
};
/** \brief True iff Points' coordinates are identical.
*
* \remarks NAN==NAN returns false, so if either point has
* not been assigned real number coordinates, they will not be ==
*/
bool operator == (const Point& point1, const Point& point2);
/// True iff Points' coordinates are not identical.
bool operator != (const Point& point1, const Point& point2);
/** \brief compare lexicographic order of points
*
* For Points p1 and p2, p1 < p2 iff either p1.x() < p2.x() or
* p1.x()==p2.x() and p1.y()<p2.y(). False if any member data have
* not been assigned (numbers).
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
bool operator < (const Point& point1, const Point& point2);
/** \brief compare lexicographic order of points
*
* For Points p1 and p2, p1 < p2 iff either p1.x() < p2.x() or
* p1.x()==p2.x() and p1.y()<p2.y(). False if any member data have
* not been assigned (numbers).
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
bool operator > (const Point& point1, const Point& point2);
/** \brief compare lexicographic order of points
*
* For Points p1 and p2, p1 < p2 iff either p1.x() < p2.x() or
* p1.x()==p2.x() and p1.y()<p2.y(). False if any member data have
* not been assigned (numbers).
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
bool operator >= (const Point& point1, const Point& point2);
/** \brief compare lexicographic order of points
*
* For Points p1 and p2, p1 < p2 iff either p1.x() < p2.x() or
* p1.x()==p2.x() and p1.y()<p2.y(). False if any member data have
* not been assigned (numbers).
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
bool operator <= (const Point& point1, const Point& point2);
/// vector addition of Points
Point operator + (const Point& point1, const Point& point2);
/// vector subtraction of Points
Point operator - (const Point& point1, const Point& point2);
//// dot (scalar) product treats the Points as vectors
Point operator * (const Point& point1, const Point& point2);
/// simple scaling treats the Point as a vector
Point operator * (double scalar, const Point& point2);
/// simple scaling treats the Point as a vector
Point operator * (const Point& point1, double scalar);
/** \brief cross product (signed) magnitude treats the Points as vectors
*
* \author Karl J. Obermeyer
* \pre Points' data are numbers
* \remarks This is equal to the (signed) area of the parallelogram created
* by the Points viewed as vectors.
*/
double cross(const Point& point1, const Point& point2);
/** \brief Euclidean distance between Points
*
* \pre Points' data are numbers
*/
double distance(const Point& point1, const Point& point2);
/** \brief Euclidean distance between a Point and a Line_Segment
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Line_Segment is nonempty
*/
double distance(const Point& point_temp,
const Line_Segment& line_segment_temp);
/** \brief Euclidean distance between a Point and a Line_Segment
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Line_Segment is nonempty
*/
double distance(const Line_Segment& line_segment_temp,
const Point& point_temp);
/** \brief Euclidean distance between a Point and a Ray
*
* \author Karl J. Obermeyer
* \pre Point's and Ray's data are numbers
*/
double distance(const Point& point_temp,
const Ray& ray_temp);
/** \brief Euclidean distance between a Point and a Ray
*
* \author Karl J. Obermeyer
* \pre Point's and Ray's data are numbers
*/
double distance(const Ray& ray_temp,
const Point& point_temp);
/** \brief Euclidean distance between a Point and a Polyline
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Polyline is nonempty
*/
double distance(const Point& point_temp,
const Polyline& polyline_temp);
/** \brief Euclidean distance between a Point and a Polyline
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Polyline is nonempty
*/
double distance(const Polyline& polyline_temp,
const Point& point_temp);
/** \brief Euclidean distance between a Point and a Polygon's boundary
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Polygon is nonempty
*/
double boundary_distance(const Point& point_temp,
const Polygon& polygon_temp);
/** \brief Euclidean distance between a Point and a Polygon's boundary
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Polygon is nonempty
*/
double boundary_distance(const Polygon& polygon_temp,
const Point& point_temp);
/** \brief Euclidean distance between a Point and a Environment's boundary
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Environment is nonempty
*/
double boundary_distance(const Point& point_temp,
const Environment& environment_temp);
/** \brief Euclidean distance between a Point and a Environment's boundary
*
* \author Karl J. Obermeyer
* \pre Point's data are numbers and the Environment is nonempty
*/
double boundary_distance(const Environment& environment_temp,
const Point& point_temp);
/// print a Point
std::ostream& operator << (std::ostream& outs, const Point& point_temp);
/** \brief line segment in the plane represented by its endpoints
*
* Closed and oriented line segment in the plane represented by its endpoints.
* \remarks may be degenerate (colocated endpoints) or empty
*/
class Line_Segment
{
public:
//Constructors
/// default to empty
Line_Segment();
/// copy constructor
Line_Segment(const Line_Segment& line_segment_temp);
/// construct degenerate segment from a single Point
Line_Segment(const Point& point_temp);
/// Line_Segment pointing from first_point_temp to second_point_temp
Line_Segment(const Point& first_point_temp,
const Point& second_point_temp, double epsilon=0);
//Accessors
/** \brief first endpoint
*
* \pre size() > 0
* \return the first Point of the Line_Segment
* \remarks If size() == 1, then both first() and second() are valid
* and will return the same Point
*/
Point first() const;
/** \brief second endpoint
*
* \pre size() > 0
* \return the second Point of the Line_Segment
* \remarks If size() == 1, then both first() and second() are valid
* and will return the same Point
*/
Point second() const;
/** \brief number of distinct endpoints
*
* \remarks
* size 0 => empty line segment;
* size 1 => degenerate (single point) line segment;
* size 2 => full-fledged (bona fide) line segment
*/
unsigned size() const { return size_; }
/** \brief midpoint
*
* \pre size() > 0
*/
Point midpoint() const;
/** \brief Euclidean length
*
* \pre size() > 0
*/
double length() const;
/** \brief true iff vertices in lex. order
*
* \pre size() > 0
* \return true iff vertices are listed beginning with the vertex
* which is lexicographically smallest (lowest x, then lowest y)
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
bool is_in_standard_form() const;
//Mutators
/// assignment operator
Line_Segment& operator = (const Line_Segment& line_segment_temp);
/** \brief set first endpoint
*
* \remarks if \a point_temp is w/in a distance \a epsilon of an existing
* endpoint, the coordinates of \a point_temp are used and size is set to
* 1 as appropriate
*/
void set_first(const Point& point_temp, double epsilon=0.0);
/** \brief set second endpoint
*
* \remarks if \a point_temp is w/in a distance \a epsilon of an existing
* endpoint, the coordinates of \a point_temp are used and size is set to
* 1 as appropriate
*/
void set_second(const Point& point_temp, double epsilon=0.0);
/** \brief reverse order of endpoints
*
* \post order of endpoints is reversed.
*/
void reverse();
/** \brief enforce that lex. smallest endpoint first
*
* \post the lexicographically smallest endpoint (lowest x, then lowest y)
* is first
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a line parallel to one of the axes
*/
void enforce_standard_form();
/// erase both endpoints and set line segment empty (size 0)
void clear();
/// destructor
virtual ~Line_Segment();
protected:
//Pointer to dynamic array of endpoints.
Point *endpoints_;
//See size() comments.
unsigned size_;
};
/** \brief true iff endpoint coordinates are exactly equal, but
* false if either Line_Segment has size 0
*
* \remarks respects ordering of vertices, i.e., even if the line segments
* overlap exactly, they are not considered == unless the orientations are
* the same
*/
bool operator == (const Line_Segment& line_segment1,
const Line_Segment& line_segment2);
/// true iff endpoint coordinates are not ==
bool operator != (const Line_Segment& line_segment1,
const Line_Segment& line_segment2);
/** \brief true iff line segments' endpoints match up w/in a (closed)
* \a epsilon ball of each other, but false if either
* Line_Segment has size 0
*
* \author Karl J. Obermeyer
* \remarks this function will return true even if it has to flip
* the orientation of one of the segments to get the vertices to
* match up
*/
bool equivalent(Line_Segment line_segment1,
Line_Segment line_segment2, double epsilon=0);
/** \brief Euclidean distance between Line_Segments
*
* \author Karl J. Obermeyer
* \pre \a line_segment1.size() > 0 and \a line_segment2.size() > 0
*/
double distance(const Line_Segment& line_segment1,
const Line_Segment& line_segment2);
/** \brief Euclidean distance between a Line_Segment and the
* boundary of a Polygon
*
* \author Karl J. Obermeyer
* \pre \a line_segment.size() > 0 and \a polygon.n() > 0
*/
double boundary_distance(const Line_Segment& line_segment,
const Polygon& polygon);
/** \brief Euclidean distance between a Line_Segment and the
* boundary of a Polygon
*
* \author Karl J. Obermeyer
* \pre \a line_segment.size() > 0 and \a polygon.n() > 0
*/
double boundary_distance(const Polygon& polygon,
const Line_Segment& line_segment);
/** \brief true iff the Euclidean distance between Line_Segments is
* no greater than \a epsilon, false if either line segment
* has size 0
*
* \author Karl J. Obermeyer
*/
bool intersect(const Line_Segment& line_segment1,
const Line_Segment& line_segment2,
double epsilon=0.0);
/** \brief true iff line segments intersect properly w/in epsilon,
* false if either line segment has size 0
*
* \author Karl J. Obermeyer
* \return true iff Line_Segments intersect exactly at a single
* point in their relative interiors. For robustness, here the
* relative interior of a Line_Segment is consider to be any Point
* in the Line_Segment which is a distance greater than \a epsilon
* from both endpoints.
*/
bool intersect_proper(const Line_Segment& line_segment1,
const Line_Segment& line_segment2,
double epsilon=0.0);
/** \brief intersection of Line_Segments
*
* \author Karl J. Obermeyer
* \return a Line_Segment of size 0, 1, or 2
* \remarks size 0 results if the distance (or at least the
* floating-point computed distance) between line_segment1 and
* line_segment2 is (strictly) greater than epsilon. size 1 results
* if the segments intersect poperly, form a T intersection, or --
* intersection. size 2 results when two or more endpoints are a
* Euclidean distance no greater than \a epsilon from the opposite
* segment, and the overlap of the segments has a length greater
* than \a epsilon.
*/
Line_Segment intersection(const Line_Segment& line_segment1,
const Line_Segment& line_segment2,
double epsilon=0.0);
/// print a Line_Segment
std::ostream& operator << (std::ostream& outs,
const Line_Segment& line_segment_temp);
/** \brief angle in radians represented by a value in
* the interval [0,2*M_PI]
*
* \remarks the intended interpretation is that angles 0 and 2*M_PI
* correspond to the positive x-axis of the coordinate system
*/
class Angle
{
public:
//Constructors
/** \brief default
*
* \remarks data defaults to NAN so that checking whether the
* data are numbers can be used as a precondition in functions
*/
Angle() : angle_radians_(NAN) { }
/// construct from real value, mod into interval [0, 2*M_PI)
Angle(double data_temp);
/** \brief construct using 4 quadrant inverse tangent into [0, 2*M_PI),
* where 0 points along the x-axis
*/
Angle(double rise_temp, double run_temp);
//Accessors
/// get radians
double get() const { return angle_radians_; }
//Mutators
/// set angle, mod into interval [0, 2*PI)
void set(double data_temp);
/** \brief set angle data to 2*M_PI
*
* \remarks sometimes it is necessary to set the angle value to
* 2*M_PI instead of 0, so that the lex. inequalities behave
* appropriately during a radial line sweep
*/
void set_to_2pi() { angle_radians_=2*M_PI; }
/// set to new random angle in [0, 2*M_PI)
void randomize();
private:
double angle_radians_;
};
/// compare angle radians
bool operator == (const Angle& angle1, const Angle& angle2);
/// compare angle radians
bool operator != (const Angle& angle1, const Angle& angle2);
/// compare angle radians
bool operator > (const Angle& angle1, const Angle& angle2);
/// compare angle radians
bool operator < (const Angle& angle1, const Angle& angle2);
/// compare angle radians
bool operator >= (const Angle& angle1, const Angle& angle2);
/// compare angle radians
bool operator <= (const Angle& angle1, const Angle& angle2);
/// add angles' radians and mod into [0, 2*M_PI)
Angle operator + (const Angle& angle1, const Angle& angle2);
/// subtract angles' radians and mod into [0, 2*M_PI)
Angle operator - (const Angle& angle1, const Angle& angle2);
/** \brief geodesic distance in radians between Angles
*
* \author Karl J. Obermeyer
* \pre \a angle1 and \a angle2 data are numbers
*/
double geodesic_distance(const Angle& angle1, const Angle& angle2);
/** \brief 1.0 => geodesic path from angle1 to angle2
* is couterclockwise, -1.0 => clockwise
*
* \author Karl J. Obermeyer
* \pre \a angle1 and \a angle2 data are numbers
*/
double geodesic_direction(const Angle& angle1, const Angle& angle2);
/// print Angle
std::ostream& operator << (std::ostream& outs, const Angle& angle_temp);
/** \brief Point in the plane packaged together with polar
* coordinates w.r.t. specified origin
*
* The origin of the polar coordinate system is stored with the
* Polar_Point (in \a polar_origin_) and bearing is measured ccw from the
* positive x-axis.
* \remarks used, e.g., for radial line sweeps
*/
class Polar_Point : public Point
{
public:
//Constructors
/** \brief default
*
* \remarks Data defaults to NAN so that checking whether the
* data are numbers can be used as a precondition in functions.
*/
Polar_Point() : Point(), range_(NAN), bearing_(NAN) { }
/** \brief construct from (Cartesian) Points
*
* \pre member data of \a polar_origin_temp and \a point_temp have
* been assigned (numbers)
* \param polar_origin_temp the origin of the polar coordinate system
* \param point_temp the point to be represented
* \remarks if polar_origin_temp == point_temp, the default
* bearing is Angle(0.0)
*/
Polar_Point(const Point& polar_origin_temp,
const Point& point_temp,
double epsilon=0.0);
//Accessors
/** \brief origin of the polar coordinate system in which the point is
* represented
*/
Point polar_origin() const { return polar_origin_; }
/// Euclidean distance from the point represented to the origin of
/// the polar coordinate system
double range() const { return range_; }
/// bearing from polar origin w.r.t. direction parallel to x-axis
Angle bearing() const { return bearing_; }
//Mutators
/** \brief set the origin of the polar coordinate system
*
* \remarks x and y held constant, bearing and range modified
* accordingly
*/
void set_polar_origin(const Point& polar_origin_temp);
/** \brief set x
*
* \remarks polar_origin held constant, bearing and range modified
* accordingly
*/
void set_x(double x_temp);
/** \brief set y
*
* \remarks polar_origin held constant, bearing and range modified
* accordingly
*/
void set_y(double y_temp);
/** \brief set range
*
* \remarks polar_origin held constant, x and y modified
* accordingly
*/
void set_range(double range_temp);
/** \brief set bearing
*
* \remarks polar_origin and range held constant, x and y modified
* accordingly
*/
void set_bearing(const Angle& bearing_temp);
/** \brief set bearing Angle data to 2*M_PI
*
* \remarks Special function for use in computations involving a
* radial line sweep; sometimes it is necessary to set the angle
* value to 2*PI instead of 0, so that the lex. inequalities
* behave appropriately
*/
void set_bearing_to_2pi() { bearing_.set_to_2pi(); }
protected:
//Origin of the polar coordinate system in world coordinates.
Point polar_origin_;
//Polar coordinates where radius always positive, and angle
//measured ccw from the world coordinate system's x-axis.
double range_;
Angle bearing_;
};
/** \brief compare member data
*
* \remarks returns false if any member data are NaN
*/
bool operator == (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
bool operator != (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
/** \brief compare according to polar lexicographic order
* (smaller bearing, then smaller range)
*
* false if any member data have not been assigned (numbers)
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a radial line
*/
bool operator > (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
/** \brief compare according to polar lexicographic order
* (smaller bearing, then smaller range)
*
* false if any member data have not been assigned (numbers)
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a radial line
*/
bool operator < (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
/** \brief compare according to polar lexicographic order
* (smaller bearing, then smaller range)
*
* false if any member data have not been assigned (numbers)
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a radial line
*/
bool operator >= (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
/** \brief compare according to polar lexicographic order
* (smaller bearing, then smaller range)
*
* false if any member data have not been assigned (numbers)
* \remarks lex. comparison is very sensitive to perturbations if
* two Points nearly define a radial line
*/
bool operator <= (const Polar_Point& polar_point1,
const Polar_Point& polar_point2);
/// print Polar_Point
std::ostream& operator << (std::ostream& outs,
const Polar_Point& polar_point_temp);
/// ray in the plane represented by base Point and bearing Angle
class Ray
{
public:
//Constructors
/** \brief default
*
* \remarks data defaults to NAN so that checking whether the data
* are numbers can be used as a precondition in functions
*/
Ray() { }
/// construct ray emanating from \a base_point_temp in the direction
/// \a bearing_temp
Ray(Point base_point_temp, Angle bearing_temp) :
base_point_(base_point_temp) , bearing_(bearing_temp) {}
/// construct ray emanating from \a base_point_temp towards
/// \a bearing_point
Ray(Point base_point_temp, Point bearing_point);
//Accessors
/// get base point
Point base_point() const { return base_point_; }
/// get bearing
Angle bearing() const { return bearing_; }
//Mutators
/// set base point
void set_base_point(const Point& point_temp)
{ base_point_ = point_temp; }
/// set bearing
void set_bearing(const Angle& angle_temp)
{ bearing_ = angle_temp; }
private:
Point base_point_;
Angle bearing_;
};