vsgXchange 1.1.9
VulkanSceneGraph 3rd party data integration library
Loading...
Searching...
No Matches
3DTiles.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2025 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shimages be included in images
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24</editor-fold> */
25
26#include <vsg/threading/OperationThreads.h>
27
28#include <vsgXchange/gltf.h>
29
30namespace vsgXchange
31{
32
35 class VSGXCHANGE_DECLSPEC Tiles3D : public vsg::Inherit<vsg::ReaderWriter, Tiles3D>
36 {
37 public:
38 Tiles3D();
39
40 vsg::ref_ptr<vsg::Object> read(const vsg::Path&, vsg::ref_ptr<const vsg::Options>) const override;
41 vsg::ref_ptr<vsg::Object> read(std::istream&, vsg::ref_ptr<const vsg::Options>) const override;
42 vsg::ref_ptr<vsg::Object> read(const uint8_t* ptr, size_t size, vsg::ref_ptr<const vsg::Options> options = {}) const override;
43
44 vsg::ref_ptr<vsg::Object> read_json(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
45 vsg::ref_ptr<vsg::Object> read_b3dm(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
46 vsg::ref_ptr<vsg::Object> read_cmpt(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
47 vsg::ref_ptr<vsg::Object> read_i3dm(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
48 vsg::ref_ptr<vsg::Object> read_pnts(std::istream&, vsg::ref_ptr<const vsg::Options>, const vsg::Path& filename = {}) const;
49 vsg::ref_ptr<vsg::Object> read_tiles(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options) const;
50
51 vsg::Logger::Level level = vsg::Logger::LOGGER_WARN;
52
53 bool supportedExtension(const vsg::Path& ext) const;
54
55 bool getFeatures(Features& features) const override;
56
57 static constexpr const char* report = "report";
58 static constexpr const char* instancing = "instancing";
59 static constexpr const char* pixel_ratio = "pixel_ratio";
60 static constexpr const char* pre_load_level = "pre_load_level";
61
62 bool readOptions(vsg::Options& options, vsg::CommandLine& arguments) const override;
63
64 public:
66 struct VSGXCHANGE_DECLSPEC BoundingVolume : public vsg::Inherit<gltf::ExtensionsExtras, BoundingVolume>
67 {
68 vsg::ValuesSchema<double> box;
69 vsg::ValuesSchema<double> region;
70 vsg::ValuesSchema<double> sphere;
71
72 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
73
74 void report(vsg::LogOutput& output);
75 };
76
78 struct VSGXCHANGE_DECLSPEC Content : public vsg::Inherit<gltf::ExtensionsExtras, Content>
79 {
80 vsg::ref_ptr<BoundingVolume> boundingVolume;
81 std::string uri;
82
83 // loaded from uri
84 vsg::ref_ptr<vsg::Object> object;
85
86 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
87 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
88
89 void report(vsg::LogOutput& output);
90 };
91
93 struct VSGXCHANGE_DECLSPEC Tile : public vsg::Inherit<gltf::ExtensionsExtras, Tile>
94 {
95 vsg::ref_ptr<BoundingVolume> boundingVolume;
96 vsg::ref_ptr<BoundingVolume> viewerRequestVolume;
97 double geometricError = 0.0;
98 std::string refine;
99 vsg::ValuesSchema<double> transform;
100 vsg::ObjectsSchema<Tile> children;
101 vsg::ref_ptr<Content> content;
102
103 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
104 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
105 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
106 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
107
108 void report(vsg::LogOutput& output);
109 };
110
112 struct VSGXCHANGE_DECLSPEC PropertyRange : public vsg::Inherit<gltf::ExtensionsExtras, PropertyRange>
113 {
114 double minimum = 0.0;
115 double maximum = 0.0;
116
117 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
118 };
119
120 struct VSGXCHANGE_DECLSPEC Properties : public vsg::Inherit<gltf::ExtensionsExtras, Properties>
121 {
122 std::map<std::string, PropertyRange> properties;
123
124 void read_object(vsg::JSONParser& parser, const std::string_view& property_name) override;
125
126 void report(vsg::LogOutput& output);
127 };
128
130 struct VSGXCHANGE_DECLSPEC Asset : public vsg::Inherit<gltf::ExtensionsExtras, Asset>
131 {
132 std::string version;
133 std::string tilesetVersion;
134
135 std::map<std::string, std::string> strings;
136 std::map<std::string, double> numbers;
137
138 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
139 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
140 void report(vsg::LogOutput& output);
141 };
142
144 struct VSGXCHANGE_DECLSPEC Tileset : public vsg::Inherit<gltf::ExtensionsExtras, Tileset>
145 {
146 vsg::ref_ptr<Asset> asset;
147 vsg::ref_ptr<Properties> properties;
148 vsg::ref_ptr<Tile> root;
149 double geometricError = 0.0;
150 vsg::StringsSchema extensionsUsed;
151 vsg::StringsSchema extensionsRequired;
152
153 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
154 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
155 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
156
157 void report(vsg::LogOutput& output);
158
159 virtual void resolveURIs(vsg::ref_ptr<const vsg::Options> options);
160 };
161
163 template<typename T>
164 struct ArraySchema : public Inherit<vsg::JSONParser::Schema, ArraySchema<T>>
165 {
166 const uint32_t invalidOffset = std::numeric_limits<uint32_t>::max();
167 uint32_t byteOffset = invalidOffset;
168 std::vector<T> values;
169
170 void read_number(vsg::JSONParser&, std::istream& input) override
171 {
172 T value;
173 input >> value;
174 values.push_back(value);
175 }
176
177 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override
178 {
179 if (property == "byteOffset")
180 input >> byteOffset;
181 else
182 parser.warning();
183 }
184
185 void assign(vsg::ubyteArray& binary, uint32_t count)
186 {
187 if (!values.empty() || byteOffset == invalidOffset) return;
188
189 T* ptr = reinterpret_cast<T*>(binary.data() + byteOffset);
190 for (uint32_t i = 0; i < count; ++i)
191 {
192 values.push_back(*(ptr++));
193 }
194 }
195
196 explicit operator bool() const noexcept { return !values.empty(); }
197 };
198
199 struct VSGXCHANGE_DECLSPEC b3dm_FeatureTable : public vsg::Inherit<gltf::ExtensionsExtras, b3dm_FeatureTable>
200 {
201 // storage for binary section
202 vsg::ref_ptr<vsg::ubyteArray> binary;
203
204 uint32_t BATCH_LENGTH = 0;
205 ArraySchema<float> RTC_CENTER;
206 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
207 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
208 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
209
210 void convert();
211
212 void report(vsg::LogOutput& output);
213 };
214
215 struct BatchTable;
216
217 struct VSGXCHANGE_DECLSPEC Batch : public vsg::Inherit<vsg::JSONtoMetaDataSchema, Batch>
218 {
219 uint32_t byteOffset = 0;
220 std::string componentType;
221 std::string type;
222
223 void convert(BatchTable& batchTable);
224
225 // read array parts
226 void read_number(vsg::JSONParser& parser, std::istream& input) override;
227
228 // read object parts
229 void read_string(vsg::JSONParser& parser, const std::string_view& property) override;
230 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
231 };
232
233 struct VSGXCHANGE_DECLSPEC BatchTable : public vsg::Inherit<gltf::ExtensionsExtras, BatchTable>
234 {
235 std::map<std::string, vsg::ref_ptr<Batch>> batches;
236
237 uint32_t length = 0;
238 vsg::ref_ptr<vsg::ubyteArray> binary;
239
240 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
241 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
242
243 void convert();
244
245 void report(vsg::LogOutput& output);
246 };
247
248 // https://github.com/CesiumGS/3d-tiles/blob/main/specification/TileFormats/Instanced3DModel/README.adoc
249 struct VSGXCHANGE_DECLSPEC i3dm_FeatureTable : public vsg::Inherit<gltf::ExtensionsExtras, i3dm_FeatureTable>
250 {
251 // storage for binary section
252 vsg::ref_ptr<vsg::ubyteArray> binary;
253
254 // Instance sematics
255 ArraySchema<float> POSITION;
256 ArraySchema<uint16_t> POSITION_QUANTIZED;
257 ArraySchema<float> NORMAL_UP;
258 ArraySchema<float> NORMAL_RIGHT;
259 ArraySchema<uint16_t> NORMAL_UP_OCT32P;
260 ArraySchema<uint16_t> NORMAL_RIGHT_OCT32P;
261 ArraySchema<float> SCALE;
262 ArraySchema<float> SCALE_NON_UNIFORM;
263 ArraySchema<uint32_t> BATCH_ID;
264
265 // Global sematics
266 uint32_t INSTANCES_LENGTH = 0;
267 ArraySchema<float> RTC_CENTER;
268 ArraySchema<float> QUANTIZED_VOLUME_OFFSET;
269 ArraySchema<float> QUANTIZED_VOLUME_SCALE;
270 bool EAST_NORTH_UP = false;
271
272 void read_array(vsg::JSONParser& parser, const std::string_view& property) override;
273 void read_object(vsg::JSONParser& parser, const std::string_view& property) override;
274 void read_number(vsg::JSONParser& parser, const std::string_view& property, std::istream& input) override;
275 void read_bool(vsg::JSONParser& parser, const std::string_view& property, bool value) override;
276
277 void convert();
278
279 void report(vsg::LogOutput& output);
280 };
281
282 public:
283 class VSGXCHANGE_DECLSPEC SceneGraphBuilder : public vsg::Inherit<vsg::Object, SceneGraphBuilder>
284 {
285 public:
286 SceneGraphBuilder();
287
288 vsg::ref_ptr<vsg::Options> options;
289 vsg::ref_ptr<vsg::ShaderSet> shaderSet;
290 vsg::ref_ptr<vsg::SharedObjects> sharedObjects;
291 vsg::ref_ptr<vsg::OperationThreads> operationThreads;
292
293 vsg::ref_ptr<vsg::EllipsoidModel> ellipsoidModel = vsg::EllipsoidModel::create();
294 vsg::CoordinateConvention source_coordinateConvention = vsg::CoordinateConvention::Y_UP;
295 double pixelErrorToScreenHeightRatio = 0.016; // 0.016 looks to replicate vsgCs worldviewer transition distances
296 uint32_t preLoadLevel = 1;
297
298 virtual void assignResourceHints(vsg::ref_ptr<vsg::Node> node);
299
300 virtual vsg::dmat4 createMatrix(const std::vector<double>& values);
301 virtual vsg::dsphere createBound(vsg::ref_ptr<BoundingVolume> boundingVolume);
302 virtual vsg::ref_ptr<vsg::Node> readTileChildren(vsg::ref_ptr<Tiles3D::Tile> tile, uint32_t level, const std::string& inherited_refine);
303 virtual vsg::ref_ptr<vsg::Node> createTile(vsg::ref_ptr<Tiles3D::Tile> tile, uint32_t level, const std::string& inherited_refine);
304 virtual vsg::ref_ptr<vsg::Object> createSceneGraph(vsg::ref_ptr<Tiles3D::Tileset> tileset, vsg::ref_ptr<const vsg::Options> in_options);
305 };
306 };
307
308} // namespace vsgXchange
309
310EVSG_type_name(vsgXchange::Tiles3D)
Definition 3DTiles.h:36
bool readOptions(vsg::Options &options, vsg::CommandLine &arguments) const override
uint, sets the SceneGraphBuilder::preLoadLevel values to control what LOD level are pre loaded when r...
static constexpr const char * instancing
bool, report parsed glTF to console, defaults to false
Definition 3DTiles.h:58
static constexpr const char * pixel_ratio
bool, hint for using vsg::InstanceNode/InstanceDraw for instancing where possible.
Definition 3DTiles.h:59
static constexpr const char * pre_load_level
double, sets the SceneGraphBuilder::pixelErrorToScreenHeightRatio value used for setting LOD ranges.
Definition 3DTiles.h:60
Template class for reading an array of values from JSON or from a binary block.
Definition 3DTiles.h:165
https://github.com/CesiumGS/3d-tiles/blob/main/specification/schema/asset.schema.json
Definition 3DTiles.h:131
Definition 3DTiles.h:218
Definition 3DTiles.h:234
https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/schema/boundingVolume....
Definition 3DTiles.h:67
https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/schema/tile.content.schema....
Definition 3DTiles.h:79
Definition 3DTiles.h:121
https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/schema/properties.schema....
Definition 3DTiles.h:113
https://github.com/CesiumGS/3d-tiles/blob/1.0/specification/schema/tile.schema.json
Definition 3DTiles.h:94
https://github.com/CesiumGS/3d-tiles/blob/main/specification/schema/tileset.schema....
Definition 3DTiles.h:145