File: Segment_3_Segment_3_intersection.h

package info (click to toggle)
cgal 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (127 lines) | stat: -rw-r--r-- 4,912 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2009  GeometryFactory (France), INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Intersections_3/include/CGAL/Intersections_3/internal/Segment_3_Segment_3_intersection.h $
// $Id: include/CGAL/Intersections_3/internal/Segment_3_Segment_3_intersection.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Sébastien Loriot
//
#ifndef CGAL_INTERNAL_INTERSECTIONS_3_SEGMENT_3_SEGMENT_3_INTERSECTION_H
#define CGAL_INTERNAL_INTERSECTIONS_3_SEGMENT_3_SEGMENT_3_INTERSECTION_H

#include <CGAL/Intersection_traits_3.h>
#include <CGAL/Intersections_3/internal/Line_3_Line_3_intersection.h>

namespace CGAL {
namespace Intersections {
namespace internal {

template <class K>
typename Intersection_traits<K, typename K::Segment_3, typename K::Segment_3>::result_type
intersection_collinear_segments(const typename K::Segment_3& s1,
                                const typename K::Segment_3& s2,
                                const K& k)
{
  CGAL_precondition(!s1.is_degenerate() && !s2.is_degenerate());

  const typename K::Point_3& p=s1[0], q=s1[1], r=s2[0], s=s2[1];

  typename K::Collinear_are_ordered_along_line_3 cln_order=k.collinear_are_ordered_along_line_3_object();

  if(cln_order(p,r,q))
  {
    if(cln_order(p,s,q))
      return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(s2);

    if(cln_order(r,p,s))
    {
      if(r != p)
        return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>( typename K::Segment_3(r,p) );

      if(cln_order(r,q,s))
        return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(s1);

      return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(p);
    }

    return (r != q) ? intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>( typename K::Segment_3(r,q) )
                    : intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(q);
  }

  if(cln_order(p,s,q))
  {
    if(cln_order(r,p,s))
    {
      if(s != p)
        return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>( typename K::Segment_3(s,p) );
      if(cln_order(r,q,s))
        return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(s1);

      return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(p);
    }

    return (s != q) ? intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>( typename K::Segment_3(s,q) )
                    : intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(q);
  }

  if(cln_order(r,p,s))
    return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(s1);

  return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>();
}

template<class K>
struct L_p_visitor
{
  typedef typename Intersection_traits<K, typename K::Segment_3, typename K::Segment_3>::result_type result_type;

  L_p_visitor(const typename K::Segment_3& s1,
              const typename K::Segment_3& s2)
    : s1(s1), s2(s2)
  { }

  const typename K::Segment_3& s1;
  const typename K::Segment_3& s2;

  result_type operator()(const typename K::Point_3& p) const
  {
    typename K::Collinear_are_ordered_along_line_3 cln_order = K().collinear_are_ordered_along_line_3_object();

    if(cln_order(s1[0],p,s1[1]) && cln_order(s2[0],p,s2[1]))
      return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>(p);
    else
      return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>();
  }

  result_type operator()(const typename K::Line_3&) const
  {
    return intersection_collinear_segments(s1,s2,K());
  }
};

template <class K>
typename Intersection_traits<K, typename K::Segment_3, typename K::Segment_3>::result_type
intersection(const typename K::Segment_3& s1,
             const typename K::Segment_3& s2,
             const K& k)
{
  CGAL_precondition(!s1.is_degenerate() && !s2.is_degenerate());

  typename Intersection_traits<K, typename K::Line_3, typename K::Line_3>::result_type
      v = internal::intersection(s1.supporting_line(), s2.supporting_line(), k);

  if(v)
    return std::visit(L_p_visitor<K>(s1, s2) , *v);

  return intersection_return<typename K::Intersect_3, typename K::Segment_3, typename K::Segment_3>();
}

} // namespace internal
} // namespace Intersections
} // namespace CGAL

#endif // CGAL_INTERNAL_INTERSECTIONS_3_SEGMENT_3_SEGMENT_3_INTERSECTION_H