summaryrefslogtreecommitdiff
path: root/src/BRepExtrema/BRepExtrema_Poly.cxx
blob: 00d423c81e138b3876d68e5779c8191284e39cbd (plain)
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
// File:	BRepExtrema_Poly.cxx
// Created:	Fri Sep  8 11:03:14 1995
// Author:	Christophe MARION

#include <BRepExtrema_Poly.hxx>

#include <BRep_Tool.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS.hxx>
#include <TopExp_Explorer.hxx>
#include <Precision.hxx>
#include <Poly_Triangulation.hxx>
#include <TColgp_Array1OfPnt.hxx>

//=======================================================================
//function : Distance
//purpose  : 
//=======================================================================

Standard_Boolean BRepExtrema_Poly::Distance (const TopoDS_Shape& S1, const TopoDS_Shape& S2,
                                             gp_Pnt& P1, gp_Pnt& P2, Standard_Real& dist)
{
  dist = Precision::Infinite();

  TopLoc_Location L;
  Handle(Poly_Triangulation) Tr;
  TopExp_Explorer exFace;

  Standard_Integer nbn1 = 0;
  for (exFace.Init(S1, TopAbs_FACE);
       exFace.More(); 
       exFace.Next())
  {
    const TopoDS_Face& F = TopoDS::Face(exFace.Current());
    Tr = BRep_Tool::Triangulation(F,L);
    if (!Tr.IsNull())
      nbn1 += Tr->NbNodes();
  }
  if (nbn1 == 0) return Standard_False;

  Standard_Integer nbn2 = 0;
  for (exFace.Init(S2, TopAbs_FACE);
       exFace.More(); 
       exFace.Next())
  {
    const TopoDS_Face& F = TopoDS::Face(exFace.Current());
    Tr = BRep_Tool::Triangulation(F,L);
    if (!Tr.IsNull())
      nbn2 += Tr->NbNodes();
  }
  if (nbn2 == 0) return Standard_False;

  Standard_Integer i,n;

  TColgp_Array1OfPnt TP1(1,nbn1);
  nbn1 = 0;
  
  for (exFace.Init(S1, TopAbs_FACE);
       exFace.More(); 
       exFace.Next())
  {
    const TopoDS_Face& F = TopoDS::Face(exFace.Current());
    Tr = BRep_Tool::Triangulation(F,L);
    if (!Tr.IsNull())
    {
      const TColgp_Array1OfPnt& Nod = Tr->Nodes();
      n = Tr->NbNodes();
      for (i = 1; i <= n; i++)
      {
        nbn1++; 
        TP1.SetValue(nbn1,Nod(i).Transformed(L));
      }
    }
  }
  
  TColgp_Array1OfPnt TP2(1,nbn2);
  nbn2 = 0;
  
  for (exFace.Init(S2, TopAbs_FACE);
       exFace.More(); 
       exFace.Next())
  {
    const TopoDS_Face& F = TopoDS::Face(exFace.Current());
    Tr = BRep_Tool::Triangulation(F,L);
    if (!Tr.IsNull())
    {
      const TColgp_Array1OfPnt& Nod = Tr->Nodes();
      n = Tr->NbNodes();
      for (i = 1; i <= n; i++)
      {
        nbn2++; 
        TP2.SetValue(nbn2,Nod(i).Transformed(L));
      }
    }
  }

  Standard_Integer i1,i2;
  for (i1 = 1; i1 <= nbn1; i1++)
  {
    const gp_Pnt& PP1 = TP1(i1);
    for (i2 = 1; i2 <= nbn2; i2++)
    {
      const gp_Pnt& PP2 = TP2(i2);
      const Standard_Real dCur = PP1.Distance(PP2);
      if (dist > dCur)
      {
        P1 = PP1;
        P2 = PP2;
        dist = dCur;
      }
    }
  }
  return Standard_True;
}