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