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