0024428: Implementation of LGPL license
[occt.git] / src / LocOpe / LocOpe.cxx
1 // Created on: 1996-04-02
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1996-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 <LocOpe.ixx>
18
19 #include <TopExp_Explorer.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRep_Builder.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <gp_Pnt2d.hxx>
24 #include <BRepAdaptor_Curve2d.hxx>
25 #include <BRepAdaptor_HSurface.hxx>
26 #include <BRepAdaptor_HCurve2d.hxx>
27 #include <Geom2dInt_GInter.hxx>
28 #include <Geom_Curve.hxx>
29
30 #include <TopTools_MapOfShape.hxx>
31
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <Precision.hxx>
36
37 #define NECHANT 10
38
39
40 //=======================================================================
41 //function : Closed
42 //purpose  : 
43 //=======================================================================
44
45 Standard_Boolean LocOpe::Closed(const TopoDS_Wire& W,
46                                 const TopoDS_Face& F)
47 {
48   TopoDS_Vertex Vf,Vl;
49   TopExp::Vertices(W,Vf,Vl);
50   if (!Vf.IsSame(Vl)) {
51     return Standard_False;
52   }
53
54   // On recherche l`edge contenant Vf FORWARD
55
56   TopExp_Explorer exp,exp2;
57   for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
58        exp.More();exp.Next()) {
59     for (exp2.Init(exp.Current(),TopAbs_VERTEX);exp2.More();exp2.Next()) {
60       if (exp2.Current().IsSame(Vf) && 
61           exp2.Current().Orientation() == TopAbs_FORWARD) {
62         break;
63       }
64     }
65     if (exp2.More()) {
66       break;
67     }
68   }
69   TopoDS_Edge Ef = TopoDS::Edge(exp.Current());
70   
71   // On recherche l`edge contenant Vl REVERSED
72
73   for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
74        exp.More();exp.Next()) {
75     for (exp2.Init(exp.Current(),TopAbs_VERTEX);exp2.More();exp2.Next()) {
76       if (exp2.Current().IsSame(Vl) && 
77           exp2.Current().Orientation() == TopAbs_REVERSED) {
78         break;
79       }
80     }
81     if (exp2.More()) {
82       break;
83     }
84   }
85   TopoDS_Edge El = TopoDS::Edge(exp.Current());
86   
87   Standard_Real f,l;
88   gp_Pnt2d pf,pl;
89   Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(Ef,F,f,l);
90   if (Ef.Orientation() == TopAbs_FORWARD) {
91     pf = C2d->Value(f);
92   }
93   else {
94     pf = C2d->Value(l);
95   }
96   C2d = BRep_Tool::CurveOnSurface(El,F,f,l);
97   if (El.Orientation() == TopAbs_FORWARD) {
98     pl = C2d->Value(l);
99   }
100   else {
101     pl = C2d->Value(f);
102   }
103
104   if (pf.Distance(pl) <= Precision::PConfusion(Precision::Confusion())) {
105     return Standard_True;
106   }
107   return Standard_False;
108 }
109
110
111 //=======================================================================
112 //function : Closed
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean LocOpe::Closed(const TopoDS_Edge& E,
117                                 const TopoDS_Face& F)
118 {
119   BRep_Builder B;
120   TopoDS_Wire W;
121   B.MakeWire(W);
122   B.Add(W,E.Oriented(TopAbs_FORWARD));
123   return LocOpe::Closed(W,F);
124 }
125
126
127
128 //=======================================================================
129 //function : Closed
130 //purpose  : 
131 //=======================================================================
132
133 Standard_Boolean LocOpe::TgtFaces(const TopoDS_Edge& E,
134                                   const TopoDS_Face& F1,
135                                   const TopoDS_Face& F2)
136 {
137   BRepAdaptor_Surface bs(F1, Standard_False);
138   Standard_Real u;
139   Standard_Real ta = 0.0001;
140
141   TopoDS_Edge e = E;
142
143   Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(F1);
144   Handle(BRepAdaptor_HSurface) HS2 = new BRepAdaptor_HSurface(F2);
145   e.Orientation(TopAbs_FORWARD);
146   Handle(BRepAdaptor_HCurve2d) HC2d = new BRepAdaptor_HCurve2d();
147   Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
148   HC2d->ChangeCurve2d().Initialize(e,F1);
149   HC2d2->ChangeCurve2d().Initialize(e,F2);
150   
151
152 //  Adaptor3d_CurveOnSurface C1(HC2d,HS1);
153
154   Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
155   Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
156   Standard_Real f,l,eps,angmin = M_PI,angmax = -M_PI,ang;
157   BRep_Tool::Range(e,f,l);
158
159   eps = (l - f)/100.;
160   f += eps; // pour eviter de faire des calculs sur les 
161   l -= eps; // pointes des carreaux pointus.
162   gp_Pnt2d p;
163   gp_Pnt pp1;
164   gp_Vec du,dv;
165   gp_Vec d1,d2;
166
167   Standard_Real uu, vv;
168   Standard_Real angle[21];
169
170   Standard_Integer i;
171   for(i = 0; i <= 20; i++){
172     u = f + (l-f)*i/20;
173     HC2d->D0(u,p);
174     HS1->D1(p.X(),p.Y(),pp1,du,dv);
175     d1 = (du.Crossed(dv)).Normalized();
176     if(rev1) d1.Reverse();
177     HC2d2->D0(u,p);
178     p.Coord(uu,vv);
179     HS2->D1(uu,vv,pp1,du,dv);
180     d2 = (du.Crossed(dv)).Normalized();
181     if(rev2) d2.Reverse();
182     ang = d1.Angle(d2);
183     angle[i] = ang;
184     if(ang <= angmin) angmin = ang;
185     if(ang >= angmax) angmax = ang;
186   }
187   return (angmax<=ta);
188 }
189
190
191 //=======================================================================
192 //function : SampleEdges
193 //purpose  : 
194 //=======================================================================
195
196 void LocOpe::SampleEdges(const TopoDS_Shape& theShape,
197                          TColgp_SequenceOfPnt& theSeq)
198 {
199   theSeq.Clear();
200   TopTools_MapOfShape theMap;
201   
202   TopExp_Explorer exp(theShape,TopAbs_EDGE);
203   TopLoc_Location Loc;
204   Handle(Geom_Curve) C;
205   Standard_Real f,l,prm;
206   Standard_Integer i;
207
208   // Computes points on edge, but does not take the extremities into account
209   for (; exp.More(); exp.Next()) {
210     const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
211     if (!theMap.Add(edg)) {
212       continue;
213     }
214     if (!BRep_Tool::Degenerated(edg)) {
215       C = BRep_Tool::Curve(edg,Loc,f,l);
216       C = Handle(Geom_Curve)::
217         DownCast(C->Transformed(Loc.Transformation()));
218       Standard_Real delta = (l - f)/NECHANT*0.123456;
219       for (i=1; i<NECHANT; i++) {
220         prm = delta + ((NECHANT-i)*f+i*l)/NECHANT;
221         theSeq.Append(C->Value(prm));
222       }
223     }
224   }
225
226   // Adds every vertex
227   for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
228     if (theMap.Add(exp.Current())) {
229       theSeq.Append(BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
230     }
231   }
232 }
233
234
235
236
237
238 /*
239 Standard_Boolean LocOpe::IsInside(const TopoDS_Face& F1,
240                                   const TopoDS_Face& F2)
241 {
242   Standard_Boolean Result = Standard_True;
243
244   TopExp_Explorer exp1, exp2;
245
246   for(exp1.Init(F1, TopAbs_EDGE); exp1.More(); exp1.Next())  {
247     TopoDS_Edge e1 = TopoDS::Edge(exp1.Current());
248     BRepAdaptor_Curve2d C1(e1, F1); 
249     for(exp2.Init(F2, TopAbs_EDGE); exp2.More(); exp2.Next())  {
250       TopoDS_Edge e2 = TopoDS::Edge(exp2.Current());
251       BRepAdaptor_Curve2d C2(e2, F2); 
252       Geom2dInt_GInter C;
253       C.Perform(C1, C2, Precision::Confusion(), Precision::Confusion());
254       if(!C.IsEmpty()) Result = Standard_False;
255       if(Result == Standard_False) {
256         for(exp3.Init(e2, TopAbs_VERTEX); exp3.More(); exp3.Next())  {
257           
258         }
259       }
260     }
261     if(Result == Standard_False) break;
262   }
263   return Result;
264 }
265
266 */
267
268
269