Salome HOME
This commit was generated by cvs2git to create tag 'TRIPOLI_323'.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_GetInPlace_2.cxx
1 // Copyright (C) 2007-2012  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_GetInPlace_2.cxx
23 // Created:
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_GetInPlace.hxx>
27
28 #include <TopAbs_ShapeEnum.hxx>
29
30 #include <gp_Pnt.hxx>
31
32 #include <TopoDS_Iterator.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Vertex.hxx>
36
37 #include <BRep_Tool.hxx>
38 #include <BRep_Builder.hxx>
39
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <TopExp.hxx>
45
46 #include <GProp_GProps.hxx>
47 #include <BRepGProp.hxx>
48
49
50 static
51   Standard_Integer Dimension(const TopAbs_ShapeEnum aType);
52 static
53   void PointProperties(const TopoDS_Shape& aS,
54                        GProp_GProps& aGProps);
55
56 //=======================================================================
57 //function : CheckGProps
58 //purpose  :
59 //=======================================================================
60 void GEOMAlgo_GetInPlace::CheckGProps()
61 {
62   myFound=Standard_False;
63   CheckGProps(myArgument);
64 }
65 //=======================================================================
66 //function : CheckGProps
67 //purpose  :
68 //=======================================================================
69 void GEOMAlgo_GetInPlace::CheckGProps(const TopoDS_Shape& aS1)
70 {
71   Standard_Boolean bOnlyClosed;
72   Standard_Integer iDim, aNbS2;
73   Standard_Real aMass1, aMass2, aD2, aTolCG2, dM;
74   TopAbs_ShapeEnum  aType1;
75   gp_Pnt aCG1, aCG2;
76   TopoDS_Iterator aIt;
77   TopoDS_Compound aC2;
78   BRep_Builder aBB;
79   TopTools_ListIteratorOfListOfShape aItLS;
80   //
81   myErrorStatus=0;
82   //
83   aType1=aS1.ShapeType();
84   if (aType1==TopAbs_COMPOUND) {
85     aIt.Initialize(aS1);
86     for(; aIt.More(); aIt.Next()) {
87       const TopoDS_Shape& aS1x=aIt.Value();
88       CheckGProps(aS1x);
89       if (!myFound) {
90         return;
91       }
92     }
93   }
94   //
95   iDim=Dimension(aType1);
96   //
97   if (!myImages.IsBound(aS1)) {
98     // it should not be.
99     return;
100   }
101   const TopTools_ListOfShape& aLS2=myImages.Find(aS1);
102   aNbS2=aLS2.Extent();
103   if (!aNbS2) {
104     // it should not be.
105     return;
106   }
107   //
108   aBB.MakeCompound(aC2);
109   aItLS.Initialize(aLS2);
110   for (; aItLS.More(); aItLS.Next()) {
111     const TopoDS_Shape& aS2x=aItLS.Value();
112     aBB.Add(aC2, aS2x);
113   }
114   //-------------------------
115   GProp_GProps aG1, aG2;
116   //
117   aTolCG2=myTolCG*myTolCG;
118   bOnlyClosed=Standard_False;
119   //
120   if (iDim==0) {
121     PointProperties(aS1, aG1);
122     PointProperties(aC2, aG2);
123   }
124   else if (iDim==1) {
125     BRepGProp::LinearProperties(aS1, aG1);
126     BRepGProp::LinearProperties(aC2, aG2);
127   }
128   else if (iDim==2) {
129     BRepGProp::SurfaceProperties(aS1, aG1);
130     BRepGProp::SurfaceProperties(aC2, aG2);
131   }
132   else if (iDim==3) {
133     BRepGProp::VolumeProperties(aS1, aG1, bOnlyClosed);
134     BRepGProp::VolumeProperties(aC2, aG2, bOnlyClosed);
135   }
136   //
137   aMass1=aG1.Mass();
138   aMass2=aG2.Mass();
139   aCG1=aG1.CentreOfMass();
140   aCG2=aG2.CentreOfMass();
141   //
142   dM=fabs(aMass1-aMass2);
143   if (aMass1 > myTolMass) {
144     dM=dM/aMass1;
145   }
146   //
147   aD2=aCG1.SquareDistance(aCG2);
148   //
149   if ((dM > myTolMass) || (aD2 > aTolCG2)) {
150     myFound=Standard_False;
151     return;
152   }
153   myFound=Standard_True;
154 }
155 //=======================================================================
156 //function : Dimension
157 //purpose  :
158 //=======================================================================
159 Standard_Integer Dimension(const TopAbs_ShapeEnum aType)
160 {
161   Standard_Integer iDim;
162   //
163   iDim=-1;
164   switch (aType) {
165     case TopAbs_VERTEX:
166       iDim=0;
167       break;
168     case TopAbs_EDGE:
169     case TopAbs_WIRE:
170       iDim=1;
171       break;
172     case TopAbs_FACE:
173     case TopAbs_SHELL:
174       iDim=2;
175       break;
176     case TopAbs_SOLID:
177     case TopAbs_COMPSOLID:
178       iDim=3;
179       break;
180     default:
181       break;
182   }
183   return iDim;
184 }
185 //=======================================================================
186 //class : GEOMAlgo_GProps
187 //purpose  :
188 //=======================================================================
189 class GEOMAlgo_GProps : public GProp_GProps {
190  public:
191   GEOMAlgo_GProps() : GProp_GProps() {
192   };
193   //
194   GEOMAlgo_GProps(const gp_Pnt& aPLoc) : GProp_GProps(aPLoc) {
195   };
196   //
197   ~GEOMAlgo_GProps() {
198   };
199   //
200   void SetMass(const Standard_Real aMass) {
201     dim=aMass;
202   }
203   //
204   void SetCG(const gp_Pnt& aPCG) {
205     g=aPCG;
206   }
207 };
208 //=======================================================================
209 //function : PointProperties
210 //purpose  :
211 //=======================================================================
212 void PointProperties(const TopoDS_Shape& aS, GProp_GProps& aGProps)
213 {
214   Standard_Integer i, aNbS;
215   Standard_Real aDensity;
216   gp_Pnt aPX;
217   TopTools_IndexedMapOfShape aMS;
218   //
219   aDensity=1.;
220   //
221   TopExp::MapShapes(aS, TopAbs_VERTEX, aMS);
222   aNbS=aMS.Extent();
223   for (i=1; i<=aNbS; ++i) {
224     GEOMAlgo_GProps aGPropsX;
225     //
226     const TopoDS_Vertex& aVX=*((TopoDS_Vertex*)&aMS(i));
227     aPX=BRep_Tool::Pnt(aVX);
228     aGPropsX.SetMass(1.);
229     aGPropsX.SetCG(aPX);
230     aGProps.Add(aGPropsX, aDensity);
231   }
232 }