1 // Created on: 2014-10-14
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <StdPrs_WFShape.hxx>
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAdaptor_Surface.hxx>
21 #include <BRepAdaptor_HSurface.hxx>
22 #include <OSD_Parallel.hxx>
23 #include <StdPrs_DeflectionCurve.hxx>
24 #include <StdPrs_Isolines.hxx>
25 #include <StdPrs_ShapeTool.hxx>
26 #include <StdPrs_ToolTriangulatedShape.hxx>
27 #include <Standard_ErrorHandler.hxx>
28 #include <Prs3d_IsoAspect.hxx>
30 #include <Poly_PolygonOnTriangulation.hxx>
31 #include <Poly_Polygon3D.hxx>
32 #include <Poly_Triangulation.hxx>
33 #include <Graphic3d_AspectLine3d.hxx>
34 #include <Graphic3d_ArrayOfSegments.hxx>
35 #include <Graphic3d_ArrayOfPoints.hxx>
37 #include <TColgp_HSequenceOfPnt.hxx>
38 #include <TColStd_Array1OfInteger.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <Standard_Mutex.hxx>
46 //! Functor for executing StdPrs_Isolines in parallel threads.
47 class StdPrs_WFShape_IsoFunctor
50 StdPrs_WFShape_IsoFunctor (Prs3d_NListOfSequenceOfPnt& thePolylinesU,
51 Prs3d_NListOfSequenceOfPnt& thePolylinesV,
52 const std::vector<TopoDS_Face>& theFaces,
53 const Handle(Prs3d_Drawer)& theDrawer,
54 Standard_Real theShapeDeflection)
55 : myPolylinesU (thePolylinesU),
56 myPolylinesV (thePolylinesV),
59 myShapeDeflection (theShapeDeflection)
64 void operator()(const Standard_Integer& theIndex) const
66 Prs3d_NListOfSequenceOfPnt aPolylinesU, aPolylinesV;
67 const TopoDS_Face& aFace = myFaces[theIndex];
68 StdPrs_Isolines::Add (aFace, myDrawer, myShapeDeflection, aPolylinesU, aPolylinesV);
70 Standard_Mutex::Sentry aLock (myMutex);
71 myPolylinesU.Append (aPolylinesU);
72 myPolylinesV.Append (aPolylinesV);
77 StdPrs_WFShape_IsoFunctor operator= (StdPrs_WFShape_IsoFunctor& );
79 Prs3d_NListOfSequenceOfPnt& myPolylinesU;
80 Prs3d_NListOfSequenceOfPnt& myPolylinesV;
81 const std::vector<TopoDS_Face>& myFaces;
82 const Handle(Prs3d_Drawer)& myDrawer;
83 mutable Standard_Mutex myMutex;
84 const Standard_Real myShapeDeflection;
88 // =========================================================================
91 // =========================================================================
92 void StdPrs_WFShape::Add (const Handle(Prs3d_Presentation)& thePresentation,
93 const TopoDS_Shape& theShape,
94 const Handle(Prs3d_Drawer)& theDrawer,
95 Standard_Boolean theIsParallel)
97 if (theShape.IsNull())
102 if (theDrawer->IsAutoTriangulation())
104 StdPrs_ToolTriangulatedShape::Tessellate (theShape, theDrawer);
107 // draw triangulation-only edges
108 if (Handle(Graphic3d_ArrayOfPrimitives) aTriFreeEdges = AddEdgesOnTriangulation (theShape, Standard_True))
110 Handle(Graphic3d_Group) aGroup = thePresentation->NewGroup();
111 aGroup->SetPrimitivesAspect (theDrawer->FreeBoundaryAspect()->Aspect());
112 aGroup->AddPrimitiveArray (aTriFreeEdges);
115 Prs3d_NListOfSequenceOfPnt aCommonPolylines;
116 const Handle(Prs3d_LineAspect)& aWireAspect = theDrawer->WireAspect();
117 const Standard_Real aShapeDeflection = StdPrs_ToolTriangulatedShape::GetDeflection (theShape, theDrawer);
121 Prs3d_NListOfSequenceOfPnt aUPolylines, aVPolylines;
122 Prs3d_NListOfSequenceOfPnt* aUPolylinesPtr = &aUPolylines;
123 Prs3d_NListOfSequenceOfPnt* aVPolylinesPtr = &aVPolylines;
125 const Handle(Prs3d_LineAspect)& anIsoAspectU = theDrawer->UIsoAspect();
126 const Handle(Prs3d_LineAspect)& anIsoAspectV = theDrawer->VIsoAspect();
127 if (anIsoAspectV->Aspect()->IsEqual (*anIsoAspectU->Aspect()))
129 aVPolylinesPtr = aUPolylinesPtr; // put both U and V isolines into single group
131 if (anIsoAspectU->Aspect()->IsEqual (*aWireAspect->Aspect()))
133 aUPolylinesPtr = &aCommonPolylines; // put U isolines into single group with common edges
135 if (anIsoAspectV->Aspect()->IsEqual (*aWireAspect->Aspect()))
137 aVPolylinesPtr = &aCommonPolylines; // put V isolines into single group with common edges
140 bool isParallelIso = false;
143 Standard_Integer aNbFaces = 0;
144 for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
150 isParallelIso = true;
151 std::vector<TopoDS_Face> aFaces (aNbFaces);
153 for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
155 const TopoDS_Face& aFace = TopoDS::Face (aFaceExplorer.Current());
156 if (theDrawer->IsoOnPlane() || !StdPrs_ShapeTool::IsPlanarFace (aFace))
158 aFaces[aNbFaces++] = aFace;
162 StdPrs_WFShape_IsoFunctor anIsoFunctor (*aUPolylinesPtr, *aVPolylinesPtr, aFaces, theDrawer, aShapeDeflection);
163 OSD_Parallel::For (0, aNbFaces, anIsoFunctor, aNbFaces < 2);
169 for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
171 const TopoDS_Face& aFace = TopoDS::Face (aFaceExplorer.Current());
172 if (theDrawer->IsoOnPlane() || !StdPrs_ShapeTool::IsPlanarFace (aFace))
174 StdPrs_Isolines::Add (aFace, theDrawer, aShapeDeflection, *aUPolylinesPtr, *aVPolylinesPtr);
179 Prs3d::AddPrimitivesGroup (thePresentation, anIsoAspectU, aUPolylines);
180 Prs3d::AddPrimitivesGroup (thePresentation, anIsoAspectV, aVPolylines);
184 Prs3d_NListOfSequenceOfPnt anUnfree, aFree;
185 Prs3d_NListOfSequenceOfPnt* anUnfreePtr = &anUnfree;
186 Prs3d_NListOfSequenceOfPnt* aFreePtr = &aFree;
187 if (!theDrawer->UnFreeBoundaryDraw())
191 else if (theDrawer->UnFreeBoundaryAspect()->Aspect()->IsEqual (*aWireAspect->Aspect()))
193 anUnfreePtr = &aCommonPolylines; // put unfree edges into single group with common edges
196 if (!theDrawer->FreeBoundaryDraw())
200 else if (theDrawer->FreeBoundaryAspect()->Aspect()->IsEqual (*aWireAspect->Aspect()))
202 aFreePtr = &aCommonPolylines; // put free edges into single group with common edges
208 theDrawer->WireDraw() ? &aCommonPolylines : NULL,
211 Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->UnFreeBoundaryAspect(), anUnfree);
212 Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->FreeBoundaryAspect(), aFree);
215 Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->WireAspect(), aCommonPolylines);
217 if (Handle(Graphic3d_ArrayOfPoints) aVertexArray = AddVertexes (theShape, theDrawer->VertexDrawMode()))
219 Handle(Graphic3d_Group) aGroup = thePresentation->NewGroup();
220 aGroup->SetPrimitivesAspect (theDrawer->PointAspect()->Aspect());
221 aGroup->AddPrimitiveArray (aVertexArray);
225 // =========================================================================
226 // function : AddAllEdges
228 // =========================================================================
229 Handle(Graphic3d_ArrayOfPrimitives) StdPrs_WFShape::AddAllEdges (const TopoDS_Shape& theShape,
230 const Handle(Prs3d_Drawer)& theDrawer)
232 const Standard_Real aShapeDeflection = StdPrs_ToolTriangulatedShape::GetDeflection (theShape, theDrawer);
233 Prs3d_NListOfSequenceOfPnt aPolylines;
234 addEdges (theShape, theDrawer, aShapeDeflection,
235 &aPolylines, &aPolylines, &aPolylines);
236 return Prs3d::PrimitivesFromPolylines (aPolylines);
239 // =========================================================================
240 // function : addEdges
242 // =========================================================================
243 void StdPrs_WFShape::addEdges (const TopoDS_Shape& theShape,
244 const Handle(Prs3d_Drawer)& theDrawer,
245 Standard_Real theShapeDeflection,
246 Prs3d_NListOfSequenceOfPnt* theWire,
247 Prs3d_NListOfSequenceOfPnt* theFree,
248 Prs3d_NListOfSequenceOfPnt* theUnFree)
250 if (theShape.IsNull())
255 TopTools_ListOfShape aLWire, aLFree, aLUnFree;
256 TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
257 TopExp::MapShapesAndAncestors (theShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap);
258 for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgeMap); anEdgeIter.More(); anEdgeIter.Next())
260 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
261 const Standard_Integer aNbNeighbours = anEdgeIter.Value().Extent();
262 switch (aNbNeighbours)
268 aLWire.Append (anEdge);
276 aLFree.Append (anEdge);
284 aLUnFree.Append (anEdge);
291 if (!aLWire.IsEmpty())
293 addEdges (aLWire, theDrawer, theShapeDeflection, *theWire);
295 if (!aLFree.IsEmpty())
297 addEdges (aLFree, theDrawer, theShapeDeflection, *theFree);
299 if (!aLUnFree.IsEmpty())
301 addEdges (aLUnFree, theDrawer, theShapeDeflection, *theUnFree);
305 // =========================================================================
306 // function : AddEdges
308 // =========================================================================
309 void StdPrs_WFShape::addEdges (const TopTools_ListOfShape& theEdges,
310 const Handle (Prs3d_Drawer)& theDrawer,
311 const Standard_Real theShapeDeflection,
312 Prs3d_NListOfSequenceOfPnt& thePolylines)
314 TopTools_ListIteratorOfListOfShape anEdgesIter;
315 for (anEdgesIter.Initialize (theEdges); anEdgesIter.More(); anEdgesIter.Next())
317 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgesIter.Value());
318 if (BRep_Tool::Degenerated (anEdge))
323 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt;
325 TopLoc_Location aLocation;
326 Handle(Poly_Triangulation) aTriangulation;
327 Handle(Poly_PolygonOnTriangulation) anEdgeIndicies;
328 BRep_Tool::PolygonOnTriangulation (anEdge, anEdgeIndicies, aTriangulation, aLocation);
329 Handle(Poly_Polygon3D) aPolygon;
331 if (!anEdgeIndicies.IsNull())
333 // Presentation based on triangulation of a face.
334 const TColStd_Array1OfInteger& anIndices = anEdgeIndicies->Nodes();
335 const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes();
337 Standard_Integer anIndex = anIndices.Lower();
338 if (aLocation.IsIdentity())
340 for (; anIndex <= anIndices.Upper(); ++anIndex)
342 aPoints->Append (aNodes (anIndices (anIndex)));
347 for (; anIndex <= anIndices.Upper(); ++anIndex)
349 aPoints->Append (aNodes (anIndices (anIndex)).Transformed (aLocation));
353 else if (!(aPolygon = BRep_Tool::Polygon3D (anEdge, aLocation)).IsNull())
355 // Presentation based on triangulation of the free edge on a surface.
356 const TColgp_Array1OfPnt& aNodes = aPolygon->Nodes();
357 Standard_Integer anIndex = aNodes.Lower();
358 if (aLocation.IsIdentity())
360 for (; anIndex <= aNodes.Upper(); ++anIndex)
362 aPoints->Append (aNodes.Value (anIndex));
367 for (; anIndex <= aNodes.Upper(); ++anIndex)
369 aPoints->Append (aNodes.Value (anIndex).Transformed (aLocation));
373 else if (BRep_Tool::IsGeometric (anEdge))
375 // Default presentation for edges without triangulation.
376 BRepAdaptor_Curve aCurve (anEdge);
377 StdPrs_DeflectionCurve::Add (Handle(Prs3d_Presentation)(),
381 aPoints->ChangeSequence(),
385 if (!aPoints->IsEmpty())
387 thePolylines.Append (aPoints);
392 // =========================================================================
393 // function : AddEdgesOnTriangulation
395 // =========================================================================
396 Handle(Graphic3d_ArrayOfPrimitives) StdPrs_WFShape::AddEdgesOnTriangulation (const TopoDS_Shape& theShape,
397 const Standard_Boolean theToExcludeGeometric)
399 TColgp_SequenceOfPnt aSeqPnts;
400 AddEdgesOnTriangulation (aSeqPnts, theShape, theToExcludeGeometric);
401 if (aSeqPnts.Size() < 2)
403 return Handle(Graphic3d_ArrayOfSegments)();
406 Standard_Integer aNbVertices = aSeqPnts.Size();
407 Handle(Graphic3d_ArrayOfSegments) aSurfArray = new Graphic3d_ArrayOfSegments (aNbVertices);
408 for (Standard_Integer anI = 1; anI <= aNbVertices; anI += 2)
410 aSurfArray->AddVertex (aSeqPnts.Value (anI));
411 aSurfArray->AddVertex (aSeqPnts.Value (anI + 1));
416 // =========================================================================
417 // function : AddEdgesOnTriangulation
419 // =========================================================================
420 void StdPrs_WFShape::AddEdgesOnTriangulation (TColgp_SequenceOfPnt& theSegments,
421 const TopoDS_Shape& theShape,
422 const Standard_Boolean theToExcludeGeometric)
424 TopLoc_Location aLocation, aDummyLoc;
425 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
427 const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
428 if (theToExcludeGeometric)
430 const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface (aFace, aDummyLoc);
436 if (const Handle(Poly_Triangulation)& aPolyTri = BRep_Tool::Triangulation (aFace, aLocation))
438 Prs3d::AddFreeEdges (theSegments, aPolyTri, aLocation);
443 // =========================================================================
444 // function : AddVertexes
446 // =========================================================================
447 Handle(Graphic3d_ArrayOfPoints) StdPrs_WFShape::AddVertexes (const TopoDS_Shape& theShape,
448 Prs3d_VertexDrawMode theVertexMode)
450 TColgp_SequenceOfPnt aShapeVertices;
451 if (theVertexMode == Prs3d_VDM_All)
453 for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX); aVertIter.More(); aVertIter.Next())
455 const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
456 aShapeVertices.Append (BRep_Tool::Pnt (aVert));
462 for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX, TopAbs_EDGE); aVertIter.More(); aVertIter.Next())
464 const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
465 aShapeVertices.Append (BRep_Tool::Pnt (aVert));
469 for (TopExp_Explorer anEdgeIter (theShape, TopAbs_EDGE); anEdgeIter.More(); anEdgeIter.Next())
471 for (TopoDS_Iterator aVertIter (anEdgeIter.Current(), Standard_False, Standard_True); aVertIter.More(); aVertIter.Next())
473 const TopoDS_Shape& aVertSh = aVertIter.Value();
474 if (aVertSh.Orientation() == TopAbs_INTERNAL
475 && aVertSh.ShapeType() == TopAbs_VERTEX)
477 const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertSh);
478 aShapeVertices.Append (BRep_Tool::Pnt (aVert));
484 if (aShapeVertices.IsEmpty())
486 return Handle(Graphic3d_ArrayOfPoints)();
489 const Standard_Integer aNbVertices = aShapeVertices.Length();
490 Handle(Graphic3d_ArrayOfPoints) aVertexArray = new Graphic3d_ArrayOfPoints (aNbVertices);
491 for (Standard_Integer aVertIter = 1; aVertIter <= aNbVertices; ++aVertIter)
493 aVertexArray->AddVertex (aShapeVertices.Value (aVertIter));