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