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>
58 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
59 OCCViewer_ViewFrame* TestViewer::myViewWindow = 0;
60 QString TestViewer::myKey = "";
62 extern QString REF_DATA_PATH;
64 OCCViewer_ViewManager* TestViewer::viewManager()
69 myViewManager = new OCCViewer_ViewManager( 0, 0 );
70 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
72 aViewer->setTrihedronSize( 100, true );
73 aViewer->setInteractionStyle( 0 );
74 aViewer->setZoomingStyle( 1 );
76 myViewManager->setViewModel( aViewer );
77 myViewWindow = dynamic_cast<OCCViewer_ViewFrame*>( myViewManager->createViewWindow() );
82 OCCViewer_Viewer* TestViewer::viewer()
84 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
87 OCCViewer_ViewFrame* TestViewer::viewWindow()
89 viewManager(); //to create the view if it was not created earlier
93 Handle(AIS_InteractiveContext) TestViewer::context()
95 return viewer()->getAISContext();
98 QColor TestViewer::GetColor(int i)
100 static QVector<QColor> aCV;
103 aCV << QColor(0,0,255)
106 << QColor(255,255,20)
107 << QColor(20,255,255)
108 << QColor(100,100,20)
109 << QColor(10,100,150);
115 QColor TestColor = aCV[i % aCV.size()];
116 QColor NewColor((TestColor.red() + i * 41) % 256,
117 (TestColor.green() + i * 13) % 256,
118 (TestColor.blue() + i * 23) % 256);
123 void TestViewer::eraseAll( bool isUpdate, bool eraseStructures )
125 context()->CloseLocalContext( -1, Standard_False );
126 context()->EraseAll( isUpdate );
127 if( eraseStructures )
129 Graphic3d_MapOfStructure GmapS;
130 viewer()->getViewer3d()->StructureManager()->DisplayedStructures(GmapS);
131 for (Graphic3d_MapOfStructure::Iterator it(GmapS); it.More(); it.Next())
133 Handle(Graphic3d_Structure) GS = it.Key();
139 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
140 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
142 QString aNewKey = theKey;
143 if( !aNewKey.isEmpty() )
149 if( theSelectionMode > 0 )
151 context()->OpenLocalContext();
152 context()->Display( theObject, theMode, theSelectionMode );
153 context()->Activate( theObject, theSelectionMode, Standard_True );
156 context()->Display( theObject, theMode, theSelectionMode );
162 void TestViewer::fitAll()
164 viewWindow()->onTopView();
165 viewWindow()->onFitAll();
168 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
169 int theUIANb, int theVIANb)
171 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
172 if( theShape.ShapeType()==TopAbs_VERTEX )
173 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
174 if (theShape.ShapeType()==TopAbs_FACE)
176 context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
177 context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
178 Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
179 aDrawer->UIsoAspect()->SetNumber(theUIANb);
180 aDrawer->VIsoAspect()->SetNumber(theVIANb);
182 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
183 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
184 context()->Display( aShape, theMode, 0, Standard_False );
189 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
190 int theUIANb, int theVIANb)
192 QString aNewKey = theKey;
193 if( !aNewKey.isEmpty() )
199 if( theShape.IsNull() )
203 //show all faces first
204 TopTools_ListOfShape aListOfFaces;
205 TopExp_Explorer aFE( theShape, TopAbs_FACE );
206 for( ; aFE.More(); aFE.Next() )
207 aListOfFaces.Append(aFE.Current());
208 GEOMUtils::SortShapes(aListOfFaces);
209 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
210 for( ; aLF.More(); aLF.Next(), i++)
211 show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
213 //show all independent wires
214 TopTools_ListOfShape aListOfWires;
215 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
216 for( ; aWE.More(); aWE.Next() )
217 aListOfWires.Append(aWE.Current());
218 GEOMUtils::SortShapes(aListOfWires);
219 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
220 for( ; aLW.More(); aLW.Next(), i++)
221 show( aLW.Value(), theMode, false, GetColor(i) );
223 //show all independent edges
224 TopTools_ListOfShape aListOfEdges;
225 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
226 for( ; anEE.More(); anEE.Next())
227 aListOfEdges.Append(anEE.Current());
228 GEOMUtils::SortShapes(aListOfEdges);
229 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
230 for( ; aLE.More(); aLE.Next(), i++)
231 show( aLE.Value(), theMode, false, GetColor(i) );
237 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
239 if( theShape.ShapeType()==TopAbs_SHELL )
241 TopoDS_Iterator anIt( theShape );
242 for( ; anIt.More(); anIt.Next())
244 show( anIt.Value(), theMode, false, GetColor(i) );
248 else if (theShape.ShapeType()==TopAbs_FACE ||
249 theShape.ShapeType()==TopAbs_WIRE ||
250 theShape.ShapeType()==TopAbs_EDGE ||
251 theShape.ShapeType()==TopAbs_VERTEX )
253 show( theShape, theMode, false, GetColor(i) );
258 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
260 if( theImage1.isNull() || theImage2.isNull() )
261 return theImage1.isNull() == theImage2.isNull();
263 if( theImage1.size() != theImage2.size() )
266 int aBytesCount = theImage1.byteCount();
267 const uchar *aBytes1 = theImage1.constBits();
268 const uchar *aBytes2 = theImage2.constBits();
269 int aBytesCountE = 0;
270 for( int i=0; i<aBytesCount; i++ )
271 if( aBytes1[i] != aBytes2[i] )
274 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
280 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase, bool swapRGB )
282 QImage anActualImage;
284 anActualImage = *theImage;
286 anActualImage = viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
290 // A temporary patch for bug in SALOME/OCC dump; the result image contains swapped RGB colors
291 anActualImage = anActualImage.rgbSwapped();
298 QString anExpectedRefFilePath = REF_DATA_PATH;
299 anExpectedRefFilePath += "/" + myKey + ".png";
300 QImage anExpectedRefImage;
301 anExpectedRefImage.load( anExpectedRefFilePath );
302 //std::cout << "Expected image loading: " << anExpectedRefFilePath.toStdString() << std::endl;
304 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
310 QString temp = QDir::tempPath();
313 if( !QDir().exists( temp ) )
314 QDir().mkpath( temp );
317 QString aPath = temp + "/" + myKey + ".png";
318 anActualImage.save( aPath );
319 //std::cout << "Actual image: " << aPath.toStdString() << std::endl;
321 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
322 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
324 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
325 QPainter aPainter( &aDiff );
326 aPainter.drawImage( 0, 0, anExpectedRefImage );
327 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
328 aPainter.drawImage( 0, 0, anActualImage );
330 QString aDiffFilePath = temp + "/" + myKey + "_diff.png";
331 aDiff.save( aDiffFilePath );
332 //std::cout << "Diff image: " << aDiffFilePath.toStdString() << std::endl;
334 QString anExpected = temp + "/" + myKey + "_1.png";
335 //std::cout << "Expected image: " << anExpected.toStdString() << std::endl;
336 anExpectedRefImage.save( anExpected );
341 Handle(AIS_ColorScale) TestViewer::colorScale()
343 static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
348 void TestViewer::showColorScale( bool isShow )
350 Handle(AIS_ColorScale) aColorScale = colorScale();
351 if( aColorScale.IsNull() )
354 Standard_Real anXPos = 0.05;
355 Standard_Real anYPos = 0.1;
356 Standard_Real aWidth = 0.2;
357 Standard_Real aHeight = 0.5;
358 Standard_Integer aTextHeight = 14;
359 Standard_Integer aNbIntervals = 30;
361 aColorScale->SetXPosition( anXPos );
362 aColorScale->SetYPosition( anYPos );
363 aColorScale->SetWidth( aWidth );
364 aColorScale->SetHeight( aHeight );
365 aColorScale->SetTextHeight( aTextHeight );
366 aColorScale->SetNumberOfIntervals( aNbIntervals );
368 aColorScale->SetTitle( "test" );
369 aColorScale->SetRange( 0, 1 );
371 aColorScale->SetToUpdate();
375 if( !context()->IsDisplayed( aColorScale ) )
376 context()->Display( aColorScale );
380 if( context()->IsDisplayed( aColorScale ) )
381 context()->Erase( aColorScale );
385 bool TestViewer::ColorScaleIsDisplayed()
387 return context()->IsDisplayed( colorScale() );
390 void TestViewer::select( int theViewX, int theViewY )
392 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
393 context()->MoveTo( theViewX, theViewY, aView );
395 // context()->CloseAllContexts();
398 QString GetLine( QFile& theFile, bool isUtf8 )
400 QByteArray aLineData = theFile.readLine();
403 aLine = QString::fromUtf8( aLineData );
409 bool TestViewer::areScriptsEqual( const QString& theBaseName,
415 QString anExpectedRefFilePath = REF_DATA_PATH;
416 anExpectedRefFilePath += "/" + theBaseName;
418 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
420 QFile anExpected( anExpectedRefFilePath );
421 QFile anActual( anActualFilePath );
422 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
423 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
426 for( int i=0; i<theLinesToOmit; i++ )
427 anExpected.readLine();
431 while( !anExpected.atEnd() && isEqual )
433 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
434 QString anActualLine = GetLine( anActual, isActualUtf8 );
435 isEqual = anExpectedLine == anActualLine;
437 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
442 isEqual = anActual.atEnd();
450 void TestViewer::setKey( const QString& theKey )