Datasets:

Modalities:
Image
Text
Formats:
text
Size:
< 1K
Libraries:
Datasets
License:
File size: 2,138 Bytes
5b89cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2019 Hanxiao Shen <hanxiao@cs.nyu.edu>
//
// 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 <typename DerivedP>
IGL_INLINE bool igl::predicates::segment_segment_intersect(
  const Eigen::MatrixBase<DerivedP>& a,
  const Eigen::MatrixBase<DerivedP>& b,
  const Eigen::MatrixBase<DerivedP>& c,
  const Eigen::MatrixBase<DerivedP>& d
)
{
  typename DerivedP::Scalar Scalar;
  
  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<DerivedP>& m,
    const Eigen::MatrixBase<DerivedP>& n,
    const Eigen::MatrixBase<DerivedP>& 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::Matrix<double, 1, 2, 1, 1, 2> >(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2> > const&);
#endif