vgl 1.0.0
Virtual Globe Library: API reference
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Jeremiah van Oosten
3//
4// C++ wrapper for vgl/context.h (docs/c-api.md, section 16).
5
6#ifndef VGL_CONTEXT_HPP
7#define VGL_CONTEXT_HPP
8
9#include <vgl/base.hpp>
10#include <vgl/context.h>
11#include <vgl/foundation.hpp>
12#include <vgl/render.h>
13
14#include <cassert>
15#include <cstdint>
16#include <limits>
17#include <span>
18#include <utility>
19#include <vector>
20
21namespace vgl
22{
23
30
36
37// A height from context::height_query. status is result::success when the height is final,
38// or result::not_ready while finer data loads; height is NaN until any data has loaded.
40{
41 double height = std::numeric_limits<double>::quiet_NaN();
43};
44
45// Where a ray meets the terrain, from context::ray_query. status is result::success when the
46// hit is final, or result::not_ready while finer data at it loads.
52
53// The texture formats a consumer can sample (vgl_texture_format).
63
64static_assert(sizeof(texture_format) == sizeof(vgl_texture_format));
65
71
72// Where an ECEF position appears in a view's viewport: x and y in pixels from its top left, y
73// downwards, and the clip-space w, the distance along the view axis for a perspective
74// projection (vgl_view_project). Behind the camera is result::not_found.
76 const vgl_dvec3& position) noexcept
77{
79 const auto r = static_cast<result>(vgl_view_project(&view, &position, &window));
80 if (failed(r))
81 {
82 return unexpected(r);
83 }
84 return window;
85}
86
87// Where a geodetic position (radians, height above the ellipsoid) appears in a view's
88// viewport, on an ellipsoid, WGS84 by default (vgl_view_geodetic_project).
89[[nodiscard]] inline expected<vgl_dvec3>
91 const vgl_geodetic& geodetic,
92 const vgl_ellipsoid* ellipsoid = nullptr) noexcept
93{
95 const auto r =
96 static_cast<result>(vgl_view_geodetic_project(&view, ellipsoid, &geodetic, &window));
97 if (failed(r))
98 {
99 return unexpected(r);
100 }
101 return window;
102}
103
104// Owns a vgl_context.
106{
107public:
110 context(const context&) = delete;
111 context& operator=(const context&) = delete;
112 context(context&& other) noexcept : handle_(other.release()) {}
114 {
115 if (this != &other)
116 {
117 reset(other.release());
118 }
119 return *this;
120 }
122
123 // Creates a context. info may be null for all defaults.
126 {
127 vgl_context* handle = nullptr;
128 const auto r = static_cast<result>(vgl_context_create(info, &handle));
129 if (failed(r))
130 {
131 return unexpected(r);
132 }
133 return context(handle);
134 }
135
136 [[nodiscard]] vgl_context* get() const noexcept { return handle_; }
137 [[nodiscard]] explicit operator bool() const noexcept { return handle_ != nullptr; }
138
139 [[nodiscard]] vgl_context* release() noexcept { return std::exchange(handle_, nullptr); }
140
142 {
143 static_cast<void>(vgl_context_destroy(std::exchange(handle_, handle)));
144 }
145
146 // Sets the view that update selects tiles for; null stops selection.
147 [[nodiscard]] result view_set(const vgl_view* view) noexcept
148 {
149 return static_cast<result>(vgl_context_view_set(handle_, view));
150 }
151
152 // The view set with view_set, or result::not_found without one.
154 {
155 auto view = init<vgl_view>();
156 const auto r = static_cast<result>(vgl_context_view_get(handle_, &view));
157 if (r != result::success)
158 {
159 return unexpected(r);
160 }
161 return view;
162 }
163
164 // The allocator of the context: the host's, or the C heap's. The context must be valid.
166 {
168 assert(handle_ != nullptr);
169 static_cast<void>(vgl_context_allocator_get(handle_, &table));
170 return table;
171 }
172
173 // Passes a message to the context's log callback.
174 [[nodiscard]] result log(log_level level, const char* message) const noexcept
175 {
176 return static_cast<result>(
177 vgl_context_log(handle_, static_cast<vgl_log_level>(level), message));
178 }
179
180 // The texture formats the consumer can sample, most preferred first (vgl/render.h).
181 [[nodiscard]] result texture_formats_set(std::span<const texture_format> formats) noexcept
182 {
183 // The two enumerations have the same size and values.
184 return static_cast<result>(vgl_context_texture_formats_set(
185 handle_,
186 reinterpret_cast<const vgl_texture_format*>(formats.data()),
187 static_cast<std::uint32_t>(formats.size())));
188 }
189
190 // The credits of the data in view, each line once (vgl_context_attribution_get). The
191 // strings stay valid until the pack they come from is destroyed. Empty when the context
192 // is not valid. Throws std::bad_alloc if the vector cannot be allocated.
193 [[nodiscard]] std::vector<const char*> attribution_get() const
194 {
195 std::uint32_t count = 0;
196 if (vgl_context_attribution_get(handle_, &count, nullptr) != VGL_RESULT_SUCCESS ||
197 count == 0)
198 {
199 return {};
200 }
201 std::vector<const char*> lines(count);
202 static_cast<void>(vgl_context_attribution_get(handle_, &count, lines.data()));
203 lines.resize(count);
204 return lines;
205 }
206
207 // What the context holds and does. The context must be valid.
209 {
211 assert(handle_ != nullptr);
212 static_cast<void>(vgl_context_statistics_get(handle_, &s));
213 return s;
214 }
215
216 // Forgets every render resource, after the consumer lost them (vgl/render.h).
218 {
219 return static_cast<result>(vgl_context_resources_reset(handle_));
220 }
221
222 // Takes completed work, decodes within the time budget, and selects tiles for the view.
223 // info may be null.
225 {
226 return static_cast<result>(vgl_context_update(handle_, info));
227 }
228
229 // Picking: where a ray in ECEF meets the terrain. A miss is result::not_found.
231 const vgl_dvec3& direction) noexcept
232 {
234 q.status = static_cast<result>(vgl_context_ray_query(handle_, &origin, &direction, &q.hit));
235 if (failed(q.status))
236 {
237 return unexpected(q.status);
238 }
239 return q;
240 }
241
242 // Latitude and longitude in radians.
244 height_query(double latitude,
245 double longitude,
247 {
249 sample.status = static_cast<result>(
251 latitude,
252 longitude,
253 static_cast<vgl_height_reference>(reference),
254 &sample.height));
255 if (failed(sample.status))
256 {
257 return unexpected(sample.status);
258 }
259 return sample;
260 }
261
262private:
263 vgl_context* handle_ = nullptr;
264};
265
266// Owns a vgl_pack. Destroy packs before their context.
267class pack
268{
269public:
272 pack(const pack&) = delete;
273 pack& operator=(const pack&) = delete;
274 pack(pack&& other) noexcept : handle_(other.release()) {}
276 {
277 if (this != &other)
278 {
279 reset(other.release());
280 }
281 return *this;
282 }
283 ~pack() { reset(); }
284
285 // Adds a pack to a context and starts loading it.
287 const vgl_pack_create_info& info) noexcept
288 {
289 vgl_pack* handle = nullptr;
290 const auto r = static_cast<result>(vgl_pack_create(owner.get(), &info, &handle));
291 if (failed(r))
292 {
293 return unexpected(r);
294 }
295 return pack(handle);
296 }
297
298 // Adds the pack at uri with a priority.
299 [[nodiscard]] static expected<pack>
300 create(context& owner, const char* uri, std::int32_t priority = 0) noexcept
301 {
303 info.uri = uri;
304 info.priority = priority;
305 return create(owner, info);
306 }
307
308 [[nodiscard]] vgl_pack* get() const noexcept { return handle_; }
309 [[nodiscard]] explicit operator bool() const noexcept { return handle_ != nullptr; }
310
311 [[nodiscard]] vgl_pack* release() noexcept { return std::exchange(handle_, nullptr); }
312
313 void reset(vgl_pack* handle = nullptr) noexcept
314 {
315 static_cast<void>(vgl_pack_destroy(std::exchange(handle_, handle)));
316 }
317
318 // Changes the priority; see vgl_pack_priority_set.
319 [[nodiscard]] result priority_set(std::int32_t priority) noexcept
320 {
321 return static_cast<result>(vgl_pack_priority_set(handle_, priority));
322 }
323
325 {
326 std::int32_t priority = 0;
327 const auto r = static_cast<result>(vgl_pack_priority_get(handle_, &priority));
328 if (failed(r))
329 {
330 return unexpected(r);
331 }
332 return priority;
333 }
334
335 // Sets the opacity of an imagery layer, from 0 (hidden) to 1; see vgl_pack_opacity_set.
336 [[nodiscard]] result opacity_set(float opacity) noexcept
337 {
338 return static_cast<result>(vgl_pack_opacity_set(handle_, opacity));
339 }
340
342 {
343 float opacity = 0.0f;
344 const auto r = static_cast<result>(vgl_pack_opacity_get(handle_, &opacity));
345 if (failed(r))
346 {
347 return unexpected(r);
348 }
349 return opacity;
350 }
351
353 {
354 vgl_pack_state state{};
356 const auto r = static_cast<result>(vgl_pack_state_get(handle_, &state, &failure));
357 if (failed(r))
358 {
359 return unexpected(r);
360 }
361 return pack_status{static_cast<pack_state>(state), static_cast<result>(failure)};
362 }
363
364private:
365 vgl_pack* handle_ = nullptr;
366};
367
368} // namespace vgl
369
370#endif // VGL_CONTEXT_HPP
vgl_result
Definition base.h:98
@ VGL_RESULT_SUCCESS
Definition base.h:100
Definition context.hpp:106
expected< ray_query_result > ray_query(const vgl_dvec3 &origin, const vgl_dvec3 &direction) noexcept
Definition context.hpp:230
context & operator=(context &&other) noexcept
Definition context.hpp:113
~context()
Definition context.hpp:121
result view_set(const vgl_view *view) noexcept
Definition context.hpp:147
context(const context &)=delete
result update(const vgl_update_info *info=nullptr) noexcept
Definition context.hpp:224
vgl_context * get() const noexcept
Definition context.hpp:136
vgl_allocator_interface allocator_get() const noexcept
Definition context.hpp:165
context() noexcept=default
std::vector< const char * > attribution_get() const
Definition context.hpp:193
vgl_context_statistics statistics_get() const noexcept
Definition context.hpp:208
result log(log_level level, const char *message) const noexcept
Definition context.hpp:174
void reset(vgl_context *handle=nullptr) noexcept
Definition context.hpp:141
expected< vgl_view > view_get() const noexcept
Definition context.hpp:153
result texture_formats_set(std::span< const texture_format > formats) noexcept
Definition context.hpp:181
context(context &&other) noexcept
Definition context.hpp:112
result resources_reset() noexcept
Definition context.hpp:217
expected< height_sample > height_query(double latitude, double longitude, height_reference reference=height_reference::ellipsoid) noexcept
Definition context.hpp:244
vgl_context * release() noexcept
Definition context.hpp:139
static expected< context > create(const vgl_context_create_info *info=nullptr) noexcept
Definition context.hpp:125
context & operator=(const context &)=delete
Definition base.hpp:87
Definition context.hpp:268
expected< pack_status > state_get() const noexcept
Definition context.hpp:352
vgl_pack * get() const noexcept
Definition context.hpp:308
vgl_pack * release() noexcept
Definition context.hpp:311
pack(pack &&other) noexcept
Definition context.hpp:274
pack & operator=(pack &&other) noexcept
Definition context.hpp:275
result priority_set(std::int32_t priority) noexcept
Definition context.hpp:319
pack(const pack &)=delete
pack() noexcept=default
expected< std::int32_t > priority_get() const noexcept
Definition context.hpp:324
~pack()
Definition context.hpp:283
static expected< pack > create(context &owner, const char *uri, std::int32_t priority=0) noexcept
Definition context.hpp:300
static expected< pack > create(context &owner, const vgl_pack_create_info &info) noexcept
Definition context.hpp:286
pack & operator=(const pack &)=delete
void reset(vgl_pack *handle=nullptr) noexcept
Definition context.hpp:313
expected< float > opacity_get() const noexcept
Definition context.hpp:341
result opacity_set(float opacity) noexcept
Definition context.hpp:336
Definition base.hpp:72
struct vgl_pack vgl_pack
Definition context.h:283
vgl_result vgl_pack_opacity_set(vgl_pack *pack, float opacity)
vgl_result vgl_pack_priority_get(const vgl_pack *pack, int32_t *out_priority)
vgl_result vgl_context_log(const vgl_context *context, vgl_log_level level, const char *message)
vgl_result vgl_view_geodetic_project(const vgl_view *view, const vgl_ellipsoid *ellipsoid, const vgl_geodetic *geodetic, vgl_dvec3 *out_window)
vgl_result vgl_context_allocator_get(const vgl_context *context, vgl_allocator_interface *out_allocator)
vgl_height_reference
Definition context.h:429
@ VGL_HEIGHT_REFERENCE_ELLIPSOID
Definition context.h:430
@ VGL_HEIGHT_REFERENCE_MEAN_SEA_LEVEL
Definition context.h:431
struct vgl_context vgl_context
Definition context.h:23
vgl_result vgl_context_attribution_get(const vgl_context *context, uint32_t *inout_count, const char **out_strings)
vgl_result vgl_pack_create(vgl_context *context, const vgl_pack_create_info *info, vgl_pack **out_pack)
vgl_result vgl_context_create(const vgl_context_create_info *info, vgl_context **out_context)
vgl_result vgl_context_view_get(const vgl_context *context, vgl_view *out_view)
vgl_result vgl_view_project(const vgl_view *view, const vgl_dvec3 *position, vgl_dvec3 *out_window)
vgl_result vgl_context_destroy(vgl_context *context)
vgl_result vgl_context_height_query(vgl_context *context, double latitude, double longitude, vgl_height_reference reference, double *out_height)
vgl_result vgl_context_statistics_get(const vgl_context *context, vgl_context_statistics *out_statistics)
vgl_result vgl_context_update(vgl_context *context, const vgl_update_info *info)
vgl_result vgl_pack_destroy(vgl_pack *pack)
vgl_result vgl_context_view_set(vgl_context *context, const vgl_view *view)
vgl_result vgl_pack_state_get(const vgl_pack *pack, vgl_pack_state *out_state, vgl_result *out_failure)
vgl_result vgl_pack_priority_set(vgl_pack *pack, int32_t priority)
vgl_result vgl_pack_opacity_get(const vgl_pack *pack, float *out_opacity)
vgl_pack_state
Definition context.h:300
@ VGL_PACK_STATE_LOADING
Definition context.h:301
@ VGL_PACK_STATE_READY
Definition context.h:302
@ VGL_PACK_STATE_FAILED
Definition context.h:303
vgl_result vgl_context_ray_query(vgl_context *context, const vgl_dvec3 *origin, const vgl_dvec3 *direction, vgl_ray_hit *out_hit)
vgl_log_level
Definition foundation.h:19
Definition base.hpp:22
texture_format
Definition context.hpp:55
log_level
Definition foundation.hpp:21
result
Definition base.hpp:25
height_reference
Definition context.hpp:32
expected< vgl_dvec3 > view_geodetic_project(const vgl_view &view, const vgl_geodetic &geodetic, const vgl_ellipsoid *ellipsoid=nullptr) noexcept
Definition context.hpp:90
pack_state
Definition context.hpp:25
expected< vgl_dvec3 > view_project(const vgl_view &view, const vgl_dvec3 &position) noexcept
Definition context.hpp:75
vgl_result vgl_context_resources_reset(vgl_context *context)
vgl_texture_format
Definition render.h:65
@ VGL_TEXTURE_FORMAT_RGBA8_SRGB
Definition render.h:67
@ VGL_TEXTURE_FORMAT_ETC2_RGB8
Definition render.h:70
@ VGL_TEXTURE_FORMAT_BC7
Definition render.h:69
@ VGL_TEXTURE_FORMAT_RGBA8
Definition render.h:66
@ VGL_TEXTURE_FORMAT_ASTC_4X4
Definition render.h:71
@ VGL_TEXTURE_FORMAT_BC1
Definition render.h:68
vgl_result vgl_context_texture_formats_set(vgl_context *context, const vgl_texture_format *formats, uint32_t format_count)
Definition context.hpp:40
result status
Definition context.hpp:42
double height
Definition context.hpp:41
Definition context.hpp:67
result failure
Definition context.hpp:69
pack_state state
Definition context.hpp:68
Definition context.hpp:48
vgl_ray_hit hit
Definition context.hpp:49
result status
Definition context.hpp:50
Definition foundation.h:49
Definition context.h:27
Definition context.h:552
Definition base.h:69
Definition geodesy.h:36
Definition geodesy.h:43
Definition context.h:287
Definition context.h:473
Definition context.h:132
Definition context.h:152