0022777: Visualization - Unsafe way to get attribute values from MeshVS_Drawer
[occt.git] / src / MeshVS / MeshVS_MeshPrsBuilder.cxx
index a3edb3b..7f7d01a 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <MeshVS_MeshPrsBuilder.ixx>
 
-#include <Prs3d_Root.hxx>
-#include <Prs3d_ShadingAspect.hxx>
-#include <Prs3d_LineAspect.hxx>
-#include <Prs3d_PointAspect.hxx>
-
-#include <Graphic3d_AspectLine3d.hxx>
-#include <Graphic3d_AspectFillArea3d.hxx>
-#include <Graphic3d_AspectMarker3d.hxx>
-#include <Graphic3d_ArrayOfPolygons.hxx>
-#include <Graphic3d_ArrayOfTriangles.hxx>
-#include <Graphic3d_ArrayOfSegments.hxx>
+#include <Aspect_InteriorStyle.hxx>
 #include <Graphic3d_ArrayOfPoints.hxx>
+#include <Graphic3d_ArrayOfPolygons.hxx>
 #include <Graphic3d_ArrayOfPolylines.hxx>
+#include <Graphic3d_ArrayOfPrimitives.hxx>
+#include <Graphic3d_ArrayOfSegments.hxx>
+#include <Graphic3d_ArrayOfTriangles.hxx>
+#include <Graphic3d_AspectFillArea3d.hxx>
+#include <Graphic3d_AspectLine3d.hxx>
+#include <Graphic3d_AspectMarker3d.hxx>
 #include <Graphic3d_Group.hxx>
-
-#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
-#include <TColStd_ListIteratorOfListOfReal.hxx>
-#include <TColStd_MapIntegerHasher.hxx>
-#include <TColStd_Array1OfReal.hxx>
-#include <TColStd_Array1OfInteger.hxx>
-#include <TColStd_SequenceOfInteger.hxx>
-#include <TColStd_PackedMapOfInteger.hxx>
-#include <TColStd_HPackedMapOfInteger.hxx>
-#include <TColStd_MapOfInteger.hxx>
-
-#include <Quantity_NameOfColor.hxx>
-#include <Aspect_InteriorStyle.hxx>
-#include <Select3D_SensitivePoint.hxx>
-
+#include <MeshVS_Buffer.hxx>
 #include <MeshVS_DataSource.hxx>
 #include <MeshVS_Drawer.hxx>
-#include <MeshVS_Mesh.hxx>
-#include <MeshVS_Tool.hxx>
 #include <MeshVS_DrawerAttribute.hxx>
 #include <MeshVS_MapOfTwoNodes.hxx>
-#include <MeshVS_Buffer.hxx>
+#include <MeshVS_Mesh.hxx>
+#include <MeshVS_MeshPrsBuilder.hxx>
 #include <MeshVS_SymmetricPairHasher.hxx>
-
+#include <MeshVS_Tool.hxx>
 #include <NCollection_Map.hxx>
 #include <NCollection_Vector.hxx>
+#include <Prs3d_LineAspect.hxx>
+#include <Prs3d_PointAspect.hxx>
+#include <Prs3d_Presentation.hxx>
+#include <Prs3d_Root.hxx>
+#include <Prs3d_ShadingAspect.hxx>
+#include <Quantity_NameOfColor.hxx>
+#include <Select3D_SensitivePoint.hxx>
+#include <Standard_Type.hxx>
+#include <TColStd_Array1OfInteger.hxx>
+#include <TColStd_Array1OfReal.hxx>
+#include <TColStd_HPackedMapOfInteger.hxx>
+#include <TColStd_ListIteratorOfListOfReal.hxx>
+#include <TColStd_MapIntegerHasher.hxx>
+#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
+#include <TColStd_MapOfInteger.hxx>
+#include <TColStd_PackedMapOfInteger.hxx>
+#include <TColStd_SequenceOfInteger.hxx>
+
+#ifdef _WIN32
+  #include <malloc.h> // for alloca()
+#endif
+
+IMPLEMENT_STANDARD_RTTIEXT(MeshVS_MeshPrsBuilder,MeshVS_PrsBuilder)
 
 //================================================================
 // Function : Constructor MeshVS_MeshPrsBuilder
