Salome HOME
Fix for the bug #42: point C is not activated, but point C is shown in preview in...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
index 33ceeda2c3519d649e220421644ddfe3e5a1f4c2..9a6c0ca700144bd562ff588259b0508e277063a9 100644 (file)
@@ -22,6 +22,9 @@
 
 #include "HYDROGUI_Shape.h"
 
+#include "HYDROGUI_DataObject.h"
+#include "HYDROGUI_Tool.h"
+
 #include <AIS_Drawer.hxx>
 
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <Graphic3d_AspectFillArea3d.hxx>
 #include <Graphic3d_MaterialAspect.hxx>
 
-#include <HYDROData_Domain.h>
+#include <HYDROData_Document.h>
+#include <HYDROData_Image.h>
+#include <HYDROData_ImmersibleZone.h>
 #include <HYDROData_Polyline.h>
+#include <HYDROData_Region.h>
+#include <HYDROData_Zone.h>
 
+#include <TopoDS.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Face.hxx>
 
 #include <Prs3d_IsoAspect.hxx>
 
 #include <QColor>
+#include <QFile>
 
 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
-                                const Handle(HYDROData_Object)&       theObject )
+                                const Handle(HYDROData_Entity)&       theObject )
 : myContext( theContext ),
   myObject( theObject ),
   myIsHighlight( false ),
@@ -56,7 +65,8 @@ HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext
   myBorderColor( Qt::black ),
   myHighlightColor( Qt::white ),
   myIsToUpdate( false ),
-  myIsVisible( true )
+  myIsVisible( true ),
+  myDisplayMode( AIS_WireFrame )
 {
 }
 
@@ -66,11 +76,13 @@ HYDROGUI_Shape::~HYDROGUI_Shape()
 
   if ( !myShape.IsNull() )
     myShape.Nullify();
+
+  removeTextureFile();
 }
 
 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
 {
-  if ( myContext.IsNull() || myShape.IsNull() )
+  if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
     return;
 
   myContext->Display( myShape, theIsUpdateViewer );
@@ -94,31 +106,126 @@ void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
   // Try to retrieve information from object
   if ( !myObject.IsNull() )
   {
-    if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Domain) ) )
+    Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
+  
+    if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
     {
-      Handle(HYDROData_Domain) aDomainObj =
-        Handle(HYDROData_Domain)::DownCast( myObject );
+      Handle(HYDROData_ImmersibleZone) aZoneObj =
+        Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
 
-      QColor aFillingColor = aDomainObj->GetFillingColor();
-      QColor aBorderColor = aDomainObj->GetBorderColor();
-      TopoDS_Face aDomainFace = aDomainObj->Face();
+      QColor aFillingColor = aZoneObj->GetFillingColor();
+      QColor aBorderColor = aZoneObj->GetBorderColor();
+      TopoDS_Face aZoneFace = TopoDS::Face( aZoneObj->GetTopShape() );
 
       setFillingColor( aFillingColor, false, false );
       setBorderColor( aBorderColor, false, false );
-      setFace( aDomainFace, false, false );
+      setFace( aZoneFace, false, false );
     }
     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
     {
       Handle(HYDROData_Polyline) aPolyline =
         Handle(HYDROData_Polyline)::DownCast( myObject );
 
-      TopoDS_Wire aPolylineWire = aPolyline->Wire();
+      TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolyline->GetTopShape() );
 
       setWire( aPolylineWire, false, false );
     }
