0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / AIS / AIS_TexturedShape.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 2fc46b4..a764719
@@ -1,30 +1,21 @@
 // Created on: 2001-07-02
 // Created by: Mathias BOSSHARD
-// Copyright (c) 2001-2012 OPEN CASCADE SAS
+// Copyright (c) 2001-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
-// Modified:
-////////////////////////////////////////////////////////////////////////
+#include <AIS_TexturedShape.hxx>
 
-
-#include <AIS_TexturedShape.ixx>
-#include <Standard_ErrorHandler.hxx>
-
-#include <AIS_Drawer.hxx>
+#include <AIS_GraphicTool.hxx>
 #include <AIS_InteractiveContext.hxx>
 #include <BRepTools.hxx>
 #include <gp_Pnt2d.hxx>
 #include <Graphic3d_Group.hxx>
 #include <Graphic3d_StructureManager.hxx>
 #include <Graphic3d_Texture2Dmanual.hxx>
+#include <Message.hxx>
+#include <Message_Messenger.hxx>
 #include <Precision.hxx>
+#include <Prs3d_Drawer.hxx>
 #include <Prs3d_Presentation.hxx>
 #include <Prs3d_Root.hxx>
+#include <Prs3d_LineAspect.hxx>
 #include <Prs3d_ShadingAspect.hxx>
 #include <PrsMgr_PresentationManager3d.hxx>
+#include <Standard_ErrorHandler.hxx>
+#include <Prs3d_BndBox.hxx>
 #include <StdPrs_ShadedShape.hxx>
-#include <StdPrs_WFDeflectionShape.hxx>
+#include <StdPrs_ToolTriangulatedShape.hxx>
 #include <StdPrs_WFShape.hxx>
 #include <TopExp_Explorer.hxx>
 
+
+IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape,AIS_Shape)
+
 //=======================================================================
 //function : AIS_TexturedShape
 //purpose  :
 //=======================================================================
 AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
-: AIS_Shape (theShape),
-  myPredefTexture (Graphic3d_NameOfTexture2D(0)),
-  myTextureFile (""),
-  DoRepeat (Standard_True),
-  myURepeat (1.0),
-  myVRepeat (1.0),
-  DoMapTexture (Standard_True),
-  DoSetTextureOrigin (Standard_True),
-  myUOrigin (0.0),
-  myVOrigin (0.0),
-  DoSetTextureScale (Standard_True),
-  myScaleU (1.0),
-  myScaleV (1.0),
-  DoShowTriangles (Standard_False),
-  myModulate (Standard_True)
+: AIS_Shape         (theShape),
+  myPredefTexture   (Graphic3d_NameOfTexture2D(0)),
+  myToMapTexture    (Standard_True),
+  myModulate        (Standard_True),
+  myIsCustomOrigin  (Standard_True),
+  myToRepeat        (Standard_True),
+  myToScale         (Standard_True),
+  myToShowTriangles (Standard_False)
 {
 }
 
@@ -71,17 +64,20 @@ AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
 //=======================================================================
 void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
 {
+  myTexturePixMap.Nullify();
+
   if (theTextureFileName.IsIntegerValue())
   {
-    if (theTextureFileName.IntegerValue() < Graphic3d_Texture2D::NumberOfTextures()
-     && theTextureFileName.IntegerValue() >= 0)
+    const Standard_Integer aValue = theTextureFileName.IntegerValue();
+    if (aValue < Graphic3d_Texture2D::NumberOfTextures()
+     && aValue >= 0)
     {
-      myPredefTexture = Graphic3d_NameOfTexture2D (theTextureFileName.IntegerValue());
+      myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
     }
     else
     {
-      std::cout << "Texture " << theTextureFileName << " doesn't exist \n";
-      std::cout << "Using Texture 0 instead ...\n";
+      Message::SendFail (TCollection_AsciiString ("Error: texture with ID ") + theTextureFileName
+                       + " is undefined. Texture 0 will be used instead.");
       myPredefTexture = Graphic3d_NameOfTexture2D (0);
     }
     myTextureFile = "";
@@ -89,22 +85,32 @@ void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTe
   else
   {
     myTextureFile   = theTextureFileName;
-    myPredefTexture = Graphic3d_NameOfTexture2D (-1);
+    myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
   }
 }
 
+//=======================================================================
+//function : SetTexturePixMap
+//purpose  :
+//=======================================================================
+void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
+{
+  myTextureFile = "";
+  myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
+  myTexturePixMap = theTexturePixMap;
+}
+
 //=======================================================================
 //function : SetTextureRepeat
 //purpose  :
 //=======================================================================
 
-void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN,
+void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
                                           const Standard_Real    theURepeat,
                                           const Standard_Real    theVRepeat)
 {
-  DoRepeat  = theRepeatYN;
-  myURepeat = theURepeat;
-  myVRepeat = theVRepeat;
+  myToRepeat = theToRepeat;
+  myUVRepeat.SetCoord (theURepeat, theVRepeat);
 }
 
 //=======================================================================
