Salome HOME
corrections
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
index 4fc29d343263750c2d268e89d4594fd7ae39d505..3ed66129a2aee16840ece1d89b7fd1790695b981 100644 (file)
@@ -1,14 +1,59 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <TestViewer.h>
+#include <HYDROData_Tool.h>
 #include <OCCViewer_ViewManager.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
+#include <OCCViewer_ViewPort3d.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <OCCViewer_ViewModel.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <OCCViewer_ViewWindow.h>
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_Shape.hxx>
+#include <Aspect_ColorScale.hxx>
+#include <Prs3d_PointAspect.hxx>
+#include <TopoDS_Iterator.hxx>
 #include <QDir>
+#include <QPainter>
+#include <QHash>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS.hxx>
+
+#ifdef WIN32
+  #pragma warning ( default: 4251 )
+#endif
+
+#include <cppunit/TestAssert.h>
 
 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
+QString TestViewer::myKey = "";
 
 OCCViewer_ViewManager* TestViewer::viewManager()
 {
@@ -44,21 +89,255 @@ Handle(AIS_InteractiveContext) context()
   return TestViewer::viewer()->getAISContext();
 }
 
-void TestViewer::show( const TopoDS_Shape& theShape, bool isFitAll )
+QColor TestViewer::GetColor(int i)
+{
+  static QVector<QColor> aCV;
+  if( aCV.isEmpty() )
+  {
+    aCV  << QColor(0,0,255) 
+         << QColor(0,255,0)
+         << QColor(255,0,0)
+         << QColor(255,255,20) 
+         << QColor(20,255,255) 
+         << QColor(100,100,20) 
+         << QColor(10,100,150);
+  }
+  if (i < aCV.size())
+    return aCV[i];
+  else
+  {
+    QColor TestColor = aCV[i % aCV.size()];
+    QColor NewColor((TestColor.red() + i * 41) % 256, 
+      (TestColor.green() + i * 13) % 256, 
+      (TestColor.blue() + i * 23) % 256);
+    return NewColor;
+  }
+}
+
+void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
+                       int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
+{
+  QString aNewKey = theKey;
+  if( !aNewKey.isEmpty() )
+  {
+    myKey = aNewKey;
+    context()->CloseLocalContext();
+    context()->EraseAll( Standard_False );
+  }
+  
+  context()->Display( theObject, theMode, theSelectionMode );
+  if( theSelectionMode > 0 )
+  {
+    context()->OpenLocalContext();
+    context()->Activate( theObject, theSelectionMode, Standard_True );
+  }
+
+  if( isFitAll )
+  {
+    viewWindow()->onTopView();
+    viewWindow()->onFitAll();
+  }
+}
+
+void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
 {
-  context()->EraseAll();
   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
-  context()->Display( aShape, AIS_Shaded, 0, Standard_False );
+  if( theShape.ShapeType()==TopAbs_VERTEX )
+    aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
+  aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
+  aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
+  context()->Display( aShape, theMode, 0, Standard_False );
+
+  if( isFitAll )
+  {
+    viewWindow()->onTopView();
+    viewWindow()->onFitAll();
+  }
+}
+
+void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
+{
+  QString aNewKey = theKey;
+  if( !aNewKey.isEmpty() )
+  {
+    context()->CloseLocalContext();
+    context()->EraseAll( Standard_False );
+    myKey = aNewKey;
+  }
+
+  if( theShape.IsNull() )
+    return;
+
+  int i = 0;
+  if( theShape.ShapeType()==TopAbs_COMPOUND )
+  {
+    TopExp_Explorer anExplorer( theShape, TopAbs_FACE );
+    for( ; anExplorer.More(); anExplorer.Next() ) 
+      ShowShape (anExplorer.Current(), theMode, i);
+  }
+  else
+    ShowShape(theShape, theMode, i);
+
+
+  if( isFitAll )
+  {
+    viewWindow()->onTopView();
+    viewWindow()->onFitAll();
+  }
+}
+
+void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
+{
+  if( theShape.ShapeType()==TopAbs_SHELL )
+  {
+    TopoDS_Iterator anIt( theShape );
+    for( ; anIt.More(); anIt.Next())
+    {
+      show( anIt.Value(), theMode, false, GetColor(i) );
+      i++;
+    }
+  }
+  else if (theShape.ShapeType()==TopAbs_FACE ||
+           theShape.ShapeType()==TopAbs_WIRE ||
+           theShape.ShapeType()==TopAbs_EDGE ||
+           theShape.ShapeType()==TopAbs_VERTEX )
+  {
+    show( theShape, theMode, false, GetColor(i) );
+    i++;
+  }
+}
+
+bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
+{
+  if( theImage1.isNull() || theImage2.isNull() )
+    return theImage1.isNull() == theImage2.isNull();
+
+  if( theImage1.size() != theImage2.size() )
+    return false;
+
+  int aBytesCount = theImage1.byteCount();
+  const uchar *aBytes1 = theImage1.constBits();
+  const uchar *aBytes2 = theImage2.constBits();
+  int aBytesCountE = 0;
+  for( int i=0; i<aBytesCount; i++ )
+    if( aBytes1[i] != aBytes2[i] )
+      aBytesCountE++;
 
-  viewWindow()->onTopView();
-  viewWindow()->onFitAll();
+  if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
+    return false;
+
+  return true;
 }
 