+    else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Region) ) )
+    {
+      Handle(HYDROData_Region) aRegion =
+        Handle(HYDROData_Region)::DownCast( myObject );
+    }
+    else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
+    {
+      Handle(HYDROData_Zone) aZone =
+        Handle(HYDROData_Zone)::DownCast( myObject );
+
+      TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
+
+      // Generate the filling color for zone
+      QStringList aGeomObjectsNames;
+
+      HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
+      HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
+      for ( ; anIter.More(); anIter.Next() )
+      {
+        Handle(HYDROData_Object) aRefbject = 
+          Handle(HYDROData_Object)::DownCast( anIter.Value() );
+        if ( aRefbject.IsNull() )
+          continue;
+
+        QString aRefObjectName = aRefbject->GetName();
+        if ( aRefObjectName.isEmpty() )
+          continue;
+
+        aGeomObjectsNames.append( aRefObjectName );
+      }
+       
+      setFace( aZoneFace, false, false );
+      if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
+      {
+        // Red color for a zone with bathymetry conflict
+        setFillingColor( Qt::red );
+      }
+      else
+      {
+        setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
+      }
+    }
+    else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
+    {
+      Handle(HYDROData_Image) anImageObj =
+        Handle(HYDROData_Image)::DownCast( myObject );
+
+      removeTextureFile();
+
+      QString aTextureFileName = generateTextureFileName( anImageObj );
+
+      QImage anImage = anImageObj->Image();
+      QString aFilePath = anImageObj->GetFilePath();
+      QTransform aTrsf = anImageObj->Trsf();
+
+      int aWidth = anImage.width();
+      int aHeight = anImage.height();
+
+      QTransform anInversion = QTransform::fromScale( -1, -1 );
+      anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
+
+      // temporary optimization, to reduce the saved image size (and the texture quality)
+      QImage anImageToSave = reduceTexture( anImage, 500 );
+      anImageToSave.save( aTextureFileName );
+
+      QPointF aPoint1( 0, 0 );
+      QPointF aPoint2( aWidth, 0 );
+      QPointF aPoint3( aWidth, aHeight );
+      QPointF aPoint4( 0, aHeight );
+
+      aPoint1 = aTrsf.map( aPoint1 );
+      aPoint2 = aTrsf.map( aPoint2 );
+      aPoint3 = aTrsf.map( aPoint3 );
+      aPoint4 = aTrsf.map( aPoint4 );
+
+      QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
+      QRectF aRect = aPolygon.boundingRect();
+
+      gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
+      gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
+      gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
+      gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
+
+      TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
+      TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
+      TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
+      TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
+
+      TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
+
+      setTextureFileName( aTextureFileName, false, false );
+      setFace( aWire, false, false );
+    }
   }
 
-  if ( myShape.IsNull() )
+  if ( myShape.IsNull() || !isVisible() )
     return;
 
   myContext->Display( myShape, theIsUpdateViewer );
@@ -163,6 +270,17 @@ bool HYDROGUI_Shape::isHighlighted() const
 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
                               const bool         theToDisplay,
                               const bool         theIsUpdateViewer )
+{
+  myTopoShape = theWire;
+  myDisplayMode = AIS_WireFrame;
+
+  buildShape();
+  updateShape( theToDisplay, theIsUpdateViewer );
+}
+
+void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
+                              const bool         theToDisplay,
+                              const bool         theIsUpdateViewer )
 {
   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
   aFaceBuilder.Build();
@@ -177,7 +295,9 @@ void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
                               const bool         theToDisplay,
                               const bool         theIsUpdateViewer )
 {
-  myFace = theFace;
+  myTopoShape = theFace;
+  myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
+
   buildShape();
   updateShape( theToDisplay, theIsUpdateViewer );
 }
@@ -218,43 +338,71 @@ QColor HYDROGUI_Shape::getHighlightColor() const
   return myHighlightColor;
 }
 
+void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
+                                         const bool     theToDisplay,
+                                         const bool     theIsUpdateViewer )
+{
+  myTextureFileName = theFileName;
+  updateShape( theToDisplay, theIsUpdateViewer );
+}
+
+QString HYDROGUI_Shape::getTextureFileName() const
+{
+  return myTextureFileName;
+}
+
 void HYDROGUI_Shape::buildShape()
 {
   // Erase previously created shape
   erase();
 
-  if( myFace.IsNull() )
+  if ( myTopoShape.IsNull() )
     return;
 
-  myShape = new AIS_Shape( myFace );
+  myShape = new AIS_TexturedShape( myTopoShape );
 
   if ( !myObject.IsNull() )
     myShape->SetOwner( myObject );
 
   myShape->SetTransparency( 0 );
-  myShape->SetDisplayMode( AIS_Shaded );
+  myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
+
+  QString aTextureFileName = getTextureFileName();
+  if( !aTextureFileName.isEmpty() )
+  {
+    myShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
+    myShape->SetTextureMapOn();
+    myShape->DisableTextureModulate();
+  }
 
-  // Init default params for shape
+    // Init default params for shape
   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
   if ( !anAttributes.IsNull() )
   {
-    Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
-    if ( !anIsoAspect.IsNull() )
-      anIsoAspect->SetNumber( 0 );
-    
-    anIsoAspect = anAttributes->VIsoAspect();
-    if ( !anIsoAspect.IsNull() )
-      anIsoAspect->SetNumber( 0 );
-
-    Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
-    if ( !aShadingAspect.IsNull() )
+    if ( myDisplayMode == AIS_Shaded )
     {
-      Graphic3d_MaterialAspect aMatAspect;
-      aMatAspect.SetAmbient( 1 );
-      aMatAspect.SetDiffuse( 0 );
-
-      aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
-      aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
+      Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
+      if ( !anIsoAspect.IsNull() )
+        anIsoAspect->SetNumber( 0 );
+      
+      anIsoAspect = anAttributes->VIsoAspect();
+      if ( !anIsoAspect.IsNull() )
+        anIsoAspect->SetNumber( 0 );
+
+      Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
+      if ( !aShadingAspect.IsNull() )
+      {
+        Graphic3d_MaterialAspect aMatAspect;
+        aMatAspect.SetAmbient( 1 );
+        aMatAspect.SetDiffuse( 0 );
+
+        aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
+        aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
+      }
+    }
+    else if ( myDisplayMode == AIS_WireFrame )
+    {
+      anAttributes->SetWireDraw( true );
     }
   }
 }
