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>
11 #include <cppunit/TestAssert.h>
13 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
14 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
16 OCCViewer_ViewManager* TestViewer::viewManager()
21 myViewManager = new OCCViewer_ViewManager( 0, 0 );
22 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
24 aViewer->setTrihedronSize( 100, true );
25 aViewer->setInteractionStyle( 0 );
26 aViewer->setZoomingStyle( 1 );
28 myViewManager->setViewModel( aViewer );
29 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
34 OCCViewer_Viewer* TestViewer::viewer()
36 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
39 OCCViewer_ViewWindow* TestViewer::viewWindow()
41 viewManager(); //to create the view if it was not created earlier
45 Handle(AIS_InteractiveContext) context()
47 return TestViewer::viewer()->getAISContext();
50 void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
52 QColor aColor = theColor;
53 if( !aColor.isValid() )
56 int aHue = rand()%255;
57 aColor = QColor::fromHsl( aHue, 255, 128 );
60 double r = aColor.red() / 255.0;
61 double g = aColor.green() / 255.0;
62 double b = aColor.blue() / 255.0;
64 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
65 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
66 aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
67 context()->Display( aShape, theMode, 0, Standard_False );
70 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
72 context()->EraseAll();
74 if( theShape.ShapeType()==TopAbs_COMPOUND )
76 TopoDS_Iterator anIt( theShape );
77 for( ; anIt.More(); anIt.Next() )
78 show( anIt.Value(), QColor(), theMode );
81 show( theShape, QColor(), theMode );
83 viewWindow()->onTopView();
84 viewWindow()->onFitAll();
87 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
89 if( theImage1.isNull() || theImage2.isNull() )
90 return theImage1.isNull() == theImage2.isNull();
92 if( theImage1.size() != theImage2.size() )
95 int aBytesCount = theImage1.byteCount();
96 const uchar *aBytes1 = theImage1.constBits();
97 const uchar *aBytes2 = theImage2.constBits();
99 for( int i=0; i<aBytesCount; i++ )
100 if( aBytes1[i] != aBytes2[i] )
103 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
109 bool TestViewer::AssertEqual( const QString& theUseCaseName )
111 QImage anActualImage = viewWindow()->dumpView();
113 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
114 anExpectedRefFilePath += "/" + theUseCaseName + ".png";
115 QImage anExpectedRefImage;
116 anExpectedRefImage.load( anExpectedRefFilePath );
118 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.2 ) )
121 QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
122 anActualImage.save( aPath );
123 std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
125 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
126 QPainter aPainter( &aDiff );
127 aPainter.drawImage( 0, 0, anExpectedRefImage );
128 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
129 aPainter.drawImage( 0, 0, anActualImage );
131 QString aDiffFilePath = QDir::tempPath() + "/" + theUseCaseName + "_diff.png";
132 aDiff.save( aDiffFilePath );
133 CPPUNIT_FAIL( aMessage.c_str() );