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);
107 QColor TestColor = aCV[i % aCV.size()];
108 QColor NewColor((TestColor.red() + i * 41) % 256,
109 (TestColor.green() + i * 13) % 256,
110 (TestColor.blue() + i * 23) % 256);
115 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
116 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
118 QString aNewKey = theKey;
119 if( !aNewKey.isEmpty() )
122 context()->CloseLocalContext();
123 context()->EraseAll( Standard_False );
126 context()->Display( theObject, theMode, theSelectionMode );
127 if( theSelectionMode > 0 )
129 context()->OpenLocalContext();
130 context()->Activate( theObject, theSelectionMode, Standard_True );
135 viewWindow()->onTopView();
136 viewWindow()->onFitAll();
140 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
142 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
143 if( theShape.ShapeType()==TopAbs_VERTEX )
144 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
145 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
146 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
147 context()->Display( aShape, theMode, 0, Standard_False );
151 viewWindow()->onTopView();
152 viewWindow()->onFitAll();
156 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
158 QString aNewKey = theKey;
159 if( !aNewKey.isEmpty() )
161 context()->CloseLocalContext();
162 context()->EraseAll( Standard_False );
166 if( theShape.IsNull() )
170 if( theShape.ShapeType()==TopAbs_SHELL )
172 TopoDS_Iterator anIt( theShape );
173 for( ; anIt.More(); anIt.Next(), i++ )
174 show( anIt.Value(), theMode, false, GetColor(i) );
177 show( theShape, theMode, false, GetColor(0) );
181 viewWindow()->onTopView();
182 viewWindow()->onFitAll();
186 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
188 if( theImage1.isNull() || theImage2.isNull() )
189 return theImage1.isNull() == theImage2.isNull();
191 if( theImage1.size() != theImage2.size() )
194 int aBytesCount = theImage1.byteCount();
195 const uchar *aBytes1 = theImage1.constBits();
196 const uchar *aBytes2 = theImage2.constBits();
197 int aBytesCountE = 0;
198 for( int i=0; i<aBytesCount; i++ )
199 if( aBytes1[i] != aBytes2[i] )
202 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
208 bool TestViewer::AssertImages( QString& theMessage )
210 QImage anActualImage = viewWindow()->dumpView();
212 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
213 anExpectedRefFilePath += "/" + myKey + ".png";
214 QImage anExpectedRefImage;
215 anExpectedRefImage.load( anExpectedRefFilePath );
217 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
223 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
224 anActualImage.save( aPath );
225 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
226 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
228 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
229 QPainter aPainter( &aDiff );
230 aPainter.drawImage( 0, 0, anExpectedRefImage );
231 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
232 aPainter.drawImage( 0, 0, anActualImage );
234 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
235 aDiff.save( aDiffFilePath );
240 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
242 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
244 return Handle(Aspect_ColorScale)();
246 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
247 if( aColorScale.IsNull() )
250 Standard_Real anXPos = 0.05;
251 Standard_Real anYPos = 0.1;
252 Standard_Real aWidth = 0.2;
253 Standard_Real aHeight = 0.5;
254 Standard_Integer aTextHeight = 14;
255 Standard_Integer aNbIntervals = 30;
257 aColorScale->SetXPosition( anXPos );
258 aColorScale->SetYPosition( anYPos );
259 aColorScale->SetWidth( aWidth );
260 aColorScale->SetHeight( aHeight );
261 aColorScale->SetTextHeight( aTextHeight );
262 aColorScale->SetNumberOfIntervals( aNbIntervals );
264 aColorScale->SetTitle( "test" );
265 aColorScale->SetRange( 0, 1 );
269 if( !aView->ColorScaleIsDisplayed() )
270 aView->ColorScaleDisplay();
274 if( aView->ColorScaleIsDisplayed() )
275 aView->ColorScaleErase();
280 void TestViewer::select( int theViewX, int theViewY )
282 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
283 context()->MoveTo( theViewX, theViewY, aView );
287 QString GetLine( QFile& theFile, bool isUtf8 )
289 QByteArray aLineData = theFile.readLine();
292 aLine = QString::fromUtf8( aLineData );
298 bool TestViewer::areScriptsEqual( const QString& theBaseName,
303 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
304 anExpectedRefFilePath += "/" + theBaseName;
306 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
308 QFile anExpected( anExpectedRefFilePath );
309 QFile anActual( anActualFilePath );
310 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
311 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
314 for( int i=0; i<theLinesToOmit; i++ )
315 anExpected.readLine();
318 while( !anExpected.atEnd() && isEqual )
320 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
321 QString anActualLine = GetLine( anActual, isActualUtf8 );
322 isEqual = anExpectedLine == anActualLine;
326 isEqual = anActual.atEnd();