0024058: Eliminate compiler warning C4702 in MSVC++ with warning level 4
[occt.git] / src / ShapeFix / ShapeFix_EdgeProjAux.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 //:r5 abv 06.04.99: ec_turbine-A.stp, #4313: protect against null curve
19 //    abv 09.04.99  S4136: add parameter preci (to eliminate BRepAPI::Precision)
20 #include <ShapeFix_EdgeProjAux.ixx>
21
22 #include <Standard_ErrorHandler.hxx>
23 #include <Standard_Failure.hxx>
24
25 #include <ElCLib.hxx>
26
27 #include <Adaptor3d_CurveOnSurface.hxx>
28 #include <Geom2dAdaptor_Curve.hxx>
29 #include <Geom2dAdaptor_HCurve.hxx>
30 #include <GeomAdaptor_Surface.hxx>
31 #include <GeomAdaptor_HSurface.hxx>
32
33 #include <Geom2d_Curve.hxx>
34 #include <Geom2d_BSplineCurve.hxx>
35 #include <Geom2d_Line.hxx>
36 #include <Geom_Curve.hxx>
37 #include <Geom_Surface.hxx> 
38 #include <Geom_SphericalSurface.hxx>
39
40 #include <BRep_Tool.hxx>
41 #include <Precision.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopExp.hxx>
44
45 #include <ShapeAnalysis.hxx>
46 #include <ShapeAnalysis_Curve.hxx>
47 #include <ShapeAnalysis_Edge.hxx>
48 #include <ShapeAnalysis_Surface.hxx>
49
50 #include <Extrema_ExtPC.hxx>
51 #include <gp_Pnt.hxx>
52
53 //=======================================================================
54 //function : ShapeFix_EdgeProjAux
55 //purpose  : 
56 //=======================================================================
57
58 ShapeFix_EdgeProjAux::ShapeFix_EdgeProjAux ()
59 {
60   myFirstDone = myLastDone = Standard_False;
61 }
62
63 //=======================================================================
64 //function : ShapeFix_EdgeProjAux
65 //purpose  : 
66 //=======================================================================
67
68 ShapeFix_EdgeProjAux::ShapeFix_EdgeProjAux (const TopoDS_Face& F,
69                                             const TopoDS_Edge& E)
70 {
71   Init (F, E);
72 }
73
74 //=======================================================================
75 //function : Init
76 //purpose  : 
77 //=======================================================================
78
79 void ShapeFix_EdgeProjAux::Init (const TopoDS_Face& F,
80                                  const TopoDS_Edge& E)
81 {
82   myFace = F;
83   myEdge = E;
84   myFirstDone = myLastDone = Standard_False;
85 }
86
87 //=======================================================================
88 //function : Compute
89 //purpose  : 
90 //=======================================================================
91
92 void ShapeFix_EdgeProjAux::Compute (const Standard_Real preci) 
93 {
94   myFirstDone = myLastDone = Standard_False;
95     
96   // Project Point3d on Surface
97   // TEMPORARY Call ShapeFix_EdgeProjAux
98
99   Init2d(preci);
100   if (IsFirstDone() && IsLastDone()) {
101     Standard_Real U1 = FirstParam();
102     Standard_Real U2 = LastParam();
103     if (U1>=U2) {
104 #ifdef DEBUG
105       cout << "Parametres inverses ... " << endl;
106 #endif
107       Standard_Real tmp = U1;
108       U1 = U2; U2 = tmp;
109     }
110     myFirstParam = U1;
111     myFirstDone  = Standard_True;
112     myLastParam  = U2;
113     myLastDone   = Standard_True;
114   }
115 }
116
117 //=======================================================================
118 //function : IsFirstDone
119 //purpose  : 
120 //=======================================================================
121
122 Standard_Boolean ShapeFix_EdgeProjAux::IsFirstDone() const
123 {
124   return myFirstDone;
125 }
126
127 //=======================================================================
128 //function : IsLastDone
129 //purpose  : 
130 //=======================================================================
131
132 Standard_Boolean ShapeFix_EdgeProjAux::IsLastDone() const
133 {
134   return myLastDone;
135 }
136
137 //=======================================================================
138 //function : FirstParam
139 //purpose  : 
140 //=======================================================================
141
142 Standard_Real ShapeFix_EdgeProjAux::FirstParam() const
143 {
144   return myFirstParam;
145 }
146
147 //=======================================================================
148 //function : LastParam
149 //purpose  : 
150 //=======================================================================
151
152 Standard_Real ShapeFix_EdgeProjAux::LastParam() const
153 {
154   return myLastParam;
155 }
156
157 //=======================================================================
158 //function : IsIso
159 //purpose  : 
160 //=======================================================================
161
162 Standard_Boolean ShapeFix_EdgeProjAux::IsIso (const Handle(Geom2d_Curve)& /*theCurve2d*/)
163 {
164   // Until an ISO is recognized by Adaptor3d_Curve
165 /*  
166   if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
167     Handle(Geom2d_Line) theLine2d = Handle(Geom2d_Line)::DownCast(theCurve2d);
168     gp_Dir2d theDir2d = theLine2d->Direction();
169     
170     gp_Dir2d dir1(0.,1.);
171     gp_Dir2d dir2(0.,-1.);
172     
173     return (theDir2d.IsEqual(dir1,Precision::Angular()) ||
174             theDir2d.IsEqual(dir2,Precision::Angular()) ||
175             theDir2d.IsNormal(dir1,Precision::Angular()) ||
176             theDir2d.IsNormal(dir2,Precision::Angular()) );
177   }
178 */
179   return Standard_False;
180 }
181
182 // ----------------------------------------------------------------------------
183 // static  : FindParameterWithExt
184 // Purpose : Computes the trimming parameter of Pt1 on COnS
185 // ----------------------------------------------------------------------------
186
187 static Standard_Boolean FindParameterWithExt (const gp_Pnt& Pt1, 
188                                               const Adaptor3d_CurveOnSurface& COnS,
189                                               const Standard_Real Uinf,
190                                               const Standard_Real Usup, 
191                                               const Standard_Real preci,
192                                               Standard_Real& w1)
193 {
194   try {  // et allez donc !
195     OCC_CATCH_SIGNALS
196     Extrema_ExtPC myExtPC (Pt1, COnS, Uinf, Usup, preci);
197
198     //ShapeFix_ExtPCOnS myExtPCOnS1 = 
199     //ShapeFix_ExtPCOnS(Pt1, COnS, Uinf, Usup, preci);
200   
201     if (myExtPC.IsDone()) {
202       Standard_Integer NbExt1 = myExtPC.NbExt();
203       for (Standard_Integer i=1; i<=NbExt1; i++) {
204         if (myExtPC.IsMin(i)) {
205           //Standard_Real dist = myExtPC.Value(i); //szv#4:S4163:12Mar99 debug mode only
206           w1   = myExtPC.Point(i).Parameter();
207         }
208       }
209       return Standard_True;
210     }
211     else return Standard_False;
212   }  // end try
213   catch(Standard_Failure) {
214 #ifdef DEB //:s5
215     cout << "Warning: ShapeFix_EdgeProjAux, FindParameterWithExt(): Exception: ";
216     Standard_Failure::Caught()->Print(cout); cout << endl;
217 #endif
218     return Standard_False;
219   }
220 }
221
222 //=======================================================================
223 //function : Init2d
224 //purpose  : 
225 //=======================================================================
226
227 void ShapeFix_EdgeProjAux::Init2d (const Standard_Real preci) 
228 {
229   Standard_Real cl, cf;
230
231   // Extract Geometries
232   Handle(Geom_Surface) theSurface = BRep_Tool::Surface(myFace);
233   Handle(Geom2d_Curve) theCurve2d = BRep_Tool::CurveOnSurface(myEdge, myFace, cf, cl);
234   if ( theCurve2d.IsNull() ) return; //:r5 abv 6 Apr 99:  ec_turbine-A.stp, #4313
235
236   TopoDS_Vertex V1,V2;
237   TopExp::Vertices(myEdge, V1, V2);
238   gp_Pnt Pt1,Pt2;
239   // pdn 28.12.98: r_39-db.stp #605: use ends of 3d curve instead of vertices 
240   ShapeAnalysis_Edge sae;
241   Standard_Real a,b;
242   Handle(Geom_Curve) C3d;
243   if(sae.Curve3d(myEdge,C3d,a,b,Standard_False)) {
244     Pt1 = C3d->Value(a);
245     Pt2 = C3d->Value(b);
246   } 
247   else {
248     Pt1 = BRep_Tool::Pnt(V1);
249     Pt2 = BRep_Tool::Pnt(V2);
250   }
251 //:S4136  Standard_Real preci = BRepAPI::Precision();
252   //pdn to manage degenerated case
253   if (V1.IsSame(V2)) {
254     Handle(ShapeAnalysis_Surface) stsu = new ShapeAnalysis_Surface (theSurface);
255     gp_Pnt2d aPt1,aPt2;
256     Standard_Real firstpar,lastpar;
257     if (stsu->DegeneratedValues(Pt1,preci,aPt1,aPt2,firstpar,lastpar)){
258       
259       if(theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
260         if (aPt1.IsEqual(theCurve2d->Value(firstpar),preci) && 
261             aPt2.IsEqual(theCurve2d->Value(lastpar),preci)){
262           myFirstParam = firstpar;
263           myLastParam  = lastpar;
264           myFirstDone = myLastDone = Standard_True;
265           return;
266         }
267       } 
268 #ifdef DEBUG
269       else cout <<"Other type of deg curve"<<endl;
270 #endif
271       
272     }
273   }
274   
275   Standard_Boolean parU = Standard_False, parV = Standard_False;
276   GeomAdaptor_Surface          SA     = GeomAdaptor_Surface(theSurface);
277   Handle(GeomAdaptor_HSurface) myHSur = new GeomAdaptor_HSurface(SA);
278   
279   cf = theCurve2d->FirstParameter();
280   cl = theCurve2d->LastParameter();
281   //pdn cutting pcurve by suface bounds
282   if (Precision::IsInfinite(cf)||Precision::IsInfinite(cl)) {
283     if(theCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line))) {
284       Standard_Real uf,ul,vf,vl;
285       theSurface->Bounds(uf,ul,vf,vl);
286       if(!Precision::IsInfinite(uf)&&!Precision::IsInfinite(ul)&&
287          !Precision::IsInfinite(vf)&&!Precision::IsInfinite(vl)) {
288         Standard_Real cfi,cli;
289         Handle(Geom2d_Line) lin = Handle(Geom2d_Line)::DownCast(theCurve2d);
290         gp_Pnt2d pnt = lin->Location();
291         gp_Dir2d dir = lin->Direction();
292         if (dir.Y()==0) {
293           parU = Standard_True;
294           cfi = (uf-pnt.X())/dir.X();
295           cli = (ul-pnt.X())/dir.X();
296         } 
297         else if (dir.X()==0) {
298           parV = Standard_True;
299           cfi = (vf-pnt.Y())/dir.Y();
300           cli = (vl-pnt.Y())/dir.Y();
301         } 
302         else {//common case
303           Standard_Real xfi, xli, yfi, yli;
304           xfi = (uf-pnt.X())/dir.X();
305           xli = (ul-pnt.X())/dir.X();
306           yfi = (vf-pnt.Y())/dir.Y();
307           yli = (vl-pnt.Y())/dir.Y();
308           if (dir.X()*dir.Y() > 0) {
309             cfi = (Abs(xli-xfi) < Abs(xli-yfi)? xfi : yfi);
310             cli = (Abs(xfi-xli) < Abs(xfi-yli)? xli : yli);
311           } else {
312             cfi = (Abs(xli-xfi) < Abs(xli-yli)? xfi : yli);
313             cli = (Abs(yli-xli) < Abs(yli-yfi)? xli : yfi);
314           }
315         }
316         if (cfi < cli) { cf = cfi; cl = cli; }
317         else { cf = cli; cl = cfi; }
318       } 
319       else if(!Precision::IsInfinite(uf)&&!Precision::IsInfinite(ul)){
320         Handle(Geom2d_Line) lin = Handle(Geom2d_Line)::DownCast(theCurve2d);
321         gp_Dir2d dir = lin->Direction();
322         if (dir.X()!=0) {
323           if (dir.Y()==0) parU = Standard_True;
324           gp_Pnt2d pnt = lin->Location(); //szv#4:S4163:12Mar99 moved
325           Standard_Real cfi = (uf-pnt.X())/dir.X();
326           Standard_Real cli = (ul-pnt.X())/dir.X();
327           if (cfi < cli) { cf = cfi; cl = cli; }
328           else { cf = cli; cl = cfi; }
329         }
330         else {
331           cf=-10000;
332           cl= 10000;
333         }
334       }
335       else {
336         cf=-10000;
337         cl= 10000;
338         //pdn not cutted by bounds
339 #ifdef DEBUG
340         cout<<"Infinite Surface"<<endl;
341 #endif  
342       }
343     }
344     else {
345       //pdn not linear case not managed
346       cf=-10000;
347       cl= 10000;
348 #ifdef DEBUG     
349       cout<<"Some infinite curve"<<endl;
350 #endif 
351     }
352   }
353   
354   Geom2dAdaptor_Curve          CA     = Geom2dAdaptor_Curve(theCurve2d,cf,cl);
355   Handle(Geom2dAdaptor_HCurve) myHCur = new Geom2dAdaptor_HCurve(CA);
356   
357   Adaptor3d_CurveOnSurface COnS = Adaptor3d_CurveOnSurface(myHCur, myHSur);
358
359   // ----------------------------------------------
360   // --- topological limit == geometric limit ? ---
361   // ----------------------------------------------
362   Standard_Real Uinf = COnS.FirstParameter();
363   Standard_Real Usup = COnS.LastParameter();
364   
365   Standard_Real w1,w2;
366   ShapeAnalysis_Curve sac;
367   gp_Pnt pnt;
368   Standard_Real dist = sac.Project(COnS,Pt1,preci,pnt,w1,Standard_False);
369   if (dist > preci) return;
370   dist = sac.Project(COnS,Pt2,preci,pnt,w2,Standard_False);
371   if (dist > preci) return;
372   
373   myFirstParam = w1;
374   myLastParam  = w2;
375   myFirstDone = myLastDone = Standard_True;
376   if ( myFirstParam == Uinf && myLastParam == Usup ) return;
377   if ( myFirstParam == Usup && myLastParam == Uinf ) {
378     myFirstParam = theCurve2d->ReversedParameter(Usup);
379     myLastParam  = theCurve2d->ReversedParameter(Uinf);
380     theCurve2d->Reverse();
381 #ifdef DEB
382     cout << "Warning: ShapeFix_EdgeProjAux: pcurve reversed" << endl;
383 #endif
384     return;
385   }
386   //:abv 29.08.01: SAT: fix for closed case
387   if ( COnS.Value(Uinf).Distance ( COnS.Value(Usup) ) < Precision::Confusion() ) {
388     // 18.11.2002 SKL OCC630 compare values with tolerance Precision::PConfusion() instead of "=="
389     if ( Abs(myFirstParam-Uinf) < ::Precision::PConfusion() &&
390          Abs(myLastParam-Uinf) < ::Precision::PConfusion() )
391         myLastParam = w2 = Usup;
392     // 18.11.2002 SKL OCC630 compare values with tolerance Precision::PConfusion() instead of "=="
393     else if ( Abs(myFirstParam-Usup) < ::Precision::PConfusion() && 
394               Abs(myLastParam-Usup) < ::Precision::PConfusion() )
395         myFirstParam = w1 = Uinf;
396   }
397    
398   //pdn adjust parameters in periodic case
399   if(parU || parV) {
400     Standard_Real uf,ul,vf,vl;
401     theSurface->Bounds(uf,ul,vf,vl);
402     Standard_Real period = (parU ? ul-uf : vl-vf);
403     w1+=ShapeAnalysis::AdjustToPeriod(w1,0,period);
404     myFirstParam = w1;
405     w2+=ShapeAnalysis::AdjustToPeriod(w2,0,period);
406     myLastParam = w2;
407     Handle(Geom_Curve) C3d1;
408     if(!sae.Curve3d (myEdge, C3d1, cf, cl, Standard_False )) {
409       UpdateParam2d(theCurve2d);
410       return;
411     }
412     gp_Pnt mid = C3d1->Value((cf+cl)/2);
413     Standard_Real wmid;
414     sac.Project(COnS,mid,preci,pnt,wmid,Standard_False);
415     wmid+=ShapeAnalysis::AdjustToPeriod(wmid,0,period);
416     if(w1>w2) {
417       if(w2 > wmid) myFirstParam -= period;
418       else if (w1 > wmid)
419         UpdateParam2d(theCurve2d);
420       else {
421         myLastParam+=period;
422 #ifdef DEBUG
423         cout <<" Added"<<endl;
424 #endif  
425       }
426     }
427     else {
428       if(w1 > wmid) {
429         myLastParam -=period;
430         UpdateParam2d(theCurve2d);
431 #ifdef DEBUG
432         cout <<" Added & Inverted"<<endl;
433 #endif  
434       } else if (w2 < wmid) {
435         myFirstParam += period;
436         UpdateParam2d(theCurve2d);
437       }
438     }
439   }
440   UpdateParam2d(theCurve2d);
441   return;
442 }
443
444 //=======================================================================
445 //function : Init3d
446 //purpose  : 
447 //=======================================================================
448
449 void ShapeFix_EdgeProjAux::Init3d (const Standard_Real preci) 
450 {
451   Standard_Real cl, cf;
452
453   // Extract Geometries
454   Handle(Geom_Surface) theSurface = BRep_Tool::Surface(myFace);
455   Handle(Geom2d_Curve) theCurve2d = BRep_Tool::CurveOnSurface(myEdge, myFace, cf, cl);
456   if ( theCurve2d.IsNull() ) return; //:r5 abv 6 Apr 99:  ec_turbine-A.stp, #4313
457   TopoDS_Vertex V1,V2;
458   
459   V1 = TopExp::FirstVertex(myEdge);
460   V2 = TopExp::LastVertex(myEdge);
461   gp_Pnt Pt1 = BRep_Tool::Pnt(V1);
462   gp_Pnt Pt2 = BRep_Tool::Pnt(V2);
463
464
465   GeomAdaptor_Surface          SA     = GeomAdaptor_Surface(theSurface);  
466   Handle(GeomAdaptor_HSurface) myHSur = new GeomAdaptor_HSurface(SA);
467
468   Geom2dAdaptor_Curve          CA     = Geom2dAdaptor_Curve(theCurve2d);
469   Handle(Geom2dAdaptor_HCurve) myHCur = new Geom2dAdaptor_HCurve(CA);
470
471   Adaptor3d_CurveOnSurface COnS = Adaptor3d_CurveOnSurface(myHCur, myHSur);
472   
473 //:S4136  Standard_Real preci = BRepAPI::Precision();
474   Standard_Real Uinf = theCurve2d->FirstParameter();
475   Standard_Real Usup = theCurve2d->LastParameter();
476
477   // ----------------------------------------------
478   // --- topological limit == geometric limit ? ---
479   // ----------------------------------------------
480   
481   if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_BoundedCurve))) {
482     
483     gp_Pnt Pdeb = COnS.Value(Uinf);
484     gp_Pnt Pfin = COnS.Value(Usup);
485     
486     //szv#4:S4163:12Mar99 optimized
487     if ( Pdeb.IsEqual(Pt1, preci) && Pfin.IsEqual(Pt2, preci) ) {
488       myFirstParam = Uinf;
489       myLastParam  = Usup;
490       myFirstDone = myLastDone = Standard_True;
491       return;
492     }
493   }
494
495   // ------------------------------------------
496   // --- The CurveOnSurface is not infinite ---
497   // ---          Try with Extrema          ---
498   // ------------------------------------------
499
500   Standard_Real w1 = COnS.FirstParameter();
501   Standard_Real w2 = COnS.LastParameter();
502
503   if ((!Precision::IsInfinite(w1) &&
504        !Precision::IsInfinite(w2) &&
505        theCurve2d->Continuity() != GeomAbs_C0) ||
506       IsIso(theCurve2d))  {
507
508     //szv#4:S4163:12Mar99 optimized
509     if ( FindParameterWithExt(Pt1, COnS, Uinf, Usup, preci, w1) &&
510          FindParameterWithExt(Pt2, COnS, Uinf, Usup, preci, w2) ) {
511       myFirstParam = w1;
512       myLastParam  = w2;
513       UpdateParam2d(theCurve2d);
514       myFirstDone = myLastDone = Standard_True;
515       return;
516     }
517   }
518   myFirstDone = myLastDone = Standard_True;
519 }
520
521 //=======================================================================
522 //function : UpdateParam2d
523 //purpose  : 
524 //=======================================================================
525
526 void ShapeFix_EdgeProjAux::UpdateParam2d (const Handle(Geom2d_Curve)& theCurve2d)
527 {
528   if (myFirstParam < myLastParam)  return;
529
530   Standard_Real cf = theCurve2d->FirstParameter();
531   Standard_Real cl = theCurve2d->LastParameter();
532 //:S4136  Standard_Real preci = BRepAPI::Precision();
533   Standard_Real preci2d = Precision::PConfusion(); //:S4136: Parametric(preci, 0.01);
534
535   // 15.11.2002 PTV OCC966
536   if (ShapeAnalysis_Curve::IsPeriodic(theCurve2d)) {
537     ElCLib::AdjustPeriodic(cf,cl,preci2d,myFirstParam,myLastParam);
538   }
539   else if (theCurve2d->IsClosed()) {
540     //szv#4:S4163:12Mar99 optimized
541     if      ( Abs ( myFirstParam - cl ) <= preci2d ) myFirstParam = cf;
542     else if ( Abs ( myLastParam  - cf ) <= preci2d ) myLastParam  = cl;
543     else {
544 #ifdef DEBUG
545       cout << "Error : curve 2d range crossing non periodic curve origin";
546       cout <<  endl;
547 #endif
548       // add fail result;
549       return;
550     }
551   }
552   // the curve is closed within the 'file' 2D tolerance 
553   else if (theCurve2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
554     Handle(Geom2d_BSplineCurve) aBSpline2d =
555       Handle(Geom2d_BSplineCurve)::DownCast(theCurve2d);
556     if (aBSpline2d->StartPoint().Distance(aBSpline2d->EndPoint()) <= preci2d) {
557       if      ( Abs ( myFirstParam - cl ) <= preci2d ) myFirstParam = cf;
558       else if ( Abs ( myLastParam  - cf ) <= preci2d ) myLastParam  = cl;
559     }
560   }
561   else {
562 #ifdef DEBUG
563     cout << "Warning : non increasing parameters for 2d curve." << endl;
564     cout << "          update parameter 2d uncertain." << endl;
565 #endif
566     Standard_Real tmp1 = myFirstParam, tmp2 = myLastParam;
567     myFirstParam = theCurve2d->ReversedParameter(tmp1);
568     myLastParam = theCurve2d->ReversedParameter(tmp2);
569     theCurve2d->Reverse();
570     //cout<<"Reversed case 2"<<endl;
571   }
572 }