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>
47 #include <Prs3d_IsoAspect.hxx>
49 #include <GEOMUtils.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #pragma warning ( default: 4251 )
57 #include <cppunit/TestAssert.h>
59 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
60 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
61 QString TestViewer::myKey = "";
63 OCCViewer_ViewManager* TestViewer::viewManager()
68 myViewManager = new OCCViewer_ViewManager( 0, 0 );
69 OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
71 aViewer->setTrihedronSize( 100, true );
72 aViewer->setInteractionStyle( 0 );
73 aViewer->setZoomingStyle( 1 );
75 myViewManager->setViewModel( aViewer );
76 myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
81 OCCViewer_Viewer* TestViewer::viewer()
83 return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
86 OCCViewer_ViewWindow* TestViewer::viewWindow()
88 viewManager(); //to create the view if it was not created earlier
92 Handle(AIS_InteractiveContext) TestViewer::context()
94 return viewer()->getAISContext();
97 QColor TestViewer::GetColor(int i)
99 static QVector<QColor> aCV;
102 aCV << QColor(0,0,255)
105 << QColor(255,255,20)
106 << QColor(20,255,255)
107 << QColor(100,100,20)
108 << QColor(10,100,150);
114 QColor TestColor = aCV[i % aCV.size()];
115 QColor NewColor((TestColor.red() + i * 41) % 256,
116 (TestColor.green() + i * 13) % 256,
117 (TestColor.blue() + i * 23) % 256);
122 void TestViewer::eraseAll( bool isUpdate )
124 context()->CloseLocalContext( -1, Standard_False );
125 context()->EraseAll( isUpdate );
128 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
129 int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
131 QString aNewKey = theKey;
132 if( !aNewKey.isEmpty() )
138 context()->Display( theObject, theMode, theSelectionMode );
139 if( theSelectionMode > 0 )
141 context()->OpenLocalContext();
142 context()->Activate( theObject, theSelectionMode, Standard_True );
147 viewWindow()->onTopView();
148 viewWindow()->onFitAll();
152 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor,
153 int theUIANb, int theVIANb)
155 Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
156 if( theShape.ShapeType()==TopAbs_VERTEX )
157 aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
158 if (theShape.ShapeType()==TopAbs_FACE)
160 context()->DefaultDrawer()->UIsoAspect()->SetNumber(theUIANb);
161 context()->DefaultDrawer()->VIsoAspect()->SetNumber(theVIANb);
162 Handle_Prs3d_Drawer aDrawer = aShape->Attributes();
163 aDrawer->UIsoAspect()->SetNumber(theUIANb);
164 aDrawer->VIsoAspect()->SetNumber(theVIANb);
166 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
167 aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
168 context()->Display( aShape, theMode, 0, Standard_False );
172 viewWindow()->onTopView();
173 viewWindow()->onFitAll();
176 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey,
177 int theUIANb, int theVIANb)
179 QString aNewKey = theKey;
180 if( !aNewKey.isEmpty() )
186 if( theShape.IsNull() )
190 //show all faces first
191 TopTools_ListOfShape aListOfFaces;
192 TopExp_Explorer aFE( theShape, TopAbs_FACE );
193 for( ; aFE.More(); aFE.Next() )
194 aListOfFaces.Append(aFE.Current());
195 GEOMUtils::SortShapes(aListOfFaces);
196 TopTools_ListIteratorOfListOfShape aLF(aListOfFaces);
197 for( ; aLF.More(); aLF.Next(), i++)
198 show( aLF.Value(), theMode, false, GetColor(i), theUIANb, theVIANb );
200 //show all independent wires
201 TopTools_ListOfShape aListOfWires;
202 TopExp_Explorer aWE( theShape, TopAbs_WIRE, TopAbs_FACE );
203 for( ; aWE.More(); aWE.Next() )
204 aListOfWires.Append(aWE.Current());
205 GEOMUtils::SortShapes(aListOfWires);
206 TopTools_ListIteratorOfListOfShape aLW(aListOfWires);
207 for( ; aLW.More(); aLW.Next(), i++)
208 show( aLW.Value(), theMode, false, GetColor(i) );
210 //show all independent edges
211 TopTools_ListOfShape aListOfEdges;
212 TopExp_Explorer anEE( theShape, TopAbs_EDGE, TopAbs_WIRE );
213 for( ; anEE.More(); anEE.Next())
214 aListOfEdges.Append(anEE.Current());
215 GEOMUtils::SortShapes(aListOfEdges);
216 TopTools_ListIteratorOfListOfShape aLE(aListOfEdges);
217 for( ; aLE.More(); aLE.Next(), i++)
218 show( aLE.Value(), theMode, false, GetColor(i) );
222 viewWindow()->onTopView();
223 viewWindow()->onFitAll();
227 /*void TestViewer::ShowShape(const TopoDS_Shape& theShape, int theMode, int& i)
229 if( theShape.ShapeType()==TopAbs_SHELL )
231 TopoDS_Iterator anIt( theShape );
232 for( ; anIt.More(); anIt.Next())
234 show( anIt.Value(), theMode, false, GetColor(i) );
238 else if (theShape.ShapeType()==TopAbs_FACE ||
239 theShape.ShapeType()==TopAbs_WIRE ||
240 theShape.ShapeType()==TopAbs_EDGE ||
241 theShape.ShapeType()==TopAbs_VERTEX )
243 show( theShape, theMode, false, GetColor(i) );
248 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
250 if( theImage1.isNull() || theImage2.isNull() )
251 return theImage1.isNull() == theImage2.isNull();
253 if( theImage1.size() != theImage2.size() )
256 int aBytesCount = theImage1.byteCount();
257 const uchar *aBytes1 = theImage1.constBits();
258 const uchar *aBytes2 = theImage2.constBits();
259 int aBytesCountE = 0;
260 for( int i=0; i<aBytesCount; i++ )
261 if( aBytes1[i] != aBytes2[i] )
264 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
270 bool TestViewer::AssertImages( QString& theMessage, const QImage* theImage, const char* theCase )
272 QImage anActualImage;
274 anActualImage = *theImage;
276 anActualImage = viewWindow()->dumpView();
281 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test/HYDRO";
282 anExpectedRefFilePath += "/" + myKey + ".png";
283 QImage anExpectedRefImage;
284 anExpectedRefImage.load( anExpectedRefFilePath );
286 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
292 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
293 anActualImage.save( aPath );
294 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
295 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
297 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
298 QPainter aPainter( &aDiff );
299 aPainter.drawImage( 0, 0, anExpectedRefImage );
300 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
301 aPainter.drawImage( 0, 0, anActualImage );
303 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
304 aDiff.save( aDiffFilePath );
306 anExpectedRefImage.save( QDir::tempPath() + "/" + myKey + "_1.png");
311 Handle_Aspect_ColorScale TestViewer::colorScale()
313 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
315 return Handle(Aspect_ColorScale)();
317 return aView->ColorScale();
320 void TestViewer::showColorScale( bool isShow )
322 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
326 Handle(Aspect_ColorScale) aColorScale = colorScale();
327 if( aColorScale.IsNull() )
330 Standard_Real anXPos = 0.05;
331 Standard_Real anYPos = 0.1;
332 Standard_Real aWidth = 0.2;
333 Standard_Real aHeight = 0.5;
334 Standard_Integer aTextHeight = 14;
335 Standard_Integer aNbIntervals = 30;
337 aColorScale->SetXPosition( anXPos );
338 aColorScale->SetYPosition( anYPos );
339 aColorScale->SetWidth( aWidth );
340 aColorScale->SetHeight( aHeight );
341 aColorScale->SetTextHeight( aTextHeight );
342 aColorScale->SetNumberOfIntervals( aNbIntervals );
344 aColorScale->SetTitle( "test" );
345 aColorScale->SetRange( 0, 1 );
349 if( !aView->ColorScaleIsDisplayed() )
350 aView->ColorScaleDisplay();
354 if( aView->ColorScaleIsDisplayed() )
355 aView->ColorScaleErase();
359 bool TestViewer::ColorScaleIsDisplayed()
361 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
364 return aView->ColorScaleIsDisplayed();
367 void TestViewer::select( int theViewX, int theViewY )
369 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
370 context()->MoveTo( theViewX, theViewY, aView );
374 QString GetLine( QFile& theFile, bool isUtf8 )
376 QByteArray aLineData = theFile.readLine();
379 aLine = QString::fromUtf8( aLineData );
385 bool TestViewer::areScriptsEqual( const QString& theBaseName,
391 QString anExpectedRefFilePath = qgetenv( "HYDRO_ROOT_DIR" ) + "/bin/salome/test/HYDRO";
392 anExpectedRefFilePath += "/" + theBaseName;
394 QString anActualFilePath = QDir::tempPath() + "/" + theBaseName;
396 QFile anExpected( anExpectedRefFilePath );
397 QFile anActual( anActualFilePath );
398 if( !anExpected.open( QFile::ReadOnly | QFile::Text ) ||
399 !anActual.open ( QFile::ReadOnly | QFile::Text ) )
402 for( int i=0; i<theLinesToOmit; i++ )
403 anExpected.readLine();
407 while( !anExpected.atEnd() && isEqual )
409 QString anExpectedLine = GetLine( anExpected, isExpectedUtf8 );
410 QString anActualLine = GetLine( anActual, isActualUtf8 );
411 isEqual = anExpectedLine == anActualLine;
413 theMsg = QString( "line %1\nActual: %2\nExpected: %3" ).arg( i ).arg( anActualLine ).arg( anExpectedLine );
418 isEqual = anActual.atEnd();