TestHyperTreeGridTernary2DFullMaterialBits.cxx 8.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*==================================================================

  Program:   Visualization Toolkit
  Module:    TestHyperTreeGridTernary2DMaterialBits.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

===================================================================*/
// .SECTION Thanks
// This test was written by Philippe Pebay and Joachim Pouderoux, Kitware 2013
// This work was supported in part by Commissariat a l'Energie Atomique (CEA/DIF)

#include "vtkHyperTreeGridGeometry.h"
#include "vtkHyperTreeGridSource.h"

#include "vtkCamera.h"
#include "vtkCellData.h"
#include "vtkContourFilter.h"
#include "vtkDataSetMapper.h"
#include "vtkNew.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkBitArray.h"
#include "vtkIdTypeArray.h"
#include "vtkPointData.h"
#include "vtkTimerLog.h"
37
#include "vtkScalarBarActor.h"
38
39
#include "vtkTextProperty.h"
#include "vtkProperty2D.h"
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <sstream>

void GenerateDescriptorAndMaterial(int depth, int sx, int sy, int sz, int branch,
                                   std::stringstream &d, std::stringstream &m)
{
  vtkIdType l = sx * sy * sz;
  vtkIdType s = 1;
  for ( int j = 0; j < depth; j++ )
    {
    for ( int i = 0; i < l; i++ )
      {
      if ( j > 0 && i%s == 0 )
        {
        d << " ";
        if ( j > 1 )
          {
          m << " ";
          }
        }
      d << "R";
      if ( j > 0 )
        {
        m << "1";
        }

      }
    s *= branch * branch;
    l *= branch * branch;
    d << " |";
    if ( j > 0 )
      {
      m << " |";
      }
    }

  for ( int i = 0; i < l; i++ )
    {
    if ( i%(s/branch) == 0 )
      {
      d << " ";
      m << " ";
      }
    d << ".";
    m << "1";
    }
}

void GenerateDescriptorAndMaterial(int depth, int sx, int sy, int sz, int branch,
                                  vtkBitArray *d, vtkBitArray* m)
{
  vtkIdType l = sx * sy * sz;
  vtkIdType s = 1;
  for ( int j = 0; j < depth - 1; j++ )
    {
    for ( int i = 0; i < l; i++ )
      {
      d->InsertNextValue(1);
      if ( j > 0 )
        {
        m->InsertNextValue(1);
        }
      }
    s *= branch * branch;
    l *= branch * branch;
    }

  for ( int i = 0; i < l; i++ )
    {
    d->InsertNextValue(0);
    m->InsertNextValue(1);
    }
  }

