Skip to content

Commit d81379d

Browse files
committed
refactor(WasmMeshToMeshFilter): use glaze
1 parent 83772f2 commit d81379d

12 files changed

+436
-204
lines changed

include/itkFloatTypesJSON.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#ifndef itkFloatTypesJSON_h
1919
#define itkFloatTypesJSON_h
2020

21-
#include <vector>
22-
2321
#include "glaze/glaze.hpp"
2422

2523
namespace itk

include/itkIntTypesJSON.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 itkIntTypesJSON_h
19+
#define itkIntTypesJSON_h
20+
21+
#include "glaze/glaze.hpp"
22+
23+
namespace itk
24+
{
25+
enum class JSONIntTypesEnum
26+
{
27+
int8,
28+
uint8,
29+
int16,
30+
uint16,
31+
int32,
32+
uint32,
33+
int64,
34+
uint64
35+
};
36+
} // end namespace itk
37+
38+
template <>
39+
struct glz::meta<itk::JSONIntTypesEnum> {
40+
using enum itk::JSONIntTypesEnum;
41+
static constexpr auto value = glz::enumerate(
42+
int8,
43+
uint8,
44+
int16,
45+
uint16,
46+
int32,
47+
uint32,
48+
int64,
49+
uint64
50+
);
51+
};
52+
53+
#endif // itkIntTypesJSON_h

