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