@@ -191,7 +195,7 @@ void MeshVS_MeshPrsBuilder::BuildElements( const Handle(Prs3d_Presentation)& Prs
   aDispMode &= MeshVS_DMF_OCCMask;
   //--------------------------------------------------------
 
-  Standard_Real aShrinkCoef;
+  Standard_Real aShrinkCoef = 0.0;
   aDrawer->GetDouble ( MeshVS_DA_ShrinkCoeff, aShrinkCoef );
 
   Standard_Boolean IsWireFrame    = ( aDispMode==MeshVS_DMF_WireFrame ),
@@ -199,11 +203,12 @@ void MeshVS_MeshPrsBuilder::BuildElements( const Handle(Prs3d_Presentation)& Prs
                    IsShrink       = ( aDispMode==MeshVS_DMF_Shrink ),
                    HasHilightFlag = ( ( DisplayMode & MeshVS_DMF_HilightPrs )   != 0 ),
                    HasSelectFlag  = ( ( DisplayMode & MeshVS_DMF_SelectionPrs ) != 0 ),
-                   IsMeshReflect, IsMeshAllowOverlap, IsReflect, IsMeshSmoothShading = Standard_False;
+                   IsMeshReflect = Standard_False, IsMeshAllowOverlap = Standard_False,
+                   IsMeshSmoothShading = Standard_False;
 
   aDrawer->GetBoolean  ( MeshVS_DA_Reflection, IsMeshReflect );
   aDrawer->GetBoolean  ( MeshVS_DA_IsAllowOverlapped, IsMeshAllowOverlap );
-  IsReflect      = ( IsMeshReflect && !HasHilightFlag );
+  const Standard_Boolean IsReflect = ( IsMeshReflect && !HasHilightFlag );
   aDrawer->GetBoolean  ( MeshVS_DA_SmoothShading, IsMeshSmoothShading );
 
   // display mode for highlighted prs of groups
@@ -215,10 +220,10 @@ void MeshVS_MeshPrsBuilder::BuildElements( const Handle(Prs3d_Presentation)& Prs
   aDrawer->GetMaterial ( MeshVS_DA_FrontMaterial, AMat );
   if ( !IsReflect )
   {
-    AMat.SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
-    AMat.SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
-    AMat.SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
-    AMat.SetReflectionModeOff(Graphic3d_TOR_EMISSION);
+    AMat.SetAmbientColor (Quantity_NOC_BLACK);
+    AMat.SetDiffuseColor (Quantity_NOC_BLACK);
+    AMat.SetSpecularColor(Quantity_NOC_BLACK);
+    AMat.SetEmissiveColor(Quantity_NOC_BLACK);
   }
   Handle( Graphic3d_AspectFillArea3d ) aFill = MeshVS_Tool::CreateAspectFillArea3d( GetDrawer(), AMat );
   Handle( Graphic3d_AspectLine3d ) aBeam = MeshVS_Tool::CreateAspectLine3d ( GetDrawer() );
@@ -326,21 +331,10 @@ void MeshVS_MeshPrsBuilder::BuildElements( const Handle(Prs3d_Presentation)& Prs
     aEdgeSegments = new Graphic3d_ArrayOfSegments (aNbEdgePrimitives * 2);
   }
 
-  Standard_Integer howMany = 1;
-  if ( IsOverlapControl )
-    howMany = 2;
-
   TColStd_PackedMapOfInteger aCustomElements;
 
-  Quantity_Color       anOldEdgeColor;
-  Quantity_Color       anEdgeColor;
-  Quantity_Color       anIntColor;
-  Aspect_InteriorStyle anIntType;
-  Aspect_TypeOfLine    aLine;
-  Standard_Real        aWidth;
-
-  aFill->Values (anIntType, anIntColor, anEdgeColor, aLine, aWidth);
-  
+  Quantity_Color anOldEdgeColor;
+  Quantity_Color anEdgeColor = aFill->EdgeColor();
   MeshVS_MapOfTwoNodes aLinkNodes;
   
   // Forbid drawings of edges which overlap with some links
@@ -540,10 +534,10 @@ void MeshVS_MeshPrsBuilder::BuildHilightPrs ( const Handle(Prs3d_Presentation)&
 
   Graphic3d_MaterialAspect AMat;
   aDrawer->GetMaterial ( MeshVS_DA_FrontMaterial, AMat );
-  AMat.SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
-  AMat.SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
-  AMat.SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
-  AMat.SetReflectionModeOff(Graphic3d_TOR_EMISSION);
+  AMat.SetAmbientColor (Quantity_NOC_BLACK);
+  AMat.SetDiffuseColor (Quantity_NOC_BLACK);
+  AMat.SetSpecularColor(Quantity_NOC_BLACK);
+  AMat.SetEmissiveColor(Quantity_NOC_BLACK);
 
   Handle( Graphic3d_AspectFillArea3d ) aFill     = MeshVS_Tool::CreateAspectFillArea3d( GetDrawer(), AMat );
   Handle( Graphic3d_AspectLine3d )     aBeam     = MeshVS_Tool::CreateAspectLine3d( GetDrawer() );
@@ -1057,48 +1051,43 @@ void MeshVS_MeshPrsBuilder::DrawArrays( const Handle(Prs3d_Presentation)& Prs,
                    IsPolylines      = ( !theLines.IsNull() && theLines->ItemNumber() > 0 ),
                    IsLinkPolylines  = ( !theLinkLines.IsNull() && theLinkLines->ItemNumber() > 0 );
 
-  Aspect_InteriorStyle aStyle;
-  Quantity_Color anIntColor, aBackColor, anEdgeColor; 
-  Aspect_TypeOfLine aType;
-  Standard_Real aWidth;
+  Quantity_Color anIntColor  = theFillAsp->InteriorColor();
+  Quantity_Color aBackColor  = theFillAsp->BackInteriorColor();
+  Quantity_Color anEdgeColor = theFillAsp->EdgeColor();
+  Standard_Real  aWidth      = theFillAsp->EdgeWidth();
 
-  theFillAsp->Values( aStyle, anIntColor, aBackColor, anEdgeColor, aType, aWidth );
+  Standard_Boolean isSupressBackFaces = Standard_False;
+  Handle(MeshVS_Drawer) aDrawer = GetDrawer();
+  if (!aDrawer.IsNull())
+  {
+    aDrawer->GetBoolean (MeshVS_DA_SupressBackFaces, isSupressBackFaces);
+  }
 
   if ( IsPolygons && theFillAsp->FrontMaterial().Transparency()<0.01 )
   {
     Prs3d_Root::NewGroup ( Prs );
     Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup ( Prs );
-
+    aGroup->SetClosed (isSupressBackFaces == Standard_True);
+    Handle(Graphic3d_AspectFillArea3d) aFillAsp = new Graphic3d_AspectFillArea3d (*theFillAsp);
     //if ( IsPolygonsEdgesOff )
-      theFillAsp->SetEdgeOff ();
+      aFillAsp->SetEdgeOff ();
     //else
-    //  theFillAsp->SetEdgeOn ();
+    //  aFillAsp->SetEdgeOn ();
 
     if( anIntColor!=aBackColor )
-      theFillAsp->SetDistinguishOn();
+      aFillAsp->SetDistinguishOn();
     else
-      theFillAsp->SetDistinguishOff();
+      aFillAsp->SetDistinguishOff();
+
+    aGroup->SetPrimitivesAspect (aFillAsp);
 
     if( IsFacePolygons )
     {
-      aGroup->SetPrimitivesAspect ( theFillAsp );
       aGroup->AddPrimitiveArray ( thePolygons );
     }
 
     if( IsVolumePolygons )
     {
-      Handle( Graphic3d_AspectFillArea3d ) aCullFillAsp = 
-          new Graphic3d_AspectFillArea3d( *( theFillAsp.operator->() ) );
-
-      Standard_Boolean isSupressBackFaces = Standard_False;
-      Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
-      if (!aDrawer.IsNull())
-        aDrawer->GetBoolean  ( MeshVS_DA_SupressBackFaces, isSupressBackFaces );
-
-      if (isSupressBackFaces)
-        aCullFillAsp->SuppressBackFace();
-
-      aGroup->SetPrimitivesAspect ( aCullFillAsp );
       aGroup->AddPrimitiveArray ( theVolumesInShad );
     }
   }
@@ -1108,7 +1097,6 @@ void MeshVS_MeshPrsBuilder::DrawArrays( const Handle(Prs3d_Presentation)& Prs,
     Prs3d_Root::NewGroup ( Prs );
     Handle (Graphic3d_Group) aLGroup = Prs3d_Root::CurrentGroup ( Prs );
 
-    theFillAsp->SetEdgeOff();
     if ( IsSelected )
       aLGroup->SetPrimitivesAspect ( theLineAsp );
     else
@@ -1118,57 +1106,43 @@ void MeshVS_MeshPrsBuilder::DrawArrays( const Handle(Prs3d_Presentation)& Prs,
         ( anEdgeColor, Aspect_TOL_SOLID, aWidth ) );
     }
     aLGroup->AddPrimitiveArray ( theLines );
-    theFillAsp->SetEdgeOn();
   }
 
   if ( IsLinkPolylines )
   {
     Prs3d_Root::NewGroup ( Prs );
     Handle (Graphic3d_Group) aBeamGroup = Prs3d_Root::CurrentGroup ( Prs );
-
-    theFillAsp->SetEdgeOff();
     if ( !IsSelected )
       aBeamGroup->SetPrimitivesAspect ( theFillAsp );
     aBeamGroup->SetPrimitivesAspect ( theLineAsp );
     aBeamGroup->AddPrimitiveArray ( theLinkLines );
-    theFillAsp->SetEdgeOn();
   }
 
   if ( IsPolygons && theFillAsp->FrontMaterial().Transparency()>=0.01 )
   {
     Prs3d_Root::NewGroup ( Prs );
     Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup ( Prs );
-
+    aGroup->SetClosed (isSupressBackFaces == Standard_True);
+    Handle(Graphic3d_AspectFillArea3d) aFillAsp = new Graphic3d_AspectFillArea3d (*theFillAsp);
     //if ( IsPolygonsEdgesOff )
-      theFillAsp->SetEdgeOff ();
+      aFillAsp->SetEdgeOff ();
     //else
-    //  theFillAsp->SetEdgeOn ();
+    //  aFillAsp->SetEdgeOn ();
 
     if( anIntColor!=aBackColor )
-      theFillAsp->SetDistinguishOn();
+      aFillAsp->SetDistinguishOn();
     else
-      theFillAsp->SetDistinguishOff();
+      aFillAsp->SetDistinguishOff();
+
+    aGroup->SetPrimitivesAspect (aFillAsp);
 
     if( IsFacePolygons )
     {
-      aGroup->SetPrimitivesAspect ( theFillAsp );
       aGroup->AddPrimitiveArray ( thePolygons );
     }
 
     if( IsVolumePolygons )
     {
-      Handle( Graphic3d_AspectFillArea3d ) aCullFillAsp = 
-          new Graphic3d_AspectFillArea3d( *( theFillAsp.operator->() ) );
-
-      Standard_Boolean isSupressBackFaces = Standard_False;
-      Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
-      if (!aDrawer.IsNull())
-        aDrawer->GetBoolean  ( MeshVS_DA_SupressBackFaces, isSupressBackFaces );
-
-      if (isSupressBackFaces)
-        aCullFillAsp->SuppressBackFace();
-
-      aGroup->SetPrimitivesAspect ( aCullFillAsp );
       aGroup->AddPrimitiveArray ( theVolumesInShad );
     }
   }