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_Shape.hxx>
38 #include <AIS_ColorScale.hxx>
39 #include <Prs3d_PointAspect.hxx>
40 #include <TopoDS_Iterator.hxx>
44 #include <TopExp_Explorer.hxx>
47 #include <GEOMUtils.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #pragma warning ( default: 4251 )
55 #include <cppunit/TestAssert.h>
57 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
58 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
59 QString TestViewer::myKey = "";
61 OCCViewer_ViewManager* TestViewer::viewManager()
66 myViewManager = new OCCViewer_ViewManager( 0, 0 );
67 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
69 aViewer->setTrihedronSize( 100, true );
70 aViewer->setInteractionStyle( 0 );
71 aViewer->setZoomingStyle( 1 );
73 myViewManager->setViewModel( aViewer );
74 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
79 OCCViewer_Viewer* TestViewer::viewer()
81 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
84 OCCViewer_ViewWindow* TestViewer::viewWindow()
86 viewManager(); //to create the view if it was not created earlier
90 Handle(AIS_InteractiveContext) TestViewer::context()
92 return viewer()->getAISContext();
95 QColor TestViewer::GetColor(int i)
97 static QVector<QColor> aCV;
100 aCV << QColor(0,0,255)
103 << QColor(255,255,20)
104 << QColor(20,255,255)
105 << QColor(100,100,20)
106 << QColor(10,100,150);
112 QColor TestColor = aCV[i % aCV.size()];
113 QColor NewColor((TestColor.red() + i * 41) % 256,
114 (TestColor.green() + i * 13) % 256,
115 (TestColor.blue() + i * 23) % 256);
120 void TestViewer::eraseAll( bool isUpdate )
122 context()->CloseLocalContext( -1, Standard_False );
123 context()->EraseAll( isUpdate );
126 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
127 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
129 QString aNewKey = theKey;
130 if( !aNewKey.isEmpty() )
136 context()->Display( theObject, theMode, theSelectionMode );
137 if( theSelectionMode > 0 )
139 context()->OpenLocalContext();
140 context()->Activate( theObject, theSelectionMode, Standard_True );
145 viewWindow()->onTopView();
146 viewWindow()->onFitAll();
150 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
152 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
153 if( theShape.ShapeType()==TopAbs_VERTEX )
154 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
155 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
156 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
157 context()->Display( aShape, theMode, 0, Standard_False );
161 viewWindow()->onTopView();
162 viewWindow()->onFitAll();
166 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
168 QString aNewKey = theKey;
169 if( !aNewKey.isEmpty() )
175 if( theShape.IsNull() )
179 //show all faces first
180 TopTools_ListOfShape aListOfFaces;
181 TopExp_Explorer aFE( theShape, TopAbs_FACE );
182 for( ; aFE.More(); aFE.Next() )
183 aListOfFaces.Append(aFE.Current());
184 GEOMUtils::SortShapes(aListOfFaces);
185 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
186 for( ; aLF.More(); aLF.Next(), i++)
187 show( aLF.Value(), theMode, false, GetColor(i) );
189 //show all independent wires
190 TopTools_ListOfShape aListOfWires;
191 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
192 for( ; aWE.More(); aWE.Next() )
193 aListOfWires.Append(aWE.Current());
194 GEOMUtils::SortShapes(aListOfWires);
195 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
196 for( ; aLW.More(); aLW.Next(), i++)
197 show( aLW.Value(), theMode, false, GetColor(i) );
199 //show all independent edges
200 TopTools_ListOfShape aListOfEdges;
201 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
202 for( ; anEE.More(); anEE.Next())
203 aListOfEdges.Append(anEE.Current());
204 GEOMUtils::SortShapes(aListOfEdges);
205 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
206 for( ; aLE.More(); aLE.Next(), i++)
207 show( aLE.Value(), theMode, false, GetColor(i) );
211 viewWindow()->onTopView();
212 viewWindow()->onFitAll();
216 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
218 if( theShape.ShapeType()==TopAbs_SHELL )
220 TopoDS_Iterator anIt( theShape );
221 for( ; anIt.More(); anIt.Next())
223 show( anIt.Value(), theMode, false, GetColor(i) );
227 else if (theShape.ShapeType()==TopAbs_FACE ||
228 theShape.ShapeType()==TopAbs_WIRE ||
229 theShape.ShapeType()==TopAbs_EDGE ||
230 theShape.ShapeType()==TopAbs_VERTEX )
232 show( theShape, theMode, false, GetColor(i) );
237 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
239 if( theImage1.isNull() || theImage2.isNull() )
240 return theImage1.isNull() == theImage2.isNull();
242 if( theImage1.size() != theImage2.size() )
245 int aBytesCount = theImage1.byteCount();
246 const uchar *aBytes1 = theImage1.constBits();
247 const uchar *aBytes2 = theImage2.constBits();
248 int aBytesCountE = 0;
249 for( int i=0; i<aBytesCount; i++ )
250 if( aBytes1[i] != aBytes2[i] )
253 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
259 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase )
261 QImage anActualImage;
263 anActualImage = *theImage;
265 anActualImage = viewWindow()->dumpView();
270 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test";
271 anExpectedRefFilePath += "/" + myKey + ".png";
272 QImage anExpectedRefImage;
273 anExpectedRefImage.load( anExpectedRefFilePath );
275 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
281 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
282 anActualImage.save( aPath );
283 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
284 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
286 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
287 QPainter aPainter( &aDiff );
288 aPainter.drawImage( 0, 0, anExpectedRefImage );
289 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
290 aPainter.drawImage( 0, 0, anActualImage );
292 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
293 aDiff.save( aDiffFilePath );
295 anExpectedRefImage.save( QDir::tempPath() + "/" + myKey + "_1.png");
300 Handle(AIS_ColorScale) TestViewer::colorScale()
302 static Handle(AIS_ColorScale) aColorScale = new AIS_ColorScale();
307 void TestViewer::showColorScale( bool isShow )
309 Handle(AIS_ColorScale) aColorScale = colorScale();
310 if( aColorScale.IsNull() )
313 Standard_Real anXPos = 0.05;
314 Standard_Real anYPos = 0.1;
315 Standard_Real aWidth = 0.2;
316 Standard_Real aHeight = 0.5;
317 Standard_Integer aTextHeight = 14;
318 Standard_Integer aNbIntervals = 30;
320 aColorScale->SetXPosition( anXPos );
321 aColorScale->SetYPosition( anYPos );
322 aColorScale->SetWidth( aWidth );
323 aColorScale->SetHeight( aHeight );
324 aColorScale->SetTextHeight( aTextHeight );
325 aColorScale->SetNumberOfIntervals( aNbIntervals );
327 aColorScale->SetTitle( "test" );
328 aColorScale->SetRange( 0, 1 );
330 aColorScale->SetToUpdate();
334 if( !context()->IsDisplayed( aColorScale ) )
335 context()->Display( aColorScale );
339 if( context()->IsDisplayed( aColorScale ) )
340 context()->Erase( aColorScale );
344 bool TestViewer::ColorScaleIsDisplayed()
346 return context()->IsDisplayed( colorScale() );
349 void TestViewer::select( int theViewX, int theViewY )
351 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
352 context()->MoveTo( theViewX, theViewY, aView );
356 QString GetLine( QFile& theFile, bool isUtf8 )
358 QByteArray aLineData = theFile.readLine();
361 aLine = QString::fromUtf8( aLineData );
367 bool TestViewer::areScriptsEqual( const QString& theBaseName,
373 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test/HYDRO";
374 anExpectedRefFilePath += "/" + theBaseName;
376 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
378 QFile anExpected( anExpectedRefFilePath );
379 QFile anActual( anActualFilePath );
380 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
381 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
384 for( int i=0; i<theLinesToOmit; i++ )
385 anExpected.readLine();
389 while( !anExpected.atEnd() && isEqual )
391 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
392 QString anActualLine = GetLine( anActual, isActualUtf8 );
393 isEqual = anExpectedLine == anActualLine;
395 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
400 isEqual = anActual.atEnd();