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