File: Ray_2_Triangle_2.h

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (121 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 1998-2004
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Distance_2/include/CGAL/Distance_2/Ray_2_Triangle_2.h $
// $Id: include/CGAL/Distance_2/Ray_2_Triangle_2.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Geert-Jan Giezeman
//                 Michel Hoffmann <hoffmann@inf.ethz.ch>
//                 Andreas Fabri <Andreas.Fabri@geometryfactory.com>

#ifndef CGAL_DISTANCE_2_RAY_2_TRIANGLE_2_H
#define CGAL_DISTANCE_2_RAY_2_TRIANGLE_2_H

#include <CGAL/Distance_2/internal/squared_distance_utils_2.h>
#include <CGAL/Distance_2/Point_2_Triangle_2.h>

#include <CGAL/Ray_2.h>
#include <CGAL/Triangle_2.h>

namespace CGAL {
namespace internal {

template <class K>
typename K::FT
squared_distance(const typename K::Ray_2& ray,
                 const typename K::Triangle_2& triangle,
                 const K& k)
{
  typedef typename K::FT       FT;
  typedef typename K::Point_2  Point_2;
  typedef typename K::Line_2   Line_2;

  int ind_tr1, ind_tr2, ind_ray = 0, ind1;

  distance_index<K>(ind_tr1, ind_tr2, ray.source(), triangle, k);
  FT mindist = squared_distance_indexed(ray.source(), triangle, ind_tr1, ind_tr2, k);

  for(int i=0; i<3; ++i)
  {
    const Point_2& pt = triangle.vertex(i);
    distance_index<K>(ind1, pt, ray, k);
    FT dist = squared_distance_indexed(pt, ray, ind1, k);
    if(dist < mindist)
    {
      ind_ray = ind1;
      ind_tr1 = i; ind_tr2 = -1;
      mindist = dist;
    }
  }

  // now check if all vertices are on the right side of the separating line.
  // In case of vertex-vertex smallest distance this is the case.
  if(ind_tr2 == -1 && ind_ray != -1)
    return mindist;

  if(ind_tr2 != -1)
  {
    // Check if all the segment vertices lie at the same side of
    // the triangle segment.
    const Point_2& vt1 = triangle.vertex(ind_tr1);
    const Point_2& vt2 = triangle.vertex(ind_tr2);
    if(clockwise(ray.direction().vector(), vt2-vt1, k))
      mindist = FT(0);
  }
  else
  {
    // Check if all the triangle vertices lie
    // at the same side of the segment.
    const Line_2& sl = ray.supporting_line();
    Oriented_side or_s = sl.oriented_side(triangle.vertex(0));
    for(int i=1; i<3; ++i)
    {
      if(sl.oriented_side(triangle.vertex(i)) != or_s)
      {
        mindist = FT(0);
        break;
      }
    }
  }

  return mindist;
}

template <class K>
inline typename K::FT
squared_distance(const typename K::Triangle_2& triangle,
                 const typename K::Ray_2& ray,
                 const K& k)
{
  return internal::squared_distance(ray, triangle, k);
}

} // namespace internal

template <class K>
inline typename K::FT
squared_distance(const Ray_2<K>& ray,
                 const Triangle_2<K>& triangle)
{
  return K().compute_squared_distance_2_object()(ray, triangle);
}

template <class K>
inline typename K::FT
squared_distance(const Triangle_2<K>& triangle,
                 const Ray_2<K>& ray)
{
  return K().compute_squared_distance_2_object()(triangle, ray);
}

} // namespace CGAL

#endif // CGAL_DISTANCE_2_RAY_2_TRIANGLE_2_H