BRL-CAD
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/* B V I E W _ U T I L . H
2 * BRL-CAD
3 *
4 * Copyright (c) 1993-2026 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/** @addtogroup bv_util
21 *
22 */
23/** @{ */
24/** @file bv/util.h */
25
26#ifndef BV_UTIL_H
27#define BV_UTIL_H
28
29#include "common.h"
30#include "bu/hash.h"
31#include "bn/tol.h"
32#include "dm/defines.h"
33#include "bv/defines.h"
34
36
37/* Set default values for a bv. */
38BV_EXPORT extern void bv_init(struct bview *v, struct bview_set *s);
39BV_EXPORT extern void bv_free(struct bview *v);
40
41/**
42 * FIXME: this routine is suspect and needs investigating. if run
43 * during view initialization, the shaders regression test fails.
44 */
45BV_EXPORT void bv_mat_aet(struct bview *v);
47BV_EXPORT extern void bv_settings_init(struct bview_settings *s);
49/* To use default scaling (0.5 model scale == 2.0 view factor) use
50 * this as an argument to bv_autoview's scale parameter */
51#define BV_AUTOVIEW_SCALE_DEFAULT -1
52/**
53 * Automatically set up the view to make the scene objects visible
54 */
55BV_EXPORT extern void bv_autoview(struct bview *v, fastf_t scale, int all_view_objs);
57/**
58 * Set up the view (center and size) to frame the supplied bounding box.
59 * Pass BV_AUTOVIEW_SCALE_DEFAULT as the scale argument to use the
60 * default scaling factor. This is the back end bv_autoview() uses once
61 * a bounding box has been determined.
62 */
63BV_EXPORT extern void bv_autoview_bounds(struct bview *v, fastf_t scale, const point_t min, const point_t max);
65/* Copy the size and camera info (deliberately not a full copy of all view state) */
66BV_EXPORT extern void bv_sync(struct bview *dest, struct bview *src);
68
69
70/* Camera accessor functions
71 *
72 * These replace direct writes to gv_scale / gv_size / gv_isize / gv_perspective /
73 * gv_aet / gv_rotation / gv_center, ensuring derived fields are kept consistent
74 * and view-policy decisions stay above the scene-data layer.
75 *
76 * Size and scale are linked: size == 2 * scale; isize == 1 / size.
77 * Setters maintain all three automatically. */
78BV_EXPORT extern fastf_t bv_view_get_scale(const struct bview *v);
79BV_EXPORT extern void bv_view_set_scale(struct bview *v, fastf_t scale);
80BV_EXPORT extern fastf_t bv_view_get_size(const struct bview *v);
81BV_EXPORT extern void bv_view_set_size(struct bview *v, fastf_t size);
83/* Perspective angle (degrees; 0 means orthographic). */
84BV_EXPORT extern fastf_t bv_view_get_perspective(const struct bview *v);
85BV_EXPORT extern void bv_view_set_perspective(struct bview *v, fastf_t perspective);
87/* Azimuth / Elevation / Twist. bv_view_set_aet recomputes gv_rotation. */
88BV_EXPORT extern void bv_view_get_aet(const struct bview *v, vect_t aet);
89BV_EXPORT extern void bv_view_set_aet(struct bview *v, const vect_t aet);
91/* Raw rotation matrix. Prefer bv_view_set_aet when the AET representation is
92 * available; use this accessor only when restoring saved matrix state (e.g.
93 * loadview). */
94BV_EXPORT extern void bv_view_get_rotation(const struct bview *v, mat_t rot);
95BV_EXPORT extern void bv_view_set_rotation(struct bview *v, const mat_t rot);
97/* View center expressed as a point (positive = model origin offset).
98 * get extracts the translation from gv_center; set stores it via
99 * MAT_DELTAS_VEC_NEG so gv_center holds the negated translation. */
100BV_EXPORT extern void bv_view_get_center_vec(const struct bview *v, point_t center);
101BV_EXPORT extern void bv_view_set_center_vec(struct bview *v, const point_t center);
103
104
105/* Copy settings (potentially) common to the view and scene objects.
106 * Return 0 if no changes were made to dest. If dest did have one
107 * or more settings updated from src, return 1. */
110/* Sync values within the bv, perform callbacks if any are defined */
111BV_EXPORT extern void bv_update(struct bview *gvp);
113/* Update objects in the selection set (if any) and their children */
114BV_EXPORT extern int bv_update_selected(struct bview *gvp);
116/* Clear or reset the knob states. Specify a category to indicate which
117 * variables should be reset:
118 *
119 * BV_KNOBS_ALL resets both rate and absolute values
120 * BV_KNOBS_RATE resets rate only
121 * BV_KNOBS_ABS resets absolute only
122 */
123#define BV_KNOBS_ALL 0
124#define BV_KNOBS_RATE 1
125#define BV_KNOBS_ABS 2
126BV_EXPORT extern void bv_knobs_reset(struct bview_knobs *k, int category);
128/* Hash the semantic (non-pointer) contents of a bview_knobs struct. This
129 * intentionally excludes any *_udata pointers to avoid pointer address noise
130 * and skips any padding that would be present if hashing the raw struct.
131 *
132 * If 'state' is non-NULL the supplied hash state is updated in-place and the
133 * function returns 0 (the caller is expected to finalize the hash later.)
134 * If 'state' is NULL an internal hash state is created, populated and
135 * finalized and the resulting hash value is returned.
136 *
137 * Returns:
138 * 0 if k is NULL or state supplied (non-owning mode)
139 * hash if state is NULL (owning mode)
140 */
141BV_EXPORT extern unsigned long long
142bv_knobs_hash(struct bview_knobs *k, struct bu_data_hash_state *state);
144/**
145 * @brief
146 * Process an individual libbv knob command.
147 *
148 * Note that the reason rvec, do_rot, tvec and do_tran are set, rather than an
149 * immediate view update being performed, is to allow parent applications to
150 * process multiple commands before finally triggering the bv_knobs_rot or
151 * bv_knobs_tran functions to implement the accumulated instructions.
152 *
153 * @param[out] rvec Pointer to rotation vector
154 * @param[out] do_rot Pointer to flag indicating whether the command implies a rotation op is needed
155 * @param[out] tvec Pointer to translation vector
156 * @param[out] do_tran Pointer to flag indicating whether the command implies a translation op is needed
157 *
158 * @param[in] v bview structure
159 * @param[in] cmd command string - valid entries are x, y, z, X, Y Z, ax, ay, az, aX, aY, aZ, S, aS
160 * @param[in] f numerical parameter to cmd (i.e. aX 0.1 - required for all commands)
161 * @param[in] origin char indicating origin - may be 'e' (eye_pt), 'm' (model origin) or 'v' (view origin - default)
162 * @param[in] model_flag Manipulate view using model coordinates rather than view coordinates
163 * @param[in] incr_flag Treat f parameter as an incremental change rather than an absolute setting
164 *
165 * @return
166 * Returns BRLCAD_OK if command was successfully processed, BRLCAD_ERROR otherwise.
167 * */
170 struct bview *v, const char *cmd, fastf_t f,
171 char origin, int model_flag, int incr_flag
172 );
173
174/**
175 * @brief
176 * Rotate the view based on an Euler angle triplet (degrees) specified
177 * in one of several coordinate frames, about one of several origins.
178 *
179 * coords: Rotation input frame
180 * 'v' - rvec in view coordinates
181 * 'm' - rvec in model coordinates (converted via Rv * Rm * Rv^{-1})
182 * 'o' - rvec in object coordinates (use obj_rot to map object->model->view,
183 * fallback to 'v' semantics if obj_rot is NULL)
184 *
185 * origin: Rotation pivot specifier
186 * 'v' : view center (0,0,0 in view space)
187 * 'm' : model origin
188 * 'e' : eye point (0,0,1 in view space)
189 * 'k' : model-space custom pivot supplied via pvt_pt
190 * Any unrecognized value falls back to 'v'.
191 *
192 * obj_rot:
193 * Accumulated object->model rotation matrix when coords=='o'. NULL otherwise.
194 *
195 * pvt_pt:
196 * Model-space pivot point when origin=='k'. Ignored otherwise.
197 * NULL == model origin.
198 *
199 * BEHAVIOR
200 * 1. rvec is converted into a pure view-space rotation matrix according
201 * to 'coords' (and obj_rot for 'o').
202 * 2. If origin != 'v', the view center (gv_center) is relocated so the
203 * specified pivot is invariant under the applied rotation.
204 * 3. gv_rotation is post-multiplied by the view rotation matrix.
205 * 4. bv_update(v) refreshes derived matrices. Absolute translation bookkeeping
206 * (tra_v_abs / tra_m_abs) is always recomputed
207 *
208 * @param[in,out] v target bview structure
209 * @param[in] rvec rotation vector (Euler angles, degrees) expressed in the coordinate frame indicated by coords.
210 * @param[in] origin char indicating origin - may be 'e' (eye_pt), 'm' (model origin), 'v' (view origin - default) or 'k' (keypoint)
211 * @param[in] coords coordinate frame - may be 'm' (model), 'o' (obj coords via obj_rot), or 'v' (view)
212 * @param[in] obj_rot pointer to accumulated object rotation matrix (may be NULL)
213 * @param[in] pvt_pt model space pivot point
214 */
215BV_EXPORT extern void
216bv_knobs_rot(struct bview *v,
218 char origin,
219 char coords,
220 const matp_t obj_rot,
221 const pointp_t pvt_pt);
222
223/* @brief
224 * Process a knob translation vector.
225 *
226 * @param[in] v bview structure
227 * @param[in] tvec Pointer to translation vector
228 * @param[in] model_flag Manipulate view using model coordinates rather than view coordinates
229 */
230BV_EXPORT extern void
231bv_knobs_tran(struct bview *v,
233 int model_flag);
234
235
236/* Update the bview struct's knob rate flags based on the vector values. */
237BV_EXPORT extern void
238bv_update_rate_flags(struct bview *v);
240
241/* Return 1 if the visible contents differ
242 * Return 2 if visible content is the same but settings differ
243 * Return 3 if content is the same but user data, dmp or callbacks differ
244 * Return -1 if one or more of the views is NULL
245 * Else return 0 */
246BV_EXPORT extern int bv_differ(struct bview *v1, struct bview *v2);
248/* Return a hash of the contents of the bv container. Returns 0 on failure. */
249BV_EXPORT extern unsigned long long bv_hash(struct bview *v);
251/* Return a hash of the contents of a display list. Returns 0 on failure. */
252BV_EXPORT extern unsigned long long bv_dl_hash(struct display_list *dl);
254/* Returns number of objects defined in any object container
255 * known to this view (0 if completely cleared). */
256BV_EXPORT extern size_t bv_clear(struct bview *v, int flags);
258/* Note that some of these are mutually exclusive as far as producing any
259 * changes - a simultaneous constraint in X and Y, for example, results in a
260 * no-op. */
261#define BV_IDLE 0x000
262#define BV_ROT 0x001
263#define BV_TRANS 0x002
264#define BV_SCALE 0x004
265#define BV_CENTER 0x008
266#define BV_CON_X 0x010
267#define BV_CON_Y 0x020
268#define BV_CON_Z 0x040
269#define BV_CON_GRID 0x080
270#define BV_CON_LINES 0x100
272/* Update a view in response to X,Y coordinate changes as generated
273 * by a graphical interface's mouse motion. */
274BV_EXPORT extern int bv_adjust(struct bview *v, int dx, int dy, point_t keypoint, int mode, unsigned long long flags);
276/* Beginning extraction of the core of libtclcad view object manipulation
277 * logic. The following functions will initially be pretty straightforward
278 * mappings from libtclcad, and will likely evolve over time.
279 */
280
281/* Return -1 if width and/or height are unset (and hence a meaningful
282 * calculation is impossible), else 0. */
283BV_EXPORT extern int bv_screen_to_view(struct bview *v, fastf_t *fx, fastf_t *fy, fastf_t x, fastf_t y);
285/* Return -1 if width and/or height are unset (and hence a meaningful
286 * calculation is impossible), else 0.
287 *
288 * x and y will normally be integers, but the types are float to allow for
289 * the possibility of sub-pixel coordinate specifications.
290 */
291BV_EXPORT extern int bv_screen_pt(point_t *p, fastf_t x, fastf_t y, struct bview *v);
293
294
295/* Compute the min, max, and center points of the scene object.
296 * Return 1 if a bound was computed, else 0 */
297BV_EXPORT extern int bv_scene_obj_bound(struct bv_scene_obj *s, struct bview *v);
299/* Find the nearest (mode == 0) or farthest (mode == 1) data_vZ value from
300 * the vlist points in s in the context of view v */
301BV_EXPORT extern fastf_t bv_vZ_calc(struct bv_scene_obj *s, struct bview *v, int mode);
303/* Copy object attributes (but not geometry) from src to dest */
304BV_EXPORT extern void bv_obj_sync(struct bv_scene_obj *dest, struct bv_scene_obj *src);
306/* Mark object and any child objects as stale for the drawing routines */
307/* There are a few options for this situation - this one, which requires the client code
308 * to explicitly notify the drawing routines they need to do work, an internal options
309 * hash stored in the bv_scene_obj itself which is checked at render time, and setter
310 * wrapper functions that do the bookkeeping for the caller (in lieu of directly setting
311 * values in the bv_scene_obj struct.) The first one isn't ideal because the visual will
312 * be wrong if the caller doesn't supply the notification, the second has unknown
313 * performance implications, and the third would be a major rework of how the bv_scene_obj
314 * data is accessed (effectively, making the internal storage of bv_scene_obj fully hidden
315 * a.l.a the libdm rework.) Not sure what the best option is yet... leaning towards #2
316 * if it is "fast enough"... */
317BV_EXPORT void bv_obj_stale(struct bv_scene_obj *s);
319/* Given a view, create an object of the specified type. Like bv_obj_get, except it
320 * leaves the addition of objects to the client. Lower level. */
321BV_EXPORT struct bv_scene_obj *
322bv_obj_create(struct bview *v, int type);
324/* Given a view, create an object of the specified type and add it to the
325 * appropriate container. Issues such as memory management as a function of
326 * view settings are handled internally, so client codes don't need to manage
327 * it. */
328BV_EXPORT struct bv_scene_obj *
329bv_obj_get(struct bview *v, int type);
331/* Given an object, create an object that is a child of that object. Issues
332 * such as memory management as a function of view settings are handled
333 * internally, so client codes don't need to manage it. */
334BV_EXPORT struct bv_scene_obj *
337/* Clear the contents of an object (including releasing its children), but keep
338 * it active in the view. Generally used when redrawing an object */
339BV_EXPORT void
340bv_obj_reset(struct bv_scene_obj *s);
342/* Release an object to the internal pools. */
343BV_EXPORT void
344bv_obj_put(struct bv_scene_obj *o);
346/* Given a scene object and a name vname, glob match child names and uuids to
347 * attempt to locate a child of s that matches vname */
348BV_EXPORT struct bv_scene_obj *
349bv_find_child(struct bv_scene_obj *s, const char *vname);
351/* Given a view and a name vname, glob match names and uuids to attempt to
352 * locate a scene object in v that matches vname.
353 *
354 * NOTE - currently this is searching the top level objects, but does not walk
355 * down into their children. May want to support that in the future... */
356BV_EXPORT struct bv_scene_obj *
357bv_find_obj(struct bview *v, const char *vname);
359/* Given a seed name, generate a name that does not collide with any existing
360 * object names in the top level. If the seed name does not collide, it is
361 * returned as the result - otherwise, a name based on the seed name will be
362 * generated.
363 */
364BV_EXPORT void
365bv_uniq_obj_name(struct bu_vls *oname, const char *seed, struct bview *v);
367/* For the specified object/view pairing, return the appropriate scene object
368 * to use with that view. Usually this will return s, but if a Level of Detail
369 * scheme or some other view-aware rendering of the object is active, that object
370 * will be returned instead. */
371BV_EXPORT struct bv_scene_obj *
372bv_obj_for_view(struct bv_scene_obj *s, struct bview *v);
374/* Get a view-specific object vobj for view v on object s. */
375BV_EXPORT struct bv_scene_obj *
376bv_obj_get_vo(struct bv_scene_obj *s, struct bview *v);
378/* Check for the presence of view-specific objects */
379BV_EXPORT int
380bv_obj_have_vo(struct bv_scene_obj *s, struct bview *v);
382/* Clear view-specific objects */
383BV_EXPORT int
384bv_clear_view_obj(struct bv_scene_obj *s, struct bview *v);
386/* Set the illumination state on the object and its children to ill_state.
387 * Returns 0 if no states were changed, and 1 if one or more states were
388 * updated. */
389BV_EXPORT int
390bv_illum_obj(struct bv_scene_obj *s, char ill_state);
392/* For the given view, return a pointer to the bu_ptbl holding active scene
393 * objects with the specified type. Note that view-specific db objects are not
394 * part of these sets - they should be retrieved from the scene objects in this
395 * set with bv_obj_for_view. */
396BV_EXPORT struct bu_ptbl *
397bv_view_objs(struct bview *v, int type);
399/* Given a view, construct the view plane */
400BV_EXPORT int
401bv_view_plane(plane_t *p, struct bview *v);
403
404/* Environment variable controlled logging.
405 *
406 * Set BV_LOG to numerical levels to get increasingly
407 * verbose reporting of drawing info */
408#define BV_ENABLE_ENV_LOGGING 1
410bv_log(int level, const char *fmt, ...) _BU_ATTR_PRINTF23;
412
413/* Debugging function for printing contents of views */
414BV_EXPORT void
415bv_view_print(const char *title, struct bview *v, int verbosity);
418
419/** @} */
420
421#endif /* BV_UTIL_H */
422
423/*
424 * Local Variables:
425 * mode: C
426 * tab-width: 8
427 * indent-tabs-mode: t
428 * c-file-style: "stroustrup"
429 * End:
430 * ex: shiftwidth=4 tabstop=8
431 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define _BU_ATTR_PRINTF23
Definition defines.h:95
void bv_free(struct bview *v)
unsigned long long bv_hash(struct bview *v)
int bv_update_selected(struct bview *gvp)
fastf_t bv_view_get_scale(const struct bview *v)
struct bu_ptbl * bv_view_objs(struct bview *v, int type)
void bv_update_rate_flags(struct bview *v)
void bv_autoview(struct bview *v, fastf_t scale, int all_view_objs)
fastf_t bv_vZ_calc(struct bv_scene_obj *s, struct bview *v, int mode)
int bv_knobs_cmd_process(vect_t *rvec, int *do_rot, vect_t *tvec, int *do_tran, struct bview *v, const char *cmd, fastf_t f, char origin, int model_flag, int incr_flag)
Process an individual libbv knob command.
void bv_obj_sync(struct bv_scene_obj *dest, struct bv_scene_obj *src)
struct bv_scene_obj * bv_obj_create(struct bview *v, int type)
void bv_view_set_perspective(struct bview *v, fastf_t perspective)
void bv_init(struct bview *v, struct bview_set *s)
void bv_view_get_rotation(const struct bview *v, mat_t rot)
int bv_adjust(struct bview *v, int dx, int dy, point_t keypoint, int mode, unsigned long long flags)
void bv_autoview_bounds(struct bview *v, fastf_t scale, const point_t min, const point_t max)
void bv_uniq_obj_name(struct bu_vls *oname, const char *seed, struct bview *v)
struct bv_scene_obj * bv_find_child(struct bv_scene_obj *s, const char *vname)
int bv_illum_obj(struct bv_scene_obj *s, char ill_state)
void bv_view_get_aet(const struct bview *v, vect_t aet)
int bv_scene_obj_bound(struct bv_scene_obj *s, struct bview *v)
size_t bv_clear(struct bview *v, int flags)
void bv_knobs_tran(struct bview *v, const vect_t tvec, int model_flag)
struct bv_scene_obj * bv_obj_get(struct bview *v, int type)
void bv_obj_put(struct bv_scene_obj *o)
int bv_clear_view_obj(struct bv_scene_obj *s, struct bview *v)
int bv_differ(struct bview *v1, struct bview *v2)
fastf_t bv_view_get_size(const struct bview *v)
void bv_settings_init(struct bview_settings *s)
void bv_log(int level, const char *fmt,...) _BU_ATTR_PRINTF23
void bv_view_set_rotation(struct bview *v, const mat_t rot)
int bv_obj_settings_sync(struct bv_obj_settings *dest, struct bv_obj_settings *src)
void bv_view_set_aet(struct bview *v, const vect_t aet)
struct bv_scene_obj * bv_obj_get_child(struct bv_scene_obj *s)
fastf_t bv_view_get_perspective(const struct bview *v)
void bv_obj_reset(struct bv_scene_obj *s)
struct bv_scene_obj * bv_obj_get_vo(struct bv_scene_obj *s, struct bview *v)
int bv_screen_to_view(struct bview *v, fastf_t *fx, fastf_t *fy, fastf_t x, fastf_t y)
void bv_sync(struct bview *dest, struct bview *src)
void bv_view_set_size(struct bview *v, fastf_t size)
int bv_screen_pt(point_t *p, fastf_t x, fastf_t y, struct bview *v)
void bv_view_get_center_vec(const struct bview *v, point_t center)
void bv_view_set_center_vec(struct bview *v, const point_t center)
unsigned long long bv_knobs_hash(struct bview_knobs *k, struct bu_data_hash_state *state)
void bv_knobs_reset(struct bview_knobs *k, int category)
int bv_view_plane(plane_t *p, struct bview *v)
int bv_obj_have_vo(struct bv_scene_obj *s, struct bview *v)
void bv_obj_stale(struct bv_scene_obj *s)
void bv_mat_aet(struct bview *v)
struct bv_scene_obj * bv_obj_for_view(struct bv_scene_obj *s, struct bview *v)
unsigned long long bv_dl_hash(struct display_list *dl)
struct bv_scene_obj * bv_find_obj(struct bview *v, const char *vname)
void bv_knobs_rot(struct bview *v, const vect_t rvec, char origin, char coords, const matp_t obj_rot, const pointp_t pvt_pt)
Rotate the view based on an Euler angle triplet (degrees) specified in one of several coordinate fram...
void bv_view_set_scale(struct bview *v, fastf_t scale)
void bv_update(struct bview *gvp)
void bv_view_print(const char *title, struct bview *v, int verbosity)
fastf_t vect_t[ELEMENTS_PER_VECT]
3-tuple vector
Definition vmath.h:348
double fastf_t
fastest 64-bit (or larger) floating point type
Definition vmath.h:333
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition vmath.h:369
fastf_t plane_t[ELEMENTS_PER_PLANE]
Definition of a plane equation.
Definition vmath.h:396
fastf_t * matp_t
pointer to a 4x4 matrix
Definition vmath.h:372
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:354
Definition ptbl.h:53
Definition vls.h:53