Back to index

salome-geom  6.5.0
GEOMAlgo_FinderShapeOn.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:        GEOMAlgo_FinderShapeOn.cxx
00024 // Created:     Tue Jan 11 14:44:31 2005
00025 // Author:      Peter KURNEV
00026 
00027 #include <GEOMAlgo_FinderShapeOn.hxx>
00028 
00029 #include <Basics_OCCTVersion.hxx>
00030 
00031 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00032 #include <Precision.hxx>
00033 #endif
00034 
00035 #include <gp_Pnt.hxx>
00036 
00037 #include <TopAbs_ShapeEnum.hxx>
00038 #include <TopAbs_Orientation.hxx>
00039 
00040 #include <TopoDS.hxx>
00041 #include <TopoDS_Face.hxx>
00042 #include <TopoDS_Shape.hxx>
00043 #include <TopoDS_Compound.hxx>
00044 #include <TopoDS_Shell.hxx>
00045 #include <TopoDS_Solid.hxx>
00046 #include <TopoDS_Vertex.hxx>
00047 #include <TopoDS_Edge.hxx>
00048 #include <TopoDS_Iterator.hxx>
00049 
00050 #include <TopTools_ListIteratorOfListOfShape.hxx>
00051 #include <TopTools_IndexedMapOfShape.hxx>
00052 #include <TopTools_DataMapOfShapeShape.hxx>
00053 
00054 #include <Bnd_Box.hxx>
00055 #include <BRepBndLib.hxx>
00056 #include <BRepMesh_IncrementalMesh.hxx>
00057 
00058 #include <BRep_Builder.hxx>
00059 #include <BRep_Tool.hxx>
00060 
00061 #include <TopExp.hxx>
00062 #include <TopExp_Explorer.hxx>
00063 
00064 #include <BRepLib_MakeFace.hxx>
00065 #include <BRepLib_FaceError.hxx>
00066 
00067 #include <BOPTools_DSFiller.hxx>
00068 
00069 #include <GEOMAlgo_WireSolid.hxx>
00070 #include <GEOMAlgo_ShellSolid.hxx>
00071 #include <GEOMAlgo_VertexSolid.hxx>
00072 #include <GEOMAlgo_ShapeSolid.hxx>
00073 #include <GEOMAlgo_SolidSolid.hxx>
00074 #include <GEOMAlgo_SurfaceTools.hxx>
00075 #include <GEOMAlgo_Tools.hxx>
00076 
00077 //=======================================================================
00078 //function : GEOMAlgo_FinderShapeOn
00079 //purpose  :
00080 //=======================================================================
00081 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
00082 :
00083   GEOMAlgo_ShapeAlgo()
00084 {
00085   myTolerance=0.0001;
00086   myShapeType=TopAbs_VERTEX;
00087   myState=GEOMAlgo_ST_UNKNOWN;
00088   myIsAnalytic=Standard_True;
00089 }
00090 //=======================================================================
00091 //function : ~
00092 //purpose  :
00093 //=======================================================================
00094 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
00095 {
00096 }
00097 //=======================================================================
00098 //function : SetSurface
00099 //purpose  :
00100 //=======================================================================
00101 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
00102 {
00103   mySurface=aS;
00104 }
00105 //=======================================================================
00106 //function : Surface
00107 //purpose  :
00108 //=======================================================================
00109 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
00110 {
00111   return mySurface;
00112 }
00113 //=======================================================================
00114 //function : SetShapeType
00115 //purpose  :
00116 //=======================================================================
00117 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
00118 {
00119   myShapeType=aType;
00120 }
00121 //=======================================================================
00122 //function : ShapeType
00123 //purpose  :
00124 //=======================================================================
00125 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
00126 {
00127   return myShapeType;
00128 }
00129 //=======================================================================
00130 //function : SetState
00131 //purpose  :
00132 //=======================================================================
00133 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
00134 {
00135   myState=aState;
00136 }
00137 //=======================================================================
00138 //function : State
00139 //purpose  :
00140 //=======================================================================
00141 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
00142 {
00143   return myState;
00144 }
00145 //=======================================================================
00146 // function: Shapes
00147 // purpose:
00148 //=======================================================================
00149 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
00150 {
00151   Standard_Boolean bIsConformState;
00152   Standard_Integer i, aNb;
00153   TopAbs_State aSt;
00154   TopTools_ListOfShape* pL;
00155   //
00156   pL=(TopTools_ListOfShape*) &myLS;
00157   pL->Clear();
00158   //
00159   aNb=myMSS.Extent();
00160   for (i=1; i<=aNb; ++i) {
00161     const TopoDS_Shape& aS=myMSS.FindKey(i);
00162     aSt=myMSS.FindFromIndex(i);
00163     //
00164     bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
00165     if (bIsConformState) {
00166       pL->Append(aS);
00167     }
00168   }
00169   return myLS;
00170 }
00171 //=======================================================================
00172 //function : Perform
00173 //purpose  :
00174 //=======================================================================
00175 void GEOMAlgo_FinderShapeOn::Perform()
00176 {
00177   myErrorStatus=0;
00178   myWarningStatus=0;
00179   myLS.Clear();
00180   myMSS.Clear();
00181   //
00182   if (!myResult.IsNull()){
00183     myResult.Nullify();
00184   }
00185   //
00186   CheckData();
00187   if(myErrorStatus) {
00188     return;
00189   }
00190   //
00191   // Initialize the context
00192   GEOMAlgo_ShapeAlgo::Perform();
00193   //
00194   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
00195   //
00196   MakeArgument1();
00197   if(myErrorStatus) {
00198     return;
00199   }
00200   //
00201   if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
00202     FindVertices();
00203     return;
00204   }
00205   //
00206   MakeArgument2();
00207   if(myErrorStatus) {
00208     return;
00209   }
00210   //
00211   Find();
00212   if(myErrorStatus || myWarningStatus) {
00213     return;
00214   }
00215   //
00216 }
00217 //=======================================================================
00218 //function : FindVertices
00219 //purpose  :
00220 //=======================================================================
00221 void GEOMAlgo_FinderShapeOn::FindVertices()
00222 {
00223   Standard_Integer i, aNb, iErr;
00224   TopAbs_State aSt;
00225   TopAbs_Orientation aOr;
00226   gp_Pnt aP;
00227   TopTools_IndexedMapOfShape aM;
00228   //
00229   TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
00230   const TopoDS_Face& aF=TopoDS::Face(aM(1));
00231   aOr=aF.Orientation();
00232   //
00233   aM.Clear();
00234   TopExp::MapShapes(myShape, myShapeType, aM);
00235   aNb=aM.Extent();
00236   if (!aNb) {
00237     myWarningStatus=10; // No found sub-shapes of type myShapeType
00238     return;
00239   }
00240   //
00241   for (i=1; i<=aNb; ++i) {
00242     const TopoDS_Shape& aS=aM(i);
00243     const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
00244     aP=BRep_Tool::Pnt(aV);
00245     iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
00246     if (aOr==TopAbs_REVERSED) {
00247       aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
00248     }
00249     myMSS.Add(aS, aSt);
00250   }
00251 }
00252 //=======================================================================
00253 //function : Find
00254 //purpose  :
00255 //=======================================================================
00256 void GEOMAlgo_FinderShapeOn::Find()
00257 {
00258   Standard_Integer i, aNb;
00259   Standard_Boolean bICS;
00260   TopTools_IndexedMapOfShape aM;
00261   //
00262   TopExp::MapShapes(myArg2, myShapeType, aM);
00263   //
00264   aNb=aM.Extent();
00265   if (!aNb) {
00266     myWarningStatus=10; // No found sub-shapes of type myShapeType
00267     return;
00268   }
00269   //
00270   bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
00271   if (!bICS || myIsAnalytic) {
00272     TopoDS_Compound aCmp;
00273     BRep_Builder aBB;
00274     //
00275     aBB.MakeCompound(aCmp);
00276     for (i=1; i<=aNb; ++i) {
00277       const TopoDS_Shape& aSi=aM(i);
00278       aBB.Add(aCmp, aSi);
00279     }
00280     //
00281     aM.Clear();
00282     aM.Add(aCmp);
00283     aNb=1;
00284   }
00285   //
00286   for (i=1; i<=aNb; ++i) {
00287     const TopoDS_Shape& aS=aM(i);
00288     Find(aS);
00289     if (myErrorStatus) {
00290       return;
00291     }
00292   }
00293 }
00294 //=======================================================================
00295 //function : Find
00296 //purpose  :
00297 //=======================================================================
00298 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
00299 {
00300   myErrorStatus=0;
00301   //
00302   Standard_Boolean bIsDone;
00303   Standard_Integer i, iErr;
00304   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
00305   TopTools_ListIteratorOfListOfShape aIt;
00306   BOPTools_DSFiller aDF;
00307   //
00308   // 1. Prepare DSFiller
00309   aDF.SetShapes (myArg1, aS);
00310   bIsDone=aDF.IsDone();
00311   if (!bIsDone) {
00312     myErrorStatus=30; // wrong args are used for DSFiller
00313     return;
00314   }
00315   aDF.Perform();
00316   bIsDone=aDF.IsDone();
00317   if (!bIsDone) {
00318     myErrorStatus=31; // DSFiller failed
00319     return;
00320   }
00321   //
00322   // 2. Find shapes
00323   GEOMAlgo_ShapeSolid* pSS;
00324   GEOMAlgo_VertexSolid aVXS;
00325   GEOMAlgo_WireSolid aWRS;
00326   GEOMAlgo_ShellSolid aSHS;
00327   GEOMAlgo_SolidSolid aSLS;
00328   //
00329   pSS=NULL;
00330   //
00331   switch (myShapeType) {
00332     case TopAbs_VERTEX:
00333       pSS=&aVXS;
00334       break;
00335     case TopAbs_EDGE:
00336       pSS=&aWRS;
00337       break;
00338     case TopAbs_FACE:
00339       pSS=&aSHS;
00340       break;
00341     case TopAbs_SOLID:
00342       aSLS.SetShape2(myArg2);
00343       pSS=&aSLS;
00344       break;
00345     default:
00346       myErrorStatus=12; // unallowed sub-shape type
00347       return;
00348   }
00349   //
00350   pSS->SetFiller(aDF);
00351   pSS->Perform();
00352   iErr=pSS->ErrorStatus();
00353   if (iErr) {
00354     myErrorStatus=32; // builder ShapeSolid failed
00355     return;
00356   }
00357   //
00358   for (i=0; i<3; ++i) {
00359     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
00360     aIt.Initialize(aLS);
00361     for (; aIt.More(); aIt.Next()) {
00362       const TopoDS_Shape& aSImage=aIt.Value();
00363       if (myImages.IsBound(aSImage)) {
00364         const TopoDS_Shape& aSx=myImages.Find(aSImage);
00365         myMSS.Add(aSx, aSts[i]);
00366       }
00367       else {
00368         myErrorStatus=33;// can not find original shape
00369         return;
00370       }
00371     }
00372   }
00373 }
00374 //=======================================================================
00375 //function : MakeArgument1
00376 //purpose  :
00377 //=======================================================================
00378 void GEOMAlgo_FinderShapeOn::MakeArgument1()
00379 {
00380   myErrorStatus=0;
00381   //
00382   Standard_Integer i, aNb;
00383   TopAbs_ShapeEnum aType;
00384   BRepLib_FaceError aFErr;
00385   BRepLib_MakeFace aMF;
00386   TopTools_IndexedMapOfShape aM;
00387   BRep_Builder aBB;
00388   TopoDS_Face aFace;
00389   TopoDS_Shell aSh;
00390   TopoDS_Solid aSd;
00391   //
00392   // Argument 1
00393   if (!myIsAnalytic) {
00394 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00395     aMF.Init(mySurface, Standard_True, Precision::Confusion());
00396 #else
00397     aMF.Init(mySurface, Standard_True);
00398 #endif
00399     aFErr=aMF.Error();
00400     if (aFErr!=BRepLib_FaceDone) {
00401       myErrorStatus=20; // can not build the face
00402       return;
00403     }
00404     //
00405     const TopoDS_Shape& aF=aMF.Shape();
00406     aFace=TopoDS::Face(aF);
00407     //
00408     // update tolerances
00409     aM.Add(aF);
00410     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
00411     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
00412     aNb=aM.Extent();
00413     for (i=1; i<=aNb; ++i) {
00414       const TopoDS_Shape& aS=aM(i);
00415       aType=aS.ShapeType();
00416       switch (aType) {
00417       case TopAbs_VERTEX: {
00418         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
00419         aBB.UpdateVertex(aVx, myTolerance);
00420       }
00421         break;
00422       case TopAbs_EDGE: {
00423         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
00424         aBB.UpdateEdge(aEx, myTolerance);
00425       }
00426         break;
00427       case TopAbs_FACE: {
00428         const TopoDS_Face& aFx=TopoDS::Face(aS);
00429         aBB.UpdateFace(aFx, myTolerance);
00430       }
00431         break;
00432       default:
00433         break;
00434       }
00435     }
00436   } //
00437   else {
00438     aBB.MakeFace(aFace, mySurface, myTolerance);
00439   }
00440   //
00441   // make solid
00442   aBB.MakeShell(aSh);
00443   aBB.Add(aSh, aFace);
00444   aBB.MakeSolid(aSd);
00445   aBB.Add(aSd, aSh);
00446   myArg1=aSd;
00447 }
00448 //=======================================================================
00449 //function : MakeArgument2
00450 //purpose  :
00451 //=======================================================================
00452 void GEOMAlgo_FinderShapeOn::MakeArgument2()
00453 {
00454   myErrorStatus=0;
00455   //
00456   TopoDS_Shape aSC;
00457   TopTools_DataMapOfShapeShape aOriginals;
00458   //
00459   myImages.Clear();
00460   //
00461   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
00462   //
00463   myArg2=aSC;
00464 }
00465 //=======================================================================
00466 //function : CheckData
00467 //purpose  :
00468 //=======================================================================
00469 void GEOMAlgo_FinderShapeOn::CheckData()
00470 {
00471   myErrorStatus=0;
00472   //
00473   if(mySurface.IsNull()) {
00474     myErrorStatus=10; // mySurface=NULL
00475     return;
00476   }
00477   //
00478   if (myShape.IsNull()) {
00479     myErrorStatus=11; // myShape=NULL
00480     return;
00481   }
00482   //
00483   if (!(myShapeType==TopAbs_VERTEX ||
00484         myShapeType==TopAbs_EDGE ||
00485         myShapeType==TopAbs_FACE ||
00486         myShapeType==TopAbs_SOLID)) {
00487     myErrorStatus=12; // unallowed sub-shape type
00488     return;
00489   }
00490   //
00491   if (myState==GEOMAlgo_ST_UNKNOWN ||
00492       myState==GEOMAlgo_ST_INOUT) {
00493     myErrorStatus=13; // unallowed state type
00494     return;
00495   }
00496 }
00497 //
00498 //=======================================================================
00499 //function : CopySource
00500 //purpose  :
00501 //=======================================================================
00502 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
00503                                         TopTools_DataMapOfShapeShape& aImages,
00504                                         TopTools_DataMapOfShapeShape& aOriginals,
00505                                         TopoDS_Shape& aEx)
00506 {
00507   Standard_Boolean bFree;
00508   TopAbs_ShapeEnum aType;
00509   Standard_Integer aR;
00510   BRep_Builder BB;
00511   TopoDS_Iterator aIt;
00512   //
00513   aType=aE.ShapeType();
00514   //
00515   if (aOriginals.IsBound(aE)) {
00516     aEx=aOriginals.ChangeFind(aE);
00517     return;
00518   }
00519   else {
00520     aEx=aE.EmptyCopied();
00521     aOriginals.Bind(aE, aEx);
00522     aImages.Bind(aEx, aE);
00523   }
00524   //
00525   aR=(Standard_Integer)aType+1;
00526   if (aR>TopAbs_VERTEX) {
00527     return;
00528   }
00529   //
00530   bFree=aEx.Free();
00531   aEx.Free(Standard_True);
00532   //
00533   aType=(TopAbs_ShapeEnum) aR;
00534   //
00535   aIt.Initialize(aE);//, Standard_False);
00536   for (; aIt.More();  aIt.Next()) {
00537     const TopoDS_Shape& aV=aIt.Value();
00538     TopoDS_Shape aVx;
00539     //
00540     CopySource (aV, aImages, aOriginals, aVx);
00541     //
00542     aVx.Orientation(aV.Orientation());
00543     BB.Add(aEx, aVx);
00544   }
00545   //
00546   aEx.Free(bFree);
00547 }
00548 //
00549 //=======================================================================
00550 //function : BuildTriangulation
00551 //purpose  :
00552 //=======================================================================
00553 Standard_Boolean
00554   GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
00555 {
00556   // calculate deflection
00557   Standard_Real aDeviationCoefficient = 0.001;
00558 
00559   Bnd_Box B;
00560   BRepBndLib::Add(theShape, B);
00561   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
00562   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
00563 
00564   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
00565   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
00566   Standard_Real aHLRAngle = 0.349066;
00567 
00568   // build triangulation
00569   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
00570 
00571   // check triangulation
00572   bool isTriangulation = true;
00573 
00574   TopExp_Explorer exp (theShape, TopAbs_FACE);
00575   if (exp.More())
00576   {
00577     TopLoc_Location aTopLoc;
00578     Handle(Poly_Triangulation) aTRF;
00579     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
00580     if (aTRF.IsNull()) {
00581       isTriangulation = false;
00582     }
00583   }
00584   else // no faces, try edges
00585   {
00586     TopExp_Explorer expe (theShape, TopAbs_EDGE);
00587     if (!expe.More()) {
00588       isTriangulation = false;
00589     }
00590     else {
00591       TopLoc_Location aLoc;
00592       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
00593       if (aPE.IsNull()) {
00594         isTriangulation = false;
00595       }
00596     }
00597   }
00598 
00599   return isTriangulation;
00600 }
00601 
00602 //
00603 // myErrorStatus :
00604 //
00605 // 10 -mySurface=NULL
00606 // 11 -myShape=NULL
00607 // 12 -unallowed type of sub-shapes
00608 // 13 -unallowed state
00609 // 20 -can not build the face
00610 // 30 -wrong args are used for DSFiller
00611 // 31 -DSFiller failed
00612 // 32 -builder ShapeSolid failed
00613 // 33 -can not find original shape
00614 //
00615 // myWarningStatus
00616 //
00617 // 10 - sub-shapes of type myShapeType can not be fond in myShape