/
vtkSlicerSplinesLogic.cxx
154 lines (124 loc) · 4.91 KB
/
vtkSlicerSplinesLogic.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
154
/*==============================================================================
Program: 3D Slicer
Copyright (c) Kitware Inc.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Julien Finet, Kitware Inc.
and was partially funded by Allen Institute
==============================================================================*/
// Splines Logic includes
#include "vtkSlicerSplinesLogic.h"
// MRML includes
#include <vtkMRMLModelNode.h>
#include <vtkMRMLScene.h>
#include <vtkMRMLSelectionNode.h>
#include <vtkMRMLMarkupsClosedCurveNode.h>
// VTK includes
#include <vtkEventBroker.h>
#include <vtkIntArray.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkPolyData.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTriangle.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyDataWriter.h>
#include <vtkAppendPolyData.h>
#include <vtkCleanPolyData.h>
#include <vtkContourTriangulator.h>
// STD includes
#include <cassert>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkSlicerSplinesLogic);
//----------------------------------------------------------------------------
vtkSlicerSplinesLogic::vtkSlicerSplinesLogic()
{
}
//----------------------------------------------------------------------------
vtkSlicerSplinesLogic::~vtkSlicerSplinesLogic()
{
}
//----------------------------------------------------------------------------
void vtkSlicerSplinesLogic::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//---------------------------------------------------------------------------
vtkSmartPointer<vtkPolyData> vtkSlicerSplinesLogic::CreateModelFromContour(
vtkPolyData* inputContour, vtkVector3d normal, double thickness)
{
if (!inputContour
|| !inputContour->GetPoints()
|| inputContour->GetPoints()->GetNumberOfPoints() < 3)
{
return nullptr;
}
vtkNew<vtkContourTriangulator> contourTriangulator;
contourTriangulator->SetInputData(inputContour);
vtkNew<vtkTransform> topHalfTransform;
topHalfTransform->Translate(
thickness * 0.5 * normal.GetX(),
thickness * 0.5 * normal.GetY(),
thickness * 0.5 * normal.GetZ());
vtkNew<vtkTransform> bottomHalfTransform;
bottomHalfTransform->Translate(
-thickness * 0.5 * normal.GetX(),
-thickness * 0.5 * normal.GetY(),
-thickness * 0.5 * normal.GetZ());
vtkNew<vtkTransformPolyDataFilter> topHalfFilter;
topHalfFilter->SetInputConnection(contourTriangulator->GetOutputPort());
topHalfFilter->SetTransform(topHalfTransform.GetPointer());
topHalfFilter->Update();
vtkNew<vtkTransformPolyDataFilter> bottomHalfFilter;
bottomHalfFilter->SetInputConnection(contourTriangulator->GetOutputPort());
bottomHalfFilter->SetTransform(bottomHalfTransform.GetPointer());
bottomHalfFilter->Update();
vtkPolyData* topHalf = topHalfFilter->GetOutput();
vtkPolyData* bottomHalf = bottomHalfFilter->GetOutput();
vtkNew<vtkPoints> points;
for (vtkIdType i = 0; i < topHalf->GetNumberOfPoints(); ++i)
{
points->InsertNextPoint(topHalf->GetPoint(i));
points->InsertNextPoint(bottomHalf->GetPoint(i));
}
vtkNew<vtkCellArray> cells;
for (vtkIdType i = 0; i < points->GetNumberOfPoints() - 2; i += 2)
{
vtkNew<vtkTriangle> beltTriangle1;
beltTriangle1->GetPointIds()->SetId(0, i);
beltTriangle1->GetPointIds()->SetId(1, i + 1);
beltTriangle1->GetPointIds()->SetId(2, i + 2);
cells->InsertNextCell(beltTriangle1.GetPointer());
vtkNew<vtkTriangle> beltTriangle2;
beltTriangle2->GetPointIds()->SetId(0, i + 1);
beltTriangle2->GetPointIds()->SetId(1, i + 3);
beltTriangle2->GetPointIds()->SetId(2, i + 2);
cells->InsertNextCell(beltTriangle2.GetPointer());
}
vtkNew<vtkPolyData> beltSurface;
beltSurface->SetPoints(points.GetPointer());
beltSurface->SetPolys(cells.GetPointer());
vtkNew<vtkAppendPolyData> appendFilter;
appendFilter->AddInputConnection(bottomHalfFilter->GetOutputPort());
appendFilter->AddInputData(beltSurface);
appendFilter->AddInputConnection(topHalfFilter->GetOutputPort());
vtkNew<vtkCleanPolyData> cleanFilter;
cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
cleanFilter->Update();
return cleanFilter->GetOutput();
}
void vtkSlicerSplinesLogic::BuildSplineModel(vtkMRMLModelNode* modelNode, vtkPolyData *contour, vtkVector3d normal, double thickness) {
if (!contour || !modelNode) return;
vtkSmartPointer<vtkPolyData> mesh = CreateModelFromContour(
contour, normal, thickness
);
modelNode->SetAndObserveMesh(mesh);
return;
}