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