0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[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_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>
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       const TColgp_Array1OfPnt&      aNodes    = aTriangulation->Nodes();
336
337       Standard_Integer anIndex = anIndices.Lower();
338       if (aLocation.IsIdentity())
339       {
340         for (; anIndex <= anIndices.Upper(); ++anIndex)
341         {
342           aPoints->Append (aNodes (anIndices (anIndex)));
343         }
344       }
345       else
346       {
347         for (; anIndex <= anIndices.Upper(); ++anIndex)
348         {
349           aPoints->Append (aNodes (anIndices (anIndex)).Transformed (aLocation));
350         }
351       }
352     }
353     else if (!(aPolygon = BRep_Tool::Polygon3D (anEdge, aLocation)).IsNull())
354     {
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())
359       {
360         for (; anIndex <= aNodes.Upper(); ++anIndex)
361         {
362           aPoints->Append (aNodes.Value (anIndex));
363         }
364       }
365       else
366       {
367         for (; anIndex <= aNodes.Upper(); ++anIndex)
368         {
369           aPoints->Append (aNodes.Value (anIndex).Transformed (aLocation));
370         }
371       }
372     }
373     else if (BRep_Tool::IsGeometric (anEdge))
374     {
375       // Default presentation for edges without triangulation.
376       BRepAdaptor_Curve aCurve (anEdge);
377       StdPrs_DeflectionCurve::Add (Handle(Prs3d_Presentation)(),
378                                    aCurve,
379                                    theShapeDeflection,
380                                    theDrawer,
381                                    aPoints->ChangeSequence(),
382                                    Standard_False);
383     }
384
385     if (!aPoints->IsEmpty())
386     {
387       thePolylines.Append (aPoints);
388     }
389   }
390 }
391
392 // =========================================================================
393 // function : AddEdgesOnTriangulation
394 // purpose  :
395 // =========================================================================
396 Handle(Graphic3d_ArrayOfPrimitives) StdPrs_WFShape::AddEdgesOnTriangulation (const TopoDS_Shape& theShape,
397                                                                              const Standard_Boolean theToExcludeGeometric)
398 {
399   TColgp_SequenceOfPnt aSeqPnts;
400   AddEdgesOnTriangulation (aSeqPnts, theShape, theToExcludeGeometric);
401   if (aSeqPnts.Size() < 2)
402   {
403     return Handle(Graphic3d_ArrayOfSegments)();
404   }
405
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)
409   {
410     aSurfArray->AddVertex (aSeqPnts.Value (anI));
411     aSurfArray->AddVertex (aSeqPnts.Value (anI + 1));
412   }
413   return aSurfArray;
414 }
415
416 // =========================================================================
417 // function : AddEdgesOnTriangulation
418 // purpose  :
419 // =========================================================================
420 void StdPrs_WFShape::AddEdgesOnTriangulation (TColgp_SequenceOfPnt& theSegments,
421                                               const TopoDS_Shape& theShape,
422                                               const Standard_Boolean theToExcludeGeometric)
423 {
424   TopLoc_Location aLocation, aDummyLoc;
425   for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
426   {
427     const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
428     if (theToExcludeGeometric)
429     {
430       const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface (aFace, aDummyLoc);
431       if (!aSurf.IsNull())
432       {
433         continue;
434       }
435     }
436     if (const Handle(Poly_Triangulation)& aPolyTri = BRep_Tool::Triangulation (aFace, aLocation))
437     {
438       Prs3d::AddFreeEdges (theSegments, aPolyTri, aLocation);
439     }
440   }
441 }
442
443 // =========================================================================
444 // function : AddVertexes
445 // purpose  :
446 // =========================================================================
447 Handle(Graphic3d_ArrayOfPoints) StdPrs_WFShape::AddVertexes (const TopoDS_Shape& theShape,
448                                                              Prs3d_VertexDrawMode theVertexMode)
449 {
450   TColgp_SequenceOfPnt aShapeVertices;
451   if (theVertexMode == Prs3d_VDM_All)
452   {
453     for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX); aVertIter.More(); aVertIter.Next())
454     {
455       const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
456       aShapeVertices.Append (BRep_Tool::Pnt (aVert));
457     }
458   }
459   else
460   {
461     // isolated vertices
462     for (TopExp_Explorer aVertIter (theShape, TopAbs_VERTEX, TopAbs_EDGE); aVertIter.More(); aVertIter.Next())
463     {
464       const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertIter.Current());
465       aShapeVertices.Append (BRep_Tool::Pnt (aVert));
466     }
467
468     // internal vertices
469     for (TopExp_Explorer anEdgeIter (theShape, TopAbs_EDGE); anEdgeIter.More(); anEdgeIter.Next())
470     {
471       for (TopoDS_Iterator aVertIter (anEdgeIter.Current(), Standard_False, Standard_True); aVertIter.More(); aVertIter.Next())
472       {
473         const TopoDS_Shape& aVertSh = aVertIter.Value();
474         if (aVertSh.Orientation() == TopAbs_INTERNAL
475             && aVertSh.ShapeType() == TopAbs_VERTEX)
476         {
477           const TopoDS_Vertex& aVert = TopoDS::Vertex (aVertSh);
478           aShapeVertices.Append (BRep_Tool::Pnt (aVert));
479         }
480       }
481     }
482   }
483
484   if (aShapeVertices.IsEmpty())
485   {
486     return Handle(Graphic3d_ArrayOfPoints)();
487   }
488
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)
492   {
493     aVertexArray->AddVertex (aShapeVertices.Value (aVertIter));
494   }
495   return aVertexArray;
496 }