Salome HOME
writing difference image if they are not equal
[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 = 0.0 )
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   for( int i=0; i<aBytesCount; i++ )
99     if( aBytes1[i] != aBytes2[i] )
100       return false;
101
102   return true;
103 }
104
105 bool TestViewer::AssertEqual( const QString& theUseCaseName )
106 {
107   QImage anActualImage = viewWindow()->dumpView();
108
109   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
110   anExpectedRefFilePath += "/" + theUseCaseName + ".png";
111   QImage anExpectedRefImage; 
112   anExpectedRefImage.load( anExpectedRefFilePath );
113
114   if( AreImagesEqual( anActualImage, anExpectedRefImage ) )
115     return true;
116
117   QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
118   anActualImage.save( aPath );
119   std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
120   CPPUNIT_FAIL( aMessage.c_str() );
121
122   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
123   QPainter aPainter( &aDiff );
124   aPainter.drawImage( 0, 0, anExpectedRefImage );
125   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
126   aPainter.drawImage( 0, 0, anActualImage );
127
128   QString aDiffFilePath = QDir::tempPath() + "/" + theUseCaseName + "_diff.png";
129   aDiff.save( aPath );
130
131   return false;
132 }