@@ -268,24 +416,30 @@ void HYDROGUI_Shape::updateShape( const bool theToDisplay,
   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
   if ( !anAttributes.IsNull() )
   {
-    // Coloring face filling
-    Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
-    if ( !aShadingAspect.IsNull() )
+    if ( myDisplayMode == AIS_Shaded )
+    {
+      // Coloring face filling
+      Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
+      if ( !aShadingAspect.IsNull() )
+      {
+        Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
+                                      getQuantityColorVal( myFillingColor.green() ),
+                                      getQuantityColorVal( myFillingColor.blue() ),
+                                      Quantity_TOC_RGB );
+
+        aShadingAspect->SetColor( aFillingColor );
+        aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
+      }
+    }
+    else if ( myDisplayMode == AIS_WireFrame )
     {
-      Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
-                                    getQuantityColorVal( myFillingColor.green() ),
-                                    getQuantityColorVal( myFillingColor.blue() ),
-                                    Quantity_TOC_RGB );
-
-      aShadingAspect->SetColor( aFillingColor );
-      aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
     }
 
     // Coloring borders
     colorShapeBorder( getActiveColor() );
   }
 
-  if ( !theToDisplay || myContext.IsNull() )
+  if ( !theToDisplay || !isVisible() || myContext.IsNull() )
     return;
   
   myContext->Display( myShape, theIsUpdateViewer );
@@ -310,25 +464,75 @@ void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
   if ( anAttributes.IsNull() )
     return;
 
-  if ( theColor.alpha() == 0 )
+  Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
+                               getQuantityColorVal( theColor.green() ),
+                               getQuantityColorVal( theColor.blue() ),
+                               Quantity_TOC_RGB );
+  if ( myDisplayMode == AIS_Shaded )
   {
-    anAttributes->SetFaceBoundaryDraw( false );
+    if ( theColor.alpha() == 0 )
+    {
+      anAttributes->SetFaceBoundaryDraw( false );
+    }
+    else
+    {
+      anAttributes->SetFaceBoundaryDraw( true );
+
+      Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
+      if ( !aBoundaryAspect.IsNull() )
+        aBoundaryAspect->SetColor( aBorderColor );
+    }
   }
-  else
+  else if ( myDisplayMode == AIS_WireFrame )
   {
-    Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
-                                 getQuantityColorVal( theColor.green() ),
-                                 getQuantityColorVal( theColor.blue() ),
-                                 Quantity_TOC_RGB );
+    myShape->SetColor( aBorderColor );
+  }
+}
 
-    anAttributes->SetFaceBoundaryDraw( true );
+QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
+{
+  QString aResult;
+  if( !theImageObj.IsNull() )
+  {
+    QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
 
-    Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
-    if ( !aBoundaryAspect.IsNull() )
-    {
-      aBoundaryAspect->SetColor( aBorderColor );
-    }
+    int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
+    QString aPrefix = QString( "image_%1" ).arg( aStudyId );
+
+    QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
+    anEntry.replace( ':', '_' );
+
+    QString anExtension = "bmp";
+
+    aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
   }
+  return aResult;
 }
 
+void HYDROGUI_Shape::removeTextureFile() const
+{
+  QFile aFile( getTextureFileName() );
+  if( aFile.exists() )
+    aFile.remove();
+}
 
+QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
+{
+  double aSizeLimit = (double)theSizeLimit;
+  double aWidth = (double)theImage.width();
+  double aHeight = (double)theImage.height();
+  if( aWidth > aSizeLimit || aHeight > aSizeLimit )
+  {
+    if( aWidth > aHeight )
+    {
+      aHeight /= ( aWidth / aSizeLimit );
+      aWidth = aSizeLimit;
+    }
+    else
+    {
+      aWidth /= ( aHeight / aSizeLimit );
+      aHeight = aSizeLimit;
+    }
+  }
+  return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
+}