@@ -114,7 +120,7 @@ void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theRepeatYN,
 
 void AIS_TexturedShape::SetTextureMapOn()
 {
-  DoMapTexture = Standard_True;
+  myToMapTexture = Standard_True;
 }
 
 //=======================================================================
@@ -124,7 +130,7 @@ void AIS_TexturedShape::SetTextureMapOn()
 
 void AIS_TexturedShape::SetTextureMapOff()
 {
-  DoMapTexture = Standard_False;
+  myToMapTexture = Standard_False;
 }
 
 //=======================================================================
@@ -132,13 +138,12 @@ void AIS_TexturedShape::SetTextureMapOff()
 //purpose  :
 //=======================================================================
 
-void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOriginYN,
+void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
                                           const Standard_Real    theUOrigin,
                                           const Standard_Real    theVOrigin)
 {
-  DoSetTextureOrigin = toSetTextureOriginYN;
-  myUOrigin = theUOrigin;
-  myVOrigin = theVOrigin;
+  myIsCustomOrigin = theToSetTextureOrigin;
+  myUVOrigin.SetCoord (theUOrigin, theVOrigin);
 }
 
 //=======================================================================
@@ -146,13 +151,12 @@ void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean toSetTextureOri
 //purpose  :
 //=======================================================================
 
-void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScaleYN,
+void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
                                          const Standard_Real    theScaleU,
                                          const Standard_Real    theScaleV)
 {
-  DoSetTextureScale = toSetTextureScaleYN;
-  myScaleU = theScaleU;
-  myScaleV = theScaleV;
+  myToScale = theToSetTextureScale;
+  myUVScale.SetCoord (theScaleU, theScaleV);
 }
 
 //=======================================================================
@@ -160,9 +164,9 @@ void AIS_TexturedShape::SetTextureScale (const Standard_Boolean toSetTextureScal
 //purpose  :
 //=======================================================================
 
-void AIS_TexturedShape::ShowTriangles (const Standard_Boolean toShowTrianglesYN)
+void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
 {
-  DoShowTriangles = toShowTrianglesYN;
+  myToShowTriangles = theToShowTriangles;
 }
 
 //=======================================================================
@@ -177,7 +181,7 @@ void AIS_TexturedShape::EnableTextureModulate()
 
 //=======================================================================
 //function : DisableTextureModulate
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 void AIS_TexturedShape::DisableTextureModulate()
@@ -185,6 +189,67 @@ void AIS_TexturedShape::DisableTextureModulate()
   myModulate = Standard_False;
 }
 
+//=======================================================================
+//function : SetColor
+//purpose  :
+//=======================================================================
+
+void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
+{
+  AIS_Shape::SetColor (theColor);
+
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
+  {
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
+  }
+}
+
+//=======================================================================
+//function : UnsetColor
+//purpose  :
+//=======================================================================
+
+void AIS_TexturedShape::UnsetColor()
+{
+  AIS_Shape::UnsetColor();
+}
+
+//=======================================================================
+//function : SetMaterial
+//purpose  : 
+//=======================================================================
+
+void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
+{
+  AIS_Shape::SetMaterial (theMat);
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
+  {
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
+  }
+}
+
+//=======================================================================
+//function : UnsetMaterial
+//purpose  : 
+//=======================================================================
+void AIS_TexturedShape::UnsetMaterial()
+{
+  AIS_Shape::UnsetMaterial();
+  for (PrsMgr_Presentations::Iterator aPrsIter (Presentations()); aPrsIter.More(); aPrsIter.Next())
+  {
+    if (aPrsIter.Value()->Mode() == 3)
+    {
+      updateAttributes (aPrsIter.Value());
+    }
+  }
+}
+
 //=======================================================================
 //function : UpdateAttributes
 //purpose  :
