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 <TopoDS_Iterator.hxx>
29 #pragma warning ( default: 4251 )
32 #include <cppunit/TestAssert.h>
34 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
35 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
36 QString TestViewer::myKey = "";
38 OCCViewer_ViewManager* TestViewer::viewManager()
43 myViewManager = new OCCViewer_ViewManager( 0, 0 );
44 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
46 aViewer->setTrihedronSize( 100, true );
47 aViewer->setInteractionStyle( 0 );
48 aViewer->setZoomingStyle( 1 );
50 myViewManager->setViewModel( aViewer );
51 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
56 OCCViewer_Viewer* TestViewer::viewer()
58 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
61 OCCViewer_ViewWindow* TestViewer::viewWindow()
63 viewManager(); //to create the view if it was not created earlier
67 Handle(AIS_InteractiveContext) context()
69 return TestViewer::viewer()->getAISContext();
72 QColor TestViewer::GetColor(int i)
74 static QVector<QColor> aCV;
77 aCV << QColor(0,0,255)
83 << QColor(10,100,150);
88 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
89 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
91 context()->CloseLocalContext();
92 context()->EraseAll( Standard_False );
93 if( theSelectionMode > 0 )
95 context()->Display( theObject, theMode, theSelectionMode );
96 context()->OpenLocalContext();
97 context()->Activate( theObject, theSelectionMode, Standard_True );
104 viewWindow()->onTopView();
105 viewWindow()->onFitAll();
109 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
111 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
112 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
113 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
114 context()->Display( aShape, theMode, 0, Standard_False );
118 viewWindow()->onTopView();
119 viewWindow()->onFitAll();
123 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
125 context()->EraseAll( Standard_False );
129 if( theShape.ShapeType()==TopAbs_SHELL )
131 TopoDS_Iterator anIt( theShape );
132 for( ; anIt.More(); anIt.Next(), i++ )
133 show( anIt.Value(), theMode, false, GetColor(i) );
136 show( theShape, theMode, false, GetColor(0) );
140 viewWindow()->onTopView();
141 viewWindow()->onFitAll();
145 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
147 if( theImage1.isNull() || theImage2.isNull() )
148 return theImage1.isNull() == theImage2.isNull();
150 if( theImage1.size() != theImage2.size() )
153 int aBytesCount = theImage1.byteCount();
154 const uchar *aBytes1 = theImage1.constBits();
155 const uchar *aBytes2 = theImage2.constBits();
156 int aBytesCountE = 0;
157 for( int i=0; i<aBytesCount; i++ )
158 if( aBytes1[i] != aBytes2[i] )
161 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
167 bool TestViewer::AssertImages( QString& theMessage )
169 QImage anActualImage = viewWindow()->dumpView();
171 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
172 anExpectedRefFilePath += "/" + myKey + ".png";
173 QImage anExpectedRefImage;
174 anExpectedRefImage.load( anExpectedRefFilePath );
176 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
182 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
183 anActualImage.save( aPath );
184 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
185 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
187 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
188 QPainter aPainter( &aDiff );
189 aPainter.drawImage( 0, 0, anExpectedRefImage );
190 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
191 aPainter.drawImage( 0, 0, anActualImage );
193 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
194 aDiff.save( aDiffFilePath );
199 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
201 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
203 return Handle(Aspect_ColorScale)();
205 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
206 if( aColorScale.IsNull() )
209 Standard_Real anXPos = 0.05; //TODO
210 Standard_Real anYPos = 0.1; //TODO
211 Standard_Real aWidth = 0.2; //TODO
212 Standard_Real aHeight = 0.5; //TODO
213 Standard_Integer aTextHeight = 14; //TODO
214 Standard_Integer aNbIntervals = 30; //TODO
216 aColorScale->SetXPosition( anXPos );
217 aColorScale->SetYPosition( anYPos );
218 aColorScale->SetWidth( aWidth );
219 aColorScale->SetHeight( aHeight );
220 aColorScale->SetTextHeight( aTextHeight );
221 aColorScale->SetNumberOfIntervals( aNbIntervals );
223 aColorScale->SetTitle( "test" );
224 aColorScale->SetRange( 0, 1 );
228 if( !aView->ColorScaleIsDisplayed() )
229 aView->ColorScaleDisplay();
233 if( aView->ColorScaleIsDisplayed() )
234 aView->ColorScaleErase();