ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkOutputPolyData.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkOutputPolyData_h
19#define itkOutputPolyData_h
20
21#include "itkPipeline.h"
23
24#ifndef ITK_WASM_NO_MEMORY_IO
25# include "itkWasmExports.h"
26# include "itkWasmPolyData.h"
28#endif
29#ifndef ITK_WASM_NO_FILESYSTEM_IO
30# include "itkMeshFileWriter.h"
32#endif
33
34namespace itk
35{
36namespace wasm
37{
48template <typename TPolyData>
49class ITK_TEMPLATE_EXPORT OutputPolyData
50{
51public:
52 using PolyDataType = TPolyData;
53
54 void
55 Set(const PolyDataType * polyData)
56 {
57 this->m_PolyData = polyData;
58 }
59
60 const PolyDataType *
61 Get() const
62 {
63 return this->m_PolyData.GetPointer();
64 }
65
67 void
68 SetIdentifier(const std::string & identifier)
69 {
70 this->m_Identifier = identifier;
71 }
72 const std::string &
74 {
75 return this->m_Identifier;
76 }
77
78 OutputPolyData() = default;
80 {
82 {
83#ifndef ITK_WASM_NO_MEMORY_IO
84 if (!this->m_PolyData.IsNull() && !this->m_Identifier.empty())
85 {
86 using PolyDataToWasmPolyDataFilterType = PolyDataToWasmPolyDataFilter<PolyDataType>;
87 auto polyDataToWasmPolyDataFilter = PolyDataToWasmPolyDataFilterType::New();
88 polyDataToWasmPolyDataFilter->SetInput(this->m_PolyData);
89 polyDataToWasmPolyDataFilter->Update();
90 auto wasmPolyData = polyDataToWasmPolyDataFilter->GetOutput();
91 const auto index = std::stoi(this->m_Identifier);
92 setMemoryStoreOutputDataObject(0, index, wasmPolyData);
93
94 if (this->m_PolyData->GetNumberOfPoints() > 0)
95 {
96 const auto pointsAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetPoints()->at(0)));
97 const auto pointsSize = wasmPolyData->GetPolyData()->GetPoints()->Size() * PolyDataType::PointDimension *
98 sizeof(typename PolyDataType::CoordinateType);
99 setMemoryStoreOutputArray(0, index, 0, pointsAddress, pointsSize);
100 }
101
102 if (this->m_PolyData->GetVertices() && this->m_PolyData->GetVertices()->Size() > 0)
103 {
104 const auto verticesAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetVertices()->at(0)));
105 const auto verticesSize = wasmPolyData->GetPolyData()->GetVertices()->Size() * sizeof(uint32_t);
106 setMemoryStoreOutputArray(0, index, 1, verticesAddress, verticesSize);
107 }
108
109 if (this->m_PolyData->GetLines() && this->m_PolyData->GetLines()->Size() > 0)
110 {
111 const auto linesAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetLines()->at(0)));
112 const auto linesSize = wasmPolyData->GetPolyData()->GetLines()->Size() * sizeof(uint32_t);
113 setMemoryStoreOutputArray(0, index, 2, linesAddress, linesSize);
114 }
115
116 if (this->m_PolyData->GetPolygons() && this->m_PolyData->GetPolygons()->Size() > 0)
117 {
118 const auto polygonsAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetPolygons()->at(0)));
119 const auto polygonsSize = wasmPolyData->GetPolyData()->GetPolygons()->Size() * sizeof(uint32_t);
120 setMemoryStoreOutputArray(0, index, 3, polygonsAddress, polygonsSize);
121 }
122
123 if (this->m_PolyData->GetTriangleStrips() && this->m_PolyData->GetTriangleStrips()->Size() > 0)
124 {
125 const auto triangleStripsAddress =
126 reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetTriangleStrips()->at(0)));
127 const auto triangleStripsSize = wasmPolyData->GetPolyData()->GetTriangleStrips()->Size() * sizeof(uint32_t);
128 setMemoryStoreOutputArray(0, index, 4, triangleStripsAddress, triangleStripsSize);
129 }
130
131 if (this->m_PolyData->GetPointData() != nullptr && this->m_PolyData->GetPointData()->Size() > 0)
132 {
133 using PointPixelType = typename PolyDataType::PixelType;
134 using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
135 const auto pointDataAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetPointData()->at(0)));
136 const auto pointDataSize = wasmPolyData->GetPolyData()->GetPointData()->Size() *
137 sizeof(typename ConvertPointPixelTraits::ComponentType) *
138 ConvertPointPixelTraits::GetNumberOfComponents();
139 setMemoryStoreOutputArray(0, index, 5, pointDataAddress, pointDataSize);
140 }
141
142 if (this->m_PolyData->GetCellData() != nullptr && this->m_PolyData->GetCellData()->Size() > 0)
143 {
144 using CellPixelType = typename PolyDataType::CellPixelType;
145 using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
146 const auto cellDataAddress = reinterpret_cast<size_t>(&(wasmPolyData->GetPolyData()->GetCellData()->at(0)));
147 const auto cellDataSize = wasmPolyData->GetPolyData()->GetCellData()->Size() *
148 sizeof(typename ConvertCellPixelTraits::ComponentType) *
149 ConvertCellPixelTraits::GetNumberOfComponents();
150 setMemoryStoreOutputArray(0, index, 6, cellDataAddress, cellDataSize);
151 }
152 }
153#else
154 std::cerr << "Memory IO not supported" << std::endl;
155 abort();
156#endif
157 }
158 else
159 {
160#ifndef ITK_WASM_NO_FILESYSTEM_IO
161 if (!this->m_PolyData.IsNull() && !this->m_Identifier.empty())
162 {
163 using PolyDataToMeshFilterType = PolyDataToMeshFilter<TPolyData>;
164 auto polyDataToMeshFilter = PolyDataToMeshFilterType::New();
165 polyDataToMeshFilter->SetInput(this->m_PolyData);
166 using MeshType = typename PolyDataToMeshFilterType::OutputMeshType;
167 using MeshWriterType = MeshFileWriter<MeshType>;
168 auto meshWriter = MeshWriterType::New();
169 meshWriter->SetFileName(this->m_Identifier);
170 meshWriter->SetInput(polyDataToMeshFilter->GetOutput());
171 meshWriter->Update();
172 }
173#else
174 std::cerr << "Filesystem IO not supported" << std::endl;
175 abort();
176#endif
177 }
178 }
179
180protected:
181 typename TPolyData::ConstPointer m_PolyData;
182
183 std::string m_Identifier;
184};
185
186template <typename TPolyData>
187bool
188lexical_cast(const std::string & input, OutputPolyData<TPolyData> & outputPolyData)
189{
190 outputPolyData.SetIdentifier(input);
191 return true;
192}
193
194} // namespace wasm
195} // namespace itk
196
197#endif
Traits class used to by ConvertPixels to convert blocks of pixels.
Writes mesh data to a single file.
Convert PolyData to a Mesh.
Convert an PolyData to an WasmPolyData object.
Output polyData for an itk::wasm::Pipeline.
void Set(const PolyDataType *polyData)
const PolyDataType * Get() const
const std::string & GetIdentifier() const
TPolyData::ConstPointer m_PolyData
void SetIdentifier(const std::string &identifier)
static auto get_use_memory_io()
WebAssemblyInterface_EXPORT void setMemoryStoreOutputDataObject(uint32_t memoryIndex, uint32_t index, const WasmDataObject *dataObject)
WebAssemblyInterface_EXPORT void setMemoryStoreOutputArray(uint32_t memoryIndex, uint32_t index, uint32_t subIndex, vcl_size_t address, vcl_size_t size)
bool lexical_cast(const std::string &input, InputImage< TImage > &inputImage)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....