Salome HOME
Merge remote-tracking branch 'origin/BR_LCM_COMP' into BR_LAND_COVER_MAP
[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 #ifdef WIN32
49   #pragma warning ( default: 4251 )
50 #endif
51
52 #include <cppunit/TestAssert.h>
53
54 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
55 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
56 QString TestViewer::myKey = "";
57
58 OCCViewer_ViewManager* TestViewer::viewManager()
59 {
60   if( myViewManager )
61     return myViewManager;
62
63   myViewManager = new OCCViewer_ViewManager( 0, 0 );
64   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
65
66   aViewer->setTrihedronSize( 100, true );
67   aViewer->setInteractionStyle( 0 );
68   aViewer->setZoomingStyle( 1 );
69
70   myViewManager->setViewModel( aViewer );
71   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
72
73   return myViewManager;
74 }
75
76 OCCViewer_Viewer* TestViewer::viewer()
77 {
78   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
79 }
80
81 OCCViewer_ViewWindow* TestViewer::viewWindow()
82 {
83   viewManager(); //to create the view if it was not created earlier
84   return myViewWindow;
85 }
86
87 Handle(AIS_InteractiveContext) context()
88 {
89   return TestViewer::viewer()->getAISContext();
90 }
91
92 QColor TestViewer::GetColor(int i)
93 {
94   static QVector<QColor> aCV;
95   if( aCV.isEmpty() )
96   {
97     aCV  << QColor(0,0,255) 
98          << QColor(0,255,0)
99          << QColor(255,0,0)
100          << QColor(255,255,20) 
101          << QColor(20,255,255) 
102          << QColor(100,100,20) 
103          << QColor(10,100,150);
104   }
105   if (i < aCV.size())
106     return aCV[i];
107   else
108   {
109     QColor TestColor = aCV[i % aCV.size()];
110     QColor NewColor((TestColor.red() + i * 41) % 256, 
111       (TestColor.green() + i * 13) % 256, 
112       (TestColor.blue() + i * 23) % 256);
113     return NewColor;
114   }
115 }
116
117 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
118                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
119 {
120   QString aNewKey = theKey;
121   if( !aNewKey.isEmpty() )
122   {
123     myKey = aNewKey;
124     context()->CloseLocalContext();
125     context()->EraseAll( Standard_False );
126   }
127   
128   context()->Display( theObject, theMode, theSelectionMode );
129   if( theSelectionMode > 0 )
130   {
131     context()->OpenLocalContext();
132     context()->Activate( theObject, theSelectionMode, Standard_True );
133   }
134
135   if( isFitAll )
136   {
137     viewWindow()->onTopView();
138     viewWindow()->onFitAll();
139   }
140 }
141
142 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
143 {
144   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
145   if( theShape.ShapeType()==TopAbs_VERTEX )
146     aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
147   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
148   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
149   context()->Display( aShape, theMode, 0, Standard_False );
150
151   if( isFitAll )
152   {
153     viewWindow()->onTopView();
154     viewWindow()->onFitAll();
155   }
156 }
157
158 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
159 {
160   QString aNewKey = theKey;
161   if( !aNewKey.isEmpty() )
162   {
163     context()->CloseLocalContext();
164     context()->EraseAll( Standard_False );
165     myKey = aNewKey;
166   }
167
168   if( theShape.IsNull() )
169     return;
170
171   int i = 0;
172   //show all faces first
173   TopExp_Explorer aFE( theShape, TopAbs_FACE );
174   for( ; aFE.More(); aFE.Next(), i++ ) 
175     show( aFE.Current(), theMode, false, GetColor(i) );
176
177   //show all independent wires
178   TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
179   for( ; aWE.More(); aWE.Next(), i++ ) 
180     show( aWE.Current(), theMode, false, GetColor(i) );
181
182   //show all independent edges
183   TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
184   for( ; anEE.More(); anEE.Next(), i++ ) 
185     show( anEE.Current(), theMode, false, GetColor(i) );
186
187   if( isFitAll )
188   {
189     viewWindow()->onTopView();
190     viewWindow()->onFitAll();
191   }
192 }
193
194 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
195 {
196   if( theShape.ShapeType()==TopAbs_SHELL )
197   {
198     TopoDS_Iterator anIt( theShape );
199     for( ; anIt.More(); anIt.Next())
200     {
201       show( anIt.Value(), theMode, false, GetColor(i) );
202       i++;
203     }
204   }
205   else if (theShape.ShapeType()==TopAbs_FACE ||
206            theShape.ShapeType()==TopAbs_WIRE ||
207            theShape.ShapeType()==TopAbs_EDGE ||
208            theShape.ShapeType()==TopAbs_VERTEX )
209   {
210     show( theShape, theMode, false, GetColor(i) );
211     i++;
212   }
213 }*/
214
215 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
216 {
217   if( theImage1.isNull() || theImage2.isNull() )
218     return theImage1.isNull() == theImage2.isNull();
219
220   if( theImage1.size() != theImage2.size() )
221     return false;
222
223   int aBytesCount = theImage1.byteCount();
224   const uchar *aBytes1 = theImage1.constBits();
225   const uchar *aBytes2 = theImage2.constBits();
226   int aBytesCountE = 0;
227   for( int i=0; i<aBytesCount; i++ )
228     if( aBytes1[i] != aBytes2[i] )
229       aBytesCountE++;
230
231   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
232     return false;
233
234   return true;
235 }
236
237 bool TestViewer::AssertImages( QString& theMessage )
238 {
239   QImage anActualImage = viewWindow()->dumpView();
240
241   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
242   anExpectedRefFilePath += "/" + myKey + ".png";
243   QImage anExpectedRefImage; 
244   anExpectedRefImage.load( anExpectedRefFilePath );
245
246   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
247   {
248     theMessage = "";
249     return true;
250   }
251
252   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
253   anActualImage.save( aPath );
254   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
255   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
256   
257   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
258   QPainter aPainter( &aDiff );
259   aPainter.drawImage( 0, 0, anExpectedRefImage );
260   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
261   aPainter.drawImage( 0, 0, anActualImage );
262
263   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
264   aDiff.save( aDiffFilePath );
265
266   return false;
267 }
268
269 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
270 {
271   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
272   if( aView.IsNull() )
273     return Handle(Aspect_ColorScale)();
274
275   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
276   if( aColorScale.IsNull() )
277     return aColorScale;
278
279   Standard_Real anXPos = 0.05;
280   Standard_Real anYPos = 0.1;
281   Standard_Real aWidth = 0.2;
282   Standard_Real aHeight = 0.5;
283   Standard_Integer aTextHeight = 14;
284   Standard_Integer aNbIntervals = 30;
285
286   aColorScale->SetXPosition( anXPos );
287   aColorScale->SetYPosition( anYPos );
288   aColorScale->SetWidth( aWidth );
289   aColorScale->SetHeight( aHeight );
290   aColorScale->SetTextHeight( aTextHeight );
291   aColorScale->SetNumberOfIntervals( aNbIntervals );
292
293   aColorScale->SetTitle( "test" );
294   aColorScale->SetRange( 0, 1 );
295
296   if( isShow )
297   {
298     if( !aView->ColorScaleIsDisplayed() )
299       aView->ColorScaleDisplay();
300   }
301   else
302   {
303     if( aView->ColorScaleIsDisplayed() )
304       aView->ColorScaleErase();
305   }
306   return aColorScale;
307 }
308
309 void TestViewer::select( int theViewX, int theViewY )
310 {
311   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
312   context()->MoveTo( theViewX, theViewY, aView );
313   context()->Select();
314 }
315
316 QString GetLine( QFile& theFile, bool isUtf8 )
317 {
318   QByteArray aLineData = theFile.readLine();
319   QString aLine;
320   if( isUtf8 )
321     aLine = QString::fromUtf8( aLineData );
322   else
323     aLine = aLineData;
324   return aLine;
325 }
326
327 bool TestViewer::areScriptsEqual( const QString& theBaseName,
328                                   bool isExpectedUtf8,
329                                   bool isActualUtf8,
330                                   int theLinesToOmit,
331                                   QString& theMsg )
332 {
333   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );  
334   anExpectedRefFilePath += "/" + theBaseName;
335   
336   QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
337
338   QFile anExpected( anExpectedRefFilePath );
339   QFile anActual( anActualFilePath );
340   if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
341       !anActual.open  ( QFile::ReadOnly | QFile::Text ) )
342     return false;
343
344   for( int i=0; i<theLinesToOmit; i++ )
345     anExpected.readLine();
346
347   bool isEqual = true;
348   int i = 1;
349   while( !anExpected.atEnd() && isEqual )
350   {
351     QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
352     QString anActualLine = GetLine( anActual, isActualUtf8 );
353     isEqual = anExpectedLine == anActualLine;
354     if( !isEqual )
355       theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
356     i++;
357   }
358   
359   if( isEqual )
360     isEqual = anActual.atEnd();
361
362   anExpected.close();
363   anActual.close();
364
365   return isEqual;
366 }