Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1
2 #include <TestViewer.h>
3 #include <OCCViewer_ViewManager.h>
4 #include <OCCViewer_ViewModel.h>
5 #include <OCCViewer_ViewWindow.h>
6 #include <AIS_InteractiveContext.hxx>
7 #include <AIS_Shape.hxx>
8 #include <TopoDS_Iterator.hxx>
9 #include <QDir>
10 #include <cppunit/TestAssert.h>
11
12 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
13 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
14
15 OCCViewer_ViewManager* TestViewer::viewManager()
16 {
17   if( myViewManager )
18     return myViewManager;
19
20   myViewManager = new OCCViewer_ViewManager( 0, 0 );
21   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
22
23   aViewer->setTrihedronSize( 100, true );
24   aViewer->setInteractionStyle( 0 );
25   aViewer->setZoomingStyle( 1 );
26
27   myViewManager->setViewModel( aViewer );
28   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
29
30   return myViewManager;
31 }
32
33 OCCViewer_Viewer* TestViewer::viewer()
34 {
35   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
36 }
37
38 OCCViewer_ViewWindow* TestViewer::viewWindow()
39 {
40   viewManager(); //to create the view if it was not created earlier
41   return myViewWindow;
42 }
43
44 Handle(AIS_InteractiveContext) context()
45 {
46   return TestViewer::viewer()->getAISContext();
47 }
48
49 void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
50 {
51   QColor aColor = theColor;
52   if( !aColor.isValid() )
53   {
54     // random color
55     int aHue = rand()%255;
56     aColor = QColor::fromHsl( aHue, 255, 128 );
57   }
58
59   double r = aColor.red() / 255.0;
60   double g = aColor.green() / 255.0;
61   double b = aColor.blue() / 255.0;
62
63   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
64   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
65   aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
66   context()->Display( aShape, theMode, 0, Standard_False );
67 }
68
69 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
70 {
71   context()->EraseAll();
72   
73   if( theShape.ShapeType()==TopAbs_COMPOUND )
74   {
75     TopoDS_Iterator anIt( theShape );
76     for( ; anIt.More(); anIt.Next() )
77       show( anIt.Value(), QColor(), theMode );
78   }
79   else
80     show( theShape, QColor(), theMode );
81
82   viewWindow()->onTopView();
83   viewWindow()->onFitAll();
84 }
85
86 bool AreImagesEqual( const QImage& theImage1, const QImage& theImage2, double theTolerance = 0.0 )
87 {
88   if( theImage1.isNull() || theImage2.isNull() )
89     return theImage1.isNull() == theImage2.isNull();
90
91   if( theImage1.size() != theImage2.size() )
92     return false;
93
94   int aBytesCount = theImage1.byteCount();
95   const uchar *aBytes1 = theImage1.constBits();
96   const uchar *aBytes2 = theImage2.constBits();
97   for( int i=0; i<aBytesCount; i++ )
98     if( aBytes1[i] != aBytes2[i] )
99       return false;
100
101   return true;
102 }
103
104 bool TestViewer::AssertEqual( const QString& theUseCaseName )
105 {
106   QImage anActualImage = viewWindow()->dumpView();
107
108   QString anExpectedRefFilePath = qgetenv( "HYDRO_REFERENCE_DATA" );
109   anExpectedRefFilePath += "/" + theUseCaseName + ".png";
110   QImage anExpectedRefImage; 
111   anExpectedRefImage.load( anExpectedRefFilePath );
112
113   if( AreImagesEqual( anActualImage, anExpectedRefImage ) )
114     return true;
115
116   QString aPath = QDir::tempPath() + "/" + theUseCaseName + ".png";
117   anActualImage.save( aPath );
118   std::string aMessage = "The viewer contents does not correspond to the reference image: " + theUseCaseName.toStdString();
119   CPPUNIT_FAIL( aMessage.c_str() );
120   return false;
121 }