1 /*==================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestHyperTreeGridTernary3DContour.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 ===================================================================*/
15 // .SECTION Thanks
16 // This test was written by Philippe Pebay, 2016
17 // This work was supported by Commissariat a l'Energie Atomique (CEA/DIF)
18 
19 #include "vtkHyperTreeGridContour.h"
20 #include "vtkHyperTreeGridGeometry.h"
21 #include "vtkHyperTreeGridSource.h"
22 
23 #include "vtkCamera.h"
24 #include "vtkCellData.h"
25 #include "vtkPointData.h"
26 #include "vtkNew.h"
27 #include "vtkPolyData.h"
28 #include "vtkPolyDataMapper.h"
29 #include "vtkProperty.h"
30 #include "vtkRegressionTestImage.h"
31 #include "vtkRenderer.h"
32 #include "vtkRenderWindow.h"
33 #include "vtkRenderWindowInteractor.h"
34 
TestHyperTreeGridTernary3DContour(int argc,char * argv[])35 int TestHyperTreeGridTernary3DContour( int argc, char* argv[] )
36 {
37   // Hyper tree grid
38   vtkNew<vtkHyperTreeGridSource> htGrid;
39   int maxLevel = 5;
40   htGrid->SetMaximumLevel( maxLevel );
41   htGrid->SetGridSize( 3, 3, 2 );
42   htGrid->SetGridScale( 1.5, 1., .7 );
43   htGrid->SetDimension( 3 );
44   htGrid->SetBranchFactor( 3 );
45   htGrid->SetDescriptor( "RRR .R. .RR ..R ..R .R.|R.......................... ........................... ........................... .............R............. ....RR.RR........R......... .....RRRR.....R.RR......... ........................... ........................... ...........................|........................... ........................... ........................... ...RR.RR.......RR.......... ........................... RR......................... ........................... ........................... ........................... ........................... ........................... ........................... ........................... ............RRR............|........................... ........................... .......RR.................. ........................... ........................... ........................... ........................... ........................... ........................... ........................... ...........................|........................... ..........................." );
46 
47   // Contour
48   vtkNew<vtkHyperTreeGridContour> contour;
49   contour->SetInputConnection( htGrid->GetOutputPort() );
50   int nContours = 4;
51   contour->SetNumberOfContours( nContours );
52   contour->SetInputConnection( htGrid->GetOutputPort() );
53   double resolution = ( maxLevel - 1 ) / ( nContours + 1. );
54   double isovalue = resolution;
55   for ( int i = 0; i < nContours; ++ i, isovalue += resolution )
56   {
57     contour->SetValue( i, isovalue );
58   }
59 
60   // Geometry
61   vtkNew<vtkHyperTreeGridGeometry> geometry;
62   geometry->SetInputConnection( htGrid->GetOutputPort() );
63   geometry->Update();
64   vtkPolyData* pd = geometry->GetPolyDataOutput();
65 
66   // Mappers
67   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
68   vtkNew<vtkPolyDataMapper> mapper1;
69   mapper1->SetInputConnection( contour->GetOutputPort() );
70   mapper1->SetScalarRange( pd->GetCellData()->GetScalars()->GetRange() );
71   vtkNew<vtkPolyDataMapper> mapper2;
72   mapper2->SetInputConnection( contour->GetOutputPort() );
73   mapper2->ScalarVisibilityOff();
74   vtkNew<vtkPolyDataMapper> mapper3;
75   mapper3->SetInputConnection( geometry->GetOutputPort() );
76   mapper3->ScalarVisibilityOff();
77 
78   // Actors
79   vtkNew<vtkActor> actor1;
80   actor1->SetMapper( mapper1 );
81   vtkNew<vtkActor> actor2;
82   actor2->SetMapper( mapper2 );
83   actor2->GetProperty()->SetRepresentationToWireframe();
84   actor2->GetProperty()->SetColor( .3, .3, .3 );
85   actor2->GetProperty()->SetLineWidth( 1 );
86   vtkNew<vtkActor> actor3;
87   actor3->SetMapper( mapper3 );
88   actor3->GetProperty()->SetRepresentationToWireframe();
89   actor3->GetProperty()->SetColor( .7, .7, .7 );
90 
91   // Camera
92   double bd[6];
93   pd->GetBounds( bd );
94   vtkNew<vtkCamera> camera;
95   camera->SetClippingRange( 1., 100. );
96   camera->SetFocalPoint( pd->GetCenter() );
97   camera->SetPosition( -.8 * bd[1], 2.1 * bd[3], -4.8 * bd[5] );
98 
99   // Renderer
100   vtkNew<vtkRenderer> renderer;
101   renderer->SetActiveCamera( camera );
102   renderer->SetBackground( 1., 1., 1. );
103   renderer->AddActor( actor1 );
104   renderer->AddActor( actor2 );
105   renderer->AddActor( actor3 );
106 
107   // Render window
108   vtkNew<vtkRenderWindow> renWin;
109   renWin->AddRenderer( renderer );
110   renWin->SetSize( 400, 400 );
111   renWin->SetMultiSamples( 0 );
112 
113   // Interactor
114   vtkNew<vtkRenderWindowInteractor> iren;
115   iren->SetRenderWindow( renWin );
116 
117   // Render and test
118   renWin->Render();
119 
120   int retVal = vtkRegressionTestImageThreshold( renWin, 60 );
121   if ( retVal == vtkRegressionTester::DO_INTERACTOR )
122   {
123     iren->Start();
124   }
125 
126   return !retVal;
127 }
128