VisiLibity v1 Source Code 1.0
visilibity.hpp
Go to the documentation of this file.
1
42#ifndef VISILIBITY_H
43#define VISILIBITY_H
44
45// Uncomment these lines when compiling under
46// Microsoft Visual Studio
47/*
48#include <limits>
49#define NAN std::numeric_limits<double>::quiet_NaN()
50#define INFINITY std::numeric_limits<double>::infinity()
51#define M_PI 3.141592653589793238462643
52#define and &&
53#define or ||
54*/
55
56#include <cmath> //math functions in std namespace
57#include <queue> //queue and priority_queue.
58#include <set> //priority queues with iteration,
59#include <vector>
60// integrated keys
61#include <algorithm> //sorting, min, max, reverse
62#include <array>
63#include <cassert> //assertions
64#include <cstdlib> //rand and srand
65#include <cstring> //C-string manipulation
66#include <ctime> //Unix time
67#include <fstream> //file I/O
68#include <iostream>
69#include <list>
70#include <string> //string class
71
73namespace VisiLibity {
74
75// Fwd declaration of all classes and structs serves as index.
76struct Bounding_Box;
77class Point;
78class Line_Segment;
79class Angle;
80class Ray;
81class Polar_Point;
82class Polyline;
83class Polygon;
84class Environment;
85class Guards;
88
95const int FIOS_PRECISION = 10;
96
113double uniform_random_sample(double lower_bound, double upper_bound);
114
121 double x_min, x_max, y_min, y_max;
122};
123
125class Point {
126public:
127 // Constructors
133 Point() : x_(NAN), y_(NAN) {}
135 Point(double x_temp, double y_temp) {
136 x_ = x_temp;
137 y_ = y_temp;
138 }
139 // Accessors
141 double x() const { return x_; }
143 double y() const { return y_; }
152 Point projection_onto(const Line_Segment &line_segment_temp) const;
160 Point projection_onto(const Ray &ray_temp) const;
168 Point projection_onto(const Polyline &polyline_temp) const;
177 Point projection_onto_vertices_of(const Polygon &polygon_temp) const;
186 Point projection_onto_vertices_of(const Environment &enviroment_temp) const;
195 Point projection_onto_boundary_of(const Polygon &polygon_temp) const;
204 Point projection_onto_boundary_of(const Environment &enviroment_temp) const;
215 bool on_boundary_of(const Polygon &polygon_temp, double epsilon = 0.0) const;
226 bool on_boundary_of(const Environment &environment_temp,
227 double epsilon = 0.0) const;
236 bool in(const Line_Segment &line_segment_temp, double epsilon = 0.0) const;
247 bool in_relative_interior_of(const Line_Segment &line_segment_temp,
248 double epsilon = 0.0) const;
263 bool in(const Polygon &polygon_temp, double epsilon = 0.0) const;
277 bool in(const Environment &environment_temp, double epsilon = 0.0) const;
286 bool is_endpoint_of(const Line_Segment &line_segment_temp,
287 double epsilon = 0.0) const;
288 // Mutators
290 void set_x(double x_temp) { x_ = x_temp; }
292 void set_y(double y_temp) { y_ = y_temp; }
305 void snap_to_vertices_of(const Polygon &polygon_temp, double epsilon = 0.0);
318 void snap_to_vertices_of(const Environment &environment_temp,
319 double epsilon = 0.0);
332 void snap_to_boundary_of(const Polygon &polygon_temp, double epsilon = 0.0);
345 void snap_to_boundary_of(const Environment &environment_temp,
346 double epsilon = 0.0);
347
348protected:
349 double x_;
350 double y_;
351};
352
358bool operator==(const Point &point1, const Point &point2);
360bool operator!=(const Point &point1, const Point &point2);
361
370bool operator<(const Point &point1, const Point &point2);
379bool operator>(const Point &point1, const Point &point2);
388bool operator>=(const Point &point1, const Point &point2);
397bool operator<=(const Point &point1, const Point &point2);
398
400Point operator+(const Point &point1, const Point &point2);
402Point operator-(const Point &point1, const Point &point2);
403
405Point operator*(const Point &point1, const Point &point2);
406
408Point operator*(double scalar, const Point &point2);
410Point operator*(const Point &point1, double scalar);
411
419double cross(const Point &point1, const Point &point2);
420
425double distance(const Point &point1, const Point &point2);
426
432double distance(const Point &point_temp, const Line_Segment &line_segment_temp);
438double distance(const Line_Segment &line_segment_temp, const Point &point_temp);
439
445double distance(const Point &point_temp, const Ray &ray_temp);
451double distance(const Ray &ray_temp, const Point &point_temp);
452
458double distance(const Point &point_temp, const Polyline &polyline_temp);
464double distance(const Polyline &polyline_temp, const Point &point_temp);
465
471double boundary_distance(const Point &point_temp, const Polygon &polygon_temp);
477double boundary_distance(const Polygon &polygon_temp, const Point &point_temp);
478
484double boundary_distance(const Point &point_temp,
485 const Environment &environment_temp);
491double boundary_distance(const Environment &environment_temp,
492 const Point &point_temp);
493
495std::ostream &operator<<(std::ostream &outs, const Point &point_temp);
496
503public:
504 // Constructors
506 Line_Segment();
508 Line_Segment(const Line_Segment &line_segment_temp);
510 Line_Segment(const Point &point_temp);
512 Line_Segment(const Point &first_point_temp, const Point &second_point_temp,
513 double epsilon = 0);
514 // Accessors
522 Point first() const;
530 Point second() const;
538 unsigned size() const { return size_; }
543 Point midpoint() const;
548 double length() const;
557 bool is_in_standard_form() const;
558 // Mutators
560 Line_Segment &operator=(const Line_Segment &line_segment_temp);
567 void set_first(const Point &point_temp, double epsilon = 0.0);
574 void set_second(const Point &point_temp, double epsilon = 0.0);
579 void reverse();
589 void clear();
591 virtual ~Line_Segment();
592
593protected:
594 // Pointer to dynamic array of endpoints.
595 Point *endpoints_;
596 // See size() comments.
597 unsigned size_;
598};
599
607bool operator==(const Line_Segment &line_segment1,
608 const Line_Segment &line_segment2);
610bool operator!=(const Line_Segment &line_segment1,
611 const Line_Segment &line_segment2);
612
622bool equivalent(Line_Segment line_segment1, Line_Segment line_segment2,
623 double epsilon = 0);
624
630double distance(const Line_Segment &line_segment1,
631 const Line_Segment &line_segment2);
632
639double boundary_distance(const Line_Segment &line_segment,
640 const Polygon &polygon);
641
648double boundary_distance(const Polygon &polygon,
649 const Line_Segment &line_segment);
650
657bool intersect(const Line_Segment &line_segment1,
658 const Line_Segment &line_segment2, double epsilon = 0.0);
659
670bool intersect_proper(const Line_Segment &line_segment1,
671 const Line_Segment &line_segment2, double epsilon = 0.0);
672
685Line_Segment intersection(const Line_Segment &line_segment1,
686 const Line_Segment &line_segment2,
687 double epsilon = 0.0);
688
690std::ostream &operator<<(std::ostream &outs,
691 const Line_Segment &line_segment_temp);
692
699class Angle {
700public:
701 // Constructors
707 Angle() : angle_radians_(NAN) {}
709 Angle(double data_temp);
713 Angle(double rise_temp, double run_temp);
714 // Accessors
716 double get() const { return angle_radians_; }
717 // Mutators
719 void set(double data_temp);
726 void set_to_2pi() { angle_radians_ = 2 * M_PI; }
728 void randomize();
729
730private:
731 double angle_radians_;
732};
733
735bool operator==(const Angle &angle1, const Angle &angle2);
737bool operator!=(const Angle &angle1, const Angle &angle2);
738
740bool operator>(const Angle &angle1, const Angle &angle2);
742bool operator<(const Angle &angle1, const Angle &angle2);
744bool operator>=(const Angle &angle1, const Angle &angle2);
746bool operator<=(const Angle &angle1, const Angle &angle2);
747
749Angle operator+(const Angle &angle1, const Angle &angle2);
751Angle operator-(const Angle &angle1, const Angle &angle2);
752
758double geodesic_distance(const Angle &angle1, const Angle &angle2);
759
766double geodesic_direction(const Angle &angle1, const Angle &angle2);
767
769std::ostream &operator<<(std::ostream &outs, const Angle &angle_temp);
770
779class Polar_Point : public Point {
780public:
781 // Constructors
787 Polar_Point() : Point(), range_(NAN), bearing_(NAN) {}
797 Polar_Point(const Point &polar_origin_temp, const Point &point_temp,
798 double epsilon = 0.0);
799 // Accessors
803 Point polar_origin() const { return polar_origin_; }
806 double range() const { return range_; }
808 Angle bearing() const { return bearing_; }
809 // Mutators
815 void set_polar_origin(const Point &polar_origin_temp);
821 void set_x(double x_temp);
827 void set_y(double y_temp);
833 void set_range(double range_temp);
839 void set_bearing(const Angle &bearing_temp);
847 void set_bearing_to_2pi() { bearing_.set_to_2pi(); }
848
849protected:
850 // Origin of the polar coordinate system in world coordinates.
851 Point polar_origin_;
852 // Polar coordinates where radius always positive, and angle
853 // measured ccw from the world coordinate system's x-axis.
854 double range_;
855 Angle bearing_;
856};
857
862bool operator==(const Polar_Point &polar_point1,
863 const Polar_Point &polar_point2);
864bool operator!=(const Polar_Point &polar_point1,
865 const Polar_Point &polar_point2);
866
874bool operator>(const Polar_Point &polar_point1,
875 const Polar_Point &polar_point2);
883bool operator<(const Polar_Point &polar_point1,
884 const Polar_Point &polar_point2);
892bool operator>=(const Polar_Point &polar_point1,
893 const Polar_Point &polar_point2);
901bool operator<=(const Polar_Point &polar_point1,
902 const Polar_Point &polar_point2);
903
905std::ostream &operator<<(std::ostream &outs,
906 const Polar_Point &polar_point_temp);
907
909class Ray {
910public:
911 // Constructors
917 Ray() {}
920 Ray(Point base_point_temp, Angle bearing_temp)
921 : base_point_(base_point_temp), bearing_(bearing_temp) {}
924 Ray(Point base_point_temp, Point bearing_point);
925 // Accessors
927 Point base_point() const { return base_point_; }
929 Angle bearing() const { return bearing_; }
930 // Mutators
932 void set_base_point(const Point &point_temp) { base_point_ = point_temp; }
934 void set_bearing(const Angle &angle_temp) { bearing_ = angle_temp; }
935
936private:
937 Point base_point_;
938 Angle bearing_;
939};
940
945bool operator==(const Ray &ray1, const Ray &ray2);
950bool operator!=(const Ray &ray1, const Ray &ray2);
951
961Line_Segment intersection(const Ray ray_temp,
962 const Line_Segment &line_segment_temp,
963 double epsilon = 0.0);
973Line_Segment intersection(const Line_Segment &line_segment_temp,
974 const Ray &ray_temp, double epsilon = 0.0);
975
977class Polyline {
978public:
979 friend class Point;
980 // Constructors
984 Polyline(const std::vector<Point> &vertices_temp) {
985 vertices_ = vertices_temp;
986 }
988 Polyline(const std::string &filename);
989
990 // Accessors
996 Point operator[](unsigned i) const { return vertices_[i]; }
998 unsigned size() const { return vertices_.size(); }
1000 double length() const;
1009 double diameter() const;
1010 // a box which fits snugly around the Polyline
1011 Bounding_Box bbox() const;
1012 // Mutators
1018 Point &operator[](unsigned i) { return vertices_[i]; }
1020 void clear() { vertices_.clear(); }
1022 void push_back(const Point &point_temp) { vertices_.push_back(point_temp); }
1024 void pop_back() { vertices_.pop_back(); }
1026 void set_vertices(const std::vector<Point> &vertices_temp) {
1027 vertices_ = vertices_temp;
1028 }
1038 void eliminate_redundant_vertices(double epsilon = 0.0);
1039 // Reduce number of vertices in representation...
1040 // void smooth(double epsilon);
1042 void reverse();
1044 void append(const Polyline &polyline);
1045
1048 return vertices_ == other.vertices_;
1049 }
1050
1051private:
1052 std::vector<Point> vertices_;
1053};
1054
1055// print Polyline
1056std::ostream &operator<<(std::ostream &outs, const Polyline &polyline_temp);
1057
1065class Polygon {
1066public:
1067 friend class Point;
1068 // Constructors
1076 Polygon(const std::string &filename);
1081 Polygon(const std::vector<Point> &vertices_temp);
1086 Polygon(const Point &point0, const Point &point1, const Point &point2);
1087 // Accessors
1093 const Point &operator[](unsigned i) const {
1094 return vertices_[i % vertices_.size()];
1095 }
1100 unsigned n() const { return vertices_.size(); }
1108 unsigned r() const;
1120 bool is_simple(double epsilon = 0.0) const;
1128 bool is_in_standard_form() const;
1130 double boundary_length() const;
1140 double area() const;
1147 Point centroid() const;
1156 double diameter() const;
1162 Bounding_Box bbox() const;
1163 // Returns a vector of n pts randomly situated in the polygon.
1164 std::vector<Point> random_points(const unsigned &count,
1165 double epsilon = 0.0) const;
1173 void write_to_file(const std::string &filename,
1174 int fios_precision_temp = FIOS_PRECISION);
1175 // Mutators
1181 Point &operator[](unsigned i) { return vertices_[i % vertices_.size()]; }
1183 void set_vertices(const std::vector<Point> &vertices_temp) {
1184 vertices_ = vertices_temp;
1185 }
1187 void push_back(const Point &vertex_temp) { vertices_.push_back(vertex_temp); }
1189 void clear() { vertices_.clear(); }
1199 void enforce_standard_form();
1210 void eliminate_redundant_vertices(double epsilon = 0.0);
1215 void reverse();
1216
1217protected:
1218 std::vector<Point> vertices_;
1219};
1220
1227bool operator==(Polygon polygon1, Polygon polygon2);
1228bool operator!=(Polygon polygon1, Polygon polygon2);
1245bool equivalent(Polygon polygon1, Polygon polygon2, double epsilon = 0.0);
1246
1252double boundary_distance(const Polygon &polygon1, const Polygon &polygon2);
1253
1254// print Polygon
1255std::ostream &operator<<(std::ostream &outs, const Polygon &polygon_temp);
1256
1264public:
1265 friend class Point;
1266 // Constructors
1274 Environment(const Polygon &polygon_temp) {
1275 outer_boundary_ = polygon_temp;
1276 update_flattened_index_key();
1277 }
1285 Environment(const std::vector<Polygon> &polygons);
1292 Environment(const std::string &filename);
1293 // Accessors
1301 const Polygon &operator[](unsigned i) const {
1302 if (i == 0) {
1303 return outer_boundary_;
1304 } else {
1305 return holes_[i - 1];
1306 }
1307 }
1321 const Point &operator()(unsigned k) const;
1323 unsigned h() const { return holes_.size(); }
1328 unsigned n() const;
1335 unsigned r() const;
1344 bool is_in_standard_form() const;
1360 bool is_valid(double epsilon = 0.0) const;
1367 double boundary_length() const;
1374 double area() const;
1384 double diameter() const { return outer_boundary_.diameter(); }
1390 Bounding_Box bbox() const { return outer_boundary_.bbox(); }
1397 std::vector<Point> random_points(const unsigned &count,
1398 double epsilon = 0.0) const;
1421 Polyline shortest_path(const Point &start, const Point &finish,
1422 const Visibility_Graph &visibility_graph,
1423 double epsilon = 0.0);
1436 Polyline shortest_path(const Point &start, const Point &finish,
1437 double epsilon = 0.0);
1444 std::vector<Polygon>
1445 compute_partition_cells(std::vector<Line_Segment> partition_inducing_segments,
1446 double epsilon = 0.0) {
1447 std::vector<Polygon> cells;
1448 return cells;
1449 }
1457 void write_to_file(const std::string &filename,
1458 int fios_precision_temp = FIOS_PRECISION);
1459 // Mutators
1468 Polygon &operator[](unsigned i) {
1469 if (i == 0) {
1470 return outer_boundary_;
1471 } else {
1472 return holes_[i - 1];
1473 }
1474 }
1475 // Mutators
1488 Point &operator()(unsigned k);
1490 void set_outer_boundary(const Polygon &polygon_temp) {
1491 outer_boundary_ = polygon_temp;
1492 update_flattened_index_key();
1493 }
1495 void add_hole(const Polygon &polygon_temp) {
1496 holes_.push_back(polygon_temp);
1497 update_flattened_index_key();
1498 }
1509 void enforce_standard_form();
1519 void eliminate_redundant_vertices(double epsilon = 0.0);
1525 void reverse_holes();
1526
1527private:
1528 Polygon outer_boundary_;
1529 // obstacles
1530 std::vector<Polygon> holes_;
1531 // allows constant time access to vertices via operator () with
1532 // flattened index as argument
1533 std::vector<std::pair<unsigned, unsigned> > flattened_index_key_;
1534 // Must call if size of outer_boundary and/or holes_ changes. Time
1535 // complexity O(n), where n is the number of vertices representing
1536 // the Environment.
1537 void update_flattened_index_key();
1538 // converts flattened index to index pair (hole #, vertex #) in
1539 // time O(n), where n is the number of vertices representing the
1540 // Environment
1541 std::pair<unsigned, unsigned> one_to_two(unsigned k) const;
1542 // node used for search tree of A* search in shortest_path() method
1544 public:
1545 // flattened index of corresponding Environment vertex
1546 // convention vertex_index = n() => corresponds to start Point
1547 // vertex_index = n() + 1 => corresponds to finish Point
1548 unsigned vertex_index;
1549 // pointer to self in search tree.
1550 std::list<Shortest_Path_Node>::iterator search_tree_location;
1551 // pointer to parent in search tree.
1552 std::list<Shortest_Path_Node>::iterator parent_search_tree_location;
1553 // Geodesic distance from start Point.
1554 double cost_to_come;
1555 // Euclidean distance to finish Point.
1556 double estimated_cost_to_go;
1557 // std::vector<Shortest_Path_Node> expand();
1558 bool operator<(const Shortest_Path_Node &spn2) const {
1559 double f1 = this->cost_to_come + this->estimated_cost_to_go;
1560 double f2 = spn2.cost_to_come + spn2.estimated_cost_to_go;
1561 if (f1 < f2)
1562 return true;
1563 else if (f2 < f1)
1564 return false;
1565 else if (this->vertex_index < spn2.vertex_index)
1566 return true;
1567 else if (this->vertex_index > spn2.vertex_index)
1568 return false;
1569 else if (&(*(this->parent_search_tree_location)) <
1570 &(*(spn2.parent_search_tree_location)))
1571 return true;
1572 else
1573 return false;
1574 }
1575 // print member data for debugging
1576 void print() const {
1577 std::cout << " vertex_index = " << vertex_index << std::endl
1578 << "parent's vertex_index = "
1579 << parent_search_tree_location->vertex_index << std::endl
1580 << " cost_to_come = " << cost_to_come << std::endl
1581 << " estimated_cost_to_go = " << estimated_cost_to_go
1582 << std::endl;
1583 }
1584 };
1585};
1586
1588std::ostream &operator<<(std::ostream &outs,
1589 const Environment &environment_temp);
1590
1593class Guards {
1594public:
1595 friend class Visibility_Graph;
1596 // Constructors
1603 Guards(const std::string &filename);
1605 Guards(const std::vector<Point> &positions) { positions_ = positions; }
1606 // Accessors
1613 const Point &operator[](unsigned i) const { return positions_[i]; }
1615 unsigned N() const { return positions_.size(); }
1617 bool are_lex_ordered() const;
1619 bool noncolocated(double epsilon = 0.0) const;
1621 bool in(const Polygon &polygon_temp, double epsilon = 0.0) const;
1623 bool in(const Environment &environment_temp, double epsilon = 0.0) const;
1633 double diameter() const;
1639 Bounding_Box bbox() const;
1647 void write_to_file(const std::string &filename,
1648 int fios_precision_temp = FIOS_PRECISION);
1649 // Mutators
1656 Point &operator[](unsigned i) { return positions_[i]; }
1658 void push_back(const Point &point_temp) { positions_.push_back(point_temp); }
1660 void set_positions(const std::vector<Point> &positions_temp) {
1661 positions_ = positions_temp;
1662 }
1671 void enforce_lex_order();
1673 void reverse();
1686 void snap_to_vertices_of(const Environment &environment_temp,
1687 double epsilon = 0.0);
1688
1701 void snap_to_vertices_of(const Polygon &polygon_temp, double epsilon = 0.0);
1714 void snap_to_boundary_of(const Environment &environment_temp,
1715 double epsilon = 0.0);
1728 void snap_to_boundary_of(const Polygon &polygon_temp, double epsilon = 0.0);
1729
1730private:
1731 std::vector<Point> positions_;
1732};
1733
1735std::ostream &operator<<(std::ostream &outs, const Guards &guards);
1736
1757public:
1758 // Constructors
1761 //:TRICKY:
1776 Visibility_Polygon(const Point &observer, const Environment &environment_temp,
1777 double epsilon = 0.0);
1791 Visibility_Polygon(const Point &observer, const Polygon &polygon_temp,
1792 double epsilon = 0.0);
1793 // Accessors
1794 // std::vector<bool> get_gap_edges(double epsilon=0.0) { return gap_edges_; }
1796 Point observer() const { return observer_; }
1797 // Mutators
1798private:
1799 // ith entry of gap_edges is true iff the edge following ith vertex
1800 // is a gap edge (not solid).
1801 // std::vector<bool> gap_edges_;
1802 Point observer_;
1803
1804 struct Polar_Edge {
1805 Polar_Point first;
1806 Polar_Point second;
1807 Polar_Edge() {}
1808 Polar_Edge(const Polar_Point &ppoint1, const Polar_Point &ppoint2)
1809 : first(ppoint1), second(ppoint2) {}
1810 };
1811
1813 public:
1814 std::list<Polar_Edge>::iterator incident_edge;
1815 bool is_first; // True iff polar_point is the first_point of the
1816 // Polar_Edge pointed to by
1817 // incident_edge.
1818 void set_polar_point(const Polar_Point &ppoint_temp) {
1819 set_polar_origin(ppoint_temp.polar_origin());
1820 set_x(ppoint_temp.x());
1821 set_y(ppoint_temp.y());
1822 set_range(ppoint_temp.range());
1823 set_bearing(ppoint_temp.bearing());
1824 }
1825 // The operator < is the same as for Polar_Point with one
1826 // exception. If two vertices have equal coordinates, but one is
1827 // the first point of its respecitve edge and the other is the
1828 // second point of its respective edge, then the vertex which is
1829 // the second point of its respective edge is considered
1830 // lexicographically smaller.
1831 friend bool operator<(const Polar_Point_With_Edge_Info &ppwei1,
1832 const Polar_Point_With_Edge_Info &ppwei2) {
1833 if (Polar_Point(ppwei1) == Polar_Point(ppwei2) and !ppwei1.is_first and
1834 ppwei2.is_first)
1835 return true;
1836 else
1837 return Polar_Point(ppwei1) < Polar_Point(ppwei2);
1838 }
1839 };
1840
1841 // Strict weak ordering (acts like <) for pointers to Polar_Edges.
1842 // Used to sort the priority_queue q2 used in the radial line sweep
1843 // of Visibility_Polygon constructors. Let p1 be a pointer to
1844 // Polar_Edge e1 and p2 be a pointer to Polar_Edge e2. Then p1 is
1845 // considered greater (higher priority) than p2 if the distance
1846 // from the observer (pointed to by observer_pointer) to e1 along
1847 // the direction to current_vertex is smaller than the distance
1848 // from the observer to e2 along the direction to current_vertex.
1850 const Point *const observer_pointer;
1851 const Polar_Point_With_Edge_Info *const current_vertex_pointer;
1852 double epsilon;
1853
1854 public:
1856 const Polar_Point_With_Edge_Info &current_vertex,
1857 double epsilon_temp)
1858 : observer_pointer(&observer), current_vertex_pointer(&current_vertex),
1859 epsilon(epsilon_temp) {}
1860 bool operator()(std::list<Polar_Edge>::iterator e1,
1861 std::list<Polar_Edge>::iterator e2) const {
1862 Polar_Point k1, k2;
1863 Line_Segment xing1 = intersection(
1864 Ray(*observer_pointer, current_vertex_pointer->bearing()),
1865 Line_Segment(e1->first, e1->second), epsilon);
1866 Line_Segment xing2 = intersection(
1867 Ray(*observer_pointer, current_vertex_pointer->bearing()),
1868 Line_Segment(e2->first, e2->second), epsilon);
1869 if (xing1.size() > 0 and xing2.size() > 0) {
1870 k1 = Polar_Point(*observer_pointer, xing1.first());
1871 k2 = Polar_Point(*observer_pointer, xing2.first());
1872 if (k1.range() <= k2.range())
1873 return false;
1874 return true;
1875 }
1876 // Otherwise infeasible edges are given higher priority, so they
1877 // get pushed out the top of the priority_queue's (q2's)
1878 // heap.
1879 else if (xing1.size() == 0 and xing2.size() > 0)
1880 return false;
1881 else if (xing1.size() > 0 and xing2.size() == 0)
1882 return true;
1883 else
1884 return true;
1885 }
1886 };
1887
1888 bool is_spike(const Point &observer, const Point &point1, const Point &point2,
1889 const Point &point3, double epsilon = 0.0) const;
1890
1891 // For eliminating spikes as they appear. In the
1892 // Visibility_Polygon constructors, these are checked every time a
1893 // Point is added to vertices.
1894 void chop_spikes_at_back(const Point &observer, double epsilon);
1895 void chop_spikes_at_wrap_around(const Point &observer, double epsilon);
1896 void chop_spikes(const Point &observer, double epsilon);
1897 // For debugging Visibility_Polygon constructors.
1898 // Prints current_vertex and active_edge data to screen.
1899 void print_cv_and_ae(const Polar_Point_With_Edge_Info &current_vertex,
1900 const std::list<Polar_Edge>::iterator &active_edge);
1901};
1902
1915public:
1916 // Constructors
1919 n_ = 0;
1920 adjacency_matrix_ = NULL;
1921 }
1943 Visibility_Graph(const Environment &environment, double epsilon = 0.0);
1944 // Constructors
1959 Visibility_Graph(const std::vector<Point> points,
1960 const Environment &environment, double epsilon = 0.0);
1961 // Constructors
1978 Visibility_Graph(const Guards &guards, const Environment &environment,
1979 double epsilon = 0.0);
1980 // Accessors
1992 bool operator()(unsigned i1, unsigned j1, unsigned i2, unsigned j2) const;
2009 bool operator()(unsigned k1, unsigned k2) const;
2011 unsigned n() const { return n_; }
2012 // Mutators
2024 bool &operator()(unsigned i1, unsigned j1, unsigned i2, unsigned j2);
2041 bool &operator()(unsigned k1, unsigned k2);
2043 Visibility_Graph &operator=(const Visibility_Graph &visibility_graph_temp);
2045 virtual ~Visibility_Graph();
2046
2047private:
2048 // total number of vertices of corresponding Environment
2049 unsigned n_;
2050 // the number of vertices in each Polygon of corresponding Environment
2051 std::vector<unsigned> vertex_counts_;
2052 // n_-by-n_ adjacency matrix data stored as 2D dynamic array
2053 bool **adjacency_matrix_;
2054 // converts vertex pairs (hole #, vertex #) to flattened index
2055 unsigned two_to_one(unsigned i, unsigned j) const;
2056};
2057
2059std::ostream &operator<<(std::ostream &outs,
2060 const Visibility_Graph &visibility_graph);
2061
2069public:
2076 static void set_output_precision();
2077
2083 static void seed_random();
2084
2093 template <std::size_t N>
2094 static std::vector<Point> make_point_vector(std::array<double, N> vertices) {
2095 static_assert(N % 2 == 0,
2096 "Specify an even number of points to make a point array.");
2097 std::vector<Point> point_vector;
2098 for (size_t i = 0; i < N; i += 2) {
2099 point_vector.push_back(Point(vertices[i], vertices[i + 1]));
2100 }
2101 return point_vector;
2102 }
2103};
2104
2111public:
2123 static bool validate(const VisiLibity::Environment &environment,
2124 const double epsilon, const VisiLibity::Guards &guards);
2125};
2126
2127} // namespace VisiLibity
2128
2129#endif // VISILIBITY_H
angle in radians represented by a value in the interval [0,2*M_PI]
Definition: visilibity.hpp:699
void set(double data_temp)
set angle, mod into interval [0, 2*PI)
Definition: visilibity.cpp:883
void randomize()
set to new random angle in [0, 2*M_PI)
Definition: visilibity.cpp:885
Angle()
default
Definition: visilibity.hpp:707
double get() const
get radians
Definition: visilibity.hpp:716
void set_to_2pi()
set angle data to 2*M_PI
Definition: visilibity.hpp:726
Definition: visilibity.hpp:1543
environment represented by simple polygonal outer boundary with simple polygonal holes
Definition: visilibity.hpp:1263
double boundary_length() const
sum of perimeter lengths of outer boundary and holes
Definition: visilibity.cpp:1768
double area() const
(obstacle/hole free) area of the Environment
Definition: visilibity.cpp:1778
Polyline shortest_path(const Point &start, const Point &finish, const Visibility_Graph &visibility_graph, double epsilon=0.0)
compute a shortest path between 2 Points
Definition: visilibity.cpp:1810
void eliminate_redundant_vertices(double epsilon=0.0)
eliminates vertices which are (epsilon) - colinear with their respective neighbors
Definition: visilibity.cpp:2131
void enforce_standard_form()
enforces outer boundary vertices are listed ccw and holes listed cw, and that these lists begin with ...
Definition: visilibity.cpp:2120
unsigned h() const
hole count
Definition: visilibity.hpp:1323
unsigned n() const
vertex count
Definition: visilibity.cpp:1651
const Polygon & operator[](unsigned i) const
raw access to Polygons
Definition: visilibity.hpp:1301
void set_outer_boundary(const Polygon &polygon_temp)
set outer boundary
Definition: visilibity.hpp:1490
Polygon & operator[](unsigned i)
raw access to Polygons
Definition: visilibity.hpp:1468
void reverse_holes()
reverse (cyclic) order of vertices belonging to holes only
Definition: visilibity.cpp:2139
const Point & operator()(unsigned k) const
raw access to vertices via flattened index
Definition: visilibity.cpp:1645
Environment(const Polygon &polygon_temp)
construct Environment without holes
Definition: visilibity.hpp:1274
void write_to_file(const std::string &filename, int fios_precision_temp=FIOS_PRECISION)
write lists of vertices to *.environment file
Definition: visilibity.cpp:2095
std::vector< Polygon > compute_partition_cells(std::vector< Line_Segment > partition_inducing_segments, double epsilon=0.0)
compute the faces (partition cells) of an arrangement of Line_Segments inside the Environment
Definition: visilibity.hpp:1445
void add_hole(const Polygon &polygon_temp)
add hole
Definition: visilibity.hpp:1495
std::vector< Point > random_points(const unsigned &count, double epsilon=0.0) const
get STL vector of count Points randomly situated within epsilon of the Environment
Definition: visilibity.cpp:1785
Environment()
default to empty
Definition: visilibity.hpp:1268
bool is_valid(double epsilon=0.0) const
true iff epsilon -valid
Definition: visilibity.cpp:1679
Bounding_Box bbox() const
box which fits snugly around the Environment
Definition: visilibity.hpp:1390
double diameter() const
Euclidean diameter.
Definition: visilibity.hpp:1384
unsigned r() const
total reflex vertex count (nonconvex vertices)
Definition: visilibity.cpp:1659
bool is_in_standard_form() const
true iff lexicographically smallest vertex is first in each list of vertices representing a Polygon o...
Definition: visilibity.cpp:1669
set of Guards represented by a list of Points
Definition: visilibity.hpp:1593
void enforce_lex_order()
sort positions in lexicographic order
Definition: visilibity.cpp:2308
Point & operator[](unsigned i)
raw access to guard position Points
Definition: visilibity.hpp:1656
void snap_to_boundary_of(const Environment &environment_temp, double epsilon=0.0)
relocate each guard to closest Point on boundary if within epsilon of the boundary (of environment_te...
Definition: visilibity.cpp:2326
Guards()
default to empty
Definition: visilibity.hpp:1598
void push_back(const Point &point_temp)
add a guard
Definition: visilibity.hpp:1658
bool noncolocated(double epsilon=0.0) const
true iff no two guards are w/in epsilon of each other
Definition: visilibity.cpp:2227
unsigned N() const
guard count
Definition: visilibity.hpp:1615
void set_positions(const std::vector< Point > &positions_temp)
set positions with STL vector of Points
Definition: visilibity.hpp:1660
const Point & operator[](unsigned i) const
raw access to guard position Points
Definition: visilibity.hpp:1613
bool are_lex_ordered() const
true iff positions are lexicographically ordered
Definition: visilibity.cpp:2218
void snap_to_vertices_of(const Environment &environment_temp, double epsilon=0.0)
relocate each guard to closest vertex if within epsilon of some vertex (of environment_temp)
Definition: visilibity.cpp:2315
double diameter() const
Euclidean diameter.
Definition: visilibity.cpp:2249
bool in(const Polygon &polygon_temp, double epsilon=0.0) const
true iff all guards are located in polygon_temp
Definition: visilibity.cpp:2235
void write_to_file(const std::string &filename, int fios_precision_temp=FIOS_PRECISION)
write list of positions to *.guards file
Definition: visilibity.cpp:2291
void reverse()
reverse order of positions
Definition: visilibity.cpp:2313
Guards(const std::vector< Point > &positions)
construct from STL vector of Points
Definition: visilibity.hpp:1605
Bounding_Box bbox() const
box which fits snugly around the Guards
Definition: visilibity.cpp:2263
line segment in the plane represented by its endpoints
Definition: visilibity.hpp:502
Line_Segment & operator=(const Line_Segment &line_segment_temp)
assignment operator
Definition: visilibity.cpp:537
void reverse()
reverse order of endpoints
Definition: visilibity.cpp:625
void enforce_standard_form()
enforce that lex. smallest endpoint first
Definition: visilibity.cpp:633
bool is_in_standard_form() const
true iff vertices in lex. order
Definition: visilibity.cpp:529
virtual ~Line_Segment()
destructor
Definition: visilibity.cpp:644
double length() const
Euclidean length.
Definition: visilibity.cpp:523
Line_Segment()
default to empty
Definition: visilibity.cpp:458
Point midpoint() const
midpoint
Definition: visilibity.cpp:517
unsigned size() const
number of distinct endpoints
Definition: visilibity.hpp:538
Point first() const
first endpoint
Definition: visilibity.cpp:502
void set_second(const Point &point_temp, double epsilon=0.0)
set second endpoint
Definition: visilibity.cpp:593
void set_first(const Point &point_temp, double epsilon=0.0)
set first endpoint
Definition: visilibity.cpp:561
Point second() const
second endpoint
Definition: visilibity.cpp:508
void clear()
erase both endpoints and set line segment empty (size 0)
Definition: visilibity.cpp:638
Point in the plane represented by Cartesian coordinates.
Definition: visilibity.hpp:125
Point(double x_temp, double y_temp)
costruct from raw coordinates
Definition: visilibity.hpp:135
bool in(const Line_Segment &line_segment_temp, double epsilon=0.0) const
true iff w/in epsilon of line_segment_temp
Definition: visilibity.cpp:211
void set_y(double y_temp)
change y coordinate
Definition: visilibity.hpp:292
bool on_boundary_of(const Polygon &polygon_temp, double epsilon=0.0) const
true iff w/in epsilon of boundary of polygon_temp
Definition: visilibity.cpp:191
bool in_relative_interior_of(const Line_Segment &line_segment_temp, double epsilon=0.0) const
true iff w/in epsilon of interior but greater than espilon away from endpoints of line_segment_temp
Definition: visilibity.cpp:219
Point projection_onto_boundary_of(const Polygon &polygon_temp) const
closest Point on boundary of polygon_temp
Definition: visilibity.cpp:156
void snap_to_boundary_of(const Polygon &polygon_temp, double epsilon=0.0)
relocate to closest Point on boundary if w/in epsilon of the boundary (of polygon_temp)
Definition: visilibity.cpp:296
double y() const
get y coordinate
Definition: visilibity.hpp:143
void snap_to_vertices_of(const Polygon &polygon_temp, double epsilon=0.0)
relocate to closest vertex if w/in epsilon of some vertex (of polygon_temp)
Definition: visilibity.cpp:280
Point projection_onto_vertices_of(const Polygon &polygon_temp) const
closest vertex of polygon_temp
Definition: visilibity.cpp:124
Point projection_onto(const Line_Segment &line_segment_temp) const
closest Point on line_segment_temp
Definition: visilibity.cpp:61
Point()
default
Definition: visilibity.hpp:133
bool is_endpoint_of(const Line_Segment &line_segment_temp, double epsilon=0.0) const
true iff w/in epsilon of some endpoint of line_segment_temp
Definition: visilibity.cpp:270
void set_x(double x_temp)
change x coordinate
Definition: visilibity.hpp:290
double x() const
get x coordinate
Definition: visilibity.hpp:141
Point in the plane packaged together with polar coordinates w.r.t. specified origin.
Definition: visilibity.hpp:779
Polar_Point()
default
Definition: visilibity.hpp:787
Point polar_origin() const
origin of the polar coordinate system in which the point is represented
Definition: visilibity.hpp:803
Angle bearing() const
bearing from polar origin w.r.t. direction parallel to x-axis
Definition: visilibity.hpp:808
void set_range(double range_temp)
set range
Definition: visilibity.cpp:976
void set_bearing_to_2pi()
set bearing Angle data to 2*M_PI
Definition: visilibity.hpp:847
void set_y(double y_temp)
set y
Definition: visilibity.cpp:972
void set_x(double x_temp)
set x
Definition: visilibity.cpp:968
void set_polar_origin(const Point &polar_origin_temp)
set the origin of the polar coordinate system
Definition: visilibity.cpp:964
double range() const
Definition: visilibity.hpp:806
void set_bearing(const Angle &bearing_temp)
set bearing
Definition: visilibity.cpp:982
simple polygon in the plane represented by list of vertices
Definition: visilibity.hpp:1065
void push_back(const Point &vertex_temp)
push a Point onto the back of the vertex list
Definition: visilibity.hpp:1187
bool is_in_standard_form() const
true iff lexicographically smallest vertex is first in the list of vertices representing the Polygon
Definition: visilibity.cpp:1308
void set_vertices(const std::vector< Point > &vertices_temp)
set vertices using STL vector of Points
Definition: visilibity.hpp:1183
unsigned r() const
reflex vertex count (nonconvex vertices)
Definition: visilibity.cpp:1267
unsigned n() const
vertex count
Definition: visilibity.hpp:1100
const Point & operator[](unsigned i) const
access with automatic wrap-around in forward direction
Definition: visilibity.hpp:1093
Point centroid() const
Polygon's centroid (center of mass)
Definition: visilibity.cpp:1336
bool is_simple(double epsilon=0.0) const
true iff Polygon is (epsilon) simple
Definition: visilibity.cpp:1285
void eliminate_redundant_vertices(double epsilon=0.0)
eliminates vertices which are (epsilon) - colinear with their respective neighbors
Definition: visilibity.cpp:1463
void enforce_standard_form()
enforces that the lexicographically smallest vertex is first in the list of vertices representing the...
Definition: visilibity.cpp:1443
double area() const
Definition: visilibity.cpp:1326
void reverse()
reverse (cyclic) order of vertices
Definition: visilibity.cpp:1508
Bounding_Box bbox() const
box which fits snugly around the Polygon
Definition: visilibity.cpp:1374
double boundary_length() const
perimeter length
Definition: visilibity.cpp:1316
void clear()
erase all vertices
Definition: visilibity.hpp:1189
Polygon()
default to empty
Definition: visilibity.hpp:1070
Point & operator[](unsigned i)
access with automatic wrap-around in forward direction
Definition: visilibity.hpp:1181
double diameter() const
Euclidean diameter.
Definition: visilibity.cpp:1360
void write_to_file(const std::string &filename, int fios_precision_temp=FIOS_PRECISION)
write list of vertices to *.polygon file
Definition: visilibity.cpp:1428
oriented polyline in the plane represented by list of vertices
Definition: visilibity.hpp:977
void pop_back()
delete a vertex to the back (end) of the list
Definition: visilibity.hpp:1024
Point & operator[](unsigned i)
raw access
Definition: visilibity.hpp:1018
double length() const
Euclidean length of the Polyline.
Definition: visilibity.cpp:1132
unsigned size() const
vertex count
Definition: visilibity.hpp:998
bool operator==(const VisiLibity::Polyline &other)
Compare two Polylines.
Definition: visilibity.hpp:1047
Point operator[](unsigned i) const
raw access
Definition: visilibity.hpp:996
void reverse()
reverse order of vertices
Definition: visilibity.cpp:1223
void clear()
erase all points
Definition: visilibity.hpp:1020
Polyline()
default to empty
Definition: visilibity.hpp:982
Polyline(const std::vector< Point > &vertices_temp)
construct from vector of vertices
Definition: visilibity.hpp:984
double diameter() const
Euclidean diameter.
Definition: visilibity.cpp:1139
void append(const Polyline &polyline)
append the points from another polyline
Definition: visilibity.cpp:1231
void push_back(const Point &point_temp)
add a vertex to the back (end) of the list
Definition: visilibity.hpp:1022
void set_vertices(const std::vector< Point > &vertices_temp)
reset the whole list of vertices at once
Definition: visilibity.hpp:1026
void eliminate_redundant_vertices(double epsilon=0.0)
eliminates vertices which are (epsilon) - colinear with their respective neighbors
Definition: visilibity.cpp:1181
ray in the plane represented by base Point and bearing Angle
Definition: visilibity.hpp:909
Ray()
default
Definition: visilibity.hpp:917
void set_base_point(const Point &point_temp)
set base point
Definition: visilibity.hpp:932
Point base_point() const
get base point
Definition: visilibity.hpp:927
Ray(Point base_point_temp, Angle bearing_temp)
Definition: visilibity.hpp:920
void set_bearing(const Angle &angle_temp)
set bearing
Definition: visilibity.hpp:934
Angle bearing() const
get bearing
Definition: visilibity.hpp:929
Utilities for writing shortest path VisiLibity unit tests.
Definition: visilibity.hpp:2110
static bool validate(const VisiLibity::Environment &environment, const double epsilon, const VisiLibity::Guards &guards)
Definition: visilibity.cpp:3178
Utilities for writing VisiLibity unit tests.
Definition: visilibity.hpp:2068
static void set_output_precision()
Definition: visilibity.cpp:3269
static std::vector< Point > make_point_vector(std::array< double, N > vertices)
Definition: visilibity.hpp:2094
static void seed_random()
Definition: visilibity.cpp:3277
visibility graph of points in an Environment, represented by adjacency matrix
Definition: visilibity.hpp:1914
unsigned n() const
total number of vertices in corresponding Environment
Definition: visilibity.hpp:2011
virtual ~Visibility_Graph()
destructor
Definition: visilibity.cpp:3156
Visibility_Graph()
default to empty
Definition: visilibity.hpp:1918
bool operator()(unsigned i1, unsigned j1, unsigned i2, unsigned j2) const
raw access to adjacency matrix data
Definition: visilibity.cpp:3103
Visibility_Graph & operator=(const Visibility_Graph &visibility_graph_temp)
assignment operator
Definition: visilibity.cpp:3119
visibility polygon of a Point in an Environment or Polygon
Definition: visilibity.hpp:1756
Visibility_Polygon()
default to empty
Definition: visilibity.hpp:1760
Point observer() const
location of observer which induced the visibility polygon
Definition: visilibity.hpp:1796
VisiLibity's sole namespace.
Definition: visilibity.hpp:73
bool operator!=(const Point &point1, const Point &point2)
True iff Points' coordinates are not identical.
Definition: visilibity.cpp:315
bool operator<(const Point &point1, const Point &point2)
compare lexicographic order of points
Definition: visilibity.cpp:319
bool operator==(const Point &point1, const Point &point2)
True iff Points' coordinates are identical.
Definition: visilibity.cpp:312
bool operator<=(const Point &point1, const Point &point2)
compare lexicographic order of points
Definition: visilibity.cpp:342
bool intersect_proper(const Line_Segment &line_segment1, const Line_Segment &line_segment2, double epsilon=0.0)
true iff line segments intersect properly w/in epsilon, false if either line segment has size 0
Definition: visilibity.cpp:732
Point operator-(const Point &point1, const Point &point2)
vector subtraction of Points
Definition: visilibity.cpp:351
double geodesic_direction(const Angle &angle1, const Angle &angle2)
1.0 => geodesic path from angle1 to angle2 is couterclockwise, -1.0 => clockwise
Definition: visilibity.cpp:926
bool operator>(const Point &point1, const Point &point2)
compare lexicographic order of points
Definition: visilibity.cpp:328
const int FIOS_PRECISION
floating-point display precision.
Definition: visilibity.hpp:95
double boundary_distance(const Point &point_temp, const Polygon &polygon_temp)
Euclidean distance between a Point and a Polygon's boundary.
Definition: visilibity.cpp:416
double cross(const Point &point1, const Point &point2)
cross product (signed) magnitude treats the Points as vectors
Definition: visilibity.cpp:366
bool operator>=(const Point &point1, const Point &point2)
compare lexicographic order of points
Definition: visilibity.cpp:337
double geodesic_distance(const Angle &angle1, const Angle &angle2)
geodesic distance in radians between Angles
Definition: visilibity.cpp:916
bool equivalent(Line_Segment line_segment1, Line_Segment line_segment2, double epsilon=0)
true iff line segments' endpoints match up w/in a (closed) epsilon ball of each other,...
Definition: visilibity.cpp:663
Line_Segment intersection(const Line_Segment &line_segment1, const Line_Segment &line_segment2, double epsilon=0.0)
intersection of Line_Segments
Definition: visilibity.cpp:767
bool intersect(const Line_Segment &line_segment1, const Line_Segment &line_segment2, double epsilon=0.0)
true iff the Euclidean distance between Line_Segments is no greater than epsilon, false if either lin...
Definition: visilibity.cpp:723
Point operator+(const Point &point1, const Point &point2)
vector addition of Points
Definition: visilibity.cpp:348
double uniform_random_sample(double lower_bound, double upper_bound)
get a uniform random sample from an (inclusive) interval on the real line
Definition: visilibity.cpp:48
std::ostream & operator<<(std::ostream &outs, const Point &point_temp)
print a Point
Definition: visilibity.cpp:451
double distance(const Point &point1, const Point &point2)
Euclidean distance between Points.
Definition: visilibity.cpp:373
rectangle with sides parallel to the x- and y-axes
Definition: visilibity.hpp:120
Definition: visilibity.hpp:1804