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