|
18 | 18 | #ifndef itkMeshToWasmMeshFilter_hxx
|
19 | 19 | #define itkMeshToWasmMeshFilter_hxx
|
20 | 20 |
|
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" |
31 | 22 |
|
32 | 23 | namespace itk
|
33 | 24 | {
|
@@ -135,134 +126,17 @@ MeshToWasmMeshFilter<TMesh>
|
135 | 126 | WasmMeshType * wasmMesh = this->GetOutput();
|
136 | 127 |
|
137 | 128 | 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) |
193 | 134 | {
|
194 |
| - numberOfPointPixels.SetInt( mesh->GetPointData()->Size() ); |
| 135 | + itkExceptionMacro("Failed to serialize TransformListJSON"); |
195 | 136 | }
|
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; |
264 | 138 |
|
265 |
| - wasmMesh->SetJSON(stringBuffer.GetString()); |
| 139 | + wasmMesh->SetJSON(serialized); |
266 | 140 | }
|
267 | 141 |
|
268 | 142 | template <typename TMesh>
|
|
0 commit comments