Salome HOME
Internal issue 0022865: Restructurization of Partition packages.
[modules/geom.git] / src / GEOMAlgo_NEW / GEOMAlgo_BuilderTools.cxx
1 // Copyright (C) 2007-2011  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_BuilderTools.cxx
23 // Author:      Peter KURNEV
24
25 #include <GEOMAlgo_BuilderTools.hxx>
26
27 #include <Basics_OCCTVersion.hxx>
28
29 #include <TColStd_Array1OfReal.hxx>
30
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Vec.hxx>
33 #include <gp_Dir.hxx>
34 #include <gp_Pnt.hxx>
35
36 #include <TColgp_Array1OfPnt.hxx>
37
38 #include <Poly_Triangulation.hxx>
39 #include <Poly_Array1OfTriangle.hxx>
40 #include <Poly_Triangle.hxx>
41
42 #include <Geom2d_Curve.hxx>
43 #include <Geom2dInt_Geom2dCurveTool.hxx>
44
45 #include <TopLoc_Location.hxx>
46 #include <TopAbs_Orientation.hxx>
47
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Iterator.hxx>
50 #include <TopoDS_Wire.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopExp_Explorer.hxx>
54
55 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
56 #include <TopExp.hxx>
57 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
58 #endif
59
60 #include <BRep_Tool.hxx>
61 #include <BRepBndLib.hxx>
62 #include <BRepMesh_FastDiscret.hxx>
63 #include <Bnd_Box.hxx>
64 #include <BRepAdaptor_Curve2d.hxx>
65
66 static 
67   Standard_Integer ComputeProps(const TopoDS_Face& aF,
68                                 Standard_Real& aA,
69                                 Standard_Real& aV);
70 static
71   void BuildTriangulation(const TopoDS_Face& aF);
72
73 //=======================================================================
74 //function : IsHole
75 //purpose  : 
76 //=======================================================================
77   Standard_Boolean GEOMAlgo_BuilderTools::IsHole(const TopoDS_Shape& aW,
78                                                  const TopoDS_Shape& aFace)
79 {
80   Standard_Boolean bIsHole;
81   Standard_Integer i, aNbS;
82   Standard_Real aT1, aT2, aS;
83   Standard_Real aU1, aU2, aU, dU;
84   Standard_Real aX1, aY1, aX0, aY0;
85   TopAbs_Orientation aOr;
86   
87   gp_Pnt2d aP2D0, aP2D1;
88   Handle(Geom2d_Curve) aC2D;
89   TopoDS_Face aF, aFF;
90   TopoDS_Iterator aItW;
91   //
92   bIsHole=Standard_False;
93   //
94   aF=TopoDS::Face(aFace);
95   aFF=aF;
96   aFF.Orientation(TopAbs_FORWARD);
97   //
98   aS=0.;
99   aItW.Initialize(aW);
100   for (; aItW.More(); aItW.Next()) { 
101     const TopoDS_Edge& aE=TopoDS::Edge(aItW.Value());
102     aOr=aE.Orientation();
103     if (!(aOr==TopAbs_FORWARD || 
104           aOr==TopAbs_REVERSED)) {
105       continue;
106     }
107     //
108     aC2D=BRep_Tool::CurveOnSurface(aE, aFF, aT1, aT2);
109     if (aC2D.IsNull()) {
110       break; //xx
111     }
112     //
113     BRepAdaptor_Curve2d aBAC2D(aE, aFF);
114     aNbS=Geom2dInt_Geom2dCurveTool::NbSamples(aBAC2D);
115     if (aNbS>2) {
116       aNbS*=4;
117     }
118     //
119     dU=(aT2-aT1)/(Standard_Real)(aNbS-1);
120     aU =aT1;
121     aU1=aT1;
122     aU2=aT2;
123     if (aOr==TopAbs_REVERSED) {
124       aU =aT2;
125       aU1=aT2;
126       aU2=aT1;
127       dU=-dU;
128     }
129     //
130     aC2D->D0(aU, aP2D0);
131     for(i=2; i<=aNbS; i++) {
132       aU=aU1+(i-1)*dU;
133       aC2D->D0(aU, aP2D1);
134       aP2D0.Coord(aX0, aY0);
135       aP2D1.Coord(aX1, aY1);
136       //
137       aS=aS+(aY0+aY1)*(aX1-aX0); 
138       //
139       aP2D0=aP2D1;
140     }
141   }//for (; aItW.More(); aItW.Next()) { 
142   bIsHole=(aS>0.);
143   return bIsHole;
144 }
145 //=======================================================================
146 //function : IsHole
147 //purpose  : 
148 //=======================================================================
149   Standard_Boolean GEOMAlgo_BuilderTools::IsHole(const TopoDS_Shape& aShell)
150 {
151   Standard_Boolean bIsHole;
152   Standard_Integer iRet;
153   Standard_Real aAi, aA, aV, aVi;
154   TopExp_Explorer aExp;
155   //
156   aA=0.;
157   aV=0.;
158   aExp.Init(aShell, TopAbs_FACE);
159   for (; aExp.More(); aExp.Next()) {
160     const TopoDS_Face& aF=TopoDS::Face(aExp.Current());
161     iRet=ComputeProps(aF, aAi, aVi);
162     if (!iRet) {
163       aA+=aAi;
164       aV+=aVi;
165     }
166   }
167   //
168   bIsHole=aV<0.;
169   return bIsHole;
170 }
171 //=======================================================================
172 //function : ComputeProps
173 //purpose  : 
174 //=======================================================================
175 Standard_Integer ComputeProps(const TopoDS_Face& aF,
176                               Standard_Real& aA,
177                               Standard_Real& aV)
178 {
179   Standard_Integer j, i, i1, i2, aNbNodes, aNbTrigs, n[3];
180   Standard_Real aAi, aVi;
181   gp_Pnt aP[3], aGC, aGC1;
182   TopLoc_Location aLoc;
183   TopAbs_Orientation aOr;
184   Handle(Poly_Triangulation) aTri;
185   //
186   aA=0.;
187   aV=0.;
188   //
189   aTri=BRep_Tool::Triangulation(aF, aLoc);
190   if(aTri.IsNull()) {
191     BuildTriangulation(aF);
192     aTri=BRep_Tool::Triangulation(aF, aLoc);
193     if(aTri.IsNull()) {
194       return 1;// a face is without triangulation
195     }
196   }
197   //
198   aNbNodes=aTri->NbNodes();
199   aNbTrigs=aTri->NbTriangles();
200   if (!aNbTrigs){
201     return 2;//no triangles
202   }
203   //
204   aOr=aF.Orientation();
205   //
206   const TColgp_Array1OfPnt& aNodes=aTri->Nodes();
207   const Poly_Array1OfTriangle& aTriangles=aTri->Triangles();
208   //
209   i1=aTriangles.Lower();
210   i2=aTriangles.Upper();
211   //
212   for (i=i1; i<=i2; ++i){
213     const Poly_Triangle& aTriangle=aTriangles.Value(i);
214     aTriangle.Get(n[0], n[1], n[2]);
215     aGC.SetCoord(0.,0.,0.);
216     for (j=0; j<3; ++j) {
217       aP[j]=aNodes.Value(n[j]);
218       aGC.ChangeCoord()+=aP[j].XYZ();
219     }
220     aGC.ChangeCoord()*=0.333333333333;
221     //
222     // Normal
223     gp_Vec aV01(aP[0], aP[1]);
224     gp_Vec aV12(aP[1], aP[2]);
225     gp_Vec aVN=aV01^aV12;
226     aAi=aVN.Magnitude();
227     aA=aA+aAi;
228     //
229     if (aAi>0.0000001) {
230       Standard_Real aSx, aZx;
231       gp_Dir aDN(aVN);
232       if (aOr==TopAbs_REVERSED) {
233         aDN.Reverse();
234       }
235       //
236       aSx=aAi*aDN.Z();
237       aZx=aGC.Z();
238       aVi=aZx*aSx;
239       aV=aV+aVi;
240     }
241   }
242   return 0;
243 }
244 //=======================================================================
245 //function : BuildTriangulation
246 //purpose  : 
247 //=======================================================================
248 void BuildTriangulation(const TopoDS_Face& aF)
249 {
250   Standard_Boolean bWithShare;
251   Standard_Real aDiscret, aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
252   Standard_Real dX, dY, dZ, dMax, aCoeff, aAngle;
253   Bnd_Box aBox;
254   //
255   bWithShare=Standard_False;
256   aAngle=0.5;
257   //
258   BRepBndLib::Add(aF, aBox);
259   //
260   // aDiscret
261   aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
262   dX=aXmax-aXmin;
263   dY=aYmax-aYmin;
264   dZ=aZmax-aZmin;
265   dMax=dX;
266   if (dY>dMax) {
267     dMax=dY;
268   }
269   if (dZ>dMax) {
270     dMax=dZ;
271   }
272   //
273   aCoeff=0.1;
274   aDiscret=aCoeff*dMax;
275   //
276   BRepMesh_FastDiscret aMesher(aDiscret,
277                                aAngle,
278                                aBox,
279                                bWithShare,
280                                Standard_True,
281                                Standard_False,
282                                Standard_True);
283
284 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
285   TopTools_IndexedDataMapOfShapeListOfShape anAncestors;
286   TopExp::MapShapesAndAncestors(aF, TopAbs_EDGE, TopAbs_FACE, anAncestors);
287   aMesher.Add(aF, anAncestors);
288 #else
289   aMesher.Add(aF);
290 #endif
291 }