0031490: Foundation Classes, Poly_Connect - speed up temporary allocations
[occt.git] / src / StdPrs / StdPrs_ShadedShape.cxx
index cfbb5eb..ca30cd5 100644 (file)
 #include <StdPrs_ShadedShape.hxx>
 
 #include <Bnd_Box.hxx>
-#include <BRep_Builder.hxx>
-#include <BRepBndLib.hxx>
-#include <BRepMesh_DiscretFactory.hxx>
-#include <BRepMesh_DiscretRoot.hxx>
 #include <BRepTools.hxx>
+#include <BRepBndLib.hxx>
+#include <BRep_Builder.hxx>
+#include <BRep_Tool.hxx>
 #include <Graphic3d_ArrayOfSegments.hxx>
 #include <Graphic3d_ArrayOfTriangles.hxx>
 #include <Graphic3d_AspectFillArea3d.hxx>
 #include <gp_Dir.hxx>
 #include <gp_Vec.hxx>
 #include <gp_Pnt.hxx>
+#include <NCollection_IncAllocator.hxx>
 #include <NCollection_List.hxx>
+#include <NCollection_Shared.hxx>
 #include <Precision.hxx>
-#include <Prs3d.hxx>
 #include <Prs3d_Drawer.hxx>
+#include <Prs3d_IsoAspect.hxx>
 #include <Prs3d_LineAspect.hxx>
 #include <Prs3d_Presentation.hxx>
 #include <Prs3d_ShadingAspect.hxx>
 #include <Poly_Connect.hxx>
 #include <Poly_PolygonOnTriangulation.hxx>
 #include <Poly_Triangulation.hxx>
-#include <StdPrs_ToolShadedShape.hxx>
+#include <StdPrs_ToolTriangulatedShape.hxx>
 #include <StdPrs_WFShape.hxx>
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
@@ -61,18 +62,16 @@ namespace
                            const TopoDS_Shape&                theShape,
                            const Handle (Prs3d_Drawer)&       theDrawer)
   {
-    Standard_Boolean aDrawAllVerticesFlag = (theDrawer->VertexDrawMode() == Prs3d_VDM_All);
-
-    if (!aDrawAllVerticesFlag && theShape.ShapeType() != TopAbs_COMPOUND)
+    TopExp_Explorer aShapeIter (theShape, TopAbs_FACE);
+    if (!aShapeIter.More())
     {
+      StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
       return;
     }
 
-    TopExp_Explorer aShapeIter (theShape, TopAbs_FACE);
-    if (!aShapeIter.More())
+    const Standard_Boolean aDrawAllVerticesFlag = (theDrawer->VertexDrawMode() == Prs3d_VDM_All);
+    if (!aDrawAllVerticesFlag && theShape.ShapeType() != TopAbs_COMPOUND)
     {
-      // compound contains no shaded elements at all
-      StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
       return;
     }
 
@@ -105,6 +104,42 @@ namespace
     }
   }
 
