File: vtkGraphAnnotationLayersFilter.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (304 lines) | stat: -rw-r--r-- 10,097 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

 Program:   Visualization Toolkit
 Module:    vtkGraphAnnotationLayersFilter.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.

 =========================================================================*/

#include "vtkGraphAnnotationLayersFilter.h"

#include "vtkAnnotation.h"
#include "vtkAnnotationLayers.h"
#include "vtkAppendPolyData.h"
#include "vtkCellData.h"
#include "vtkConvexHull2D.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"

vtkStandardNewMacro(vtkGraphAnnotationLayersFilter);

//-----------------------------------------------------------------------------
vtkGraphAnnotationLayersFilter::vtkGraphAnnotationLayersFilter()
{
  this->SetNumberOfInputPorts(2);
  this->SetNumberOfOutputPorts(2);

  this->HullAppend = vtkSmartPointer<vtkAppendPolyData>::New();
  this->OutlineAppend = vtkSmartPointer<vtkAppendPolyData>::New();
  this->ConvexHullFilter = vtkSmartPointer<vtkConvexHull2D>::New();
}

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

//-----------------------------------------------------------------------------
int vtkGraphAnnotationLayersFilter::FillInputPortInformation(int port,
  vtkInformation* info)
{
  if (port == 0)
  {
    info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),
      "vtkGraph");
    return 1;
  }
  else if (port == 1)
  {
    info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),
      "vtkAnnotationLayers");
    return 1;
  }
  return 0;
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::OutlineOn()
{
  this->ConvexHullFilter->OutlineOn();
}

void vtkGraphAnnotationLayersFilter::OutlineOff()
{
  this->ConvexHullFilter->OutlineOff();
}

