Salome HOME
d02aa1f7dedd21bf163e6e5914fff27afebb36b9
[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
190 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
191                        int theUIANb, int theVIANb)
192 {
193   QString aNewKey = theKey;
194   if( !aNewKey.isEmpty() )
195   {
196     eraseAll( false );
197     myKey = aNewKey;
198   }
199
200   if( theShape.IsNull() )
201     return;
202
203   int i = 0;
204   //show all faces first
205   TopTools_ListOfShape aListOfFaces;
206   TopExp_Explorer aFE( theShape, TopAbs_FACE );
207   for( ; aFE.More(); aFE.Next() )
208     aListOfFaces.Append(aFE.Current());
209   GEOMUtils::SortShapes(aListOfFaces);
210   TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
211   for( ; aLF.More(); aLF.Next(), i++)
212     show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
213
214   //show all independent wires
215   TopTools_ListOfShape aListOfWires;
216   TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
217   for( ; aWE.More(); aWE.Next() )
218     aListOfWires.Append(aWE.Current());
219   GEOMUtils::SortShapes(aListOfWires);
220   TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
221   for( ; aLW.More(); aLW.Next(), i++)
222     show( aLW.Value(), theMode, false, GetColor(i) );
223
224   //show all independent edges
225   TopTools_ListOfShape aListOfEdges;
226   TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
227   for( ; anEE.More(); anEE.Next())
228     aListOfEdges.Append(anEE.Current());
229   GEOMUtils::SortShapes(aListOfEdges);
230   TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
231   for( ; aLE.More(); aLE.Next(), i++)
232     show( aLE.Value(), theMode, false, GetColor(i) );
233
234   if( isFitAll )
235     fitAll();
236 }
237
238 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
239 {
240   if( theShape.ShapeType()==TopAbs_SHELL )
241   {
242     TopoDS_Iterator anIt( theShape );
243     for( ; anIt.More(); anIt.Next())
244     {
245       show( anIt.Value(), theMode, false, GetColor(i) );
246       i++;
247     }
248   }
249   else if (theShape.ShapeType()==TopAbs_FACE ||
250            theShape.ShapeType()==TopAbs_WIRE ||
251            theShape.ShapeType()==TopAbs_EDGE ||
252            theShape.ShapeType()==TopAbs_VERTEX )
253   {
254     show( theShape, theMode, false, GetColor(i) );
255     i++;
256   }
257 }*/
258
259 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
260 {
261   if( theImage1.isNull() || theImage2.isNull() )
262     return theImage1.isNull() == theImage2.isNull();
263
264   if( theImage1.size() != theImage2.size() )
265     return false;
266
267   int aBytesCount = theImage1.byteCount();
268   const uchar *aBytes1 = theImage1.constBits();
269   const uchar *aBytes2 = theImage2.constBits();
270   int aBytesCountE = 0;
271   for( int i=0; i<aBytesCount; i++ )
272     if( aBytes1[i] != aBytes2[i] )
273       aBytesCountE++;
274
275   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
276     return false;
277
278   return true;
279 }
280
281 QImage TestViewer::diff( const QImage& theExpectedRefImage, const QImage& theActualImage )
282 {
283   const QImage::Format aFormat = QImage::Format_RGB32;
284
285   QImage anExpectedRefImage = theExpectedRefImage.convertToFormat( aFormat );
286   QImage anActualImage = theActualImage.convertToFormat( aFormat );
287
288   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), aFormat );
289   QPainter aPainter( &aDiff );
290   aPainter.drawImage( 0, 0, anExpectedRefImage );
291   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
292   aPainter.drawImage( 0, 0, anActualImage );
293   return aDiff;
294 }
295
296 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase, bool swapRGB )
297 {
298   QImage anActualImage;
299   if( theImage )
300     anActualImage = *theImage;
301   else
302     anActualImage = viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
303
304   if( swapRGB )
305   {
306     // A temporary patch for bug in SALOME/OCC dump; the result image contains swapped RGB colors
307     anActualImage = anActualImage.rgbSwapped();
308   }
309
310
311   if( theCase )
312     myKey = theCase;
313
314   QString anExpectedRefFilePath = REF_DATA_PATH;
315   anExpectedRefFilePath += "/" + myKey + ".png";
316   QImage anExpectedRefImage; 
317   anExpectedRefImage.load( anExpectedRefFilePath );
318   //std::cout << "Expected image loading: " << anExpectedRefFilePath.toStdString() << std::endl;
319
320   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
321   {
322     theMessage = "";
323     return true;
324   }
325
326   QString temp = QDir::tempPath();
327 #ifndef WIN32
328   temp += "/hydro";
329   if( !QDir().exists( temp ) )
330     QDir().mkpath( temp );
331 #endif
332
333   QString aPath = temp + "/" + myKey + ".png";
334   anActualImage.save( aPath );
335   //std::cout << "Actual image: " << aPath.toStdString() << std::endl;
336   
337   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
338   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
339   
340   QImage aDiff = diff( anExpectedRefImage, anActualImage );
341
342   QString aDiffFilePath = temp + "/" + myKey + "_diff.png";
343   aDiff.save( aDiffFilePath );
344   //std::cout << "Diff image: " << aDiffFilePath.toStdString() << std::endl;
345
346   QString anExpected = temp + "/" + myKey + "_1.png";
347   //std::cout << "Expected image: " << anExpected.toStdString() << std::endl;
348   anExpectedRefImage.save( anExpected );
349
350   return false;
351 }
352
353 Handle(AIS_ColorScale) TestViewer::colorScale()
354 {
355   static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
356
357   return aColorScale;
358 }
359
360 void TestViewer::showColorScale( bool isShow )
361 {
362   Handle(AIS_ColorScale) aColorScale = colorScale();
363   if( aColorScale.IsNull() )
364     return;
365
366   Standard_Real anXPos = 0.05;
367   Standard_Real anYPos = 0.1;
368   Standard_Real aWidth = 0.2;
369   Standard_Real aHeight = 0.5;
370   Standard_Integer aTextHeight = 14;
371   Standard_Integer aNbIntervals = 30;
372
373   aColorScale->SetXPosition( anXPos );
374   aColorScale->SetYPosition( anYPos );
375   aColorScale->SetWidth( aWidth );
376   aColorScale->SetHeight( aHeight );
377   aColorScale->SetTextHeight( aTextHeight );
378   aColorScale->SetNumberOfIntervals( aNbIntervals );
379
380   aColorScale->SetTitle( "test" );
381   aColorScale->SetRange( 0, 1 );
382
383   aColorScale->SetToUpdate();
384
385   if( isShow )
386   {
387     if( !context()->IsDisplayed( aColorScale ) )
388       context()->Display( aColorScale );
389   }
390   else
391   {
392     if( context()->IsDisplayed( aColorScale ) )
393       context()->Erase( aColorScale );
394   }
395 }
396
397 bool TestViewer::ColorScaleIsDisplayed()
398 {
399   return context()->IsDisplayed( colorScale() );
400 }
401
402 void TestViewer::select( int theViewX, int theViewY )
403 {
404   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
405   context()->MoveTo( theViewX, theViewY, aView );
406   context()->Select();
407   // context()->CloseAllContexts();
408 }
409
410 QString GetLine( QFile& theFile, bool isUtf8 )
411 {
412   QByteArray aLineData = theFile.readLine();
413   QString aLine;
414   if( isUtf8 )
415     aLine = QString::fromUtf8( aLineData );
416   else
417     aLine = aLineData;
418   return aLine;
419 }
420
421 bool TestViewer::areScriptsEqual( const QString& theBaseName,
422                                   bool isExpectedUtf8,
423                                   bool isActualUtf8,
424                                   int theLinesToOmit,
425                                   QString& theMsg )
426 {
427   QString anExpectedRefFilePath = REF_DATA_PATH;
428   anExpectedRefFilePath += "/" + theBaseName;
429   
430   QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
431
432   QFile anExpected( anExpectedRefFilePath );
433   QFile anActual( anActualFilePath );
434   if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
435       !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
436     return false;
437
438   for( int i=0; i<theLinesToOmit; i++ )
439     anExpected.readLine();
440
441   bool isEqual = true;
442   int i = 1;
443   while( !anExpected.atEnd() && isEqual )
444   {
445     QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
446     QString anActualLine = GetLine( anActual, isActualUtf8 );
447     isEqual = anExpectedLine == anActualLine;
448     if( !isEqual )
449       theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
450     i++;
451   }
452   
453   if( isEqual )
454     isEqual = anActual.atEnd();
455
456   anExpected.close();
457   anActual.close();
458
459   return isEqual;
460 }
461
462 void TestViewer::setKey( const QString& theKey )
463 {
464   myKey = theKey;
465 }