include/itkMeshJSON.h

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
21+
#include "itkMeshConvertPixelTraits.h"
22+
23+
#include "itkWasmMapComponentType.h"
24+
#include "itkWasmMapPixelType.h"
25+
#include "itkIntTypesJSON.h"
26+
#include "itkFloatTypesJSON.h"
27+
#include "itkPixelTypesJSON.h"
28+
#include "itkWasmMesh.h"
29+
30+
#include "glaze/glaze.hpp"
31+
32+
namespace itk
33+
{
34+
/** \class MeshTypeJSON
35+
*
36+
* \brief Mesh type JSON representation data structure.
37+
*
38+
* \ingroup WebAssemblyInterface
39+
*/
40+
struct MeshTypeJSON
41+
{
42+
unsigned int dimension { 2 };
43+
JSONFloatTypesEnum pointComponentType { JSONFloatTypesEnum::float32 };
44+
JSONComponentTypesEnum pointPixelComponentType { JSONComponentTypesEnum::float32 };
45+
JSONPixelTypesEnum pointPixelType { JSONPixelTypesEnum::Scalar };
46+
unsigned int pointPixelComponents { 1 };
47+
JSONIntTypesEnum cellComponentType { JSONIntTypesEnum::uint32 };
48+
JSONComponentTypesEnum cellPixelComponentType { JSONComponentTypesEnum::float32 };
49+
JSONPixelTypesEnum cellPixelType { JSONPixelTypesEnum::Scalar };
50+
unsigned int cellPixelComponents { 1 };
51+
};
52+
53+
/** \class MeshJSON
54+
*
55+
* \brief Mesh JSON representation data structure.
56+
*
57+
* \ingroup WebAssemblyInterface
58+
*/
59+
struct MeshJSON
60+
{
61+
MeshTypeJSON meshType;
62+
63+
std::string name;
64+
65+
size_t numberOfPoints{ 0 };
66+
std::string points;
67+
68+
size_t numberOfPointPixels{ 0 };
69+
std::string pointData;
70+
71+
size_t numberOfCells{ 0 };
72+
std::string cells;
73+
size_t cellBufferSize{ 0 };
74+
75+
size_t numberOfCellPixels{ 0 };
76+
std::string cellData;
77+
};
78+
79+
template<typename TMesh>
80+
auto meshToMeshJSON(const TMesh * mesh, const WasmMesh<TMesh> * wasmMesh, bool inMemory) -> MeshJSON
81+
{
82+
using MeshType = TMesh;
83+
84+
MeshJSON meshJSON;
85+
86+
meshJSON.meshType.dimension = MeshType::PointDimension;
87+
88+
meshJSON.meshType.pointComponentType = wasm::MapComponentType<typename MeshType::CoordRepType>::JSONFloatTypeEnum;
89+
using PointPixelType = typename TMesh::PixelType;
90+
using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
91+
meshJSON.meshType.pointPixelComponentType = wasm::MapComponentType<typename ConvertPointPixelTraits::ComponentType>::JSONComponentEnum;
92+
meshJSON.meshType.pointPixelType = wasm::MapPixelType<PointPixelType>::JSONPixelEnum;
93+
meshJSON.meshType.pointPixelComponents = ConvertPointPixelTraits::GetNumberOfComponents();
94+
meshJSON.meshType.cellComponentType = wasm::MapComponentType<typename MeshType::CellIdentifier>::JSONIntTypeEnum;
95+
using CellPixelType = typename TMesh::CellPixelType;
96+
using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
97+
meshJSON.meshType.cellPixelComponentType = wasm::MapComponentType<typename ConvertPointPixelTraits::ComponentType>::JSONComponentEnum;
98+
meshJSON.meshType.cellPixelType = wasm::MapPixelType<CellPixelType>::JSONPixelEnum;
99+
meshJSON.meshType.cellPixelComponents = ConvertCellPixelTraits::GetNumberOfComponents();
100+
101+
meshJSON.name = mesh->GetObjectName();
102+
meshJSON.numberOfPoints = mesh->GetNumberOfPoints();
103+
if (mesh->GetPointData() == nullptr)
104+
{
105+
meshJSON.numberOfPointPixels = 0;
106+
}
107+
else
108+
{
109+
meshJSON.numberOfPointPixels = mesh->GetPointData()->Size();
110+
}
111+
meshJSON.numberOfCells = mesh->GetNumberOfCells();
112+
if (mesh->GetCellData() == nullptr)
113+
{
114+
meshJSON.numberOfCellPixels = 0;
115+
}
116+
else
117+
{
118+
meshJSON.numberOfCellPixels = mesh->GetCellData()->Size();
119+
}
120+
meshJSON.cellBufferSize = wasmMesh->GetCellBuffer()->Size();
121+
122+
const auto pointsAddress = reinterpret_cast< size_t >( &(mesh->GetPoints()->at(0)) );
123+
std::ostringstream pointsStream;
124+
pointsStream << "data:application/vnd.itk.address,0:";
125+
pointsStream << pointsAddress;
126+
meshJSON.points = pointsStream.str();
127+
size_t cellsAddress = 0;
128+
if (mesh->GetNumberOfCells() > 0)
129+
{
130+
cellsAddress = reinterpret_cast< size_t >( &(wasmMesh->GetCellBuffer()->at(0)) );
131+
}
132+
std::ostringstream cellsStream;
133+
cellsStream << "data:application/vnd.itk.address,0:";
134+
cellsStream << cellsAddress;
135+
meshJSON.cells = cellsStream.str();
136+
137+
size_t pointDataAddress = 0;
138+
if (mesh->GetPointData() != nullptr && mesh->GetPointData()->Size() > 0)
139+
{
140+
pointDataAddress = reinterpret_cast< size_t >( &(mesh->GetPointData()->at(0)) );
141+
}
142+
std::ostringstream pointDataStream;
143+
pointDataStream << "data:application/vnd.itk.address,0:";
144+
pointDataStream << pointDataAddress;
145+
meshJSON.pointData = pointDataStream.str();
146+
147+
size_t cellDataAddress = 0;
148+
if (mesh->GetCellData() != nullptr && mesh->GetCellData()->Size() > 0)
149+
{
150+
cellDataAddress = reinterpret_cast< size_t >( &(mesh->GetCellData()->at(0)) );
151+
}
152+
std::ostringstream cellDataStream;
153+
cellDataStream << "data:application/vnd.itk.address,0:";
154+
cellDataStream << cellDataAddress;
155+
meshJSON.cellData = cellDataStream.str();
156+
157+
return meshJSON;
158+
}
159+
} // end namespace itk
160+
161+
#endif // itkMeshJSON_h

include/itkMeshToWasmMeshFilter.hxx

Lines changed: 9 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@
1818
#ifndef itkMeshToWasmMeshFilter_hxx
1919
#define itkMeshToWasmMeshFilter_hxx
2020

