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