-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvtkAnnotateOBBs.cxx
153 lines (123 loc) · 4.52 KB
/
vtkAnnotateOBBs.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAnnotateOBBs.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 "vtkAnnotateOBBs.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkSmartPointer.h"
#include "vtkAppendPolyData.h"
#include "vtkThresholdPoints.h"
#include "vtkOutlineSource.h"
#include "vtkOBBTree.h"
#include <Eigen/Geometry>
//----------------------------------------------------------------------------
namespace {
static void ComputeOBB(vtkPolyData* polyData, double cornerData[24])
{
Eigen::Vector3d origin, x, y, z, sizes;
vtkSmartPointer<vtkOBBTree> obb = vtkSmartPointer<vtkOBBTree>::New();
obb->ComputeOBB(polyData->GetPoints(), origin.data(), x.data(), y.data(), z.data(), sizes.data());
// make right handed
Eigen::Vector3d rightHandedZ = x.cross(y);
if (z.dot(rightHandedZ) < 0)
{
origin += z;
z *= -1;
}
Eigen::Vector3d corners[8];
corners[0] = origin;
corners[1] = origin+x;
corners[2] = origin+y;
corners[3] = origin+x+y;
corners[4] = z+origin;
corners[5] = z+origin+x;
corners[6] = z+origin+y;
corners[7] = z+origin+x+y;
for (int i = 0; i < 8; ++i)
{
cornerData[i*3+0] = corners[i][0];
cornerData[i*3+1] = corners[i][1];
cornerData[i*3+2] = corners[i][2];
}
}
}
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkAnnotateOBBs);
//----------------------------------------------------------------------------
vtkAnnotateOBBs::vtkAnnotateOBBs()
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
this->AnnotateLabelZero = true;
this->SetInputArrayToProcess(
0,0,0, vtkDataObject::FIELD_ASSOCIATION_POINTS,
vtkDataSetAttributes::SCALARS);
}
//----------------------------------------------------------------------------
vtkAnnotateOBBs::~vtkAnnotateOBBs()
{
}
//----------------------------------------------------------------------------
int vtkAnnotateOBBs::RequestData(
vtkInformation* vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataArray* labels = this->GetInputArrayToProcess(0, inputVector);
if (!labels)
{
vtkErrorMacro("Could not get input scalars.");
return 0;
}
vtkSmartPointer<vtkPolyData> inputCopy = vtkSmartPointer<vtkPolyData>::New();
inputCopy->ShallowCopy(input);
vtkIdType minLabel = labels->GetRange()[0];
vtkIdType maxLabel = labels->GetRange()[1];
if (minLabel == 0 && !this->AnnotateLabelZero)
{
minLabel = 1;
}
vtkNew<vtkAppendPolyData> appendFilter;
for (vtkIdType i = minLabel; i <= maxLabel; ++i)
{
vtkNew<vtkThresholdPoints> threshold;
threshold->ThresholdBetween(i, i);
threshold->SetInput(inputCopy);
threshold->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, labels->GetName());
threshold->Update();
vtkPolyData* labelPoints = threshold->GetOutput();
double cornerData[24];
ComputeOBB(labelPoints, cornerData);
vtkSmartPointer<vtkOutlineSource> outline = vtkSmartPointer<vtkOutlineSource>::New();
outline->SetBoxTypeToOriented();
outline->SetCorners(cornerData);
appendFilter->AddInputConnection(outline->GetOutputPort());
}
if (appendFilter->GetNumberOfInputConnections(0))
{
appendFilter->Update();
}
output->ShallowCopy(appendFilter->GetOutput());
return 1;
}
//----------------------------------------------------------------------------
void vtkAnnotateOBBs::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}