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 TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
80 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
81 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
82 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
83 context()->Display( aShape, theMode, 0, Standard_False );
87 viewWindow()->onTopView();
88 viewWindow()->onFitAll();
92 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
94 context()->EraseAll();
98 if( theShape.ShapeType()==TopAbs_COMPOUND )
100 TopoDS_Iterator anIt( theShape );
101 for( ; anIt.More(); anIt.Next() )
102 show( anIt.Value(), theMode, false, randomColor() );
105 show( theShape, theMode, false, randomColor() );
109 viewWindow()->onTopView();
110 viewWindow()->onFitAll();
114 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
116 if( theImage1.isNull() || theImage2.isNull() )
117 return theImage1.isNull() == theImage2.isNull();
119 if( theImage1.size() != theImage2.size() )
122 int aBytesCount = theImage1.byteCount();
123 const uchar *aBytes1 = theImage1.constBits();
124 const uchar *aBytes2 = theImage2.constBits();
125 int aBytesCountE = 0;
126 for( int i=0; i<aBytesCount; i++ )
127 if( aBytes1[i] != aBytes2[i] )
130 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
136 bool TestViewer::AssertImages( QString& theMessage )
138 QImage anActualImage = viewWindow()->dumpView();
140 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
141 anExpectedRefFilePath += "/" + myKey + ".png";
142 QImage anExpectedRefImage;
143 anExpectedRefImage.load( anExpectedRefFilePath );
145 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
151 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
152 anActualImage.save( aPath );
153 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
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 );