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