Salome HOME
refs #662: the tests for land cover presentations
[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 #include <OCCViewer_ViewPort3d.h>
7 #ifdef WIN32
8   #pragma warning ( disable: 4251 )
9 #endif
10 #include <OCCViewer_ViewModel.h>
11 #ifdef WIN32
12   #pragma warning ( disable: 4251 )
13 #endif
14 #include <OCCViewer_ViewWindow.h>
15 #ifdef WIN32
16   #pragma warning ( disable: 4251 )
17 #endif
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_Shape.hxx>
20 #include <Aspect_ColorScale.hxx>
21 #include <TopoDS_Iterator.hxx>
22
23 #include <QDir>
24 #include <QPainter>
25 #include <QHash>
26
27 #ifdef WIN32
28   #pragma warning ( default: 4251 )
29 #endif
30
31 #include <cppunit/TestAssert.h>
32
33 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
34 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
35 QString TestViewer::myKey = "";
36
37 OCCViewer_ViewManager* TestViewer::viewManager()
38 {
39   if( myViewManager )
40     return myViewManager;
41
42   myViewManager = new OCCViewer_ViewManager( 0, 0 );
43   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
44
45   aViewer->setTrihedronSize( 100, true );
46   aViewer->setInteractionStyle( 0 );
47   aViewer->setZoomingStyle( 1 );
48
49   myViewManager->setViewModel( aViewer );
50   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
51
52   return myViewManager;
53 }
54
55 OCCViewer_Viewer* TestViewer::viewer()
56 {
57   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
58 }
59
60 OCCViewer_ViewWindow* TestViewer::viewWindow()
61 {
62   viewManager(); //to create the view if it was not created earlier
63   return myViewWindow;
64 }
65
66 Handle(AIS_InteractiveContext) context()
67 {
68   return TestViewer::viewer()->getAISContext();
69 }
70
71 QColor randomColor()
72 {
73   int r = test_rand();
74   int aHue = r%255;
75   //std::cout << "hue: " << aHue << std::endl;
76   QColor aColor = QColor::fromHsl( aHue, 255, 128 );
77   return aColor;
78 }
79
80 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
81                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
82 {
83   context()->EraseAll( Standard_False );
84   context()->Display( theObject, theMode, theSelectionMode );
85
86   myKey = theKey;
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 QColor& theColor )
96 {
97   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
98   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
99   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
100   context()->Display( aShape, theMode, 0, Standard_False );
101
102   if( isFitAll )
103   {
104     viewWindow()->onTopView();
105     viewWindow()->onFitAll();
106   }
107 }
108
109 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
110 {
111   context()->EraseAll( Standard_False );
112   
113   myKey = theKey;
114   test_srand( 0 );
115   if( theShape.ShapeType()==TopAbs_COMPOUND )
116   {
117     TopoDS_Iterator anIt( theShape );
118     for( ; anIt.More(); anIt.Next() )
119       show( anIt.Value(), theMode, false, randomColor() );
120   }
121   else
122     show( theShape, theMode, false, randomColor() );
123
124   if( isFitAll )
125   {
126     viewWindow()->onTopView();
127     viewWindow()->onFitAll();
128   }
129 }
130
131 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
132 {
133   if( theImage1.isNull() || theImage2.isNull() )
134     return theImage1.isNull() == theImage2.isNull();
135
136   if( theImage1.size() != theImage2.size() )
137     return false;
138
139   int aBytesCount = theImage1.byteCount();
140   const uchar *aBytes1 = theImage1.constBits();
141   const uchar *aBytes2 = theImage2.constBits();
142   int aBytesCountE = 0;
143   for( int i=0; i<aBytesCount; i++ )
144     if( aBytes1[i] != aBytes2[i] )
145       aBytesCountE++;
146
147   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
148     return false;
149
150   return true;
151 }
152
153 bool TestViewer::AssertImages( QString& theMessage )
154 {
155   QImage anActualImage = viewWindow()->dumpView();
156
157   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
158   anExpectedRefFilePath += "/" + myKey + ".png";
159   QImage anExpectedRefImage; 
160   anExpectedRefImage.load( anExpectedRefFilePath );
161
162   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
163   {
164     theMessage = "";
165     return true;
166   }
167
168   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
169   anActualImage.save( aPath );
170   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
171   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
172   
173   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
174   QPainter aPainter( &aDiff );
175   aPainter.drawImage( 0, 0, anExpectedRefImage );
176   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
177   aPainter.drawImage( 0, 0, anActualImage );
178
179   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
180   aDiff.save( aDiffFilePath );
181
182   return false;
183 }
184
185 Handle_Aspect_ColorScale TestViewer::showColorScale()
186 {
187   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
188   if( aView.IsNull() )
189     return Handle(Aspect_ColorScale)();
190
191   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
192   if( aColorScale.IsNull() )
193     return aColorScale;
194
195   Standard_Real anXPos = 0.05; //TODO
196   Standard_Real anYPos = 0.1; //TODO
197   Standard_Real aWidth = 0.2; //TODO
198   Standard_Real aHeight = 0.5; //TODO
199   Standard_Integer aTextHeight = 14; //TODO
200   Standard_Integer aNbIntervals = 30; //TODO
201
202   aColorScale->SetXPosition( anXPos );
203   aColorScale->SetYPosition( anYPos );
204   aColorScale->SetWidth( aWidth );
205   aColorScale->SetHeight( aHeight );
206   aColorScale->SetTextHeight( aTextHeight );
207   aColorScale->SetNumberOfIntervals( aNbIntervals );
208
209   aColorScale->SetTitle( "test" );
210   aColorScale->SetRange( 0, 1 );
211
212   if( !aView->ColorScaleIsDisplayed() )
213     aView->ColorScaleDisplay();
214
215   return aColorScale;
216 }