0022459: The ImportExport sample crash
[occt.git] / src / ShapeAnalysis / ShapeAnalysis_Wire.cxx
1 // File:        ShapeAnalysis_Wire.cxx
2 // Created:     Thu Jan 20 14:46:55 2000
3 // Author:      data exchange team
4 //              <det@nnov>
5
6 //:pdn 11.12.98: FixDegenerated improved
7 //:pdn 05.01.99: renaming method CheckLittle to CheckSmall
8 //:l0 abv 10.01.99: CATIA01 #1727: fix intersecting edges always if edge is lacking
9 //:n2 abv 22.01.99: ma-test5.igs: IGES read (pref3d): remove degen edge with no pcurve
10 //:o4 abv 17.02.99: r0301_db.stp #53082: adding parameter isClosed to CheckOrder
11 //    rln 03.03.99  S4135: using updated ShapeAnalysis_Surface for checking of singularities
12 //:p9 abv 11.03.99: PRO7226 #489490: fix :i9 moved to allow fixing a set of degenerated edges
13 //#77 rln 11.03.99: S4135: using singularity which has minimum gap between singular point and input 3D point
14 //#84 rln 18.03.99: inserting degenerated edge between ends of pcurves
15 //pdn 12.03.99 S4135 check degenerated applies minimal tolerance first.
16 //pdn 16.03.99 S4135 adding check of non adjacent edjes.
17 //#83 rln 19.03.99: processing segments in intersection as in BRepCheck
18 //%15 pdn 15.03.99  checking of small area wire added
19 //#2 smh 26.03.99  S4163 Zero divide
20 //#4 szv           S4163 optimizing
21 //:r6 abv 08.04.99: protect FixIE against working out of curve range
22 //:s1 abv 22.04.99: PRO7226 #489490: ensure fixing of degenerated edge
23 //#9 smh 14.12.99 BUC60615 Using tolerance of verteces during checking degenerated edge.
24
25 #include <ShapeAnalysis_Wire.ixx>
26
27 #include <Precision.hxx>
28
29 #include <Geom_Curve.hxx>
30 #include <Geom2d_Curve.hxx>
31 #include <GeomAdaptor_HSurface.hxx>
32 #include <Geom2dAdaptor_Curve.hxx>
33 #include <Geom2dInt_GInter.hxx>
34 #include <IntRes2d_Domain.hxx>
35 #include <IntRes2d_Transition.hxx>
36 #include <IntRes2d_IntersectionPoint.hxx>
37 #include <IntRes2d_IntersectionSegment.hxx>
38
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <BRep_Builder.hxx>
42 #include <BRep_Tool.hxx>
43 #include <BRepTools.hxx>
44
45 #include <ShapeExtend.hxx>
46 #include <ShapeAnalysis.hxx>
47 #include <ShapeAnalysis_Curve.hxx>
48 #include <ShapeAnalysis_Edge.hxx>
49
50 #include <TopoDS.hxx>
51 #include <Precision.hxx>
52 #include <TColgp_Array1OfPnt.hxx>
53 #include <TColStd_Array1OfReal.hxx>
54 #include <Bnd_Array1OfBox2d.hxx>
55 #include <BndLib_Add2dCurve.hxx>
56 #include <Bnd_Box2d.hxx>
57
58 //szvsh addition
59 #include <Geom2dAdaptor_HCurve.hxx>
60 #include <Adaptor3d_CurveOnSurface.hxx>
61 #include <TColgp_SequenceOfPnt.hxx>
62 #include <ShapeAnalysis_Surface.hxx>
63 #include <TopoDS_Wire.hxx>
64 #include <ShapeAnalysis.hxx>
65 #include <Geom_Plane.hxx>
66 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
67 #include <TopoDS_Iterator.hxx>
68 #include <TopTools_DataMapOfShapeListOfShape.hxx>
69 #include <TopTools_ListOfShape.hxx>
70 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <TopTools_ListIteratorOfListOfShape.hxx>
72 #include <TopExp.hxx>
73
74 //=======================================================================
75 //function : ShapeAnalysis_Wire
76 //purpose  : 
77 //=======================================================================
78
79 ShapeAnalysis_Wire::ShapeAnalysis_Wire()
80 {
81   ClearStatuses();
82   myPrecision = ::Precision::Confusion();
83 }
84
85 //=======================================================================
86 //function : ShapeAnalysis_Wire
87 //purpose  : 
88 //=======================================================================
89
90 ShapeAnalysis_Wire::ShapeAnalysis_Wire (const TopoDS_Wire& wire,
91                                         const TopoDS_Face& face,
92                                         const Standard_Real precision)
93 {
94   Init (wire, face, precision);
95 }
96
97 //=======================================================================
98 //function : ShapeAnalysis_Wire
99 //purpose  : 
100 //=======================================================================
101
102 ShapeAnalysis_Wire::ShapeAnalysis_Wire (const Handle(ShapeExtend_WireData)& sbwd,
103                                         const TopoDS_Face& face,
104                                         const Standard_Real precision)
105 {
106   Init (sbwd, face, precision);
107 }
108
109 //=======================================================================
110 //function : Init
111 //purpose  : 
112 //=======================================================================
113
114 void ShapeAnalysis_Wire::Init (const TopoDS_Wire& wire,
115                                const TopoDS_Face& face, const Standard_Real precision) 
116 {
117   Init (new ShapeExtend_WireData (wire), face, precision);
118 }
119
120 //=======================================================================
121 //function : Init
122 //purpose  : 
123 //=======================================================================
124
125 void ShapeAnalysis_Wire::Init (const Handle(ShapeExtend_WireData)& sbwd,
126                                const TopoDS_Face& face, const Standard_Real precision) 
127 {
128   Load (sbwd);
129   SetFace (face);
130   SetPrecision (precision);
131 }
132
133 //=======================================================================
134 //function : Load
135 //purpose  : 
136 //=======================================================================
137
138 void ShapeAnalysis_Wire::Load (const TopoDS_Wire& wire) 
139 {
140   ClearStatuses();
141   myWire = new ShapeExtend_WireData (wire);
142 }
143
144 //=======================================================================
145 //function : Load
146 //purpose  : 
147 //=======================================================================
148
149 void ShapeAnalysis_Wire::Load (const Handle(ShapeExtend_WireData)& sbwd) 
150 {
151   ClearStatuses();
152   myWire = sbwd;
153 }
154
155 //=======================================================================
156 //function : SetFace
157 //purpose  : 
158 //=======================================================================
159
160 void ShapeAnalysis_Wire::SetFace(const TopoDS_Face& face) 
161 {
162   myFace = face;
163   if(!face.IsNull())
164     mySurf = new ShapeAnalysis_Surface ( BRep_Tool::Surface ( myFace ) );
165 }
166
167 //=======================================================================
168 //function : SetSurface
169 //purpose  : 
170 //=======================================================================
171
172 void ShapeAnalysis_Wire::SetSurface (const Handle(Geom_Surface)& surface) 
173 {
174   SetSurface ( surface, TopLoc_Location() );
175 }
176
177 //=======================================================================
178 //function : SetSurface
179 //purpose  : 
180 //=======================================================================
181
182 void ShapeAnalysis_Wire::SetSurface (const Handle(Geom_Surface)& surface,
183                                      const TopLoc_Location& location) 
184 {
185   BRep_Builder B;
186   TopoDS_Face face;
187   B.MakeFace ( face, surface, location, ::Precision::Confusion() );
188   SetFace ( face );
189 }
190
191 //=======================================================================
192 //function : SetPrecision
193 //purpose  : 
194 //=======================================================================
195
196  void ShapeAnalysis_Wire::SetPrecision(const Standard_Real precision) 
197 {
198   myPrecision = precision;
199 }
200
201 //=======================================================================
202 //function : ClearStatuses
203 //purpose  : 
204 //=======================================================================
205
206  void ShapeAnalysis_Wire::ClearStatuses()
207 {
208   myStatusOrder = myStatusConnected =
209     myStatusEdgeCurves = myStatusDegenerated =
210       myStatusClosed = myStatusLacking =
211         myStatusSelfIntersection = myStatusSmall =
212           myStatusGaps3d = myStatusGaps2d =
213             myStatusCurveGaps = myStatusLoop = myStatus = 0;
214
215   myMin3d = myMin2d = myMax3d = myMax2d = 0.;
216 }
217
218 //=======================================================================
219 //function : Perform
220 //purpose  : 
221 //=======================================================================
222
223  Standard_Boolean ShapeAnalysis_Wire::Perform() 
224 {
225   Standard_Boolean result = Standard_False;
226   result |= CheckOrder();
227   result |= CheckSmall();
228   result |= CheckConnected();
229   result |= CheckEdgeCurves();
230   result |= CheckDegenerated();
231   result |= CheckSelfIntersection();
232   result |= CheckLacking();
233   result |= CheckClosed();
234   return result;
235 }
236
237 //=======================================================================
238 //function : CheckOrder
239 //purpose  : 
240 //=======================================================================
241
242  Standard_Boolean ShapeAnalysis_Wire::CheckOrder (const Standard_Boolean isClosed,
243                                                   const Standard_Boolean mode3d) 
244 {
245   ShapeAnalysis_WireOrder sawo;
246   CheckOrder (sawo, isClosed, mode3d);
247   myStatusOrder = myStatus;
248   return StatusOrder (ShapeExtend_DONE);
249 }
250
251 //=======================================================================
252 //function : CheckSmall
253 //purpose  : 
254 //=======================================================================
255
256  Standard_Boolean ShapeAnalysis_Wire::CheckSmall(const Standard_Real precsmall) 
257 {
258   for (Standard_Integer i = 1; i <= myWire->NbEdges(); i++) {
259     CheckSmall (i, precsmall);
260     myStatusSmall |= myStatus;
261   }
262   return StatusSmall (ShapeExtend_DONE);
263 }
264
265 //=======================================================================
266 //function : CheckConnected
267 //purpose  : 
268 //=======================================================================
269
270  Standard_Boolean ShapeAnalysis_Wire::CheckConnected(const Standard_Real prec) 
271 {
272   for (Standard_Integer i = 1; i <= myWire->NbEdges(); i++) {
273     CheckConnected ( i, prec );
274     myStatusConnected |= myStatus;
275   }
276   return StatusConnected (ShapeExtend_DONE);
277 }
278
279 //=======================================================================
280 //function : CheckEdgeCurves
281 //purpose  : 
282 //=======================================================================
283
284  Standard_Boolean ShapeAnalysis_Wire::CheckEdgeCurves() 
285 {
286   myStatusEdgeCurves = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
287   if ( ! IsReady() ) return Standard_False;
288   
289   Standard_Integer i, nb = myWire->NbEdges();
290   ShapeAnalysis_Edge SAE;
291   
292   for (i = 1; i <= nb; i++) {
293     TopoDS_Edge E = myWire->Edge (i);
294
295     SAE.CheckCurve3dWithPCurve (E, myFace);
296     if (SAE.Status (ShapeExtend_DONE))
297       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
298     if (SAE.Status ( ShapeExtend_FAIL)) 
299       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
300
301     SAE.CheckVerticesWithPCurve (E, myFace);
302     if (SAE.Status (ShapeExtend_DONE))
303       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
304     if (SAE.Status ( ShapeExtend_FAIL)) 
305       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
306
307     SAE.CheckVerticesWithCurve3d (E);
308     if (SAE.Status (ShapeExtend_DONE))
309       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
310     if (SAE.Status ( ShapeExtend_FAIL)) 
311       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
312
313     CheckSeam (i);
314     if (LastCheckStatus (ShapeExtend_DONE)) 
315       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE4);
316     if (LastCheckStatus (ShapeExtend_FAIL)) 
317       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL4);
318
319     CheckGap3d (i);
320     if (LastCheckStatus (ShapeExtend_DONE)) 
321       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE5);
322     if (LastCheckStatus (ShapeExtend_FAIL)) 
323       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL5);
324
325     CheckGap2d (i);
326     if (LastCheckStatus (ShapeExtend_DONE)) 
327       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE6);
328     if (LastCheckStatus (ShapeExtend_FAIL)) 
329       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL6);
330
331     Standard_Real maxdev = 0.0;
332     SAE.CheckSameParameter (myWire->Edge (i), maxdev);
333     if (SAE.Status (ShapeExtend_DONE))
334       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_DONE7);
335     if (SAE.Status ( ShapeExtend_FAIL)) 
336       myStatusEdgeCurves |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL7);
337   }
338   return StatusEdgeCurves (ShapeExtend_DONE);
339 }
340
341 //=======================================================================
342 //function : CheckDegenerated
343 //purpose  : 
344 //=======================================================================
345
346  Standard_Boolean ShapeAnalysis_Wire::CheckDegenerated() 
347 {
348   for (Standard_Integer i = 1; i <= myWire->NbEdges(); i++) {
349     CheckDegenerated (i);
350     myStatusDegenerated |= myStatus;
351   }
352   return StatusDegenerated (ShapeExtend_DONE);
353 }
354
355 //=======================================================================
356 //function : CheckSelfIntersection
357 //purpose  : 
358 //=======================================================================
359
360  Standard_Boolean ShapeAnalysis_Wire::CheckSelfIntersection() 
361 {
362   myStatusSelfIntersection = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
363   if (!IsReady()) return Standard_False;
364   Standard_Integer i, nb = myWire->NbEdges();
365   for (i = 1; i <= nb; i++) {
366     CheckSelfIntersectingEdge (i);
367     if (LastCheckStatus (ShapeExtend_DONE))
368       myStatusSelfIntersection |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
369     if (LastCheckStatus (ShapeExtend_FAIL)) 
370       myStatusSelfIntersection |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
371
372     CheckIntersectingEdges (i);
373     if (LastCheckStatus (ShapeExtend_DONE))
374       myStatusSelfIntersection |= ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
375     if (LastCheckStatus (ShapeExtend_FAIL)) 
376       myStatusSelfIntersection |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
377   }
378   
379   Bnd_Array1OfBox2d boxes(1,nb);
380   TopLoc_Location L;
381   const Handle(Geom_Surface)& S = BRep_Tool::Surface(Face(), L);
382   Handle(Geom2d_Curve) c2d;
383   Standard_Real cf,cl;
384   ShapeAnalysis_Edge sae;
385   Handle(ShapeExtend_WireData) sbwd = WireData();
386   for(i = 1; i <= nb; i++){
387     TopoDS_Edge E = sbwd->Edge (i);
388     if(sae.PCurve (E,S,L,c2d,cf,cl,Standard_False)) {
389       Bnd_Box2d box;
390       Geom2dAdaptor_Curve gac(c2d,cf,cl);
391       BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
392       boxes(i) = box;
393     }
394   }
395   
396   Standard_Boolean isFail = Standard_False, isDone = Standard_False;
397   for(Standard_Integer num1 = 1; num1 < nb-1; num1++) {
398     Standard_Integer fin = nb;
399     if (CheckClosed(Precision::Confusion()) && 1 == num1)
400       fin = nb-1;
401     for(Standard_Integer num2 = num1+2; num2 <= fin; num2++) 
402       if(!boxes(num1).IsOut(boxes(num2))){
403         CheckIntersectingEdges(num1, num2);
404         isFail |= LastCheckStatus ( ShapeExtend_FAIL1 );
405         isDone |= LastCheckStatus ( ShapeExtend_DONE1 );
406       }
407   }
408   if(isFail)
409     myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
410   if(isDone)
411     myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
412   
413   return StatusSelfIntersection (ShapeExtend_DONE);
414 }
415
416 //=======================================================================
417 //function : CheckLacking
418 //purpose  : 
419 //=======================================================================
420
421  Standard_Boolean ShapeAnalysis_Wire::CheckLacking() 
422 {
423   if (!IsReady() || NbEdges() < 2) return Standard_False; 
424   for (Standard_Integer i = 1; i <= myWire->NbEdges(); i++) {
425     CheckLacking (i);
426     myStatusLacking |= myStatus;
427   }
428   return StatusLacking (ShapeExtend_DONE);
429 }
430
431 //=======================================================================
432 //function : CheckClosed
433 //purpose  : 
434 //=======================================================================
435
436  Standard_Boolean ShapeAnalysis_Wire::CheckClosed(const Standard_Real prec) 
437 {
438   myStatusClosed = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
439   if (!IsReady() || NbEdges() < 1) return Standard_False;
440   
441   CheckConnected (1, prec);
442   if ( LastCheckStatus ( ShapeExtend_DONE ) ) 
443     myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
444   if ( LastCheckStatus ( ShapeExtend_FAIL ) ) 
445     myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
446
447   CheckDegenerated ( 1 );
448   if ( LastCheckStatus ( ShapeExtend_DONE ) ) 
449     myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
450   if ( LastCheckStatus ( ShapeExtend_FAIL ) ) 
451     myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
452
453   return StatusClosed ( ShapeExtend_DONE );
454 }
455
456 //=======================================================================
457 //function : CheckGaps3d
458 //purpose  : 
459 //=======================================================================
460
461  Standard_Boolean ShapeAnalysis_Wire::CheckGaps3d ()
462 {
463   myStatusGaps3d = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
464   if (!IsLoaded() || NbEdges() < 1) return Standard_False; //gka IsLoaded
465   
466   Standard_Real dist, maxdist = 0.;
467   
468   for (Standard_Integer i = 1; i <= NbEdges(); i++) {
469     CheckGap3d(i);
470     myStatusGaps3d |= myStatus;
471     if (!LastCheckStatus(ShapeExtend_FAIL1)) {
472       dist = MinDistance3d();
473       if (maxdist<dist) maxdist = dist;
474     }
475   }
476   myMin3d = myMax3d = maxdist;
477
478   return StatusGaps3d ( ShapeExtend_DONE );
479 }
480
481 //=======================================================================
482 //function : CheckGaps2d
483 //purpose  : 
484 //=======================================================================
485
486  Standard_Boolean ShapeAnalysis_Wire::CheckGaps2d ()
487 {
488   myStatusGaps2d = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
489   if (!IsReady() || NbEdges() < 1) return Standard_False;
490   
491   Standard_Real dist, maxdist = 0.;
492
493   for (Standard_Integer i = 1; i <= NbEdges(); i++) {
494     CheckGap2d(i);
495     myStatusGaps2d |= myStatus;
496     if (!LastCheckStatus(ShapeExtend_FAIL1)) {
497       dist = MinDistance2d();
498       if (maxdist<dist) maxdist = dist;
499     }
500   }
501   myMin2d = myMax2d = maxdist;
502
503   return StatusGaps2d ( ShapeExtend_DONE );
504 }
505
506 //=======================================================================
507 //function : CheckCurveGaps
508 //purpose  : 
509 //=======================================================================
510
511  Standard_Boolean ShapeAnalysis_Wire::CheckCurveGaps ()
512 {
513   myStatusCurveGaps = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
514   if (!IsReady() || NbEdges() < 1) return Standard_False;
515   
516   Standard_Real dist, maxdist = 0.;
517
518   for (Standard_Integer i = 1; i <= NbEdges(); i++) {
519     CheckCurveGap(i);
520     myStatusCurveGaps |= myStatus;
521     if (!LastCheckStatus(ShapeExtend_FAIL1)) {
522       dist = MinDistance3d();
523       if (maxdist<dist) maxdist = dist;
524     }
525   }
526   myMin3d = myMax3d = maxdist;
527
528   return StatusCurveGaps ( ShapeExtend_DONE );
529 }
530
531 //=======================================================================
532 //function : CheckOrder
533 //purpose  : 
534 //=======================================================================
535
536 Standard_Boolean ShapeAnalysis_Wire::CheckOrder(ShapeAnalysis_WireOrder& sawo,
537                                                 const Standard_Boolean isClosed,
538                                                 const Standard_Boolean mode3d) 
539 {
540   if ( ! mode3d && myFace.IsNull() ) {
541     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2); 
542     return Standard_False;
543   }
544   
545   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
546   sawo.SetMode ( mode3d, ( mode3d ? myPrecision : ::Precision::PConfusion() ) );
547   Standard_Integer i, nb = myWire->NbEdges();
548   ShapeAnalysis_Edge EA;
549   for (i = 1; i <= nb; i ++) {
550     TopoDS_Edge E = myWire->Edge(i);
551     if ( mode3d ) {
552       TopoDS_Vertex V1 = EA.FirstVertex (E); 
553       TopoDS_Vertex V2 = EA.LastVertex  (E); 
554       if (V1.IsNull() || V2.IsNull())
555       {
556         myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
557         return Standard_False;
558       }
559       gp_Pnt p1 = BRep_Tool::Pnt (V1);
560       gp_Pnt p2 = BRep_Tool::Pnt (V2);
561       sawo.Add (p1.XYZ(),p2.XYZ());
562     }
563     else {
564       Standard_Real f,l;
565       Handle(Geom2d_Curve) c2d;
566       TopoDS_Shape tmpF = myFace.Oriented(TopAbs_FORWARD);
567       if ( ! EA.PCurve(E,TopoDS::Face(tmpF),c2d,f,l) ) {
568         myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
569         return Standard_False;
570       }
571       sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
572     }
573   }
574   sawo.Perform(isClosed);
575   Standard_Integer stat = sawo.Status();
576   switch (stat) {
577   case   0: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);    break;
578   case   1: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1); break;
579   case   2: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2); break;
580   case  -1: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE3); break;
581   case  -2: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE4); break;
582   case -10: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1); break;
583   }
584   return LastCheckStatus (ShapeExtend_DONE);
585 }
586
587 //=======================================================================
588 //function : CheckConnected
589 //purpose  : 
590 //=======================================================================
591
592 Standard_Boolean ShapeAnalysis_Wire::CheckConnected (const Standard_Integer num,
593                                                      const Standard_Real prec) 
594 {
595   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
596   if ( ! IsLoaded() || NbEdges() < 1 ) return Standard_False;
597
598   Standard_Integer n2 = ( num >0 ? num  : NbEdges() );
599   Standard_Integer n1 = ( n2  >1 ? n2-1 : NbEdges() );
600 //  if (n1 == n2) return 0;
601
602   TopoDS_Edge E1 = WireData()->Edge ( n1 );
603   TopoDS_Edge E2 = WireData()->Edge ( n2 );
604   
605   ShapeAnalysis_Edge sae;
606   TopoDS_Vertex V1 = sae.LastVertex (E1);
607   TopoDS_Vertex V2 = sae.FirstVertex (E2);
608   if (V1.IsNull() || V2.IsNull())
609   {
610     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
611     return Standard_False;
612   }
613   if (V1.IsSame(V2)) return Standard_False;
614
615   gp_Pnt p1 = BRep_Tool::Pnt (V1);
616   gp_Pnt p2 = BRep_Tool::Pnt (V2);
617   myMin3d = p1.Distance(p2);
618   if ( myMin3d <= gp::Resolution() ) myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
619   else if ( myMin3d <= myPrecision ) myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
620   else if ( myMin3d <= prec )        myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE3);
621   else {
622     // et en inversant la derniere edge ?
623     if ( n1 == n2 ) myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
624     else {
625       V2 = sae.LastVertex (E2);
626       p2 = BRep_Tool::Pnt (V2);
627       Standard_Real dist = p1.Distance(p2);
628       if ( dist > myPrecision ) myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
629       else {
630         myMin3d = dist;
631         myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
632       }
633     }
634     return Standard_False;
635   }
636   return Standard_True;
637 }
638
639 //=======================================================================
640 //function : CheckSmall
641 //purpose  : 
642 //=======================================================================
643
644 Standard_Boolean ShapeAnalysis_Wire::CheckSmall (const Standard_Integer num,
645                                                  const Standard_Real precsmall) 
646 {
647   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
648   if ( ! IsLoaded() || NbEdges() <= 1 ) return Standard_False;
649
650   //Standard_Integer n = ( num ? num : NbEdges() ); //szv#4:S4163:12Mar99 not needed
651   TopoDS_Edge E = myWire->Edge ( num ? num : NbEdges() );
652   ShapeAnalysis_Edge sae;
653
654   if ( BRep_Tool::Degenerated ( E ) ) {
655     //:n2 abv 22 Jan 99: ma-test5.igs -> IGES (brep) -> read (pref3d):
656     // degen edge with no pcurve should be removed
657     if ( ! myFace.IsNull() && sae.HasPCurve ( E, Face() ) ) return Standard_False;
658     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
659   }
660   
661   TopoDS_Vertex V1 = sae.FirstVertex (E);
662   TopoDS_Vertex V2 = sae.LastVertex (E);
663   gp_Pnt p1 = BRep_Tool::Pnt (V1);
664   gp_Pnt p2 = BRep_Tool::Pnt (V2);
665   Standard_Real dist = p1.Distance(p2);
666   Standard_Real prec = precsmall;//Min ( myPrecision, precsmall );
667   //Standard_Real prec = Min(BRep_Tool::Tolerance(V1),BRep_Tool::Tolerance(V2)); //skl
668   if (dist > prec) return Standard_False;  // pas nulle
669   
670   // La courbe 3D a present : est-elle FERMEE ou DE LONGUEUR NULLE ... ???
671   // Pour cela on prend le point milieu (y a-t-il mieux)
672   // Si pas de C3D, on essaie la C2D ...
673
674   gp_Pnt Pm;
675   Standard_Real cf,cl;
676   Handle(Geom_Curve) c3d;    
677   if ( sae.Curve3d (E,c3d,cf,cl,Standard_False) ) Pm = c3d->Value ( (cf+cl)/2. );
678   else {
679     Handle(Geom2d_Curve) c2d;
680     if ( ! myFace.IsNull() && sae.PCurve (E,myFace,c2d,cf,cl,Standard_False)) {
681       gp_Pnt2d p2m = c2d->Value ( (cf+cl)/2. );
682       Pm = mySurf->Value (p2m);
683     }
684     else {
685       myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
686       Pm = p1;
687 //:n2      return Standard_False;
688     }
689   }
690   if ( Pm.Distance(p1) > prec || Pm.Distance(p2) > prec ) return Standard_False;
691
692   myStatus |= ShapeExtend::EncodeStatus ( V1.IsSame(V2) ? ShapeExtend_DONE1 : ShapeExtend_DONE2 );
693   return Standard_True;
694 }
695
696 //=======================================================================
697 //function : CheckSeam
698 //purpose  : 
699 //=======================================================================
700
701  Standard_Boolean ShapeAnalysis_Wire::CheckSeam(const Standard_Integer num,
702                                                 Handle(Geom2d_Curve)& C1,
703                                                 Handle(Geom2d_Curve)& C2,
704                                                 Standard_Real& cf,
705                                                 Standard_Real& cl) 
706 {
707   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
708   if (!IsReady()) return Standard_False;
709   Standard_Integer n = num;    if (n == 0) n = NbEdges();
710   TopoDS_Edge E = myWire->Edge (n);
711   if ( ! ShapeAnalysis_Edge().IsSeam ( E, myFace ) ) return Standard_False;
712   // Extract the Two PCurves of the Seam 
713   TopoDS_Face ForwardFace = myFace; ForwardFace.Orientation (TopAbs_FORWARD);
714   //szv#4:S4163:12Mar99 SGI warns
715   TopoDS_Shape EF = E.Oriented(TopAbs_FORWARD);
716   TopoDS_Shape ER = E.Oriented(TopAbs_REVERSED);
717   C1 = BRep_Tool::CurveOnSurface(TopoDS::Edge(EF), ForwardFace, cf, cl);
718   C2 = BRep_Tool::CurveOnSurface(TopoDS::Edge(ER), ForwardFace, cf, cl);
719   if (C1.IsNull() || C2.IsNull()) return Standard_False;
720
721 //  SelectForward est destine a devenir un outil distinct
722
723   Standard_Integer theCurveIndice = ShapeAnalysis_Curve().SelectForwardSeam (C1,C2);
724   if ( theCurveIndice != 2 ) return Standard_False;
725
726   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
727   return Standard_True;
728 }
729
730 //=======================================================================
731 //function : CheckSeam
732 //purpose  : 
733 //=======================================================================
734
735   Standard_Boolean ShapeAnalysis_Wire::CheckSeam(const Standard_Integer num) 
736 {
737   Handle(Geom2d_Curve) C1, C2;
738   Standard_Real cf, cl;
739   return CheckSeam (num, C1, C2, cf, cl);
740 }
741
742 //=======================================================================
743 //function : CheckDegenerated
744 //purpose  : 
745 //=======================================================================
746
747 Standard_Boolean ShapeAnalysis_Wire::CheckDegenerated (const Standard_Integer num,
748                                                        gp_Pnt2d& p2d1, gp_Pnt2d& p2d2) 
749 {
750   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
751   if ( ! IsReady() || NbEdges() < 1 ) return Standard_False;
752
753   Standard_Integer n2 = (num > 0)? num : NbEdges();
754   Standard_Integer n1 = (n2 > 1)? n2-1 : NbEdges();
755   Standard_Integer n3 = (n2 < NbEdges())? n2+1 : 1;
756   TopoDS_Edge E1 = myWire->Edge ( n1 );
757   TopoDS_Edge E2 = myWire->Edge ( n2 );
758   TopoDS_Edge E3 = myWire->Edge ( n3 );
759   
760   ShapeAnalysis_Edge sae;
761   
762   // skip if edge is already marked as degenerated and has pcurve
763   if ( BRep_Tool::Degenerated ( E2 ) && sae.HasPCurve ( E2, Face() ) ) {
764     // skl 30.12.2004 for OCC7630 - we have to check pcurve
765     if( sae.HasPCurve(E1,Face()) && sae.HasPCurve(E3,Face()) ) {
766       Handle(Geom2d_Curve) c2d;
767       Standard_Real fp,lp;
768       sae.PCurve ( E2, myFace, c2d, fp, lp, Standard_True );
769       gp_Pnt2d p21 = c2d->Value(fp);
770       gp_Pnt2d p22 = c2d->Value(lp);
771       sae.PCurve ( E1, myFace, c2d, fp, lp, Standard_True );
772       gp_Pnt2d p12 = c2d->Value(lp);
773       sae.PCurve ( E3, myFace, c2d, fp, lp, Standard_True );
774       gp_Pnt2d p31 = c2d->Value(fp);
775       if( fabs(p12.Distance(p31)-p21.Distance(p22)) > 2*Precision::PConfusion() ) {
776         // pcurve is bad => we can remove this edge in ShapeFix
777         // if set needed status
778         myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
779       }
780     }
781     return Standard_False;
782   }
783   
784   //pdn allows to insert two sequences of degenerated edges (on separate bounds of surfaces)
785   if ( n1 != n2 && BRep_Tool::Degenerated ( E1 ) && 
786        ! sae.HasPCurve ( E1, Face() ) ) {
787     //:abv 13.05.02: OCC320 - fail (to remove edge) if two consequtive degenerated edges w/o pcurves
788     if ( BRep_Tool::Degenerated ( E2 ) )
789       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
790     return Standard_False;
791   }
792 //:i8  if ( BRep_Tool::Degenerated ( E1 ) || 
793 //:i8       BRep_Tool::Degenerated ( E2 ) ) return Standard_False;  // deja OK
794   
795   TopoDS_Vertex Vp = sae.FirstVertex (E1); //:i9
796   TopoDS_Vertex V0 = sae.LastVertex  (E1);
797   TopoDS_Vertex V1 = sae.FirstVertex (E2);
798   TopoDS_Vertex V2 = sae.LastVertex  (E2);
799   gp_Pnt pp = BRep_Tool::Pnt (Vp); //:i9
800   gp_Pnt p0 = BRep_Tool::Pnt (V0);
801   gp_Pnt p1 = BRep_Tool::Pnt (V1);
802   gp_Pnt p2 = BRep_Tool::Pnt (V2);
803   Standard_Real par1, par2;
804   Standard_Boolean lack = Standard_False;
805   Standard_Boolean dgnr = Standard_False;
806   //pdn 12.03.99 minimal value processing first
807   Standard_Real precFirst = Min(myPrecision,BRep_Tool::Tolerance(V1));
808   Standard_Real precFin   = Max(myPrecision,BRep_Tool::Tolerance(V1));
809   Standard_Real precVtx   = (myPrecision<BRep_Tool::Tolerance(V1) ? 2*precFin : precFin);
810   //  forward : si Edge <num> FWD/REV. Si LACK, toujours True
811   Standard_Boolean forward = ( E2.Orientation() == TopAbs_FORWARD );
812   //  FIX FEV 1998 : recompute singularity according precision
813
814   if (p1.Distance(p2) <= precFirst) { // edge DGNR
815     dgnr = mySurf->DegeneratedValues ( p1, precVtx, p2d1, p2d2, par1, par2, forward ); //smh#9
816     if ( dgnr ) { // abv 24 Feb 00: trj3_as1-ac-214.stp #6065: avoid making closed edge degenerated
817       Standard_Real a, b;
818       Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E2, a, b );
819       if ( ! C3d.IsNull() ) {
820         gp_Pnt p = C3d->Value ( 0.5 * ( a + b ) );
821         if ( p.SquareDistance ( p1 ) > precVtx * precVtx ) dgnr = Standard_False;
822       }
823     }
824   }
825   if ( ! dgnr ) {
826     //:i9 abv 23 Sep 98: CTS20315-2 #63231: check that previous edge is not degenerated
827     if ( n1 != n2 && p1.Distance(pp) <= precFirst &&
828          mySurf->IsDegenerated ( pp, precFirst ) && 
829          ! BRep_Tool::Degenerated ( E1 ) ) return Standard_False;
830     //rln S4135 ShapeAnalysis_Surface new algorithms for singularities
831     //:45 by abv 16 Dec 97: BUC60035 2659: precision increased to vertex tolerance
832     //Standard_Real prec = Max ( myPrecision, BRep_Tool::Tolerance(V1) );
833     //:51 abv 22 Dec 97: recompute singularities if necessary
834     //rln S4135 if ( prec > myPrecision ) mySurf->ComputeSingularities ( 2 * prec ); //:51 //:74 abv 15 Jan 97: *2
835
836     if ( p0.Distance ( p1 ) <= precFin ) {// ou DGNR manquante ?
837       //rln S4135 singularity with precision = 2 * prec, but distance <= prec
838       //lack = mySurf->DegeneratedValues ( p1, prec, p2d1, p2d2, par1, par2, forward);
839       Standard_Real tmpPreci;
840       gp_Pnt tmpP3d;
841       Standard_Boolean tmpUIsoDeg;
842       //#77 rln S4135: using singularity which has minimum gap between singular point and input 3D point
843       Standard_Integer indMin = -1;
844       Standard_Real gapMin2 = RealLast();
845       for (Standard_Integer i = 1; i <= mySurf->NbSingularities (precVtx); i++) {
846         mySurf->Singularity (i, tmpPreci, tmpP3d, p2d1, p2d2, par1, par2, tmpUIsoDeg);
847         Standard_Real gap2 = p1.SquareDistance (tmpP3d);
848         if (gap2 <= precVtx * precVtx)
849           if (gapMin2 > gap2) {
850             gapMin2 = gap2;
851             indMin = i;
852           }
853       }
854       if (indMin >= 1) {
855         mySurf->Singularity (indMin, tmpPreci, tmpP3d, p2d1, p2d2, par1, par2, tmpUIsoDeg);
856         lack = Standard_True;
857       }
858     }
859
860     //rln S4135 if ( prec > myPrecision ) mySurf->ComputeSingularities ( myPrecision ); //:51
861   }
862
863   //  voila, on a soit dgnr soit lack
864   if ( ! lack && ! dgnr ) {
865     //:abv 29.08.01: if singularity not detected but edge is marked 
866     // as degenerated, report fail
867     if ( BRep_Tool::Degenerated ( E2 ) && ! sae.HasPCurve ( E2, Face() ) )
868       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
869     return Standard_False;
870   }
871   
872   // OK, degenerated case detected; we will find its start and end in 2d
873   
874   if ( lack ) forward = Standard_True;
875
876   //:24 by abv 28 Nov 97: 
877   // make degenerative pcurve parametrized exactly from end of pcurve of the 
878   // previous edge to the start of the next one
879   if ( lack || n1 != n2 ) { //:i8 abv 18 Sep 98: ProSTEP TR9 r0501-ug.stp #182180: single degedge is a wire at apex of a cone
880     Standard_Real a, b;
881     Handle(Geom2d_Curve) c2d;
882     if ( sae.PCurve ( E1, myFace, c2d, a, b, Standard_True ) ) {
883       p2d1 = c2d->Value ( b );
884       //#84 rln gp_Pnt2d p2d = c2d->Value ( b );
885       //#84 rln par1 = ( p2d.XY() - aP2d.XY() ) * theDir2d.XY();
886     }
887     else myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
888     //pdn pcurves (fixing regression in f0 in degenerated case) 
889     if ( sae.PCurve ( ( dgnr ? E3 : E2 ), myFace, c2d, a, b, Standard_True ) ) {
890       p2d2 = c2d->Value ( a );
891       //#84 rln gp_Pnt2d p2d = c2d->Value ( a );
892       //#84 rln par2 = ( p2d.XY() - aP2d.XY() ) * theDir2d.XY();
893     }
894     else myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
895   }
896 /*  
897   if ( par2 < par1 ) {
898     par1 = -par1;
899     par2 = -par2;
900     theDir2d.Reverse();
901   }
902 */
903   
904   //#84 rln 18.03.99 if pcurve is not degenerate anymore, the fix is postponned
905   //to ShapeFix_Wire::FixLacking
906   if ( ! mySurf->IsDegenerated ( p2d1, p2d2, precVtx, 10. ) ) { //:s1 abv 22 Apr 99: PRO7226 #489490 //smh#9
907     //:abv 24.05.02: OCC320 - fail (to remove edge) if two consequtive degenerated edges w/o pcurves
908     if ( BRep_Tool::Degenerated ( E2 ) )
909       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
910     return Standard_False;
911   }
912
913   //added by rln 18/12/97 CSR# CTS18544 entity 13638
914   //the situation when degenerated edge already exists but flag is not set
915   //(i.e. the parametric space is closed)
916   GeomAdaptor_Surface& Ads = mySurf->Adaptor3d()->ChangeSurface();
917   Standard_Real max = Max ( Ads.UResolution(myPrecision), 
918                             Ads.VResolution(myPrecision) );
919   if ( p2d1.Distance (p2d2) /*Abs (par1 - par2)*/ <= max + gp::Resolution() ) return Standard_False;
920
921   //#84 rln p2d1 = aP2d.XY() + par1 * theDir2d.XY();
922   //#84 rln p2d2 = aP2d.XY() + par2 * theDir2d.XY();
923   myStatus = ShapeExtend::EncodeStatus ( dgnr ? ShapeExtend_DONE2 : ShapeExtend_DONE1 );
924   return Standard_True;
925 }
926
927 //=======================================================================
928 //function : CheckDegenerated
929 //purpose  : 
930 //=======================================================================
931
932 Standard_Boolean ShapeAnalysis_Wire::CheckDegenerated (const Standard_Integer num)
933 {
934   gp_Pnt2d p2d1, p2d2;
935   return CheckDegenerated ( num, p2d1, p2d2 );
936 }
937
938 //=======================================================================
939 //function : CheckGap3d
940 //purpose  : 
941 //=======================================================================
942
943  Standard_Boolean ShapeAnalysis_Wire::CheckGap3d(const Standard_Integer num) 
944 {
945   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
946   //szv#4:S4163:12Mar99 optimized
947   if ( !IsLoaded() || NbEdges() < 1 ) return Standard_False; //szvsh was nbedges < 2
948   Standard_Integer n2 = ( num >0 ? num  : NbEdges() );
949   Standard_Integer n1 = ( n2  >1 ? n2-1 : NbEdges() );
950   TopoDS_Edge E1 = myWire->Edge(n1);
951   TopoDS_Edge E2 = myWire->Edge(n2);
952   Standard_Real uf1,ul1,uf2,ul2;
953   Handle(Geom_Curve) C1,C2;
954   ShapeAnalysis_Edge SAE;
955   if (!SAE.Curve3d (E1,C1,uf1,ul1) || !SAE.Curve3d (E2,C2,uf2,ul2)) {
956     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
957     return Standard_False;
958   }
959   gp_Pnt p1 = C1->Value (ul1);
960   gp_Pnt p2 = C2->Value (uf2);
961   myMin3d = myMax3d = p1.Distance (p2);
962   if (myMin3d > myPrecision)
963     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
964   return LastCheckStatus (ShapeExtend_DONE);
965 }
966
967 //=======================================================================
968 //function : CheckGap2d
969 //purpose  : 
970 //=======================================================================
971
972  Standard_Boolean ShapeAnalysis_Wire::CheckGap2d(const Standard_Integer num) 
973 {
974   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
975   //szv#4:S4163:12Mar99 optimized
976   if ( !IsReady() || NbEdges() < 1 ) return Standard_False; //szvsh was nbedges < 2
977   Standard_Integer n2 = ( num >0 ? num  : NbEdges() );
978   Standard_Integer n1 = ( n2  >1 ? n2-1 : NbEdges() );
979   TopoDS_Edge E1 = myWire->Edge(n1);
980   TopoDS_Edge E2 = myWire->Edge(n2);
981   Standard_Real uf1,ul1,uf2,ul2;
982   Handle(Geom2d_Curve) C1,C2;
983   ShapeAnalysis_Edge SAE;
984   if (!SAE.PCurve (E1,myFace,C1,uf1,ul1) || !SAE.PCurve (E2,myFace,C2,uf2,ul2)) {
985     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
986     return Standard_False;
987   }
988   gp_Pnt2d p1 = C1->Value (ul1);
989   gp_Pnt2d p2 = C2->Value (uf2);
990   myMin2d = myMax2d = p1.Distance (p2);
991   GeomAdaptor_Surface& SA = mySurf->Adaptor3d()->ChangeSurface();
992   if (myMin2d > (Max (SA.UResolution (myPrecision), SA.VResolution (myPrecision)) + Precision::PConfusion()))
993     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
994   return LastCheckStatus (ShapeExtend_DONE);
995 }
996
997 //=======================================================================
998 //function : CheckCurveGap
999 //purpose  : 
1000 //=======================================================================
1001
1002  Standard_Boolean ShapeAnalysis_Wire::CheckCurveGap(const Standard_Integer num)
1003 {
1004   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1005   if ( !IsLoaded() || NbEdges() < 1 ) return Standard_False;
1006   Standard_Integer n = ( num >0 ? num  : NbEdges() );
1007   TopoDS_Edge E = myWire->Edge(n);
1008   Standard_Real cuf,cul,pcuf,pcul;
1009   Handle(Geom_Curve) c;
1010   ShapeAnalysis_Edge SAE;
1011   if (!SAE.Curve3d (E,c,cuf,cul,Standard_False)) {
1012     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1013     return Standard_False;
1014   }
1015   Handle(Geom2d_Curve) pc;
1016   if (!SAE.PCurve (E,myFace,pc,pcuf,pcul,Standard_False)) {
1017     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1018     return Standard_False;
1019   }
1020   Handle(Geom2dAdaptor_HCurve) AC = new Geom2dAdaptor_HCurve(pc,pcuf,pcul);
1021   Handle(GeomAdaptor_HSurface) AS = new GeomAdaptor_HSurface(mySurf->Surface());
1022   Adaptor3d_CurveOnSurface ACS(AC,AS);
1023   gp_Pnt cpnt, pcpnt;
1024   Standard_Integer nbp = 45;
1025   Standard_Real dist, maxdist=0.;
1026   for (Standard_Integer i=0; i<nbp; i++) {
1027     cpnt = c->Value(cuf + i*(cul-cuf)/(nbp-1));
1028     pcpnt = ACS.Value(pcuf + i*(pcul-pcuf)/(nbp-1));
1029     dist = cpnt.SquareDistance(pcpnt);
1030     if (maxdist<dist) maxdist = dist;
1031   }
1032   myMin3d = myMax3d = Sqrt(maxdist);
1033   if (myMin3d > myPrecision)
1034     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1035   return LastCheckStatus (ShapeExtend_DONE);
1036 }
1037
1038 //=======================================================================
1039 //function : CheckSelfIntersectingEdge
1040 //purpose  : 
1041 //=======================================================================
1042
1043 // auxiliary function
1044 //:h0 abv 29 May 98: PRO10105 1949: like in BRepCheck, point is to be taken 
1045 // from 3d curve (but only if edge is SameParameter)
1046 static gp_Pnt GetPointOnEdge ( const TopoDS_Edge &edge, 
1047                                const Handle(ShapeAnalysis_Surface) &surf,
1048                                const Handle(Geom2d_Curve) &Crv2d, 
1049                                const Standard_Real param )
1050 {
1051   if ( BRep_Tool::SameParameter ( edge ) ) {
1052     Standard_Real f,l;
1053     TopLoc_Location L;
1054     const Handle(Geom_Curve) ConS = BRep_Tool::Curve ( edge, L, f, l );
1055     if ( ! ConS.IsNull() )
1056       return ConS->Value ( param ).Transformed ( L.Transformation() );
1057   }
1058   return surf->Value ( Crv2d->Value ( param ) );
1059 }
1060
1061 //=======================================================================
1062 //function : CheckSelfIntersectingEdge
1063 //purpose  : 
1064 //=======================================================================
1065
1066 Standard_Boolean ShapeAnalysis_Wire::CheckSelfIntersectingEdge (const Standard_Integer num,
1067                                                                 IntRes2d_SequenceOfIntersectionPoint& points2d,
1068                                                                 TColgp_SequenceOfPnt& points3d) 
1069 {
1070   points2d.Clear();
1071   points3d.Clear();
1072   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1073   if ( ! IsReady() ) return Standard_False;
1074
1075   TopoDS_Edge edge = WireData()->Edge ( num >0 ? num : NbEdges() );
1076   ShapeAnalysis_Edge sae;
1077
1078   Standard_Real a, b;
1079   Handle(Geom2d_Curve) Crv;
1080   if ( ! sae.PCurve ( edge, myFace, Crv, a, b, Standard_False ) ) {
1081     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1082     return Standard_False;
1083   }
1084   if ( Abs ( a - b ) <= ::Precision::PConfusion() ) return Standard_False;
1085   
1086   Standard_Real tolint = 1.0e-10; 
1087   //szv#4:S4163:12Mar99 warning
1088   IntRes2d_Domain domain ( Crv->Value ( a ), a, tolint, Crv->Value ( b ), b, tolint );
1089   Geom2dAdaptor_Curve AC ( Crv );
1090   Geom2dInt_GInter Inter ( AC, domain, tolint, tolint );
1091
1092   if ( ! Inter.IsDone() ) return Standard_False;
1093
1094   TopoDS_Vertex V1 = sae.FirstVertex ( edge );
1095   TopoDS_Vertex V2 = sae.LastVertex ( edge );
1096   if ( V1.IsNull() || V2.IsNull() ) {
1097     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1098     return Standard_False;
1099   }
1100   Standard_Real tol1 = BRep_Tool::Tolerance ( V1 );
1101   Standard_Real tol2 = BRep_Tool::Tolerance ( V2 );
1102
1103   gp_Pnt pnt1 = BRep_Tool::Pnt ( V1 );
1104   gp_Pnt pnt2 = BRep_Tool::Pnt ( V2 );
1105
1106   for ( Standard_Integer i=1; i <= Inter.NbPoints(); i++ ) {
1107     const IntRes2d_IntersectionPoint &IP = Inter.Point ( i );
1108     const IntRes2d_Transition &Tr1 = IP.TransitionOfFirst();
1109     const IntRes2d_Transition &Tr2 = IP.TransitionOfSecond();
1110     if ( Tr1.PositionOnCurve() != IntRes2d_Middle &&
1111          Tr2.PositionOnCurve() != IntRes2d_Middle ) continue;
1112     gp_Pnt pint = GetPointOnEdge ( edge, mySurf, Crv, IP.ParamOnFirst() );
1113     Standard_Real dist21 = pnt1.SquareDistance ( pint );
1114     Standard_Real dist22 = pnt2.SquareDistance ( pint );
1115     if ( dist21 > tol1 * tol1 && dist22 > tol2 * tol2 ) {
1116       points2d.Append ( IP );
1117       points3d.Append ( pint );
1118       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1119     }
1120   }
1121
1122   return LastCheckStatus ( ShapeExtend_DONE );
1123 }
1124
1125 //=======================================================================
1126 //function : CheckSelfIntersectingEdge
1127 //purpose  : 
1128 //=======================================================================
1129
1130 Standard_Boolean ShapeAnalysis_Wire::CheckSelfIntersectingEdge (const Standard_Integer num)
1131 {
1132   IntRes2d_SequenceOfIntersectionPoint points2d;
1133   TColgp_SequenceOfPnt points3d;
1134   return CheckSelfIntersectingEdge ( num, points2d, points3d );
1135 }
1136
1137 //=======================================================================
1138 //function : CheckIntersectingEdges
1139 //purpose  : Test if two consequent edges are intersecting 
1140 //           It is made in accordance with the following check in BRepCheck:
1141 //         - in BRepCheck_Wire::Orientation(), test for self-intersection
1142 //=======================================================================
1143
1144 Standard_Boolean ShapeAnalysis_Wire::CheckIntersectingEdges (const Standard_Integer num,
1145                                                              IntRes2d_SequenceOfIntersectionPoint& points2d,
1146                                                              TColgp_SequenceOfPnt& points3d,
1147                                                              TColStd_SequenceOfReal& errors)
1148 {
1149   points2d.Clear();
1150   points3d.Clear();
1151   errors.Clear();
1152   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1153   if ( ! IsReady() || NbEdges() <2 ) return Standard_False;
1154   
1155   //szv#4:S4163:12Mar99 optimized
1156   Standard_Integer n2 = (num > 0)? num : NbEdges();
1157   Standard_Integer n1 = (n2 > 1)? n2-1 : NbEdges();
1158   TopoDS_Edge edge1 = myWire->Edge ( n1 );
1159   TopoDS_Edge edge2 = myWire->Edge ( n2 );
1160
1161   ShapeAnalysis_Edge sae;
1162   TopoDS_Vertex V1 = sae.LastVertex ( edge1 );
1163   TopoDS_Vertex V2 = sae.FirstVertex ( edge2 );
1164   if ( V1.IsNull() || V2.IsNull() ) {
1165     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1166     return Standard_False;
1167   }
1168   if ( ! BRepTools::Compare ( V1, V2 ) ) {
1169     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1170     return Standard_False;
1171   }
1172
1173   TopoDS_Vertex Vp = sae.FirstVertex ( edge1 );
1174   TopoDS_Vertex Vn = sae.LastVertex ( edge2 );
1175
1176   Standard_Real a1, b1, a2, b2;
1177   Handle(Geom2d_Curve) Crv1, Crv2;
1178   if ( ! sae.PCurve ( edge1, myFace, Crv1, a1, b1, Standard_False ) ) {
1179     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1180     return Standard_False;
1181   }
1182   if ( ! sae.PCurve ( edge2, myFace, Crv2, a2, b2, Standard_False ) ) {
1183     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1184     return Standard_False;
1185   }
1186   if ( Abs ( a1 - b1 ) <= ::Precision::PConfusion() ||
1187        Abs ( a2 - b2 ) <= ::Precision::PConfusion() ) return Standard_False; //:f7 abv 6 May 98: BUC50070 on #42276
1188
1189   Standard_Boolean isForward1 = ( edge1.Orientation() == TopAbs_FORWARD );
1190   Standard_Boolean isForward2 = ( edge2.Orientation() == TopAbs_FORWARD );
1191
1192   Standard_Real tol0 = Max ( BRep_Tool::Tolerance ( V1 ), BRep_Tool::Tolerance ( V2 ) );
1193   Standard_Real tol = tol0;
1194
1195   gp_Pnt pnt = BRep_Tool::Pnt ( V1 );
1196
1197 //  Standard_Boolean Status = Standard_False;
1198
1199   Standard_Real tolint = 1.0e-10; 
1200
1201   //szv#4:S4163:12Mar99 warning
1202   IntRes2d_Domain d1 ( Crv1->Value ( a1 ), a1, tolint, 
1203                        Crv1->Value ( b1 ), b1, tolint );
1204   IntRes2d_Domain d2 ( Crv2->Value ( a2 ), a2, tolint, 
1205                        Crv2->Value ( b2 ), b2, tolint );
1206   Geom2dAdaptor_Curve C1 ( Crv1 ), C2 ( Crv2 );
1207
1208   //:64 abv 25 Dec 97: Attention!
1209   // Since Intersection algorithm is not symmetrical, for consistency with BRepCheck 
1210   // edge with lower order number shoud be intersecting with edge with higher one
1211   // i.e., for intersection of last and first edges, they should go in reversed order
1212   // Example: entity #38285 from bug CSR #CTS17806
1213   // NOTE: Tr1 and Tr2 are not reordered because they are used in the same manner
1214   Geom2dInt_GInter Inter;
1215   if ( num ==1 ) Inter.Perform ( C2, d2, C1, d1, tolint, tolint );
1216   else           Inter.Perform ( C1, d1, C2, d2, tolint, tolint );
1217   if ( ! Inter.IsDone() ) return Standard_False;
1218
1219   //:86 abv 22 Jan 98: fix self-intersection even if tolerance of vertex is enough
1220   // to annihilate it. This is done to prevent wrong effects if vertex tolerance 
1221   // will be decreased (e.g., in FixLacking)
1222   Standard_Real tole = Max ( ( BRep_Tool::SameParameter ( edge1 ) ? 
1223                                BRep_Tool::Tolerance ( edge1 ) : tol0 ),
1224                              ( BRep_Tool::SameParameter ( edge2 ) ? 
1225                                BRep_Tool::Tolerance ( edge2 ) : tol0 ) );
1226   Standard_Real tolt = Min ( tol, Max ( tole, myPrecision ) );
1227   //Standard_Real prevRange1 = RealLast(), prevRange2 = RealLast(); //SK
1228   Standard_Integer isLacking = -1; //:l0 abv: CATIA01 #1727: protect against adding lacking
1229   //#83 rln 19.03.99 sim2.igs, entity 4292
1230   //processing also segments as in BRepCheck
1231   Standard_Integer NbPoints = Inter.NbPoints(), NbSegments = Inter.NbSegments();
1232   for ( Standard_Integer i=1; i <= NbPoints + NbSegments; i++ ) {
1233     IntRes2d_IntersectionPoint IP;
1234     IntRes2d_Transition Tr1, Tr2;
1235     if (i <= NbPoints)
1236       IP = Inter.Point ( i );
1237     else {
1238       const IntRes2d_IntersectionSegment &Seg = Inter.Segment ( i - NbPoints );
1239       if (!Seg.HasFirstPoint() || !Seg.HasLastPoint()) continue;
1240       IP = Seg.FirstPoint();
1241       Tr1 = IP.TransitionOfFirst();
1242       Tr2 = IP.TransitionOfSecond();
1243       if (Tr1.PositionOnCurve() == IntRes2d_Middle || Tr2.PositionOnCurve() == IntRes2d_Middle)
1244         IP = Seg.LastPoint();
1245     }
1246     Tr1 = IP.TransitionOfFirst();
1247     Tr2 = IP.TransitionOfSecond();      
1248
1249     if ( Tr1.PositionOnCurve() != IntRes2d_Middle &&
1250          Tr2.PositionOnCurve() != IntRes2d_Middle ) continue;
1251     Standard_Real param1, param2;
1252     param1 = ( num ==1 ? IP.ParamOnSecond() : IP.ParamOnFirst() ); 
1253     param2 = ( num ==1 ? IP.ParamOnFirst()  : IP.ParamOnSecond() );
1254     
1255     //:r6 abv 8 Apr 99: r_47-sd.stp #173850: protect against working out of curve range
1256     if ( a1-param1 > ::Precision::PConfusion() || 
1257          param1-b1 > ::Precision::PConfusion() || 
1258          a2-param2 > ::Precision::PConfusion() || 
1259          param2-b2 > ::Precision::PConfusion() ) continue;
1260
1261     //:82 abv 21 Jan 98: point of intersection on Crv1 and Crv2 is different
1262     gp_Pnt pi1 = GetPointOnEdge ( edge1, mySurf, Crv1, param1 ); //:h0: thesurf.Value ( Crv1->Value ( param1 ) );
1263     gp_Pnt pi2 = GetPointOnEdge ( edge2, mySurf, Crv2, param2 ); //:h0: thesurf.Value ( Crv2->Value ( param2 ) );
1264     gp_Pnt pint = 0.5 * ( pi1.XYZ() + pi2.XYZ() );
1265     Standard_Real di1 = pi1.SquareDistance ( pnt );
1266     Standard_Real di2 = pi2.SquareDistance ( pnt );
1267     Standard_Real dist2 = Max ( di1, di2 );
1268
1269     //rln 03/02/98: CSR#BUC50004 entity 56 (to avoid later inserting lacking edge)
1270     if ( isLacking <0 ) { //:l0
1271       gp_Pnt2d end1 = Crv1->Value ( isForward1 ? b1 : a1 );
1272       gp_Pnt2d end2 = Crv2->Value ( isForward2 ? a2 : b2 );
1273 //:l0      Standard_Real distab2 = mySurf->Value ( end1 ).SquareDistance ( mySurf->Value ( end2 ) );
1274       //:l0: test like in BRepCheck
1275       GeomAdaptor_Surface& Ads = mySurf->Adaptor3d()->ChangeSurface();
1276       Standard_Real tol2d = 2 * Max ( Ads.UResolution(tol), Ads.VResolution(tol) );
1277       isLacking = ( end1.SquareDistance(end2) >= tol2d * tol2d );
1278     }
1279       
1280     if ( ( dist2 > tolt * tolt || //:86: tol -> tolt
1281            isLacking ) && //:l0
1282 //:l0      distab2 > BRep_Tool::Tolerance ( edge1 ) + BRep_Tool::Tolerance ( edge2 ) ) && //rln
1283          ( ! BRepTools::Compare ( Vp, Vn ) ||                          //:63
1284            dist2 < pint.SquareDistance ( BRep_Tool::Pnt ( Vp ) ) ) ) { //:63
1285       points2d.Append ( IP );
1286       points3d.Append ( pint );
1287       errors.Append ( 0.5 * pi1.Distance ( pi2 ) );
1288       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1289     }
1290   }
1291
1292   return LastCheckStatus ( ShapeExtend_DONE );
1293 }
1294
1295 //=======================================================================
1296 //function : CheckIntersectingEdges
1297 //purpose  : 
1298 //=======================================================================
1299
1300 Standard_Boolean ShapeAnalysis_Wire::CheckIntersectingEdges (const Standard_Integer num)
1301 {
1302   IntRes2d_SequenceOfIntersectionPoint points2d;
1303   TColgp_SequenceOfPnt points3d;
1304   TColStd_SequenceOfReal errors;
1305   return CheckIntersectingEdges ( num, points2d, points3d, errors );
1306 }
1307
1308 //=======================================================================
1309 //function : CheckIntersectingEdges
1310 //purpose  : 
1311 //=======================================================================
1312
1313 Standard_Boolean ShapeAnalysis_Wire::CheckIntersectingEdges(const Standard_Integer num1,
1314                                                             const Standard_Integer num2,
1315                                                             IntRes2d_SequenceOfIntersectionPoint& points2d,
1316                                                             TColgp_SequenceOfPnt& points3d,
1317                                                             TColStd_SequenceOfReal& errors)
1318 {
1319   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1320   if ( ! IsReady() ) return Standard_False;
1321   Handle(ShapeExtend_WireData) sbwd = WireData();
1322   Standard_Integer n2 = ( num2 >0 ? num2  : sbwd->NbEdges() );
1323   Standard_Integer n1 = ( num1 >0 ? num1  : sbwd->NbEdges() );
1324   
1325   TopoDS_Edge edge1 = sbwd->Edge ( n1 );
1326   TopoDS_Edge edge2 = sbwd->Edge ( n2 );
1327   
1328   ShapeAnalysis_Edge sae;
1329   Standard_Real a1, b1, a2, b2;
1330   Handle(Geom2d_Curve) Crv1, Crv2;
1331   if(!sae.PCurve ( edge1, myFace, Crv1, a1, b1, Standard_False )){
1332     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1333     return Standard_False;
1334   }
1335     
1336   if(!sae.PCurve ( edge2, myFace, Crv2, a2, b2, Standard_False )){
1337     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1338     return Standard_False;
1339   }
1340   
1341   if ( Abs ( a1 - b1 ) <= ::Precision::PConfusion() ||
1342        Abs ( a2 - b2 ) <= ::Precision::PConfusion() ) return Standard_False;
1343   
1344   points2d.Clear();
1345   points3d.Clear();
1346   errors.Clear();
1347   TColgp_Array1OfPnt vertexPoints(1,4);
1348   TColStd_Array1OfReal vertexTolers(1,4);
1349   vertexPoints(1) = BRep_Tool::Pnt(sae.FirstVertex(edge1));
1350   vertexTolers(1) = BRep_Tool::Tolerance(sae.FirstVertex(edge1));
1351   vertexPoints(2) = BRep_Tool::Pnt(sae.LastVertex(edge1));
1352   vertexTolers(2) = BRep_Tool::Tolerance(sae.LastVertex(edge1));
1353   vertexPoints(3) = BRep_Tool::Pnt(sae.FirstVertex(edge2));
1354   vertexTolers(3) = BRep_Tool::Tolerance(sae.FirstVertex(edge2));
1355   vertexPoints(4) = BRep_Tool::Pnt(sae.LastVertex(edge2));
1356   vertexTolers(4) = BRep_Tool::Tolerance(sae.LastVertex(edge2));
1357   
1358   Standard_Real tolint = 1.0e-10; 
1359
1360   IntRes2d_Domain d1 ( Crv1->Value ( a1 ), a1, tolint, 
1361                        Crv1->Value ( b1 ), b1, tolint );
1362   IntRes2d_Domain d2 ( Crv2->Value ( a2 ), a2, tolint, 
1363                        Crv2->Value ( b2 ), b2, tolint );
1364   Geom2dAdaptor_Curve C1 ( Crv1 ), C2 ( Crv2 );
1365   
1366   Geom2dInt_GInter Inter;
1367   Inter.Perform ( C1, d1, C2, d2, tolint, tolint );
1368   if ( ! Inter.IsDone() ) return Standard_False;
1369   
1370   //#83 rln 19.03.99 sim2.igs, entity 4292
1371   //processing also segments as in BRepCheck
1372   Standard_Integer NbPoints = Inter.NbPoints(), NbSegments = Inter.NbSegments();
1373   for ( Standard_Integer i=1; i <= NbPoints + NbSegments; i++ ) {
1374     IntRes2d_IntersectionPoint IP;
1375     IntRes2d_Transition Tr1, Tr2;
1376     if (i <= NbPoints)
1377       IP = Inter.Point ( i );
1378     else {
1379       const IntRes2d_IntersectionSegment &Seg = Inter.Segment ( i - NbPoints );
1380       if (!Seg.HasFirstPoint() || !Seg.HasLastPoint()) continue;
1381       IP = Seg.FirstPoint();
1382       Tr1 = IP.TransitionOfFirst();
1383       Tr2 = IP.TransitionOfSecond();
1384       if (Tr1.PositionOnCurve() == IntRes2d_Middle || Tr2.PositionOnCurve() == IntRes2d_Middle)
1385         IP = Seg.LastPoint();
1386     }
1387     Tr1 = IP.TransitionOfFirst();
1388     Tr2 = IP.TransitionOfSecond();      
1389     if ( Tr1.PositionOnCurve() != IntRes2d_Middle &&
1390          Tr2.PositionOnCurve() != IntRes2d_Middle ) continue;
1391     Standard_Real param1 = IP.ParamOnFirst(); 
1392     Standard_Real param2 = IP.ParamOnSecond();
1393     gp_Pnt pi1 = GetPointOnEdge ( edge1, mySurf, Crv1, param1 ); //:h0: thesurf.Value ( Crv1->Value ( param1 ) );
1394     gp_Pnt pi2 = GetPointOnEdge ( edge2, mySurf, Crv2, param2 );
1395     Standard_Boolean OK = Standard_False;
1396     for(Standard_Integer j=1; (j<=4)&&!OK; j++) {
1397       Standard_Real di1 = pi1.SquareDistance (vertexPoints(j));
1398       Standard_Real di2 = pi2.SquareDistance (vertexPoints(j));
1399       Standard_Real dist2 = Max ( di1, di2 );
1400       if(dist2 < vertexTolers(j) * vertexTolers(j))
1401         OK = Standard_True;
1402     }
1403     if(!OK) {
1404       gp_Pnt pint = 0.5 * ( pi1.XYZ() + pi2.XYZ() );
1405       points2d.Append ( IP );
1406       points3d.Append ( pint );
1407       errors.Append ( 0.5 * pi1.Distance ( pi2 ) );
1408       myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE1);   
1409     }
1410   }
1411   return LastCheckStatus ( ShapeExtend_DONE );
1412 }
1413
1414 //=======================================================================
1415 //function : CheckIntersectingEdges
1416 //purpose  : 
1417 //=======================================================================
1418
1419 Standard_Boolean ShapeAnalysis_Wire::CheckIntersectingEdges (const Standard_Integer num1,
1420                                                              const Standard_Integer num2)
1421 {
1422   IntRes2d_SequenceOfIntersectionPoint points2d;
1423   TColgp_SequenceOfPnt points3d;
1424   TColStd_SequenceOfReal errors;
1425   return CheckIntersectingEdges(num1, num2, points2d, points3d, errors);
1426 }
1427
1428 //=======================================================================
1429 //function : CheckLacking
1430 //purpose  : Test if two edges are disconnected in 2d according to the 
1431 //           Adaptor_Surface::Resolution
1432 //=======================================================================
1433
1434 Standard_Boolean ShapeAnalysis_Wire::CheckLacking (const Standard_Integer num,
1435                                                    const Standard_Real Tolerance,
1436                                                    gp_Pnt2d &p2d1, gp_Pnt2d &p2d2) 
1437 {
1438   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1439   if ( ! IsReady() ) return Standard_False;
1440   
1441   //szv#4:S4163:12Mar99 optimized
1442   Standard_Integer n2 = (num > 0)? num : NbEdges();
1443   Standard_Integer n1 = (n2 > 1)? n2-1 : NbEdges();
1444   TopoDS_Edge E1 = myWire->Edge ( n1 );
1445   TopoDS_Edge E2 = myWire->Edge ( n2 );
1446   
1447   ShapeAnalysis_Edge sae;
1448   TopoDS_Vertex V1 = sae.LastVertex ( E1 );
1449   TopoDS_Vertex V2 = sae.FirstVertex ( E2 );
1450   // CKY 4 MAR 1998 : protection against null vertex
1451   if ( V1.IsNull() || V2.IsNull() ) {
1452     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1453     return Standard_False;
1454   }
1455   if ( ! BRepTools::Compare ( V1, V2 ) ) {
1456     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1457     return Standard_False;
1458   }
1459
1460   Standard_Real a, b;
1461   gp_Vec2d v1, v2, v12;
1462   Handle(Geom2d_Curve) c2d;
1463   if ( ! sae.PCurve ( E1, myFace, c2d, a, b, Standard_True ) ) {
1464     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1465     return Standard_False;
1466   }
1467   c2d->D1 ( b, p2d1, v1 ); 
1468   if ( E1.Orientation() == TopAbs_REVERSED ) v1.Reverse();
1469   if ( ! sae.PCurve ( E2, myFace, c2d, a, b, Standard_True ) ) {
1470     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1471     return Standard_False;
1472   }
1473   c2d->D1 ( a, p2d2, v2 );
1474   if ( E2.Orientation() == TopAbs_REVERSED ) v2.Reverse();
1475   v12 = p2d2.XY() - p2d1.XY();
1476   myMax2d = v12.SquareMagnitude();
1477
1478   // test like in BRepCheck
1479   Standard_Real tol = Max ( BRep_Tool::Tolerance ( V1 ), BRep_Tool::Tolerance ( V2 ) );
1480   tol = ( Tolerance > gp::Resolution() && Tolerance < tol ? Tolerance : tol );
1481   GeomAdaptor_Surface& Ads = mySurf->Adaptor3d()->ChangeSurface();
1482   Standard_Real tol2d = 2 * Max ( Ads.UResolution(tol), Ads.VResolution(tol) );
1483   if ( // tol2d < gp::Resolution() || //#2 smh 26.03.99 S4163 Zero divide
1484        myMax2d < tol2d * tol2d ) return Standard_False;
1485
1486   myMax2d = Sqrt ( myMax2d );
1487   myMax3d = tol * myMax2d / Max ( tol2d, gp::Resolution() );
1488   myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1489
1490   if ( myMax2d < Precision::PConfusion() || //:abv 03.06.02 CTS21866.stp
1491        ( v1.SquareMagnitude() > gp::Resolution() && Abs ( v12.Angle ( v1 ) ) > 0.9 * M_PI ) ||
1492        ( v2.SquareMagnitude() > gp::Resolution() && Abs ( v12.Angle ( v2 ) ) > 0.9 * M_PI ) ) 
1493        myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1494   return Standard_True;
1495 }
1496
1497 //=======================================================================
1498 //function : CheckLacking
1499 //purpose  :
1500 //          
1501 //=======================================================================
1502
1503 Standard_Boolean ShapeAnalysis_Wire::CheckLacking (const Standard_Integer num,
1504                                                    const Standard_Real Tolerance)
1505 {
1506   gp_Pnt2d p1, p2;
1507   return CheckLacking (num, Tolerance, p1, p2);
1508 }
1509
1510 //=======================================================================
1511 //function : CheckOuterBound
1512 //purpose  : 
1513 //=======================================================================
1514
1515  Standard_Boolean ShapeAnalysis_Wire::CheckOuterBound(const Standard_Boolean APIMake)
1516 {
1517   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1518   if ( ! IsReady() ) return Standard_False;
1519
1520   TopoDS_Wire wire;
1521   if (APIMake) wire = myWire->WireAPIMake();
1522   else         wire = myWire->Wire();
1523   
1524   TopoDS_Shape sh = myFace.EmptyCopied(); //szv#4:S4163:12Mar99 SGI warns
1525   TopoDS_Face face = TopoDS::Face(sh);
1526   BRep_Builder B;
1527   B.Add (face, wire);
1528   if (ShapeAnalysis::IsOuterBound (face)) return Standard_False;
1529   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1530   return Standard_True;
1531 }
1532
1533 //=======================================================================
1534 //function : CheckNotchedEdges
1535 //purpose  : 
1536 //=======================================================================
1537
1538 static Standard_Real ProjectInside(const Adaptor3d_CurveOnSurface AD,
1539                                    const gp_Pnt pnt,
1540                                    const Standard_Real preci,
1541                                    gp_Pnt& proj,
1542                                    Standard_Real& param,
1543                                    const Standard_Boolean adjustToEnds = Standard_True)
1544 {
1545   ShapeAnalysis_Curve sac;
1546   Standard_Real dist = sac.Project(AD,pnt,preci,proj,param,adjustToEnds);
1547   Standard_Real uFirst = AD.FirstParameter();
1548   Standard_Real uLast = AD.LastParameter();
1549   if(param<uFirst) {
1550     param = uFirst;
1551     proj = AD.Value(uFirst);
1552     return proj.Distance(pnt);
1553   }
1554   
1555   if(param>uLast) {
1556     param = uLast;
1557     proj = AD.Value(uLast);
1558     return proj.Distance(pnt);
1559   }
1560   return dist;
1561 }
1562   
1563 Standard_Boolean ShapeAnalysis_Wire::CheckNotchedEdges(const Standard_Integer num,
1564                                                        Standard_Integer& shortNum,
1565                                                        Standard_Real& param,
1566                                                        const Standard_Real Tolerance)
1567 {
1568   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1569   if ( ! IsReady() ) return Standard_False;
1570   
1571   Standard_Integer n2 = (num > 0)? num : NbEdges();
1572   Standard_Integer n1 = (n2 > 1)? n2-1 : NbEdges();
1573   TopoDS_Edge E1 = myWire->Edge ( n1 );
1574   TopoDS_Edge E2 = myWire->Edge ( n2 );
1575   
1576   if(BRep_Tool::Degenerated(E1)||BRep_Tool::Degenerated(E2))
1577     return Standard_False;
1578   
1579   ShapeAnalysis_Edge sae;
1580   TopoDS_Vertex V1 = sae.LastVertex ( E1 );
1581   TopoDS_Vertex V2 = sae.FirstVertex ( E2 );
1582   
1583   if ( V1.IsNull() || V2.IsNull() ) {
1584     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1585     return Standard_False;
1586   }
1587   if ( ! BRepTools::Compare ( V1, V2 ) ) {
1588     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1589     return Standard_False;
1590   }
1591
1592   Standard_Real a1, b1, a2, b2;
1593   gp_Pnt2d p2d1, p2d2;
1594   gp_Vec2d v1, v2;
1595   Handle(Geom2d_Curve) c2d1, c2d2;
1596   if ( ! sae.PCurve ( E1, myFace, c2d1, a1, b1, Standard_False ) ) {
1597     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1598     return Standard_False;
1599   }
1600   
1601   if(E1.Orientation()==TopAbs_REVERSED)
1602     c2d1->D1 ( a1, p2d1, v1 );
1603   else {
1604     c2d1->D1 ( b1, p2d1, v1 );
1605     v1.Reverse();
1606   }
1607   
1608   if ( ! sae.PCurve ( E2, myFace, c2d2, a2, b2, Standard_False ) ) {
1609     myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_FAIL3);
1610     return Standard_False;
1611   }
1612   if(E2.Orientation()==TopAbs_REVERSED) {
1613     c2d2->D1 ( b2, p2d2, v2 );
1614     v2.Reverse();
1615   }
1616   else 
1617     c2d2->D1 ( a2, p2d2, v2 );
1618   
1619   if ( v2.Magnitude() < gp::Resolution() || v1.Magnitude() < gp::Resolution())
1620     return Standard_False;
1621   
1622   if ( Abs ( v2.Angle ( v1 ) ) > 0.1 || p2d1.Distance(p2d2) > Tolerance)
1623     return Standard_False;
1624   
1625   Handle(Geom2dAdaptor_HCurve) AC2d1  = new Geom2dAdaptor_HCurve(c2d1,a1,b1);
1626   Handle(GeomAdaptor_HSurface) AdS1 = new GeomAdaptor_HSurface(new Geom_Plane(gp_Pln()));
1627   Adaptor3d_CurveOnSurface Ad1(AC2d1,AdS1);
1628   
1629   Handle(Geom2dAdaptor_HCurve) AC2d2  = new Geom2dAdaptor_HCurve(c2d2,a2,b2);
1630   Handle(GeomAdaptor_HSurface) AdS2 = new GeomAdaptor_HSurface(new Geom_Plane(gp_Pln()));
1631   Adaptor3d_CurveOnSurface Ad2(AC2d2,AdS2);
1632   
1633   Adaptor3d_CurveOnSurface longAD, shortAD;
1634   Standard_Real lenP, firstP;
1635   
1636   ShapeAnalysis_Curve sac;
1637   
1638   gp_Pnt Proj1, Proj2;
1639   Standard_Real param1 = 0., param2 = 0.;
1640   p2d2=c2d2->Value(E2.Orientation()==TopAbs_FORWARD ? b2 : a2);
1641   p2d1=c2d1->Value(E1.Orientation()==TopAbs_FORWARD ? a1 : b1);
1642   Standard_Real dist1 = ProjectInside(Ad1,gp_Pnt(p2d2.X(),p2d2.Y(),0),Tolerance,Proj1,param1,Standard_False);
1643   Standard_Real dist2 = ProjectInside(Ad2,gp_Pnt(p2d1.X(),p2d1.Y(),0),Tolerance,Proj2,param2,Standard_False);
1644   
1645   if ( dist1 > Tolerance && dist2 > Tolerance)
1646     return Standard_False;
1647   
1648   if (dist1 < dist2 ) {
1649     shortAD = Ad2;
1650     longAD = Ad1;
1651     lenP = b2 - a2;
1652     firstP = a2;
1653     shortNum=n2;
1654     param=param1;
1655     
1656   }
1657   else {
1658     shortAD = Ad1;
1659     longAD = Ad2;
1660     lenP = b1 - a1;
1661     firstP = a1;
1662     shortNum=n1;
1663     param=param2;
1664   }
1665   
1666   Standard_Real step = lenP/23;
1667   for (Standard_Integer i = 1; i < 23; i++,firstP+=step) {
1668     Standard_Real d1 = sac.Project(longAD,shortAD.Value(firstP),Tolerance,Proj1,param1);
1669     if (d1 > Tolerance) {
1670       return Standard_False;
1671     }
1672   }
1673   
1674   return Standard_True; 
1675 }
1676
1677 //=======================================================================
1678 //function : CheckSmallArea
1679 //purpose  : 
1680 //=======================================================================
1681
1682 Standard_Boolean ShapeAnalysis_Wire::CheckSmallArea(const Standard_Real prec2d)
1683 {
1684   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1685   Standard_Integer NbEdges = myWire->NbEdges();
1686   if ( !IsReady() || NbEdges <1 ) return Standard_False;
1687   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
1688   
1689   Standard_Integer NbControl=23;
1690   Standard_Real area=0;
1691   gp_XY prev, cont;
1692   for (Standard_Integer nbe = 1; nbe <= NbEdges; nbe++) {
1693     Standard_Real First, Last;
1694     Handle(Geom2d_Curve) c2d;
1695     ShapeAnalysis_Edge sae;
1696     if (!sae.PCurve(myWire->Edge(nbe),myFace,c2d,First,Last)) {
1697       myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1698       return Standard_False;
1699     }
1700     
1701     Standard_Integer ibeg = 0;
1702     if( nbe == 1 ) {
1703       gp_Pnt2d pntIni = c2d->Value(First);
1704       prev = pntIni.XY();
1705       cont = prev;
1706       ibeg = 1;
1707     }
1708     for ( Standard_Integer i = ibeg; i < NbControl; i++) {
1709       Standard_Real prm = ((NbControl-1-i)*First + i*Last)/(NbControl-1);
1710       gp_Pnt2d pntCurr = c2d->Value(prm);
1711       gp_XY curr = pntCurr.XY();
1712       area += curr ^ prev;
1713       prev = curr;
1714     }
1715   }
1716   area +=  cont ^ prev;
1717   if ( Abs(area) < 2*prec2d*prec2d ) {
1718     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1719     return Standard_True;
1720   }
1721   return Standard_False;
1722 }
1723
1724 //=======================================================================
1725 //function : CheckShapeConnect
1726 //purpose  : 
1727 //=======================================================================
1728
1729  Standard_Boolean ShapeAnalysis_Wire::CheckShapeConnect(const TopoDS_Shape& shape,const Standard_Real prec) 
1730 {
1731   Standard_Real tailhead, tailtail, headhead, headtail;
1732   return CheckShapeConnect (tailhead, tailtail, headtail, headhead, shape, prec);
1733 }
1734
1735 //=======================================================================
1736 //function : CheckShapeConnect
1737 //purpose  : 
1738 //=======================================================================
1739
1740  Standard_Boolean ShapeAnalysis_Wire::CheckShapeConnect(Standard_Real& tailhead, Standard_Real& tailtail,
1741                                                         Standard_Real& headtail, Standard_Real& headhead,
1742                                                         const TopoDS_Shape& shape, const Standard_Real prec) 
1743 {
1744     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL1);
1745   if (!IsLoaded () || shape.IsNull()) return Standard_False;
1746   TopoDS_Vertex V1,V2;
1747   TopoDS_Edge E;  TopoDS_Wire W;
1748   ShapeAnalysis_Edge SAE;
1749   if (shape.ShapeType() == TopAbs_EDGE) {
1750     E = TopoDS::Edge (shape);
1751     V1 = SAE.FirstVertex (E);    V2 = SAE.LastVertex  (E);
1752   } else if (shape.ShapeType() == TopAbs_WIRE) {
1753     W = TopoDS::Wire (shape);
1754     ShapeAnalysis::FindBounds (W,V1,V2); 
1755   }
1756   else return Standard_False;
1757   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1758 //  on va comparer les points avec ceux de thevfirst et thevlast
1759   gp_Pnt p1 = BRep_Tool::Pnt(V1);
1760   gp_Pnt p2 = BRep_Tool::Pnt(V2);
1761
1762   TopoDS_Vertex vfirst = SAE.FirstVertex (myWire->Edge (1)),
1763                 vlast  = SAE.LastVertex (myWire->Edge (NbEdges()));
1764   gp_Pnt pf = BRep_Tool::Pnt(vfirst);
1765   gp_Pnt pl = BRep_Tool::Pnt(vlast);
1766
1767   tailhead = p1.Distance(pl);
1768   tailtail = p2.Distance(pl);
1769   headhead = p1.Distance(pf);
1770   headtail = p2.Distance(pf);
1771   Standard_Real dm1 = tailhead, dm2 = headtail;
1772   Standard_Integer res1 = 0, res2 = 0;
1773
1774   if (tailhead > tailtail) {res1 = 1; dm1 = tailtail;}
1775   if (headtail > headhead) {res2 = 1; dm2 = headhead;}
1776   Standard_Integer result = res1;
1777   myMin3d = Min (dm1, dm2);
1778   myMax3d = Max (dm1, dm2);
1779   if (dm1 > dm2) {dm1 = dm2; result = res2 + 2;}
1780   switch (result) {
1781   case 1: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2); break;
1782   case 2: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE3); break;
1783   case 3: myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE4); break;
1784   }
1785   if (!res1) myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE5);
1786   if (!res2) myStatus |= ShapeExtend::EncodeStatus (ShapeExtend_DONE6);
1787
1788   if (myMin3d > Max (myPrecision, prec))
1789     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_FAIL2);
1790   return LastCheckStatus (ShapeExtend_DONE);
1791 }
1792
1793 //=======================================================================
1794 //function : CheckLoop
1795 //purpose  : 
1796 //=======================================================================
1797 Standard_Boolean isMultiVertex(const TopTools_ListOfShape& alshape,
1798                                const TopTools_MapOfShape& aMapSmallEdges,
1799                                const TopTools_MapOfShape& aMapSeemEdges)
1800 {
1801   TopTools_ListIteratorOfListOfShape lIt1(alshape);
1802   Standard_Integer nbNotAccount =0;
1803   
1804   for( ; lIt1.More() ; lIt1.Next()) 
1805   {
1806     if(aMapSmallEdges.Contains(lIt1.Value()))
1807       nbNotAccount++;
1808     else if(aMapSeemEdges.Contains(lIt1.Value()))
1809       nbNotAccount++;
1810   }
1811   return ((alshape.Extent() -nbNotAccount) >2); 
1812 }
1813  Standard_Boolean ShapeAnalysis_Wire::CheckLoop(TopTools_IndexedMapOfShape& aMapLoopVertices,
1814                                                 TopTools_DataMapOfShapeListOfShape& aMapVertexEdges,
1815                                                 TopTools_MapOfShape& aMapSmallEdges,
1816                                                 TopTools_MapOfShape& aMapSeemEdges) 
1817 {
1818   myStatus = ShapeExtend::EncodeStatus(ShapeExtend_OK);
1819   if (!IsLoaded() || NbEdges() < 2) return Standard_False;
1820   Standard_Real aSavPreci = Precision();
1821   SetPrecision(Precision::Infinite());
1822   Standard_Integer i =1;
1823  
1824   for( ; i <= myWire->NbEdges(); i++) {
1825     TopoDS_Edge aedge = myWire->Edge(i);
1826     TopoDS_Vertex aV1,aV2;
1827     TopExp::Vertices(aedge,aV1,aV2);
1828     Standard_Boolean isSame = aV1.IsSame(aV2);
1829     if(myWire->IsSeam(i))
1830       aMapSeemEdges.Add(aedge); ///continue;
1831     else if(BRep_Tool::Degenerated(aedge))
1832       aMapSmallEdges.Add(aedge);
1833     else if(isSame && CheckSmall(i,BRep_Tool::Tolerance(aV1)))
1834       aMapSmallEdges.Add(aedge);
1835
1836     if(!aMapVertexEdges.IsBound(aV1)) {
1837       TopTools_ListOfShape alshape;
1838       aMapVertexEdges.Bind(aV1,alshape);
1839     }
1840     if(!aMapVertexEdges.IsBound(aV2)) {
1841       TopTools_ListOfShape alshape;
1842       aMapVertexEdges.Bind(aV2,alshape);
1843     }
1844     if(isSame)
1845     {
1846       TopTools_ListOfShape& alshape =  aMapVertexEdges.ChangeFind(aV1);
1847       alshape.Append(aedge);
1848       alshape.Append(aedge);
1849       if(alshape.Extent() >2 && isMultiVertex( alshape,aMapSmallEdges,aMapSeemEdges))
1850         aMapLoopVertices.Add(aV1);
1851     }
1852     else {
1853       TopTools_ListOfShape& alshape =  aMapVertexEdges.ChangeFind(aV1);
1854       alshape.Append(aedge);
1855       if(alshape.Extent() >2 && isMultiVertex( alshape,aMapSmallEdges,aMapSeemEdges))
1856         aMapLoopVertices.Add(aV1);
1857       TopTools_ListOfShape& alshape2 =  aMapVertexEdges.ChangeFind(aV2);
1858       alshape2.Append(aedge);
1859       if(alshape2.Extent() >2 && isMultiVertex( alshape2,aMapSmallEdges,aMapSeemEdges))
1860         aMapLoopVertices.Add(aV2);
1861     }
1862   }
1863   SetPrecision(aSavPreci);
1864   if(aMapLoopVertices.Extent())
1865   {
1866     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
1867     myStatusLoop |= myStatus;
1868     return Standard_True;
1869   }
1870   return Standard_False;
1871 }