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