int TestHyperTreeGridTernary2DFullMaterialBits( int argc, char* argv[] )
{
  std::stringstream descriptor;
  std::stringstream materialMask;
  int sx = 10;
  int sy = 10;
  int sz = 1;
120
121
  int depth = 2;
  int branch = 3;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

  vtkNew<vtkTimerLog> timer;

  vtkNew<vtkHyperTreeGridSource> htGrid;
  int maxLevel = depth;
  htGrid->SetMaximumLevel( maxLevel );
  htGrid->SetGridSize( sx, sy, sz );
  htGrid->SetGridScale( 1., 1., 1. );
  htGrid->SetDimension( 2 );
  htGrid->SetBranchFactor( branch );
  htGrid->UseMaterialMaskOn();
  vtkNew<vtkIdTypeArray> zero;
  for ( int i = 0; i < sx * sy * sz; i++ )
    {
    zero->InsertNextValue(i);
    }
  htGrid->SetLevelZeroMaterialIndex( zero.GetPointer() );
  vtkNew<vtkBitArray> desc;
  vtkNew<vtkBitArray> mat;
  timer->StartTimer();
  cout << "Generating descriptors..." << endl;
  GenerateDescriptorAndMaterial( depth, sx, sy, sz, branch, desc.GetPointer(), mat.GetPointer() );
  timer->StopTimer();
  htGrid->SetDescriptorBits( desc.GetPointer() );
  htGrid->SetMaterialMaskBits( mat.GetPointer() );
  cout << " Done in " << timer->GetElapsedTime() << "s (" << desc->GetNumberOfTuples() << " nodes)" << endl;

  cout << "Constructing HTG " << sx << "x" << sy << "x" << sz << "  branch: " << branch << "  depth: " << depth << "..." << endl;
  timer->StartTimer();
  htGrid->Update();
  timer->StopTimer();
153
  vtkIdType nbCells = htGrid->GetOutput()->GetNumberOfCells();
154
155
156
157
158
159
  cout << " Done in " << timer->GetElapsedTime() << "s" << endl;
  cout << "#pts " << htGrid->GetOutput()->GetNumberOfPoints() << endl;
  timer->StartTimer();
  timer->StopTimer();
  cout << "#cells " << nbCells << endl;

160
161
  cout << "HTG takes " << htGrid->GetOutput()->GetActualMemorySize() << "KB in memory." << endl;

162
  // Prepare an array of ids
163
  vtkNew<vtkIdTypeArray> idArray;
164
165
166
167
  idArray->SetName( "Ids" );
  idArray->SetNumberOfComponents( 1 );
  vtkIdType nbPoints = htGrid->GetOutput()->GetNumberOfPoints();
  idArray->SetNumberOfValues( nbPoints );
168
  for ( vtkIdType i = 0; i < nbPoints; ++ i )
169
    {
170
171
    idArray->SetValue( i, i );
    }
172
  htGrid->GetOutput()->GetPointData()->SetScalars( idArray.GetPointer() );
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

  // Geometry
  cout << "Constructing geometry..." << endl;
  timer->StartTimer();
  vtkNew<vtkHyperTreeGridGeometry> geometry;
  geometry->SetInputConnection( htGrid->GetOutputPort() );
  geometry->Update();
  vtkPolyData* pd = geometry->GetOutput();
  timer->StopTimer();
  cout << " Done in " << timer->GetElapsedTime() << "s" << endl;

  // Mappers
  vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
  vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters( 1, 1 );
  vtkNew<vtkPolyDataMapper> mapper1;
  mapper1->SetInputConnection( geometry->GetOutputPort() );
  mapper1->SetScalarRange( pd->GetCellData()->GetScalars()->GetRange() );
  vtkNew<vtkPolyDataMapper> mapper2;
  mapper2->SetInputConnection( geometry->GetOutputPort() );
  mapper2->ScalarVisibilityOff();
  vtkNew<vtkDataSetMapper> mapper3;
  mapper3->SetInputConnection( htGrid->GetOutputPort() );
  mapper3->ScalarVisibilityOff();
  vtkNew<vtkDataSetMapper> mapper4;
  mapper4->SetInputConnection( htGrid->GetOutputPort() );
  mapper4->ScalarVisibilityOff();

  // Actors
  vtkNew<vtkActor> actor1;
  actor1->SetMapper( mapper1.GetPointer() );
  vtkNew<vtkActor> actor2;
  actor2->SetMapper( mapper2.GetPointer() );
  actor2->GetProperty()->SetRepresentationToWireframe();
  actor2->GetProperty()->SetColor( .7, .7, .7 );
207

208
209
210
211
212
213
214
215
216
217
  vtkNew<vtkActor> actor3;
  actor3->SetMapper( mapper3.GetPointer() );
  actor3->GetProperty()->SetRepresentationToWireframe();
  actor3->GetProperty()->SetColor( .0, .0, .0 );
  vtkNew<vtkActor> actor4;
  actor4->SetMapper( mapper4.GetPointer() );
  actor4->GetProperty()->SetRepresentationToPoints();
  actor4->GetProperty()->SetPointSize(4);
  actor4->GetProperty()->SetColor( 0., 1., 0. );

218
219
  vtkNew<vtkScalarBarActor> scalarBar;
  scalarBar->SetLookupTable(mapper1->GetLookupTable());
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  scalarBar->SetLabelFormat("%.0f");
  scalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
  scalarBar->GetPositionCoordinate()->SetValue( .80, .32 );
  scalarBar->SetTitle( "  id  " );
  scalarBar->SetNumberOfLabels( 4 );
  scalarBar->SetWidth( 0.15 );
  scalarBar->SetHeight( 0.4 );
  scalarBar->SetTextPad( 4 );
  scalarBar->SetMaximumWidthInPixels( 60 );
  scalarBar->SetMaximumHeightInPixels( 200 );
  scalarBar->SetTextPositionToPrecedeScalarBar();
  scalarBar->GetTitleTextProperty()->SetColor( .4, .4, .4 );
  scalarBar->GetLabelTextProperty()->SetColor( .4, .4, .4 );
  scalarBar->SetDrawFrame( 1 );
  scalarBar->GetFrameProperty()->SetColor( .4, .4, .4 );
  scalarBar->SetDrawBackground( 1 );
  scalarBar->GetBackgroundProperty()->SetColor( 1., 1., 1. );
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  // Camera
  double bd[6];
  pd->GetBounds( bd );
  vtkNew<vtkCamera> camera;
  camera->SetClippingRange( 1., 100. );
  camera->SetFocalPoint( pd->GetCenter() );
  camera->SetPosition( .5 * bd[1], .5 * bd[3], 2 * bd[1] );

  // Renderer
  vtkNew<vtkRenderer> renderer;
  renderer->SetActiveCamera( camera.GetPointer() );
  renderer->SetBackground( 1., 1., 1. );
  renderer->AddActor( actor1.GetPointer() );
  renderer->AddActor( actor2.GetPointer() );
  renderer->AddActor( actor3.GetPointer() );
  renderer->AddActor( actor4.GetPointer() );
254
  renderer->AddActor2D( scalarBar.GetPointer() );
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

  // Render window
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer( renderer.GetPointer() );
  renWin->SetSize( 400, 400 );
  renWin->SetMultiSamples( 0 );

  // Interactor
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow( renWin.GetPointer() );

  // Render and test
  renWin->Render();

  int retVal = vtkRegressionTestImageThreshold( renWin.GetPointer(), 70 );
  if ( retVal == vtkRegressionTester::DO_INTERACTOR )
    {
    iren->Start();
    }

  return !retVal;
}