]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDRO_tests/TestViewer.cxx
Salome HOME
tests on Linux
[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   //std::cout << "hue: " << aHue << std::endl;
73   QColor aColor = QColor::fromHsl( aHue, 255, 128 );
74   return aColor;
75 }
76
77 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
78 {
79   double r = theColor.red() / 255.0;
80   double g = theColor.green() / 255.0;
81   double b = theColor.blue() / 255.0;
82
83   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
84   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
85   aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
86   context()->Display( aShape, theMode, 0, Standard_False );
87
88   if( isFitAll )
89   {
90     viewWindow()->onTopView();
91     viewWindow()->onFitAll();
92   }
93 }
94
95 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
96 {
97   context()->EraseAll();
98   
99   myKey = theKey;
100   test_srand( 0 );
101   if( theShape.ShapeType()==TopAbs_COMPOUND )
102   {
103     TopoDS_Iterator anIt( theShape );
104     for( ; anIt.More(); anIt.Next() )
105       show( anIt.Value(), theMode, false, randomColor() );
106   }
107   else
108     show( theShape, theMode, false, randomColor() );
109
110   if( isFitAll )
111   {
112     viewWindow()->onTopView();
113     viewWindow()->onFitAll();
114   }
115 }
116
117 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
118 {
119   if( theImage1.isNull() || theImage2.isNull() )
120     return theImage1.isNull() == theImage2.isNull();
121
122   if( theImage1.size() != theImage2.size() )
123     return false;
124
125   int aBytesCount = theImage1.byteCount();
126   const uchar *aBytes1 = theImage1.constBits();
127   const uchar *aBytes2 = theImage2.constBits();
128   int aBytesCountE = 0;
129   for( int i=0; i<aBytesCount; i++ )
130     if( aBytes1[i] != aBytes2[i] )
131       aBytesCountE++;
132
133   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
134     return false;
135
136   return true;
137 }
138
139 bool TestViewer::AssertImages( QString& theMessage )
140 {
141   QImage anActualImage = viewWindow()->dumpView();
142
143   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
144   anExpectedRefFilePath += "/" + myKey + ".png";
145   QImage anExpectedRefImage; 
146   anExpectedRefImage.load( anExpectedRefFilePath );
147
148   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
149   {
150     theMessage = "";
151     return true;
152   }
153
154   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
155   anActualImage.save( aPath );
156   std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
157   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
158   
159   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
160   QPainter aPainter( &aDiff );
161   aPainter.drawImage( 0, 0, anExpectedRefImage );
162   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
163   aPainter.drawImage( 0, 0, anActualImage );
164
165   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
166   aDiff.save( aDiffFilePath );
167
168   return false;
169 }