3563ed7d41851e2c47f4efaebbca35b8b57d88e4
[occt.git] / src / StepToTopoDS / StepToTopoDS_GeometricTool.cxx
1 // Created on: 1995-01-06
2 // Created by: Frederic MAUPAS
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 under
9 // the terms of the GNU Lesser General Public License 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 //pdn 11.01.99 #144 bm1_pe_t4 protection of exceptions in draw
18 //    abv 13.04.99 S4136: eliminate BRepAPI::Precision()
19
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <BRepTools_WireExplorer.hxx>
24 #include <ElCLib.hxx>
25 #include <ElSLib.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom2d_Line.hxx>
28 #include <Geom2d_TrimmedCurve.hxx>
29 #include <Geom2dAPI_InterCurveCurve.hxx>
30 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
31 #include <Geom_BoundedCurve.hxx>
32 #include <Geom_BSplineCurve.hxx>
33 #include <Geom_BSplineSurface.hxx>
34 #include <Geom_ConicalSurface.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_CylindricalSurface.hxx>
37 #include <Geom_Line.hxx>
38 #include <Geom_Plane.hxx>
39 #include <Geom_SphericalSurface.hxx>
40 #include <Geom_ToroidalSurface.hxx>
41 #include <GeomAbs_CurveType.hxx>
42 #include <GeomAbs_Shape.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 #include <GeomAPI_ProjectPointOnSurf.hxx>
46 #include <gp_Vec2d.hxx>
47 #include <Precision.hxx>
48 #include <Standard_ErrorHandler.hxx>
49 #include <Standard_Failure.hxx>
50 #include <StepGeom_CartesianPoint.hxx>
51 #include <StepGeom_Direction.hxx>
52 #include <StepGeom_Line.hxx>
53 #include <StepGeom_Pcurve.hxx>
54 #include <StepGeom_PcurveOrSurface.hxx>
55 #include <StepGeom_SeamCurve.hxx>
56 #include <StepGeom_Surface.hxx>
57 #include <StepGeom_SurfaceCurve.hxx>
58 #include <StepGeom_Vector.hxx>
59 #include <StepRepr_DefinitionalRepresentation.hxx>
60 #include <StepShape_Edge.hxx>
61 #include <StepShape_EdgeLoop.hxx>
62 #include <StepShape_OrientedEdge.hxx>
63 #include <StepToTopoDS.hxx>
64 #include <StepToTopoDS_GeometricTool.hxx>
65 #include <TopExp.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopoDS.hxx>
68 #include <Transfer_TransientProcess.hxx>
69
70 //#include <StepToTopoDS_ExtPCOnS.hxx>
71 //#include <BRepAPI.hxx>
72 // ----------------------------------------------------------------------------
73 // Method  : HasPCurve
74 // Purpose : returns true if the surface curve has at least one pcurve lying
75 //on the surface
76 // ----------------------------------------------------------------------------
77 Standard_Integer StepToTopoDS_GeometricTool::PCurve
78   (const Handle(StepGeom_SurfaceCurve)& SurfCurve,
79    const Handle(StepGeom_Surface)&      BasisSurf,
80    Handle(StepGeom_Pcurve)& thePCurve,  const Standard_Integer last)
81 {
82   Standard_Integer NbAssGeom = SurfCurve->NbAssociatedGeometry();
83   thePCurve.Nullify();
84   for (Standard_Integer i= last+1; i<=NbAssGeom; i++) {
85     thePCurve = SurfCurve->AssociatedGeometryValue(i).Pcurve();
86     if (!thePCurve.IsNull()) {
87       if (thePCurve->BasisSurface() == BasisSurf) return i;
88     }
89   }
90   thePCurve.Nullify();
91   return 0;
92 }
93
94 // ----------------------------------------------------------------------------
95 // Method  : IsSeamCurve
96 // Purpose : Two edges of the same wire references the same oriented edge
97 //           Then the surface_curve is a seam curve
98 // ----------------------------------------------------------------------------
99
100 Standard_Boolean  StepToTopoDS_GeometricTool::IsSeamCurve
101 (const Handle(StepGeom_SurfaceCurve)& SurfCurve,
102  const Handle(StepGeom_Surface)&      Surf,
103  const Handle(StepShape_Edge)&         StepEdge,
104  const Handle(StepShape_EdgeLoop)&     EdgeLoop)
105 {
106   if (SurfCurve->IsKind(STANDARD_TYPE(StepGeom_SeamCurve)))
107     return Standard_True;
108   
109   if (SurfCurve->NbAssociatedGeometry() != 2) return Standard_False;
110
111   Handle(StepGeom_Pcurve) StepPCurve1 =
112     SurfCurve->AssociatedGeometryValue(1).Pcurve();
113   Handle(StepGeom_Pcurve) StepPCurve2 = 
114     SurfCurve->AssociatedGeometryValue(2).Pcurve();
115
116   // Do the two pcurves lye on the same surface ?
117
118   if ((!StepPCurve1.IsNull() && !StepPCurve2.IsNull()) && 
119       (StepPCurve1->BasisSurface() == Surf) &&
120       (StepPCurve2->BasisSurface()== Surf)) {
121
122     Standard_Integer NbEdge = EdgeLoop->NbEdgeList();
123     Standard_Integer nbOE = 0;
124
125     Handle(StepShape_OrientedEdge) OrEdge;
126
127     for (Standard_Integer i = 1; i <= NbEdge; i ++ ) {
128       OrEdge = EdgeLoop->EdgeListValue(i);
129       if (StepEdge == OrEdge->EdgeElement()) nbOE ++;
130     }
131     // two oriented edges of the same wire share the same edge
132     if (nbOE == 2) return Standard_True;
133   }
134   return Standard_False;
135 }
136
137 // ----------------------------------------------------------------------------
138 // Method  : IsLikeSeam
139 // Purpose : The two pcurves lies on the same surface but on different wires.
140 //           This is typical situation in CATIA BRep : a cylinder is coded
141 //           with two faces on the same 'Closed' BSplineSurf, which in the
142 //           range of gp_Resolution is not identified as closed
143 // ----------------------------------------------------------------------------
144
145 Standard_Boolean  StepToTopoDS_GeometricTool::IsLikeSeam
146 (const Handle(StepGeom_SurfaceCurve)& SurfCurve,
147  const Handle(StepGeom_Surface)&      Surf,
148  const Handle(StepShape_Edge)&         StepEdge,
149  const Handle(StepShape_EdgeLoop)&     EdgeLoop)
150 {
151   if (SurfCurve->NbAssociatedGeometry() != 2) return Standard_False;
152
153   Handle(StepGeom_Pcurve) StepPCurve1 = 
154     SurfCurve->AssociatedGeometryValue(1).Pcurve();
155   Handle(StepGeom_Pcurve) StepPCurve2 = 
156     SurfCurve->AssociatedGeometryValue(2).Pcurve();
157   
158   // Do the two pcurves lye on the same surface ?
159   
160   if ((!StepPCurve1.IsNull() && !StepPCurve2.IsNull()) && 
161       (StepPCurve1->BasisSurface() == Surf) &&
162       (StepPCurve2->BasisSurface() == Surf)) {
163     
164     Standard_Integer NbEdge = EdgeLoop->NbEdgeList();
165     Standard_Integer nbOE = 0;
166
167     Handle(StepShape_OrientedEdge) OrEdge;
168
169     for (Standard_Integer i = 1; i <= NbEdge; i ++ ) {
170       OrEdge = EdgeLoop->EdgeListValue(i);
171       if (StepEdge == OrEdge->EdgeElement()) nbOE ++;
172     }
173     // the two oriented edges are not in the same wire
174     if (nbOE == 1) { 
175       // check is the two pcurve are not indentical ?
176       Handle(StepGeom_Line) line1 = Handle(StepGeom_Line)::DownCast
177         (StepPCurve1->ReferenceToCurve()->ItemsValue(1));
178       Handle(StepGeom_Line) line2 = Handle(StepGeom_Line)::DownCast
179         (StepPCurve2->ReferenceToCurve()->ItemsValue(1));
180       if (!line1.IsNull() && !line2.IsNull()) {
181         // Same Origin in X OR Y && Same Vector ??
182         // WITHIN A given tolerance !!!
183         Standard_Real DeltaX = Abs(line1->Pnt()->CoordinatesValue(1) -
184                                    line2->Pnt()->CoordinatesValue(1));
185         Standard_Real DeltaY = Abs(line1->Pnt()->CoordinatesValue(2) -
186                                    line2->Pnt()->CoordinatesValue(2));
187
188         Standard_Real DeltaDirX = 
189           Abs(line1->Dir()->Orientation()->DirectionRatiosValue(1) -
190               line2->Dir()->Orientation()->DirectionRatiosValue(1));
191         Standard_Real DeltaDirY = 
192           Abs(line1->Dir()->Orientation()->DirectionRatiosValue(2) -
193               line2->Dir()->Orientation()->DirectionRatiosValue(2));
194
195         Standard_Real preci2d = Precision::PConfusion(); //:S4136: Parametric(BRepAPI::Precision(),10);
196         
197         if ((DeltaX < preci2d) || (DeltaY < preci2d))
198           return ((DeltaDirX < preci2d) && (DeltaDirY < preci2d));
199         else return Standard_False;
200
201         // Warning : la manipulation de tolerances dans ce contexte est un
202         //           peu trop dangeureux.
203         //           il serait preferable de plus de ne pas restreindre au
204         //           cas de deux lignes.
205         //           un mode plus convenable de detection serait de se servir
206         //           des isos (ou bords naturels) de la surface de base
207         //           et de detecter que les deux courbes se trouvent sur le
208         //           bord de fermeture.
209         //           il faut toutefois prevoir le cas ou les deux courbes
210         //           sont confondues (ex : CATIA, "couture" de separation
211         //           en deux faces d un support periodique.
212         //  Ce travail reste evidement A FAIRE !!! ...
213       }
214       else return Standard_False;
215     }
216     return Standard_False;
217   }
218   return Standard_False;
219 }
220
221 // ----------------------------------------------------------------------------
222 // Method  : UpdateParam3d
223 // Purpose : According to the type of curve update parameter (w1>w2)
224 //           This situation occurs when an edge crosses the parametric origin.
225 // ----------------------------------------------------------------------------
226
227 Standard_Boolean  StepToTopoDS_GeometricTool::UpdateParam3d
228 (const Handle(Geom_Curve)& theCurve, Standard_Real& w1, Standard_Real& w2,
229  const Standard_Real preci)
230 {
231   // w1 et/ou w2 peuvent etre en dehors des bornes naturelles de la courbe.
232   // On donnera alors la valeur en bout a w1 et/ou w2
233   
234   Standard_Real cf = theCurve->FirstParameter();
235   Standard_Real cl = theCurve->LastParameter();
236 //  Standard_Real preci = BRepAPI::Precision();
237
238   if (theCurve->IsKind(STANDARD_TYPE(Geom_BoundedCurve)) && !theCurve->IsClosed()) {
239     if (w1 < cf) {
240 #ifdef OCCT_DEBUG
241       cout << "Update Edge First Parameter to Curve First Parameter" << endl;
242 #endif
243       w1 = cf;
244     }
245     else if (w1 > cl) {
246 #ifdef OCCT_DEBUG
247       cout << "Update Edge First Parameter to Curve Last Parameter" << endl;
248 #endif
249       w1 = cl;
250     }
251     if (w2 < cf) {
252 #ifdef OCCT_DEBUG
253       cout << "Update Edge Last Parameter to Curve First Parameter" << endl;
254 #endif
255       w2 = cf;
256     }
257     else if (w2 > cl) {
258 #ifdef OCCT_DEBUG
259       cout << "Update Edge Last Parameter to Curve Last Parameter" << endl;
260 #endif
261       w2 = cl;
262     }
263   }
264
265   if (w1 < w2) return Standard_True;
266
267   if (theCurve->IsPeriodic()) 
268     ElCLib::AdjustPeriodic(cf,cl,Precision::PConfusion(),w1,w2); //:a7 abv 11 Feb 98: preci -> PConfusion()
269   else if (theCurve->IsClosed()) {
270     // l'un des points projecte se trouve sur l'origine du parametrage
271     // de la courbe 3D. L algo a donne cl +- preci au lieu de cf ou vice-versa
272     // DANGER precision 3d applique a une espace 1d
273     
274     // w2 = cf au lieu de w2 = cl
275     if      (Abs(w2 - cf) < Precision::PConfusion() /*preci*/)  w2 = cl ;
276     // w1 = cl au lieu de w1 = cf
277     else if (Abs(w1 - cl) < Precision::PConfusion() /*preci*/)  w1 = cf;
278
279     // on se trouve dans un cas ou l origine est traversee
280     // illegal sur une courbe fermee non periodique
281     // on inverse quand meme les parametres !!!!!!
282     else {
283       //:S4136 abv 20 Apr 99: r0701_ug.stp #6230: add check in 3d
284       if ( theCurve->Value(w1).Distance(theCurve->Value(cf)) < preci ) w1 = cf;
285       if ( theCurve->Value(w2).Distance(theCurve->Value(cl)) < preci ) w2 = cl;
286       if ( w1 > w2 ) {
287 #ifdef OCCT_DEBUG
288         cout << "Warning : parameter range of edge crossing non periodic curve origin" << endl;
289 #endif
290         Standard_Real tmp = w1;
291         w1 = w2;
292         w2 = tmp;
293       }
294     }
295   }
296   // The curve is closed within the 3D tolerance
297   else if (theCurve->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
298     Handle(Geom_BSplineCurve) aBSpline = 
299       Handle(Geom_BSplineCurve)::DownCast(theCurve);
300     if (aBSpline->StartPoint().Distance(aBSpline->EndPoint()) <= preci ) {
301 //:S4136        <= BRepAPI::Precision()) {
302       // l'un des points projecte se trouve sur l'origine du parametrage
303       // de la courbe 3D. L algo a donne cl +- preci au lieu de cf ou vice-versa
304       // DANGER precision 3d applique a une espace 1d
305       
306       // w2 = cf au lieu de w2 = cl
307       if      (Abs(w2 - cf) < Precision::PConfusion() /*preci*/) w2 = cl ;
308       // w1 = cl au lieu de w1 = cf
309       else if (Abs(w1 - cl) < Precision::PConfusion() /*preci*/) w1 =  cf;
310
311       // on se trouve dans un cas ou l origine est traversee
312       // illegal sur une courbe fermee non periodique
313       // on inverse quand meme les parametres !!!!!!
314       else {
315 #ifdef OCCT_DEBUG
316         cout << "Warning : parameter range of edge crossing non periodic curve origin" << endl;
317 #endif
318         Standard_Real tmp = w1;
319         w1 = w2;
320         w2 = tmp;
321       }
322     }
323     //abv 15.03.00 #72 bm1_pe_t4 protection of exceptions in draw
324     else if ( w1 > w2 ) {
325 #ifdef OCCT_DEBUG
326       cout << "Warning: parameter range is bad; curve reversed" << endl;
327 #endif
328       w1 = theCurve->ReversedParameter ( w1 );
329       w2 = theCurve->ReversedParameter ( w2 );
330       theCurve->Reverse();
331     }
332 //:j9 abv 11 Dec 98: PRO7747 #4875, after :j8:    else 
333     if (w1 == w2) {  //gka 10.07.1998 file PRO7656 entity 33334
334       w1 = cf; w2 = cl;
335       return Standard_False;
336     }
337   }
338   else {
339 #ifdef OCCT_DEBUG
340     cout << "UpdateParam3d Failed" << endl;
341     cout << "  - Curve Type : " << theCurve->DynamicType() << endl;
342     cout << "  - Param 1    : " << w1 << endl;
343     cout << "  - Param 2    : " << w2 << endl;
344 #endif
345     //abv 15.03.00 #72 bm1_pe_t4 protection of exceptions in draw
346     if ( w1 > w2 ) {
347 #ifdef OCCT_DEBUG
348       cout << "Warning: parameter range is bad; curve reversed" << endl;
349 #endif
350       w1 = theCurve->ReversedParameter ( w1 );
351       w2 = theCurve->ReversedParameter ( w2 );
352       theCurve->Reverse();
353     }
354     //pdn 11.01.99 #144 bm1_pe_t4 protection of exceptions in draw
355     if (w1 == w2) {
356       w1 -= Precision::PConfusion();
357       w2 += Precision::PConfusion();
358     }
359     return Standard_False;
360   }
361   return Standard_True;
362 }