+  //! Computes special wireframe presentation for faces without triangulation.
+  void wireframeNoTriangFacesFromShape (const Handle(Prs3d_Presentation)& thePrs,
+                                        const TopoDS_Shape&               theShape,
+                                        const Handle(Prs3d_Drawer)&       theDrawer)
+  {
+    TopoDS_Compound aCompoundWF;
+    BRep_Builder aBuilder;
+    aBuilder.MakeCompound (aCompoundWF);
+    TopLoc_Location aLoc;
+    Standard_Boolean hasElement = Standard_False;
+
+    for (TopExp_Explorer aShapeIter(theShape, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
+    {
+      const TopoDS_Face& aFace = TopoDS::Face (aShapeIter.Current());
+      const Handle(Poly_Triangulation) aTriang = BRep_Tool::Triangulation (aFace, aLoc);
+      if (aTriang.IsNull())
+      {
+        hasElement = Standard_True;
+        aBuilder.Add (aCompoundWF, aFace);
+      }
+    }
+
+    if (hasElement)
+    {
+      Standard_Integer aPrevUIsoNb = theDrawer->UIsoAspect()->Number();
+      Standard_Integer aPrevVIsoNb = theDrawer->VIsoAspect()->Number();
+      theDrawer->UIsoAspect()->SetNumber (5);
+      theDrawer->VIsoAspect()->SetNumber (5);
+
+      StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
+
+      theDrawer->UIsoAspect()->SetNumber (aPrevUIsoNb);
+      theDrawer->VIsoAspect()->SetNumber (aPrevVIsoNb);
+    }
+  }
+
   //! Gets triangulation of every face of shape and fills output array of triangles
   static Handle(Graphic3d_ArrayOfTriangles) fillTriangles (const TopoDS_Shape&    theShape,
                                                            const Standard_Boolean theHasTexels,
@@ -125,7 +160,7 @@ namespace
     for (; aFaceIt.More(); aFaceIt.Next())
     {
       const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
-      aT = StdPrs_ToolShadedShape::Triangulation (aFace, aLoc);
+      aT = BRep_Tool::Triangulation (aFace, aLoc);
       if (!aT.IsNull())
       {
         aNbTriangles += aT->NbTriangles();
@@ -143,18 +178,24 @@ namespace
     for (aFaceIt.Init (theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
     {
       const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
-      aT = StdPrs_ToolShadedShape::Triangulation (aFace, aLoc);
+      aT = BRep_Tool::Triangulation (aFace, aLoc);
       if (aT.IsNull())
       {
         continue;
       }
       const gp_Trsf& aTrsf = aLoc.Transformation();
-      Poly_Connect aPolyConnect (aT);
+
+      // Determinant of transform matrix less then 0 means that mirror transform applied.
+      Standard_Boolean isMirrored = aTrsf.VectorialPart().Determinant() < 0;
+
       // Extracts vertices & normals from nodes
       const TColgp_Array1OfPnt&   aNodes   = aT->Nodes();
-      const TColgp_Array1OfPnt2d& aUVNodes = aT->UVNodes();
-      TColgp_Array1OfDir aNormals (aNodes.Lower(), aNodes.Upper());
-      StdPrs_ToolShadedShape::Normal (aFace, aPolyConnect, aNormals);
+      const TColgp_Array1OfPnt2d* aUVNodes = theHasTexels && aT->HasUVNodes() && aT->UVNodes().Upper() == aNodes.Upper()
+                                           ? &aT->UVNodes()
+                                           : NULL;
+      StdPrs_ToolTriangulatedShape::ComputeNormals (aFace, aT);
+      const TShort_Array1OfShortReal& aNormals = aT->Normals();
+      const Standard_ShortReal*       aNormArr = &aNormals.First();
 
       if (theHasTexels)
       {
@@ -167,21 +208,29 @@ namespace
       for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
       {
         aPoint = aNodes (aNodeIter);
+        const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
+        gp_Dir aNorm (aNormArr[anId + 0], aNormArr[anId + 1], aNormArr[anId + 2]);
+        if ((aFace.Orientation() == TopAbs_REVERSED) ^ isMirrored)
+        {
+          aNorm.Reverse();
+        }
         if (!aLoc.IsIdentity())
         {
           aPoint.Transform (aTrsf);
-          aNormals (aNodeIter).Transform (aTrsf);
+          aNorm.Transform (aTrsf);
         }
 
-        if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())
+        if (aUVNodes != NULL)
         {
-          const gp_Pnt2d aTexel = gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
-                                            (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
-          anArray->AddVertex (aPoint, aNormals (aNodeIter), aTexel);
+          const gp_Pnt2d aTexel = (dUmax == 0.0 || dVmax == 0.0)
+                                ? aUVNodes->Value (aNodeIter)
+                                : gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes->Value (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
+                                            (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes->Value (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
+          anArray->AddVertex (aPoint, aNorm, aTexel);
         }
         else
         {
-          anArray->AddVertex (aPoint, aNormals (aNodeIter));
+          anArray->AddVertex (aPoint, aNorm);
         }
       }
 
@@ -190,7 +239,7 @@ namespace
       Standard_Integer anIndex[3];
       for (Standard_Integer aTriIter = 1; aTriIter <= aT->NbTriangles(); ++aTriIter)
       {
-        if (aFace.Orientation() == TopAbs_REVERSED)
+        if ((aFace.Orientation() == TopAbs_REVERSED))
         {
           aTriangles (aTriIter).Get (anIndex[0], anIndex[2], anIndex[1]);
         }
@@ -223,65 +272,15 @@ namespace
         aV1.Cross (aV2);
         if (aV1.SquareMagnitude() > aPreci)
         {
-          anArray->AddEdge (anIndex[0] + aDecal);
-          anArray->AddEdge (anIndex[1] + aDecal);
-          anArray->AddEdge (anIndex[2] + aDecal);
+          anArray->AddEdges (anIndex[0] + aDecal,
+                             anIndex[1] + aDecal,
+                             anIndex[2] + aDecal);
         }
       }
     }
     return anArray;
   }
 
-  //! Searches closed and unclosed subshapes in shape structure
-  //! and puts them into two compounds for separate processing of closed and unclosed sub-shapes.
-  static void exploreSolids (const TopoDS_Shape& theShape,
-                             const BRep_Builder& theBuilder,
-                             TopoDS_Compound&    theCompoundForClosed,
-                             TopoDS_Compound&    theCompoundForOpened)
-  {
-    if (theShape.IsNull())
-    {
-      return;
-    }
-
-    switch (theShape.ShapeType())
-    {
-      case TopAbs_COMPOUND:
-      case TopAbs_COMPSOLID:
-      {
-        for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
-        {
-          exploreSolids (anIter.Value(), theBuilder, theCompoundForClosed, theCompoundForOpened);
-        }
-        return;
-      }
-      case TopAbs_SOLID:
-      {
-        for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
-        {
-          const TopoDS_Shape& aSubShape   = anIter.Value();
-          const Standard_Boolean isClosed = aSubShape.ShapeType() == TopAbs_SHELL &&
-                                            BRep_Tool::IsClosed (aSubShape) &&
-                                            StdPrs_ToolShadedShape::IsTriangulated (aSubShape);
-          theBuilder.Add (isClosed ? theCompoundForClosed : theCompoundForOpened, aSubShape);
-        }
-        return;
-      }
-      case TopAbs_SHELL:
-      case TopAbs_FACE:
-      {
-        theBuilder.Add (theCompoundForOpened, theShape);
-        return;
-      }
-      case TopAbs_WIRE:
-      case TopAbs_EDGE:
-      case TopAbs_VERTEX:
-      case TopAbs_SHAPE:
-      default:
-        return;
-    }
-  }
-
   //! Prepare shaded presentation for specified shape
   static Standard_Boolean shadeFromShape (const TopoDS_Shape&               theShape,
                                           const Handle(Prs3d_Presentation)& thePrs,
@@ -290,7 +289,7 @@ namespace
                                           const gp_Pnt2d&                   theUVOrigin,
                                           const gp_Pnt2d&                   theUVRepeat,
                                           const gp_Pnt2d&                   theUVScale,
-                                          const Standard_Boolean            theIsClosed)
+                                          const bool                        theIsClosed)
   {
     Handle(Graphic3d_ArrayOfTriangles) aPArray = fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
     if (aPArray.IsNull())
@@ -300,135 +299,221 @@ namespace
 
     Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup (thePrs);
     aGroup->SetClosed (theIsClosed);
-    if (!theDrawer->ShadingAspectGlobal())
-    {
-      Handle(Graphic3d_AspectFillArea3d) anAsp = theDrawer->ShadingAspect()->Aspect();
-      theIsClosed ? anAsp->SuppressBackFace() : anAsp->AllowBackFace();
-      aGroup->SetGroupPrimitivesAspect (anAsp);
-    }
+    aGroup->SetGroupPrimitivesAspect (theDrawer->ShadingAspect()->Aspect());
     aGroup->AddPrimitiveArray (aPArray);
     return Standard_True;
   }
 
   //! Compute boundary presentation for faces of the shape.
-  static void computeFaceBoundaries (const TopoDS_Shape&               theShape,
-                                     const Handle(Prs3d_Presentation)& thePrs,
-                                     const Handle(Prs3d_Drawer)&       theDrawer)
+  static Handle(Graphic3d_ArrayOfSegments) fillFaceBoundaries (const TopoDS_Shape& theShape,
+                                                               GeomAbs_Shape theUpperContinuity)
   {
     // collection of all triangulation nodes on edges
     // for computing boundaries presentation
-    NCollection_List<Handle(TColgp_HArray1OfPnt)> aNodeCollection;
     Standard_Integer aNodeNumber = 0;
+    Standard_Integer aNbPolylines = 0;
 
     TopLoc_Location aTrsf;
 
+    Handle(NCollection_Shared<TColgp_SequenceOfPnt>) aSeqPntsExtra;
+    for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
+    {
+      const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
+      if (aFace.NbChildren() == 0)
+      {
+        // handle specifically faces without boundary definition (triangulation-only)
+        if (aSeqPntsExtra.IsNull())
+        {
+          Handle(NCollection_IncAllocator) anIncAlloc = new NCollection_IncAllocator();
+          aSeqPntsExtra = new NCollection_Shared<TColgp_SequenceOfPnt> (anIncAlloc);
+        }
+        StdPrs_WFShape::AddEdgesOnTriangulation (*aSeqPntsExtra, aFace, Standard_False);
+      }
+    }
+
     // explore all boundary edges
     TopTools_IndexedDataMapOfShapeListOfShape anEdgesMap;
-    TopExp::MapShapesAndAncestors (
-      theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
-
-    Standard_Integer anEdgeIdx = 1;
-    for ( ; anEdgeIdx <= anEdgesMap.Extent (); anEdgeIdx++)
+    TopExp::MapShapesAndAncestors (theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
+    for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
     {
       // reject free edges
-      const TopTools_ListOfShape& aFaceList = anEdgesMap.FindFromIndex (anEdgeIdx);
-      if (aFaceList.Extent() == 0)
+      if (anEdgeIter.Value().Extent() == 0)
+      {
         continue;
+      }
 
       // take one of the shared edges and get edge triangulation
-      const TopoDS_Face& aFace  = TopoDS::Face (aFaceList.First ());
-      const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgesMap.FindKey (anEdgeIdx));
-
-      Handle(Poly_Triangulation) aTriangulation =
-        BRep_Tool::Triangulation (aFace, aTrsf);
+      const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
+      Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
+      if (aTriangulation.IsNull())
+      {
+        continue;
+      }
 
-      if (aTriangulation.IsNull ())
+      const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
+      if (theUpperContinuity < GeomAbs_CN
+       && anEdgeIter.Value().Extent() >= 2
+       && BRep_Tool::MaxContinuity (anEdge) > theUpperContinuity)
+      {
         continue;
+      }
 
-      Handle(Poly_PolygonOnTriangulation) anEdgePoly =
-        BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
+      Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
+      if (!anEdgePoly.IsNull()
+        && anEdgePoly->Nodes().Length() >= 2)
+      {
+        aNodeNumber += anEdgePoly->Nodes().Length();
+        ++aNbPolylines;
+      }
+    }
+    const Standard_Integer aNbExtra = !aSeqPntsExtra.IsNull() ? aSeqPntsExtra->Size() : 0;
+    if (aNodeNumber == 0)
+    {
+      if (aNbExtra < 2)
+      {
+        return Handle(Graphic3d_ArrayOfSegments)();
+      }
 
-      if (anEdgePoly.IsNull ())
+      Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (aNbExtra);
+      for (TColgp_SequenceOfPnt::Iterator aPntIter (*aSeqPntsExtra); aPntIter.More(); aPntIter.Next())
+      {
+        aSegments->AddVertex (aPntIter.Value());
+      }
+      return aSegments;
+    }
+
+    // create indexed segments array to pack polylines from different edges into single array
+    const Standard_Integer aSegmentEdgeNb = (aNodeNumber - aNbPolylines) * 2;
+    Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (aNodeNumber + aNbExtra, aSegmentEdgeNb + aNbExtra);
+    for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
+    {
+      if (anEdgeIter.Value().Extent() == 0)
+      {
         continue;
+      }
 
-      // get edge nodes indexes from face triangulation
-      const TColgp_Array1OfPnt& aTriNodes = aTriangulation->Nodes ();
-      const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes ();
+      const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
+      Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
+      if (aTriangulation.IsNull())
+      {
+        continue;
+      }
+
+      const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
+      if (theUpperContinuity < GeomAbs_CN
+       && anEdgeIter.Value().Extent() >= 2
+       && BRep_Tool::MaxContinuity (anEdge) > theUpperContinuity)
+      {
+        continue;
+      }
 
-      if (anEdgeNodes.Length () < 2)
+      Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
+      if (anEdgePoly.IsNull()
+       || anEdgePoly->Nodes().Length () < 2)
+      {
         continue;
+      }
 
-      // collect the edge nodes
-      Handle(TColgp_HArray1OfPnt) aCollected =
-        new TColgp_HArray1OfPnt (anEdgeNodes.Lower (), anEdgeNodes.Upper ());
+      // get edge nodes indexes from face triangulation
+      const TColgp_Array1OfPnt&      aTriNodes   = aTriangulation->Nodes();
+      const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes();
 
-      Standard_Integer aNodeIdx = anEdgeNodes.Lower ();
-      for ( ; aNodeIdx <= anEdgeNodes.Upper (); aNodeIdx++)
+      // collect the edge nodes
+      Standard_Integer aSegmentEdge = aSegments->VertexNumber() + 1;
+      for (Standard_Integer aNodeIdx = anEdgeNodes.Lower(); aNodeIdx <= anEdgeNodes.Upper(); ++aNodeIdx)
       {
         // node index in face triangulation
-        Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
-
         // get node and apply location transformation to the node
+        const Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
         gp_Pnt aTriNode = aTriNodes.Value (aTriIndex);
-        if (!aTrsf.IsIdentity ())
+        if (!aTrsf.IsIdentity())
+        {
           aTriNode.Transform (aTrsf);
+        }
 
-        // add node to the boundary array
-        aCollected->SetValue (aNodeIdx, aTriNode);
+        aSegments->AddVertex (aTriNode);
+        if (aNodeIdx != anEdgeNodes.Lower())
+        {
+          aSegments->AddEdge (  aSegmentEdge);
+          aSegments->AddEdge (++aSegmentEdge);
+        }
       }
-
-      aNodeNumber += anEdgeNodes.Length ();
-      aNodeCollection.Append (aCollected);
     }
 
-    // check if it possible to continue building the presentation
-    if (aNodeNumber == 0)
-      return;
+    if (!aSeqPntsExtra.IsNull())
+    {
+      Standard_Integer aSegmentEdge = aSegments->VertexNumber();
+      for (TColgp_SequenceOfPnt::Iterator aPntIter (*aSeqPntsExtra); aPntIter.More(); aPntIter.Next())
+      {
+        aSegments->AddVertex (aPntIter.Value());
+        aSegments->AddEdge (++aSegmentEdge);
+      }
+    }
 
-    // allocate polyline array for presentation
-    Standard_Integer aSegmentEdgeNb = 
-      (aNodeNumber - aNodeCollection.Extent()) * 2;
+    return aSegments;
+  }
 
-    Handle(Graphic3d_ArrayOfSegments) aSegments = 
-      new Graphic3d_ArrayOfSegments (aNodeNumber, aSegmentEdgeNb);
+} // anonymous namespace
 
-    // build presentation for edge bondaries
-    NCollection_List<Handle(TColgp_HArray1OfPnt)>::Iterator 
-      aCollIt (aNodeCollection);
+// =======================================================================
+// function : ExploreSolids
+// purpose  :
+// =======================================================================
+void StdPrs_ShadedShape::ExploreSolids (const TopoDS_Shape&    theShape,
+                                        const BRep_Builder&    theBuilder,
+                                        TopoDS_Compound&       theClosed,
+                                        TopoDS_Compound&       theOpened,
+                                        const Standard_Boolean theIgnore1DSubShape)
+{
+  if (theShape.IsNull())
+  {
+    return;
+  }
 
-    // the edge index is increased in each iteration step to
-    // avoid contiguous segments between different face edges.
-    for ( ; aCollIt.More(); aCollIt.Next () )
+  switch (theShape.ShapeType())
+  {
+    case TopAbs_COMPOUND:
+    case TopAbs_COMPSOLID:
     {
-      const Handle(TColgp_HArray1OfPnt)& aNodeArray = aCollIt.Value ();
-
-      Standard_Integer aNodeIdx = aNodeArray->Lower ();
-
-      // add first node (this node is not shared with previous segment).
-      // for each face edge, indices for sharing nodes 
-      // between segments begin from the first added node.
-      Standard_Integer aSegmentEdge = 
-        aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
-
-      // add subsequent nodes and provide edge indexes for sharing
-      // the nodes between the sequential segments.
-      for ( aNodeIdx++; aNodeIdx <= aNodeArray->Upper (); aNodeIdx++ )
+      for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
       {
-        aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
-        aSegments->AddEdge (  aSegmentEdge);
-        aSegments->AddEdge (++aSegmentEdge);
+        ExploreSolids (anIter.Value(), theBuilder, theClosed, theOpened, theIgnore1DSubShape);
       }
+      return;
     }
-
-    // set up aspect and add polyline data
-    Handle(Graphic3d_AspectLine3d) aBoundaryAspect = 
-      theDrawer->FaceBoundaryAspect ()->Aspect ();
-
-    Handle(Graphic3d_Group) aPrsGrp = Prs3d_Root::CurrentGroup (thePrs);
-    aPrsGrp->SetGroupPrimitivesAspect (aBoundaryAspect);
-    aPrsGrp->AddPrimitiveArray (aSegments);
+    case TopAbs_SOLID:
+    {
+      for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
+      {
+        const TopoDS_Shape& aSubShape   = anIter.Value();
+        const Standard_Boolean isClosed = aSubShape.ShapeType() == TopAbs_SHELL &&
+                                          BRep_Tool::IsClosed (aSubShape)       &&
+                                          StdPrs_ToolTriangulatedShape::IsTriangulated (aSubShape);
+        theBuilder.Add (isClosed ? theClosed : theOpened, aSubShape);
+      }
+      return;
+    }
+    case TopAbs_SHELL:
+    case TopAbs_FACE:
+    {
+      theBuilder.Add (theOpened, theShape);
+      return;
+    }
+    case TopAbs_WIRE:
+    case TopAbs_EDGE:
+    case TopAbs_VERTEX:
+    {
+      if (!theIgnore1DSubShape)
+      {
+        theBuilder.Add (theOpened, theShape);
+      }
+      return;
+    }
+    case TopAbs_SHAPE:
+    default:
+      return;
   }
-};
+}
 
 // =======================================================================
 // function : Add
@@ -437,35 +522,11 @@ namespace
 void StdPrs_ShadedShape::Add (const Handle(Prs3d_Presentation)& thePrs,
                               const TopoDS_Shape&               theShape,
                               const Handle(Prs3d_Drawer)&       theDrawer,
-                              const Standard_Boolean            theToExploreSolids)
+                              const StdPrs_Volume               theVolume)
 {
   gp_Pnt2d aDummy;
   StdPrs_ShadedShape::Add (thePrs, theShape, theDrawer,
-                           Standard_False, aDummy, aDummy, aDummy, theToExploreSolids);
-}
-
-// =======================================================================
-// function : Tessellate
-// purpose  :
-// =======================================================================
-void StdPrs_ShadedShape::Tessellate (const TopoDS_Shape&          theShape,
-                                     const Handle (Prs3d_Drawer)& theDrawer)
-{
-  // Check if it is possible to avoid unnecessary recomputation of shape triangulation
-  Standard_Real aDeflection = Prs3d::GetDeflection (theShape, theDrawer);
-  if (BRepTools::Triangulation (theShape, aDeflection))
-  {
-    return;
-  }
-
-  // retrieve meshing tool from Factory
-  Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
-                                                                                   aDeflection,
-                                                                                   theDrawer->HLRAngle());
-  if (!aMeshAlgo.IsNull())
-  {
-    aMeshAlgo->Perform();
-  }
+                           Standard_False, aDummy, aDummy, aDummy, theVolume);
 }
 
 // =======================================================================
@@ -479,18 +540,25 @@ void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePrs,
                               const gp_Pnt2d&                    theUVOrigin,
                               const gp_Pnt2d&                    theUVRepeat,
                               const gp_Pnt2d&                    theUVScale,
-                              const Standard_Boolean             theToExploreSolids)
+                              const StdPrs_Volume                theVolume)
 {
   if (theShape.IsNull())
   {
     return;
   }
 
+  // Use automatic re-triangulation with deflection-check logic only if this feature is enable
+  if (theDrawer->IsAutoTriangulation())
+  {
+    // Triangulation completeness is important for "open-closed" analysis - perform tessellation beforehand
+    StdPrs_ToolTriangulatedShape::Tessellate (theShape, theDrawer);
+  }
+
   // add wireframe presentation for isolated edges and vertices
   wireframeFromShape (thePrs, theShape, theDrawer);
 
-  // Triangulation completeness is important for "open-closed" analysis - perform tessellation beforehand
-  Tessellate (theShape, theDrawer);
+  // add special wireframe presentation for faces without triangulation
+  wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);
 
   // The shape types listed below need advanced analysis as potentially containing
   // both closed and open parts. Solids are also included, because they might
@@ -498,38 +566,87 @@ void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePrs,
   if ((theShape.ShapeType() == TopAbs_COMPOUND
     || theShape.ShapeType() == TopAbs_COMPSOLID
     || theShape.ShapeType() == TopAbs_SOLID)
-   &&  theToExploreSolids)
+   &&  theVolume == StdPrs_Volume_Autodetection)
   {
     // collect two compounds: for opened and closed (solid) sub-shapes
     TopoDS_Compound anOpened, aClosed;
     BRep_Builder aBuilder;
     aBuilder.MakeCompound (aClosed);
     aBuilder.MakeCompound (anOpened);
-    exploreSolids (theShape, aBuilder, aClosed, anOpened);
+    ExploreSolids (theShape, aBuilder, aClosed, anOpened, Standard_True);
 
-    TopoDS_Iterator aShapeIter (aClosed);
-    if (aShapeIter.More())
+    if (aClosed.NbChildren() > 0)
     {
       shadeFromShape (aClosed, thePrs, theDrawer,
-                      theHasTexels, theUVOrigin, theUVRepeat, theUVScale, Standard_True);
+                      theHasTexels, theUVOrigin, theUVRepeat, theUVScale, true);
     }
 
-    aShapeIter.Initialize (anOpened);
-    if (aShapeIter.More())
+    if (anOpened.NbChildren() > 0)
     {
       shadeFromShape (anOpened, thePrs, theDrawer,
-                      theHasTexels, theUVOrigin, theUVRepeat, theUVScale, Standard_False);
+                      theHasTexels, theUVOrigin, theUVRepeat, theUVScale, false);
     }
   }
   else
   {
+    // if the shape type is not compound, composolid or solid, use autodetection back-facing filled
     shadeFromShape (theShape, thePrs, theDrawer,
                     theHasTexels, theUVOrigin, theUVRepeat, theUVScale,
-                    StdPrs_ToolShadedShape::IsClosed (theShape));
+                    theVolume == StdPrs_Volume_Closed);
   }
 
