Salome HOME
test for polyline presentation
[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   QString aNewKey = theKey;
93   if( !aNewKey.isEmpty() )
94   {
95     myKey = aNewKey;
96     context()->CloseLocalContext();
97     context()->EraseAll( Standard_False );
98   }
99   
100   context()->Display( theObject, theMode, theSelectionMode );
101   if( theSelectionMode > 0 )
102   {
103     context()->OpenLocalContext();
104     context()->Activate( theObject, theSelectionMode, Standard_True );
105   }
106
107   if( isFitAll )
108   {
109     viewWindow()->onTopView();
110     viewWindow()->onFitAll();
111   }
112 }
113
114 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
115 {
116   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
117   if( theShape.ShapeType()==TopAbs_VERTEX )
118     aShape->Attributes()->PointAspect()->SetTypeOfMarker( Aspect_TOM_X );
119   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
120   aShape->SetColor( HYDROData_Tool::toOccColor( theColor ) );
121   context()->Display( aShape, theMode, 0, Standard_False );
122
123   if( isFitAll )
124   {
125     viewWindow()->onTopView();
126     viewWindow()->onFitAll();
127   }
128 }
129
130 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
131 {
132   context()->CloseLocalContext();
133   context()->EraseAll( Standard_False );
134   
135   myKey = theKey;
136   int i = 0;
137   if( theShape.ShapeType()==TopAbs_SHELL )
138   {
139     TopoDS_Iterator anIt( theShape );
140     for( ; anIt.More(); anIt.Next(), i++ )
141       show( anIt.Value(), theMode, false, GetColor(i) );
142   }
143   else
144     show( theShape, theMode, false, GetColor(0) );
145
146   if( isFitAll )
147   {
148     viewWindow()->onTopView();
149     viewWindow()->onFitAll();
150   }
151 }
152
153 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
154 {
155   if( theImage1.isNull() || theImage2.isNull() )
156     return theImage1.isNull() == theImage2.isNull();
157
158   if( theImage1.size() != theImage2.size() )
159     return false;
160
161   int aBytesCount = theImage1.byteCount();
162   const uchar *aBytes1 = theImage1.constBits();
163   const uchar *aBytes2 = theImage2.constBits();
164   int aBytesCountE = 0;
165   for( int i=0; i<aBytesCount; i++ )
166     if( aBytes1[i] != aBytes2[i] )
167       aBytesCountE++;
168
169   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
170     return false;
171
172   return true;
173 }
174
175 bool TestViewer::AssertImages( QString& theMessage )
176 {
177   QImage anActualImage = viewWindow()->dumpView();
178
179   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
180   anExpectedRefFilePath += "/" + myKey + ".png";
181   QImage anExpectedRefImage; 
182   anExpectedRefImage.load( anExpectedRefFilePath );
183
184   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
185   {
186     theMessage = "";
187     return true;
188   }
189
190   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
191   anActualImage.save( aPath );
192   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
193   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
194   
195   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
196   QPainter aPainter( &aDiff );
197   aPainter.drawImage( 0, 0, anExpectedRefImage );
198   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
199   aPainter.drawImage( 0, 0, anActualImage );
200
201   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
202   aDiff.save( aDiffFilePath );
203
204   return false;
205 }
206
207 Handle_Aspect_ColorScale TestViewer::showColorScale( bool isShow )
208 {
209   Handle(V3d_View) aView = myViewWindow->getViewPort()->getView();
210   if( aView.IsNull() )
211     return Handle(Aspect_ColorScale)();
212
213   Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
214   if( aColorScale.IsNull() )
215     return aColorScale;
216
217   Standard_Real anXPos = 0.05; //TODO
218   Standard_Real anYPos = 0.1; //TODO
219   Standard_Real aWidth = 0.2; //TODO
220   Standard_Real aHeight = 0.5; //TODO
221   Standard_Integer aTextHeight = 14; //TODO
222   Standard_Integer aNbIntervals = 30; //TODO
223
224   aColorScale->SetXPosition( anXPos );
225   aColorScale->SetYPosition( anYPos );
226   aColorScale->SetWidth( aWidth );
227   aColorScale->SetHeight( aHeight );
228   aColorScale->SetTextHeight( aTextHeight );
229   aColorScale->SetNumberOfIntervals( aNbIntervals );
230
231   aColorScale->SetTitle( "test" );
232   aColorScale->SetRange( 0, 1 );
233
234   if( isShow )
235   {
236     if( !aView->ColorScaleIsDisplayed() )
237       aView->ColorScaleDisplay();
238   }
239   else
240   {
241     if( aView->ColorScaleIsDisplayed() )
242       aView->ColorScaleErase();
243   }
244   return aColorScale;
245 }