Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_GetInPlace_1.cxx
1 // Copyright (C) 2007-2013  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
23 // File:        GEOMAlgo_GetInPlace_1.cxx
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_GetInPlace.hxx>
27
28 #include <math.h>
29
30 #include <gp_Pnt.hxx>
31 #include <gp_Vec.hxx>
32 #include <gp_Pnt2d.hxx>
33 #include <gp_Dir2d.hxx>
34
35 #include <Geom_Surface.hxx>
36 #include <Geom2d_Curve.hxx>
37 #include <Geom2d_Line.hxx>
38 #include <Geom2d_TrimmedCurve.hxx>
39 #include <Geom2dAdaptor_Curve.hxx>
40 #include <Geom2dHatch_Hatcher.hxx>
41 #include <Geom2dHatch_Intersector.hxx>
42 #include <HatchGen_Domain.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44
45 #include <TopAbs_State.hxx>
46 #include <TopAbs_ShapeEnum.hxx>
47 #include <TopAbs_Orientation.hxx>
48
49 #include <TopoDS_Vertex.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <TopoDS_Face.hxx>
53
54 #include <BRep_Tool.hxx>
55 #include <TopExp_Explorer.hxx>
56
57 #include <BRepTools.hxx>
58 #include <BRepClass3d_SolidClassifier.hxx>
59
60 #include <IntTools_Tools.hxx>
61
62 #include <BOPTools_Tools3D.hxx>
63 #include <BOPTools_Tools2D.hxx>
64
65
66
67 static
68   Standard_Integer PntInEdge(const TopoDS_Edge& aF,
69                              gp_Pnt& aP);
70 static
71   Standard_Integer PntInEdge(const TopoDS_Edge& aF,
72                              gp_Pnt& aP,
73                              Standard_Real& aT);
74 static
75   Standard_Integer PntInFace(const TopoDS_Face& aF,
76                              gp_Pnt& aP);
77 static
78   Standard_Integer PntInFace(const TopoDS_Face& aF,
79                              gp_Pnt& aP,
80                              gp_Pnt2d& theP2D);
81 static
82   Standard_Integer PntInSolid(const TopoDS_Solid& aZ,
83                               const Standard_Real aTol,
84                               gp_Pnt& aP);
85
86
87 //=======================================================================
88 //function : CheckCoincidence
89 //purpose  :
90 //=======================================================================
91 Standard_Boolean GEOMAlgo_GetInPlace::CheckCoincidence(const TopoDS_Shape& aS1,
92                                                        const TopoDS_Shape& aS2)
93 {
94   Standard_Boolean bOk;
95   Standard_Integer iErr;
96   Standard_Real aTol2;
97   TopAbs_ShapeEnum aType1, aType2;
98   TopAbs_State aState;
99   gp_Pnt aP1, aP2;
100   //
101   myErrorStatus=0;
102   //
103   iErr=0;
104   bOk=Standard_False;
105   aTol2=myTolerance*myTolerance;
106   aType1=aS1.ShapeType();
107   aType2=aS2.ShapeType();
108   //
109   // 1. A point on shape #2 -> aP2
110   if (myMapShapePnt.IsBound(aS2)) {
111     aP2=myMapShapePnt.Find(aS2);
112   }
113   else {//else 1
114     if (aType2==TopAbs_VERTEX) {
115       const TopoDS_Vertex& aV2=*((TopoDS_Vertex*)&aS2);
116       aP2=BRep_Tool::Pnt(aV2);
117     }
118     //
119     else if (aType2==TopAbs_EDGE) {
120       const TopoDS_Edge& aE2=*((TopoDS_Edge*)&aS2);
121       iErr=PntInEdge(aE2, aP2);
122     }
123     //
124     else if (aType2==TopAbs_FACE) {
125       const TopoDS_Face& aF2=*((TopoDS_Face*)&aS2);
126       iErr=PntInFace(aF2, aP2);
127     }
128     //
129     else if (aType2==TopAbs_SOLID) {
130       const TopoDS_Solid& aZ2=*((TopoDS_Solid*)&aS2);
131       iErr=PntInSolid(aZ2, myTolerance, aP2);
132     }
133     //
134     else {
135       iErr=1;
136     }
137     //
138     if (iErr) {
139       myErrorStatus=50;
140       return bOk;
141     }
142     //
143     myMapShapePnt.Bind(aS2, aP2);
144   } //else 1
145   //
146   // 2. Project the point aP2 on shape #1 and check
147   if (aType1==TopAbs_EDGE) {
148     Standard_Integer aNbPoints;
149     Standard_Real aDmin, aT, aT1, aT2, dT;
150     //
151     const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
152     //
153     GeomAPI_ProjectPointOnCurve& aPPC=myContext->ProjPC(aE1);
154     aPPC.Perform(aP2);
155     aNbPoints=aPPC.NbPoints();
156     if (aNbPoints) {
157       aDmin=aPPC.LowerDistance();
158       aT=aPPC.LowerDistanceParameter();
159       if (aDmin < myTolerance) {
160         dT=1.e-12;
161         BRep_Tool::Curve(aE1, aT1, aT2);
162         if(aT > (aT1-dT) && aT < (aT2+dT)) {
163           bOk=Standard_True;
164         }
165       }
166     }
167     //else {
168     // iErr=2;
169     //}
170   }//if (aType1==TopAbs_EDGE) {
171   //
172   else if (aType1==TopAbs_FACE) {
173     const TopoDS_Face& aF1=*((TopoDS_Face*)&aS1);
174     //
175     bOk=myContext->IsValidPointForFace(aP2, aF1, myTolerance);
176   }
177   //
178   else if (aType1==TopAbs_SOLID) {
179     const TopoDS_Solid& aZ1=*((TopoDS_Solid*)&aS1);
180     //
181     BRepClass3d_SolidClassifier& aSC=myContext->SolidClassifier(aZ1);
182     aSC.Perform(aP2, myTolerance);
183     aState=aSC.State();
184     bOk=(aState==TopAbs_IN);
185   }
186   //
187   if (iErr) {
188     myErrorStatus=50;
189   }
190   //
191   return bOk;
192 }
193 //=======================================================================
194 //
195 //=======================================================================
196 //function : PntInEdge
197 //purpose  :
198 //=======================================================================
199 Standard_Integer PntInEdge(const TopoDS_Edge& aE,
200                            gp_Pnt& aP)
201
202 {
203   Standard_Integer iErr;
204   Standard_Real aT;
205   //
206   iErr=PntInEdge(aE, aP, aT);
207   //
208   return iErr;
209 }
210 //=======================================================================
211 //function : PntInEdge
212 //purpose  :
213 //=======================================================================
214 Standard_Integer PntInEdge(const TopoDS_Edge& aE,
215                            gp_Pnt& aP,
216                            Standard_Real& aT)
217 {
218   Standard_Integer iErr;
219   Standard_Real aT1, aT2;
220   Handle(Geom_Curve) aC3D;
221   //
222   iErr=0;
223   //
224   aC3D=BRep_Tool::Curve(aE, aT1, aT2);
225   aT=IntTools_Tools::IntermediatePoint(aT1, aT2);
226   aC3D->D0(aT, aP);
227   //
228   return iErr;
229 }
230 //=======================================================================
231 //function : PntInSolid
232 //purpose  :
233 //=======================================================================
234 Standard_Integer PntInSolid(const TopoDS_Solid& aZ,
235                             const Standard_Real aTol,
236                             gp_Pnt& aP)
237 {
238   Standard_Integer iErr;
239   Standard_Real aUx, aVx, aCoef;
240   gp_Pnt aPx;
241   gp_Pnt2d aP2Dx;
242   gp_Vec aDNx;
243
244   TopoDS_Face aF;
245   TopExp_Explorer aExp;
246   //
247   iErr=0;
248   aCoef=10.;
249   //
250   aExp.Init (aZ, TopAbs_FACE);
251   for (; aExp.More() ; aExp.Next()) {
252     aF=*((TopoDS_Face*)&aExp.Current());
253     break;
254   }
255   //
256   iErr=PntInFace(aF, aPx, aP2Dx);
257   if (iErr) {
258     return iErr;
259   }
260   //
261   aP2Dx.Coord(aUx, aVx);
262   BOPTools_Tools2D::FaceNormal(aF, aUx, aVx, aDNx);
263   aDNx.Reverse();
264   //
265   aP.SetXYZ(aPx.XYZ()+aCoef*aTol*aDNx.XYZ());
266   //
267   return iErr;
268 }
269 //=======================================================================
270 //function : PntInFace
271 //purpose  :
272 //=======================================================================
273 Standard_Integer PntInFace(const TopoDS_Face& aF,
274                            gp_Pnt& aP)
275 {
276   Standard_Integer iErr;
277   //
278   gp_Pnt2d aP2Dx;
279   //
280   iErr=PntInFace(aF, aP, aP2Dx);
281   //
282   return iErr;
283 }
284 //=======================================================================
285 //function : PntInFace
286 //purpose  :
287 //=======================================================================
288 Standard_Integer PntInFace(const TopoDS_Face& aF,
289                            gp_Pnt& theP,
290                            gp_Pnt2d& theP2D)
291 {
292   Standard_Boolean bIsDone, bHasFirstPoint, bHasSecondPoint;
293   Standard_Integer iErr, aIx, aNbDomains, i;
294   Standard_Real aUMin, aUMax, aVMin, aVMax;
295   Standard_Real aVx, aUx, aV1, aV2, aU1, aU2, aEpsT;
296   Standard_Real aTotArcIntr, aTolTangfIntr, aTolHatch2D, aTolHatch3D;
297   gp_Dir2d aD2D (0., 1.);
298   gp_Pnt2d aP2D;
299   gp_Pnt aPx;
300   Handle(Geom2d_Curve) aC2D;
301   Handle(Geom2d_TrimmedCurve) aCT2D;
302   Handle(Geom2d_Line) aL2D;
303   Handle(Geom_Surface) aS;
304   TopAbs_Orientation aOrE;
305   TopoDS_Face aFF;
306   TopExp_Explorer aExp;
307   //
308   aTolHatch2D=1.e-8;
309   aTolHatch3D=1.e-8;
310   aTotArcIntr=1.e-10;
311   aTolTangfIntr=1.e-10;
312   //
313   Geom2dHatch_Intersector aIntr(aTotArcIntr, aTolTangfIntr);
314   Geom2dHatch_Hatcher aHatcher(aIntr,
315                                aTolHatch2D, aTolHatch3D,
316                                Standard_True, Standard_False);
317   //
318   iErr=0;
319   aEpsT=1.e-12;
320   //
321   aFF=aF;
322   aFF.Orientation (TopAbs_FORWARD);
323   //
324   aS=BRep_Tool::Surface(aFF);
325   BRepTools::UVBounds(aFF, aUMin, aUMax, aVMin, aVMax);
326   //
327   // 1
328   aExp.Init (aFF, TopAbs_EDGE);
329   for (; aExp.More() ; aExp.Next()) {
330     const TopoDS_Edge& aE=*((TopoDS_Edge*)&aExp.Current());
331     aOrE=aE.Orientation();
332     //
333     aC2D=BRep_Tool::CurveOnSurface (aE, aFF, aU1, aU2);
334     if (aC2D.IsNull() ) {
335       iErr=1;
336       return iErr;
337     }
338     if (fabs(aU1-aU2) < aEpsT) {
339       iErr=2;
340       return iErr;
341     }
342     //
343     aCT2D=new Geom2d_TrimmedCurve(aC2D, aU1, aU2);
344     aHatcher.AddElement(aCT2D, aOrE);
345   }// for (; aExp.More() ; aExp.Next()) {
346   //
347   // 2
348   aUx=IntTools_Tools::IntermediatePoint(aUMin, aUMax);
349   aP2D.SetCoord(aUx, 0.);
350   aL2D=new Geom2d_Line (aP2D, aD2D);
351   Geom2dAdaptor_Curve aHCur(aL2D);
352   //
353   aIx=aHatcher.AddHatching(aHCur) ;
354   //
355   // 3.
356   aHatcher.Trim();
357   bIsDone=aHatcher.TrimDone(aIx);
358   if (!bIsDone) {
359     iErr=3;
360     return iErr;
361   }
362   //
363   aHatcher.ComputeDomains(aIx);
364   bIsDone=aHatcher.IsDone(aIx);
365   if (!bIsDone) {
366     iErr=4;
367     return iErr;
368   }
369   //
370   // 4.
371   aNbDomains=aHatcher.NbDomains(aIx);
372   for (i=1; i<=aNbDomains; ++i) {
373     const HatchGen_Domain& aDomain=aHatcher.Domain (aIx, i) ;
374     bHasFirstPoint=aDomain.HasFirstPoint();
375     if (!bHasFirstPoint) {
376       iErr=5;
377       return iErr;
378     }
379     //
380     aV1=aDomain.FirstPoint().Parameter();
381     //
382     bHasSecondPoint=aDomain.HasSecondPoint();
383     if (!bHasSecondPoint) {
384       iErr=6;
385       return iErr;
386     }
387     //
388     aV2=aDomain.SecondPoint().Parameter();
389     //
390     aVx=IntTools_Tools::IntermediatePoint(aV1, aV2);
391     //
392     break;
393   }
394   //
395   aS->D0(aUx, aVx, aPx);
396   //
397   theP2D.SetCoord(aUx, aVx);
398   theP=aPx;
399   //
400   return iErr;
401 }
402