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;
65 OCCViewer_ViewManager* TestViewer::viewManager()
70 myViewManager = new OCCViewer_ViewManager( 0, 0 );
71 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
73 aViewer->setTrihedronSize( 100, true );
74 aViewer->setInteractionStyle( 0 );
75 aViewer->setZoomingStyle( 1 );
77 myViewManager->setViewModel( aViewer );
78 myViewWindow = dynamic_cast<OCCViewer_ViewFrame*>( myViewManager->createViewWindow() );
83 OCCViewer_Viewer* TestViewer::viewer()
85 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
88 OCCViewer_ViewFrame* TestViewer::viewWindow()
90 viewManager(); //to create the view if it was not created earlier
94 Handle(AIS_InteractiveContext) TestViewer::context()
96 return viewer()->getAISContext();
99 QColor TestViewer::GetColor(int i)
101 static QVector<QColor> aCV;
104 aCV << QColor(0,0,255)
107 << QColor(255,255,20)
108 << QColor(20,255,255)
109 << QColor(100,100,20)
110 << QColor(10,100,150);
116 QColor TestColor = aCV[i % aCV.size()];
117 QColor NewColor((TestColor.red() + i * 41) % 256,
118 (TestColor.green() + i * 13) % 256,
119 (TestColor.blue() + i * 23) % 256);
124 void TestViewer::eraseAll( bool isUpdate, bool eraseStructures )
126 context()->CloseLocalContext( -1, Standard_False );
127 context()->EraseAll( isUpdate );
128 if( eraseStructures )
130 Graphic3d_MapOfStructure GmapS;
131 viewer()->getViewer3d()->StructureManager()->DisplayedStructures(GmapS);
132 for (Graphic3d_MapOfStructure::Iterator it(GmapS); it.More(); it.Next())
134 Handle(Graphic3d_Structure) GS = it.Key();
138 viewer()->setTrihedronShown( false );
139 qApp->processEvents();
142 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
143 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
145 QString aNewKey = theKey;
146 if( !aNewKey.isEmpty() )
149 eraseAll( false, true );
152 if( theSelectionMode > 0 )
154 context()->OpenLocalContext();
155 context()->Display( theObject, theMode, theSelectionMode );
156 context()->Activate( theObject, theSelectionMode, Standard_True );
159 context()->Display( theObject, theMode, theSelectionMode );
165 void TestViewer::fitAll()
167 viewWindow()->onTopView();
168 viewWindow()->onFitAll();
171 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
172 int theUIANb, int theVIANb)
174 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
175 if( theShape.ShapeType()==TopAbs_VERTEX )
176 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
177 if (theShape.ShapeType()==TopAbs_FACE)
179 context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
180 context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
181 Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
182 aDrawer->UIsoAspect()->SetNumber(theUIANb);
183 aDrawer->VIsoAspect()->SetNumber(theVIANb);
185 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
186 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
187 context()->Display( aShape, theMode, 0, Standard_False );
193 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
194 int theUIANb, int theVIANb)
196 QString aNewKey = theKey;
197 if( !aNewKey.isEmpty() )
203 if( theShape.IsNull() )
207 //show all faces first
208 TopTools_ListOfShape aListOfFaces;
209 TopExp_Explorer aFE( theShape, TopAbs_FACE );
210 for( ; aFE.More(); aFE.Next() )
211 aListOfFaces.Append(aFE.Current());
212 GEOMUtils::SortShapes(aListOfFaces);
213 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
214 for( ; aLF.More(); aLF.Next(), i++)
215 show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
217 //show all independent wires
218 TopTools_ListOfShape aListOfWires;
219 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
220 for( ; aWE.More(); aWE.Next() )
221 aListOfWires.Append(aWE.Current());
222 GEOMUtils::SortShapes(aListOfWires);
223 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
224 for( ; aLW.More(); aLW.Next(), i++)
225 show( aLW.Value(), theMode, false, GetColor(i) );
227 //show all independent edges
228 TopTools_ListOfShape aListOfEdges;
229 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
230 for( ; anEE.More(); anEE.Next())
231 aListOfEdges.Append(anEE.Current());
232 GEOMUtils::SortShapes(aListOfEdges);
233 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
234 for( ; aLE.More(); aLE.Next(), i++)
235 show( aLE.Value(), theMode, false, GetColor(i) );
241 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
243 if( theShape.ShapeType()==TopAbs_SHELL )
245 TopoDS_Iterator anIt( theShape );
246 for( ; anIt.More(); anIt.Next())
248 show( anIt.Value(), theMode, false, GetColor(i) );
252 else if (theShape.ShapeType()==TopAbs_FACE ||
253 theShape.ShapeType()==TopAbs_WIRE ||
254 theShape.ShapeType()==TopAbs_EDGE ||
255 theShape.ShapeType()==TopAbs_VERTEX )
257 show( theShape, theMode, false, GetColor(i) );
262 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
264 if( theImage1.isNull() || theImage2.isNull() )
265 return theImage1.isNull() == theImage2.isNull();
267 if( theImage1.size() != theImage2.size() )
270 int aBytesCount = theImage1.byteCount();
271 const uchar *aBytes1 = theImage1.constBits();
272 const uchar *aBytes2 = theImage2.constBits();
273 int aBytesCountE = 0;
274 for( int i=0; i<aBytesCount; i++ )
275 if( aBytes1[i] != aBytes2[i] )
278 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
284 QImage TestViewer::diff( const QImage& theExpectedRefImage, const QImage& theActualImage )
286 const QImage::Format aFormat = QImage::Format_RGB32;
288 QImage anExpectedRefImage = theExpectedRefImage.convertToFormat( aFormat );
289 QImage anActualImage = theActualImage.convertToFormat( aFormat );
291 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), aFormat );
292 QPainter aPainter( &aDiff );
293 aPainter.drawImage( 0, 0, anExpectedRefImage );
294 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
295 aPainter.drawImage( 0, 0, anActualImage );
299 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase, bool swapRGB )
301 QImage anActualImage;
303 anActualImage = *theImage;
305 anActualImage = viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
309 // A temporary patch for bug in SALOME/OCC dump; the result image contains swapped RGB colors
310 anActualImage = anActualImage.rgbSwapped();
317 QString anExpectedRefFilePath = REF_DATA_PATH;
318 anExpectedRefFilePath += "/" + myKey + ".png";
319 QImage anExpectedRefImage;
320 anExpectedRefImage.load( anExpectedRefFilePath );
321 //std::cout << "Expected image loading: " << anExpectedRefFilePath.toStdString() << std::endl;
323 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
329 QString temp = QDir::tempPath();
332 if( !QDir().exists( temp ) )
333 QDir().mkpath( temp );
336 QString aPath = temp + "/" + myKey + ".png";
337 anActualImage.save( aPath );
338 //std::cout << "Actual image: " << aPath.toStdString() << std::endl;
340 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
341 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
343 QImage aDiff = diff( anExpectedRefImage, anActualImage );
345 QString aDiffFilePath = temp + "/" + myKey + "_diff.png";
346 aDiff.save( aDiffFilePath );
347 //std::cout << "Diff image: " << aDiffFilePath.toStdString() << std::endl;
349 QString anExpected = temp + "/" + myKey + "_1.png";
350 //std::cout << "Expected image: " << anExpected.toStdString() << std::endl;
351 anExpectedRefImage.save( anExpected );
356 Handle(AIS_ColorScale) TestViewer::colorScale()
358 static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
363 void TestViewer::showColorScale( bool isShow )
365 Handle(AIS_ColorScale) aColorScale = colorScale();
366 if( aColorScale.IsNull() )
369 Standard_Real anXPos = 0.05;
370 Standard_Real anYPos = 0.1;
371 Standard_Real aWidth = 0.2;
372 Standard_Real aHeight = 0.5;
373 Standard_Integer aTextHeight = 14;
374 Standard_Integer aNbIntervals = 30;
376 aColorScale->SetXPosition( anXPos );
377 aColorScale->SetYPosition( anYPos );
378 aColorScale->SetWidth( aWidth );
379 aColorScale->SetHeight( aHeight );
380 aColorScale->SetTextHeight( aTextHeight );
381 aColorScale->SetNumberOfIntervals( aNbIntervals );
383 aColorScale->SetTitle( "test" );
384 aColorScale->SetRange( 0, 1 );
386 aColorScale->SetToUpdate();
390 if( !context()->IsDisplayed( aColorScale ) )
391 context()->Display( aColorScale );
395 if( context()->IsDisplayed( aColorScale ) )
396 context()->Erase( aColorScale );
400 bool TestViewer::ColorScaleIsDisplayed()
402 return context()->IsDisplayed( colorScale() );
405 void TestViewer::select( int theViewX, int theViewY )
407 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
408 context()->MoveTo( theViewX, theViewY, aView );
410 // context()->CloseAllContexts();
413 QString GetLine( QFile& theFile, bool isUtf8 )
415 QByteArray aLineData = theFile.readLine();
418 aLine = QString::fromUtf8( aLineData );
424 bool TestViewer::areScriptsEqual( const QString& theBaseName,
430 QString anExpectedRefFilePath = REF_DATA_PATH;
431 anExpectedRefFilePath += "/" + theBaseName;
434 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
436 QString anActualFilePath = QDir::tempPath() + "/hydro/" + theBaseName;
439 QFile anExpected( anExpectedRefFilePath );
440 QFile anActual( anActualFilePath );
441 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
442 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
445 for( int i=0; i<theLinesToOmit; i++ )
446 anExpected.readLine();
450 while( !anExpected.atEnd() && isEqual )
452 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
453 QString anActualLine = GetLine( anActual, isActualUtf8 );
454 isEqual = anExpectedLine == anActualLine;
456 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
460 while( !anActual.atEnd() && isEqual )
462 QString anActualLine = GetLine( anActual, isActualUtf8 );
463 anActualLine = anActualLine.trimmed();
464 if( !anActualLine.isEmpty() )
474 void TestViewer::setKey( const QString& theKey )