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