2 #include <TestViewer.h>
3 #include <OCCViewer_ViewManager.h>
4 #include <OCCViewer_ViewModel.h>
5 #include <OCCViewer_ViewWindow.h>
6 #include <AIS_InteractiveContext.hxx>
7 #include <AIS_Shape.hxx>
8 #include <TopoDS_Iterator.hxx>
10 #include <cppunit/TestAssert.h>
12 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
13 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
15 OCCViewer_ViewManager* TestViewer::viewManager()
20 myViewManager = new OCCViewer_ViewManager( 0, 0 );
21 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
23 aViewer->setTrihedronSize( 100, true );
24 aViewer->setInteractionStyle( 0 );
25 aViewer->setZoomingStyle( 1 );
27 myViewManager->setViewModel( aViewer );
28 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
33 OCCViewer_Viewer* TestViewer::viewer()
35 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
38 OCCViewer_ViewWindow* TestViewer::viewWindow()
40 viewManager(); //to create the view if it was not created earlier
44 Handle(AIS_InteractiveContext) context()
46 return TestViewer::viewer()->getAISContext();
49 void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
51 QColor aColor = theColor;
52 if( !aColor.isValid() )
55 int aHue = rand()%255;
56 aColor = QColor::fromHsl( aHue, 255, 128 );
59 double r = aColor.red() / 255.0;
60 double g = aColor.green() / 255.0;
61 double b = aColor.blue() / 255.0;
63 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
64 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
65 aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
66 context()->Display( aShape, theMode, 0, Standard_False );
69 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
71 context()->EraseAll();
73 if( theShape.ShapeType()==TopAbs_COMPOUND )
75 TopoDS_Iterator anIt( theShape );
76 for( ; anIt.More(); anIt.Next() )
77 show( anIt.Value(), QColor(), theMode );
80 show( theShape, QColor(), theMode );
82 viewWindow()->onTopView();
83 viewWindow()->onFitAll();
86 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance = 0.0 )
88 if( theImage1.isNull() || theImage2.isNull() )
89 return theImage1.isNull() == theImage2.isNull();
91 if( theImage1.size() != theImage2.size() )
94 int aBytesCount = theImage1.byteCount();
95 const uchar *aBytes1 = theImage1.constBits();
96 const uchar *aBytes2 = theImage2.constBits();
97 for( int i=0; i<aBytesCount; i++ )
98 if( aBytes1[i] != aBytes2[i] )
104 bool TestViewer::AssertEqual( const QString& theUseCaseName )
106 QImage anActualImage = viewWindow()->dumpView();
108 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
109 anExpectedRefFilePath += "/" + theUseCaseName + ".png";
110 QImage anExpectedRefImage;
111 anExpectedRefImage.load( anExpectedRefFilePath );
113 if( AreImagesEqual( anActualImage, anExpectedRefImage ) )
116 QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
117 anActualImage.save( aPath );
118 std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
119 CPPUNIT_FAIL( aMessage.c_str() );