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.ShapeType()==TopAbs_SHELL )
139 TopoDS_Iterator anIt( theShape );
140 for( ; anIt.More(); anIt.Next(), i++ )
141 show( anIt.Value(), theMode, false, GetColor(i) );
144 show( theShape, theMode, false, GetColor(0) );
148 viewWindow()->onTopView();
149 viewWindow()->onFitAll();
153 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
155 if( theImage1.isNull() || theImage2.isNull() )
156 return theImage1.isNull() == theImage2.isNull();
158 if( theImage1.size() != theImage2.size() )
161 int aBytesCount = theImage1.byteCount();
162 const uchar *aBytes1 = theImage1.constBits();
163 const uchar *aBytes2 = theImage2.constBits();
164 int aBytesCountE = 0;
165 for( int i=0; i<aBytesCount; i++ )
166 if( aBytes1[i] != aBytes2[i] )
169 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
175 bool TestViewer::AssertImages( QString& theMessage )
177 QImage anActualImage = viewWindow()->dumpView();
179 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
180 anExpectedRefFilePath += "/" + myKey + ".png";
181 QImage anExpectedRefImage;
182 anExpectedRefImage.load( anExpectedRefFilePath );
184 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
190 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
191 anActualImage.save( aPath );
192 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
193 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
195 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
196 QPainter aPainter( &aDiff );
197 aPainter.drawImage( 0, 0, anExpectedRefImage );
198 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
199 aPainter.drawImage( 0, 0, anActualImage );
201 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
202 aDiff.save( aDiffFilePath );
207 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
209 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
211 return Handle(Aspect_ColorScale)();
213 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
214 if( aColorScale.IsNull() )
217 Standard_Real anXPos = 0.05; //TODO
218 Standard_Real anYPos = 0.1; //TODO
219 Standard_Real aWidth = 0.2; //TODO
220 Standard_Real aHeight = 0.5; //TODO
221 Standard_Integer aTextHeight = 14; //TODO
222 Standard_Integer aNbIntervals = 30; //TODO
224 aColorScale->SetXPosition( anXPos );
225 aColorScale->SetYPosition( anYPos );
226 aColorScale->SetWidth( aWidth );
227 aColorScale->SetHeight( aHeight );
228 aColorScale->SetTextHeight( aTextHeight );
229 aColorScale->SetNumberOfIntervals( aNbIntervals );
231 aColorScale->SetTitle( "test" );
232 aColorScale->SetRange( 0, 1 );
236 if( !aView->ColorScaleIsDisplayed() )
237 aView->ColorScaleDisplay();
241 if( aView->ColorScaleIsDisplayed() )
242 aView->ColorScaleErase();
247 void TestViewer::select( int theViewX, int theViewY )
249 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
250 context()->MoveTo( theViewX, theViewY, aView );