0032133: Modeling Data - Restriction of access to internal arrays for Poly_Triangulat...
[occt.git] / src / StdPrs / StdPrs_WFShape.cxx
1 // Created on: 2014-10-14
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <StdPrs_WFShape.hxx>
17
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAdaptor_Surface.hxx>
21 #include <BRepAdaptor_Surface.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>
29 #include <Prs3d.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>
36 #include <gp_Pnt.hxx>
37 #include <TColgp_HSequenceOfPnt.hxx>
38 #include <TColStd_Array1OfInteger.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <Standard_Mutex.hxx>
45
46 //! Functor for executing StdPrs_Isolines in parallel threads.
47 class StdPrs_WFShape_IsoFunctor
48 {
49 public:
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),
57     myFaces (theFaces),
58     myDrawer (theDrawer),
59     myShapeDeflection (theShapeDeflection)
60   {
61     //
62   }
63
64   void operator()(const Standard_Integer& theIndex) const
65   {
66     Prs3d_NListOfSequenceOfPnt aPolylinesU, aPolylinesV;
67     const TopoDS_Face& aFace = myFaces[theIndex];
68     StdPrs_Isolines::Add (aFace, myDrawer, myShapeDeflection, aPolylinesU, aPolylinesV);
69     {
70       Standard_Mutex::Sentry aLock (myMutex);
71       myPolylinesU.Append (aPolylinesU);
72       myPolylinesV.Append (aPolylinesV);
73     }
74   }
75
76 private:
77   StdPrs_WFShape_IsoFunctor operator= (StdPrs_WFShape_IsoFunctor& );
78 private:
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;
85 };
86
87
88 // =========================================================================
89 // function : Add
90 // purpose  :
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)
96 {
97   if (theShape.IsNull())
98   {
99     return;
100   }
101
102   if (theDrawer->IsAutoTriangulation())
103   {
104     StdPrs_ToolTriangulatedShape::Tessellate (theShape, theDrawer);
105   }
106
107   // draw triangulation-only edges
108   if (Handle(Graphic3d_ArrayOfPrimitives) aTriFreeEdges = AddEdgesOnTriangulation (theShape, Standard_True))
109   {
110     Handle(Graphic3d_Group) aGroup = thePresentation->NewGroup();
111     aGroup->SetPrimitivesAspect (theDrawer->FreeBoundaryAspect()->Aspect());
112     aGroup->AddPrimitiveArray (aTriFreeEdges);
113   }
114
115   Prs3d_NListOfSequenceOfPnt aCommonPolylines;
116   const Handle(Prs3d_LineAspect)& aWireAspect = theDrawer->WireAspect();
117   const Standard_Real aShapeDeflection = StdPrs_ToolTriangulatedShape::GetDeflection (theShape, theDrawer);
118
119   // Draw isolines
120   {
121     Prs3d_NListOfSequenceOfPnt  aUPolylines, aVPolylines;
122     Prs3d_NListOfSequenceOfPnt* aUPolylinesPtr = &aUPolylines;
123     Prs3d_NListOfSequenceOfPnt* aVPolylinesPtr = &aVPolylines;
124
125     const Handle(Prs3d_LineAspect)& anIsoAspectU = theDrawer->UIsoAspect();
126     const Handle(Prs3d_LineAspect)& anIsoAspectV = theDrawer->VIsoAspect();
127     if (anIsoAspectV->Aspect()->IsEqual (*anIsoAspectU->Aspect()))
128     {
129       aVPolylinesPtr = aUPolylinesPtr;  // put both U and V isolines into single group
130     }
131     if (anIsoAspectU->Aspect()->IsEqual (*aWireAspect->Aspect()))
132     {
133       aUPolylinesPtr = &aCommonPolylines; // put U isolines into single group with common edges
134     }
135     if (anIsoAspectV->Aspect()->IsEqual (*aWireAspect->Aspect()))
136     {
137       aVPolylinesPtr = &aCommonPolylines; // put V isolines into single group with common edges
138     }
139
140     bool isParallelIso = false;
141     if (theIsParallel)
142     {
143       Standard_Integer aNbFaces = 0;
144       for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
145       {
146         ++aNbFaces;
147       }
148       if (aNbFaces > 1)
149       {
150         isParallelIso = true;
151         std::vector<TopoDS_Face> aFaces (aNbFaces);
152         aNbFaces = 0;
153         for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
154         {
155           const TopoDS_Face& aFace = TopoDS::Face (aFaceExplorer.Current());
156           if (theDrawer->IsoOnPlane() || !StdPrs_ShapeTool::IsPlanarFace (aFace))
157           {
158             aFaces[aNbFaces++] = aFace;
159           }
160         }
161
162         StdPrs_WFShape_IsoFunctor anIsoFunctor (*aUPolylinesPtr, *aVPolylinesPtr, aFaces, theDrawer, aShapeDeflection);
163         OSD_Parallel::For (0, aNbFaces, anIsoFunctor, aNbFaces < 2);
164       }
165     }
166
167     if (!isParallelIso)
168     {
169       for (TopExp_Explorer aFaceExplorer (theShape, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next())
170       {
171         const TopoDS_Face& aFace = TopoDS::Face (aFaceExplorer.Current());
172         if (theDrawer->IsoOnPlane() || !StdPrs_ShapeTool::IsPlanarFace (aFace))
173         {
174           StdPrs_Isolines::Add (aFace, theDrawer, aShapeDeflection, *aUPolylinesPtr, *aVPolylinesPtr);
175         }
176       }
177     }
178
179     Prs3d::AddPrimitivesGroup (thePresentation, anIsoAspectU, aUPolylines);
180     Prs3d::AddPrimitivesGroup (thePresentation, anIsoAspectV, aVPolylines);
181   }
182
183   {
184     Prs3d_NListOfSequenceOfPnt anUnfree, aFree;
185     Prs3d_NListOfSequenceOfPnt* anUnfreePtr = &anUnfree;
186     Prs3d_NListOfSequenceOfPnt* aFreePtr = &aFree;
187     if (!theDrawer->UnFreeBoundaryDraw())
188     {
189       anUnfreePtr = NULL;
190     }
191     else if (theDrawer->UnFreeBoundaryAspect()->Aspect()->IsEqual (*aWireAspect->Aspect()))
192     {
193       anUnfreePtr = &aCommonPolylines; // put unfree edges into single group with common edges
194     }
195
196     if (!theDrawer->FreeBoundaryDraw())
197     {
198       aFreePtr = NULL;
199     }
200     else if (theDrawer->FreeBoundaryAspect()->Aspect()->IsEqual (*aWireAspect->Aspect()))
201     {
202       aFreePtr = &aCommonPolylines; // put free edges into single group with common edges
203     }
204
205     addEdges (theShape,
206               theDrawer,
207               aShapeDeflection,
208               theDrawer->WireDraw() ? &aCommonPolylines : NULL,
209               aFreePtr,
210               anUnfreePtr);
211     Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->UnFreeBoundaryAspect(), anUnfree);
212     Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->FreeBoundaryAspect(), aFree);
213   }
214
215   Prs3d::AddPrimitivesGroup (thePresentation, theDrawer->WireAspect(), aCommonPolylines);
216
217   if (Handle(Graphic3d_ArrayOfPoints) aVertexArray = AddVertexes (theShape, theDrawer->VertexDrawMode()))
218   {
219     Handle(Graphic3d_Group) aGroup = thePresentation->NewGroup();
220     aGroup->SetPrimitivesAspect (theDrawer->PointAspect()->Aspect());
221     aGroup->AddPrimitiveArray (aVertexArray);
222   }
223 }
224
225 // =========================================================================
226 // function : AddAllEdges
227 // purpose  :
228 // =========================================================================
229 Handle(Graphic3d_ArrayOfPrimitives) StdPrs_WFShape::AddAllEdges (const TopoDS_Shape& theShape,
230                                                                  const Handle(Prs3d_Drawer)& theDrawer)
231 {
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);
237 }
238
239 // =========================================================================
240 // function : addEdges
241 // purpose  :
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)
249 {
250   if (theShape.IsNull())
251   {
252     return;
253   }
254
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())
259   {
260     const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
261     const Standard_Integer aNbNeighbours = anEdgeIter.Value().Extent();
262     switch (aNbNeighbours)
263     {
264       case 0:
265       {
266         if (theWire != NULL)
267         {
268           aLWire.Append (anEdge);
269         }
270         break;
271       }
272       case 1:
273       {
274         if (theFree != NULL)
275         {
276           aLFree.Append (anEdge);
277         }
278         break;
279       }
280       default:
281       {
282         if (theUnFree)
283         {
284           aLUnFree.Append (anEdge);
285         }
286         break;
287       }
288     }
289   }
290
291   if (!aLWire.IsEmpty())
292   {
293     addEdges (aLWire, theDrawer, theShapeDeflection, *theWire);
294   }
295   if (!aLFree.IsEmpty())
296   {
297     addEdges (aLFree, theDrawer, theShapeDeflection, *theFree);
298   }
299   if (!aLUnFree.IsEmpty())
300   {
301     addEdges (aLUnFree, theDrawer, theShapeDeflection, *theUnFree);
302   }
303 }
304
305 // =========================================================================
306 // function : AddEdges
307 // purpose  :
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)
313 {
314   TopTools_ListIteratorOfListOfShape anEdgesIter;
315   for (anEdgesIter.Initialize (theEdges); anEdgesIter.More(); anEdgesIter.Next())
316   {
317     const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgesIter.Value());
318     if (BRep_Tool::Degenerated (anEdge))
319     {
320       continue;
321     }
322
323     Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt;
324
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;
330
331     if (!anEdgeIndicies.IsNull())
332     {
333       // Presentation based on triangulation of a face.
334       const TColStd_Array1OfInteger& anIndices = anEdgeIndicies->Nodes();
335
336       Standard_Integer anIndex = anIndices.Lower();
337       if (aLocation.IsIdentity())
338       {
339         for (; anIndex <= anIndices.Upper(); ++anIndex)
340         {
341           aPoints->Append (aTriangulation->Node (anIndices[anIndex]));
342         }
343       }
344       else
345       {
346         for (; anIndex <= anIndices.Upper(); ++anIndex)
347         {
348           aPoints->Append (aTriangulation->Node (anIndices[anIndex]).Transformed (aLocation));
349         }
350       }
351     }
352     else if (!(aPolygon = BRep_Tool::Polygon3D (anEdge, aLocation)).IsNull())
353     {
354       // Presentation based on triangulation of the free edge on a surface.
355       const TColgp_Array1OfPnt& aNodes = aPolygon->Nodes();
356       Standard_Integer anIndex = aNodes.Lower();
357       if (aLocation.IsIdentity())
358       {
359         for (; anIndex <= aNodes.Upper(); ++anIndex)
360         {
361           aPoints->Append (aNodes.Value (anIndex));
362         }
363       }
364       else
365       {
366         for (; anIndex <= aNodes.Upper(); ++anIndex)
367         {
368           aPoints->Append (aNodes.Value (anIndex).Transformed (aLocation));
369         }
370       }
371     }
372     else if (BRep_Tool::IsGeometric (anEdge))
373     {
374       // Default presentation for edges without triangulation.
375       BRepAdaptor_Curve aCurve (anEdge);
376       StdPrs_DeflectionCurve::Add (Handle(Prs3d_Presentation)(),
377                                    aCurve,
378                                    theShapeDeflection,
379                                    theDrawer,
380                                    aPoints->ChangeSequence(),
381                                    Standard_False);
382     }
383
384     if (!aPoints->IsEmpty())
385     {
386       thePolylines.Append (aPoints);
387     }
388   }
389 }
390
391 // =========================================================================
392 // function : AddEdgesOnTriangulation
393 // purpose  :
394 // =========================================================================
395 Handle(Graphic3d_ArrayOfPrimitives) StdPrs_WFShape::AddEdgesOnTriangulation (const TopoDS_Shape& theShape,
396                                                                              const Standard_Boolean theToExcludeGeometric)
397 {
398   TColgp_SequenceOfPnt aSeqPnts;
399   AddEdgesOnTriangulation (aSeqPnts, theShape, theToExcludeGeometric);
400   if (aSeqPnts.Size() < 2)
401   {
402     return Handle(Graphic3d_ArrayOfSegments)();
403   }
404
405   Standard_Integer aNbVertices = aSeqPnts.Size();
406   Handle(Graphic3d_ArrayOfSegments) aSurfArray = new Graphic3d_ArrayOfSegments (aNbVertices);
407   for (Standard_Integer anI = 1; anI <= aNbVertices; anI += 2)
408   {
409     aSurfArray->AddVertex (aSeqPnts.Value (anI));
410     aSurfArray->AddVertex (aSeqPnts.Value (anI + 1));
411   }
412   return aSurfArray;
413 }
414
415 // =========================================================================
416 // function : AddEdgesOnTriangulation
417 // purpose  :
418 // =========================================================================
419 void StdPrs_WFShape::AddEdgesOnTriangulation (TColgp_SequenceOfPnt& theSegments,
420                                               const TopoDS_Shape& theShape,
421                                               const Standard_Boolean theToExcludeGeometric)
422 {
423   TopLoc_Location aLocation, aDummyLoc;
424   for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
425   {
426     const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
427     if (theToExcludeGeometric)
428     {
429       const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface (aFace, aDummyLoc);
430       if (!aSurf.IsNull())
431       {
432         continue;
433       }
434     }
435     if (const Handle(Poly_Triangulation)& aPolyTri = BRep_Tool::Triangulation (aFace, aLocation))
436     {
437       Prs3d::AddFreeEdges (theSegments, aPolyTri, aLocation);
438     }
439   }
440 }
441
442 // =========================================================================
443 // function : AddVertexes
444 // purpose  :
445 // =========================================================================
446 Handle(Graphic3d_ArrayOfPoints) StdPrs_WFShape::AddVertexes (const TopoDS_Shape& theShape,
447                                                              Prs3d_VertexDrawMode theVertexMode)
448 {
449   TColgp_SequenceOfPnt aShapeVertices;
450   if (theVertexMode == Prs3d_VDM_All)
451   {
452     for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX); aVertIter.More(); aVertIter.Next())
453     {
454       const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
455       aShapeVertices.Append (BRep_Tool::Pnt (aVert));
456     }
457   }
458   else
459   {
460     // isolated vertices
461     for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX, TopAbs_EDGE); aVertIter.More(); aVertIter.Next())
462     {
463       const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
464       aShapeVertices.Append (BRep_Tool::Pnt (aVert));
465     }
466
467     // internal vertices
468     for (TopExp_Explorer anEdgeIter (theShape, TopAbs_EDGE); anEdgeIter.More(); anEdgeIter.Next())
469     {
470       for (TopoDS_Iterator aVertIter (anEdgeIter.Current(), Standard_False, Standard_True); aVertIter.More(); aVertIter.Next())
471       {
472         const TopoDS_Shape& aVertSh = aVertIter.Value();
473         if (aVertSh.Orientation() == TopAbs_INTERNAL
474             && aVertSh.ShapeType() == TopAbs_VERTEX)
475         {
476           const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertSh);
477           aShapeVertices.Append (BRep_Tool::Pnt (aVert));
478         }
479       }
480     }
481   }
482
483   if (aShapeVertices.IsEmpty())
484   {
485     return Handle(Graphic3d_ArrayOfPoints)();
486   }
487
488   const Standard_Integer aNbVertices = aShapeVertices.Length();
489   Handle(Graphic3d_ArrayOfPoints) aVertexArray = new Graphic3d_ArrayOfPoints (aNbVertices);
490   for (Standard_Integer aVertIter = 1; aVertIter <= aNbVertices; ++aVertIter)
491   {
492     aVertexArray->AddVertex (aShapeVertices.Value (aVertIter));
493   }
494   return aVertexArray;
495 }