Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <TestViewer.h>
20 #include <HYDROData_Tool.h>
21 #include <OCCViewer_ViewManager.h>
22 #ifdef WIN32
23   #pragma warning ( disable: 4251 )
24 #endif
25 #include <OCCViewer_ViewPort3d.h>
26 #ifdef WIN32
27   #pragma warning ( disable: 4251 )
28 #endif
29 #include <OCCViewer_ViewModel.h>
30 #ifdef WIN32
31   #pragma warning ( disable: 4251 )
32 #endif
33 #include <OCCViewer_ViewWindow.h>
34 #ifdef WIN32
35   #pragma warning ( disable: 4251 )
36 #endif
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_Shape.hxx>
39 #include <Aspect_ColorScale.hxx>
40 #include <Prs3d_PointAspect.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <QDir>
43 #include <QPainter>
44 #include <QHash>
45
46 #ifdef WIN32
47   #pragma warning ( default: 4251 )
48 #endif
49
50 #include <cppunit/TestAssert.h>
51
52 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
53 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
54 QString TestViewer::myKey = "";
55
56 OCCViewer_ViewManager* TestViewer::viewManager()
57 {
58   if( myViewManager )
59     return myViewManager;
60
61   myViewManager = new OCCViewer_ViewManager( 0, 0 );
62   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
63
64   aViewer->setTrihedronSize( 100, true );
65   aViewer->setInteractionStyle( 0 );
66   aViewer->setZoomingStyle( 1 );
67
68   myViewManager->setViewModel( aViewer );
69   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
70
71   return myViewManager;
72 }
73
74 OCCViewer_Viewer* TestViewer::viewer()
75 {
76   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
77 }
78
79 OCCViewer_ViewWindow* TestViewer::viewWindow()
80 {
81   viewManager(); //to create the view if it was not created earlier
82   return myViewWindow;
83 }
84
85 Handle(AIS_InteractiveContext) context()
86 {
87   return TestViewer::viewer()->getAISContext();
88 }
89
90 QColor TestViewer::GetColor(int i)
91 {
92   static QVector<QColor> aCV;
93   if( aCV.isEmpty() )
94   {
95     aCV  << QColor(0,0,255) 
96          << QColor(0,255,0)
97          << QColor(255,0,0)
98          << QColor(255,255,20) 
99          << QColor(20,255,255) 
100          << QColor(100,100,20) 
101          << QColor(10,100,150);
102   }
103   if (i < aCV.size())
104     return aCV[i];
105   else
106   {
107     QColor TestColor = aCV[i % aCV.size()];
108     QColor NewColor((TestColor.red() + i * 41) % 256, 
109       (TestColor.green() + i * 13) % 256, 
110       (TestColor.blue() + i * 23) % 256);
111     return NewColor;
112   }
113 }
114
115 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
116                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
117 {
118   QString aNewKey = theKey;
119   if( !aNewKey.isEmpty() )
120   {
121     myKey = aNewKey;
122     context()->CloseLocalContext();
123     context()->EraseAll( Standard_False );
124   }
125   
126   context()->Display( theObject, theMode, theSelectionMode );
127   if( theSelectionMode > 0 )
128   {
129     context()->OpenLocalContext();
130     context()->Activate( theObject, theSelectionMode, Standard_True );
131   }
132
133   if( isFitAll )
134   {
135     viewWindow()->onTopView();
136     viewWindow()->onFitAll();
137   }
138 }
139
140 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
141 {
142   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
143   if( theShape.ShapeType()==TopAbs_VERTEX )
144     aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
145   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
146   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
147   context()->Display( aShape, theMode, 0, Standard_False );
148
149   if( isFitAll )
150   {
151     viewWindow()->onTopView();
152     viewWindow()->onFitAll();
153   }
154 }
155
156 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
157 {
158   QString aNewKey = theKey;
159   if( !aNewKey.isEmpty() )
160   {
161     context()->CloseLocalContext();
162     context()->EraseAll( Standard_False );
163     myKey = aNewKey;
164   }
165
166   if( theShape.IsNull() )
167     return;
168
169   int i = 0;
170   if( theShape.ShapeType()==TopAbs_SHELL )
171   {
172     TopoDS_Iterator anIt( theShape );
173     for( ; anIt.More(); anIt.Next(), i++ )
174       show( anIt.Value(), theMode, false, GetColor(i) );
175   }
176   else
177     show( theShape, theMode, false, GetColor(0) );
178
179   if( isFitAll )
180   {
181     viewWindow()->onTopView();
182     viewWindow()->onFitAll();
183   }
184 }
185
186 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
187 {
188   if( theImage1.isNull() || theImage2.isNull() )
189     return theImage1.isNull() == theImage2.isNull();
190
191   if( theImage1.size() != theImage2.size() )
192     return false;
193
194   int aBytesCount = theImage1.byteCount();
195   const uchar *aBytes1 = theImage1.constBits();
196   const uchar *aBytes2 = theImage2.constBits();
197   int aBytesCountE = 0;
198   for( int i=0; i<aBytesCount; i++ )
199     if( aBytes1[i] != aBytes2[i] )
200       aBytesCountE++;
201
202   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
203     return false;
204
205   return true;
206 }
207
208 bool TestViewer::AssertImages( QString& theMessage )
209 {
210   QImage anActualImage = viewWindow()->dumpView();
211
212   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
213   anExpectedRefFilePath += "/" + myKey + ".png";
214   QImage anExpectedRefImage; 
215   anExpectedRefImage.load( anExpectedRefFilePath );
216
217   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
218   {
219     theMessage = "";
220     return true;
221   }
222
223   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
224   anActualImage.save( aPath );
225   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
226   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
227   
228   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
229   QPainter aPainter( &aDiff );
230   aPainter.drawImage( 0, 0, anExpectedRefImage );
231   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
232   aPainter.drawImage( 0, 0, anActualImage );
233
234   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
235   aDiff.save( aDiffFilePath );
236
237   return false;
238 }
239
240 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
241 {
242   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
243   if( aView.IsNull() )
244     return Handle(Aspect_ColorScale)();
245
246   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
247   if( aColorScale.IsNull() )
248     return aColorScale;
249
250   Standard_Real anXPos = 0.05;
251   Standard_Real anYPos = 0.1;
252   Standard_Real aWidth = 0.2;
253   Standard_Real aHeight = 0.5;
254   Standard_Integer aTextHeight = 14;
255   Standard_Integer aNbIntervals = 30;
256
257   aColorScale->SetXPosition( anXPos );
258   aColorScale->SetYPosition( anYPos );
259   aColorScale->SetWidth( aWidth );
260   aColorScale->SetHeight( aHeight );
261   aColorScale->SetTextHeight( aTextHeight );
262   aColorScale->SetNumberOfIntervals( aNbIntervals );
263
264   aColorScale->SetTitle( "test" );
265   aColorScale->SetRange( 0, 1 );
266
267   if( isShow )
268   {
269     if( !aView->ColorScaleIsDisplayed() )
270       aView->ColorScaleDisplay();
271   }
272   else
273   {
274     if( aView->ColorScaleIsDisplayed() )
275       aView->ColorScaleErase();
276   }
277   return aColorScale;
278 }
279
280 void TestViewer::select( int theViewX, int theViewY )
281 {
282   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
283   context()->MoveTo( theViewX, theViewY, aView );
284   context()->Select();
285 }
286
287 QString GetLine( QFile& theFile, bool isUtf8 )
288 {
289   QByteArray aLineData = theFile.readLine();
290   QString aLine;
291   if( isUtf8 )
292     aLine = QString::fromUtf8( aLineData );
293   else
294     aLine = aLineData;
295   return aLine;
296 }
297
298 bool TestViewer::areScriptsEqual( const QString& theBaseName,
299                                   bool isExpectedUtf8,
300                                   bool isActualUtf8,
301                                   int theLinesToOmit,
302                                   QString& theMsg )
303 {
304   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );  
305   anExpectedRefFilePath += "/" + theBaseName;
306   
307   QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
308
309   QFile anExpected( anExpectedRefFilePath );
310   QFile anActual( anActualFilePath );
311   if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
312       !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
313     return false;
314
315   for( int i=0; i<theLinesToOmit; i++ )
316     anExpected.readLine();
317
318   bool isEqual = true;
319   int i = 1;
320   while( !anExpected.atEnd() && isEqual )
321   {
322     QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
323     QString anActualLine = GetLine( anActual, isActualUtf8 );
324     isEqual = anExpectedLine == anActualLine;
325     if( !isEqual )
326       theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
327     i++;
328   }
329   
330   if( isEqual )
331     isEqual = anActual.atEnd();
332
333   anExpected.close();
334   anActual.close();
335
336   return isEqual;
337 }