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