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_ViewFrame.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>
57 #include <QApplication>
59 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
60 OCCViewer_ViewFrame* TestViewer::myViewWindow = 0;
61 QString TestViewer::myKey = "";
63 extern QString REF_DATA_PATH;
64 extern QString TMP_DIR;
66 OCCViewer_ViewManager* TestViewer::viewManager()
71 myViewManager = new OCCViewer_ViewManager( 0, 0 );
72 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
74 aViewer->setTrihedronSize( 100, true );
75 aViewer->setInteractionStyle( 0 );
76 aViewer->setZoomingStyle( 1 );
78 myViewManager->setViewModel( aViewer );
79 myViewWindow = dynamic_cast<OCCViewer_ViewFrame*>( myViewManager->createViewWindow() );
84 OCCViewer_Viewer* TestViewer::viewer()
86 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
89 OCCViewer_ViewFrame* TestViewer::viewWindow()
91 viewManager(); //to create the view if it was not created earlier
95 Handle(AIS_InteractiveContext) TestViewer::context()
97 return viewer()->getAISContext();
100 QColor TestViewer::GetColor(int i)
102 static QVector<QColor> aCV;
105 aCV << QColor(0,0,255)
108 << QColor(255,255,20)
109 << QColor(20,255,255)
110 << QColor(100,100,20)
111 << QColor(10,100,150);
117 QColor TestColor = aCV[i % aCV.size()];
118 QColor NewColor((TestColor.red() + i * 41) % 256,
119 (TestColor.green() + i * 13) % 256,
120 (TestColor.blue() + i * 23) % 256);
125 void TestViewer::eraseAll( bool isUpdate, bool eraseStructures )
127 context()->CloseLocalContext( -1, Standard_False );
128 context()->EraseAll( isUpdate );
129 if( eraseStructures )
131 Graphic3d_MapOfStructure GmapS;
132 viewer()->getViewer3d()->StructureManager()->DisplayedStructures(GmapS);
133 for (Graphic3d_MapOfStructure::Iterator it(GmapS); it.More(); it.Next())
135 Handle(Graphic3d_Structure) GS = it.Key();
139 viewer()->setTrihedronShown( false );
140 qApp->processEvents();
143 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
144 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
146 QString aNewKey = theKey;
147 if( !aNewKey.isEmpty() )
150 eraseAll( false, true );
153 if( theSelectionMode > 0 )
155 context()->OpenLocalContext();
156 context()->Display( theObject, theMode, theSelectionMode );
157 context()->Activate( theObject, theSelectionMode, Standard_True );
160 context()->Display( theObject, theMode, theSelectionMode );
166 void TestViewer::fitAll()
168 viewWindow()->onTopView();
169 viewWindow()->onFitAll();
172 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
173 int theUIANb, int theVIANb)
175 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
176 if( theShape.ShapeType()==TopAbs_VERTEX )
177 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
178 if (theShape.ShapeType()==TopAbs_FACE)
180 context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
181 context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
182 Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
183 aDrawer->UIsoAspect()->SetNumber(theUIANb);
184 aDrawer->VIsoAspect()->SetNumber(theVIANb);
186 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
187 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
188 context()->Display( aShape, theMode, 0, Standard_False );
194 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
195 int theUIANb, int theVIANb)
197 QString aNewKey = theKey;
198 if( !aNewKey.isEmpty() )
204 if( theShape.IsNull() )
208 //show all faces first
209 TopTools_ListOfShape aListOfFaces;
210 TopExp_Explorer aFE( theShape, TopAbs_FACE );
211 for( ; aFE.More(); aFE.Next() )
212 aListOfFaces.Append(aFE.Current());
213 GEOMUtils::SortShapes(aListOfFaces);
214 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
215 for( ; aLF.More(); aLF.Next(), i++)
216 show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
218 //show all independent wires
219 TopTools_ListOfShape aListOfWires;
220 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
221 for( ; aWE.More(); aWE.Next() )
222 aListOfWires.Append(aWE.Current());
223 GEOMUtils::SortShapes(aListOfWires);
224 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
225 for( ; aLW.More(); aLW.Next(), i++)
226 show( aLW.Value(), theMode, false, GetColor(i) );
228 //show all independent edges
229 TopTools_ListOfShape aListOfEdges;
230 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
231 for( ; anEE.More(); anEE.Next())
232 aListOfEdges.Append(anEE.Current());
233 GEOMUtils::SortShapes(aListOfEdges);
234 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
235 for( ; aLE.More(); aLE.Next(), i++)
236 show( aLE.Value(), theMode, false, GetColor(i) );
242 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
244 if( theShape.ShapeType()==TopAbs_SHELL )
246 TopoDS_Iterator anIt( theShape );
247 for( ; anIt.More(); anIt.Next())
249 show( anIt.Value(), theMode, false, GetColor(i) );
253 else if (theShape.ShapeType()==TopAbs_FACE ||
254 theShape.ShapeType()==TopAbs_WIRE ||
255 theShape.ShapeType()==TopAbs_EDGE ||
256 theShape.ShapeType()==TopAbs_VERTEX )
258 show( theShape, theMode, false, GetColor(i) );
263 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
265 if( theImage1.isNull() || theImage2.isNull() )
266 return theImage1.isNull() == theImage2.isNull();
268 if( theImage1.size() != theImage2.size() )
271 int aBytesCount = theImage1.byteCount();
272 const uchar *aBytes1 = theImage1.constBits();
273 const uchar *aBytes2 = theImage2.constBits();
274 int aBytesCountE = 0;
275 for( int i=0; i<aBytesCount; i++ )
276 if( aBytes1[i] != aBytes2[i] )
279 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
285 QImage TestViewer::diff( const QImage& theExpectedRefImage, const QImage& theActualImage )
287 const QImage::Format aFormat = QImage::Format_RGB32;
289 QImage anExpectedRefImage = theExpectedRefImage.convertToFormat( aFormat );
290 QImage anActualImage = theActualImage.convertToFormat( aFormat );
292 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), aFormat );
293 QPainter aPainter( &aDiff );
294 aPainter.drawImage( 0, 0, anExpectedRefImage );
295 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
296 aPainter.drawImage( 0, 0, anActualImage );
300 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase, bool swapRGB )
302 QImage anActualImage;
304 anActualImage = *theImage;
306 anActualImage = viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
310 // A temporary patch for bug in SALOME/OCC dump; the result image contains swapped RGB colors
311 anActualImage = anActualImage.rgbSwapped();
318 QString anExpectedRefFilePath = REF_DATA_PATH;
319 anExpectedRefFilePath += "/" + myKey + ".png";
320 QImage anExpectedRefImage;
321 anExpectedRefImage.load( anExpectedRefFilePath );
322 //std::cout << "Expected image loading: " << anExpectedRefFilePath.toStdString() << std::endl;
324 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
330 QString aPath = TMP_DIR + "/" + myKey + ".png";
331 anActualImage.save( aPath );
332 //std::cout << "Actual image: " << aPath.toStdString() << std::endl;
334 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
335 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
337 QImage aDiff = diff( anExpectedRefImage, anActualImage );
339 QString aDiffFilePath = TMP_DIR + "/" + myKey + "_diff.png";
340 aDiff.save( aDiffFilePath );
341 //std::cout << "Diff image: " << aDiffFilePath.toStdString() << std::endl;
343 QString anExpected = TMP_DIR + "/" + myKey + "_1.png";
344 //std::cout << "Expected image: " << anExpected.toStdString() << std::endl;
345 anExpectedRefImage.save( anExpected );
350 Handle(AIS_ColorScale) TestViewer::colorScale()
352 static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
357 void TestViewer::showColorScale( bool isShow )
359 Handle(AIS_ColorScale) aColorScale = colorScale();
360 if( aColorScale.IsNull() )
363 Standard_Real anXPos = 0.05;
364 Standard_Real anYPos = 0.1;
365 Standard_Real aWidth = 0.2;
366 Standard_Real aHeight = 0.5;
367 Standard_Integer aTextHeight = 14;
368 Standard_Integer aNbIntervals = 30;
370 aColorScale->SetXPosition( anXPos );
371 aColorScale->SetYPosition( anYPos );
372 aColorScale->SetWidth( aWidth );
373 aColorScale->SetHeight( aHeight );
374 aColorScale->SetTextHeight( aTextHeight );
375 aColorScale->SetNumberOfIntervals( aNbIntervals );
377 aColorScale->SetTitle( "test" );
378 aColorScale->SetRange( 0, 1 );
380 aColorScale->SetToUpdate();
384 if( !context()->IsDisplayed( aColorScale ) )
385 context()->Display( aColorScale );
389 if( context()->IsDisplayed( aColorScale ) )
390 context()->Erase( aColorScale );
394 bool TestViewer::ColorScaleIsDisplayed()
396 return context()->IsDisplayed( colorScale() );
399 void TestViewer::select( int theViewX, int theViewY )
401 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
402 context()->MoveTo( theViewX, theViewY, aView );
404 // context()->CloseAllContexts();
407 QString GetLine( QFile& theFile, bool isUtf8 )
409 QByteArray aLineData = theFile.readLine();
412 aLine = QString::fromUtf8( aLineData );
418 bool TestViewer::areScriptsEqual( const QString& theBaseName,
424 QString anExpectedRefFilePath = REF_DATA_PATH;
425 anExpectedRefFilePath += "/" + theBaseName;
427 QString anActualFilePath = TMP_DIR + "/" + theBaseName;
429 QFile anExpected( anExpectedRefFilePath );
430 QFile anActual( anActualFilePath );
431 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) )
433 theMsg = "Expected file cannot be opened: " + anExpectedRefFilePath;
437 if( !anActual.open( QFile::ReadOnly | QFile::Text ) )
439 theMsg = "Actual file cannot be opened: " + anActualFilePath;
443 for( int i=0; i<theLinesToOmit; i++ )
444 anExpected.readLine();
448 while( !anExpected.atEnd() && isEqual )
450 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
451 QString anActualLine = GetLine( anActual, isActualUtf8 );
452 isEqual = anExpectedLine == anActualLine;
454 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
458 while( !anActual.atEnd() && isEqual )
460 QString anActualLine = GetLine( anActual, isActualUtf8 );
461 anActualLine = anActualLine.trimmed();
462 if( !anActualLine.isEmpty() )
472 void TestViewer::setKey( const QString& theKey )