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