void vtkGraphAnnotationLayersFilter::SetOutline(bool b)
{
  this->ConvexHullFilter->SetOutline(b);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetScaleFactor(double scale)
{
  this->ConvexHullFilter->SetScaleFactor(scale);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetHullShapeToBoundingRectangle()
{
  this->ConvexHullFilter->SetHullShape(vtkConvexHull2D::BoundingRectangle);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetHullShapeToConvexHull()
{
  this->ConvexHullFilter->SetHullShape(vtkConvexHull2D::ConvexHull);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetMinHullSizeInWorld(double size)
{
  this->ConvexHullFilter->SetMinHullSizeInWorld(size);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetMinHullSizeInDisplay(int size)
{
  this->ConvexHullFilter->SetMinHullSizeInDisplay(size);
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::SetRenderer(vtkRenderer* renderer)
{
  this->ConvexHullFilter->SetRenderer(renderer);
}

//-----------------------------------------------------------------------------
vtkMTimeType vtkGraphAnnotationLayersFilter::GetMTime()
{
  if (this->ConvexHullFilter)
  {
    return this->ConvexHullFilter->GetMTime();
  }
  else
  {
    return this->MTime;
  }
}

//-----------------------------------------------------------------------------
int vtkGraphAnnotationLayersFilter::RequestData(vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector, vtkInformationVector *outputVector)
{
  // Get the input and output.
  vtkInformation *inGraphInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *inLayersInfo = inputVector[1]->GetInformationObject(0);

  vtkGraph* graph = vtkGraph::SafeDownCast(
    inGraphInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkPoints* inputPoints = graph->GetPoints();
  vtkAnnotationLayers* layers = vtkAnnotationLayers::SafeDownCast(
    inLayersInfo->Get(vtkDataObject::DATA_OBJECT()));

  vtkInformation *outInfo0 = outputVector->GetInformationObject(0);
  vtkInformation *outInfo1 = outputVector->GetInformationObject(1);

  vtkPolyData *outputHull = vtkPolyData::SafeDownCast(outInfo0->Get(
    vtkDataObject::DATA_OBJECT()));
  vtkPolyData *outputOutline = vtkPolyData::SafeDownCast(outInfo1->Get(
    vtkDataObject::DATA_OBJECT()));

  this->HullAppend->RemoveAllInputs();
  this->OutlineAppend->RemoveAllInputs();

  unsigned numberOfAnnotations = layers->GetNumberOfAnnotations();

  // Generate one hull/polydata per selection node
  vtkIdType hullId = 0;
  for (unsigned annotationId = 0; annotationId < numberOfAnnotations;
    ++annotationId)
  {
    vtkAnnotation* annotation = layers->GetAnnotation(annotationId);
    if (annotation->GetInformation()->Get(vtkAnnotation::ENABLE()) == 0)
    {
      continue;
    }

    vtkSelection* selection = annotation->GetSelection();
    unsigned numberOfSelectionNodes = selection->GetNumberOfNodes();

    for (unsigned selectionNodeId = 0; selectionNodeId < numberOfSelectionNodes;
      ++selectionNodeId)
    {
      vtkSmartPointer<vtkPoints> hullPoints = vtkSmartPointer<vtkPoints>::New();

      hullId++;
      vtkSelectionNode* selectionNode = selection->GetNode(selectionNodeId);
      if (selectionNode->GetFieldType() != vtkSelectionNode::VERTEX)
      {
        continue;
      }
      vtkIdTypeArray* vertexIds = vtkArrayDownCast<vtkIdTypeArray>(
        selectionNode->GetSelectionList());

      // Get points from graph
      vtkIdType numberOfNodePoints = vertexIds->GetNumberOfTuples();
      if (numberOfNodePoints == 0)
      {
        continue;
      }
      for (vtkIdType i = 0; i < numberOfNodePoints; ++i)
      {
        hullPoints->InsertNextPoint(inputPoints->GetPoint(vertexIds->GetValue(i)));
      }

      // Create filled polygon
      vtkSmartPointer<vtkPolyData> hullPolyData =
        vtkSmartPointer<vtkPolyData>::New();
      hullPolyData->SetPoints(hullPoints);
      ConvexHullFilter->SetInputData(hullPolyData);
      ConvexHullFilter->Update();
      hullPolyData->ShallowCopy(ConvexHullFilter->GetOutput());

      // Add data arrays to the polydata
      vtkIdType representativeVertex = vertexIds->GetValue(0);
      vtkIdType numberOfCells = hullPolyData->GetNumberOfCells();

      vtkUnsignedCharArray* outColors = vtkUnsignedCharArray::New();
      outColors->SetNumberOfComponents(4);
      outColors->SetName("Hull color");
      double* color = annotation->GetInformation()->Get(vtkAnnotation::COLOR());
      double opacity = annotation->GetInformation()->Get(vtkAnnotation::OPACITY());
      unsigned char outColor[4] = {
        static_cast<unsigned char>(color[0] * 255),
        static_cast<unsigned char>(color[1] * 255),
        static_cast<unsigned char>(color[2] * 255),
        static_cast<unsigned char>(opacity * 255) };
      for (vtkIdType i = 0; i < numberOfCells; ++i)
      {
        outColors->InsertNextTypedTuple(outColor);
      }
      hullPolyData->GetCellData()->AddArray(outColors);
      outColors->Delete();

      vtkIdTypeArray* hullIds = vtkIdTypeArray::New();
      hullIds->SetName("Hull id");
      for (vtkIdType i = 0; i < numberOfCells; ++i)
      {
        hullIds->InsertNextValue(hullId);
      }
      hullPolyData->GetCellData()->AddArray(hullIds);
      hullIds->Delete();

      vtkStringArray* hullName = vtkStringArray::New();
      hullName->SetName("Hull name");
      for (vtkIdType i = 0; i < numberOfCells; ++i)
      {
        hullName->InsertNextValue(
          annotation->GetInformation()->Get(vtkAnnotation::LABEL()));
      }
      hullPolyData->GetCellData()->AddArray(hullName);
      hullName->Delete();

      vtkDoubleArray* hullCentreVertex = vtkDoubleArray::New();
      hullCentreVertex->SetName("Hull point");
      hullCentreVertex->SetNumberOfComponents(3);
      for (vtkIdType i = 0; i < numberOfCells; ++i)
      {
        hullCentreVertex->InsertNextTuple(
          inputPoints->GetPoint(representativeVertex));
      }
      hullPolyData->GetCellData()->AddArray(hullCentreVertex);
      hullCentreVertex->Delete();

      this->HullAppend->AddInputData(hullPolyData);

      if (this->ConvexHullFilter->GetOutline())
      {
        vtkSmartPointer<vtkPolyData> outlinePolyData =
          vtkSmartPointer<vtkPolyData>::New();
        outlinePolyData->ShallowCopy(ConvexHullFilter->GetOutput(1));
        this->OutlineAppend->AddInputData(outlinePolyData);
      }
    } // Next selection node.
  } // Next annotation.

  // Send data to output
  if (this->HullAppend->GetNumberOfInputConnections(0) > 0)
  {
    this->HullAppend->Update();
    outputHull->ShallowCopy(this->HullAppend->GetOutput());
  }
  if (this->OutlineAppend->GetNumberOfInputConnections(0) > 0)
  {
    this->OutlineAppend->Update();
    outputOutline->ShallowCopy(this->OutlineAppend->GetOutput());
  }
  return 1;
}

//-----------------------------------------------------------------------------
void vtkGraphAnnotationLayersFilter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "ConvexHull2D: ";
    if (this->ConvexHullFilter)
    {
      os << endl;
      this->ConvexHullFilter->PrintSelf(os, indent.GetNextIndent());
    }
    else
    {
      os << "(none)" << endl;
    }
}