File: vtkPythonExtractSelection.cxx

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (344 lines) | stat: -rw-r--r-- 11,320 bytes parent folder | download
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
37
38
39
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
120
121
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*=========================================================================

  Program:   ParaView
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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.

=========================================================================*/
#include "vtkPythonExtractSelection.h"

#include "vtkAbstractArray.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataObjectTypes.h"
#include "vtkExtractSelectedIds.h"
#include "vtkExtractSelectedRows.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPythonInterpreter.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
#include "vtkUnstructuredGrid.h"

#include <cassert>
#include <sstream>

vtkStandardNewMacro(vtkPythonExtractSelection);
//----------------------------------------------------------------------------
vtkPythonExtractSelection::vtkPythonExtractSelection()
{
}

//----------------------------------------------------------------------------
vtkPythonExtractSelection::~vtkPythonExtractSelection()
{
}

//----------------------------------------------------------------------------
int vtkPythonExtractSelection::FillInputPortInformation(int port, vtkInformation *info)
{
  if (port == 0)
    {
    // This filter handles composite datasets, datasets and table. Not graphs and others.
    info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
    info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet");
    info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
    info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
    }
  else
    {
    assert(port == 1);
    info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
    info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
    }
  return 1;
}

//----------------------------------------------------------------------------
int vtkPythonExtractSelection::RequestDataObject(
  vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // Output type is same as input
  vtkDataObject *input = vtkDataObject::GetData(inputVector[0], 0);
  if (input)
    {
    const char* outputType = NULL;
    if (this->PreserveTopology)
      {
      outputType = input->GetClassName();
      }
    else
      {
      outputType = "vtkUnstructuredGrid";
      if (vtkCompositeDataSet::SafeDownCast(input))
        {
        outputType = "vtkMultiBlockDataSet";
        }
      else if (vtkTable::SafeDownCast(input))
        {
        outputType = "vtkTable";
        }
      }
    vtkInformation* info = outputVector->GetInformationObject(0);
    vtkDataObject *output = info->Get(vtkDataObject::DATA_OBJECT());
    if (!output || !output->IsA(outputType))
      {
      vtkDataObject* newOutput =
        vtkDataObjectTypes::NewDataObject(outputType);
      info->Set(vtkDataObject::DATA_OBJECT(), newOutput);
      newOutput->Delete();
      this->GetOutputPortInformation(0)->Set(
        vtkDataObject::DATA_EXTENT_TYPE(), newOutput->GetExtentType());
      }
    return 1;
    }
  return 0;
}

//----------------------------------------------------------------------------
int vtkPythonExtractSelection::RequestData(
  vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // if not selection is specified, return.
  if (inputVector[1]->GetNumberOfInformationObjects() == 0)
    {
    return 1;
    }

  vtkDataObject* input = vtkDataObject::GetData(inputVector[0], 0);
  vtkSelection* selection = vtkSelection::GetData(inputVector[1], 0);
  if (selection == NULL || selection->GetNumberOfNodes() == 0)
    {
    // empty selection.
    return 1;
    }

  if (selection->GetNumberOfNodes() > 1)
    {
    vtkWarningMacro("vtkPythonExtractSelection currently only supports a selection "
      "with a single vtkSelectionNode instance. All other instances will be ignored, "
      "except the first one.");
    }

  vtkDataObject* output = vtkDataObject::GetData(outputVector, 0);
  this->InitializeOutput(output, input);

  // Set self to point to this
  char addrofthis[1024];
  sprintf(addrofthis, "%p", this);
  char *aplus = addrofthis;
  if ((addrofthis[0] == '0') &&
      ((addrofthis[1] == 'x') || addrofthis[1] == 'X'))
    {
    aplus += 2; //skip over "0x"
    }

  // ensure Python is initialized.
  vtkPythonInterpreter::Initialize();

  std::ostringstream stream;
  stream
    << "def vtkPythonExtractSelection_RequestData():" << endl
    << "    from paraview import extract_selection as pv_es" << endl
    << "    from paraview.vtk.vtkPVClientServerCoreCore import vtkPythonExtractSelection" << endl
    << "    me = vtkPythonExtractSelection('" << aplus << " ')" << endl
    << "    pv_es.execute(me)" << endl
    << "    del me" << endl
    << "    del pv_es" << endl
    << "vtkPythonExtractSelection_RequestData()" << endl
    << "del vtkPythonExtractSelection_RequestData" << endl;
  vtkPythonInterpreter::RunSimpleString(stream.str().c_str());
  return 1;
}