@@ -192,36 +257,89 @@ void AIS_TexturedShape::DisableTextureModulate()
 
 void AIS_TexturedShape::UpdateAttributes()
 {
-  Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager();
-  Prs3d_ShadingAspect aDummy;
-  myAspect = aDummy.Aspect();
-  Handle(Prs3d_Presentation) aPrs = Presentation();
-  if (!DoMapTexture)
+  updateAttributes (Presentation());
+}
+
+//=======================================================================
+//function : updateAttributes
+//purpose  :
+//=======================================================================
+
+void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
+{
+  myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
+  if (HasPolygonOffsets())
   {
-    myAspect->SetTextureMapOff();
-    return;
+    Standard_Integer aMode;
+    Standard_ShortReal aFactor, aUnits;
+    PolygonOffsets (aMode, aFactor, aUnits);
+    myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
   }
 
-  if (myPredefTexture != -1)
-    mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myPredefTexture);
-  else
-    mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myTextureFile.ToCString());
+  Standard_Boolean hasTexture = Standard_False;
+  if (myToMapTexture)
+  {
+    TCollection_AsciiString aTextureDesc;
+    if (!myTexturePixMap.IsNull())
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
+      aTextureDesc = " (custom image)";
+    }
+    else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
+      aTextureDesc = TCollection_AsciiString(" (predefined texture ") + myTexture->GetId() + ")";
+    }
+    else
+    {
+      myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
+      aTextureDesc = TCollection_AsciiString(" (") + myTextureFile + ")";
+    }
 
-  myAspect->SetTextureMapOn();
+    if (myModulate)
+    {
+      myTexture->EnableModulate();
+    }
+    else
+    {
+      myTexture->DisableModulate();
+    }
 
-  myAspect->SetTextureMap (mytexture);
-  if (!mytexture->IsDone())
+    if (myTexture->IsDone())
+    {
+      hasTexture = Standard_True;
+    }
+    else
+    {
+      Message::SendFail (TCollection_AsciiString ("Error: texture can not be loaded ") + aTextureDesc);
+    }
+  }
+
+  myAspect->SetTextureMap (myTexture);
+  if (hasTexture)
   {
-    std::cout << "An error occured while building texture \n";
-    return;
+    myAspect->SetTextureMapOn();
+  }
+  else
+  {
+    myAspect->SetTextureMapOff();
   }
 
-  if (DoShowTriangles)
+  if (myToShowTriangles)
+  {
     myAspect->SetEdgeOn();
+  }
   else
+  {
     myAspect->SetEdgeOff();
+  }
 
-  Prs3d_Root::CurrentGroup (aPrs)->SetGroupPrimitivesAspect (myAspect);
+  // Go through all groups to change fill aspect for all primitives
+  for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
+  {
+    const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
+    aGroup->SetGroupPrimitivesAspect (myAspect);
+  }
 }
 
 //=======================================================================
@@ -229,12 +347,10 @@ void AIS_TexturedShape::UpdateAttributes()
 //purpose  :
 //=======================================================================
 
-void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresManager*/,
+void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
                                  const Handle(Prs3d_Presentation)&           thePrs,
                                  const Standard_Integer                      theMode)
 {
-  thePrs->Clear();
-
   if (myshape.IsNull())
   {
     return;
@@ -262,202 +378,64 @@ void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*t
 
   switch (theMode)
   {
-    case 0: // Wireframe
+    case AIS_WireFrame:
     {
-      StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+      StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
+      StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
       break;
     }
-    case 1: // Shading
+    case AIS_Shaded:
+    case 3: // texture mapping on triangulation
     {
-      Standard_Real prevangle;
-      Standard_Real newangle;
-      Standard_Real prevcoeff;
-      Standard_Real newcoeff;
-
-      if (OwnDeviationAngle (newangle, prevangle) || OwnDeviationCoefficient (newcoeff, prevcoeff))
-      {
-        if (Abs (newangle - prevangle) > Precision::Angular() || Abs (newcoeff - prevcoeff) > Precision::Confusion())
-        {
-          BRepTools::Clean (myshape);
-        }
-      }
+      StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
       if (myshape.ShapeType() > TopAbs_FACE)
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
         break;
       }
-      myDrawer->SetShadingAspectGlobal (Standard_False);
+
       if (IsInfinite())
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
         break;
       }
       try
       {
         OCC_CATCH_SIGNALS
-        StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
+        if (theMode == AIS_Shaded)
+        {
+          StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
+        }
+        else
+        {
+          StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
+                                   Standard_True,
+                                   myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
+                                   myUVRepeat,
+                                   myToScale        ? myUVScale  : gp_Pnt2d (1.0, 1.0));
+          updateAttributes (thePrs);
+        }
       }
-      catch (Standard_Failure)
+      catch (Standard_Failure const&)
       {
+#ifdef OCCT_DEBUG
         std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
+#endif
         StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
       }
       break;
     }
