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