BRL-CAD
lod.h
Go to the documentation of this file.
1/* L O D . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21/*----------------------------------------------------------------------*/
22/* @file lod.h */
23/** @addtogroup bg_lod */
24/** @{ */
25
26/**
27 * @brief Functions for generating view dependent level-of-detail data,
28 * particularly for meshes.
29 */
30
31#ifndef BG_LOD_H
32#define BG_LOD_H
33
34#include "common.h"
35#include "vmath.h"
36#include "bv.h"
37#include "bg/defines.h"
38
39__BEGIN_DECLS
40
41/* Given a view, construct either an oriented bounding box or a view frustum
42 * extruded to contain scene objects visible in the view. Conceptually, think
43 * of it as a framebuffer pane pushed through the scene in the direction the
44 * camera is looking. If the view width and height are not set or there is
45 * some other problem, no volume is computed. This function is intended
46 * primarily to be set as an updating callback for the bview structure. */
47BG_EXPORT extern void
49
50/* Given a screen x,y coordinate, construct and return the set of all scene
51 * objects whose AABB intersect with the OBB created by the projection of that
52 * pixel through the scene. */
53BG_EXPORT int
54bg_view_objs_select(struct bv_scene_obj ***set, struct bview *v, int x, int y);
55
56/* Given a screen x1,y1,x2,y2 rectangle, construct and return the set of all scene
57 * objects whose AABB intersect with the OBB created by the projection of that
58 * rectangle through the scene. */
59BG_EXPORT int
60bg_view_objs_rect_select(struct bv_scene_obj ***set, struct bview *v, int x1, int y1, int x2, int y2);
61
62/* Storing and reading from a lot of small, individual files doesn't work very
63 * well on some platforms. We provide a "context" to manage bookkeeping of data
64 * across objects. The details are implementation internal - the application
65 * will simply create a context with a file name and provide it to the various
66 * LoD calls. */
67struct bg_mesh_lod_context_internal;
69 struct bg_mesh_lod_context_internal *i;
70};
71
72/* Create an LoD context using "name". If data is already present associated
73 * with that name it will be loaded, otherwise a new storage structure will be
74 * initialized. If creation or loading fails for any reason the return value
75 * NULL.
76 *
77 * Note that "name" should be a full, unique path associated with the source
78 * database. libbu will manage where the context data is cached.
79*/
80BG_EXPORT struct bg_mesh_lod_context *
82
83/* Free all memory associated with context c. Note that this call does NOT
84 * remove the on-disk data. */
85BG_EXPORT void
87
88/* Remove cache data associated with key. If key == 0, remove ALL cache data
89 * associated with all LoD objects in c. (i.e. a full LoD cache reset for that
90 * .g database). If key == 0 AND c == NULL, clear all LoD cache data for all
91 * .g databases associated with the current user's cache.
92 */
93BG_EXPORT void
94bg_mesh_lod_clear_cache(struct bg_mesh_lod_context *c, unsigned long long key);
95
96/**
97 * Given a set of points and faces, calculate a lookup key and determine if the
98 * cache has the LoD data for this particular mesh. If it does not, do the
99 * initial calculations to generate the cached LoD data needed for subsequent
100 * lookups, otherwise just return the calculated key.
101 *
102 * This is potentially an expensive operation, particularly if the LoD data set
103 * must be generated for the mesh.
104 *
105 * If user_key != 0, it will be used instead of the mesh data hash as the db
106 * key to be used for subsequent lookups. Typically calculated with
107 * bg_mesh_lod_custom_key from user supplied data, if the mesh data is not
108 * the desired source of the key.
109 *
110 * Note: to clear pre-existing cached data, run bg_mesh_lod_clear_cache();
111 *
112 * @return the lookup key calculated from the data */
113BG_EXPORT unsigned long long
114bg_mesh_lod_cache(struct bg_mesh_lod_context *c, const point_t *v, size_t vcnt, const vect_t *vn, int *f, size_t fcnt, unsigned long long user_key, fastf_t fratio);
115
116
117/**
118 * Given a user specified data stream, calculate a cache key from it. This is
119 * done if the users wishes to use some data other than the mesh information
120 * on which to base a look-up key (for example, mesh data being used to represent
121 * a non-mesh object.
122 */
123BG_EXPORT unsigned long long
124bg_mesh_lod_custom_key(void *data, size_t data_size);
125
126
127
128/**
129 * Given a name, see if the context has a key associated with that name.
130 * If so return it, else return zero.
131 *
132 * Users of this feature need to be aware that it is the responsibility
133 * of the application to maintain the LoD cache data and keep it current -
134 * it is quite possible, in a data sense, for the LoD data to be out of
135 * date if something has changed the named geometry object and not updated
136 * the cache.
137 *
138 * The advantage of this feature, if the application does maintain the
139 * data correctly, is to make it possible to load a high level view of
140 * a large model without having to hash the full mesh data of the model
141 * to retrieve the data.
142 */
143BG_EXPORT unsigned long long
144bg_mesh_lod_key_get(struct bg_mesh_lod_context *c, const char *name);
145
146/**
147 * Given a name and a key, instruct the context to associate that name with
148 * the key.
149 *
150 * Returns 0 if successful, else error
151 */
152BG_EXPORT int
153bg_mesh_lod_key_put(struct bg_mesh_lod_context *c, const char *name, unsigned long long key);
154
155/**
156 * Set up the bg_mesh_lod container using cached LoD information associated
157 * with key. If no cached data has been prepared, a NULL container is
158 * returned - to prepare cached data, call bg_mesh_lod_cache with the original
159 * mesh input data. This call is intended to be usable in situations where
160 * we don't want to pull the full mesh data set into memory, so we can't assume
161 * the original data is present.
162 *
163 * Note: bg_mesh_lod assumes a non-changing mesh - if the mesh is changed after
164 * it is created, the internal container does *NOT* automatically update. In
165 * that case the old struct should be destroyed and a new one created.
166 *
167 * A bg_mesh_lod return from this function will be initialized only to the
168 * lowest level of data (i.e. the coarsest possible representation of the
169 * object.) To tailor the data, use the bg_mesh_lod_view function. For lower
170 * level control, the bg_mesh_lod_level function may be used to explicitly
171 * manipulate the loaded LoD (usually used for debugging, but also useful if an
172 * application wishes to visualize levels explicitly.)
173 */
174BG_EXPORT struct bv_mesh_lod *
175bg_mesh_lod_create(struct bg_mesh_lod_context *c, unsigned long long key);
176
177/* Clean up the lod container. */
178BG_EXPORT void
180
181/**
182 * Given a scene object with mesh LoD data stored in s->draw_data, reduce
183 * memory footprint (for use after client codes have completed use of a
184 * particular level's info, but aren't done with the object. Main use case
185 * currently is OpenGL display lists - once generated, we can clear the
186 * internally stored LoD data until the level changes. Note that there is a
187 * re-loading performance penalty as a trade-off to the memory savings. */
188BG_EXPORT void
190
191/**
192 * Given a scene object with mesh LoD data stored in s->draw_data and a bview,
193 * load the appropriate level of detail for displaying the mesh in that view.
194 * Set reset == 1 if the caller wants to undo a memshrink operation even if the
195 * level isn't changed by the current view settings.
196 *
197 * Returns the level selected. If v == NULL, return current level of l. If
198 * there is an error or l == NULL, return -1; */
199BG_EXPORT int
200bg_mesh_lod_view(struct bv_scene_obj *s, struct bview *v, int reset);
201
202/**
203 * Given a scene object with mesh LoD data stored in s->draw_data and a detail
204 * level, load the appropriate data. This is not normally used by client codes
205 * directly, but may be needed if an app needs manipulate the level of detail
206 * without a view. Set reset == 1 if the caller wants to undo a memshrink
207 * operation even if the level isn't changed by the current view settings.
208 *
209 * Returns the level selected. If level == -1, return current level of l. If
210 * there is an error, return -1; */
211BG_EXPORT int
212bg_mesh_lod_level(struct bv_scene_obj *s, int level, int reset);
213
214/* Free a scene object's LoD data. Suitable as a s_free_callback function
215 * for a bv_scene_obj. This function will also trigger any additional
216 * "free" callback functions which might be defined for the LoD container. */
217BG_EXPORT void
219
220
221
222/* In order to preserve library barriers, a number of operations needed to
223 * realize LoD drawing are defined at other library layers and made available
224 * to the core LoD management logic with callbacks */
225
226/* Set function callbacks for retrieving and freeing high levels of mesh detail */
227BG_EXPORT void
228bg_mesh_lod_detail_setup_clbk(struct bv_mesh_lod *lod, int (*clbk)(struct bv_mesh_lod *, void *), void *cb_data);
229BG_EXPORT void
230bg_mesh_lod_detail_clear_clbk(struct bv_mesh_lod *lod, int (*clbk)(struct bv_mesh_lod *, void *));
231BG_EXPORT void
232bg_mesh_lod_detail_free_clbk(struct bv_mesh_lod *lod, int (*clbk)(struct bv_mesh_lod *, void *));
233
234__END_DECLS
235
236#endif /* BG_LOD_H */
237/** @} */
238/*
239 * Local Variables:
240 * mode: C
241 * tab-width: 8
242 * indent-tabs-mode: t
243 * c-file-style: "stroustrup"
244 * End:
245 * ex: shiftwidth=4 tabstop=8
246 */
Header file for the BRL-CAD common definitions.
unsigned long long bg_mesh_lod_cache(struct bg_mesh_lod_context *c, const point_t *v, size_t vcnt, const vect_t *vn, int *f, size_t fcnt, unsigned long long user_key, fastf_t fratio)
void bg_view_bounds(struct bview *v)
Functions for generating view dependent level-of-detail data, particularly for meshes.
struct bg_mesh_lod_context * bg_mesh_lod_context_create(const char *name)
void bg_mesh_lod_context_destroy(struct bg_mesh_lod_context *c)
void bg_mesh_lod_clear_cache(struct bg_mesh_lod_context *c, unsigned long long key)
int bg_mesh_lod_key_put(struct bg_mesh_lod_context *c, const char *name, unsigned long long key)
void bg_mesh_lod_free(struct bv_scene_obj *s)
void bg_mesh_lod_destroy(struct bv_mesh_lod *l)
void bg_mesh_lod_detail_clear_clbk(struct bv_mesh_lod *lod, int(*clbk)(struct bv_mesh_lod *, void *))
unsigned long long bg_mesh_lod_key_get(struct bg_mesh_lod_context *c, const char *name)
void bg_mesh_lod_detail_setup_clbk(struct bv_mesh_lod *lod, int(*clbk)(struct bv_mesh_lod *, void *), void *cb_data)
int bg_view_objs_select(struct bv_scene_obj ***set, struct bview *v, int x, int y)
int bg_mesh_lod_level(struct bv_scene_obj *s, int level, int reset)
void bg_mesh_lod_memshrink(struct bv_scene_obj *s)
int bg_mesh_lod_view(struct bv_scene_obj *s, struct bview *v, int reset)
int bg_view_objs_rect_select(struct bv_scene_obj ***set, struct bview *v, int x1, int y1, int x2, int y2)
void bg_mesh_lod_detail_free_clbk(struct bv_mesh_lod *lod, int(*clbk)(struct bv_mesh_lod *, void *))
struct bv_mesh_lod * bg_mesh_lod_create(struct bg_mesh_lod_context *c, unsigned long long key)
unsigned long long bg_mesh_lod_custom_key(void *data, size_t data_size)
void float float * y
Definition: tig.h:73
void int * c
Definition: tig.h:139
void float * x
Definition: tig.h:72
fastf_t vect_t[ELEMENTS_PER_VECT]
3-tuple vector
Definition: vmath.h:345
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition: vmath.h:351
struct bg_mesh_lod_context_internal * i
Definition: lod.h:69
struct bv_scene_obj * s
Definition: defines.h:386
Definition: defines.h:476
fundamental vector, matrix, quaternion math macros