34 #define HAVE_INT64_T_64 # Prevents conflict with OpenCV typedef of int64 35 #include <geos/geom/CoordinateArraySequence.h> 36 #include <geos/geom/GeometryFactory.h> 37 #include <geos/geom/Polygon.h> 38 #include <geos/util/TopologyException.h> 39 #undef HAVE_INT64_T_64 50 double d = (p1[0] - p2[0]) * (p3[1] - p4[1]) - (p1[1] - p2[1]) * (p3[0] - p4[0]);
56 double n_a = (p1[0] * p2[1] - p1[1] * p2[0]) * (p3[0] - p4[0]) - (p1[0] - p2[0]) * (p3[0] * p4[1] - p3[1] * p4[0]);
57 double n_b = (p1[0] * p2[1] - p1[1] * p2[0]) * (p3[1] - p4[1]) - (p1[1] - p2[1]) * (p3[0] * p4[1] - p3[1] * p4[0]);
98 cv::Point2d r(p2 - p1);
100 cv::Point2d
s(p4 - p3);
101 cv::Point2d qp(q - p);
103 double rs = r.cross(s);
104 if (std::fabs(rs) > std::numeric_limits<float>::epsilon())
108 double t = qp.cross(cv::Point2d(s.x / rs, s.y / rs));
109 double u = (qp * -1).cross(cv::Point2d(r.x / -rs, r.y / -rs));
111 if (u >= 0 && t >= 0 && u <= 1.0 && t <= 1.0)
123 else if (std::fabs(qp.cross(r)) > std::numeric_limits<float>::epsilon())
131 double rlen = r.dot(r);
132 cv::Point2d unit_r(r.x / rlen, r.y / rlen);
133 double t0 = qp.dot(unit_r);
134 double t1 = t0 + s.dot(unit_r);
141 if (t0 <= 1.0 && t1 >= 0.0)
145 double t = std::max(0.0, t0);
161 if (((p2[0] - p1[0]) * (p3[1] - p1[0])) - ((p3[0] - p1[0]) * (p2[1] - p1[1])) > std::numeric_limits<float>::epsilon())
168 return (p1[0] <= p3[0] && p2[0] <= p1[0]) || (p1[0] <= p2[0] && p3[0] <= p1[0]);
172 return (p1[1] <= p3[1] && p2[1] <= p1[1]) || (p1[1] <= p2[1] && p3[1] <= p1[1]);
177 const std::vector<cv::Vec2d>& a,
178 const std::vector<cv::Vec2d>& b)
181 geos::geom::CoordinateArraySequence* a_coords =
new geos::geom::CoordinateArraySequence();
182 for (
size_t i = 0; i < a.size(); i++)
184 a_coords->add(geos::geom::Coordinate(a[i][0], a[i][1]));
186 a_coords->add(a_coords->front());
188 geos::geom::LinearRing* a_ring = geos::geom::GeometryFactory::getDefaultInstance()->createLinearRing(a_coords);
189 geos::geom::Polygon* a_polygon = geos::geom::GeometryFactory::getDefaultInstance()->createPolygon(a_ring, 0);
190 a_polygon->normalize();
193 geos::geom::CoordinateArraySequence* b_coords =
new geos::geom::CoordinateArraySequence();
194 for (
size_t i = 0; i < b.size(); i++)
196 b_coords->add(geos::geom::Coordinate(b[i][0], b[i][1]));
198 b_coords->add(b_coords->front());
200 geos::geom::LinearRing* b_ring = geos::geom::GeometryFactory::getDefaultInstance()->createLinearRing(b_coords);
201 geos::geom::Polygon* b_polygon = geos::geom::GeometryFactory::getDefaultInstance()->createPolygon(b_ring, 0);
202 b_polygon->normalize();
204 bool intersects = a_polygon->intersects(b_polygon);
214 const std::vector<cv::Vec2d>& a,
215 const std::vector<cv::Vec2d>& b)
217 if (a.size() < 3 || b.size() < 3)
224 geos::geom::CoordinateArraySequence* a_coords =
new geos::geom::CoordinateArraySequence();
225 for (
size_t i = 0; i < a.size(); i++)
227 a_coords->add(geos::geom::Coordinate(a[i][0], a[i][1]));
229 a_coords->add(a_coords->front());
231 geos::geom::LinearRing* a_ring = geos::geom::GeometryFactory::getDefaultInstance()->createLinearRing(a_coords);
232 geos::geom::Polygon* a_polygon = geos::geom::GeometryFactory::getDefaultInstance()->createPolygon(a_ring, 0);
233 a_polygon->normalize();
236 geos::geom::CoordinateArraySequence* b_coords =
new geos::geom::CoordinateArraySequence();
237 for (
size_t i = 0; i < b.size(); i++)
239 b_coords->add(geos::geom::Coordinate(b[i][0], b[i][1]));
241 b_coords->add(b_coords->front());
243 geos::geom::LinearRing* b_ring = geos::geom::GeometryFactory::getDefaultInstance()->createLinearRing(b_coords);
244 geos::geom::Polygon* b_polygon = geos::geom::GeometryFactory::getDefaultInstance()->createPolygon(b_ring, 0);
245 b_polygon->normalize();
249 if (a_polygon->intersects(b_polygon))
251 area = a_polygon->intersection(b_polygon)->getArea();
254 catch (
const geos::util::TopologyException& e)
bool LineSegmentIntersection(const cv::Vec2d &p1, const cv::Vec2d &p2, const cv::Vec2d &p3, const cv::Vec2d &p4, cv::Vec2d &c)
bool PolygonsIntersect(const std::vector< cv::Vec2d > &a, const std::vector< cv::Vec2d > &b)
bool LineIntersection(const cv::Vec2d &p1, const cv::Vec2d &p2, const cv::Vec2d &p3, const cv::Vec2d &p4, cv::Vec2d &c)
double PolygonIntersectionArea(const std::vector< cv::Vec2d > &a, const std::vector< cv::Vec2d > &b)
bool PointOnLineSegment(const cv::Vec2d &p1, const cv::Vec2d &p2, const cv::Vec2d &p3)