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