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