//----------------------------------------------------------------------------
void vtkPythonExtractSelection::InitializeOutput(
  vtkDataObject* output, vtkDataObject* input)
{
  if (this->PreserveTopology)
    {
    // When preserving topology, we need to shallow copy input to output.
    output->ShallowCopy(input);

    vtkCompositeDataSet* outputCD = vtkCompositeDataSet::SafeDownCast(output);
    if (!outputCD)
      {
      return;
      }

    // For composite datasets, the ShallowCopy simply shares the leaf datasets.
    // We need to create new instances for those.
    vtkSmartPointer<vtkCompositeDataIterator> iter;
    iter.TakeReference(outputCD->NewIterator());
    for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
      {
      vtkDataObject* ds = iter->GetCurrentDataObject();
      assert(ds != NULL);

      vtkDataObject* clone = ds->NewInstance();
      clone->ShallowCopy(ds);
      outputCD->SetDataSet(iter, clone);
      clone->FastDelete();
      }
    }
  else
    {
    // not preserving topology. In that case, we just ensure that the output
    // composite dataset has same structure as the input.
    if (vtkCompositeDataSet* outputCD = vtkCompositeDataSet::SafeDownCast(output))
      {
      vtkCompositeDataSet* inputCD = vtkCompositeDataSet::SafeDownCast(input);
      assert(inputCD != NULL);
      outputCD->CopyStructure(inputCD);

      // To make it easier for the Python code to pass the "original ids" array back,
      // we initialize the non-null leaf nodes in this composite dataset with empty datasets.
      vtkSmartPointer<vtkCompositeDataIterator> iter;
      iter.TakeReference(inputCD->NewIterator());
      for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
        {
        vtkDataObject* ds = iter->GetCurrentDataObject();
        assert(ds != NULL);

        if (vtkTable::SafeDownCast(ds))
          {
          vtkTable* table = vtkTable::New();
          outputCD->SetDataSet(iter, table);
          table->FastDelete();
          }
        else if (vtkDataSet::SafeDownCast(ds))
          {
          vtkUnstructuredGrid* ug = vtkUnstructuredGrid::New();
          outputCD->SetDataSet(iter, ug);
          ug->FastDelete();
          }
        else
          {
          vtkWarningMacro("Composite data has unsupported type: "
            << ds->GetClassName());
          }
        }
      }
    }
}

//----------------------------------------------------------------------------
bool vtkPythonExtractSelection::ExtractElements(
  int attributeType, vtkDataObject* input, vtkDataObject* output)
{
  assert(this->PreserveTopology == 0);
  if (vtkCompositeDataSet* inputCD = vtkCompositeDataSet::SafeDownCast(input))
    {
    return this->ExtractElements(attributeType,
      inputCD, vtkCompositeDataSet::SafeDownCast(output));
    }

  int fieldType;
  switch (attributeType)
    {
  case vtkDataObject::CELL:
    fieldType = vtkSelectionNode::CELL;
    break;

  case vtkDataObject::POINT:
    fieldType = vtkSelectionNode::POINT;
    break;

  case vtkDataObject::ROW:
    fieldType = vtkSelectionNode::ROW;
    break;

  default:
    vtkWarningMacro("Unsupported attributeType: " << attributeType);
    return false;
    }

  // sanity check: ensure that the attribute type specified is valid for the type of
  // input dataset.
  if (input->GetAttributes(attributeType) == NULL)
    {
    vtkWarningMacro("Incorrect attributeType '" << attributeType << "' "
      "for input data type '" << input->GetClassName() << "'");
    return false;
    }

  // extract_selection.execute() puts the selected ids array in field data of the output dataset.
  // This is done so to keep the code clean for the case with composite datasets.
  vtkAbstractArray* idsToExtact = output->GetFieldData()->GetAbstractArray("vtkSelectedIds");
  if (idsToExtact && idsToExtact->GetNumberOfTuples() > 0)
    {
    vtkNew<vtkSelection> selection;
    vtkNew<vtkSelectionNode> node;
    selection->AddNode(node.GetPointer());
    node->SetContentType(vtkSelectionNode::INDICES);
    node->SetFieldType(fieldType);
    node->SetSelectionList(idsToExtact);

    vtkSmartPointer<vtkAlgorithm> extractor;
    if (vtkTable::SafeDownCast(input))
      {
      vtkNew<vtkExtractSelectedRows> filter;
      filter->SetAddOriginalRowIdsArray(true);
      extractor = filter.GetPointer();
      }
    else
      {
      vtkNew<vtkExtractSelectedIds> filter;
      filter->PreserveTopologyOff();
      extractor = filter.GetPointer();
      }
    extractor->SetInputDataObject(0, input);
    extractor->SetInputDataObject(1, selection.GetPointer());
    extractor->Update();

    idsToExtact = NULL;
    // note: the ShallowCopy will overwrite output->FieldData, hence idsToExtact will be
    // dangling.
    output->ShallowCopy(extractor->GetOutputDataObject(0));
    return true;
    }

  output->Initialize();
  return false;
}

//----------------------------------------------------------------------------
bool vtkPythonExtractSelection::ExtractElements(
  int attributeType, vtkCompositeDataSet* input, vtkCompositeDataSet* output)
{
  assert(this->PreserveTopology == 0);

  // this method simply iterates over all the leaf nodes in the dataset and calls
  // ExtractElements.
  vtkSmartPointer<vtkCompositeDataIterator> iter;
  iter.TakeReference(input->NewIterator());
  for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
    {
    if (!this->ExtractElements(attributeType,
        iter->GetCurrentDataObject(), output->GetDataSet(iter)))
      {
      output->SetDataSet(iter, NULL);
      }
    }
  return true;
}

//----------------------------------------------------------------------------
void vtkPythonExtractSelection::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}