2 #include <TestViewer.h>
4 #include <OCCViewer_ViewManager.h>
6 #pragma warning ( disable: 4251 )
8 #include <OCCViewer_ViewModel.h>
10 #pragma warning ( disable: 4251 )
12 #include <OCCViewer_ViewWindow.h>
14 #pragma warning ( disable: 4251 )
16 #include <AIS_InteractiveContext.hxx>
17 #include <AIS_Shape.hxx>
18 #include <TopoDS_Iterator.hxx>
25 #pragma warning ( default: 4251 )
28 #include <cppunit/TestAssert.h>
30 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
31 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
32 QString TestViewer::myKey = "";
34 OCCViewer_ViewManager* TestViewer::viewManager()
39 myViewManager = new OCCViewer_ViewManager( 0, 0 );
40 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
42 aViewer->setTrihedronSize( 100, true );
43 aViewer->setInteractionStyle( 0 );
44 aViewer->setZoomingStyle( 1 );
46 myViewManager->setViewModel( aViewer );
47 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
52 OCCViewer_Viewer* TestViewer::viewer()
54 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
57 OCCViewer_ViewWindow* TestViewer::viewWindow()
59 viewManager(); //to create the view if it was not created earlier
63 Handle(AIS_InteractiveContext) context()
65 return TestViewer::viewer()->getAISContext();
68 QColor TestViewer::GetColor(int i)
71 aCV << QColor(0,0,255)
77 << QColor(10,100,150);
81 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
83 double r = theColor.red() / 255.0;
84 double g = theColor.green() / 255.0;
85 double b = theColor.blue() / 255.0;
87 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
88 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
89 aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
90 context()->Display( aShape, theMode, 0, Standard_False );
94 viewWindow()->onTopView();
95 viewWindow()->onFitAll();
99 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
101 context()->EraseAll();
105 if( theShape.ShapeType()==TopAbs_SHELL )
107 TopoDS_Iterator anIt( theShape );
108 for( ; anIt.More(); anIt.Next(), i++ )
109 show( anIt.Value(), theMode, false, GetColor(i) );
112 show( theShape, theMode, false, GetColor(0) );
116 viewWindow()->onTopView();
117 viewWindow()->onFitAll();
121 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
123 if( theImage1.isNull() || theImage2.isNull() )
124 return theImage1.isNull() == theImage2.isNull();
126 if( theImage1.size() != theImage2.size() )
129 int aBytesCount = theImage1.byteCount();
130 const uchar *aBytes1 = theImage1.constBits();
131 const uchar *aBytes2 = theImage2.constBits();
132 int aBytesCountE = 0;
133 for( int i=0; i<aBytesCount; i++ )
134 if( aBytes1[i] != aBytes2[i] )
137 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
143 bool TestViewer::AssertImages( QString& theMessage )
145 QImage anActualImage = viewWindow()->dumpView();
147 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
148 anExpectedRefFilePath += "/" + myKey + ".png";
149 QImage anExpectedRefImage;
150 anExpectedRefImage.load( anExpectedRefFilePath );
152 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
158 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
159 anActualImage.save( aPath );
160 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
161 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
163 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
164 QPainter aPainter( &aDiff );
165 aPainter.drawImage( 0, 0, anExpectedRefImage );
166 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
167 aPainter.drawImage( 0, 0, anActualImage );
169 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
170 aDiff.save( aDiffFilePath );