Salome HOME
debug of tests
[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 #include <TopExp_Explorer.hxx>
46 #include <TopoDS.hxx>
47 #include <Prs3d_IsoAspect.hxx>
48
49 #include <GEOMUtils.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52
53 #ifdef WIN32
54   #pragma warning ( default: 4251 )
55 #endif
56
57 #include <cppunit/TestAssert.h>
58
59 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
60 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
61 QString TestViewer::myKey = "";
62
63 OCCViewer_ViewManager* TestViewer::viewManager()
64 {
65   if( myViewManager )
66     return myViewManager;
67
68   myViewManager = new OCCViewer_ViewManager( 0, 0 );
69   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
70
71   aViewer->setTrihedronSize( 100, true );
72   aViewer->setInteractionStyle( 0 );
73   aViewer->setZoomingStyle( 1 );
74
75   myViewManager->setViewModel( aViewer );
76   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
77
78   return myViewManager;
79 }
80
81 OCCViewer_Viewer* TestViewer::viewer()
82 {
83   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
84 }
85
86 OCCViewer_ViewWindow* TestViewer::viewWindow()
87 {
88   viewManager(); //to create the view if it was not created earlier
89   return myViewWindow;
90 }
91
92 Handle(AIS_InteractiveContext) TestViewer::context()
93 {
94   return viewer()->getAISContext();
95 }
96
97 QColor TestViewer::GetColor(int i)
98 {
99   static QVector<QColor> aCV;
100   if( aCV.isEmpty() )
101   {
102     aCV  << QColor(0,0,255) 
103          << QColor(0,255,0)
104          << QColor(255,0,0)
105          << QColor(255,255,20) 
106          << QColor(20,255,255) 
107          << QColor(100,100,20) 
108          << QColor(10,100,150);
109   }
110   if (i < aCV.size())
111     return aCV[i];
112   else
113   {
114     QColor TestColor = aCV[i % aCV.size()];
115     QColor NewColor((TestColor.red() + i * 41) % 256, 
116       (TestColor.green() + i * 13) % 256, 
117       (TestColor.blue() + i * 23) % 256);
118     return NewColor;
119   }
120 }
121
122 void TestViewer::eraseAll( bool isUpdate )
123 {
124   context()->CloseLocalContext( -1, Standard_False );
125   context()->EraseAll( isUpdate );
126 }
127
128 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
129                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
130 {
131   QString aNewKey = theKey;
132   if( !aNewKey.isEmpty() )
133   {
134     myKey = aNewKey;
135     eraseAll( false );
136   }
137   
138   context()->Display( theObject, theMode, theSelectionMode );
139   if( theSelectionMode > 0 )
140   {
141     context()->OpenLocalContext();
142     context()->Activate( theObject, theSelectionMode, Standard_True );
143   }
144
145   if( isFitAll )
146   {
147     viewWindow()->onTopView();
148     viewWindow()->onFitAll();
149   }
150 }
151
152 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
153   int theUIANb, int theVIANb)
154 {
155   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
156   if( theShape.ShapeType()==TopAbs_VERTEX )
157     aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
158   if (theShape.ShapeType()==TopAbs_FACE)
159   {
160     context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
161     context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
162     Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
163     aDrawer->UIsoAspect()->SetNumber(theUIANb);
164     aDrawer->VIsoAspect()->SetNumber(theVIANb);
165   }
166   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
167   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
168   context()->Display( aShape, theMode, 0, Standard_False );
169
170   if( isFitAll )
171   {
172     viewWindow()->onTopView();
173     viewWindow()->onFitAll();
174   }
175 }
176 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
177                        int theUIANb, int theVIANb)
178 {
179   QString aNewKey = theKey;
180   if( !aNewKey.isEmpty() )
181   {
182     eraseAll( false );
183     myKey = aNewKey;
184   }
185
186   if( theShape.IsNull() )
187     return;
188
189   int i = 0;
190   //show all faces first
191   TopTools_ListOfShape aListOfFaces;
192   TopExp_Explorer aFE( theShape, TopAbs_FACE );
193   for( ; aFE.More(); aFE.Next() )
194     aListOfFaces.Append(aFE.Current());
195   GEOMUtils::SortShapes(aListOfFaces);
196   TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
197   for( ; aLF.More(); aLF.Next(), i++)
198     show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
199
200   //show all independent wires
201   TopTools_ListOfShape aListOfWires;
202   TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
203   for( ; aWE.More(); aWE.Next() )
204     aListOfWires.Append(aWE.Current());
205   GEOMUtils::SortShapes(aListOfWires);
206   TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
207   for( ; aLW.More(); aLW.Next(), i++)
208     show( aLW.Value(), theMode, false, GetColor(i) );
209
210   //show all independent edges
211   TopTools_ListOfShape aListOfEdges;
212   TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
213   for( ; anEE.More(); anEE.Next())
214     aListOfEdges.Append(anEE.Current());
215   GEOMUtils::SortShapes(aListOfEdges);
216   TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
217   for( ; aLE.More(); aLE.Next(), i++)
218     show( aLE.Value(), theMode, false, GetColor(i) );
219
220   if( isFitAll )
221   {
222     viewWindow()->onTopView();
223     viewWindow()->onFitAll();
224   }
225 }
226
227 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
228 {
229   if( theShape.ShapeType()==TopAbs_SHELL )
230   {
231     TopoDS_Iterator anIt( theShape );
232     for( ; anIt.More(); anIt.Next())
233     {
234       show( anIt.Value(), theMode, false, GetColor(i) );
235       i++;
236     }
237   }
238   else if (theShape.ShapeType()==TopAbs_FACE ||
239            theShape.ShapeType()==TopAbs_WIRE ||
240            theShape.ShapeType()==TopAbs_EDGE ||
241            theShape.ShapeType()==TopAbs_VERTEX )
242   {
243     show( theShape, theMode, false, GetColor(i) );
244     i++;
245   }
246 }*/
247
248 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
249 {
250   if( theImage1.isNull() || theImage2.isNull() )
251     return theImage1.isNull() == theImage2.isNull();
252
253   if( theImage1.size() != theImage2.size() )
254     return false;
255
256   int aBytesCount = theImage1.byteCount();
257   const uchar *aBytes1 = theImage1.constBits();
258   const uchar *aBytes2 = theImage2.constBits();
259   int aBytesCountE = 0;
260   for( int i=0; i<aBytesCount; i++ )
261     if( aBytes1[i] != aBytes2[i] )
262       aBytesCountE++;
263
264   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
265     return false;
266
267   return true;
268 }
269
270 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase )
271 {
272   QImage anActualImage;
273   if( theImage )
274     anActualImage = *theImage;
275   else
276     anActualImage = viewWindow()->dumpView();
277
278   if( theCase )
279     myKey = theCase;
280
281   QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test";
282   anExpectedRefFilePath += "/" + myKey + ".png";
283   QImage anExpectedRefImage; 
284   anExpectedRefImage.load( anExpectedRefFilePath );
285
286   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
287   {
288     theMessage = "";
289     return true;
290   }
291
292   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
293   anActualImage.save( aPath );
294   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
295   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
296   
297   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
298   QPainter aPainter( &aDiff );
299   aPainter.drawImage( 0, 0, anExpectedRefImage );
300   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
301   aPainter.drawImage( 0, 0, anActualImage );
302
303   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
304   aDiff.save( aDiffFilePath );
305
306   anExpectedRefImage.save( QDir::tempPath() + "/" + myKey + "_1.png");
307
308   return false;
309 }
310
311 Handle_Aspect_ColorScale TestViewer::colorScale()
312 {
313   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
314   if( aView.IsNull() )
315     return Handle(Aspect_ColorScale)();
316   else
317     return aView->ColorScale();
318 }
319
320 void TestViewer::showColorScale( bool isShow )
321 {
322   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
323   if( aView.IsNull() )
324     return;
325
326   Handle(Aspect_ColorScale) aColorScale = colorScale();
327   if( aColorScale.IsNull() )
328     return;
329
330   Standard_Real anXPos = 0.05;
331   Standard_Real anYPos = 0.1;
332   Standard_Real aWidth = 0.2;
333   Standard_Real aHeight = 0.5;
334   Standard_Integer aTextHeight = 14;
335   Standard_Integer aNbIntervals = 30;
336
337   aColorScale->SetXPosition( anXPos );
338   aColorScale->SetYPosition( anYPos );
339   aColorScale->SetWidth( aWidth );
340   aColorScale->SetHeight( aHeight );
341   aColorScale->SetTextHeight( aTextHeight );
342   aColorScale->SetNumberOfIntervals( aNbIntervals );
343
344   aColorScale->SetTitle( "test" );
345   aColorScale->SetRange( 0, 1 );
346
347   if( isShow )
348   {
349     if( !aView->ColorScaleIsDisplayed() )
350       aView->ColorScaleDisplay();
351   }
352   else
353   {
354     if( aView->ColorScaleIsDisplayed() )
355       aView->ColorScaleErase();
356   }
357 }
358
359 bool TestViewer::ColorScaleIsDisplayed()
360 {
361   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
362   if( aView.IsNull() )
363     return false;
364   return aView->ColorScaleIsDisplayed();
365 }
366
367 void TestViewer::select( int theViewX, int theViewY )
368 {
369   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
370   context()->MoveTo( theViewX, theViewY, aView );
371   context()->Select();
372 }
373
374 QString GetLine( QFile& theFile, bool isUtf8 )
375 {
376   QByteArray aLineData = theFile.readLine();
377   QString aLine;
378   if( isUtf8 )
379     aLine = QString::fromUtf8( aLineData );
380   else
381     aLine = aLineData;
382   return aLine;
383 }
384
385 bool TestViewer::areScriptsEqual( const QString& theBaseName,
386                                   bool isExpectedUtf8,
387                                   bool isActualUtf8,
388                                   int theLinesToOmit,
389                                   QString& theMsg )
390 {
391   QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test";
392   anExpectedRefFilePath += "/" + theBaseName;
393   
394   QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
395
396   QFile anExpected( anExpectedRefFilePath );
397   QFile anActual( anActualFilePath );
398   if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
399       !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
400     return false;
401
402   for( int i=0; i<theLinesToOmit; i++ )
403     anExpected.readLine();
404
405   bool isEqual = true;
406   int i = 1;
407   while( !anExpected.atEnd() && isEqual )
408   {
409     QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
410     QString anActualLine = GetLine( anActual, isActualUtf8 );
411     isEqual = anExpectedLine == anActualLine;
412     if( !isEqual )
413       theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
414     i++;
415   }
416   
417   if( isEqual )
418     isEqual = anActual.atEnd();
419
420   anExpected.close();
421   anActual.close();
422
423   return isEqual;
424 }