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