-void TestViewer::dump( const TopoDS_Shape& theShape, const QString& theName )
+bool TestViewer::AssertImages( QString& theMessage )
 {
-  show( theShape );
-  QImage anImage = viewWindow()->dumpView();
+  QImage anActualImage = viewWindow()->dumpView();
+
+  QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
+  anExpectedRefFilePath += "/" + myKey + ".png";
+  QImage anExpectedRefImage; 
+  anExpectedRefImage.load( anExpectedRefFilePath );
+
+  if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
+  {
+    theMessage = "";
+    return true;
+  }
+
+  QString aPath = QDir::tempPath() + "/" + myKey + ".png";
+  anActualImage.save( aPath );
+  //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
+  theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
+  
+  QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
+  QPainter aPainter( &aDiff );
+  aPainter.drawImage( 0, 0, anExpectedRefImage );
+  aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
+  aPainter.drawImage( 0, 0, anActualImage );
+
+  QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
+  aDiff.save( aDiffFilePath );
+
+  return false;
+}
+
+Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
+{
+  Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
+  if( aView.IsNull() )
+    return Handle(Aspect_ColorScale)();
+
+  Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
+  if( aColorScale.IsNull() )
+    return aColorScale;
+
+  Standard_Real anXPos = 0.05;
+  Standard_Real anYPos = 0.1;
+  Standard_Real aWidth = 0.2;
+  Standard_Real aHeight = 0.5;
+  Standard_Integer aTextHeight = 14;
+  Standard_Integer aNbIntervals = 30;
+
+  aColorScale->SetXPosition( anXPos );
+  aColorScale->SetYPosition( anYPos );
+  aColorScale->SetWidth( aWidth );
+  aColorScale->SetHeight( aHeight );
+  aColorScale->SetTextHeight( aTextHeight );
+  aColorScale->SetNumberOfIntervals( aNbIntervals );
+
+  aColorScale->SetTitle( "test" );
+  aColorScale->SetRange( 0, 1 );
+
+  if( isShow )
+  {
+    if( !aView->ColorScaleIsDisplayed() )
+      aView->ColorScaleDisplay();
+  }
+  else
+  {
+    if( aView->ColorScaleIsDisplayed() )
+      aView->ColorScaleErase();
+  }
+  return aColorScale;
+}
+
+void TestViewer::select( int theViewX, int theViewY )
+{
+  Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
+  context()->MoveTo( theViewX, theViewY, aView );
+  context()->Select();
+}
+
+bool TestViewer::areScriptsEqual( const QString& theBaseName )
+{
+  QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );  
+  anExpectedRefFilePath += "/" + theBaseName;
+  
+  QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
+
+  QFile anExpected( anExpectedRefFilePath );
+  QFile anActual( anActualFilePath );
+  if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
+      !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
+    return false;
+
+  const int aLinesToOmit = 20;
+  for( int i=0; i<aLinesToOmit; i++ )
+    anExpected.readLine();
+
+  bool isEqual = true;
+  while( !anExpected.atEnd() && isEqual )
+  {
+    QString anExpectedLine = QString::fromUtf8( anExpected.readLine() );
+    QString anActualLine = anActual.readLine();
+    isEqual = anExpectedLine == anActualLine;
+  }
+
+  if( isEqual )
+    isEqual = anActual.atEnd();
+
+  anExpected.close();
+  anActual.close();
 
-  QString aPath = QDir::tempPath() + "/" + theName + ".png";
-  anImage.save( aPath );
+  return isEqual;
 }