Salome HOME
refs #1341: debug of tests on 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, true );
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 temp = QDir::tempPath();
311 #ifndef WIN32
312   temp += "/hydro";
313   if( !QDir().exists( temp ) )
314     QDir().mkpath( temp );
315 #endif
316
317   QString aPath = temp + "/" + myKey + ".png";
318   anActualImage.save( aPath );
319   //std::cout << "Actual image: " << aPath.toStdString() << std::endl;
320   
321   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
322   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
323   
324   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
325   QPainter aPainter( &aDiff );
326   aPainter.drawImage( 0, 0, anExpectedRefImage );
327   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
328   aPainter.drawImage( 0, 0, anActualImage );
329
330   QString aDiffFilePath = temp + "/" + myKey + "_diff.png";
331   aDiff.save( aDiffFilePath );
332   //std::cout << "Diff image: " << aDiffFilePath.toStdString() << std::endl;
333
334   QString anExpected = temp + "/" + myKey + "_1.png";
335   //std::cout << "Expected image: " << anExpected.toStdString() << std::endl;
336   anExpectedRefImage.save( anExpected );
337
338   return false;
339 }
340
341 Handle(AIS_ColorScale) TestViewer::colorScale()
342 {
343   static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
344
345   return aColorScale;
346 }
347
348 void TestViewer::showColorScale( bool isShow )
349 {
350   Handle(AIS_ColorScale) aColorScale = colorScale();
351   if( aColorScale.IsNull() )
352     return;
353
354   Standard_Real anXPos = 0.05;
355   Standard_Real anYPos = 0.1;
356   Standard_Real aWidth = 0.2;
357   Standard_Real aHeight = 0.5;
358   Standard_Integer aTextHeight = 14;
359   Standard_Integer aNbIntervals = 30;
360
361   aColorScale->SetXPosition( anXPos );
362   aColorScale->SetYPosition( anYPos );
363   aColorScale->SetWidth( aWidth );
364   aColorScale->SetHeight( aHeight );
365   aColorScale->SetTextHeight( aTextHeight );
366   aColorScale->SetNumberOfIntervals( aNbIntervals );
367
368   aColorScale->SetTitle( "test" );
369   aColorScale->SetRange( 0, 1 );
370
371   aColorScale->SetToUpdate();
372
373   if( isShow )
374   {
375     if( !context()->IsDisplayed( aColorScale ) )
376       context()->Display( aColorScale );
377   }
378   else
379   {
380     if( context()->IsDisplayed( aColorScale ) )
381       context()->Erase( aColorScale );
382   }
383 }
384
385 bool TestViewer::ColorScaleIsDisplayed()
386 {
387   return context()->IsDisplayed( colorScale() );
388 }
389
390 void TestViewer::select( int theViewX, int theViewY )
391 {
392   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
393   context()->MoveTo( theViewX, theViewY, aView );
394   context()->Select();
395   // context()->CloseAllContexts();
396 }
397
398 QString GetLine( QFile& theFile, bool isUtf8 )
399 {
400   QByteArray aLineData = theFile.readLine();
401   QString aLine;
402   if( isUtf8 )
403     aLine = QString::fromUtf8( aLineData );
404   else
405     aLine = aLineData;
406   return aLine;
407 }
408
409 bool TestViewer::areScriptsEqual( const QString& theBaseName,
410                                   bool isExpectedUtf8,
411                                   bool isActualUtf8,
412                                   int theLinesToOmit,
413                                   QString& theMsg )
414 {
415   QString anExpectedRefFilePath = REF_DATA_PATH;
416   anExpectedRefFilePath += "/" + theBaseName;
417   
418   QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
419
420   QFile anExpected( anExpectedRefFilePath );
421   QFile anActual( anActualFilePath );
422   if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
423       !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
424     return false;
425
426   for( int i=0; i<theLinesToOmit; i++ )
427     anExpected.readLine();
428
429   bool isEqual = true;
430   int i = 1;
431   while( !anExpected.atEnd() && isEqual )
432   {
433     QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
434     QString anActualLine = GetLine( anActual, isActualUtf8 );
435     isEqual = anExpectedLine == anActualLine;
436     if( !isEqual )
437       theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
438     i++;
439   }
440   
441   if( isEqual )
442     isEqual = anActual.atEnd();
443
444   anExpected.close();
445   anActual.close();
446
447   return isEqual;
448 }
449
450 void TestViewer::setKey( const QString& theKey )
451 {
452   myKey = theKey;
453 }