1 /*==================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestHyperTreeGridTernaryHyperbola.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, Kitware 2012
17 // This test was revised by Philippe Pebay, 2016
18 // This work was supported by Commissariat a l'Energie Atomique (CEA/DIF)
19 
20 #include "vtkHyperTreeGridGeometry.h"
21 #include "vtkHyperTreeGridSource.h"
22 
23 #include "vtkCamera.h"
24 #include "vtkCellData.h"
25 #include "vtkColorTransferFunction.h"
26 #include "vtkContourFilter.h"
27 #include "vtkMath.h"
28 #include "vtkNew.h"
29 #include "vtkPolyDataMapper.h"
30 #include "vtkProperty.h"
31 #include "vtkProperty2D.h"
32 #include "vtkRegressionTestImage.h"
33 #include "vtkRenderer.h"
34 #include "vtkRenderWindow.h"
35 #include "vtkRenderWindowInteractor.h"
36 #include "vtkScalarBarActor.h"
37 #include "vtkTextProperty.h"
38 #include "vtkQuadric.h"
39 
TestHyperTreeGridTernaryHyperbola(int argc,char * argv[])40 int TestHyperTreeGridTernaryHyperbola( int argc, char* argv[] )
41 {
42   // Hyper tree grid
43   vtkNew<vtkHyperTreeGridSource> htGrid;
44   htGrid->SetMaximumLevel( 6 );
45   htGrid->SetGridSize( 8, 12, 1 );
46   htGrid->SetGridScale( 1.5, 1., .7 );
47   htGrid->SetDimension( 2 );
48   htGrid->SetOrientation( 2 ); // in xy plane
49   htGrid->SetBranchFactor( 3 );
50   htGrid->UseDescriptorOff();
51   htGrid->UseMaterialMaskOff();
52   vtkNew<vtkQuadric> quadric;
53   quadric->SetCoefficients( 1., -1., 0.,
54                             0., 0., 0.,
55                             -12., 12., 0.,
56                             1. );
57   htGrid->SetQuadric( quadric );
58 
59   // Geometry
60   vtkNew<vtkHyperTreeGridGeometry> geometry;
61   geometry->SetInputConnection( htGrid->GetOutputPort() );
62   geometry->Update();
63   vtkPolyData* pd = geometry->GetPolyDataOutput();
64   pd->GetCellData()->SetActiveScalars( "Quadric" );
65 
66   // Contour
67   vtkNew<vtkContourFilter> contour;
68   contour->SetInputConnection( htGrid->GetOutputPort() );
69   contour->SetNumberOfContours( 0 );
70   contour->SetValue( 0, 0 );
71   contour->SetInputArrayToProcess( 0, 0, 0,
72                                    vtkDataObject::FIELD_ASSOCIATION_POINTS,
73                                    "Quadric" );
74   //  Color transfer function
75   vtkNew<vtkColorTransferFunction> colorFunction;
76   colorFunction->AddRGBSegment( -30., 0., 0., 1.,
77                                 0.,  0., 1., 1.);
78   colorFunction->AddRGBSegment( VTK_DBL_MIN,  1., 1., 0.,
79                                 30., 1., 0., 0.);
80 
81   // Mappers
82   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
83   vtkNew<vtkPolyDataMapper> mapper1;
84   mapper1->SetInputConnection( geometry->GetOutputPort() );
85   mapper1->UseLookupTableScalarRangeOn();
86   mapper1->SetLookupTable( colorFunction );
87   vtkNew<vtkPolyDataMapper> mapper2;
88   mapper2->SetInputConnection( geometry->GetOutputPort() );
89   mapper2->ScalarVisibilityOff();
90   vtkNew<vtkPolyDataMapper> mapper3;
91   mapper3->SetInputConnection( contour->GetOutputPort() );
92   mapper3->ScalarVisibilityOff();
93 
94   // Actors
95   vtkNew<vtkActor> actor1;
96   actor1->SetMapper( mapper1 );
97   vtkNew<vtkActor> actor2;
98   actor2->SetMapper( mapper2 );
99   actor2->GetProperty()->SetRepresentationToWireframe();
100   actor2->GetProperty()->SetColor( .7, .7, .7 );
101   vtkNew<vtkActor> actor3;
102   actor3->SetMapper( mapper3 );
103   actor3->GetProperty()->SetColor( 0., 0., 0. );
104   actor3->GetProperty()->SetLineWidth( 2 );
105 
106   // Camera
107   double bd[6];
108   pd->GetBounds( bd );
109   vtkNew<vtkCamera> camera;
110   camera->SetClippingRange( 1., 100. );
111   camera->SetFocalPoint( pd->GetCenter() );
112   camera->SetPosition( .5 * bd[1], .5 * bd[3], 24. );
113 
114   // Scalar bar
115   vtkNew<vtkScalarBarActor> scalarBar;
116   scalarBar->SetLookupTable( colorFunction );
117   scalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
118   scalarBar->GetPositionCoordinate()->SetValue( .65, .05 );
119   scalarBar->SetTitle( "Quadric" );
120   scalarBar->SetWidth( 0.15 );
121   scalarBar->SetHeight( 0.4 );
122   scalarBar->SetTextPad( 4 );
123   scalarBar->SetMaximumWidthInPixels( 60 );
124   scalarBar->SetMaximumHeightInPixels( 200 );
125   scalarBar->SetTextPositionToPrecedeScalarBar();
126   scalarBar->GetTitleTextProperty()->SetColor( .4, .4, .4 );
127   scalarBar->GetLabelTextProperty()->SetColor( .4, .4, .4 );
128   scalarBar->SetDrawFrame( 1 );
129   scalarBar->GetFrameProperty()->SetColor( .4, .4, .4 );
130   scalarBar->SetDrawBackground( 1 );
131   scalarBar->GetBackgroundProperty()->SetColor( 1., 1., 1. );
132 
133   // Renderer
134   vtkNew<vtkRenderer> renderer;
135   renderer->SetActiveCamera( camera );
136   renderer->SetBackground( 1., 1., 1. );
137   renderer->AddActor( actor1 );
138   renderer->AddActor( actor2 );
139   renderer->AddActor( actor3 );
140   renderer->AddActor( scalarBar );
141 
142   // Render window
143   vtkNew<vtkRenderWindow> renWin;
144   renWin->AddRenderer( renderer );
145   renWin->SetSize( 400, 400 );
146   renWin->SetMultiSamples( 0 );
147 
148   // Interactor
149   vtkNew<vtkRenderWindowInteractor> iren;
150   iren->SetRenderWindow( renWin );
151 
152   // Render and test
153   renWin->Render();
154 
155   int retVal = vtkRegressionTestImageThreshold( renWin, 70 );
156   if ( retVal == vtkRegressionTester::DO_INTERACTOR )
157   {
158     iren->Start();
159   }
160 
161   return !retVal;
162 }
163