-  if (theDrawer->IsFaceBoundaryDraw())
+  if (theDrawer->FaceBoundaryDraw())
   {
-    computeFaceBoundaries (theShape, thePrs, theDrawer);
+    if (Handle(Graphic3d_ArrayOfSegments) aBndSegments = fillFaceBoundaries (theShape, theDrawer->FaceBoundaryUpperContinuity()))
+    {
+      Handle(Graphic3d_Group) aPrsGrp = thePrs->NewGroup();
+      aPrsGrp->SetGroupPrimitivesAspect (theDrawer->FaceBoundaryAspect()->Aspect());
+      aPrsGrp->AddPrimitiveArray (aBndSegments);
+    }
   }
 }
+
+// =======================================================================
+// function : FillTriangles
+// purpose  :
+// =======================================================================
+Handle(Graphic3d_ArrayOfTriangles) StdPrs_ShadedShape::FillTriangles (const TopoDS_Shape&    theShape,
+                                                                      const Standard_Boolean theHasTexels,
+                                                                      const gp_Pnt2d&        theUVOrigin,
+                                                                      const gp_Pnt2d&        theUVRepeat,
+                                                                      const gp_Pnt2d&        theUVScale)
+{
+  return fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
+}
+
+// =======================================================================
+// function : FillFaceBoundaries
+// purpose  :
+// =======================================================================
+Handle(Graphic3d_ArrayOfSegments) StdPrs_ShadedShape::FillFaceBoundaries (const TopoDS_Shape& theShape,
+                                                                          GeomAbs_Shape theUpperContinuity)
+{
+  return fillFaceBoundaries (theShape, theUpperContinuity);
+}
+
+// =======================================================================
+// function : AddWireframeForFreeElements
+// purpose  :
+// =======================================================================
+void StdPrs_ShadedShape::AddWireframeForFreeElements (const Handle (Prs3d_Presentation)& thePrs,
+                                                      const TopoDS_Shape&                theShape,
+                                                      const Handle (Prs3d_Drawer)&       theDrawer)
+{
+  wireframeFromShape (thePrs, theShape, theDrawer);
+}
+
+// =======================================================================
+// function : AddWireframeForFacesWithoutTriangles
+// purpose  :
+// =======================================================================
+void StdPrs_ShadedShape::AddWireframeForFacesWithoutTriangles (const Handle(Prs3d_Presentation)& thePrs,
+                                                               const TopoDS_Shape&               theShape,
+                                                               const Handle(Prs3d_Drawer)&       theDrawer)
+{
+  wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);
+}