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>
45 #include <TopExp_Explorer.hxx>
49 #pragma warning ( default: 4251 )
52 #include <cppunit/TestAssert.h>
54 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
55 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
56 QString TestViewer::myKey = "";
58 OCCViewer_ViewManager* TestViewer::viewManager()
63 myViewManager = new OCCViewer_ViewManager( 0, 0 );
64 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
66 aViewer->setTrihedronSize( 100, true );
67 aViewer->setInteractionStyle( 0 );
68 aViewer->setZoomingStyle( 1 );
70 myViewManager->setViewModel( aViewer );
71 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
76 OCCViewer_Viewer* TestViewer::viewer()
78 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
81 OCCViewer_ViewWindow* TestViewer::viewWindow()
83 viewManager(); //to create the view if it was not created earlier
87 Handle(AIS_InteractiveContext) context()
89 return TestViewer::viewer()->getAISContext();
92 QColor TestViewer::GetColor(int i)
94 static QVector<QColor> aCV;
97 aCV << QColor(0,0,255)
100 << QColor(255,255,20)
101 << QColor(20,255,255)
102 << QColor(100,100,20)
103 << QColor(10,100,150);
109 QColor TestColor = aCV[i % aCV.size()];
110 QColor NewColor((TestColor.red() + i * 41) % 256,
111 (TestColor.green() + i * 13) % 256,
112 (TestColor.blue() + i * 23) % 256);
117 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
118 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
120 QString aNewKey = theKey;
121 if( !aNewKey.isEmpty() )
124 context()->CloseLocalContext();
125 context()->EraseAll( Standard_False );
128 context()->Display( theObject, theMode, theSelectionMode );
129 if( theSelectionMode > 0 )
131 context()->OpenLocalContext();
132 context()->Activate( theObject, theSelectionMode, Standard_True );
137 viewWindow()->onTopView();
138 viewWindow()->onFitAll();
142 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
144 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
145 if( theShape.ShapeType()==TopAbs_VERTEX )
146 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
147 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
148 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
149 context()->Display( aShape, theMode, 0, Standard_False );
153 viewWindow()->onTopView();
154 viewWindow()->onFitAll();
158 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
160 QString aNewKey = theKey;
161 if( !aNewKey.isEmpty() )
163 context()->CloseLocalContext();
164 context()->EraseAll( Standard_False );
168 if( theShape.IsNull() )
172 if( theShape.ShapeType()==TopAbs_COMPOUND )
174 TopExp_Explorer anExplorer( theShape, TopAbs_FACE );
175 for( ; anExplorer.More(); anExplorer.Next() )
176 ShowShape (anExplorer.Current(), theMode, i);
179 ShowShape(theShape, theMode, i);
184 viewWindow()->onTopView();
185 viewWindow()->onFitAll();
189 void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
191 if( theShape.ShapeType()==TopAbs_SHELL )
193 TopoDS_Iterator anIt( theShape );
194 for( ; anIt.More(); anIt.Next())
196 show( anIt.Value(), theMode, false, GetColor(i) );
200 else if (theShape.ShapeType()==TopAbs_FACE ||
201 theShape.ShapeType()==TopAbs_WIRE ||
202 theShape.ShapeType()==TopAbs_EDGE ||
203 theShape.ShapeType()==TopAbs_VERTEX )
205 show( theShape, theMode, false, GetColor(i) );
210 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
212 if( theImage1.isNull() || theImage2.isNull() )
213 return theImage1.isNull() == theImage2.isNull();
215 if( theImage1.size() != theImage2.size() )
218 int aBytesCount = theImage1.byteCount();
219 const uchar *aBytes1 = theImage1.constBits();
220 const uchar *aBytes2 = theImage2.constBits();
221 int aBytesCountE = 0;
222 for( int i=0; i<aBytesCount; i++ )
223 if( aBytes1[i] != aBytes2[i] )
226 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
232 bool TestViewer::AssertImages( QString& theMessage )
234 QImage anActualImage = viewWindow()->dumpView();
236 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
237 anExpectedRefFilePath += "/" + myKey + ".png";
238 QImage anExpectedRefImage;
239 anExpectedRefImage.load( anExpectedRefFilePath );
241 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
247 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
248 anActualImage.save( aPath );
249 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
250 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
252 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
253 QPainter aPainter( &aDiff );
254 aPainter.drawImage( 0, 0, anExpectedRefImage );
255 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
256 aPainter.drawImage( 0, 0, anActualImage );
258 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
259 aDiff.save( aDiffFilePath );
264 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
266 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
268 return Handle(Aspect_ColorScale)();
270 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
271 if( aColorScale.IsNull() )
274 Standard_Real anXPos = 0.05;
275 Standard_Real anYPos = 0.1;
276 Standard_Real aWidth = 0.2;
277 Standard_Real aHeight = 0.5;
278 Standard_Integer aTextHeight = 14;
279 Standard_Integer aNbIntervals = 30;
281 aColorScale->SetXPosition( anXPos );
282 aColorScale->SetYPosition( anYPos );
283 aColorScale->SetWidth( aWidth );
284 aColorScale->SetHeight( aHeight );
285 aColorScale->SetTextHeight( aTextHeight );
286 aColorScale->SetNumberOfIntervals( aNbIntervals );
288 aColorScale->SetTitle( "test" );
289 aColorScale->SetRange( 0, 1 );
293 if( !aView->ColorScaleIsDisplayed() )
294 aView->ColorScaleDisplay();
298 if( aView->ColorScaleIsDisplayed() )
299 aView->ColorScaleErase();
304 void TestViewer::select( int theViewX, int theViewY )
306 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
307 context()->MoveTo( theViewX, theViewY, aView );
311 QString GetLine( QFile& theFile, bool isUtf8 )
313 QByteArray aLineData = theFile.readLine();
316 aLine = QString::fromUtf8( aLineData );
322 bool TestViewer::areScriptsEqual( const QString& theBaseName,
328 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
329 anExpectedRefFilePath += "/" + theBaseName;
331 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
333 QFile anExpected( anExpectedRefFilePath );
334 QFile anActual( anActualFilePath );
335 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
336 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
339 for( int i=0; i<theLinesToOmit; i++ )
340 anExpected.readLine();
344 while( !anExpected.atEnd() && isEqual )
346 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
347 QString anActualLine = GetLine( anActual, isActualUtf8 );
348 isEqual = anExpectedLine == anActualLine;
350 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
355 isEqual = anActual.atEnd();