2 #include <TestViewer.h>
4 #include <OCCViewer_ViewManager.h>
6 #pragma warning ( disable: 4251 )
8 #include <OCCViewer_ViewModel.h>
10 #pragma warning ( disable: 4251 )
12 #include <OCCViewer_ViewWindow.h>
14 #pragma warning ( disable: 4251 )
16 #include <AIS_InteractiveContext.hxx>
17 #include <AIS_Shape.hxx>
18 #include <TopoDS_Iterator.hxx>
25 #pragma warning ( default: 4251 )
28 #include <cppunit/TestAssert.h>
30 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
31 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
32 QString TestViewer::myKey = "";
34 OCCViewer_ViewManager* TestViewer::viewManager()
39 myViewManager = new OCCViewer_ViewManager( 0, 0 );
40 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
42 aViewer->setTrihedronSize( 100, true );
43 aViewer->setInteractionStyle( 0 );
44 aViewer->setZoomingStyle( 1 );
46 myViewManager->setViewModel( aViewer );
47 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
52 OCCViewer_Viewer* TestViewer::viewer()
54 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
57 OCCViewer_ViewWindow* TestViewer::viewWindow()
59 viewManager(); //to create the view if it was not created earlier
63 Handle(AIS_InteractiveContext) context()
65 return TestViewer::viewer()->getAISContext();
70 int aHue = rand()%255;
71 QColor aColor = QColor::fromHsl( aHue, 255, 128 );
75 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
77 double r = theColor.red() / 255.0;
78 double g = theColor.green() / 255.0;
79 double b = theColor.blue() / 255.0;
81 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
82 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
83 aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
84 context()->Display( aShape, theMode, 0, Standard_False );
88 viewWindow()->onTopView();
89 viewWindow()->onFitAll();
93 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, char* theKey )
95 context()->EraseAll();
98 test_srand( qHash( theKey ) );
99 if( theShape.ShapeType()==TopAbs_COMPOUND )
101 TopoDS_Iterator anIt( theShape );
102 for( ; anIt.More(); anIt.Next() )
103 show( anIt.Value(), theMode, false, randomColor() );
106 show( theShape, theMode, false, randomColor() );
110 viewWindow()->onTopView();
111 viewWindow()->onFitAll();
115 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
117 if( theImage1.isNull() || theImage2.isNull() )
118 return theImage1.isNull() == theImage2.isNull();
120 if( theImage1.size() != theImage2.size() )
123 int aBytesCount = theImage1.byteCount();
124 const uchar *aBytes1 = theImage1.constBits();
125 const uchar *aBytes2 = theImage2.constBits();
126 int aBytesCountE = 0;
127 for( int i=0; i<aBytesCount; i++ )
128 if( aBytes1[i] != aBytes2[i] )
131 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
137 bool TestViewer::AssertImages( QString& theMessage )
139 QImage anActualImage = viewWindow()->dumpView();
141 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
142 anExpectedRefFilePath += "/" + myKey + ".png";
143 QImage anExpectedRefImage;
144 anExpectedRefImage.load( anExpectedRefFilePath );
146 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
152 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
153 anActualImage.save( aPath );
154 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
156 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
157 QPainter aPainter( &aDiff );
158 aPainter.drawImage( 0, 0, anExpectedRefImage );
159 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
160 aPainter.drawImage( 0, 0, anActualImage );
162 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
163 aDiff.save( aDiffFilePath );