2 #include <TestViewer.h>
3 #include <HYDROData_Tool.h>
4 #include <OCCViewer_ViewManager.h>
5 #include <OCCViewer_ViewPort3d.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 <Aspect_ColorScale.hxx>
20 #include <TopoDS_Iterator.hxx>
27 #pragma warning ( default: 4251 )
30 #include <cppunit/TestAssert.h>
32 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
33 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
34 QString TestViewer::myKey = "";
36 OCCViewer_ViewManager* TestViewer::viewManager()
41 myViewManager = new OCCViewer_ViewManager( 0, 0 );
42 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
44 aViewer->setTrihedronSize( 100, true );
45 aViewer->setInteractionStyle( 0 );
46 aViewer->setZoomingStyle( 1 );
48 myViewManager->setViewModel( aViewer );
49 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
54 OCCViewer_Viewer* TestViewer::viewer()
56 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
59 OCCViewer_ViewWindow* TestViewer::viewWindow()
61 viewManager(); //to create the view if it was not created earlier
65 Handle(AIS_InteractiveContext) context()
67 return TestViewer::viewer()->getAISContext();
70 QColor TestViewer::GetColor(int i)
73 aCV << QColor(0,0,255)
79 << QColor(10,100,150);
83 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
84 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
86 context()->EraseAll( Standard_False );
87 context()->Display( theObject, theMode, theSelectionMode );
93 viewWindow()->onTopView();
94 viewWindow()->onFitAll();
98 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
100 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
101 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
102 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
103 context()->Display( aShape, theMode, 0, Standard_False );
107 viewWindow()->onTopView();
108 viewWindow()->onFitAll();
112 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
114 context()->EraseAll( Standard_False );
118 if( theShape.ShapeType()==TopAbs_SHELL )
120 TopoDS_Iterator anIt( theShape );
121 for( ; anIt.More(); anIt.Next(), i++ )
122 show( anIt.Value(), theMode, false, GetColor(i) );
125 show( theShape, theMode, false, GetColor(0) );
129 viewWindow()->onTopView();
130 viewWindow()->onFitAll();
134 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
136 if( theImage1.isNull() || theImage2.isNull() )
137 return theImage1.isNull() == theImage2.isNull();
139 if( theImage1.size() != theImage2.size() )
142 int aBytesCount = theImage1.byteCount();
143 const uchar *aBytes1 = theImage1.constBits();
144 const uchar *aBytes2 = theImage2.constBits();
145 int aBytesCountE = 0;
146 for( int i=0; i<aBytesCount; i++ )
147 if( aBytes1[i] != aBytes2[i] )
150 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
156 bool TestViewer::AssertImages( QString& theMessage )
158 QImage anActualImage = viewWindow()->dumpView();
160 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
161 anExpectedRefFilePath += "/" + myKey + ".png";
162 QImage anExpectedRefImage;
163 anExpectedRefImage.load( anExpectedRefFilePath );
165 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
171 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
172 anActualImage.save( aPath );
173 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
174 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
176 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
177 QPainter aPainter( &aDiff );
178 aPainter.drawImage( 0, 0, anExpectedRefImage );
179 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
180 aPainter.drawImage( 0, 0, anActualImage );
182 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
183 aDiff.save( aDiffFilePath );
188 Handle_Aspect_ColorScale TestViewer::showColorScale()
190 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
192 return Handle(Aspect_ColorScale)();
194 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
195 if( aColorScale.IsNull() )
198 Standard_Real anXPos = 0.05; //TODO
199 Standard_Real anYPos = 0.1; //TODO
200 Standard_Real aWidth = 0.2; //TODO
201 Standard_Real aHeight = 0.5; //TODO
202 Standard_Integer aTextHeight = 14; //TODO
203 Standard_Integer aNbIntervals = 30; //TODO
205 aColorScale->SetXPosition( anXPos );
206 aColorScale->SetYPosition( anYPos );
207 aColorScale->SetWidth( aWidth );
208 aColorScale->SetHeight( aHeight );
209 aColorScale->SetTextHeight( aTextHeight );
210 aColorScale->SetNumberOfIntervals( aNbIntervals );
212 aColorScale->SetTitle( "test" );
213 aColorScale->SetRange( 0, 1 );
215 if( !aView->ColorScaleIsDisplayed() )
216 aView->ColorScaleDisplay();