ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkOutputMesh.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 itkOutputMesh_h
19#define itkOutputMesh_h
20
21#include "itkPipeline.h"
23#include "itkMesh.h"
24#include "itkQuadEdgeMesh.h"
25
26#ifndef ITK_WASM_NO_MEMORY_IO
27# include "itkWasmExports.h"
28# include "itkWasmMesh.h"
30#endif
31#ifndef ITK_WASM_NO_FILESYSTEM_IO
32# include "itkMeshFileWriter.h"
33#endif
34
35namespace itk
36{
37namespace wasm
38{
49template <typename TMesh>
50class ITK_TEMPLATE_EXPORT OutputMesh
51{
52public:
53 using MeshType = TMesh;
54
55 void
56 Set(const MeshType * mesh)
57 {
58 this->m_Mesh = mesh;
59 }
60
61 const MeshType *
62 Get() const
63 {
64 return this->m_Mesh.GetPointer();
65 }
66
68 void
69 SetIdentifier(const std::string & identifier)
70 {
71 this->m_Identifier = identifier;
72 }
73 const std::string &
75 {
76 return this->m_Identifier;
77 }
78
79 OutputMesh() = default;
81 {
83 {
84#ifndef ITK_WASM_NO_MEMORY_IO
85 if (!this->m_Mesh.IsNull() && !this->m_Identifier.empty())
86 {
87 using MeshToWasmMeshFilterType = MeshToWasmMeshFilter<MeshType>;
88 auto meshToWasmMeshFilter = MeshToWasmMeshFilterType::New();
89 meshToWasmMeshFilter->SetInput(this->m_Mesh);
90 meshToWasmMeshFilter->Update();
91 auto wasmMesh = meshToWasmMeshFilter->GetOutput();
92 const auto index = std::stoi(this->m_Identifier);
93 setMemoryStoreOutputDataObject(0, index, wasmMesh);
94
95 if (this->m_Mesh->GetNumberOfPoints() > 0)
96 {
97 const auto pointsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetMesh()->GetPoints()->at(0)));
98 const auto pointsSize = wasmMesh->GetMesh()->GetPoints()->Size() * sizeof(typename MeshType::CoordinateType) *
99 MeshType::PointDimension;
100 setMemoryStoreOutputArray(0, index, 0, pointsAddress, pointsSize);
101 }
102
103 if (this->m_Mesh->GetNumberOfCells() > 0)
104 {
105 const auto cellsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellBuffer()->at(0)));
106 const auto cellsSize = wasmMesh->GetCellBuffer()->Size() * sizeof(typename MeshType::CellIdentifier);
107 setMemoryStoreOutputArray(0, index, 1, cellsAddress, cellsSize);
108 }
109
110 if (this->m_Mesh->GetPointData() != nullptr && this->m_Mesh->GetPointData()->Size() > 0)
111 {
112 using PointPixelType = typename MeshType::PixelType;
113 using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
114 const auto pointDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetMesh()->GetPointData()->at(0)));
115 const auto pointDataSize = wasmMesh->GetMesh()->GetPointData()->Size() *
116 sizeof(typename ConvertPointPixelTraits::ComponentType) *
117 ConvertPointPixelTraits::GetNumberOfComponents();
118 setMemoryStoreOutputArray(0, index, 2, pointDataAddress, pointDataSize);
119 }
120
121 if (this->m_Mesh->GetCellData() != nullptr && this->m_Mesh->GetCellData()->Size() > 0)
122 {
123 using CellPixelType = typename MeshType::CellPixelType;
124 using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
125 const auto cellDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetMesh()->GetCellData()->at(0)));
126 const auto cellDataSize = wasmMesh->GetMesh()->GetCellData()->Size() *
127 sizeof(typename ConvertCellPixelTraits::ComponentType) *
128 ConvertCellPixelTraits::GetNumberOfComponents();
129 setMemoryStoreOutputArray(0, index, 3, cellDataAddress, cellDataSize);
130 }
131 }
132#else
133 std::cerr << "Memory IO not supported" << std::endl;
134 abort();
135#endif
136 }
137 else
138 {
139#ifndef ITK_WASM_NO_FILESYSTEM_IO
140 if (!this->m_Mesh.IsNull() && !this->m_Identifier.empty())
141 {
142 using MeshWriterType = itk::MeshFileWriter<TMesh>;
143 auto meshWriter = MeshWriterType::New();
144 meshWriter->SetFileName(this->m_Identifier);
145 meshWriter->SetInput(this->m_Mesh);
146 meshWriter->Update();
147 }
148#else
149 std::cerr << "Filesystem IO not supported" << std::endl;
150 abort();
151#endif
152 }
153 }
154
155protected:
156 typename TMesh::ConstPointer m_Mesh;
157
158 std::string m_Identifier;
159};
160
161// write a template specialization for itk::QuadEdgeMesh that does not assume
162// that the points, etc. are stored in itk::VectorContainers
163template <typename TPixel, unsigned int VDimension>
164class OutputMesh<itk::QuadEdgeMesh<TPixel, VDimension>>
165{
166public:
168
169 void
170 Set(const MeshType * mesh)
171 {
172 this->m_Mesh = mesh;
173 }
174
175 const MeshType *
176 Get() const
177 {
178 return this->m_Mesh.GetPointer();
179 }
180
182 void
183 SetIdentifier(const std::string & identifier)
184 {
185 this->m_Identifier = identifier;
186 }
187 const std::string &
189 {
190 return this->m_Identifier;
191 }
192
193 OutputMesh() = default;
195 {
197 {
198#ifndef ITK_WASM_NO_MEMORY_IO
199 if (!this->m_Mesh.IsNull() && !this->m_Identifier.empty())
200 {
201 using MeshToWasmMeshFilterType = MeshToWasmMeshFilter<MeshType>;
202 auto meshToWasmMeshFilter = MeshToWasmMeshFilterType::New();
203 meshToWasmMeshFilter->SetInput(this->m_Mesh);
204 meshToWasmMeshFilter->Update();
205 auto wasmMesh = meshToWasmMeshFilter->GetOutput();
206 const auto index = std::stoi(this->m_Identifier);
207 setMemoryStoreOutputDataObject(0, index, wasmMesh);
208
209 if (this->m_Mesh->GetNumberOfPoints() > 0)
210 {
211 const auto pointsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetPointsBuffer().at(0)));
212 const auto pointsSize = wasmMesh->GetPointsBuffer().size() * sizeof(typename MeshType::PointIdentifier);
213 setMemoryStoreOutputArray(0, index, 0, pointsAddress, pointsSize);
214 }
215
216 if (this->m_Mesh->GetNumberOfCells() > 0)
217 {
218 const auto cellsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellBuffer()->at(0)));
219 const auto cellsSize = wasmMesh->GetCellBuffer()->Size() * sizeof(typename MeshType::CellIdentifier);
220 setMemoryStoreOutputArray(0, index, 1, cellsAddress, cellsSize);
221 }
222
223 if (this->m_Mesh->GetPointData() != nullptr && this->m_Mesh->GetPointData()->Size() > 0)
224 {
225 using PointPixelType = typename MeshType::PixelType;
226 using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
227 const auto pointDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetPointDataBuffer().at(0)));
228 const auto pointDataSize =
229 wasmMesh->GetPointDataBuffer().size() * sizeof(typename ConvertPointPixelTraits::ComponentType);
230 setMemoryStoreOutputArray(0, index, 2, pointDataAddress, pointDataSize);
231 }
232
233 if (this->m_Mesh->GetCellData() != nullptr && this->m_Mesh->GetCellData()->Size() > 0)
234 {
235 using CellPixelType = typename MeshType::CellPixelType;
236 using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
237 const auto cellDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellDataBuffer().at(0)));
238 const auto cellDataSize =
239 wasmMesh->GetCellDataBuffer().size() * sizeof(typename ConvertCellPixelTraits::ComponentType);
240 setMemoryStoreOutputArray(0, index, 3, cellDataAddress, cellDataSize);
241 }
242 }
243#else
244 std::cerr << "Memory IO not supported" << std::endl;
245 abort();
246#endif
247 }
248 else
249 {
250#ifndef ITK_WASM_NO_FILESYSTEM_IO
251 if (!this->m_Mesh.IsNull() && !this->m_Identifier.empty())
252 {
253 using MeshWriterType = itk::MeshFileWriter<MeshType>;
254 auto meshWriter = MeshWriterType::New();
255 meshWriter->SetFileName(this->m_Identifier);
256 meshWriter->SetInput(this->m_Mesh);
257 meshWriter->Update();
258 }
259#else
260 std::cerr << "Filesystem IO not supported" << std::endl;
261 abort();
262#endif
263 }
264 }
265
266protected:
268
269 std::string m_Identifier;
270};
271
272template <typename TMesh>
273bool
274lexical_cast(const std::string & input, OutputMesh<TMesh> & outputMesh)
275{
276 outputMesh.SetIdentifier(input);
277 return true;
278}
279
280} // namespace wasm
281} // namespace itk
282
283#endif
Traits class used to by ConvertPixels to convert blocks of pixels.
Writes mesh data to a single file.
Convert an Mesh to an WasmMesh object.
typename MeshTraits::CellIdentifier CellIdentifier
Definition itkMesh.h:178
typename MeshTraits::CellPixelType CellPixelType
Definition itkMesh.h:148
typename MeshTraits::PointIdentifier PointIdentifier
Mesh class for 2D manifolds embedded in ND space.
SmartPointer< const Self > ConstPointer
Output mesh for an itk::wasm::Pipeline.
const MeshType * Get() const
const std::string & GetIdentifier() const
void Set(const MeshType *mesh)
TMesh::ConstPointer m_Mesh
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....