Salome HOME
Improve GEOM_AISShape::computeMassCenter() on edges and faces
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
index b8531ad4035e839e518dfb31e0fe629601fbf0ca..4734bf77e11f57d358c04d6bdcb19c1b3ebec113 100644 (file)
 
 // Open CASCADE Includes
 #include <AIS_InteractiveContext.hxx>
+#include <BRepAdaptor_Surface.hxx>
 #include <BRep_Tool.hxx>
 #include <GCPnts_AbscissaPoint.hxx>
 #include <GeomAdaptor_Curve.hxx>
-#include <gp_Pnt.hxx>
-#include <gp_Dir.hxx>
-#include <gp_Vec.hxx>
+#include <Graphic3d_ArrayOfPoints.hxx>
 #include <Graphic3d_AspectFillArea3d.hxx>
 #include <Graphic3d_AspectLine3d.hxx>
 #include <Graphic3d_AspectMarker3d.hxx>
 #include <Graphic3d_AspectText3d.hxx>
-
-#include <Prs3d_ShadingAspect.hxx>
 #include <Prs3d_Arrow.hxx>
 #include <Prs3d_IsoAspect.hxx>
-#if OCC_VERSION_LARGE > 0x06070200
-#include <Prs3d_VertexDrawMode.hxx>
-#endif
-
+#include <Prs3d_ShadingAspect.hxx>
 #include <SelectBasics_SensitiveEntity.hxx>
 #include <SelectMgr_EntityOwner.hxx>
-#include <StdSelect_BRepOwner.hxx>
 #include <SelectMgr_IndexedMapOfOwner.hxx>
 #include <SelectMgr_Selection.hxx>
-#include <StdSelect_DisplayMode.hxx>
 #include <StdPrs_ShadedShape.hxx>
-#include <StdPrs_WFDeflectionShape.hxx>
-
+#include <StdSelect_BRepOwner.hxx>
+#include <StdSelect_DisplayMode.hxx>
 #include <TColStd_IndexedMapOfInteger.hxx>
 #include <TColStd_ListIteratorOfListOfInteger.hxx>
 #include <TColStd_ListOfInteger.hxx>
 #include <TopExp.hxx>
-#include <TopoDS_Shape.hxx>
-#include <TopTools_IndexedMapOfShape.hxx>
-#include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
+#include <TopTools_IndexedMapOfShape.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Vertex.hxx>
+#include <V3d_View.hxx>
+#include <gp_Dir.hxx>
+#include <gp_Pnt.hxx>
+#include <gp_Vec.hxx>
 
-#include <TColStd_SequenceOfInteger.hxx>
+#if OCC_VERSION_LARGE > 0x06070200
+#include <Prs3d_VertexDrawMode.hxx>
+#endif
 
-#include <V3d_View.hxx>
+#if OCC_VERSION_MAJOR < 7
+  #include <StdPrs_WFDeflectionShape.hxx>
+#else
+  #include <StdPrs_WFShape.hxx>
+#endif
 
 #include <SalomeApp_Tools.h>
 #include <SUIT_Session.h>
 #include <SUIT_ResourceMgr.h>
 
-IMPLEMENT_STANDARD_HANDLE (GEOM_AISShape, SALOME_AISShape)
-IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
+OCCT_IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
 
 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
 Quantity_Color GEOM_AISShape::myTopLevelColor;
@@ -251,7 +250,11 @@ void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresent
       if( !isTopLev && anIsColorField && myFieldDimension == 1 )
         drawField( aPrs, false, aMode == CustomHighlight );
       else
+#if OCC_VERSION_MAJOR < 7
         StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);      
+#else
+        StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
+#endif
       break;
     }
     case Shading:
@@ -272,9 +275,9 @@ void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresent
     case TexturedShape:
     {
 #ifdef USE_TEXTURED_SHAPE
-       AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
+        AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
 #else
-       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
+        AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
 #endif
     }
   }
@@ -612,7 +615,11 @@ void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
             myDrawer->WireAspect()->SetWidth( myOwnWidth );
           else
             myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
+#if OCC_VERSION_MAJOR < 7
           StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
+#else
+          StdPrs_WFShape::Add( thePrs, aSubShape, myDrawer );
+#endif
         }
         else if( myFieldDimension == 2 ||
                  myFieldDimension == 3 ||
@@ -645,30 +652,46 @@ void GEOM_AISShape::drawName( const Handle(Prs3d_Presentation)& thePrs )
 }
 
 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
-                                                   gp_Pnt& theCenter )
+                                                   gp_Pnt&             theCenter )
 {
-  Standard_Real aX = 0, aY = 0, aZ = 0;
+  theCenter.SetCoord( 0,0,0 );
   Standard_Integer aNbPoints = 0;
 
-  TopExp_Explorer anExp;
-  for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
+  if ( theShape.ShapeType() == TopAbs_EDGE )
+  {
+    double f,l;
+    Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( theShape ), f, l );
+    if ( !curve.IsNull() )
+    {
+      theCenter = curve->Value( 0.5 * ( f + l ));
+      aNbPoints = 1;
+    }
+  }
+  else if ( theShape.ShapeType() == TopAbs_FACE )
   {
-    TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
-    if( !aVertex.IsNull() )
+    BRepAdaptor_Surface surface( TopoDS::Face( theShape ));
+    theCenter = surface.Value( 0.5 * ( surface.FirstUParameter() + surface.LastUParameter() ),
+                               0.5 * ( surface.FirstVParameter() + surface.LastVParameter() ));
+    aNbPoints = 1;
+  }
+
+  if ( aNbPoints == 0 )
+  {
+    TopExp_Explorer anExp;
+    for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
     {
-      gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
-      aX += aPnt.X();
-      aY += aPnt.Y();
-      aZ += aPnt.Z();
-      aNbPoints++;
+      TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
+      if( !aVertex.IsNull() )
+      {
+        gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
+        theCenter.ChangeCoord() += aPnt.XYZ();
+        aNbPoints++;
+      }
     }
   }
 
-  if( aNbPoints == 0 )
-    return Standard_False;
+  if ( aNbPoints > 0 )
+    theCenter.ChangeCoord() /= (Standard_Real) aNbPoints;
 
-  theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
-                      aY / (Standard_Real)aNbPoints,
-                      aZ / (Standard_Real)aNbPoints );
-  return Standard_True;
+  return aNbPoints;
 }