1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: GEOMAlgo_GetInPlace_1.cxx
23 // Author: Peter KURNEV
25 #include <GEOMAlgo_GetInPlace.hxx>
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Dir2d.hxx>
34 #include <Geom_Surface.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom2d_Line.hxx>
37 #include <Geom2d_TrimmedCurve.hxx>
38 #include <Geom2dAdaptor_Curve.hxx>
39 #include <Geom2dHatch_Hatcher.hxx>
40 #include <Geom2dHatch_Intersector.hxx>
41 #include <HatchGen_Domain.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <TopAbs_State.hxx>
45 #include <TopAbs_ShapeEnum.hxx>
46 #include <TopAbs_Orientation.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Solid.hxx>
51 #include <TopoDS_Face.hxx>
53 #include <BRep_Tool.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <BRepTools.hxx>
57 #include <BRepClass3d_SolidClassifier.hxx>
59 #include <IntTools_Tools.hxx>
61 #include <BOPTools_Tools3D.hxx>
62 #include <BOPTools_Tools2D.hxx>
67 Standard_Integer PntInEdge(const TopoDS_Edge& aF,
70 Standard_Integer PntInEdge(const TopoDS_Edge& aF,
74 Standard_Integer PntInFace(const TopoDS_Face& aF,
77 Standard_Integer PntInFace(const TopoDS_Face& aF,
81 Standard_Integer PntInSolid(const TopoDS_Solid& aZ,
82 const Standard_Real aTol,
86 //=======================================================================
87 //function : CheckCoincidence
89 //=======================================================================
90 Standard_Boolean GEOMAlgo_GetInPlace::CheckCoincidence(const TopoDS_Shape& aS1,
91 const TopoDS_Shape& aS2)
94 Standard_Integer iErr;
96 TopAbs_ShapeEnum aType1, aType2;
104 aTol2=myTolerance*myTolerance;
105 aType1=aS1.ShapeType();
106 aType2=aS2.ShapeType();
108 // 1. A point on shape #2 -> aP2
109 if (myMapShapePnt.IsBound(aS2)) {
110 aP2=myMapShapePnt.Find(aS2);
113 if (aType2==TopAbs_VERTEX) {
114 const TopoDS_Vertex& aV2=*((TopoDS_Vertex*)&aS2);
115 aP2=BRep_Tool::Pnt(aV2);
118 else if (aType2==TopAbs_EDGE) {
119 const TopoDS_Edge& aE2=*((TopoDS_Edge*)&aS2);
120 iErr=PntInEdge(aE2, aP2);
123 else if (aType2==TopAbs_FACE) {
124 const TopoDS_Face& aF2=*((TopoDS_Face*)&aS2);
125 iErr=PntInFace(aF2, aP2);
128 else if (aType2==TopAbs_SOLID) {
129 const TopoDS_Solid& aZ2=*((TopoDS_Solid*)&aS2);
130 iErr=PntInSolid(aZ2, myTolerance, aP2);
142 myMapShapePnt.Bind(aS2, aP2);
145 // 2. Project the point aP2 on shape #1 and check
146 if (aType1==TopAbs_EDGE) {
147 Standard_Integer aNbPoints;
148 Standard_Real aDmin, aT, aT1, aT2, dT;
150 const TopoDS_Edge& aE1=*((TopoDS_Edge*)&aS1);
152 GeomAPI_ProjectPointOnCurve& aPPC=myContext.ProjPC(aE1);
154 aNbPoints=aPPC.NbPoints();
156 aDmin=aPPC.LowerDistance();
157 aT=aPPC.LowerDistanceParameter();
158 if (aDmin < myTolerance) {
160 BRep_Tool::Curve(aE1, aT1, aT2);
161 if(aT > (aT1-dT) && aT < (aT2+dT)) {
169 }//if (aType1==TopAbs_EDGE) {
171 else if (aType1==TopAbs_FACE) {
172 const TopoDS_Face& aF1=*((TopoDS_Face*)&aS1);
174 bOk=myContext.IsValidPointForFace(aP2, aF1, myTolerance);
177 else if (aType1==TopAbs_SOLID) {
178 const TopoDS_Solid& aZ1=*((TopoDS_Solid*)&aS1);
180 BRepClass3d_SolidClassifier& aSC=myContext.SolidClassifier(aZ1);
181 aSC.Perform(aP2, myTolerance);
183 bOk=(aState==TopAbs_IN);
192 //=======================================================================
194 //=======================================================================
195 //function : PntInEdge
197 //=======================================================================
198 Standard_Integer PntInEdge(const TopoDS_Edge& aE,
202 Standard_Integer iErr;
205 iErr=PntInEdge(aE, aP, aT);
209 //=======================================================================
210 //function : PntInEdge
212 //=======================================================================
213 Standard_Integer PntInEdge(const TopoDS_Edge& aE,
217 Standard_Integer iErr;
218 Standard_Real aT1, aT2;
219 Handle(Geom_Curve) aC3D;
223 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
224 aT=IntTools_Tools::IntermediatePoint(aT1, aT2);
229 //=======================================================================
230 //function : PntInSolid
232 //=======================================================================
233 Standard_Integer PntInSolid(const TopoDS_Solid& aZ,
234 const Standard_Real aTol,
237 Standard_Integer iErr;
238 Standard_Real aUx, aVx, aCoef;
244 TopExp_Explorer aExp;
249 aExp.Init (aZ, TopAbs_FACE);
250 for (; aExp.More() ; aExp.Next()) {
251 aF=*((TopoDS_Face*)&aExp.Current());
255 iErr=PntInFace(aF, aPx, aP2Dx);
260 aP2Dx.Coord(aUx, aVx);
261 BOPTools_Tools2D::FaceNormal(aF, aUx, aVx, aDNx);
264 aP.SetXYZ(aPx.XYZ()+aCoef*aTol*aDNx.XYZ());
268 //=======================================================================
269 //function : PntInFace
271 //=======================================================================
272 Standard_Integer PntInFace(const TopoDS_Face& aF,
275 Standard_Integer iErr;
279 iErr=PntInFace(aF, aP, aP2Dx);
283 //=======================================================================
284 //function : PntInFace
286 //=======================================================================
287 Standard_Integer PntInFace(const TopoDS_Face& aF,
291 Standard_Boolean bIsDone, bHasFirstPoint, bHasSecondPoint;
292 Standard_Integer iErr, aIx, aNbDomains, i;
293 Standard_Real aUMin, aUMax, aVMin, aVMax;
294 Standard_Real aVx, aUx, aV1, aV2, aU1, aU2, aEpsT;
295 Standard_Real aTotArcIntr, aTolTangfIntr, aTolHatch2D, aTolHatch3D;
296 gp_Dir2d aD2D (0., 1.);
299 Handle(Geom2d_Curve) aC2D;
300 Handle(Geom2d_TrimmedCurve) aCT2D;
301 Handle(Geom2d_Line) aL2D;
302 Handle(Geom_Surface) aS;
303 TopAbs_Orientation aOrE;
305 TopExp_Explorer aExp;
310 aTolTangfIntr=1.e-10;
312 Geom2dHatch_Intersector aIntr(aTotArcIntr, aTolTangfIntr);
313 Geom2dHatch_Hatcher aHatcher(aIntr,
314 aTolHatch2D, aTolHatch3D,
315 Standard_True, Standard_False);
321 aFF.Orientation (TopAbs_FORWARD);
323 aS=BRep_Tool::Surface(aFF);
324 BRepTools::UVBounds(aFF, aUMin, aUMax, aVMin, aVMax);
327 aExp.Init (aFF, TopAbs_EDGE);
328 for (; aExp.More() ; aExp.Next()) {
329 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aExp.Current());
330 aOrE=aE.Orientation();
332 aC2D=BRep_Tool::CurveOnSurface (aE, aFF, aU1, aU2);
333 if (aC2D.IsNull() ) {
337 if (fabs(aU1-aU2) < aEpsT) {
342 aCT2D=new Geom2d_TrimmedCurve(aC2D, aU1, aU2);
343 aHatcher.AddElement(aCT2D, aOrE);
344 }// for (; aExp.More() ; aExp.Next()) {
347 aUx=IntTools_Tools::IntermediatePoint(aUMin, aUMax);
348 aP2D.SetCoord(aUx, 0.);
349 aL2D=new Geom2d_Line (aP2D, aD2D);
350 Geom2dAdaptor_Curve aHCur(aL2D);
352 aIx=aHatcher.AddHatching(aHCur) ;
356 bIsDone=aHatcher.TrimDone(aIx);
362 aHatcher.ComputeDomains(aIx);
363 bIsDone=aHatcher.IsDone(aIx);
370 aNbDomains=aHatcher.NbDomains(aIx);
371 for (i=1; i<=aNbDomains; ++i) {
372 const HatchGen_Domain& aDomain=aHatcher.Domain (aIx, i) ;
373 bHasFirstPoint=aDomain.HasFirstPoint();
374 if (!bHasFirstPoint) {
379 aV1=aDomain.FirstPoint().Parameter();
381 bHasSecondPoint=aDomain.HasSecondPoint();
382 if (!bHasSecondPoint) {
387 aV2=aDomain.SecondPoint().Parameter();
389 aVx=IntTools_Tools::IntermediatePoint(aV1, aV2);
394 aS->D0(aUx, aVx, aPx);
396 theP2D.SetCoord(aUx, aVx);