Salome HOME
copyrights are updated
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <TestViewer.h>
20 #include <HYDROData_Tool.h>
21 #include <OCCViewer_ViewManager.h>
22 #ifdef WIN32
23   #pragma warning ( disable: 4251 )
24 #endif
25 #include <OCCViewer_ViewPort3d.h>
26 #ifdef WIN32
27   #pragma warning ( disable: 4251 )
28 #endif
29 #include <OCCViewer_ViewModel.h>
30 #ifdef WIN32
31   #pragma warning ( disable: 4251 )
32 #endif
33 #include <OCCViewer_ViewWindow.h>
34 #ifdef WIN32
35   #pragma warning ( disable: 4251 )
36 #endif
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>
42 #include <QDir>
43 #include <QPainter>
44 #include <QHash>
45
46 #ifdef WIN32
47   #pragma warning ( default: 4251 )
48 #endif
49
50 #include <cppunit/TestAssert.h>
51
52 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
53 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
54 QString TestViewer::myKey = "";
55
56 OCCViewer_ViewManager* TestViewer::viewManager()
57 {
58   if( myViewManager )
59     return myViewManager;
60
61   myViewManager = new OCCViewer_ViewManager( 0, 0 );
62   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
63
64   aViewer->setTrihedronSize( 100, true );
65   aViewer->setInteractionStyle( 0 );
66   aViewer->setZoomingStyle( 1 );
67
68   myViewManager->setViewModel( aViewer );
69   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
70
71   return myViewManager;
72 }
73
74 OCCViewer_Viewer* TestViewer::viewer()
75 {
76   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
77 }
78
79 OCCViewer_ViewWindow* TestViewer::viewWindow()
80 {
81   viewManager(); //to create the view if it was not created earlier
82   return myViewWindow;
83 }
84
85 Handle(AIS_InteractiveContext) context()
86 {
87   return TestViewer::viewer()->getAISContext();
88 }
89
90 QColor TestViewer::GetColor(int i)
91 {
92   static QVector<QColor> aCV;
93   if( aCV.isEmpty() )
94   {
95     aCV  << QColor(0,0,255) 
96          << QColor(0,255,0)
97          << QColor(255,0,0)
98          << QColor(255,255,20) 
99          << QColor(20,255,255) 
100          << QColor(100,100,20) 
101          << QColor(10,100,150);
102   }
103   return aCV[i];
104 }
105
106 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
107                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
108 {
109   QString aNewKey = theKey;
110   if( !aNewKey.isEmpty() )
111   {
112     myKey = aNewKey;
113     context()->CloseLocalContext();
114     context()->EraseAll( Standard_False );
115   }
116   
117   context()->Display( theObject, theMode, theSelectionMode );
118   if( theSelectionMode > 0 )
119   {
120     context()->OpenLocalContext();
121     context()->Activate( theObject, theSelectionMode, Standard_True );
122   }
123
124   if( isFitAll )
125   {
126     viewWindow()->onTopView();
127     viewWindow()->onFitAll();
128   }
129 }
130
131 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
132 {
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 );
139
140   if( isFitAll )
141   {
142     viewWindow()->onTopView();
143     viewWindow()->onFitAll();
144   }
145 }
146
147 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
148 {
149   context()->CloseLocalContext();
150   context()->EraseAll( Standard_False );
151   
152   myKey = theKey;
153
154   if( theShape.IsNull() )
155     return;
156
157   int i = 0;
158   if( theShape.ShapeType()==TopAbs_SHELL )
159   {
160     TopoDS_Iterator anIt( theShape );
161     for( ; anIt.More(); anIt.Next(), i++ )
162       show( anIt.Value(), theMode, false, GetColor(i) );
163   }
164   else
165     show( theShape, theMode, false, GetColor(0) );
166
167   if( isFitAll )
168   {
169     viewWindow()->onTopView();
170     viewWindow()->onFitAll();
171   }
172 }
173
174 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
175 {
176   if( theImage1.isNull() || theImage2.isNull() )
177     return theImage1.isNull() == theImage2.isNull();
178
179   if( theImage1.size() != theImage2.size() )
180     return false;
181
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] )
188       aBytesCountE++;
189
190   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
191     return false;
192
193   return true;
194 }
195
196 bool TestViewer::AssertImages( QString& theMessage )
197 {
198   QImage anActualImage = viewWindow()->dumpView();
199
200   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
201   anExpectedRefFilePath += "/" + myKey + ".png";
202   QImage anExpectedRefImage; 
203   anExpectedRefImage.load( anExpectedRefFilePath );
204
205   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
206   {
207     theMessage = "";
208     return true;
209   }
210
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;
215   
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 );
221
222   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
223   aDiff.save( aDiffFilePath );
224
225   return false;
226 }
227
228 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
229 {
230   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
231   if( aView.IsNull() )
232     return Handle(Aspect_ColorScale)();
233
234   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
235   if( aColorScale.IsNull() )
236     return aColorScale;
237
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
244
245   aColorScale->SetXPosition( anXPos );
246   aColorScale->SetYPosition( anYPos );
247   aColorScale->SetWidth( aWidth );
248   aColorScale->SetHeight( aHeight );
249   aColorScale->SetTextHeight( aTextHeight );
250   aColorScale->SetNumberOfIntervals( aNbIntervals );
251
252   aColorScale->SetTitle( "test" );
253   aColorScale->SetRange( 0, 1 );
254
255   if( isShow )
256   {
257     if( !aView->ColorScaleIsDisplayed() )
258       aView->ColorScaleDisplay();
259   }
260   else
261   {
262     if( aView->ColorScaleIsDisplayed() )
263       aView->ColorScaleErase();
264   }
265   return aColorScale;
266 }
267
268 void TestViewer::select( int theViewX, int theViewY )
269 {
270   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
271   context()->MoveTo( theViewX, theViewY, aView );
272   context()->Select();
273 }