| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file SimPolygon2D.h | 
					
						
							|  |  |  |  * @brief Polygons for simulation use | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose2.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/geometry/SimWall2D.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-16 11:45:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <random>
 | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * General polygon class for convex polygons | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-04-02 03:29:53 +08:00
										 |  |  | class GTSAM_UNSTABLE_EXPORT SimPolygon2D { | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |   Point2Vector landmarks_; | 
					
						
							| 
									
										
										
										
											2019-06-16 11:45:43 +08:00
										 |  |  |   static std::minstd_rand rng; | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Don't use this constructor, use a named one instead */ | 
					
						
							|  |  |  |   SimPolygon2D() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** seed the random number generator - only needs to be done once */ | 
					
						
							|  |  |  |   static void seedGenerator(unsigned long seed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Named constructor for creating triangles */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static SimPolygon2D createTriangle(const Point2& pA, const Point2& pB, const Point2& pC); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Named constructor for creating axis-aligned rectangles | 
					
						
							|  |  |  |    * @param p is the lower-left corner | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static SimPolygon2D createRectangle(const Point2& p, double height, double width); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Randomly generate a triangle that does not conflict with others | 
					
						
							|  |  |  |    * Uniformly distributed over box area, with normally distributed lengths of edges | 
					
						
							|  |  |  |    * THROWS: std::runtime_error if can't find a position | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   static SimPolygon2D randomTriangle(double side_len, double mean_side_len, double sigma_side_len, | 
					
						
							|  |  |  |       double min_vertex_dist, double min_side_len, const std::vector<SimPolygon2D>& existing_polys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Randomly generate a rectangle that does not conflict with others | 
					
						
							|  |  |  |    * Uniformly distributed over box area, with normally distributed lengths of edges | 
					
						
							|  |  |  |    * THROWS: std::runtime_error if can't find a position | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   static SimPolygon2D randomRectangle(double side_len, double mean_side_len, double sigma_side_len, | 
					
						
							|  |  |  |       double min_vertex_dist, double min_side_len, const std::vector<SimPolygon2D>& existing_polys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // access to underlying points
 | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   const Point2& landmark(size_t i) const { return landmarks_[i]; } | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  |   size_t size() const { return landmarks_.size(); } | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |   const Point2Vector& vertices() const { return landmarks_; } | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // testable requirements
 | 
					
						
							|  |  |  |   bool equals(const SimPolygon2D& p, double tol=1e-5) const; | 
					
						
							|  |  |  |   void print(const std::string& s="") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Get a set of walls along the edges | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   std::vector<SimWall2D> walls() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Core function for randomly generating scenarios. | 
					
						
							|  |  |  |    * Polygons are closed, convex shapes. | 
					
						
							|  |  |  |    * @return true if the given point is contained by this polygon | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   bool contains(const Point2& p) const; | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Checks two polygons to determine if they overlap | 
					
						
							|  |  |  |    * @return true iff at least one vertex of one polygon is contained in the other | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   bool overlaps(const SimPolygon2D& p) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** returns true iff p is contained in any of a set of polygons */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static bool anyContains(const Point2& p, const std::vector<SimPolygon2D>& obstacles); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** returns true iff polygon p overlaps with any of a set of polygons */ | 
					
						
							|  |  |  |   static bool anyOverlaps(const SimPolygon2D& p, const std::vector<SimPolygon2D>& obstacles); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** returns true iff p is inside a square centered at zero with side s */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static bool insideBox(double s, const Point2& p); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** returns true iff p is within threshold of any point in S */ | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |   static bool nearExisting(const Point2Vector& S, | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |       const Point2& p, double threshold); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random point uniformly over a box of side s */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Point2 randomPoint2(double s); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** randomly generate a Rot2 with a uniform distribution over theta */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Rot2 randomAngle(); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** generate a distance from a normal distribution given a mean and sigma */ | 
					
						
							|  |  |  |   static double randomDistance(double mu, double sigma, double min_dist = -1.0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random point within a box that is further than dist d away from existing landmarks */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Point2 randomBoundedPoint2(double boundary_size, | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |       const Point2Vector& landmarks, double min_landmark_dist); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random point within a box that meets above requirements, as well as staying out of obstacles */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Point2 randomBoundedPoint2(double boundary_size, | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |       const Point2Vector& landmarks, | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  |       const std::vector<SimPolygon2D>& obstacles, double min_landmark_dist); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random point that only avoid obstacles */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Point2 randomBoundedPoint2(double boundary_size, | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  |       const std::vector<SimPolygon2D>& obstacles); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random point in box defined by lower left and upper right corners */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Point2 randomBoundedPoint2( | 
					
						
							|  |  |  |       const Point2& LL_corner, const Point2& UR_corner, | 
					
						
							| 
									
										
										
										
											2018-09-27 22:40:44 +08:00
										 |  |  |       const Point2Vector& landmarks, | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  |       const std::vector<SimPolygon2D>& obstacles, double min_landmark_dist); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** pick a random pose in a bounded area that is not in an obstacle */ | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:39 +08:00
										 |  |  |   static Pose2 randomFreePose(double boundary_size, const std::vector<SimPolygon2D>& obstacles); | 
					
						
							| 
									
										
										
										
											2013-03-24 04:19:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef std::vector<SimPolygon2D> SimPolygon2DVector; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } //\namespace gtsam
 | 
					
						
							|  |  |  | 
 |