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 context()->CloseLocalContext();
150 context()->EraseAll( Standard_False );
154 if( theShape.IsNull() )
158 if( theShape.ShapeType()==TopAbs_SHELL )
160 TopoDS_Iterator anIt( theShape );
161 for( ; anIt.More(); anIt.Next(), i++ )
162 show( anIt.Value(), theMode, false, GetColor(i) );
165 show( theShape, theMode, false, GetColor(0) );
169 viewWindow()->onTopView();
170 viewWindow()->onFitAll();
174 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
176 if( theImage1.isNull() || theImage2.isNull() )
177 return theImage1.isNull() == theImage2.isNull();
179 if( theImage1.size() != theImage2.size() )
182 int aBytesCount = theImage1.byteCount();
183 const uchar *aBytes1 = theImage1.constBits();
184 const uchar *aBytes2 = theImage2.constBits();
185 int aBytesCountE = 0;
186 for( int i=0; i<aBytesCount; i++ )
187 if( aBytes1[i] != aBytes2[i] )
190 if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
196 bool TestViewer::AssertImages( QString& theMessage )
198 QImage anActualImage = viewWindow()->dumpView();
200 QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
201 anExpectedRefFilePath += "/" + myKey + ".png";
202 QImage anExpectedRefImage;
203 anExpectedRefImage.load( anExpectedRefFilePath );
205 if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
211 QString aPath = QDir::tempPath() + "/" + myKey + ".png";
212 anActualImage.save( aPath );
213 //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
214 theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
216 QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
217 QPainter aPainter( &aDiff );
218 aPainter.drawImage( 0, 0, anExpectedRefImage );
219 aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
220 aPainter.drawImage( 0, 0, anActualImage );
222 QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
223 aDiff.save( aDiffFilePath );
228 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
230 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
232 return Handle(Aspect_ColorScale)();
234 Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
235 if( aColorScale.IsNull() )
238 Standard_Real anXPos = 0.05; //TODO
239 Standard_Real anYPos = 0.1; //TODO
240 Standard_Real aWidth = 0.2; //TODO
241 Standard_Real aHeight = 0.5; //TODO
242 Standard_Integer aTextHeight = 14; //TODO
243 Standard_Integer aNbIntervals = 30; //TODO
245 aColorScale->SetXPosition( anXPos );
246 aColorScale->SetYPosition( anYPos );
247 aColorScale->SetWidth( aWidth );
248 aColorScale->SetHeight( aHeight );
249 aColorScale->SetTextHeight( aTextHeight );
250 aColorScale->SetNumberOfIntervals( aNbIntervals );
252 aColorScale->SetTitle( "test" );
253 aColorScale->SetRange( 0, 1 );
257 if( !aView->ColorScaleIsDisplayed() )
258 aView->ColorScaleDisplay();
262 if( aView->ColorScaleIsDisplayed() )
263 aView->ColorScaleErase();
268 void TestViewer::select( int theViewX, int theViewY )
270 Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
271 context()->MoveTo( theViewX, theViewY, aView );