Salome HOME
refs #624: test for split polylines
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1
2 #include <TestViewer.h>
3 #include <HYDROData_Tool.h>
4 #include <OCCViewer_ViewManager.h>
5 #ifdef WIN32
6   #pragma warning ( disable: 4251 )
7 #endif
8 #include <OCCViewer_ViewPort3d.h>
9 #ifdef WIN32
10   #pragma warning ( disable: 4251 )
11 #endif
12 #include <OCCViewer_ViewModel.h>
13 #ifdef WIN32
14   #pragma warning ( disable: 4251 )
15 #endif
16 #include <OCCViewer_ViewWindow.h>
17 #ifdef WIN32
18   #pragma warning ( disable: 4251 )
19 #endif
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_Shape.hxx>
22 #include <Aspect_ColorScale.hxx>
23 #include <Prs3d_PointAspect.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <QDir>
26 #include <QPainter>
27 #include <QHash>
28
29 #ifdef WIN32
30   #pragma warning ( default: 4251 )
31 #endif
32
33 #include <cppunit/TestAssert.h>
34
35 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
36 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
37 QString TestViewer::myKey = "";
38
39 OCCViewer_ViewManager* TestViewer::viewManager()
40 {
41   if( myViewManager )
42     return myViewManager;
43
44   myViewManager = new OCCViewer_ViewManager( 0, 0 );
45   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
46
47   aViewer->setTrihedronSize( 100, true );
48   aViewer->setInteractionStyle( 0 );
49   aViewer->setZoomingStyle( 1 );
50
51   myViewManager->setViewModel( aViewer );
52   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
53
54   return myViewManager;
55 }
56
57 OCCViewer_Viewer* TestViewer::viewer()
58 {
59   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
60 }
61
62 OCCViewer_ViewWindow* TestViewer::viewWindow()
63 {
64   viewManager(); //to create the view if it was not created earlier
65   return myViewWindow;
66 }
67
68 Handle(AIS_InteractiveContext) context()
69 {
70   return TestViewer::viewer()->getAISContext();
71 }
72
73 QColor TestViewer::GetColor(int i)
74 {
75   static QVector<QColor> aCV;
76   if( aCV.isEmpty() )
77   {
78     aCV  << QColor(0,0,255) 
79          << QColor(0,255,0)
80          << QColor(255,0,0)
81          << QColor(255,255,20) 
82          << QColor(20,255,255) 
83          << QColor(100,100,20) 
84          << QColor(10,100,150);
85   }
86   return aCV[i];
87 }
88
89 void TestViewer::show( const Handle(AIS_InteractiveObject)& theObject,
90                        int theMode, int theSelectionMode, bool isFitAll, const char* theKey )
91 {
92   context()->CloseLocalContext();
93   context()->EraseAll( Standard_False );
94   if( theSelectionMode > 0 )
95   {
96     context()->Display( theObject, theMode, theSelectionMode );
97     context()->OpenLocalContext();
98     context()->Activate( theObject, theSelectionMode, Standard_True );
99   }
100
101   myKey = theKey;
102
103   if( isFitAll )
104   {
105     viewWindow()->onTopView();
106     viewWindow()->onFitAll();
107   }
108 }
109
110 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
111 {
112   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
113   if( theShape.ShapeType()==TopAbs_VERTEX )
114     aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
115   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
116   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
117   context()->Display( aShape, theMode, 0, Standard_False );
118
119   if( isFitAll )
120   {
121     viewWindow()->onTopView();
122     viewWindow()->onFitAll();
123   }
124 }
125
126 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
127 {
128   context()->EraseAll( Standard_False );
129   
130   myKey = theKey;
131   int i = 0;
132   if( theShape.ShapeType()==TopAbs_SHELL )
133   {
134     TopoDS_Iterator anIt( theShape );
135     for( ; anIt.More(); anIt.Next(), i++ )
136       show( anIt.Value(), theMode, false, GetColor(i) );
137   }
138   else
139     show( theShape, theMode, false, GetColor(0) );
140
141   if( isFitAll )
142   {
143     viewWindow()->onTopView();
144     viewWindow()->onFitAll();
145   }
146 }
147
148 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
149 {
150   if( theImage1.isNull() || theImage2.isNull() )
151     return theImage1.isNull() == theImage2.isNull();
152
153   if( theImage1.size() != theImage2.size() )
154     return false;
155
156   int aBytesCount = theImage1.byteCount();
157   const uchar *aBytes1 = theImage1.constBits();
158   const uchar *aBytes2 = theImage2.constBits();
159   int aBytesCountE = 0;
160   for( int i=0; i<aBytesCount; i++ )
161     if( aBytes1[i] != aBytes2[i] )
162       aBytesCountE++;
163
164   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
165     return false;
166
167   return true;
168 }
169
170 bool TestViewer::AssertImages( QString& theMessage )
171 {
172   QImage anActualImage = viewWindow()->dumpView();
173
174   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
175   anExpectedRefFilePath += "/" + myKey + ".png";
176   QImage anExpectedRefImage; 
177   anExpectedRefImage.load( anExpectedRefFilePath );
178
179   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
180   {
181     theMessage = "";
182     return true;
183   }
184
185   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
186   anActualImage.save( aPath );
187   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
188   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
189   
190   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
191   QPainter aPainter( &aDiff );
192   aPainter.drawImage( 0, 0, anExpectedRefImage );
193   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
194   aPainter.drawImage( 0, 0, anActualImage );
195
196   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
197   aDiff.save( aDiffFilePath );
198
199   return false;
200 }
201
202 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
203 {
204   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
205   if( aView.IsNull() )
206     return Handle(Aspect_ColorScale)();
207
208   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
209   if( aColorScale.IsNull() )
210     return aColorScale;
211
212   Standard_Real anXPos = 0.05; //TODO
213   Standard_Real anYPos = 0.1; //TODO
214   Standard_Real aWidth = 0.2; //TODO
215   Standard_Real aHeight = 0.5; //TODO
216   Standard_Integer aTextHeight = 14; //TODO
217   Standard_Integer aNbIntervals = 30; //TODO
218
219   aColorScale->SetXPosition( anXPos );
220   aColorScale->SetYPosition( anYPos );
221   aColorScale->SetWidth( aWidth );
222   aColorScale->SetHeight( aHeight );
223   aColorScale->SetTextHeight( aTextHeight );
224   aColorScale->SetNumberOfIntervals( aNbIntervals );
225
226   aColorScale->SetTitle( "test" );
227   aColorScale->SetRange( 0, 1 );
228
229   if( isShow )
230   {
231     if( !aView->ColorScaleIsDisplayed() )
232       aView->ColorScaleDisplay();
233   }
234   else
235   {
236     if( aView->ColorScaleIsDisplayed() )
237       aView->ColorScaleErase();
238   }
239   return aColorScale;
240 }