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