2 #include <TestViewer.h>
3 #include <HYDROData_Tool.h>
5 #include <OCCViewer_ViewManager.h>
6 #include <OCCViewer_ViewPort3d.h>
8 #pragma warning ( disable: 4251 )
10 #include <OCCViewer_ViewModel.h>
12 #pragma warning ( disable: 4251 )
14 #include <OCCViewer_ViewWindow.h>
16 #pragma warning ( disable: 4251 )
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_Shape.hxx>
20 #include <Aspect_ColorScale.hxx>
21 #include <TopoDS_Iterator.hxx>
28 #pragma warning ( default: 4251 )
31 #include <cppunit/TestAssert.h>
33 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
34 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
35 QString TestViewer::myKey = "";
37 OCCViewer_ViewManager* TestViewer::viewManager()
42 myViewManager = new OCCViewer_ViewManager( 0, 0 );
43 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
45 aViewer->setTrihedronSize( 100, true );
46 aViewer->setInteractionStyle( 0 );
47 aViewer->setZoomingStyle( 1 );
49 myViewManager->setViewModel( aViewer );
50 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
55 OCCViewer_Viewer* TestViewer::viewer()
57 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
60 OCCViewer_ViewWindow* TestViewer::viewWindow()
62 viewManager(); //to create the view if it was not created earlier
66 Handle(AIS_InteractiveContext) context()
68 return TestViewer::viewer()->getAISContext();
75 //std::cout << "hue: " << aHue << std::endl;
76 QColor aColor = QColor::fromHsl( aHue, 255, 128 );
80 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
81 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
83 context()->EraseAll( Standard_False );
84 context()->Display( theObject, theMode, theSelectionMode );
90 viewWindow()->onTopView();
91 viewWindow()->onFitAll();
95 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
97 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
98 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
99 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
100 context()->Display( aShape, theMode, 0, Standard_False );
104 viewWindow()->onTopView();
105 viewWindow()->onFitAll();
109 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
111 context()->EraseAll( Standard_False );
115 if( theShape.ShapeType()==TopAbs_COMPOUND )
117 TopoDS_Iterator anIt( theShape );
118 for( ; anIt.More(); anIt.Next() )
119 show( anIt.Value(), theMode, false, randomColor() );
122 show( theShape, theMode, false, randomColor() );
126 viewWindow()->onTopView();
127 viewWindow()->onFitAll();
131 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
133 if( theImage1.isNull() || theImage2.isNull() )
134 return theImage1.isNull() == theImage2.isNull();
136 if( theImage1.size() != theImage2.size() )
139 int aBytesCount = theImage1.byteCount();
140 const uchar *aBytes1 = theImage1.constBits();
141 const uchar *aBytes2 = theImage2.constBits();
142 int aBytesCountE = 0;
143 for( int i=0; i<aBytesCount; i++ )
144 if( aBytes1[i] != aBytes2[i] )
147 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
153 bool TestViewer::AssertImages( QString& theMessage )
155 QImage anActualImage = viewWindow()->dumpView();
157 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
158 anExpectedRefFilePath += "/" + myKey + ".png";
159 QImage anExpectedRefImage;
160 anExpectedRefImage.load( anExpectedRefFilePath );
162 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
168 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
169 anActualImage.save( aPath );
170 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
171 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
173 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
174 QPainter aPainter( &aDiff );
175 aPainter.drawImage( 0, 0, anExpectedRefImage );
176 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
177 aPainter.drawImage( 0, 0, anActualImage );
179 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
180 aDiff.save( aDiffFilePath );
185 Handle_Aspect_ColorScale TestViewer::showColorScale()
187 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
189 return Handle(Aspect_ColorScale)();
191 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
192 if( aColorScale.IsNull() )
195 Standard_Real anXPos = 0.05; //TODO
196 Standard_Real anYPos = 0.1; //TODO
197 Standard_Real aWidth = 0.2; //TODO
198 Standard_Real aHeight = 0.5; //TODO
199 Standard_Integer aTextHeight = 14; //TODO
200 Standard_Integer aNbIntervals = 30; //TODO
202 aColorScale->SetXPosition( anXPos );
203 aColorScale->SetYPosition( anYPos );
204 aColorScale->SetWidth( aWidth );
205 aColorScale->SetHeight( aHeight );
206 aColorScale->SetTextHeight( aTextHeight );
207 aColorScale->SetNumberOfIntervals( aNbIntervals );
209 aColorScale->SetTitle( "test" );
210 aColorScale->SetRange( 0, 1 );
212 if( !aView->ColorScaleIsDisplayed() )
213 aView->ColorScaleDisplay();