Back to index

salome-geom  6.5.0
GEOMAlgo_GetInPlace_2.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 // File:      GEOMAlgo_GetInPlace_2.cxx
00023 // Created:
00024 // Author:    Peter KURNEV
00025 
00026 #include <GEOMAlgo_GetInPlace.hxx>
00027 
00028 #include <TopAbs_ShapeEnum.hxx>
00029 
00030 #include <gp_Pnt.hxx>
00031 
00032 #include <TopoDS_Iterator.hxx>
00033 #include <TopoDS_Compound.hxx>
00034 #include <TopoDS_Shape.hxx>
00035 #include <TopoDS_Vertex.hxx>
00036 
00037 #include <BRep_Tool.hxx>
00038 #include <BRep_Builder.hxx>
00039 
00040 #include <TopTools_ListIteratorOfListOfShape.hxx>
00041 #include <TopTools_ListOfShape.hxx>
00042 #include <TopTools_IndexedMapOfShape.hxx>
00043 
00044 #include <TopExp.hxx>
00045 
00046 #include <GProp_GProps.hxx>
00047 #include <BRepGProp.hxx>
00048 
00049 
00050 static
00051   Standard_Integer Dimension(const TopAbs_ShapeEnum aType);
00052 static
00053   void PointProperties(const TopoDS_Shape& aS,
00054                      GProp_GProps& aGProps);
00055 
00056 //=======================================================================
00057 //function : CheckGProps
00058 //purpose  :
00059 //=======================================================================
00060 void GEOMAlgo_GetInPlace::CheckGProps()
00061 {
00062   myFound=Standard_False;
00063   CheckGProps(myArgument);
00064 }
00065 //=======================================================================
00066 //function : CheckGProps
00067 //purpose  :
00068 //=======================================================================
00069 void GEOMAlgo_GetInPlace::CheckGProps(const TopoDS_Shape& aS1)
00070 {
00071   Standard_Boolean bOnlyClosed;
00072   Standard_Integer iDim, aNbS2;
00073   Standard_Real aMass1, aMass2, aD2, aTolCG2, dM;
00074   TopAbs_ShapeEnum  aType1;
00075   gp_Pnt aCG1, aCG2;
00076   TopoDS_Iterator aIt;
00077   TopoDS_Compound aC2;
00078   BRep_Builder aBB;
00079   TopTools_ListIteratorOfListOfShape aItLS;
00080   //
00081   myErrorStatus=0;
00082   //
00083   aType1=aS1.ShapeType();
00084   if (aType1==TopAbs_COMPOUND) {
00085     aIt.Initialize(aS1);
00086     for(; aIt.More(); aIt.Next()) {
00087       const TopoDS_Shape& aS1x=aIt.Value();
00088       CheckGProps(aS1x);
00089       if (!myFound) {
00090        return;
00091       }
00092     }
00093   }
00094   //
00095   iDim=Dimension(aType1);
00096   //
00097   if (!myImages.IsBound(aS1)) {
00098     // it should not be.
00099     return;
00100   }
00101   const TopTools_ListOfShape& aLS2=myImages.Find(aS1);
00102   aNbS2=aLS2.Extent();
00103   if (!aNbS2) {
00104     // it should not be.
00105     return;
00106   }
00107   //
00108   aBB.MakeCompound(aC2);
00109   aItLS.Initialize(aLS2);
00110   for (; aItLS.More(); aItLS.Next()) {
00111     const TopoDS_Shape& aS2x=aItLS.Value();
00112     aBB.Add(aC2, aS2x);
00113   }
00114   //-------------------------
00115   GProp_GProps aG1, aG2;
00116   //
00117   aTolCG2=myTolCG*myTolCG;
00118   bOnlyClosed=Standard_False;
00119   //
00120   if (iDim==0) {
00121     PointProperties(aS1, aG1);
00122     PointProperties(aC2, aG2);
00123   }
00124   else if (iDim==1) {
00125     BRepGProp::LinearProperties(aS1, aG1);
00126     BRepGProp::LinearProperties(aC2, aG2);
00127   }
00128   else if (iDim==2) {
00129     BRepGProp::SurfaceProperties(aS1, aG1);
00130     BRepGProp::SurfaceProperties(aC2, aG2);
00131   }
00132   else if (iDim==3) {
00133     BRepGProp::VolumeProperties(aS1, aG1, bOnlyClosed);
00134     BRepGProp::VolumeProperties(aC2, aG2, bOnlyClosed);
00135   }
00136   //
00137   aMass1=aG1.Mass();
00138   aMass2=aG2.Mass();
00139   aCG1=aG1.CentreOfMass();
00140   aCG2=aG2.CentreOfMass();
00141   //
00142   dM=fabs(aMass1-aMass2);
00143   if (aMass1 > myTolMass) {
00144     dM=dM/aMass1;
00145   }
00146   //
00147   aD2=aCG1.SquareDistance(aCG2);
00148   //
00149   if ((dM > myTolMass) || (aD2 > aTolCG2)) {
00150     myFound=Standard_False;
00151     return;
00152   }
00153   myFound=Standard_True;
00154 }
00155 //=======================================================================
00156 //function : Dimension
00157 //purpose  :
00158 //=======================================================================
00159 Standard_Integer Dimension(const TopAbs_ShapeEnum aType)
00160 {
00161   Standard_Integer iDim;
00162   //
00163   iDim=-1;
00164   switch (aType) {
00165     case TopAbs_VERTEX:
00166       iDim=0;
00167       break;
00168     case TopAbs_EDGE:
00169     case TopAbs_WIRE:
00170       iDim=1;
00171       break;
00172     case TopAbs_FACE:
00173     case TopAbs_SHELL:
00174       iDim=2;
00175       break;
00176     case TopAbs_SOLID:
00177     case TopAbs_COMPSOLID:
00178       iDim=3;
00179       break;
00180     default:
00181       break;
00182   }
00183   return iDim;
00184 }
00185 //=======================================================================
00186 //class : GEOMAlgo_GProps
00187 //purpose  :
00188 //=======================================================================
00189 class GEOMAlgo_GProps : public GProp_GProps {
00190  public:
00191   GEOMAlgo_GProps() : GProp_GProps() {
00192   };
00193   //
00194   GEOMAlgo_GProps(const gp_Pnt& aPLoc) : GProp_GProps(aPLoc) {
00195   };
00196   //
00197   ~GEOMAlgo_GProps() {
00198   };
00199   //
00200   void SetMass(const Standard_Real aMass) {
00201     dim=aMass;
00202   }
00203   //
00204   void SetCG(const gp_Pnt& aPCG) {
00205     g=aPCG;
00206   }
00207 };
00208 //=======================================================================
00209 //function : PointProperties
00210 //purpose  :
00211 //=======================================================================
00212 void PointProperties(const TopoDS_Shape& aS, GProp_GProps& aGProps)
00213 {
00214   Standard_Integer i, aNbS;
00215   Standard_Real aDensity;
00216   gp_Pnt aPX;
00217   TopTools_IndexedMapOfShape aMS;
00218   //
00219   aDensity=1.;
00220   //
00221   TopExp::MapShapes(aS, TopAbs_VERTEX, aMS);
00222   aNbS=aMS.Extent();
00223   for (i=1; i<=aNbS; ++i) {
00224     GEOMAlgo_GProps aGPropsX;
00225     //
00226     const TopoDS_Vertex& aVX=*((TopoDS_Vertex*)&aMS(i));
00227     aPX=BRep_Tool::Pnt(aVX);
00228     aGPropsX.SetMass(1.);
00229     aGPropsX.SetCG(aPX);
00230     aGProps.Add(aGPropsX, aDensity);
00231   }
00232 }