2 #include <TestViewer.h>
3 #include <HYDROData_Tool.h>
4 #include <OCCViewer_ViewManager.h>
6 #pragma warning ( disable: 4251 )
8 #include <OCCViewer_ViewPort3d.h>
10 #pragma warning ( disable: 4251 )
12 #include <OCCViewer_ViewModel.h>
14 #pragma warning ( disable: 4251 )
16 #include <OCCViewer_ViewWindow.h>
18 #pragma warning ( disable: 4251 )
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_Shape.hxx>
22 #include <Aspect_ColorScale.hxx>
23 #include <Prs3d_PointAspect.hxx>
24 #include <TopoDS_Iterator.hxx>
30 #pragma warning ( default: 4251 )
33 #include <cppunit/TestAssert.h>
35 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
36 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
37 QString TestViewer::myKey = "";
39 OCCViewer_ViewManager* TestViewer::viewManager()
44 myViewManager = new OCCViewer_ViewManager( 0, 0 );
45 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
47 aViewer->setTrihedronSize( 100, true );
48 aViewer->setInteractionStyle( 0 );
49 aViewer->setZoomingStyle( 1 );
51 myViewManager->setViewModel( aViewer );
52 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
57 OCCViewer_Viewer* TestViewer::viewer()
59 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
62 OCCViewer_ViewWindow* TestViewer::viewWindow()
64 viewManager(); //to create the view if it was not created earlier
68 Handle(AIS_InteractiveContext) context()
70 return TestViewer::viewer()->getAISContext();
73 QColor TestViewer::GetColor(int i)
75 static QVector<QColor> aCV;
78 aCV << QColor(0,0,255)
84 << QColor(10,100,150);
89 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
90 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
92 QString aNewKey = theKey;
93 if( !aNewKey.isEmpty() )
96 context()->CloseLocalContext();
97 context()->EraseAll( Standard_False );
100 context()->Display( theObject, theMode, theSelectionMode );
101 if( theSelectionMode > 0 )
103 context()->OpenLocalContext();
104 context()->Activate( theObject, theSelectionMode, Standard_True );
109 viewWindow()->onTopView();
110 viewWindow()->onFitAll();
114 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
116 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
117 if( theShape.ShapeType()==TopAbs_VERTEX )
118 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
119 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
120 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
121 context()->Display( aShape, theMode, 0, Standard_False );
125 viewWindow()->onTopView();
126 viewWindow()->onFitAll();
130 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
132 context()->CloseLocalContext();
133 context()->EraseAll( Standard_False );
137 if( theShape.IsNull() )
141 if( theShape.ShapeType()==TopAbs_SHELL )
143 TopoDS_Iterator anIt( theShape );
144 for( ; anIt.More(); anIt.Next(), i++ )
145 show( anIt.Value(), theMode, false, GetColor(i) );
148 show( theShape, theMode, false, GetColor(0) );
152 viewWindow()->onTopView();
153 viewWindow()->onFitAll();
157 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
159 if( theImage1.isNull() || theImage2.isNull() )
160 return theImage1.isNull() == theImage2.isNull();
162 if( theImage1.size() != theImage2.size() )
165 int aBytesCount = theImage1.byteCount();
166 const uchar *aBytes1 = theImage1.constBits();
167 const uchar *aBytes2 = theImage2.constBits();
168 int aBytesCountE = 0;
169 for( int i=0; i<aBytesCount; i++ )
170 if( aBytes1[i] != aBytes2[i] )
173 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
179 bool TestViewer::AssertImages( QString& theMessage )
181 QImage anActualImage = viewWindow()->dumpView();
183 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
184 anExpectedRefFilePath += "/" + myKey + ".png";
185 QImage anExpectedRefImage;
186 anExpectedRefImage.load( anExpectedRefFilePath );
188 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
194 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
195 anActualImage.save( aPath );
196 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
197 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
199 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
200 QPainter aPainter( &aDiff );
201 aPainter.drawImage( 0, 0, anExpectedRefImage );
202 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
203 aPainter.drawImage( 0, 0, anActualImage );
205 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
206 aDiff.save( aDiffFilePath );
211 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
213 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
215 return Handle(Aspect_ColorScale)();
217 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
218 if( aColorScale.IsNull() )
221 Standard_Real anXPos = 0.05; //TODO
222 Standard_Real anYPos = 0.1; //TODO
223 Standard_Real aWidth = 0.2; //TODO
224 Standard_Real aHeight = 0.5; //TODO
225 Standard_Integer aTextHeight = 14; //TODO
226 Standard_Integer aNbIntervals = 30; //TODO
228 aColorScale->SetXPosition( anXPos );
229 aColorScale->SetYPosition( anYPos );
230 aColorScale->SetWidth( aWidth );
231 aColorScale->SetHeight( aHeight );
232 aColorScale->SetTextHeight( aTextHeight );
233 aColorScale->SetNumberOfIntervals( aNbIntervals );
235 aColorScale->SetTitle( "test" );
236 aColorScale->SetRange( 0, 1 );
240 if( !aView->ColorScaleIsDisplayed() )
241 aView->ColorScaleDisplay();
245 if( aView->ColorScaleIsDisplayed() )
246 aView->ColorScaleErase();
251 void TestViewer::select( int theViewX, int theViewY )
253 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
254 context()->MoveTo( theViewX, theViewY, aView );