Salome HOME
5863579a732aac015c4724685a06bb26d600b73f
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1
2 #include <TestViewer.h>
3 //#include <random.h>
4 #include <OCCViewer_ViewManager.h>
5 #ifdef WIN32
6   #pragma warning ( disable: 4251 )
7 #endif
8 #include <OCCViewer_ViewModel.h>
9 #ifdef WIN32
10   #pragma warning ( disable: 4251 )
11 #endif
12 #include <OCCViewer_ViewWindow.h>
13 #ifdef WIN32
14   #pragma warning ( disable: 4251 )
15 #endif
16 #include <AIS_InteractiveContext.hxx>
17 #include <AIS_Shape.hxx>
18 #include <TopoDS_Iterator.hxx>
19
20 #include <QDir>
21 #include <QPainter>
22 #include <QHash>
23
24 #ifdef WIN32
25   #pragma warning ( default: 4251 )
26 #endif
27
28 #include <cppunit/TestAssert.h>
29
30 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
31 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
32 QString TestViewer::myKey = "";
33
34 OCCViewer_ViewManager* TestViewer::viewManager()
35 {
36   if( myViewManager )
37     return myViewManager;
38
39   myViewManager = new OCCViewer_ViewManager( 0, 0 );
40   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
41
42   aViewer->setTrihedronSize( 100, true );
43   aViewer->setInteractionStyle( 0 );
44   aViewer->setZoomingStyle( 1 );
45
46   myViewManager->setViewModel( aViewer );
47   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
48
49   return myViewManager;
50 }
51
52 OCCViewer_Viewer* TestViewer::viewer()
53 {
54   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
55 }
56
57 OCCViewer_ViewWindow* TestViewer::viewWindow()
58 {
59   viewManager(); //to create the view if it was not created earlier
60   return myViewWindow;
61 }
62
63 Handle(AIS_InteractiveContext) context()
64 {
65   return TestViewer::viewer()->getAISContext();
66 }
67
68 QColor TestViewer::GetColor(int i)
69 {
70   static QVector<QColor> aCV;
71   if (aCV.isEmpty())
72   {
73     aCV  << QColor(0,0,255) 
74       << QColor(0,255,0)
75       << QColor(255,0,0)
76       << QColor(255,255,20) 
77       << QColor(20,255,255) 
78       << QColor(100,100,20) 
79       << QColor(10,100,150);
80   }
81   return aCV[i];
82 }
83
84 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const QColor& theColor )
85 {
86   double r = theColor.red() / 255.0;
87   double g = theColor.green() / 255.0;
88   double b = theColor.blue() / 255.0;
89
90   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
91   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
92   aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
93   context()->Display( aShape, theMode, 0, Standard_False );
94
95   if( isFitAll )
96   {
97     viewWindow()->onTopView();
98     viewWindow()->onFitAll();
99   }
100 }
101
102 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll, const char* theKey )
103 {
104   context()->EraseAll();
105   
106   myKey = theKey;
107   int i = 0;
108   if( theShape.ShapeType()==TopAbs_SHELL )
109   {
110     TopoDS_Iterator anIt( theShape );
111     for( ; anIt.More(); anIt.Next(), i++ )
112       show( anIt.Value(), theMode, false, GetColor(i) );
113   }
114   else
115     show( theShape, theMode, false, GetColor(0) );
116
117   if( isFitAll )
118   {
119     viewWindow()->onTopView();
120     viewWindow()->onFitAll();
121   }
122 }
123
124 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance )
125 {
126   if( theImage1.isNull() || theImage2.isNull() )
127     return theImage1.isNull() == theImage2.isNull();
128
129   if( theImage1.size() != theImage2.size() )
130     return false;
131
132   int aBytesCount = theImage1.byteCount();
133   const uchar *aBytes1 = theImage1.constBits();
134   const uchar *aBytes2 = theImage2.constBits();
135   int aBytesCountE = 0;
136   for( int i=0; i<aBytesCount; i++ )
137     if( aBytes1[i] != aBytes2[i] )
138       aBytesCountE++;
139
140   if ((double)aBytesCountE / (double)aBytesCount > theTolerance)
141     return false;
142
143   return true;
144 }
145
146 bool TestViewer::AssertImages( QString& theMessage )
147 {
148   QImage anActualImage = viewWindow()->dumpView();
149
150   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
151   anExpectedRefFilePath += "/" + myKey + ".png";
152   QImage anExpectedRefImage; 
153   anExpectedRefImage.load( anExpectedRefFilePath );
154
155   if( AreImagesEqual( anActualImage, anExpectedRefImage, 0.001 ) )
156   {
157     theMessage = "";
158     return true;
159   }
160
161   QString aPath = QDir::tempPath() + "/" + myKey + ".png";
162   anActualImage.save( aPath );
163   //std::cout << anActualImage.width() << "x" << anActualImage.height() << std::endl;
164   theMessage = "The viewer contents does not correspond to the reference image: " + myKey;
165   
166   QImage aDiff( anExpectedRefImage.width(), anExpectedRefImage.height(), QImage::Format_ARGB32 );
167   QPainter aPainter( &aDiff );
168   aPainter.drawImage( 0, 0, anExpectedRefImage );
169   aPainter.setCompositionMode( QPainter::RasterOp_SourceXorDestination );
170   aPainter.drawImage( 0, 0, anActualImage );
171
172   QString aDiffFilePath = QDir::tempPath() + "/" + myKey + "_diff.png";
173   aDiff.save( aDiffFilePath );
174
175   return false;
176 }