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-2025 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/hist.h"
34#include "bu/ptbl.h"
35#include "bn/tol.h"
36#include "rt/defines.h"
37#include "rt/db_instance.h"
38#include "rt/region.h"
39#include "rt/resource.h"
40#include "rt/space_partition.h" /* cutter */
41#include "rt/soltab.h"
42#include "rt/tol.h"
43
45
46// libbu's callback type isn't quite right for this case, so we might as well
47// be specific.
48typedef void(*rti_clbk_t)(struct rt_i *rtip, struct db_tree_state *tsp, struct region *r);
49
50/**
51 * This structure keeps track of almost everything for ray-tracing
52 * support: Regions, primitives, model bounding box, statistics.
53 *
54 * Definitions for librt which are specific to the particular model
55 * being processed, one copy for each model. Initially, a pointer to
56 * this is returned from rt_dirbuild().
57 *
58 * During gettree processing, the most time consuming step is
59 * searching the list of existing solids to see if a new solid is
60 * actually an identical instance of a previous solid. Therefore, the
61 * list has been divided into several lists. The same macros & hash
62 * value that accesses the dbi_Head[] array are used here. The hash
63 * value is computed by db_dirhash().
64 */
65struct rt_i {
66 uint32_t rti_magic; /**< @brief magic # for integrity check */
67 /* THESE ITEMS ARE AVAILABLE FOR APPLICATIONS TO READ & MODIFY */
68 int useair; /**< @brief 1="air" regions are retained while prepping */
69 int rti_save_overlaps; /**< @brief 1=fill in pt_overlap_reg, change boolweave behavior */
70 int rti_dont_instance; /**< @brief 1=Don't compress instances of solids into 1 while prepping */
71 int rti_hasty_prep; /**< @brief 1=hasty prep, slower ray-trace */
72 size_t rti_nlights; /**< @brief number of light sources */
73 int rti_prismtrace; /**< @brief add support for pixel prism trace */
74 char * rti_region_fix_file; /**< @brief rt_regionfix() file or NULL */
75 int rti_space_partition; /**< @brief space partitioning method */
76 struct bn_tol rti_tol; /**< @brief Math tolerances for this model */
77 struct bg_tess_tol rti_ttol; /**< @brief Tessellation tolerance defaults */
78 fastf_t rti_max_beam_radius; /**< @brief Max threat radius for FASTGEN cline solid */
79 rti_clbk_t rti_gettrees_clbk; /**< @brief Optional user clbk function called during rt_gettrees_and_attrs */
80 void * rti_udata; /**< @brief ptr for user data. */
81 /* THESE ITEMS ARE AVAILABLE FOR APPLICATIONS TO READ */
82 point_t mdl_min; /**< @brief min corner of model bounding RPP */
83 point_t mdl_max; /**< @brief max corner of model bounding RPP */
84 point_t rti_pmin; /**< @brief for plotting, min RPP */
85 point_t rti_pmax; /**< @brief for plotting, max RPP */
86 double rti_radius; /**< @brief radius of model bounding sphere */
87 struct db_i * rti_dbip; /**< @brief prt to Database instance struct */
88 /* THESE ITEMS SHOULD BE CONSIDERED OPAQUE, AND SUBJECT TO CHANGE */
89 int needprep; /**< @brief needs rt_prep */
90 struct region ** Regions; /**< @brief ptrs to regions [reg_bit] */
91 struct bu_list HeadRegion; /**< @brief ptr of list of regions in model */
92 struct bu_ptbl delete_regs; /**< @brief list of region pointers to delete after light_init() */
93 /* Ray-tracing statistics */
94 size_t nregions; /**< @brief total # of regions participating */
95 size_t nsolids; /**< @brief total # of solids participating */
96 size_t rti_nrays; /**< @brief # calls to rt_shootray() */
97 size_t nmiss_model; /**< @brief rays missed model RPP */
98 size_t nshots; /**< @brief # of calls to ft_shot() */
99 size_t nmiss; /**< @brief solid ft_shot() returned a miss */
100 size_t nhits; /**< @brief solid ft_shot() returned a hit */
101 size_t nmiss_tree; /**< @brief shots missed sub-tree RPP */
102 size_t nmiss_solid; /**< @brief shots missed solid RPP */
103 size_t ndup; /**< @brief duplicate shots at a given solid */
104 size_t nempty_cells; /**< @brief number of empty spatial partition cells passed through */
105 union cutter rti_CutHead; /**< @brief Head of cut tree */
106 union cutter rti_inf_box; /**< @brief List of infinite solids */
107 union cutter * rti_CutFree; /**< @brief cut Freelist */
108 struct bu_ptbl rti_busy_cutter_nodes; /**< @brief List of "cutter" mallocs */
110 size_t rti_cut_maxlen; /**< @brief max len RPP list in 1 cut bin */
111 size_t rti_ncut_by_type[CUT_MAXIMUM+1]; /**< @brief number of cuts by type */
112 size_t rti_cut_totobj; /**< @brief # objs in all bins, total */
113 size_t rti_cut_maxdepth; /**< @brief max depth of cut tree */
117 size_t rti_air_discards; /**< @brief # of air regions discarded */
118 struct bu_hist rti_hist_cellsize; /**< @brief occupancy of cut cells */
119 struct bu_hist rti_hist_cell_pieces; /**< @brief solid pieces per cell */
120 struct bu_hist rti_hist_cutdepth; /**< @brief depth of cut tree */
121 struct soltab ** rti_Solids; /**< @brief ptrs to soltab [st_bit] */
122 struct bu_list rti_solidheads[RT_DBNHASH]; /**< @brief active solid lists */
123 struct bu_ptbl rti_resources; /**< @brief list of 'struct resource's encountered */
124 size_t rti_cutlen; /**< @brief goal for # solids per boxnode */
125 size_t rti_cutdepth; /**< @brief goal for depth of NUBSPT cut tree */
126 /* Parameters required for rt_submodel */
127 char * rti_treetop; /**< @brief bu_strduped, for rt_submodel rti's only */
128 size_t rti_uses; /**< @brief for rt_submodel */
129 /* Parameters for accelerating "pieces" of solids */
130 size_t rti_nsolids_with_pieces; /**< @brief # solids using pieces */
131 /* Parameters for dynamic geometry */
134};
135
136
137#define RTI_NULL ((struct rt_i *)0)
138
139#define RT_CHECK_RTI(_p) BU_CKMAG(_p, RTI_MAGIC, "struct rt_i")
140#define RT_CK_RTI(_p) RT_CHECK_RTI(_p)
141
142/**
143 * Macros to painlessly visit all the active solids. Serving suggestion:
144 *
145 * RT_VISIT_ALL_SOLTABS_START(stp, rtip) {
146 * rt_pr_soltab(stp);
147 * } RT_VISIT_ALL_SOLTABS_END
148 */
149#define RT_VISIT_ALL_SOLTABS_START(_s, _rti) { \
150 struct bu_list *_head = &((_rti)->rti_solidheads[0]); \
151 for (; _head < &((_rti)->rti_solidheads[RT_DBNHASH]); _head++) \
152 for (BU_LIST_FOR(_s, soltab, _head)) {
153
154#define RT_VISIT_ALL_SOLTABS_END } }
155
156/**************************/
157/* Applications interface */
158/**************************/
159
160/* Prepare for raytracing */
161
162RT_EXPORT extern struct rt_i *rt_new_rti(struct db_i *dbip);
163RT_EXPORT extern void rt_free_rti(struct rt_i *rtip);
164RT_EXPORT extern void rt_prep(struct rt_i *rtip);
165RT_EXPORT extern void rt_prep_parallel(struct rt_i *rtip,
166 int ncpu);
167
168
169/* Get expr tree for object */
170/**
171 * User-called function to add a tree hierarchy to the displayed set.
172 *
173 * This function is not multiply re-entrant.
174 *
175 * Returns -
176 * 0 Ordinarily
177 * -1 On major error
178 *
179 * Note: -2 returns from rt_gettrees_and_attrs are filtered.
180 */
181RT_EXPORT extern int rt_gettree(struct rt_i *rtip,
182 const char *node);
183RT_EXPORT extern int rt_gettrees(struct rt_i *rtip,
184 int argc,
185 const char **argv, int ncpus);
186
187/**
188 * User-called function to add a set of tree hierarchies to the active set.
189 * Includes getting the indicated list of attributes and an optional
190 * user-supplied rti_gettrees_clbk callback function to collect additional
191 * information in rti_udata. (stashed in the rt_i structure).
192 *
193 * This function may run in parallel, but is not multiply re-entrant itself,
194 * because db_walk_tree() isn't multiply re-entrant. Note that callback
195 * implementations should protect any data writes to a shared structure with
196 * the RT_SEM_RESULTS semaphore.
197 *
198 * Semaphores used for critical sections in parallel mode:
199 * RT_SEM_TREE ====> protects rti_solidheads[] lists, d_uses(solids)
200 * RT_SEM_RESULTS => protects HeadRegion, mdl_min/max, d_uses(reg), nregions
201 * RT_SEM_WORKER ==> (db_walk_dispatcher, from db_walk_tree)
202 * RT_SEM_STATS ===> nsolids
203 *
204 * INPUTS:
205 *
206 * rtip - RT instance pointer
207 *
208 * attrs - attribute value set
209 *
210 * argc - number of trees to get
211 *
212 * argv - array of char pointers to the names of the tree tops
213 *
214 * ncpus - number of cpus to use
215 *
216 * Returns -
217 * 0 Ordinarily
218 * -1 On major error
219 */
220RT_EXPORT extern int rt_gettrees_and_attrs(struct rt_i *rtip,
221 const char **attrs,
222 int argc,
223 const char **argv,
224 int ncpus);
225
226/* Print the partitions */
227RT_EXPORT extern void rt_pr_partitions(const struct rt_i *rtip,
228 const struct partition *phead,
229 const char *title);
230
231/**
232 * @brief
233 * Find solid by leaf name
234 *
235 * Given the (leaf) name of a solid, find the first occurrence of it
236 * in the solid list. Used mostly to find the light source. Returns
237 * soltab pointer, or RT_SOLTAB_NULL.
238 */
239RT_EXPORT extern struct soltab *rt_find_solid(const struct rt_i *rtip,
240 const char *name);
241
242/**
243 * initialize a memory resource structure for use during ray tracing.
244 *
245 * a given resource structure is prepared for use and marked as the
246 * resource for a given thread of execution (indicated by 'cpu_num').
247 * if an 'rtip' ray tracing instance pointer is provided, the resource
248 * structure will be stored within so that it's available to threads
249 * of execution during parallel ray tracing.
250 *
251 * This routine should initialize all the same resources that
252 * rt_clean_resource() releases. It shouldn't (but currently does for
253 * ptbl) allocate any dynamic memory, just init pointers & lists.
254 */
255
256struct rt_i; /* forward declaration */
257
258RT_EXPORT extern void rt_init_resource(struct resource *resp, int cpu_num, struct rt_i *rtip);
259
260
261RT_EXPORT extern void rt_clean_resource_basic(struct rt_i *rtip,
262 struct resource *resp);
263RT_EXPORT extern void rt_clean_resource(struct rt_i *rtip,
264 struct resource *resp);
266 struct resource *resp);
267
268
269/* Plot a solid */
271 FILE *fp,
272 struct rt_i *rtip,
273 const struct soltab *stp,
274 struct resource *resp);
275
276/* Release storage assoc with rt_i */
277RT_EXPORT extern void rt_clean(struct rt_i *rtip);
278RT_EXPORT extern int rt_del_regtree(struct rt_i *rtip,
279 struct region *delregp,
280 struct resource *resp);
281/* Check in-memory data structures */
282RT_EXPORT extern void rt_ck(struct rt_i *rtip);
283
284/* Print value of tree for a partition */
285RT_EXPORT extern void rt_pr_tree_val(const union tree *tp,
286 const struct partition *partp,
287 int pr_name, int lvl);
288/* Print a partition */
289RT_EXPORT extern void rt_pr_partition(const struct rt_i *rtip,
290 const struct partition *pp);
292 const struct rt_i *rtip,
293 const struct partition *pp);
294
295
296/**
297 * Go through all the solids in the model, given the model mins and
298 * maxes, and generate a cutting tree. A strategy better than
299 * incrementally cutting each solid is to build a box node which
300 * contains everything in the model, and optimize it.
301 *
302 * This is the main entry point into space partitioning from
303 * rt_prep().
304 */
305RT_EXPORT extern void rt_cut_it(struct rt_i *rtip,
306 int ncpu);
307
308/* free a cut tree */
309/**
310 * Free a whole cut tree below the indicated node. The strategy we
311 * use here is to free everything BELOW the given node, so as not to
312 * clobber rti_CutHead !
313 */
314RT_EXPORT extern void rt_fr_cut(struct rt_i *rtip,
315 union cutter *cutp);
316
317/**
318 * Apply any deltas to reg_regionid values to allow old applications
319 * that use the reg_regionid number to distinguish between different
320 * instances of the same prototype region.
321 *
322 * Called once, from rt_prep(), before raytracing begins.
323 */
324RT_EXPORT extern void rt_regionfix(struct rt_i *rtip);
325
326
327#ifdef USE_OPENCL
328RT_EXPORT extern void clt_init(void);
329
330RT_EXPORT extern void
331clt_db_store(size_t count, struct soltab *solids[]);
332
333RT_EXPORT extern void
334clt_db_store_bvh(size_t count, struct clt_linear_bvh_node *nodes);
335
336RT_EXPORT extern void
338
339RT_EXPORT extern void
341
342RT_EXPORT extern void clt_db_release(void);
343
344
345RT_EXPORT void clt_prep(struct rt_i *rtip);
346#endif
347
348
350
351#endif /* RT_RT_INSTANCE_H */
352/** @} */
353/*
354 * Local Variables:
355 * tab-width: 8
356 * mode: C
357 * indent-tabs-mode: t
358 * c-file-style: "stroustrup"
359 * End:
360 * ex: shiftwidth=4 tabstop=8
361 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition defines.h:161
#define ID_MAX_SOLID
Maximum defined ID_xxx for solids.
Definition defines.h:118
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)
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:48
int rt_del_regtree(struct rt_i *rtip, struct region *delregp, struct resource *resp)
void rt_clean_resource(struct rt_i *rtip, struct resource *resp)
int rt_plot_solid(FILE *fp, struct rt_i *rtip, const struct soltab *stp, struct resource *resp)
void rt_pr_partition_vls(struct bu_vls *v, const struct rt_i *rtip, const struct partition *pp)
struct rt_i * rt_new_rti(struct db_i *dbip)
void rt_clean_resource_basic(struct rt_i *rtip, struct resource *resp)
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)
void rt_free_rti(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)
int rt_gettrees(struct rt_i *rtip, int argc, const char **argv, int ncpus)
void rt_clean(struct rt_i *rtip)
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)
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:334
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:355
#define CUT_MAXIMUM
Definition tol.h:72
Definition hist.h:49
Definition ptbl.h:53
Definition vls.h:53
struct bu_hist rti_hist_cellsize
occupancy of cut cells
size_t rti_nsolids_with_pieces
# solids using pieces
double rti_radius
radius of model bounding sphere
Definition rt_instance.h:86
size_t nmiss
solid ft_shot() returned a miss
Definition rt_instance.h:99
size_t rti_cutlen
goal for # solids per boxnode
struct bn_tol rti_tol
Math tolerances for this model.
Definition rt_instance.h:76
size_t nmiss_tree
shots missed sub-tree RPP
uint32_t rti_magic
magic # for integrity check
Definition rt_instance.h:66
size_t nsolids
total # of solids participating
Definition rt_instance.h:95
size_t rti_nrays
# calls to rt_shootray()
Definition rt_instance.h:96
point_t mdl_min
min corner of model bounding RPP
Definition rt_instance.h:82
fastf_t rti_max_beam_radius
Max threat radius for FASTGEN cline solid.
Definition rt_instance.h:78
struct bu_hist rti_hist_cutdepth
depth of cut tree
size_t rti_nsol_by_type[ID_MAX_SOLID+1]
struct bg_tess_tol rti_ttol
Tessellation tolerance defaults.
Definition rt_instance.h:77
size_t rti_air_discards
# of air regions discarded
size_t rti_cut_maxlen
max len RPP list in 1 cut bin
int needprep
needs rt_prep
Definition rt_instance.h:89
size_t rti_uses
for rt_submodel
size_t rti_cut_maxdepth
max depth of cut tree
int rti_dont_instance
1=Don't compress instances of solids into 1 while prepping
Definition rt_instance.h:70
union cutter * rti_CutFree
cut Freelist
int rti_save_overlaps
1=fill in pt_overlap_reg, change boolweave behavior
Definition rt_instance.h:69
struct bu_hist rti_hist_cell_pieces
solid pieces per cell
union cutter rti_inf_box
List of infinite solids.
size_t rti_ncut_by_type[CUT_MAXIMUM+1]
number of cuts by type
point_t rti_pmax
for plotting, max RPP
Definition rt_instance.h:85
int rti_space_partition
space partitioning method
Definition rt_instance.h:75
struct bu_ptbl rti_cuts_waiting
size_t nmiss_solid
shots missed solid RPP
size_t rti_cut_totobj
# objs in all bins, total
int rti_prismtrace
add support for pixel prism trace
Definition rt_instance.h:73
size_t rti_maxsol_by_type
struct region ** Regions
ptrs to regions [reg_bit]
Definition rt_instance.h:90
int useair
1="air" regions are retained while prepping
Definition rt_instance.h:68
struct bu_ptbl rti_resources
list of 'struct resource's encountered
size_t nempty_cells
number of empty spatial partition cells passed through
int rti_add_to_new_solids_list
point_t rti_pmin
for plotting, min RPP
Definition rt_instance.h:84
size_t ndup
duplicate shots at a given solid
struct bu_list rti_solidheads[RT_DBNHASH]
active solid lists
int rti_hasty_prep
1=hasty prep, slower ray-trace
Definition rt_instance.h:71
void * rti_udata
ptr for user data.
Definition rt_instance.h:80
size_t nshots
# of calls to ft_shot()
Definition rt_instance.h:98
struct soltab ** rti_sol_by_type[ID_MAX_SOLID+1]
char * rti_treetop
bu_strduped, for rt_submodel rti's only
size_t rti_nlights
number of light sources
Definition rt_instance.h:72
size_t rti_cutdepth
goal for depth of NUBSPT cut tree
char * rti_region_fix_file
rt_regionfix() file or NULL
Definition rt_instance.h:74
point_t mdl_max
max corner of model bounding RPP
Definition rt_instance.h:83
size_t nhits
solid ft_shot() returned a hit
size_t nmiss_model
rays missed model RPP
Definition rt_instance.h:97
struct bu_list HeadRegion
ptr of list of regions in model
Definition rt_instance.h:91
struct db_i * rti_dbip
prt to Database instance struct
Definition rt_instance.h:87
union cutter rti_CutHead
Head of cut tree.
struct bu_ptbl delete_regs
list of region pointers to delete after light_init()
Definition rt_instance.h:92
size_t nregions
total # of regions participating
Definition rt_instance.h:94
struct bu_ptbl rti_new_solids
rti_clbk_t rti_gettrees_clbk
Optional user clbk function called during rt_gettrees_and_attrs.
Definition rt_instance.h:79
struct soltab ** rti_Solids
ptrs to soltab [st_bit]
struct bu_ptbl rti_busy_cutter_nodes
List of "cutter" mallocs.
Definition tree.h:169
fundamental vector, matrix, quaternion math macros