File: Triangle_2_Triangle_2.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (123 lines) | stat: -rw-r--r-- 3,525 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
// 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/Distance_2/include/CGAL/Distance_2/Triangle_2_Triangle_2.h $
// $Id: include/CGAL/Distance_2/Triangle_2_Triangle_2.h b26b07a1242 $
// 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_TRIANGLE_2_TRIANGLE_2_H
#define CGAL_DISTANCE_2_TRIANGLE_2_TRIANGLE_2_H

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

#include <CGAL/Triangle_2.h>

namespace CGAL {
namespace internal {

template <class K>
typename K::FT
squared_distance(const typename K::Triangle_2& triangle1,
                 const typename K::Triangle_2& triangle2,
                 const K& k)
{
  typedef typename K::FT       FT;
  typedef typename K::Point_2  Point_2;

  typename K::Orientation_2 orientation = k.orientation_2_object();

  int ind1_1 = 0, ind1_2 = -1, ind2_1 = 0, ind2_2 = -1, ind1, ind2;
  FT dist;

  FT mindist = internal::squared_distance(triangle1.vertex(0), triangle2.vertex(0), k);
  for(int i=0; i<3; ++i)
  {
    const Point_2& pt = triangle1.vertex(i);
    distance_index<K>(ind1, ind2, pt, triangle2, k);
    dist = squared_distance_indexed(pt, triangle2, ind1, ind2, k);
    if(dist < mindist)
    {
      ind1_1 = i; ind1_2 = -1;
      ind2_1 = ind1; ind2_2 = ind2;
      mindist = dist;
    }
  }

  for(int i=0; i<3; ++i)
  {
    const Point_2& pt = triangle2.vertex(i);
    distance_index<K>(ind1, ind2, pt, triangle1, k);
    dist = squared_distance_indexed(pt, triangle1, ind1, ind2, k);
    if(dist < mindist)
    {
      ind1_1 = ind1; ind1_2 = ind2;
      ind2_1 = i; ind2_2 = -1;
      mindist = dist;
    }
  }

  // now check if all vertices are on the right side of the separating line.
  if(ind1_2 == -1 && ind2_2 == -1)
    return mindist;

  // In case of point-segment closest distance, there is still the
  // possibility of overlapping triangles.  Check if all the
  // vertices lie at the same side of the segment.
  if(ind1_2 != -1)
  {
    const Point_2& vt1 = triangle1.vertex(ind1_1);
    const Point_2& vt2 = triangle1.vertex(ind1_2);
    const Orientation or_s = orientation(vt1, vt2, triangle2.vertex(0));
    for(int i=1; i<3; ++i)
    {
      if(orientation(vt1, vt2, triangle2.vertex(i)) != or_s)
      {
        mindist = FT(0);
        break;
      }
    }
  }
  else
  {
    const Point_2& vt1 = triangle2.vertex(ind2_1);
    const Point_2& vt2 = triangle2.vertex(ind2_2);
    const Orientation or_s = orientation(vt1, vt2, triangle1.vertex(0));
    for(int i=1; i<3; ++i)
    {
      if(orientation(vt1, vt2, triangle1.vertex(i)) != or_s)
      {
        mindist = FT(0);
        break;
      }
    }
  }

  return mindist;
}

} // namespace internal

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

} // namespace CGAL

#endif // CGAL_DISTANCE_2_TRIANGLE_2_TRIANGLE_2_H