ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkMeshJSON.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 itkMeshJSON_h
19#define itkMeshJSON_h
20
22
24#include "itkWasmMapPixelType.h"
25#include "itkIntTypesJSON.h"
26#include "itkFloatTypesJSON.h"
27#include "itkPixelTypesJSON.h"
28#include "itkWasmMesh.h"
30#include "itkQuadEdge.h"
31
32#include "glaze/glaze.hpp"
33
34namespace itk
35{
54
62{
64
65 std::string name{ "Mesh" };
66
67 size_t numberOfPoints{ 0 };
68 std::string points;
69
71 std::string pointData;
72
73 size_t numberOfCells{ 0 };
74 std::string cells;
75 size_t cellBufferSize{ 0 };
76
77 size_t numberOfCellPixels{ 0 };
78 std::string cellData;
79
81};
82
83template <typename TMesh>
84auto
85meshToMeshJSON(const TMesh * mesh, const WasmMesh<TMesh> * wasmMesh, bool inMemory) -> MeshJSON
86{
87 using MeshType = TMesh;
88
89 MeshJSON meshJSON;
90
91 meshJSON.meshType.dimension = MeshType::PointDimension;
92
94 using PointPixelType = typename TMesh::PixelType;
95 using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
99 meshJSON.meshType.pointPixelComponents = ConvertPointPixelTraits::GetNumberOfComponents();
101 using CellPixelType = typename TMesh::CellPixelType;
102 using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
106 meshJSON.meshType.cellPixelComponents = ConvertCellPixelTraits::GetNumberOfComponents();
107
108 meshJSON.name = mesh->GetObjectName();
109 meshJSON.numberOfPoints = mesh->GetNumberOfPoints();
110 if (mesh->GetPointData() == nullptr)
111 {
112 meshJSON.numberOfPointPixels = 0;
113 }
114 else
115 {
116 meshJSON.numberOfPointPixels = mesh->GetPointData()->Size();
117 }
118 meshJSON.numberOfCells = mesh->GetNumberOfCells();
119 if (mesh->GetCellData() == nullptr)
120 {
121 meshJSON.numberOfCellPixels = 0;
122 }
123 else
124 {
125 meshJSON.numberOfCellPixels = mesh->GetCellData()->Size();
126 }
127 if (inMemory)
128 {
129 meshJSON.cellBufferSize = wasmMesh->GetCellBuffer()->Size();
130
131 const auto pointsAddress = reinterpret_cast<size_t>(&(mesh->GetPoints()->at(0)));
132 std::ostringstream pointsStream;
133 pointsStream << "data:application/vnd.itk.address,0:";
134 pointsStream << pointsAddress;
135 meshJSON.points = pointsStream.str();
136 size_t cellsAddress = 0;
137 if (mesh->GetNumberOfCells() > 0)
138 {
139 cellsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellBuffer()->at(0)));
140 }
141 std::ostringstream cellsStream;
142 cellsStream << "data:application/vnd.itk.address,0:";
143 cellsStream << cellsAddress;
144 meshJSON.cells = cellsStream.str();
145
146 size_t pointDataAddress = 0;
147 if (mesh->GetPointData() != nullptr && mesh->GetPointData()->Size() > 0)
148 {
149 pointDataAddress = reinterpret_cast<size_t>(&(mesh->GetPointData()->at(0)));
150 }
151 std::ostringstream pointDataStream;
152 pointDataStream << "data:application/vnd.itk.address,0:";
153 pointDataStream << pointDataAddress;
154 meshJSON.pointData = pointDataStream.str();
155
156 size_t cellDataAddress = 0;
157 if (mesh->GetCellData() != nullptr && mesh->GetCellData()->Size() > 0)
158 {
159 cellDataAddress = reinterpret_cast<size_t>(&(mesh->GetCellData()->at(0)));
160 }
161 std::ostringstream cellDataStream;
162 cellDataStream << "data:application/vnd.itk.address,0:";
163 cellDataStream << cellDataAddress;
164 meshJSON.cellData = cellDataStream.str();
165 }
166 else
167 {
168 meshJSON.points = "data:application/vnd.itk.path,data/points.raw";
169 meshJSON.cells = "data:application/vnd.itk.path,data/cells.raw";
170 meshJSON.pointData = "data:application/vnd.itk.path,data/point-data.raw";
171 meshJSON.cellData = "data:application/vnd.itk.path,data/cell-data.raw";
172 }
173
174 auto dictionary = mesh->GetMetaDataDictionary();
175 metaDataDictionaryToJSON(dictionary, meshJSON.metadata);
176
177 return meshJSON;
178}
179
180template <typename TPixel, unsigned int VDimension>
181auto
184 bool inMemory) -> MeshJSON
185{
186 using MeshType = QuadEdgeMesh<TPixel, VDimension>;
187
188 MeshJSON meshJSON;
189
190 meshJSON.meshType.dimension = MeshType::PointDimension;
191
193 using PointPixelType = typename MeshType::PixelType;
194 using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
198 meshJSON.meshType.pointPixelComponents = ConvertPointPixelTraits::GetNumberOfComponents();
200 using CellPixelType = typename MeshType::CellPixelType;
201 using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
205 meshJSON.meshType.cellPixelComponents = ConvertCellPixelTraits::GetNumberOfComponents();
206
207 meshJSON.name = mesh->GetObjectName();
208 meshJSON.numberOfPoints = mesh->GetNumberOfPoints();
209 if (mesh->GetPointData() == nullptr)
210 {
211 meshJSON.numberOfPointPixels = 0;
212 }
213 else
214 {
215 meshJSON.numberOfPointPixels = mesh->GetPointData()->Size();
216 }
217 meshJSON.numberOfCells = mesh->GetNumberOfCells();
218 if (mesh->GetCellData() == nullptr)
219 {
220 meshJSON.numberOfCellPixels = 0;
221 }
222 else
223 {
224 meshJSON.numberOfCellPixels = mesh->GetCellData()->Size();
225 }
226 if (inMemory)
227 {
228 meshJSON.cellBufferSize = wasmMesh->GetCellBuffer()->Size();
229
230 const auto pointsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetPointsBuffer().at(0)));
231 std::ostringstream pointsStream;
232 pointsStream << "data:application/vnd.itk.address,0:";
233 pointsStream << pointsAddress;
234 meshJSON.points = pointsStream.str();
235 size_t cellsAddress = 0;
236 if (mesh->GetNumberOfCells() > 0)
237 {
238 cellsAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellBuffer()->at(0)));
239 }
240 std::ostringstream cellsStream;
241 cellsStream << "data:application/vnd.itk.address,0:";
242 cellsStream << cellsAddress;
243 meshJSON.cells = cellsStream.str();
244
245 size_t pointDataAddress = 0;
246 if (mesh->GetPointData() != nullptr && mesh->GetPointData()->Size() > 0)
247 {
248 pointDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetPointDataBuffer().at(0)));
249 }
250 std::ostringstream pointDataStream;
251 pointDataStream << "data:application/vnd.itk.address,0:";
252 pointDataStream << pointDataAddress;
253 meshJSON.pointData = pointDataStream.str();
254
255 size_t cellDataAddress = 0;
256 if (mesh->GetCellData() != nullptr && mesh->GetCellData()->Size() > 0)
257 {
258 cellDataAddress = reinterpret_cast<size_t>(&(wasmMesh->GetCellDataBuffer().at(0)));
259 }
260 std::ostringstream cellDataStream;
261 cellDataStream << "data:application/vnd.itk.address,0:";
262 cellDataStream << cellDataAddress;
263 meshJSON.cellData = cellDataStream.str();
264 }
265 else
266 {
267 meshJSON.points = "data:application/vnd.itk.path,data/points.raw";
268 meshJSON.cells = "data:application/vnd.itk.path,data/cells.raw";
269 meshJSON.pointData = "data:application/vnd.itk.path,data/point-data.raw";
270 meshJSON.cellData = "data:application/vnd.itk.path,data/cell-data.raw";
271 }
272
273 auto dictionary = mesh->GetMetaDataDictionary();
274 metaDataDictionaryToJSON(dictionary, meshJSON.metadata);
275
276 return meshJSON;
277}
278} // end namespace itk
279
280#endif // itkMeshJSON_h
Traits class used to by ConvertPixels to convert blocks of pixels.
Mesh class for 2D manifolds embedded in ND space.
JSON representation for an itk::Mesh.
Definition itkWasmMesh.h:45
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
WebAssemblyInterface_EXPORT void metaDataDictionaryToJSON(const itk::MetaDataDictionary &dictionary, MetadataJSON &metadataJson)
std::vector< MetadataEntryJSON > MetadataJSON
auto meshToMeshJSON(const TMesh *mesh, const WasmMesh< TMesh > *wasmMesh, bool inMemory) -> MeshJSON
Definition itkMeshJSON.h:85
Mesh JSON representation data structure.
Definition itkMeshJSON.h:62
vcl_size_t numberOfCells
Definition itkMeshJSON.h:73
std::string name
Definition itkMeshJSON.h:65
std::string pointData
Definition itkMeshJSON.h:71
vcl_size_t numberOfPointPixels
Definition itkMeshJSON.h:70
MeshTypeJSON meshType
Definition itkMeshJSON.h:63
vcl_size_t cellBufferSize
Definition itkMeshJSON.h:75
std::string cells
Definition itkMeshJSON.h:74
MetadataJSON metadata
Definition itkMeshJSON.h:80
std::string points
Definition itkMeshJSON.h:68
std::string cellData
Definition itkMeshJSON.h:78
vcl_size_t numberOfCellPixels
Definition itkMeshJSON.h:77
vcl_size_t numberOfPoints
Definition itkMeshJSON.h:67
Mesh type JSON representation data structure.
Definition itkMeshJSON.h:43
unsigned int dimension
Definition itkMeshJSON.h:44
JSONPixelTypesEnum pointPixelType
Definition itkMeshJSON.h:47
JSONComponentTypesEnum pointPixelComponentType
Definition itkMeshJSON.h:46
unsigned int cellPixelComponents
Definition itkMeshJSON.h:52
JSONFloatTypesEnum pointComponentType
Definition itkMeshJSON.h:45
JSONPixelTypesEnum cellPixelType
Definition itkMeshJSON.h:51
unsigned int pointPixelComponents
Definition itkMeshJSON.h:48
JSONComponentTypesEnum cellPixelComponentType
Definition itkMeshJSON.h:50
JSONIntTypesEnum cellComponentType
Definition itkMeshJSON.h:49
static constexpr JSONIntTypesEnum JSONIntTypeEnum
static constexpr JSONComponentTypesEnum JSONComponentEnum
static constexpr JSONFloatTypesEnum JSONFloatTypeEnum
static constexpr JSONPixelTypesEnum JSONPixelEnum