Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_FinderShapeOn.cxx
24 // Created:     Tue Jan 11 14:44:31 2005
25 // Author:      Peter KURNEV
26
27 #include <GEOMAlgo_FinderShapeOn.hxx>
28
29 #include <Basics_OCCTVersion.hxx>
30
31 #include <Precision.hxx>
32
33 #include <gp_Pnt.hxx>
34
35 #include <TopAbs_ShapeEnum.hxx>
36 #include <TopAbs_Orientation.hxx>
37
38 #include <TopoDS.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Shell.hxx>
43 #include <TopoDS_Solid.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Iterator.hxx>
47
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_DataMapOfShapeShape.hxx>
51
52 #include <Bnd_Box.hxx>
53 #include <BRepBndLib.hxx>
54 #include <BRepMesh_IncrementalMesh.hxx>
55
56 #include <BRep_Builder.hxx>
57 #include <BRep_Tool.hxx>
58
59 #include <TopExp.hxx>
60 #include <TopExp_Explorer.hxx>
61
62 #include <BRepLib_MakeFace.hxx>
63 #include <BRepLib_FaceError.hxx>
64
65
66 #include <GEOMAlgo_WireSolid.hxx>
67 #include <GEOMAlgo_ShellSolid.hxx>
68 #include <GEOMAlgo_VertexSolid.hxx>
69 #include <GEOMAlgo_ShapeSolid.hxx>
70 #include <GEOMAlgo_SolidSolid.hxx>
71 #include <GEOMAlgo_SurfaceTools.hxx>
72 #include <GEOMAlgo_AlgoTools.hxx>
73
74 #include <BOPAlgo_PaveFiller.hxx>
75 #include <BOPCol_ListOfShape.hxx>
76
77 //=======================================================================
78 //function : GEOMAlgo_FinderShapeOn
79 //purpose  :
80 //=======================================================================
81 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
82 :
83   GEOMAlgo_ShapeAlgo()
84 {
85   myTolerance=0.0001;
86   myShapeType=TopAbs_VERTEX;
87   myState=GEOMAlgo_ST_UNKNOWN;
88   myIsAnalytic=Standard_True;
89 }
90 //=======================================================================
91 //function : ~
92 //purpose  :
93 //=======================================================================
94 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
95 {
96 }
97 //=======================================================================
98 //function : SetSurface
99 //purpose  :
100 //=======================================================================
101 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
102 {
103   mySurface=aS;
104 }
105 //=======================================================================
106 //function : Surface
107 //purpose  :
108 //=======================================================================
109 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
110 {
111   return mySurface;
112 }
113 //=======================================================================
114 //function : SetShapeType
115 //purpose  :
116 //=======================================================================
117 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
118 {
119   myShapeType=aType;
120 }
121 //=======================================================================
122 //function : ShapeType
123 //purpose  :
124 //=======================================================================
125 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
126 {
127   return myShapeType;
128 }
129 //=======================================================================
130 //function : SetState
131 //purpose  :
132 //=======================================================================
133 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
134 {
135   myState=aState;
136 }
137 //=======================================================================
138 //function : State
139 //purpose  :
140 //=======================================================================
141 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
142 {
143   return myState;
144 }
145 //=======================================================================
146 // function: Shapes
147 // purpose:
148 //=======================================================================
149 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
150 {
151   Standard_Boolean bIsConformState;
152   Standard_Integer i, aNb;
153   TopAbs_State aSt;
154   TopTools_ListOfShape* pL;
155   //
156   pL=(TopTools_ListOfShape*) &myLS;
157   pL->Clear();
158   //
159   aNb=myMSS.Extent();
160   for (i=1; i<=aNb; ++i) {
161     const TopoDS_Shape& aS=myMSS.FindKey(i);
162     aSt=myMSS.FindFromIndex(i);
163     //
164     bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
165     if (bIsConformState) {
166       pL->Append(aS);
167     }
168   }
169   return myLS;
170 }
171 //=======================================================================
172 //function : Perform
173 //purpose  :
174 //=======================================================================
175 void GEOMAlgo_FinderShapeOn::Perform()
176 {
177   myErrorStatus=0;
178   myWarningStatus=0;
179   myLS.Clear();
180   myMSS.Clear();
181   //
182   if (!myResult.IsNull()){
183     myResult.Nullify();
184   }
185   //
186   CheckData();
187   if(myErrorStatus) {
188     return;
189   }
190   //
191   // Initialize the context
192   GEOMAlgo_ShapeAlgo::Perform();
193   //
194   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
195   //
196   MakeArgument1();
197   if(myErrorStatus) {
198     return;
199   }
200   //
201   if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
202     FindVertices();
203     return;
204   }
205   //
206   MakeArgument2();
207   if(myErrorStatus) {
208     return;
209   }
210   //
211   Find();
212   if(myErrorStatus || myWarningStatus) {
213     return;
214   }
215   //
216 }
217 //=======================================================================
218 //function : FindVertices
219 //purpose  :
220 //=======================================================================
221 void GEOMAlgo_FinderShapeOn::FindVertices()
222 {
223   Standard_Integer i, aNb, iErr;
224   TopAbs_State aSt;
225   TopAbs_Orientation aOr;
226   gp_Pnt aP;
227   TopTools_IndexedMapOfShape aM;
228   //
229   TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
230   const TopoDS_Face& aF=TopoDS::Face(aM(1));
231   aOr=aF.Orientation();
232   //
233   aM.Clear();
234   TopExp::MapShapes(myShape, myShapeType, aM);
235   aNb=aM.Extent();
236   if (!aNb) {
237     myWarningStatus=10; // No found sub-shapes of type myShapeType
238     return;
239   }
240   //
241   for (i=1; i<=aNb; ++i) {
242     const TopoDS_Shape& aS=aM(i);
243     const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
244     aP=BRep_Tool::Pnt(aV);
245     iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
246     if (aOr==TopAbs_REVERSED) {
247       aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
248     }
249     myMSS.Add(aS, aSt);
250   }
251 }
252 //=======================================================================
253 //function : Find
254 //purpose  :
255 //=======================================================================
256 void GEOMAlgo_FinderShapeOn::Find()
257 {
258   Standard_Integer i, aNb;
259   Standard_Boolean bICS;
260   TopTools_IndexedMapOfShape aM;
261   //
262   TopExp::MapShapes(myArg2, myShapeType, aM);
263   //
264   aNb=aM.Extent();
265   if (!aNb) {
266     myWarningStatus=10; // No found sub-shapes of type myShapeType
267     return;
268   }
269   //
270   bICS=GEOMAlgo_AlgoTools::IsCompositeShape(myArg2);
271   if (!bICS || myIsAnalytic) {
272     TopoDS_Compound aCmp;
273     BRep_Builder aBB;
274     //
275     aBB.MakeCompound(aCmp);
276     for (i=1; i<=aNb; ++i) {
277       const TopoDS_Shape& aSi=aM(i);
278       aBB.Add(aCmp, aSi);
279     }
280     //
281     aM.Clear();
282     aM.Add(aCmp);
283     aNb=1;
284   }
285   //
286   for (i=1; i<=aNb; ++i) {
287     const TopoDS_Shape& aS=aM(i);
288     Find(aS);
289     if (myErrorStatus) {
290       return;
291     }
292   }
293 }
294 //=======================================================================
295 //function : Find
296 //purpose  :
297 //=======================================================================
298 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
299 {
300   myErrorStatus=0;
301   //
302   Standard_Integer i, iErr;
303   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
304   TopTools_ListIteratorOfListOfShape aIt;
305   BOPCol_ListOfShape aLS;
306   BOPAlgo_PaveFiller aPF;
307   //
308   // 1. Prepare DSFiller
309   aLS.Append(myArg1);
310   aLS.Append(aS);
311   aPF.SetArguments(aLS);
312   //
313   aPF.Perform();
314   iErr=aPF.ErrorStatus();
315   if (iErr) {
316     myErrorStatus=31; //  PaveFiller is failed
317     return;
318   }
319   //
320   // 2. Find shapes
321   GEOMAlgo_ShapeSolid* pSS;
322   GEOMAlgo_VertexSolid aVXS;
323   GEOMAlgo_WireSolid aWRS;
324   GEOMAlgo_ShellSolid aSHS;
325   GEOMAlgo_SolidSolid aSLS;
326   //
327   pSS=NULL;
328   //
329   switch (myShapeType) {
330     case TopAbs_VERTEX:
331       pSS=&aVXS;
332       break;
333     case TopAbs_EDGE:
334       pSS=&aWRS;
335       break;
336     case TopAbs_FACE:
337       pSS=&aSHS;
338       break;
339     case TopAbs_SOLID:
340       aSLS.SetShape2(myArg2);
341       pSS=&aSLS;
342       break;
343     default:
344       myErrorStatus=12; // unallowed sub-shape type
345       return;
346   }
347   //
348   pSS->SetFiller(aPF);
349   pSS->Perform();
350   iErr=pSS->ErrorStatus();
351   if (iErr) {
352     myErrorStatus=32; // builder ShapeSolid failed
353     return;
354   }
355   //
356   for (i=0; i<3; ++i) {
357     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
358     aIt.Initialize(aLS);
359     for (; aIt.More(); aIt.Next()) {
360       const TopoDS_Shape& aSImage=aIt.Value();
361       if (myImages.IsBound(aSImage)) {
362         const TopoDS_Shape& aSx=myImages.Find(aSImage);
363         myMSS.Add(aSx, aSts[i]);
364       }
365       else {
366         myErrorStatus=33;// can not find original shape
367         return;
368       }
369     }
370   }
371 }
372 //=======================================================================
373 //function : MakeArgument1
374 //purpose  :
375 //=======================================================================
376 void GEOMAlgo_FinderShapeOn::MakeArgument1()
377 {
378   myErrorStatus=0;
379   //
380   Standard_Integer i, aNb;
381   TopAbs_ShapeEnum aType;
382   BRepLib_FaceError aFErr;
383   BRepLib_MakeFace aMF;
384   TopTools_IndexedMapOfShape aM;
385   BRep_Builder aBB;
386   TopoDS_Face aFace;
387   TopoDS_Shell aSh;
388   TopoDS_Solid aSd;
389   //
390   // Argument 1
391   if (!myIsAnalytic) {
392     aMF.Init(mySurface, Standard_True, Precision::Confusion());
393
394     aFErr=aMF.Error();
395     if (aFErr!=BRepLib_FaceDone) {
396       myErrorStatus=20; // can not build the face
397       return;
398     }
399     //
400     const TopoDS_Shape& aF=aMF.Shape();
401     aFace=TopoDS::Face(aF);
402     //
403     // update tolerances
404     aM.Add(aF);
405     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
406     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
407
408     aNb=aM.Extent();
409     for (i=1; i<=aNb; ++i) {
410       const TopoDS_Shape& aS=aM(i);
411       aType=aS.ShapeType();
412       switch (aType) {
413       case TopAbs_VERTEX: {
414         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
415         aBB.UpdateVertex(aVx, myTolerance);
416       }
417         break;
418       case TopAbs_EDGE: {
419         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
420         aBB.UpdateEdge(aEx, myTolerance);
421       }
422         break;
423       case TopAbs_FACE: {
424         const TopoDS_Face& aFx=TopoDS::Face(aS);
425         aBB.UpdateFace(aFx, myTolerance);
426       }
427         break;
428       default:
429         break;
430       }
431     }
432   } //
433   else {
434     aBB.MakeFace(aFace, mySurface, myTolerance);
435   }
436   //
437   // make solid
438   aBB.MakeShell(aSh);
439   aBB.Add(aSh, aFace);
440   aBB.MakeSolid(aSd);
441   aBB.Add(aSd, aSh);
442   myArg1=aSd;
443 }
444 //=======================================================================
445 //function : MakeArgument2
446 //purpose  :
447 //=======================================================================
448 void GEOMAlgo_FinderShapeOn::MakeArgument2()
449 {
450   myErrorStatus=0;
451   //
452   TopoDS_Shape aSC;
453   TopTools_DataMapOfShapeShape aOriginals;
454   //
455   myImages.Clear();
456   //
457   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
458   //
459   myArg2=aSC;
460 }
461 //=======================================================================
462 //function : CheckData
463 //purpose  :
464 //=======================================================================
465 void GEOMAlgo_FinderShapeOn::CheckData()
466 {
467   myErrorStatus=0;
468   //
469   if(mySurface.IsNull()) {
470     myErrorStatus=10; // mySurface=NULL
471     return;
472   }
473   //
474   if (myShape.IsNull()) {
475     myErrorStatus=11; // myShape=NULL
476     return;
477   }
478   //
479   if (!(myShapeType==TopAbs_VERTEX ||
480         myShapeType==TopAbs_EDGE ||
481         myShapeType==TopAbs_FACE ||
482         myShapeType==TopAbs_SOLID)) {
483     myErrorStatus=12; // unallowed sub-shape type
484     return;
485   }
486   //
487   if (myState==GEOMAlgo_ST_UNKNOWN ||
488       myState==GEOMAlgo_ST_INOUT) {
489     myErrorStatus=13; // unallowed state type
490     return;
491   }
492 }
493 //
494 //=======================================================================
495 //function : CopySource
496 //purpose  :
497 //=======================================================================
498 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
499                                         TopTools_DataMapOfShapeShape& aImages,
500                                         TopTools_DataMapOfShapeShape& aOriginals,
501                                         TopoDS_Shape& aEx)
502 {
503   Standard_Boolean bFree;
504   TopAbs_ShapeEnum aType;
505   Standard_Integer aR;
506   BRep_Builder BB;
507   TopoDS_Iterator aIt;
508   //
509   aType=aE.ShapeType();
510   //
511   if (aOriginals.IsBound(aE)) {
512     aEx=aOriginals.ChangeFind(aE);
513     return;
514   }
515   else {
516     aEx=aE.EmptyCopied();
517     aOriginals.Bind(aE, aEx);
518     aImages.Bind(aEx, aE);
519   }
520   //
521   aR=(Standard_Integer)aType+1;
522   if (aR>TopAbs_VERTEX) {
523     return;
524   }
525   //
526   bFree=aEx.Free();
527   aEx.Free(Standard_True);
528   //
529   aType=(TopAbs_ShapeEnum) aR;
530   //
531   aIt.Initialize(aE);//, Standard_False);
532   for (; aIt.More();  aIt.Next()) {
533     const TopoDS_Shape& aV=aIt.Value();
534     TopoDS_Shape aVx;
535     //
536     CopySource (aV, aImages, aOriginals, aVx);
537     //
538     aVx.Orientation(aV.Orientation());
539     BB.Add(aEx, aVx);
540   }
541   //
542   aEx.Free(bFree);
543 }
544 //
545 //=======================================================================
546 //function : BuildTriangulation
547 //purpose  :
548 //=======================================================================
549 Standard_Boolean
550   GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
551 {
552   // calculate deflection
553   Standard_Real aDeviationCoefficient = 0.001;
554
555   Bnd_Box B;
556   BRepBndLib::Add(theShape, B);
557   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
558   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
559
560   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
561   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
562   Standard_Real aHLRAngle = 0.349066;
563
564   // build triangulation
565   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
566
567   // check triangulation
568   bool isTriangulation = true;
569
570   TopExp_Explorer exp (theShape, TopAbs_FACE);
571   if (exp.More())
572   {
573     TopLoc_Location aTopLoc;
574     Handle(Poly_Triangulation) aTRF;
575     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
576     if (aTRF.IsNull()) {
577       isTriangulation = false;
578     }
579   }
580   else // no faces, try edges
581   {
582     TopExp_Explorer expe (theShape, TopAbs_EDGE);
583     if (!expe.More()) {
584       isTriangulation = false;
585     }
586     else {
587       TopLoc_Location aLoc;
588       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
589       if (aPE.IsNull()) {
590         isTriangulation = false;
591       }
592     }
593   }
594
595   return isTriangulation;
596 }
597
598 //
599 // myErrorStatus :
600 //
601 // 10 -mySurface=NULL
602 // 11 -myShape=NULL
603 // 12 -unallowed type of sub-shapes
604 // 13 -unallowed state
605 // 20 -can not build the face
606 // 30 -wrong args are used for DSFiller
607 // 31 -DSFiller failed
608 // 32 -builder ShapeSolid failed
609 // 33 -can not find original shape
610 //
611 // myWarningStatus
612 //
613 // 10 - sub-shapes of type myShapeType can not be fond in myShape