Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
1 // Copyright (C) 2007-2013  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
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 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
32 #include <Precision.hxx>
33 #endif
34
35 #include <gp_Pnt.hxx>
36
37 #include <TopAbs_ShapeEnum.hxx>
38 #include <TopAbs_Orientation.hxx>
39
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Shell.hxx>
45 #include <TopoDS_Solid.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Iterator.hxx>
49
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_DataMapOfShapeShape.hxx>
53
54 #include <Bnd_Box.hxx>
55 #include <BRepBndLib.hxx>
56 #include <BRepMesh_IncrementalMesh.hxx>
57
58 #include <BRep_Builder.hxx>
59 #include <BRep_Tool.hxx>
60
61 #include <TopExp.hxx>
62 #include <TopExp_Explorer.hxx>
63
64 #include <BRepLib_MakeFace.hxx>
65 #include <BRepLib_FaceError.hxx>
66
67 #include <BOPTools_DSFiller.hxx>
68
69 #include <GEOMAlgo_WireSolid.hxx>
70 #include <GEOMAlgo_ShellSolid.hxx>
71 #include <GEOMAlgo_VertexSolid.hxx>
72 #include <GEOMAlgo_ShapeSolid.hxx>
73 #include <GEOMAlgo_SolidSolid.hxx>
74 #include <GEOMAlgo_SurfaceTools.hxx>
75 #include <GEOMAlgo_Tools.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_Tools::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_Boolean bIsDone;
303   Standard_Integer i, iErr;
304   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
305   TopTools_ListIteratorOfListOfShape aIt;
306   BOPTools_DSFiller aDF;
307   //
308   // 1. Prepare DSFiller
309   aDF.SetShapes (myArg1, aS);
310   bIsDone=aDF.IsDone();
311   if (!bIsDone) {
312     myErrorStatus=30; // wrong args are used for DSFiller
313     return;
314   }
315   aDF.Perform();
316   bIsDone=aDF.IsDone();
317   if (!bIsDone) {
318     myErrorStatus=31; // DSFiller failed
319     return;
320   }
321   //
322   // 2. Find shapes
323   GEOMAlgo_ShapeSolid* pSS;
324   GEOMAlgo_VertexSolid aVXS;
325   GEOMAlgo_WireSolid aWRS;
326   GEOMAlgo_ShellSolid aSHS;
327   GEOMAlgo_SolidSolid aSLS;
328   //
329   pSS=NULL;
330   //
331   switch (myShapeType) {
332     case TopAbs_VERTEX:
333       pSS=&aVXS;
334       break;
335     case TopAbs_EDGE:
336       pSS=&aWRS;
337       break;
338     case TopAbs_FACE:
339       pSS=&aSHS;
340       break;
341     case TopAbs_SOLID:
342       aSLS.SetShape2(myArg2);
343       pSS=&aSLS;
344       break;
345     default:
346       myErrorStatus=12; // unallowed sub-shape type
347       return;
348   }
349   //
350   pSS->SetFiller(aDF);
351   pSS->Perform();
352   iErr=pSS->ErrorStatus();
353   if (iErr) {
354     myErrorStatus=32; // builder ShapeSolid failed
355     return;
356   }
357   //
358   for (i=0; i<3; ++i) {
359     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
360     aIt.Initialize(aLS);
361     for (; aIt.More(); aIt.Next()) {
362       const TopoDS_Shape& aSImage=aIt.Value();
363       if (myImages.IsBound(aSImage)) {
364         const TopoDS_Shape& aSx=myImages.Find(aSImage);
365         myMSS.Add(aSx, aSts[i]);
366       }
367       else {
368         myErrorStatus=33;// can not find original shape
369         return;
370       }
371     }
372   }
373 }
374 //=======================================================================
375 //function : MakeArgument1
376 //purpose  :
377 //=======================================================================
378 void GEOMAlgo_FinderShapeOn::MakeArgument1()
379 {
380   myErrorStatus=0;
381   //
382   Standard_Integer i, aNb;
383   TopAbs_ShapeEnum aType;
384   BRepLib_FaceError aFErr;
385   BRepLib_MakeFace aMF;
386   TopTools_IndexedMapOfShape aM;
387   BRep_Builder aBB;
388   TopoDS_Face aFace;
389   TopoDS_Shell aSh;
390   TopoDS_Solid aSd;
391   //
392   // Argument 1
393   if (!myIsAnalytic) {
394 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
395     aMF.Init(mySurface, Standard_True, Precision::Confusion());
396 #else
397     aMF.Init(mySurface, Standard_True);
398 #endif
399     aFErr=aMF.Error();
400     if (aFErr!=BRepLib_FaceDone) {
401       myErrorStatus=20; // can not build the face
402       return;
403     }
404     //
405     const TopoDS_Shape& aF=aMF.Shape();
406     aFace=TopoDS::Face(aF);
407     //
408     // update tolerances
409     aM.Add(aF);
410     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
411     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
412     aNb=aM.Extent();
413     for (i=1; i<=aNb; ++i) {
414       const TopoDS_Shape& aS=aM(i);
415       aType=aS.ShapeType();
416       switch (aType) {
417       case TopAbs_VERTEX: {
418         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
419         aBB.UpdateVertex(aVx, myTolerance);
420       }
421         break;
422       case TopAbs_EDGE: {
423         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
424         aBB.UpdateEdge(aEx, myTolerance);
425       }
426         break;
427       case TopAbs_FACE: {
428         const TopoDS_Face& aFx=TopoDS::Face(aS);
429         aBB.UpdateFace(aFx, myTolerance);
430       }
431         break;
432       default:
433         break;
434       }
435     }
436   } //
437   else {
438     aBB.MakeFace(aFace, mySurface, myTolerance);
439   }
440   //
441   // make solid
442   aBB.MakeShell(aSh);
443   aBB.Add(aSh, aFace);
444   aBB.MakeSolid(aSd);
445   aBB.Add(aSd, aSh);
446   myArg1=aSd;
447 }
448 //=======================================================================
449 //function : MakeArgument2
450 //purpose  :
451 //=======================================================================
452 void GEOMAlgo_FinderShapeOn::MakeArgument2()
453 {
454   myErrorStatus=0;
455   //
456   TopoDS_Shape aSC;
457   TopTools_DataMapOfShapeShape aOriginals;
458   //
459   myImages.Clear();
460   //
461   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
462   //
463   myArg2=aSC;
464 }
465 //=======================================================================
466 //function : CheckData
467 //purpose  :
468 //=======================================================================
469 void GEOMAlgo_FinderShapeOn::CheckData()
470 {
471   myErrorStatus=0;
472   //
473   if(mySurface.IsNull()) {
474     myErrorStatus=10; // mySurface=NULL
475     return;
476   }
477   //
478   if (myShape.IsNull()) {
479     myErrorStatus=11; // myShape=NULL
480     return;
481   }
482   //
483   if (!(myShapeType==TopAbs_VERTEX ||
484         myShapeType==TopAbs_EDGE ||
485         myShapeType==TopAbs_FACE ||
486         myShapeType==TopAbs_SOLID)) {
487     myErrorStatus=12; // unallowed sub-shape type
488     return;
489   }
490   //
491   if (myState==GEOMAlgo_ST_UNKNOWN ||
492       myState==GEOMAlgo_ST_INOUT) {
493     myErrorStatus=13; // unallowed state type
494     return;
495   }
496 }
497 //
498 //=======================================================================
499 //function : CopySource
500 //purpose  :
501 //=======================================================================
502 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
503                                         TopTools_DataMapOfShapeShape& aImages,
504                                         TopTools_DataMapOfShapeShape& aOriginals,
505                                         TopoDS_Shape& aEx)
506 {
507   Standard_Boolean bFree;
508   TopAbs_ShapeEnum aType;
509   Standard_Integer aR;
510   BRep_Builder BB;
511   TopoDS_Iterator aIt;
512   //
513   aType=aE.ShapeType();
514   //
515   if (aOriginals.IsBound(aE)) {
516     aEx=aOriginals.ChangeFind(aE);
517     return;
518   }
519   else {
520     aEx=aE.EmptyCopied();
521     aOriginals.Bind(aE, aEx);
522     aImages.Bind(aEx, aE);
523   }
524   //
525   aR=(Standard_Integer)aType+1;
526   if (aR>TopAbs_VERTEX) {
527     return;
528   }
529   //
530   bFree=aEx.Free();
531   aEx.Free(Standard_True);
532   //
533   aType=(TopAbs_ShapeEnum) aR;
534   //
535   aIt.Initialize(aE);//, Standard_False);
536   for (; aIt.More();  aIt.Next()) {
537     const TopoDS_Shape& aV=aIt.Value();
538     TopoDS_Shape aVx;
539     //
540     CopySource (aV, aImages, aOriginals, aVx);
541     //
542     aVx.Orientation(aV.Orientation());
543     BB.Add(aEx, aVx);
544   }
545   //
546   aEx.Free(bFree);
547 }
548 //
549 //=======================================================================
550 //function : BuildTriangulation
551 //purpose  :
552 //=======================================================================
553 Standard_Boolean
554   GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
555 {
556   // calculate deflection
557   Standard_Real aDeviationCoefficient = 0.001;
558
559   Bnd_Box B;
560   BRepBndLib::Add(theShape, B);
561   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
562   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
563
564   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
565   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
566   Standard_Real aHLRAngle = 0.349066;
567
568   // build triangulation
569   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
570
571   // check triangulation
572   bool isTriangulation = true;
573
574   TopExp_Explorer exp (theShape, TopAbs_FACE);
575   if (exp.More())
576   {
577     TopLoc_Location aTopLoc;
578     Handle(Poly_Triangulation) aTRF;
579     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
580     if (aTRF.IsNull()) {
581       isTriangulation = false;
582     }
583   }
584   else // no faces, try edges
585   {
586     TopExp_Explorer expe (theShape, TopAbs_EDGE);
587     if (!expe.More()) {
588       isTriangulation = false;
589     }
590     else {
591       TopLoc_Location aLoc;
592       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
593       if (aPE.IsNull()) {
594         isTriangulation = false;
595       }
596     }
597   }
598
599   return isTriangulation;
600 }
601
602 //
603 // myErrorStatus :
604 //
605 // 10 -mySurface=NULL
606 // 11 -myShape=NULL
607 // 12 -unallowed type of sub-shapes
608 // 13 -unallowed state
609 // 20 -can not build the face
610 // 30 -wrong args are used for DSFiller
611 // 31 -DSFiller failed
612 // 32 -builder ShapeSolid failed
613 // 33 -can not find original shape
614 //
615 // myWarningStatus
616 //
617 // 10 - sub-shapes of type myShapeType can not be fond in myShape