Salome HOME
Win32 compilation.
[modules/geom.git] / src / GEOMAlgo_NEW / 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.hxx>
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   // Initialize the context
191   GEOMAlgo_ShapeAlgo::Perform();
192   //
193   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
194   //
195   MakeArgument1();
196   if(myErrorStatus) {
197     return;
198   }
199   //
200   if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
201     FindVertices();
202     return;
203   }
204   //
205   MakeArgument2();
206   if(myErrorStatus) {
207     return;
208   }
209   //
210   Find();
211   if(myErrorStatus || myWarningStatus) {
212     return;
213   }
214   //
215 }
216 //=======================================================================
217 //function : FindVertices
218 //purpose  : 
219 //=======================================================================
220 void GEOMAlgo_FinderShapeOn::FindVertices()
221
222   Standard_Integer i, aNb, iErr;
223   TopAbs_State aSt;
224   TopAbs_Orientation aOr;
225   gp_Pnt aP;
226   TopTools_IndexedMapOfShape aM;
227   //
228   TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
229   const TopoDS_Face& aF=TopoDS::Face(aM(1));
230   aOr=aF.Orientation();
231   //
232   aM.Clear();
233   TopExp::MapShapes(myShape, myShapeType, aM);
234   aNb=aM.Extent();
235   if (!aNb) {
236     myWarningStatus=10; // No found sub-shapes of type myShapeType
237     return;
238   }
239   //
240   for (i=1; i<=aNb; ++i) {
241     const TopoDS_Shape& aS=aM(i);
242     const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
243     aP=BRep_Tool::Pnt(aV);
244     iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
245     if (aOr==TopAbs_REVERSED) {
246       aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
247     }
248     myMSS.Add(aS, aSt);
249   }
250 }
251 //=======================================================================
252 //function : Find
253 //purpose  : 
254 //=======================================================================
255 void GEOMAlgo_FinderShapeOn::Find()
256 {
257   Standard_Integer i, aNb;
258   Standard_Boolean bICS;
259   TopTools_IndexedMapOfShape aM;
260   //
261   TopExp::MapShapes(myArg2, myShapeType, aM);
262   //
263   aNb=aM.Extent();
264   if (!aNb) {
265     myWarningStatus=10; // No found sub-shapes of type myShapeType
266     return;
267   }
268   //
269   bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
270   if (!bICS || myIsAnalytic) {
271     TopoDS_Compound aCmp;
272     BRep_Builder aBB;
273     //
274     aBB.MakeCompound(aCmp);
275     for (i=1; i<=aNb; ++i) {
276       const TopoDS_Shape& aSi=aM(i);
277       aBB.Add(aCmp, aSi);
278     }
279     //
280     aM.Clear();
281     aM.Add(aCmp);
282     aNb=1;
283   }
284   //
285   for (i=1; i<=aNb; ++i) {
286     const TopoDS_Shape& aS=aM(i);
287     Find(aS);
288     if (myErrorStatus) {
289       return;
290     }
291   }
292 }
293 //=======================================================================
294 //function : Find
295 //purpose  : 
296 //=======================================================================
297 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
298 {
299   myErrorStatus=0;
300   //
301   Standard_Boolean bIsDone;
302   Standard_Integer i, iErr;
303   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
304   TopTools_ListIteratorOfListOfShape aIt;
305   BOPTools_DSFiller aDF;
306   //
307   // 1. Prepare DSFiller
308   aDF.SetShapes (myArg1, aS);
309   bIsDone=aDF.IsDone();
310   if (!bIsDone) {
311     myErrorStatus=30; // wrong args are used for DSFiller
312     return;
313   }
314   aDF.Perform();
315   bIsDone=aDF.IsDone();
316   if (!bIsDone) {
317     myErrorStatus=31; // DSFiller failed
318     return;
319   }
320   // 
321   // 2. Find shapes
322   GEOMAlgo_ShapeSolid* pSS;
323   GEOMAlgo_VertexSolid aVXS;
324   GEOMAlgo_WireSolid aWRS;
325   GEOMAlgo_ShellSolid aSHS;
326   GEOMAlgo_SolidSolid aSLS;
327   //
328   pSS=NULL;
329   //
330   switch (myShapeType) {
331     case TopAbs_VERTEX:
332       pSS=&aVXS;
333       break;
334     case TopAbs_EDGE:
335       pSS=&aWRS;
336       break;
337     case TopAbs_FACE:
338       pSS=&aSHS;
339       break;
340     case TopAbs_SOLID:
341       aSLS.SetShape2(myArg2);
342       pSS=&aSLS;
343       break;
344     default:
345       myErrorStatus=12; // unallowed sub-shape type
346       return;
347   }
348   //
349   pSS->SetFiller(aDF);
350   pSS->Perform();
351   iErr=pSS->ErrorStatus();
352   if (iErr) {
353     myErrorStatus=32; // builder ShapeSolid failed
354     return;
355   }
356   //
357   for (i=0; i<3; ++i) {
358     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
359     aIt.Initialize(aLS);
360     for (; aIt.More(); aIt.Next()) {
361       const TopoDS_Shape& aSImage=aIt.Value(); 
362       if (myImages.IsBound(aSImage)) { 
363         const TopoDS_Shape& aSx=myImages.Find(aSImage); 
364         myMSS.Add(aSx, aSts[i]);
365       }
366       else {
367         myErrorStatus=33;// can not find original shape
368         return; 
369       }
370     }
371   }
372 }
373 //=======================================================================
374 //function : MakeArgument1
375 //purpose  : 
376 //=======================================================================
377 void GEOMAlgo_FinderShapeOn::MakeArgument1()
378 {
379   myErrorStatus=0;
380   //
381   Standard_Integer i, aNb;
382   TopAbs_ShapeEnum aType;
383   BRepLib_FaceError aFErr;
384   BRepLib_MakeFace aMF;
385   TopTools_IndexedMapOfShape aM;
386   BRep_Builder aBB;
387   TopoDS_Face aFace;
388   TopoDS_Shell aSh;
389   TopoDS_Solid aSd;
390   //
391   // Argument 1
392   if (!myIsAnalytic) {
393 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
394     aMF.Init(mySurface, Standard_True, Precision::Confusion());
395 #else
396     aMF.Init(mySurface, Standard_True);
397 #endif
398     aFErr=aMF.Error();
399     if (aFErr!=BRepLib_FaceDone) {
400       myErrorStatus=20; // can not build the face
401       return;
402     }
403     //
404     const TopoDS_Shape& aF=aMF.Shape();
405     aFace=TopoDS::Face(aF);
406     //
407     // update tolerances
408     aM.Add(aF);
409     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
410     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
411     aNb=aM.Extent();
412     for (i=1; i<=aNb; ++i) {
413       const TopoDS_Shape& aS=aM(i);
414       aType=aS.ShapeType();
415       switch (aType) {
416       case TopAbs_VERTEX: {
417         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
418         aBB.UpdateVertex(aVx, myTolerance);
419       }
420         break;
421       case TopAbs_EDGE: {
422         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
423         aBB.UpdateEdge(aEx, myTolerance);
424       }
425         break;
426       case TopAbs_FACE: {
427         const TopoDS_Face& aFx=TopoDS::Face(aS);
428         aBB.UpdateFace(aFx, myTolerance);
429       }
430         break;
431       default:
432         break;
433       }
434     }
435   } //  
436   else {
437     aBB.MakeFace(aFace, mySurface, myTolerance);
438   }
439   //
440   // make solid
441   aBB.MakeShell(aSh);
442   aBB.Add(aSh, aFace);
443   aBB.MakeSolid(aSd);
444   aBB.Add(aSd, aSh);
445   myArg1=aSd;
446 }
447 //=======================================================================
448 //function : MakeArgument2
449 //purpose  : 
450 //=======================================================================
451 void GEOMAlgo_FinderShapeOn::MakeArgument2()
452 {
453   myErrorStatus=0;
454   //
455   TopoDS_Shape aSC;
456   TopTools_DataMapOfShapeShape aOriginals;
457   //
458   myImages.Clear();
459   //
460   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
461   //
462   myArg2=aSC;
463 }
464 //=======================================================================
465 //function : CheckData
466 //purpose  : 
467 //=======================================================================
468 void GEOMAlgo_FinderShapeOn::CheckData()
469 {
470   myErrorStatus=0;
471   //
472   if(mySurface.IsNull()) {
473     myErrorStatus=10; // mySurface=NULL
474     return;
475   }
476   //
477   if (myShape.IsNull()) {
478     myErrorStatus=11; // myShape=NULL
479     return;
480   }
481   //
482   if (!(myShapeType==TopAbs_VERTEX ||
483         myShapeType==TopAbs_EDGE ||
484         myShapeType==TopAbs_FACE ||
485         myShapeType==TopAbs_SOLID)) {
486     myErrorStatus=12; // unallowed sub-shape type
487     return;
488   }
489   //
490   if (myState==GEOMAlgo_ST_UNKNOWN || 
491       myState==GEOMAlgo_ST_INOUT) {
492     myErrorStatus=13; // unallowed state type
493     return;
494   }
495 }
496 //
497 //=======================================================================
498 //function : CopySource
499 //purpose  : 
500 //=======================================================================
501 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
502                                         TopTools_DataMapOfShapeShape& aImages,
503                                         TopTools_DataMapOfShapeShape& aOriginals,
504                                         TopoDS_Shape& aEx)
505 {
506   Standard_Boolean bFree;
507   TopAbs_ShapeEnum aType;
508   Standard_Integer aR;
509   BRep_Builder BB;
510   TopoDS_Iterator aIt;
511   //
512   aType=aE.ShapeType();
513   //
514   if (aOriginals.IsBound(aE)) {
515     aEx=aOriginals.ChangeFind(aE);
516     return;
517   }
518   else {
519     aEx=aE.EmptyCopied();
520     aOriginals.Bind(aE, aEx);
521     aImages.Bind(aEx, aE);
522   }
523   //
524   aR=(Standard_Integer)aType+1;
525   if (aR>TopAbs_VERTEX) {
526     return;
527   }
528   //
529   bFree=aEx.Free();
530   aEx.Free(Standard_True);
531   //
532   aType=(TopAbs_ShapeEnum) aR;
533   //
534   aIt.Initialize(aE);//, Standard_False);
535   for (; aIt.More();  aIt.Next()) {
536     const TopoDS_Shape& aV=aIt.Value();
537     TopoDS_Shape aVx;
538     //
539     CopySource (aV, aImages, aOriginals, aVx);  
540     //
541     aVx.Orientation(aV.Orientation());
542     BB.Add(aEx, aVx);
543   }
544   //
545   aEx.Free(bFree);
546 }
547 //
548 //=======================================================================
549 //function : BuildTriangulation
550 //purpose  : 
551 //=======================================================================
552 Standard_Boolean 
553   GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
554 {
555   // calculate deflection
556   Standard_Real aDeviationCoefficient = 0.001;
557
558   Bnd_Box B;
559   BRepBndLib::Add(theShape, B);
560   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
561   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
562
563   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
564   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
565   Standard_Real aHLRAngle = 0.349066;
566
567   // build triangulation
568   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
569
570   // check triangulation
571   bool isTriangulation = true;
572
573   TopExp_Explorer exp (theShape, TopAbs_FACE);
574   if (exp.More())
575   {
576     TopLoc_Location aTopLoc;
577     Handle(Poly_Triangulation) aTRF;
578     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
579     if (aTRF.IsNull()) {
580       isTriangulation = false;
581     }
582   }
583   else // no faces, try edges
584   {
585     TopExp_Explorer expe (theShape, TopAbs_EDGE);
586     if (!expe.More()) {
587       isTriangulation = false;
588     }
589     else {
590       TopLoc_Location aLoc;
591       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
592       if (aPE.IsNull()) {
593         isTriangulation = false;
594       }
595     }
596   }
597
598   return isTriangulation;
599 }
600
601 //
602 // myErrorStatus :
603 //
604 // 10 -mySurface=NULL
605 // 11 -myShape=NULL
606 // 12 -unallowed type of sub-shapes 
607 // 13 -unallowed state  
608 // 20 -can not build the face
609 // 30 -wrong args are used for DSFiller
610 // 31 -DSFiller failed
611 // 32 -builder ShapeSolid failed
612 // 33 -can not find original shape
613 //
614 // myWarningStatus
615 //
616 // 10 - sub-shapes of type myShapeType can not be fond in myShape