Salome HOME
7f891922ce71ed235ff56c2de38143bdea297d17
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1
2 #include <TestViewer.h>
3 #include <OCCViewer_ViewManager.h>
4 #include <OCCViewer_ViewModel.h>
5 #include <OCCViewer_ViewWindow.h>
6 #include <AIS_InteractiveContext.hxx>
7 #include <AIS_Shape.hxx>
8 #include <TopoDS_Iterator.hxx>
9 #include <QDir>
10 #include <QPainter>
11 #include <cppunit/TestAssert.h>
12
13 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
14 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
15
16 OCCViewer_ViewManager* TestViewer::viewManager()
17 {
18   if( myViewManager )
19     return myViewManager;
20
21   myViewManager = new OCCViewer_ViewManager( 0, 0 );
22   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
23
24   aViewer->setTrihedronSize( 100, true );
25   aViewer->setInteractionStyle( 0 );
26   aViewer->setZoomingStyle( 1 );
27
28   myViewManager->setViewModel( aViewer );
29   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
30
31   return myViewManager;
32 }
33
34 OCCViewer_Viewer* TestViewer::viewer()
35 {
36   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
37 }
38
39 OCCViewer_ViewWindow* TestViewer::viewWindow()
40 {
41   viewManager(); //to create the view if it was not created earlier
42   return myViewWindow;
43 }
44
45 Handle(AIS_InteractiveContext) context()
46 {
47   return TestViewer::viewer()->getAISContext();
48 }
49
50 void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
51 {
52   QColor aColor = theColor;
53   if( !aColor.isValid() )
54   {
55     // random color
56     int aHue = rand()%255;
57     aColor = QColor::fromHsl( aHue, 255, 128 );
58   }
59
60   double r = aColor.red() / 255.0;
61   double g = aColor.green() / 255.0;
62   double b = aColor.blue() / 255.0;
63
64   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
65   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
66   aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
67   context()->Display( aShape, theMode, 0, Standard_False );
68 }
69
70 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
71 {
72   context()->EraseAll();
73   
74   if( theShape.ShapeType()==TopAbs_COMPOUND )
75   {
76     TopoDS_Iterator anIt( theShape );
77     for( ; anIt.More(); anIt.Next() )
78       show( anIt.Value(), QColor(), theMode );
79   }
80   else
81     show( theShape, QColor(), theMode );
82
83   viewWindow()->onTopView();
84   viewWindow()->onFitAll();
85 }
86
87 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
88 {
89   if( theImage1.isNull() || theImage2.isNull() )
90     return theImage1.isNull() == theImage2.isNull();
91
92   if( theImage1.size() != theImage2.size() )
93     return false;
94
95   int aBytesCount = theImage1.byteCount();
96   const uchar *aBytes1 = theImage1.constBits();
97   const uchar *aBytes2 = theImage2.constBits();
98   int aBytesCountE = 0;
99   for( int i=0; i<aBytesCount; i++ )
100     if( aBytes1[i] != aBytes2[i] )
101       aBytesCountE++;
102
103   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
104     return false;
105
106   return true;
107 }
108
109 bool TestViewer::AssertEqual( const QString& theUseCaseName )
110 {
111   QImage anActualImage = viewWindow()->dumpView();
112
113   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
114   anExpectedRefFilePath += "/" + theUseCaseName + ".png";
115   QImage anExpectedRefImage; 
116   anExpectedRefImage.load( anExpectedRefFilePath );
117
118   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.2 ) )
119     return true;
120
121   QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
122   anActualImage.save( aPath );
123   std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
124   
125   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
126   QPainter aPainter( &aDiff );
127   aPainter.drawImage( 0, 0, anExpectedRefImage );
128   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
129   aPainter.drawImage( 0, 0, anActualImage );
130
131   QString aDiffFilePath = QDir::tempPath() + "/" + theUseCaseName + "_diff.png";
132   aDiff.save( aDiffFilePath );
133   CPPUNIT_FAIL( aMessage.c_str() );
134
135   return false;
136 }