Salome HOME
Porting to OCCT development version: Standard_PI -> M_PI
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
1 // Copyright (C) 2007-2011  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.
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 // File:        GEOMAlgo_FinderShapeOn.cxx
23 // Created:     Tue Jan 11 14:44:31 2005
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_FinderShapeOn.ixx>
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
31 #include <Precision.hxx>
32 #endif
33
34 #include <gp_Pnt.hxx>
35
36 #include <TopAbs_ShapeEnum.hxx>
37 #include <TopAbs_Orientation.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Shell.hxx>
44 #include <TopoDS_Solid.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Iterator.hxx>
48
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_DataMapOfShapeShape.hxx>
52
53 #include <Bnd_Box.hxx>
54 #include <BRepBndLib.hxx>
55 #include <BRepMesh_IncrementalMesh.hxx>
56
57 #include <BRep_Builder.hxx>
58 #include <BRep_Tool.hxx>
59
60 #include <TopExp.hxx>
61 #include <TopExp_Explorer.hxx>
62
63 #include <BRepLib_MakeFace.hxx>
64 #include <BRepLib_FaceError.hxx>
65
66 #include <BOPTools_DSFiller.hxx>
67
68 #include <GEOMAlgo_WireSolid.hxx>
69 #include <GEOMAlgo_ShellSolid.hxx>
70 #include <GEOMAlgo_VertexSolid.hxx>
71 #include <GEOMAlgo_ShapeSolid.hxx>
72 #include <GEOMAlgo_SolidSolid.hxx>
73 #include <GEOMAlgo_SurfaceTools.hxx>
74 #include <GEOMAlgo_Tools.hxx>
75
76 //=======================================================================
77 //function : GEOMAlgo_FinderShapeOn
78 //purpose  : 
79 //=======================================================================
80 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
81 :
82   GEOMAlgo_ShapeAlgo()
83 {
84   myTolerance=0.0001;
85   myShapeType=TopAbs_VERTEX;
86   myState=GEOMAlgo_ST_UNKNOWN;
87   myIsAnalytic=Standard_True;
88 }
89 //=======================================================================
90 //function : ~
91 //purpose  : 
92 //=======================================================================
93 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
94 {
95 }
96 //=======================================================================
97 //function : SetSurface
98 //purpose  : 
99 //=======================================================================
100 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
101 {
102   mySurface=aS;
103 }
104 //=======================================================================
105 //function : Surface
106 //purpose  : 
107 //=======================================================================
108 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
109 {
110   return mySurface;
111 }
112 //=======================================================================
113 //function : SetShapeType
114 //purpose  : 
115 //=======================================================================
116 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
117 {
118   myShapeType=aType;
119 }
120 //=======================================================================
121 //function : ShapeType
122 //purpose  : 
123 //=======================================================================
124 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
125 {
126   return myShapeType;
127 }
128 //=======================================================================
129 //function : SetState
130 //purpose  : 
131 //=======================================================================
132 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
133 {
134   myState=aState;
135 }
136 //=======================================================================
137 //function : State
138 //purpose  : 
139 //=======================================================================
140 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
141 {
142   return myState;
143 }
144 //=======================================================================
145 // function: Shapes
146 // purpose: 
147 //=======================================================================
148 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
149 {
150   Standard_Boolean bIsConformState;
151   Standard_Integer i, aNb;
152   TopAbs_State aSt;
153   TopTools_ListOfShape* pL;
154   //
155   pL=(TopTools_ListOfShape*) &myLS;
156   pL->Clear();
157   //
158   aNb=myMSS.Extent();
159   for (i=1; i<=aNb; ++i) {
160     const TopoDS_Shape& aS=myMSS.FindKey(i);
161     aSt=myMSS.FindFromIndex(i);
162     //
163     bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
164     if (bIsConformState) {
165       pL->Append(aS);
166     }
167   }
168   return myLS;
169 }
170 //=======================================================================
171 //function : Perform
172 //purpose  : 
173 //=======================================================================
174 void GEOMAlgo_FinderShapeOn::Perform()
175 {
176   myErrorStatus=0;
177   myWarningStatus=0;
178   myLS.Clear();
179   myMSS.Clear();
180   //
181   if (!myResult.IsNull()){
182     myResult.Nullify();
183   }
184   //
185   CheckData();
186   if(myErrorStatus) {
187     return;
188   }
189   //
190   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
191   //
192   MakeArgument1();
193   if(myErrorStatus) {
194     return;
195   }
196   //
197   if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
198     FindVertices();
199     return;
200   }
201   //
202   MakeArgument2();
203   if(myErrorStatus) {
204     return;
205   }
206   //
207   Find();
208   if(myErrorStatus || myWarningStatus) {
209     return;
210   }
211   //
212 }
213 //=======================================================================
214 //function : FindVertices
215 //purpose  : 
216 //=======================================================================
217 void GEOMAlgo_FinderShapeOn::FindVertices()
218
219   Standard_Integer i, aNb, iErr;
220   TopAbs_State aSt;
221   TopAbs_Orientation aOr;
222   gp_Pnt aP;
223   TopTools_IndexedMapOfShape aM;
224   //
225   TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
226   const TopoDS_Face& aF=TopoDS::Face(aM(1));
227   aOr=aF.Orientation();
228   //
229   aM.Clear();
230   TopExp::MapShapes(myShape, myShapeType, aM);
231   aNb=aM.Extent();
232   if (!aNb) {
233     myWarningStatus=10; // No found subshapes of type myShapeType
234     return;
235   }
236   //
237   for (i=1; i<=aNb; ++i) {
238     const TopoDS_Shape& aS=aM(i);
239     const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
240     aP=BRep_Tool::Pnt(aV);
241     iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
242     if (aOr==TopAbs_REVERSED) {
243       aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
244     }
245     myMSS.Add(aS, aSt);
246   }
247 }
248 //=======================================================================
249 //function : Find
250 //purpose  : 
251 //=======================================================================
252 void GEOMAlgo_FinderShapeOn::Find()
253 {
254   Standard_Integer i, aNb;
255   Standard_Boolean bICS;
256   TopTools_IndexedMapOfShape aM;
257   //
258   TopExp::MapShapes(myArg2, myShapeType, aM);
259   //
260   aNb=aM.Extent();
261   if (!aNb) {
262     myWarningStatus=10; // No found subshapes of type myShapeType
263     return;
264   }
265   //
266   bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
267   if (!bICS || myIsAnalytic) {
268     TopoDS_Compound aCmp;
269     BRep_Builder aBB;
270     //
271     aBB.MakeCompound(aCmp);
272     for (i=1; i<=aNb; ++i) {
273       const TopoDS_Shape& aSi=aM(i);
274       aBB.Add(aCmp, aSi);
275     }
276     //
277     aM.Clear();
278     aM.Add(aCmp);
279     aNb=1;
280   }
281   //
282   for (i=1; i<=aNb; ++i) {
283     const TopoDS_Shape& aS=aM(i);
284     Find(aS);
285     if (myErrorStatus) {
286       return;
287     }
288   }
289 }
290 //=======================================================================
291 //function : Find
292 //purpose  : 
293 //=======================================================================
294 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
295 {
296   myErrorStatus=0;
297   //
298   Standard_Boolean bIsDone;
299   Standard_Integer i, iErr;
300   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
301   TopTools_ListIteratorOfListOfShape aIt;
302   BOPTools_DSFiller aDF;
303   //
304   // 1. Prepare DSFiller
305   aDF.SetShapes (myArg1, aS);
306   bIsDone=aDF.IsDone();
307   if (!bIsDone) {
308     myErrorStatus=30; // wrong args are used for DSFiller
309     return;
310   }
311   aDF.Perform();
312   bIsDone=aDF.IsDone();
313   if (!bIsDone) {
314     myErrorStatus=31; // DSFiller failed
315     return;
316   }
317   // 
318   // 2. Find shapes
319   GEOMAlgo_ShapeSolid* pSS;
320   GEOMAlgo_VertexSolid aVXS;
321   GEOMAlgo_WireSolid aWRS;
322   GEOMAlgo_ShellSolid aSHS;
323   GEOMAlgo_SolidSolid aSLS;
324   //
325   pSS=NULL;
326   //
327   switch (myShapeType) {
328     case TopAbs_VERTEX:
329       pSS=&aVXS;
330       break;
331     case TopAbs_EDGE:
332       pSS=&aWRS;
333       break;
334     case TopAbs_FACE:
335       pSS=&aSHS;
336       break;
337     case TopAbs_SOLID:
338       aSLS.SetShape2(myArg2);
339       pSS=&aSLS;
340       break;
341     default:
342       myErrorStatus=12; // unallowed subshape type
343       return;
344   }
345   //
346   pSS->SetFiller(aDF);
347   pSS->Perform();
348   iErr=pSS->ErrorStatus();
349   if (iErr) {
350     myErrorStatus=32; // builder ShapeSolid failed
351     return;
352   }
353   //
354   for (i=0; i<3; ++i) {
355     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
356     aIt.Initialize(aLS);
357     for (; aIt.More(); aIt.Next()) {
358       const TopoDS_Shape& aSImage=aIt.Value(); 
359       if (myImages.IsBound(aSImage)) { 
360         const TopoDS_Shape& aSx=myImages.Find(aSImage); 
361         myMSS.Add(aSx, aSts[i]);
362       }
363       else {
364         myErrorStatus=33;// can not find original shape
365         return; 
366       }
367     }
368   }
369 }
370 //=======================================================================
371 //function : MakeArgument1
372 //purpose  : 
373 //=======================================================================
374 void GEOMAlgo_FinderShapeOn::MakeArgument1()
375 {
376   myErrorStatus=0;
377   //
378   Standard_Integer i, aNb;
379   TopAbs_ShapeEnum aType;
380   BRepLib_FaceError aFErr;
381   BRepLib_MakeFace aMF;
382   TopTools_IndexedMapOfShape aM;
383   BRep_Builder aBB;
384   TopoDS_Face aFace;
385   TopoDS_Shell aSh;
386   TopoDS_Solid aSd;
387   //
388   // Argument 1
389   if (!myIsAnalytic) {
390 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
391     aMF.Init(mySurface, Standard_True, Precision::Confusion());
392 #else
393     aMF.Init(mySurface, Standard_True);
394 #endif
395     aFErr=aMF.Error();
396     if (aFErr!=BRepLib_FaceDone) {
397       myErrorStatus=20; // can not build the face
398       return;
399     }
400     //
401     const TopoDS_Shape& aF=aMF.Shape();
402     aFace=TopoDS::Face(aF);
403     //
404     // update tolerances
405     aM.Add(aF);
406     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
407     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
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 subshape 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 bool GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
550 {
551   // calculate deflection
552   Standard_Real aDeviationCoefficient = 0.001;
553
554   Bnd_Box B;
555   BRepBndLib::Add(theShape, B);
556   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
557   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
558
559   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
560   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
561   Standard_Real aHLRAngle = 0.349066;
562
563   // build triangulation
564   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
565
566   // check triangulation
567   bool isTriangulation = true;
568
569   TopExp_Explorer exp (theShape, TopAbs_FACE);
570   if (exp.More())
571   {
572     TopLoc_Location aTopLoc;
573     Handle(Poly_Triangulation) aTRF;
574     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
575     if (aTRF.IsNull()) {
576       isTriangulation = false;
577     }
578   }
579   else // no faces, try edges
580   {
581     TopExp_Explorer expe (theShape, TopAbs_EDGE);
582     if (!expe.More()) {
583       isTriangulation = false;
584     }
585     else {
586       TopLoc_Location aLoc;
587       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
588       if (aPE.IsNull()) {
589         isTriangulation = false;
590       }
591     }
592   }
593
594   return isTriangulation;
595 }
596
597 //
598 // myErrorStatus :
599 //
600 // 10 -mySurface=NULL
601 // 11 -myShape=NULL
602 // 12 -unallowed type of subshapes 
603 // 13 -unallowed state  
604 // 20 -can not build the face
605 // 30 -wrong args are used for DSFiller
606 // 31 -DSFiller failed
607 // 32 -builder ShapeSolid failed
608 // 33 -can not find original shape
609 //
610 // myWarningStatus
611 //
612 // 10 -subshapes of type myShapeType can not be fond in myShape