1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
23 #include <HYDROData_Projection.h>
24 #include <BRepAdaptor_Curve.hxx>
25 #include <BRep_Builder.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
27 #include <BRepLib.hxx>
28 #include <BRepLib_MakeEdge.hxx>
29 #include <BRepLib_MakeFace.hxx>
30 #include <BRepExtrema_DistShapeShape.hxx>
31 #include <Bnd_Box.hxx>
32 #include <BRepBndLib.hxx>
35 #include <HLRAlgo_Projector.hxx>
36 #include <HLRBRep_Algo.hxx>
37 #include <HLRBRep_HLRToShape.hxx>
38 #include <IntCurvesFace_ShapeIntersector.hxx>
39 #include <Precision.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Iterator.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50 #include <TopTools_DataMapOfShapeReal.hxx>
52 HYDROData_Make3dMesh::HYDROData_Make3dMesh( const TopoDS_Shape& aShape,
53 const Standard_Real Tolerance )
55 myIntersector.Load(aShape, Tolerance);
58 Standard_Boolean HYDROData_Make3dMesh::GetHighestOriginal(const Standard_Real aX,
59 const Standard_Real aY,
62 gp_Pnt ProjPnt(aX, aY, 0.);
63 gp_Lin Ray(ProjPnt, -gp::DZ());
64 myIntersector.PerformNearest(Ray, -Precision::Infinite(), 0.);
65 if (!myIntersector.IsDone() || myIntersector.NbPnt() == 0)
66 return Standard_False;
68 HighestPoint = myIntersector.Pnt(1);
72 //=======================================================================
73 //function : FindClosestDirection
74 //purpose : auxiliary function
75 //=======================================================================
76 TopoDS_Edge HYDROData_Projection::FindClosestDirection( const TopoDS_Vertex& StartVertex,
77 const TopTools_ListOfShape& Candidates,
78 const gp_Vec& StartDir )
80 Standard_Real MinAngle = M_PI;
83 TopTools_ListIteratorOfListOfShape itl(Candidates);
84 for (; itl.More(); itl.Next())
86 TopoDS_Edge anEdge = TopoDS::Edge(itl.Value());
87 BRepAdaptor_Curve BAcurve(anEdge);
91 Standard_Real Par, Par2 = 0.; //to avoid warning
92 Standard_Boolean ToReverse = Standard_False;
95 TopExp::Vertices(anEdge, V1, V2);
96 if (StartVertex.IsSame(V1))
97 Par = 0.99*BAcurve.FirstParameter() + 0.01*BAcurve.LastParameter();
100 Par = 0.01*BAcurve.FirstParameter() + 0.99*BAcurve.LastParameter();
101 ToReverse = Standard_True;
105 Par2 = 0.01*BAcurve.FirstParameter() + 0.99*BAcurve.LastParameter();
108 BAcurve.D1(Par, aPnt, aDir);
114 BAcurve.D1(Par2, aPnt2, aDir2);
118 Standard_Real anAngle = StartDir.Angle(aDir);
119 if (anAngle < MinAngle)
127 anAngle = StartDir.Angle(aDir2);
128 if (anAngle < MinAngle)
139 //=======================================================================
140 //function : BuildOutWire
141 //purpose : auxiliary function
142 //=======================================================================
143 TopoDS_Wire HYDROData_Projection::BuildOutWire( const TopTools_IndexedDataMapOfShapeListOfShape& VEmap,
144 const TopoDS_Vertex& StartVertex,
145 TopoDS_Edge& StartEdge)
147 Standard_Real AngularTol = 0.1; //0.01; //1.e-4; //Precision::Angular();
148 TopTools_MapOfShape BoundEdges;
152 BB.MakeWire(theWire);
154 TopoDS_Vertex V1, V2;
155 TopExp::Vertices(StartEdge, V1, V2);
156 if (StartVertex.IsSame(V1))
157 StartEdge.Orientation(TopAbs_FORWARD);
159 StartEdge.Orientation(TopAbs_REVERSED);
160 BB.Add(theWire, StartEdge);
161 BoundEdges.Add(StartEdge);
163 TopoDS_Vertex CurVertex = (StartVertex.IsSame(V1))? V2 : V1;
164 TopoDS_Edge CurEdge = StartEdge;
165 gp_Vec Normal(0., 0., 1.);
168 if (CurVertex.IsSame(StartVertex))
170 theWire.Closed(Standard_True);
174 const TopTools_ListOfShape& Candidates = VEmap.FindFromKey(CurVertex);
175 Standard_Real MinAngle = M_PI;
177 BRepAdaptor_Curve CurCurve(CurEdge);
178 TopExp::Vertices(CurEdge, V1, V2);
179 Standard_Real CurPar;
180 if (CurVertex.IsSame(V2))
181 CurPar = CurCurve.LastParameter();
183 CurPar = CurCurve.FirstParameter();
186 CurCurve.D1(CurPar, aPnt, CurDir);
187 if (CurDir.SquareMagnitude() < Precision::PConfusion())
189 CurPar = (CurVertex.IsSame(V2))?
190 0.01*CurCurve.FirstParameter() + 0.99*CurCurve.LastParameter() :
191 0.99*CurCurve.FirstParameter() + 0.01*CurCurve.LastParameter();
192 CurCurve.D1(CurPar, aPnt, CurDir);
194 if (CurVertex.IsSame(V1))
198 TopTools_ListIteratorOfListOfShape itl(Candidates);
199 for (; itl.More(); itl.Next())
201 TopoDS_Edge anEdge = TopoDS::Edge(itl.Value());
202 if (BoundEdges.Contains(anEdge)) //if (anEdge.IsSame(CurEdge))
205 TopExp::Vertices(anEdge, V1, V2);
209 BRepAdaptor_Curve BAcurve(anEdge);
211 Standard_Real aPar = (CurVertex.IsSame(V1))?
212 0.99*BAcurve.FirstParameter() + 0.01*BAcurve.LastParameter() :
213 0.01*BAcurve.FirstParameter() + 0.99*BAcurve.LastParameter();
214 //BAcurve.FirstParameter() : BAcurve.LastParameter();
216 BAcurve.D1(aPar, aPnt, aDir);
217 if (CurVertex.IsSame(V2))
220 Standard_Real anAngle = CurDir.AngleWithRef(aDir, Normal);
221 if (Abs(anAngle + M_PI) < AngularTol) //anAngle = -PI
223 if (anAngle < MinAngle)
229 if (theEdge.IsNull())
232 TopExp::Vertices(theEdge, V1, V2);
233 if (CurVertex.IsSame(V1))
234 theEdge.Orientation(TopAbs_FORWARD);
236 theEdge.Orientation(TopAbs_REVERSED);
237 BB.Add(theWire, theEdge);
238 BoundEdges.Add(theEdge);
240 CurVertex = (CurVertex.IsSame(V1))? V2 : V1;
247 //=======================================================================
248 //Function : MakeProjection
249 //purpose : this function makes the "shadow" of a shape on the plane XOY
250 //=======================================================================
251 TopoDS_Face HYDROData_Projection::MakeProjection( const TopoDS_Shape& aShape )
253 if ( aShape.IsNull() )
254 return TopoDS_Face();
256 HLRAlgo_Projector theProjector(gp::XOY());
258 Handle(HLRBRep_Algo) aHLRAlgo = new HLRBRep_Algo();
259 aHLRAlgo->Add(aShape);
260 aHLRAlgo->Projector(theProjector);
263 HLRBRep_HLRToShape aHLRToShape(aHLRAlgo);
265 TopoDS_Shape SharpEdges, OutLines;
266 TopoDS_Compound Total;
268 BB.MakeCompound(Total);
270 SharpEdges = aHLRToShape.VCompound();
271 OutLines = aHLRToShape.OutLineVCompound();
274 if (!SharpEdges.IsNull())
276 for (itc.Initialize(SharpEdges); itc.More(); itc.Next())
277 BB.Add(Total, itc.Value());
279 if (!OutLines.IsNull())
281 for (itc.Initialize(OutLines); itc.More(); itc.Next())
282 BB.Add(Total, itc.Value());
285 BRepBuilderAPI_Sewing aSewing;
286 Standard_Real tol = 1.0e-06;
287 Standard_Boolean NonManifoldMode = Standard_False;
288 aSewing.Init(tol, Standard_True,Standard_True,Standard_True,NonManifoldMode);
289 aSewing.SetFloatingEdgesMode(Standard_True);
291 aSewing.Add( Total );
294 TopoDS_Shape SewedEdges = aSewing.SewedShape();
296 BRepLib::BuildCurves3d( SewedEdges );
299 BRepBndLib::Add(SewedEdges, theBox);
300 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
301 theBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
303 gp_Pnt P1(Xmin, Ymin, 0.), P2(Xmax, Ymin, 0.);
304 TopoDS_Edge LowerBound = BRepLib_MakeEdge(P1, P2);
306 Standard_Real Deviation = 1.e-7; //1.e-5;
307 BRepExtrema_DistShapeShape DSS(SewedEdges, LowerBound, Deviation);
309 TopTools_IndexedDataMapOfShapeListOfShape StartShapes;
310 TopTools_DataMapOfShapeReal EdgesWithPars;
312 for (i = 1; i <= DSS.NbSolution(); i++)
314 if (!StartShapes.Contains( DSS.SupportOnShape1(i) ))
316 TopTools_ListOfShape aList;
317 aList.Append( DSS.SupportOnShape2(i) );
318 StartShapes.Add( DSS.SupportOnShape1(i), aList );
322 TopTools_ListOfShape& aList = StartShapes.ChangeFromKey( DSS.SupportOnShape1(i) );
323 aList.Append( DSS.SupportOnShape2(i) );
325 if (DSS.SupportTypeShape1(i) == BRepExtrema_IsOnEdge)
328 DSS.ParOnEdgeS1(i, aPar);
329 EdgesWithPars.Bind( DSS.SupportOnShape1(i), aPar );
333 TopoDS_Shape aStartShape;
334 for (i = 1; i <= StartShapes.Extent(); i++)
336 const TopoDS_Shape& aShape = StartShapes.FindKey(i);
337 if (aShape.ShapeType() == TopAbs_EDGE)
339 aStartShape = aShape;
343 if (aStartShape.IsNull())
344 aStartShape = StartShapes.FindKey(1); //it is a vertex
346 TopoDS_Vertex StartVertex;
347 TopoDS_Edge StartEdge;
348 gp_Vec StartDir(1.,0.,0.);
349 TopTools_IndexedDataMapOfShapeListOfShape VEmap;
350 TopExp::MapShapesAndAncestors(SewedEdges, TopAbs_VERTEX, TopAbs_EDGE, VEmap);
351 if (aStartShape.ShapeType() == TopAbs_EDGE)
353 StartEdge = TopoDS::Edge(aStartShape);
354 Standard_Real StartPar = EdgesWithPars(StartEdge);
355 BRepAdaptor_Curve BAcurve(StartEdge);
358 BAcurve.D1(StartPar, aPnt, aDir);
359 StartVertex = (StartDir * aDir > 0.)?
360 TopExp::FirstVertex(StartEdge) : TopExp::LastVertex(StartEdge);
362 else // it is a vertex
364 StartVertex = TopoDS::Vertex(aStartShape);
365 const TopTools_ListOfShape& Candidates = VEmap.FindFromKey(StartVertex);
366 StartEdge = FindClosestDirection(StartVertex, Candidates, StartDir);
369 TopoDS_Wire OutWire = BuildOutWire(VEmap, StartVertex, StartEdge);
370 TopoDS_Face ProjFace = BRepLib_MakeFace(OutWire, Standard_True); //only plane