1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <TestViewer.h>
20 #include <HYDROData_Tool.h>
21 #include <OCCViewer_ViewManager.h>
23 #pragma warning ( disable: 4251 )
25 #include <OCCViewer_ViewPort3d.h>
27 #pragma warning ( disable: 4251 )
29 #include <OCCViewer_ViewModel.h>
31 #pragma warning ( disable: 4251 )
33 #include <OCCViewer_ViewWindow.h>
35 #pragma warning ( disable: 4251 )
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_Shape.hxx>
39 #include <Aspect_ColorScale.hxx>
40 #include <Prs3d_PointAspect.hxx>
41 #include <TopoDS_Iterator.hxx>
47 #pragma warning ( default: 4251 )
50 #include <cppunit/TestAssert.h>
52 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
53 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
54 QString TestViewer::myKey = "";
56 OCCViewer_ViewManager* TestViewer::viewManager()
61 myViewManager = new OCCViewer_ViewManager( 0, 0 );
62 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
64 aViewer->setTrihedronSize( 100, true );
65 aViewer->setInteractionStyle( 0 );
66 aViewer->setZoomingStyle( 1 );
68 myViewManager->setViewModel( aViewer );
69 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
74 OCCViewer_Viewer* TestViewer::viewer()
76 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
79 OCCViewer_ViewWindow* TestViewer::viewWindow()
81 viewManager(); //to create the view if it was not created earlier
85 Handle(AIS_InteractiveContext) context()
87 return TestViewer::viewer()->getAISContext();
90 QColor TestViewer::GetColor(int i)
92 static QVector<QColor> aCV;
95 aCV << QColor(0,0,255)
100 << QColor(100,100,20)
101 << QColor(10,100,150);
106 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
107 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
109 QString aNewKey = theKey;
110 if( !aNewKey.isEmpty() )
113 context()->CloseLocalContext();
114 context()->EraseAll( Standard_False );
117 context()->Display( theObject, theMode, theSelectionMode );
118 if( theSelectionMode > 0 )
120 context()->OpenLocalContext();
121 context()->Activate( theObject, theSelectionMode, Standard_True );
126 viewWindow()->onTopView();
127 viewWindow()->onFitAll();
131 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
133 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
134 if( theShape.ShapeType()==TopAbs_VERTEX )
135 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
136 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
137 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
138 context()->Display( aShape, theMode, 0, Standard_False );
142 viewWindow()->onTopView();
143 viewWindow()->onFitAll();
147 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
149 QString aNewKey = theKey;
150 if( !aNewKey.isEmpty() )
152 context()->CloseLocalContext();
153 context()->EraseAll( Standard_False );
157 if( theShape.IsNull() )
161 if( theShape.ShapeType()==TopAbs_SHELL )
163 TopoDS_Iterator anIt( theShape );
164 for( ; anIt.More(); anIt.Next(), i++ )
165 show( anIt.Value(), theMode, false, GetColor(i) );
168 show( theShape, theMode, false, GetColor(0) );
172 viewWindow()->onTopView();
173 viewWindow()->onFitAll();
177 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
179 if( theImage1.isNull() || theImage2.isNull() )
180 return theImage1.isNull() == theImage2.isNull();
182 if( theImage1.size() != theImage2.size() )
185 int aBytesCount = theImage1.byteCount();
186 const uchar *aBytes1 = theImage1.constBits();
187 const uchar *aBytes2 = theImage2.constBits();
188 int aBytesCountE = 0;
189 for( int i=0; i<aBytesCount; i++ )
190 if( aBytes1[i] != aBytes2[i] )
193 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
199 bool TestViewer::AssertImages( QString& theMessage )
201 QImage anActualImage = viewWindow()->dumpView();
203 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
204 anExpectedRefFilePath += "/" + myKey + ".png";
205 QImage anExpectedRefImage;
206 anExpectedRefImage.load( anExpectedRefFilePath );
208 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
214 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
215 anActualImage.save( aPath );
216 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
217 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
219 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
220 QPainter aPainter( &aDiff );
221 aPainter.drawImage( 0, 0, anExpectedRefImage );
222 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
223 aPainter.drawImage( 0, 0, anActualImage );
225 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
226 aDiff.save( aDiffFilePath );
231 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
233 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
235 return Handle(Aspect_ColorScale)();
237 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
238 if( aColorScale.IsNull() )
241 Standard_Real anXPos = 0.05;
242 Standard_Real anYPos = 0.1;
243 Standard_Real aWidth = 0.2;
244 Standard_Real aHeight = 0.5;
245 Standard_Integer aTextHeight = 14;
246 Standard_Integer aNbIntervals = 30;
248 aColorScale->SetXPosition( anXPos );
249 aColorScale->SetYPosition( anYPos );
250 aColorScale->SetWidth( aWidth );
251 aColorScale->SetHeight( aHeight );
252 aColorScale->SetTextHeight( aTextHeight );
253 aColorScale->SetNumberOfIntervals( aNbIntervals );
255 aColorScale->SetTitle( "test" );
256 aColorScale->SetRange( 0, 1 );
260 if( !aView->ColorScaleIsDisplayed() )
261 aView->ColorScaleDisplay();
265 if( aView->ColorScaleIsDisplayed() )
266 aView->ColorScaleErase();
271 void TestViewer::select( int theViewX, int theViewY )
273 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
274 context()->MoveTo( theViewX, theViewY, aView );
278 bool TestViewer::areScriptsEqual( const QString& theBaseName )
280 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
281 anExpectedRefFilePath += "/" + theBaseName;
283 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
285 QFile anExpected( anExpectedRefFilePath );
286 QFile anActual( anActualFilePath );
287 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
288 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
291 const int aLinesToOmit = 20;
292 for( int i=0; i<aLinesToOmit; i++ )
293 anExpected.readLine();
296 while( !anExpected.atEnd() && isEqual )
298 QString anExpectedLine = QString::fromUtf8( anExpected.readLine() );
299 QString anActualLine = anActual.readLine();
300 isEqual = anExpectedLine == anActualLine;
304 isEqual = anActual.atEnd();