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