21-
#include "itkMeshToWasmMeshFilter.h"
22-
23-
#include "itkMeshConvertPixelTraits.h"
24-
25-
#include "itkWasmMapComponentType.h"
26-
#include "itkWasmMapPixelType.h"
27-
28-
#include "rapidjson/document.h"
29-
#include "rapidjson/stringbuffer.h"
30-
#include "rapidjson/writer.h"
21+
#include "glaze/glaze.hpp"
3122

3223
namespace itk
3324
{
@@ -135,134 +126,17 @@ MeshToWasmMeshFilter<TMesh>
135126
WasmMeshType * wasmMesh = this->GetOutput();
136127

137128
wasmMesh->SetMesh(mesh);
138-
139-
rapidjson::Document document;
140-
document.SetObject();
141-
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
142-
143-
rapidjson::Value meshType;
144-
meshType.SetObject();
145-
146-
constexpr unsigned int dimension = MeshType::PointDimension;
147-
meshType.AddMember("dimension", rapidjson::Value(dimension).Move(), allocator );
148-
149-
rapidjson::Value pointComponentType;
150-
pointComponentType.SetString( wasm::MapComponentType<typename MeshType::CoordRepType>::ComponentString.data(), allocator );
151-
meshType.AddMember("pointComponentType", pointComponentType.Move(), allocator );
152-
153-
using PointPixelType = typename TMesh::PixelType;
154-
using ConvertPointPixelTraits = MeshConvertPixelTraits<PointPixelType>;
155-
rapidjson::Value pointPixelComponentType;
156-
pointPixelComponentType.SetString( wasm::MapComponentType<typename ConvertPointPixelTraits::ComponentType>::ComponentString.data(), allocator );
157-
meshType.AddMember("pointPixelComponentType", pointPixelComponentType.Move(), allocator );
158-
159-
rapidjson::Value pointPixelType;
160-
pointPixelType.SetString( wasm::MapPixelType<PointPixelType>::PixelString.data(), allocator );
161-
meshType.AddMember("pointPixelType", pointPixelType.Move(), allocator );
162-
163-
meshType.AddMember("pointPixelComponents", rapidjson::Value( ConvertPointPixelTraits::GetNumberOfComponents() ).Move(), allocator );
164-
165-
rapidjson::Value cellComponentType;
166-
cellComponentType.SetString( wasm::MapComponentType<typename MeshType::CellsVectorContainer::Element>::ComponentString.data(),allocator );
167-
meshType.AddMember("cellComponentType", cellComponentType.Move(), allocator );
168-
169-
using CellPixelType = typename TMesh::CellPixelType;
170-
using ConvertCellPixelTraits = MeshConvertPixelTraits<CellPixelType>;
171-
rapidjson::Value cellPixelComponentType;
172-
cellPixelComponentType.SetString( wasm::MapComponentType<typename ConvertCellPixelTraits::ComponentType>::ComponentString.data(), allocator );
173-
meshType.AddMember("cellPixelComponentType", cellPixelComponentType.Move(), allocator );
174-
175-
rapidjson::Value cellPixelType;
176-
cellPixelType.SetString( wasm::MapPixelType<CellPixelType>::PixelString.data(), allocator );
177-
meshType.AddMember("cellPixelType", cellPixelType, allocator );
178-
179-
meshType.AddMember("cellPixelComponents", rapidjson::Value( ConvertCellPixelTraits::GetNumberOfComponents() ).Move(), allocator );
180-
181-
document.AddMember( "meshType", meshType.Move(), allocator );
182-
183-
rapidjson::Value numberOfPoints;
184-
numberOfPoints.SetInt( mesh->GetNumberOfPoints() );
185-
document.AddMember( "numberOfPoints", numberOfPoints.Move(), allocator );
186-
187-
rapidjson::Value numberOfPointPixels;
188-
if (mesh->GetPointData() == nullptr)
189-
{
190-
numberOfPointPixels.SetInt( 0 );
191-
}
192-
else
129+
constexpr bool inMemory = true;
130+
const auto meshJSON = meshToMeshJSON<MeshType>(mesh, wasmMesh, inMemory);
131+
std::string serialized{};
132+
auto ec = glz::write<glz::opts{ .prettify = true }>(meshJSON, serialized);
133+
if (ec)
193134
{
194-
numberOfPointPixels.SetInt( mesh->GetPointData()->Size() );
135+
itkExceptionMacro("Failed to serialize TransformListJSON");
195136
}
196-
document.AddMember( "numberOfPointPixels", numberOfPointPixels.Move(), allocator );
197-
198-
rapidjson::Value numberOfCells;
199-
numberOfCells.SetInt( mesh->GetNumberOfCells() );
200-
document.AddMember( "numberOfCells", numberOfCells.Move(), allocator );
201-
202-
rapidjson::Value numberOfCellPixels;
203-
if (mesh->GetCellData() == nullptr)
204-
{
205-
numberOfCellPixels.SetInt( 0 );
206-
}
207-
else
208-
{
209-
numberOfCellPixels.SetInt( mesh->GetCellData()->Size() );
210-
}
211-
document.AddMember( "numberOfCellPixels", numberOfCellPixels.Move(), allocator );
212-
213-
rapidjson::Value cellBufferSizeMember;
214-
cellBufferSizeMember.SetInt( wasmMesh->GetCellBuffer()->Size() );
215-
document.AddMember( "cellBufferSize", cellBufferSizeMember.Move(), allocator );
216-
217-
const auto pointsAddress = reinterpret_cast< size_t >( &(mesh->GetPoints()->at(0)) );
218-
std::ostringstream pointsStream;
219-
pointsStream << "data:application/vnd.itk.address,0:";
220-
pointsStream << pointsAddress;
221-
rapidjson::Value pointsString;
222-
pointsString.SetString( pointsStream.str().c_str(), allocator );
223-
document.AddMember( "points", pointsString.Move(), allocator );
224-
225-
size_t cellsAddress = 0;
226-
if (mesh->GetNumberOfCells() > 0)
227-
{
228-
cellsAddress = reinterpret_cast< size_t >( &(wasmMesh->GetCellBuffer()->at(0)) );
229-
}
230-
std::ostringstream cellsStream;
231-
cellsStream << "data:application/vnd.itk.address,0:";
232-
cellsStream << cellsAddress;
233-
rapidjson::Value cellsString;
234-
cellsString.SetString( cellsStream.str().c_str(), allocator );
235-
document.AddMember( "cells", cellsString.Move(), allocator );
236-
237-
size_t pointDataAddress = 0;
238-
if (mesh->GetPointData() != nullptr && mesh->GetPointData()->Size() > 0)
239-
{
240-
pointDataAddress = reinterpret_cast< size_t >( &(mesh->GetPointData()->at(0)) );
241-
}
242-
std::ostringstream pointDataStream;
243-
pointDataStream << "data:application/vnd.itk.address,0:";
244-
pointDataStream << pointDataAddress;
245-
rapidjson::Value pointDataString;
246-
pointDataString.SetString( pointDataStream.str().c_str(), allocator );
247-
document.AddMember( "pointData", pointDataString.Move(), allocator );
248-
249-
size_t cellDataAddress = 0;
250-
if (mesh->GetCellData() != nullptr && mesh->GetCellData()->Size() > 0)
251-
{
252-
cellDataAddress = reinterpret_cast< size_t >( &(mesh->GetCellData()->at(0)) );
253-
}
254-
std::ostringstream cellDataStream;
255-
cellDataStream << "data:application/vnd.itk.address,0:";
256-
cellDataStream << cellDataAddress;
257-
rapidjson::Value cellDataString;
258-
cellDataString.SetString( cellDataStream.str().c_str(), allocator );
259-
document.AddMember( "cellData", cellDataString.Move(), allocator );
260-
261-
rapidjson::StringBuffer stringBuffer;
262-
rapidjson::Writer<rapidjson::StringBuffer> writer(stringBuffer);
263-
document.Accept(writer);
137+
std::cout << "SERIALIZED: " << serialized << std::endl;
264138

265-
wasmMesh->SetJSON(stringBuffer.GetString());
139+
wasmMesh->SetJSON(serialized);
266140
}
267141

268142
template <typename TMesh>

0 commit comments

Comments
 (0)