2 #include <TestViewer.h>
3 #include <HYDROData_Tool.h>
5 #include <OCCViewer_ViewManager.h>
7 #pragma warning ( disable: 4251 )
9 #include <OCCViewer_ViewModel.h>
11 #pragma warning ( disable: 4251 )
13 #include <OCCViewer_ViewWindow.h>
15 #pragma warning ( disable: 4251 )
17 #include <AIS_InteractiveContext.hxx>
18 #include <AIS_Shape.hxx>
19 #include <TopoDS_Iterator.hxx>
26 #pragma warning ( default: 4251 )
29 #include <cppunit/TestAssert.h>
31 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
32 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
33 QString TestViewer::myKey = "";
35 OCCViewer_ViewManager* TestViewer::viewManager()
40 myViewManager = new OCCViewer_ViewManager( 0, 0 );
41 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
43 aViewer->setTrihedronSize( 100, true );
44 aViewer->setInteractionStyle( 0 );
45 aViewer->setZoomingStyle( 1 );
47 myViewManager->setViewModel( aViewer );
48 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
53 OCCViewer_Viewer* TestViewer::viewer()
55 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
58 OCCViewer_ViewWindow* TestViewer::viewWindow()
60 viewManager(); //to create the view if it was not created earlier
64 Handle(AIS_InteractiveContext) context()
66 return TestViewer::viewer()->getAISContext();
73 //std::cout << "hue: " << aHue << std::endl;
74 QColor aColor = QColor::fromHsl( aHue, 255, 128 );
78 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
79 int theMode, int theSelectionMode, bool isFitAll )
81 context()->EraseAll( Standard_False );
82 context()->Display( theObject, theMode, theSelectionMode );
86 viewWindow()->onTopView();
87 viewWindow()->onFitAll();
91 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
93 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
94 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
95 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
96 context()->Display( aShape, theMode, 0, Standard_False );
100 viewWindow()->onTopView();
101 viewWindow()->onFitAll();
105 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
107 context()->EraseAll( Standard_False );
111 if( theShape.ShapeType()==TopAbs_COMPOUND )
113 TopoDS_Iterator anIt( theShape );
114 for( ; anIt.More(); anIt.Next() )
115 show( anIt.Value(), theMode, false, randomColor() );
118 show( theShape, theMode, false, randomColor() );
122 viewWindow()->onTopView();
123 viewWindow()->onFitAll();
127 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
129 if( theImage1.isNull() || theImage2.isNull() )
130 return theImage1.isNull() == theImage2.isNull();
132 if( theImage1.size() != theImage2.size() )
135 int aBytesCount = theImage1.byteCount();
136 const uchar *aBytes1 = theImage1.constBits();
137 const uchar *aBytes2 = theImage2.constBits();
138 int aBytesCountE = 0;
139 for( int i=0; i<aBytesCount; i++ )
140 if( aBytes1[i] != aBytes2[i] )
143 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
149 bool TestViewer::AssertImages( QString& theMessage )
151 QImage anActualImage = viewWindow()->dumpView();
153 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
154 anExpectedRefFilePath += "/" + myKey + ".png";
155 QImage anExpectedRefImage;
156 anExpectedRefImage.load( anExpectedRefFilePath );
158 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
164 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
165 anActualImage.save( aPath );
166 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
167 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
169 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
170 QPainter aPainter( &aDiff );
171 aPainter.drawImage( 0, 0, anExpectedRefImage );
172 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
173 aPainter.drawImage( 0, 0, anActualImage );
175 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
176 aDiff.save( aDiffFilePath );