// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2019 Hanxiao Shen // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "segment_segment_intersect.h" // https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ template IGL_INLINE bool igl::predicates::segment_segment_intersect( const Eigen::MatrixBase& a, const Eigen::MatrixBase& b, const Eigen::MatrixBase& c, const Eigen::MatrixBase& d ) { auto t1 = igl::predicates::orient2d(a,b,c); auto t2 = igl::predicates::orient2d(b,c,d); auto t3 = igl::predicates::orient2d(a,b,d); auto t4 = igl::predicates::orient2d(a,c,d); // assume m,n,p are colinear, check whether p is in range [m,n] auto on_segment = []( const Eigen::MatrixBase& m, const Eigen::MatrixBase& n, const Eigen::MatrixBase& p ){ return ((p(0) >= std::min(m(0),n(0))) && (p(0) <= std::max(m(0),n(0))) && (p(1) >= std::min(m(1),n(1))) && (p(1) <= std::max(m(1),n(1)))); }; // colinear case if((t1 == igl::predicates::Orientation::COLLINEAR && on_segment(a,b,c)) || (t2 == igl::predicates::Orientation::COLLINEAR && on_segment(c,d,b)) || (t3 == igl::predicates::Orientation::COLLINEAR && on_segment(a,b,d)) || (t4 == igl::predicates::Orientation::COLLINEAR && on_segment(c,d,a))) return true; // ordinary case return (t1 != t3 && t2 != t4); } #ifdef IGL_STATIC_LIBRARY template bool igl::predicates::segment_segment_intersect >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); #endif