-
     case 2: // Bounding box
     {
       if (IsInfinite())
       {
-        StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
+        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
       }
       else
       {
-        AIS_Shape::DisplayBox (thePrs, BoundingBox(), myDrawer);
-      }
-      break;
-    }
-
-    case 3: // texture mapping on triangulation
-    {
-      BRepTools::Clean (myshape);
-      BRepTools::Update (myshape);
-
-      Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager();
-      {
-        Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect();
-        myAspect = aPrs3d_ShadingAspect->Aspect();
-
-        // Issue 23115: copy polygon offset settings passed through myDrawer
-        if (HasPolygonOffsets())
-        {
-          Standard_Integer aMode;
-          Standard_ShortReal aFactor, aUnits;
-          PolygonOffsets(aMode, aFactor, aUnits);
-          myAspect->SetPolygonOffsets(aMode, aFactor, aUnits);
-        }
-      }
-      if (!DoMapTexture)
-      {
-        myAspect->SetTextureMapOff();
-        return;
-      }
-      myAspect->SetTextureMapOn();
-
-      if (myPredefTexture != -1)
-        mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myPredefTexture);
-      else
-        mytexture = new Graphic3d_Texture2Dmanual (aStrucMana, myTextureFile.ToCString());
-
-      if (!mytexture->IsDone())
-      {
-        std::cout << "An error occured while building texture \n";
-        return;
-      }
-
-      if (myModulate)
-        mytexture->EnableModulate();
-      else
-        mytexture->DisableModulate();
-
-      myAspect->SetTextureMap (mytexture);
-      if (DoShowTriangles)
-        myAspect->SetEdgeOn();
-      else
-        myAspect->SetEdgeOff();
-
-      if (DoRepeat)
-        mytexture->EnableRepeat();
-      else
-        mytexture->DisableRepeat();
-
-      const gp_Pnt2d aUVOrigin (myUOrigin, myVOrigin);
-      const gp_Pnt2d aUVRepeat (myURepeat, myVRepeat);
-      const gp_Pnt2d aUVScale  (myScaleU,  myScaleV);
-      try
-      {
-        OCC_CATCH_SIGNALS
-        StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
-                                 Standard_True, aUVOrigin, aUVRepeat, aUVScale);
-        // within primitive arrays - object should be in one group of primitives
-        Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
-      }
-      catch (Standard_Failure)
-      {
-        std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
-        StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
+        Prs3d_BndBox::Add (thePrs, BoundingBox(), myDrawer);
       }
       break;
     }
   }
 }
-
-Standard_Boolean AIS_TexturedShape::TextureMapState() const
-{
-  return DoMapTexture;
-}
-
-Standard_Real AIS_TexturedShape::URepeat() const
-{
-  return myURepeat;
-}
-
-Standard_Boolean AIS_TexturedShape::TextureRepeat() const
-{
-  return DoRepeat;
-}
-
-Standard_Real AIS_TexturedShape::Deflection() const
-{
-  return myDeflection;
-}
-
-Standard_CString AIS_TexturedShape::TextureFile() const
-{
-  return myTextureFile.ToCString();
-}
-
-Standard_Real AIS_TexturedShape::VRepeat() const
-{
-  return myVRepeat;
-}
-
-Standard_Boolean AIS_TexturedShape::ShowTriangles() const
-{
-  return DoShowTriangles;
-}
-
-Standard_Real AIS_TexturedShape::TextureUOrigin() const
-{
-  return myUOrigin;
-}
-
-Standard_Real AIS_TexturedShape::TextureVOrigin() const
-{
-  return myVOrigin;
-}
-
-Standard_Real AIS_TexturedShape::TextureScaleU() const
-{
-  return myScaleU;
-}
-
-Standard_Real AIS_TexturedShape::TextureScaleV() const
-{
-  return myScaleV;
-}
-
-Standard_Boolean AIS_TexturedShape::TextureScale() const
-{
-  return DoSetTextureScale;
-}
-
-Standard_Boolean AIS_TexturedShape::TextureOrigin() const
-{
-  return DoSetTextureOrigin;
-}
-
-Standard_Boolean AIS_TexturedShape::TextureModulate() const
-{
-  return myModulate;
-}