0024428: Implementation of LGPL license
[occt.git] / src / BRepExtrema / BRepExtrema_Poly.cxx
1 // Created on: 1995-09-08
2 // Created by: Christophe MARION
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and / or modify it
9 // under the terms of the GNU Lesser General Public version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <BRepExtrema_Poly.hxx>
18
19 #include <BRep_Tool.hxx>
20 #include <TopoDS_Face.hxx>
21 #include <TopoDS.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <Precision.hxx>
24 #include <Poly_Triangulation.hxx>
25 #include <TColgp_Array1OfPnt.hxx>
26
27 //=======================================================================
28 //function : Distance
29 //purpose  : 
30 //=======================================================================
31
32 Standard_Boolean BRepExtrema_Poly::Distance (const TopoDS_Shape& S1, const TopoDS_Shape& S2,
33                                              gp_Pnt& P1, gp_Pnt& P2, Standard_Real& dist)
34 {
35   dist = Precision::Infinite();
36
37   TopLoc_Location L;
38   Handle(Poly_Triangulation) Tr;
39   TopExp_Explorer exFace;
40
41   Standard_Integer nbn1 = 0;
42   for (exFace.Init(S1, TopAbs_FACE);
43        exFace.More(); 
44        exFace.Next())
45   {
46     const TopoDS_Face& F = TopoDS::Face(exFace.Current());
47     Tr = BRep_Tool::Triangulation(F,L);
48     if (!Tr.IsNull())
49       nbn1 += Tr->NbNodes();
50   }
51   if (nbn1 == 0) return Standard_False;
52
53   Standard_Integer nbn2 = 0;
54   for (exFace.Init(S2, TopAbs_FACE);
55        exFace.More(); 
56        exFace.Next())
57   {
58     const TopoDS_Face& F = TopoDS::Face(exFace.Current());
59     Tr = BRep_Tool::Triangulation(F,L);
60     if (!Tr.IsNull())
61       nbn2 += Tr->NbNodes();
62   }
63   if (nbn2 == 0) return Standard_False;
64
65   Standard_Integer i,n;
66
67   TColgp_Array1OfPnt TP1(1,nbn1);
68   nbn1 = 0;
69   
70   for (exFace.Init(S1, TopAbs_FACE);
71        exFace.More(); 
72        exFace.Next())
73   {
74     const TopoDS_Face& F = TopoDS::Face(exFace.Current());
75     Tr = BRep_Tool::Triangulation(F,L);
76     if (!Tr.IsNull())
77     {
78       const TColgp_Array1OfPnt& Nod = Tr->Nodes();
79       n = Tr->NbNodes();
80       for (i = 1; i <= n; i++)
81       {
82         nbn1++; 
83         TP1.SetValue(nbn1,Nod(i).Transformed(L));
84       }
85     }
86   }
87   
88   TColgp_Array1OfPnt TP2(1,nbn2);
89   nbn2 = 0;
90   
91   for (exFace.Init(S2, TopAbs_FACE);
92        exFace.More(); 
93        exFace.Next())
94   {
95     const TopoDS_Face& F = TopoDS::Face(exFace.Current());
96     Tr = BRep_Tool::Triangulation(F,L);
97     if (!Tr.IsNull())
98     {
99       const TColgp_Array1OfPnt& Nod = Tr->Nodes();
100       n = Tr->NbNodes();
101       for (i = 1; i <= n; i++)
102       {
103         nbn2++; 
104         TP2.SetValue(nbn2,Nod(i).Transformed(L));
105       }
106     }
107   }
108
109   Standard_Integer i1,i2;
110   for (i1 = 1; i1 <= nbn1; i1++)
111   {
112     const gp_Pnt& PP1 = TP1(i1);
113     for (i2 = 1; i2 <= nbn2; i2++)
114     {
115       const gp_Pnt& PP2 = TP2(i2);
116       const Standard_Real dCur = PP1.Distance(PP2);
117       if (dist > dCur)
118       {
119         P1 = PP1;
120         P2 = PP2;
121         dist = dCur;
122       }
123     }
124   }
125   return Standard_True;
126 }