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 context()->CloseLocalContext();
93 context()->EraseAll( Standard_False );
94 if( theSelectionMode > 0 )
96 context()->Display( theObject, theMode, theSelectionMode );
97 context()->OpenLocalContext();
98 context()->Activate( theObject, theSelectionMode, Standard_True );
105 viewWindow()->onTopView();
106 viewWindow()->onFitAll();
110 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
112 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
113 if( theShape.ShapeType()==TopAbs_VERTEX )
114 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
115 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
116 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
117 context()->Display( aShape, theMode, 0, Standard_False );
121 viewWindow()->onTopView();
122 viewWindow()->onFitAll();
126 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
128 context()->CloseLocalContext();
129 context()->EraseAll( Standard_False );
133 if( theShape.ShapeType()==TopAbs_SHELL )
135 TopoDS_Iterator anIt( theShape );
136 for( ; anIt.More(); anIt.Next(), i++ )
137 show( anIt.Value(), theMode, false, GetColor(i) );
140 show( theShape, theMode, false, GetColor(0) );
144 viewWindow()->onTopView();
145 viewWindow()->onFitAll();
149 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
151 if( theImage1.isNull() || theImage2.isNull() )
152 return theImage1.isNull() == theImage2.isNull();
154 if( theImage1.size() != theImage2.size() )
157 int aBytesCount = theImage1.byteCount();
158 const uchar *aBytes1 = theImage1.constBits();
159 const uchar *aBytes2 = theImage2.constBits();
160 int aBytesCountE = 0;
161 for( int i=0; i<aBytesCount; i++ )
162 if( aBytes1[i] != aBytes2[i] )
165 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
171 bool TestViewer::AssertImages( QString& theMessage )
173 QImage anActualImage = viewWindow()->dumpView();
175 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
176 anExpectedRefFilePath += "/" + myKey + ".png";
177 QImage anExpectedRefImage;
178 anExpectedRefImage.load( anExpectedRefFilePath );
180 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
186 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
187 anActualImage.save( aPath );
188 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
189 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
191 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
192 QPainter aPainter( &aDiff );
193 aPainter.drawImage( 0, 0, anExpectedRefImage );
194 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
195 aPainter.drawImage( 0, 0, anActualImage );
197 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
198 aDiff.save( aDiffFilePath );
203 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
205 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
207 return Handle(Aspect_ColorScale)();
209 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
210 if( aColorScale.IsNull() )
213 Standard_Real anXPos = 0.05; //TODO
214 Standard_Real anYPos = 0.1; //TODO
215 Standard_Real aWidth = 0.2; //TODO
216 Standard_Real aHeight = 0.5; //TODO
217 Standard_Integer aTextHeight = 14; //TODO
218 Standard_Integer aNbIntervals = 30; //TODO
220 aColorScale->SetXPosition( anXPos );
221 aColorScale->SetYPosition( anYPos );
222 aColorScale->SetWidth( aWidth );
223 aColorScale->SetHeight( aHeight );
224 aColorScale->SetTextHeight( aTextHeight );
225 aColorScale->SetNumberOfIntervals( aNbIntervals );
227 aColorScale->SetTitle( "test" );
228 aColorScale->SetRange( 0, 1 );
232 if( !aView->ColorScaleIsDisplayed() )
233 aView->ColorScaleDisplay();
237 if( aView->ColorScaleIsDisplayed() )
238 aView->ColorScaleErase();