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