BRL-CAD
Loading...
Searching...
No Matches
rt_instance.h
Go to the documentation of this file.
1/* R T _ I N S T A N C E . 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 rt_instance
21 * @brief The "raytrace instance" structure contains definitions for
22 * librt which are specific to the particular model being processed.
23 */
24/** @{ */
25/** @file rt/rt_instance.h */
26
27#ifndef RT_RT_INSTANCE_H
28#define RT_RT_INSTANCE_H
29
30#include "common.h"
31#include "vmath.h"
32#include "bu/list.h"
33#include "bu/ptbl.h"
34#include "bn/tol.h"
35#include "rt/defines.h"
36#include "rt/db_instance.h"
37#include "rt/region.h"
38#include "rt/resource.h"
39#include "rt/space_partition.h" /* cutter */
40#include "rt/soltab.h"
41#include "rt/tol.h"
42
44
45// libbu's callback type isn't quite right for this case, so we might as well
46// be specific.
47typedef void(*rti_clbk_t)(struct rt_i *rtip, struct db_tree_state *tsp, struct region *r);
48
49/**
50 * Callback type for rt_iterate_regions(). Return 0 to continue
51 * iteration; return non-zero to stop early.
52 */
53typedef int (*rt_region_callback_t)(struct region *regp, void *udata);
54
55struct rt_i_internal; /* forward declaration for private state */
56
57/**
58 * Read-only statistics counters for a ray-trace instance.
59 *
60 * This sub-struct groups all scalar performance and geometry counters
61 * that are maintained by librt during prep and ray-shooting.
62 * Applications may freely read any field; all writes are performed
63 * internally by librt.
64 *
65 * Access pattern: rtip->stats.nregions, rtip->stats.rti_nrays, etc.
66 */
67struct rt_i_stats {
68 /* Geometry counts (set during rt_prep) */
69 size_t nregions; /**< @brief total # of regions participating */
70 size_t nsolids; /**< @brief total # of solids participating */
71
72 /* Ray-shooting counters (accumulated during rt_shootray / rt_shootrays) */
73 size_t rti_nrays; /**< @brief # calls to rt_shootray() */
74 size_t nmiss_model; /**< @brief rays missed model RPP */
75 size_t nshots; /**< @brief # of calls to ft_shot() */
76 size_t nmiss; /**< @brief solid ft_shot() returned a miss */
77 size_t nhits; /**< @brief solid ft_shot() returned a hit */
78 size_t nmiss_tree; /**< @brief shots missed sub-tree RPP */
79 size_t nmiss_solid; /**< @brief shots missed solid RPP */
80 size_t ndup; /**< @brief duplicate shots at a given solid */
81 size_t nempty_cells; /**< @brief number of empty spatial partition cells passed through */
82
83 /* Space-partition (cut tree) statistics (set during rt_cut_it) */
84 size_t rti_cut_maxlen; /**< @brief max len RPP list in 1 cut bin */
85 size_t rti_ncut_by_type[CUT_MAXIMUM+1]; /**< @brief number of cuts by type */
86 size_t rti_cut_totobj; /**< @brief # objs in all bins, total */
87 size_t rti_cut_maxdepth; /**< @brief max depth of cut tree */
88};
89
90/**
91 * This structure keeps track of almost everything for ray-tracing
92 * support: Regions, primitives, model bounding box, statistics.
93 *
94 * Definitions for librt which are specific to the particular model
95 * being processed, one copy for each model. Initially, a pointer to
96 * this is returned from rt_dirbuild().
97 *
98 * During gettree processing, the most time consuming step is
99 * searching the list of existing solids to see if a new solid is
100 * actually an identical instance of a previous solid. Therefore, the
101 * list has been divided into several lists. The same macros & hash
102 * value that accesses the dbi_Head[] array are used here. The hash
103 * value is computed by db_dirhash().
104 */
105struct rt_i {
106 uint32_t rti_magic; /**< @brief magic # for integrity check */
107 /* THESE ITEMS ARE AVAILABLE FOR APPLICATIONS TO READ & MODIFY */
108 int useair; /**< @brief 1="air" regions are retained while prepping */
109 int rti_save_overlaps; /**< @brief 1=fill in pt_overlap_reg, change boolweave behavior */
110 int rti_dont_instance; /**< @brief 1=Don't compress instances of solids into 1 while prepping */
111 int rti_hasty_prep; /**< @brief 1=hasty prep, slower ray-trace */
112 size_t rti_nlights; /**< @brief number of light sources */
113 int rti_prismtrace; /**< @brief add support for pixel prism trace */
114 char * rti_region_fix_file; /**< @brief rt_regionfix() file or NULL */
115 int rti_space_partition; /**< @brief space partitioning method */
116 struct bn_tol rti_tol; /**< @brief Math tolerances for this model */
117 struct bg_tess_tol rti_ttol; /**< @brief Tessellation tolerance defaults */
118 fastf_t rti_max_beam_radius; /**< @brief Max threat radius for FASTGEN cline solid */
119 rti_clbk_t rti_gettrees_clbk; /**< @brief Optional user clbk function called during rt_gettrees_and_attrs */
120 void * rti_udata; /**< @brief ptr for user data. */
121 /* THESE ITEMS ARE AVAILABLE FOR APPLICATIONS TO READ */
122 point_t mdl_min; /**< @brief min corner of model bounding RPP */
123 point_t mdl_max; /**< @brief max corner of model bounding RPP */
124 point_t rti_pmin; /**< @brief for plotting, min RPP */
125 point_t rti_pmax; /**< @brief for plotting, max RPP */
126 double rti_radius; /**< @brief radius of model bounding sphere */
127 struct db_i * rti_dbip; /**< @brief prt to Database instance struct */
128 int needprep; /**< @brief needs rt_prep */
129 struct rt_i_stats stats; /**< @brief geometry counts and ray-shooting counters */
130
131 /* THESE ITEMS SHOULD BE CONSIDERED OPAQUE, AND SUBJECT TO CHANGE */
132 struct bu_list HeadRegion; /**< @brief ptr of list of regions in model */
133 struct bu_ptbl rti_resources; /**< @brief list of 'struct resource's encountered */
134
135 /* PRIVATE librt-internal state; see src/librt/librt_private.h */
137};
138
139
140#define RTI_NULL ((struct rt_i *)0)
141
142#define RT_CHECK_RTI(_p) BU_CKMAG(_p, RTI_MAGIC, "struct rt_i")
143#define RT_CK_RTI(_p) RT_CHECK_RTI(_p)
144
145/**
146 * Macros to painlessly visit all the active solids. Serving suggestion:
147 *
148 * RT_VISIT_ALL_SOLTABS_START(stp, rtip) {
149 * rt_pr_soltab(stp);
150 * } RT_VISIT_ALL_SOLTABS_END
151 */
152#define RT_VISIT_ALL_SOLTABS_START(_s, _rti) { \
153 int _i; \
154 for (_i = 0; _i < RT_DBNHASH; _i++) { \
155 struct bu_list *_head = rt_solidhead_ptr((_rti), _i); \
156 for (BU_LIST_FOR(_s, soltab, _head)) {
157
158#define RT_VISIT_ALL_SOLTABS_END } } }
159
160/**************************/
161/* Applications interface */
162/**************************/
163
164RT_EXPORT extern struct rt_i *rt_i_create(struct db_i *dbip);
165RT_EXPORT extern void rt_i_destroy(struct rt_i *rtip);
166
167/* Use these if working with an rt_i not being managed by the
168 * rt_i_create/rt_i_destroy pairing. */
169RT_EXPORT extern void rt_i_init(struct rt_i *rtip, struct db_i *dbip);
170RT_EXPORT extern void rt_i_clear(struct rt_i *rtip);
171
172// Old names - use rt_i_create and rt_i_destroy instead.
173DEPRECATED RT_EXPORT extern struct rt_i *rt_new_rti(struct db_i *dbip);
174DEPRECATED RT_EXPORT extern void rt_free_rti(struct rt_i *rtip);
175
176
177/* Prepare for raytracing */
178RT_EXPORT extern void rt_prep(struct rt_i *rtip);
179RT_EXPORT extern void rt_prep_parallel(struct rt_i *rtip,
180 int ncpu);
181
182/**
183 * Return a pointer to the idx-th active-solid list head in rtip.
184 * Used by the RT_VISIT_ALL_SOLTABS_START macro and any code that
185 * needs to iterate over all prepared solids without accessing the
186 * private rt_i_internal struct directly.
187 */
188RT_EXPORT extern struct bu_list *rt_solidhead_ptr(struct rt_i *rtip, int idx);
189
190
191/* Get expr tree for object */
192/**
193 * User-called function to add a tree hierarchy to the displayed set.
194 *
195 * This function is not multiply re-entrant.
196 *
197 * Returns -
198 * 0 Ordinarily
199 * -1 On major error
200 *
201 * Note: -2 returns from rt_gettrees_and_attrs are filtered.
202 */
203RT_EXPORT extern int rt_gettree(struct rt_i *rtip,
204 const char *node);
205RT_EXPORT extern int rt_gettrees(struct rt_i *rtip,
206 int argc,
207 const char **argv, int ncpus);
208
209/**
210 * User-called function to add a set of tree hierarchies to the active set.
211 * Includes getting the indicated list of attributes and an optional
212 * user-supplied rti_gettrees_clbk callback function to collect additional
213 * information in rti_udata. (stashed in the rt_i structure).
214 *
215 * This function may run in parallel, but is not multiply re-entrant itself,
216 * because db_walk_tree() isn't multiply re-entrant. Note that callback
217 * implementations should protect any data writes to a shared structure with
218 * the RT_SEM_RESULTS semaphore.
219 *
220 * Semaphores used for critical sections in parallel mode:
221 * RT_SEM_TREE ====> protects rtip->i->rti_solidheads[] lists, d_uses(solids)
222 * RT_SEM_RESULTS => protects HeadRegion, mdl_min/max, d_uses(reg), nregions
223 * RT_SEM_WORKER ==> (db_walk_dispatcher, from db_walk_tree)
224 * RT_SEM_STATS ===> nsolids
225 *
226 * INPUTS:
227 *
228 * rtip - RT instance pointer
229 *
230 * attrs - attribute value set
231 *
232 * argc - number of trees to get
233 *
234 * argv - array of char pointers to the names of the tree tops
235 *
236 * ncpus - number of cpus to use
237 *
238 * Returns -
239 * 0 Ordinarily
240 * -1 On major error
241 */
242RT_EXPORT extern int rt_gettrees_and_attrs(struct rt_i *rtip,
243 const char **attrs,
244 int argc,
245 const char **argv,
246 int ncpus);
247
248/* Print the partitions */
249RT_EXPORT extern void rt_pr_partitions(const struct rt_i *rtip,
250 const struct partition *phead,
251 const char *title);
252
253/**
254 * @brief
255 * Find solid by leaf name
256 *
257 * Given the (leaf) name of a solid, find the first occurrence of it
258 * in the solid list. Used mostly to find the light source. Returns
259 * soltab pointer, or RT_SOLTAB_NULL.
260 */
261RT_EXPORT extern struct soltab *rt_find_solid(const struct rt_i *rtip,
262 const char *name);
263
264/**
265 * initialize a memory resource structure for use during ray tracing.
266 *
267 * a given resource structure is prepared for use and marked as the
268 * resource for a given thread of execution (indicated by 'cpu_num').
269 * if an 'rtip' ray tracing instance pointer is provided, the resource
270 * structure will be stored within so that it's available to threads
271 * of execution during parallel ray tracing.
272 *
273 * This routine should initialize all the same resources that
274 * rt_clean_resource() releases. It shouldn't (but currently does for
275 * ptbl) allocate any dynamic memory, just init pointers & lists.
276 */
277
278struct rt_i; /* forward declaration */
279
280RT_EXPORT extern void rt_init_resource(struct resource *resp, int cpu_num, struct rt_i *rtip);
281
282
283RT_EXPORT extern void rt_clean_resource_basic(struct rt_i *rtip,
284 struct resource *resp);
285RT_EXPORT extern void rt_clean_resource(struct rt_i *rtip,
286 struct resource *resp);
287/* Deprecated - use rt_clean_resource_basic */
289 struct resource *resp);
290
291
292/* Plot a solid */
294 FILE *fp,
295 struct rt_i *rtip,
296 const struct soltab *stp);
297
298/* Release storage assoc with rt_i */
299RT_EXPORT extern void rt_clean(struct rt_i *rtip);
300RT_EXPORT extern int rt_del_regtree(struct rt_i *rtip,
301 struct region *delregp);
302
303/**
304 * Iterate over all regions in the rt_i, calling @p callback for each one.
305 * Iteration stops early if @p callback returns non-zero.
306 * This API hides the internal bu_list representation of the region list.
307 */
308RT_EXPORT extern void rt_iterate_regions(struct rt_i *rtip,
310 void *udata);
311
312/**
313 * Mark a region for deletion after light_init() completes.
314 * This hides the internal delete_regs bu_ptbl storage.
315 */
316RT_EXPORT extern void rt_mark_region_deleted(struct rt_i *rtip,
317 struct region *regp);
318
319/**
320 * Report count of delete_regs (used by src/rt.view.c)
321 */
322RT_EXPORT extern size_t rt_deleted_regions_cnt(struct rt_i *rtip);
323
324/**
325 * Get deleted region n
326 */
327RT_EXPORT extern struct region * rt_deleted_region_get(struct rt_i *rtip, size_t n);
328
329
330/* Check in-memory data structures */
331RT_EXPORT extern void rt_ck(struct rt_i *rtip);
332
333/* Print value of tree for a partition */
334RT_EXPORT extern void rt_pr_tree_val(const union tree *tp,
335 const struct partition *partp,
336 int pr_name, int lvl);
337/* Print a partition */
338RT_EXPORT extern void rt_pr_partition(const struct rt_i *rtip,
339 const struct partition *pp);
341 const struct rt_i *rtip,
342 const struct partition *pp);
343
344
345/**
346 * Go through all the solids in the model, given the model mins and
347 * maxes, and generate a cutting tree. A strategy better than
348 * incrementally cutting each solid is to build a box node which
349 * contains everything in the model, and optimize it.
350 *
351 * This is the main entry point into space partitioning from
352 * rt_prep().
353 */
354RT_EXPORT extern void rt_cut_it(struct rt_i *rtip,
355 int ncpu);
356
357/* free a cut tree */
358/**
359 * Free a whole cut tree below the indicated node. The strategy we
360 * use here is to free everything BELOW the given node, so as not to
361 * clobber rti_CutHead !
362 */
363RT_EXPORT extern void rt_fr_cut(struct rt_i *rtip,
364 union cutter *cutp);
365
366/**
367 * Apply any deltas to reg_regionid values to allow old applications
368 * that use the reg_regionid number to distinguish between different
369 * instances of the same prototype region.
370 *
371 * Called once, from rt_prep(), before raytracing begins.
372 */
373RT_EXPORT extern void rt_regionfix(struct rt_i *rtip);
374
375
376#ifdef USE_OPENCL
377RT_EXPORT extern void clt_init(void);
378
379RT_EXPORT extern void
380clt_db_store(size_t count, struct soltab *solids[]);
381
382RT_EXPORT extern void
383clt_db_store_bvh(size_t count, struct clt_linear_bvh_node *nodes);
384
385RT_EXPORT extern void
386clt_db_store_regions(size_t sz_btree_array, struct bit_tree *btp, size_t nregions, struct cl_bool_region *regions, struct cl_region *mtls);
387
388RT_EXPORT extern void
390
391RT_EXPORT extern void clt_db_release(void);
392
393
394RT_EXPORT void clt_prep(struct rt_i *rtip);
395#endif
396
397
399
400#endif /* RT_RT_INSTANCE_H */
401/** @} */
402/*
403 * Local Variables:
404 * tab-width: 8
405 * mode: C
406 * indent-tabs-mode: t
407 * c-file-style: "stroustrup"
408 * End:
409 * ex: shiftwidth=4 tabstop=8
410 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define DEPRECATED
Definition common.h:433
void rt_pr_tree_val(const union tree *tp, const struct partition *partp, int pr_name, int lvl)
void rt_ck(struct rt_i *rtip)
DEPRECATED void rt_free_rti(struct rt_i *rtip)
int rt_gettrees_and_attrs(struct rt_i *rtip, const char **attrs, int argc, const char **argv, int ncpus)
void(* rti_clbk_t)(struct rt_i *rtip, struct db_tree_state *tsp, struct region *r)
Definition rt_instance.h:47
void rt_clean_resource(struct rt_i *rtip, struct resource *resp)
struct region * rt_deleted_region_get(struct rt_i *rtip, size_t n)
void rt_i_init(struct rt_i *rtip, struct db_i *dbip)
int(* rt_region_callback_t)(struct region *regp, void *udata)
Definition rt_instance.h:53
void rt_mark_region_deleted(struct rt_i *rtip, struct region *regp)
void rt_pr_partition_vls(struct bu_vls *v, const struct rt_i *rtip, const struct partition *pp)
void rt_iterate_regions(struct rt_i *rtip, rt_region_callback_t callback, void *udata)
void rt_clean_resource_basic(struct rt_i *rtip, struct resource *resp)
int rt_plot_solid(FILE *fp, struct rt_i *rtip, const struct soltab *stp)
size_t rt_deleted_regions_cnt(struct rt_i *rtip)
DEPRECATED struct rt_i * rt_new_rti(struct db_i *dbip)
struct soltab * rt_find_solid(const struct rt_i *rtip, const char *name)
Find solid by leaf name.
void rt_regionfix(struct rt_i *rtip)
void rt_init_resource(struct resource *resp, int cpu_num, struct rt_i *rtip)
int rt_gettree(struct rt_i *rtip, const char *node)
void rt_pr_partition(const struct rt_i *rtip, const struct partition *pp)
void rt_i_destroy(struct rt_i *rtip)
int rt_gettrees(struct rt_i *rtip, int argc, const char **argv, int ncpus)
void rt_clean(struct rt_i *rtip)
int rt_del_regtree(struct rt_i *rtip, struct region *delregp)
struct bu_list * rt_solidhead_ptr(struct rt_i *rtip, int idx)
void rt_fr_cut(struct rt_i *rtip, union cutter *cutp)
void rt_prep_parallel(struct rt_i *rtip, int ncpu)
void rt_pr_partitions(const struct rt_i *rtip, const struct partition *phead, const char *title)
void rt_cut_it(struct rt_i *rtip, int ncpu)
struct rt_i * rt_i_create(struct db_i *dbip)
void rt_i_clear(struct rt_i *rtip)
DEPRECATED void rt_clean_resource_complete(struct rt_i *rtip, struct resource *resp)
void rt_prep(struct rt_i *rtip)
double fastf_t
fastest 64-bit (or larger) floating point type
Definition vmath.h:333
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:354
#define CUT_MAXIMUM
Definition tol.h:72
Definition ptbl.h:53
Definition vls.h:53
size_t nmiss
solid ft_shot() returned a miss
Definition rt_instance.h:76
size_t nmiss_tree
shots missed sub-tree RPP
Definition rt_instance.h:78
size_t nsolids
total # of solids participating
Definition rt_instance.h:70
size_t rti_nrays
# calls to rt_shootray()
Definition rt_instance.h:73
size_t rti_cut_maxlen
max len RPP list in 1 cut bin
Definition rt_instance.h:84
size_t rti_cut_maxdepth
max depth of cut tree
Definition rt_instance.h:87
size_t rti_ncut_by_type[CUT_MAXIMUM+1]
number of cuts by type
Definition rt_instance.h:85
size_t nmiss_solid
shots missed solid RPP
Definition rt_instance.h:79
size_t rti_cut_totobj
# objs in all bins, total
Definition rt_instance.h:86
size_t nempty_cells
number of empty spatial partition cells passed through
Definition rt_instance.h:81
size_t ndup
duplicate shots at a given solid
Definition rt_instance.h:80
size_t nshots
# of calls to ft_shot()
Definition rt_instance.h:75
size_t nhits
solid ft_shot() returned a hit
Definition rt_instance.h:77
size_t nmiss_model
rays missed model RPP
Definition rt_instance.h:74
size_t nregions
total # of regions participating
Definition rt_instance.h:69
double rti_radius
radius of model bounding sphere
struct bn_tol rti_tol
Math tolerances for this model.
uint32_t rti_magic
magic # for integrity check
point_t mdl_min
min corner of model bounding RPP
fastf_t rti_max_beam_radius
Max threat radius for FASTGEN cline solid.
struct bg_tess_tol rti_ttol
Tessellation tolerance defaults.
int needprep
needs rt_prep
int rti_dont_instance
1=Don't compress instances of solids into 1 while prepping
struct rt_i_internal * i
int rti_save_overlaps
1=fill in pt_overlap_reg, change boolweave behavior
struct rt_i_stats stats
geometry counts and ray-shooting counters
point_t rti_pmax
for plotting, max RPP
int rti_space_partition
space partitioning method
int rti_prismtrace
add support for pixel prism trace
int useair
1="air" regions are retained while prepping
struct bu_ptbl rti_resources
list of 'struct resource's encountered
point_t rti_pmin
for plotting, min RPP
int rti_hasty_prep
1=hasty prep, slower ray-trace
void * rti_udata
ptr for user data.
size_t rti_nlights
number of light sources
char * rti_region_fix_file
rt_regionfix() file or NULL
point_t mdl_max
max corner of model bounding RPP
struct bu_list HeadRegion
ptr of list of regions in model
struct db_i * rti_dbip
prt to Database instance struct
rti_clbk_t rti_gettrees_clbk
Optional user clbk function called during rt_gettrees_and_attrs.
Definition tree.h:166
fundamental vector, matrix, quaternion math macros