0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / BRepExtrema / BRepExtrema_Poly.cxx
CommitLineData
b311480e 1// Created on: 1995-09-08
2// Created by: Christophe MARION
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
92d1589b
A
17#include <BRepExtrema_Poly.hxx>
18
7fd59977 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
92d1589b
A
32Standard_Boolean BRepExtrema_Poly::Distance (const TopoDS_Shape& S1, const TopoDS_Shape& S2,
33 gp_Pnt& P1, gp_Pnt& P2, Standard_Real& dist)
7fd59977 34{
7fd59977 35 dist = Precision::Infinite();
92d1589b 36
7fd59977 37 TopLoc_Location L;
92d1589b
A
38 Handle(Poly_Triangulation) Tr;
39 TopExp_Explorer exFace;
7fd59977 40
92d1589b 41 Standard_Integer nbn1 = 0;
7fd59977 42 for (exFace.Init(S1, TopAbs_FACE);
43 exFace.More();
92d1589b
A
44 exFace.Next())
45 {
7fd59977 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 }
92d1589b
A
51 if (nbn1 == 0) return Standard_False;
52
7fd59977 53 Standard_Integer nbn2 = 0;
7fd59977 54 for (exFace.Init(S2, TopAbs_FACE);
55 exFace.More();
92d1589b
A
56 exFace.Next())
57 {
7fd59977 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 }
92d1589b 63 if (nbn2 == 0) return Standard_False;
7fd59977 64
92d1589b 65 Standard_Integer i,n;
7fd59977 66
67 TColgp_Array1OfPnt TP1(1,nbn1);
68 nbn1 = 0;
69
70 for (exFace.Init(S1, TopAbs_FACE);
71 exFace.More();
92d1589b
A
72 exFace.Next())
73 {
7fd59977 74 const TopoDS_Face& F = TopoDS::Face(exFace.Current());
75 Tr = BRep_Tool::Triangulation(F,L);
92d1589b
A
76 if (!Tr.IsNull())
77 {
7fd59977 78 const TColgp_Array1OfPnt& Nod = Tr->Nodes();
79 n = Tr->NbNodes();
92d1589b
A
80 for (i = 1; i <= n; i++)
81 {
82 nbn1++;
83 TP1.SetValue(nbn1,Nod(i).Transformed(L));
7fd59977 84 }
85 }
86 }
87
88 TColgp_Array1OfPnt TP2(1,nbn2);
89 nbn2 = 0;
90
91 for (exFace.Init(S2, TopAbs_FACE);
92 exFace.More();
92d1589b
A
93 exFace.Next())
94 {
7fd59977 95 const TopoDS_Face& F = TopoDS::Face(exFace.Current());
96 Tr = BRep_Tool::Triangulation(F,L);
92d1589b
A
97 if (!Tr.IsNull())
98 {
7fd59977 99 const TColgp_Array1OfPnt& Nod = Tr->Nodes();
100 n = Tr->NbNodes();
92d1589b
A
101 for (i = 1; i <= n; i++)
102 {
103 nbn2++;
104 TP2.SetValue(nbn2,Nod(i).Transformed(L));
7fd59977 105 }
106 }
107 }
92d1589b
A
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;
7fd59977 122 }
123 }
124 }
125 return Standard_True;
126}