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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <TestViewer.h>
20 #include <HYDROData_Tool.h>
21 #include <OCCViewer_ViewManager.h>
23 #pragma warning ( disable: 4251 )
25 #include <OCCViewer_ViewPort3d.h>
27 #pragma warning ( disable: 4251 )
29 #include <OCCViewer_ViewModel.h>
31 #pragma warning ( disable: 4251 )
33 #include <OCCViewer_ViewWindow.h>
35 #pragma warning ( disable: 4251 )
37 #include <AIS_Shape.hxx>
38 #include <AIS_ColorScale.hxx>
39 #include <Prs3d_PointAspect.hxx>
40 #include <TopoDS_Iterator.hxx>
44 #include <TopExp_Explorer.hxx>
46 #include <Prs3d_IsoAspect.hxx>
48 #include <GEOMUtils.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #pragma warning ( default: 4251 )
56 #include <cppunit/TestAssert.h>
58 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
59 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
60 QString TestViewer::myKey = "";
62 OCCViewer_ViewManager* TestViewer::viewManager()
67 myViewManager = new OCCViewer_ViewManager( 0, 0 );
68 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
70 aViewer->setTrihedronSize( 100, true );
71 aViewer->setInteractionStyle( 0 );
72 aViewer->setZoomingStyle( 1 );
74 myViewManager->setViewModel( aViewer );
75 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
80 OCCViewer_Viewer* TestViewer::viewer()
82 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
85 OCCViewer_ViewWindow* TestViewer::viewWindow()
87 viewManager(); //to create the view if it was not created earlier
91 Handle(AIS_InteractiveContext) TestViewer::context()
93 return viewer()->getAISContext();
96 QColor TestViewer::GetColor(int i)
98 static QVector<QColor> aCV;
101 aCV << QColor(0,0,255)
104 << QColor(255,255,20)
105 << QColor(20,255,255)
106 << QColor(100,100,20)
107 << QColor(10,100,150);
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);
121 void TestViewer::eraseAll( bool isUpdate )
123 context()->CloseLocalContext( -1, Standard_False );
124 context()->EraseAll( isUpdate );
127 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
128 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
130 QString aNewKey = theKey;
131 if( !aNewKey.isEmpty() )
137 if( theSelectionMode > 0 )
139 context()->OpenLocalContext();
140 context()->Display( theObject, theMode, theSelectionMode );
141 context()->Activate( theObject, theSelectionMode, Standard_True );
144 context()->Display( theObject, theMode, theSelectionMode );
148 viewWindow()->onTopView();
149 viewWindow()->onFitAll();
153 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
154 int theUIANb, int theVIANb)
156 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
157 if( theShape.ShapeType()==TopAbs_VERTEX )
158 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
159 if (theShape.ShapeType()==TopAbs_FACE)
161 context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
162 context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
163 Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
164 aDrawer->UIsoAspect()->SetNumber(theUIANb);
165 aDrawer->VIsoAspect()->SetNumber(theVIANb);
167 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
168 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
169 context()->Display( aShape, theMode, 0, Standard_False );
173 viewWindow()->onTopView();
174 viewWindow()->onFitAll();
177 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
178 int theUIANb, int theVIANb)
180 QString aNewKey = theKey;
181 if( !aNewKey.isEmpty() )
187 if( theShape.IsNull() )
191 //show all faces first
192 TopTools_ListOfShape aListOfFaces;
193 TopExp_Explorer aFE( theShape, TopAbs_FACE );
194 for( ; aFE.More(); aFE.Next() )
195 aListOfFaces.Append(aFE.Current());
196 GEOMUtils::SortShapes(aListOfFaces);
197 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
198 for( ; aLF.More(); aLF.Next(), i++)
199 show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
201 //show all independent wires
202 TopTools_ListOfShape aListOfWires;
203 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
204 for( ; aWE.More(); aWE.Next() )
205 aListOfWires.Append(aWE.Current());
206 GEOMUtils::SortShapes(aListOfWires);
207 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
208 for( ; aLW.More(); aLW.Next(), i++)
209 show( aLW.Value(), theMode, false, GetColor(i) );
211 //show all independent edges
212 TopTools_ListOfShape aListOfEdges;
213 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
214 for( ; anEE.More(); anEE.Next())
215 aListOfEdges.Append(anEE.Current());
216 GEOMUtils::SortShapes(aListOfEdges);
217 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
218 for( ; aLE.More(); aLE.Next(), i++)
219 show( aLE.Value(), theMode, false, GetColor(i) );
223 viewWindow()->onTopView();
224 viewWindow()->onFitAll();
228 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
230 if( theShape.ShapeType()==TopAbs_SHELL )
232 TopoDS_Iterator anIt( theShape );
233 for( ; anIt.More(); anIt.Next())
235 show( anIt.Value(), theMode, false, GetColor(i) );
239 else if (theShape.ShapeType()==TopAbs_FACE ||
240 theShape.ShapeType()==TopAbs_WIRE ||
241 theShape.ShapeType()==TopAbs_EDGE ||
242 theShape.ShapeType()==TopAbs_VERTEX )
244 show( theShape, theMode, false, GetColor(i) );
249 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
251 if( theImage1.isNull() || theImage2.isNull() )
252 return theImage1.isNull() == theImage2.isNull();
254 if( theImage1.size() != theImage2.size() )
257 int aBytesCount = theImage1.byteCount();
258 const uchar *aBytes1 = theImage1.constBits();
259 const uchar *aBytes2 = theImage2.constBits();
260 int aBytesCountE = 0;
261 for( int i=0; i<aBytesCount; i++ )
262 if( aBytes1[i] != aBytes2[i] )
265 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
271 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase )
273 QImage anActualImage;
275 anActualImage = *theImage;
277 anActualImage = viewWindow()->dumpView();
279 const bool SWAP_RGB_ORDER = true;
282 // A temporary patch for bug in SALOME/OCC dump; the result image contains swapped RGB colors
283 anActualImage = anActualImage.rgbSwapped();
290 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test/HYDRO";
291 anExpectedRefFilePath += "/" + myKey + ".png";
292 QImage anExpectedRefImage;
293 anExpectedRefImage.load( anExpectedRefFilePath );
295 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
301 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
302 anActualImage.save( aPath );
303 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
304 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
306 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
307 QPainter aPainter( &aDiff );
308 aPainter.drawImage( 0, 0, anExpectedRefImage );
309 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
310 aPainter.drawImage( 0, 0, anActualImage );
312 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
313 aDiff.save( aDiffFilePath );
315 anExpectedRefImage.save( QDir::tempPath() + "/" + myKey + "_1.png");
320 Handle(AIS_ColorScale) TestViewer::colorScale()
322 static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
327 void TestViewer::showColorScale( bool isShow )
329 Handle(AIS_ColorScale) aColorScale = colorScale();
330 if( aColorScale.IsNull() )
333 Standard_Real anXPos = 0.05;
334 Standard_Real anYPos = 0.1;
335 Standard_Real aWidth = 0.2;
336 Standard_Real aHeight = 0.5;
337 Standard_Integer aTextHeight = 14;
338 Standard_Integer aNbIntervals = 30;
340 aColorScale->SetXPosition( anXPos );
341 aColorScale->SetYPosition( anYPos );
342 aColorScale->SetWidth( aWidth );
343 aColorScale->SetHeight( aHeight );
344 aColorScale->SetTextHeight( aTextHeight );
345 aColorScale->SetNumberOfIntervals( aNbIntervals );
347 aColorScale->SetTitle( "test" );
348 aColorScale->SetRange( 0, 1 );
350 aColorScale->SetToUpdate();
354 if( !context()->IsDisplayed( aColorScale ) )
355 context()->Display( aColorScale );
359 if( context()->IsDisplayed( aColorScale ) )
360 context()->Erase( aColorScale );
364 bool TestViewer::ColorScaleIsDisplayed()
366 return context()->IsDisplayed( colorScale() );
369 void TestViewer::select( int theViewX, int theViewY )
371 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
372 context()->MoveTo( theViewX, theViewY, aView );
376 QString GetLine( QFile& theFile, bool isUtf8 )
378 QByteArray aLineData = theFile.readLine();
381 aLine = QString::fromUtf8( aLineData );
387 bool TestViewer::areScriptsEqual( const QString& theBaseName,
393 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test/HYDRO";
394 anExpectedRefFilePath += "/" + theBaseName;
396 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
398 QFile anExpected( anExpectedRefFilePath );
399 QFile anActual( anActualFilePath );
400 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
401 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
404 for( int i=0; i<theLinesToOmit; i++ )
405 anExpected.readLine();
409 while( !anExpected.atEnd() && isEqual )
411 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
412 QString anActualLine = GetLine( anActual, isActualUtf8 );
413 isEqual = anExpectedLine == anActualLine;
415 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
420 isEqual = anActual.atEnd();
428 void TestViewer::setKey( const QString& theKey )