BRL-CAD
Loading...
Searching...
No Matches
func.h
Go to the documentation of this file.
1/* R T F U N C . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2010-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_obj
21 *
22 * @brief Primitive manipulation functions from former functab
23 * callback table.
24 *
25 * As this is a relatively new set of interfaces, consider these
26 * functions preliminary (i.e. DEPRECATED) and subject to change until
27 * this message goes away.
28 *
29 */
30#ifndef RT_FUNC_H
31#define RT_FUNC_H
32
33#include "common.h"
34
35#include "bu/list.h"
36#include "bu/parse.h"
37#include "bu/vls.h"
38#include "bg/plane.h"
39#include "bn/tol.h"
40#include "rt/defines.h"
41#include "rt/application.h"
42#include "rt/functab.h"
43#include "rt/hit.h"
44#include "rt/piece.h"
45#include "rt/seg.h"
46#include "rt/soltab.h"
47#include "rt/tol.h"
48#include "rt/db_internal.h"
49#include "rt/db_instance.h"
50#include "rt/rt_instance.h"
51#include "rt/xray.h"
52#include "pc.h"
53
54/** @{ */
55/** @file rt/func.h */
56
58
59/**
60 * prep an object for ray tracing
61 */
62RT_EXPORT extern int rt_obj_prep(struct soltab *stp, struct rt_db_internal *ip, struct rt_i *rtip);
63
64/**
65 * shoot a ray at an object that has been prepped for ray tracing
66 */
67RT_EXPORT extern int rt_obj_shot(struct soltab *stp, struct xray *rp, struct application *ap, struct seg *seghead);
68
69/**
70 * TBD.
71 */
72RT_EXPORT extern int rt_obj_piece_shot(struct rt_piecestate *psp, struct rt_piecelist *plp, double dist_corr, struct xray *rp, struct application *ap, struct seg *seghead);
73
74/**
75 * TBD.
76 */
77RT_EXPORT extern int rt_obj_piece_hitsegs(struct rt_piecestate *psp, struct seg *seghead, struct application *ap);
78
79/**
80 * print an objects parameters in debug/diagnostic form
81 */
82RT_EXPORT extern int rt_obj_print(const struct soltab *stp);
83
84/**
85 * calculate a normal on an object that has been hit via rt_shot()
86 */
87RT_EXPORT extern int rt_obj_norm(struct hit *hitp, struct soltab *stp, struct xray *rp);
88
89/**
90 * calculate object uv parameterization for a given hit point
91 */
92RT_EXPORT extern int rt_obj_uv(struct application *ap, struct soltab *stp, struct hit *hitp, struct uvcoord *uvp);
93
94/**
95 * calculate object curvature for a given hit point
96 */
97RT_EXPORT extern int rt_obj_curve(struct curvature *cvp, struct hit *hitp, struct soltab *stp);
98
99/**
100 * DEPRECATED: Unimplemented.
101 */
103
104/**
105 * release the memory used by a solid
106 */
107RT_EXPORT extern int rt_obj_free(struct soltab *stp);
108
109/**
110 * obtain a vlist wireframe representation of an object for plotting purposes
111 */
112RT_EXPORT extern int rt_obj_plot(struct bu_list *vhead, struct rt_db_internal *ip, const struct bg_tess_tol *ttol, const struct bn_tol *tol);
113
114/**
115 * shoot an array of rays at a set of homogeneous objects.
116 */
117RT_EXPORT extern int rt_obj_vshot(struct soltab *stp[], struct xray *rp[], struct seg *segp, int n, struct application *ap);
118
119/**
120 * tessellate an object (into NMG form)
121 */
122RT_EXPORT extern int rt_obj_tess(struct nmgregion **r, struct model *m, struct rt_db_internal *ip, const struct bg_tess_tol *ttol, const struct bn_tol *tol);
123
124/**
125 * tessellate an object (into NURBS NMG form)
126 */
127RT_EXPORT extern int rt_obj_tnurb(struct nmgregion **r, struct model *m, struct rt_db_internal *ip, const struct bn_tol *tol);
128
129/**
130 * v4/v5 object import from disk
131 */
132RT_EXPORT extern int rt_obj_import(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip);
133
134/**
135 * v4/v5 object export to disk
136 */
137RT_EXPORT extern int rt_obj_export(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip);
138
139/**
140 * free the internal representation of an object
141 */
143
144/**
145 * db object 'get' support, obtain a tcl list representation
146 */
147RT_EXPORT extern int rt_obj_get(struct bu_vls *logstr, const struct rt_db_internal *ip, const char *attr);
148
149/**
150 * db object 'adjust' support, modify a tcl list representation
151 */
152RT_EXPORT extern int rt_obj_adjust(struct bu_vls *logstr, struct rt_db_internal *ip, int argc, const char **argv);
153
154/**
155 * describe an object in text form (used by the 'l' command)
156 */
157RT_EXPORT extern int rt_obj_describe(struct bu_vls *logstr, const struct rt_db_internal *ip, int verbose, double mm2local);
158
159/**
160 * Return to @p labels (separated by @p sep, defaulting to space if NULL)
161 * the set of type labels accepted by rt_obj_make(); sorted alphabetically.
162 * Caller's responsibility to free bu_vls.
163 */
164RT_EXPORT extern void rt_obj_make_labels(struct bu_vls *labels, const char *sep);
165
166/**
167 * create a 'default' object of the type named by @p label, scaled by
168 * @p scale and positioned at @p origin.
169 *
170 * @p label is the user's type word ("sph", "rcc", "arb6", "tor", ...) and
171 * doubles as the variant selector for primitives with geometry aliases.
172 * Returns BRLCAD_OK on success, BRLCAD_ERROR otherwise.
173 */
174RT_EXPORT extern int rt_obj_make(const char *label, const point_t origin, double scale, struct rt_db_internal *ip);
175
176/**
177 * apply a matrix transformation to an object (translation, rotation, scale)
178 */
179RT_EXPORT extern int rt_obj_xform(struct rt_db_internal *op, const mat_t mat, struct rt_db_internal *ip, int release, struct db_i *dbip);
180
181/**
182 * obtain parameters for an object in libpc form
183 */
184RT_EXPORT extern int rt_obj_params(struct pc_pc_set *ps, const struct rt_db_internal *ip);
185
186/**
187 * mirror an object about a plane
188 */
190
191/**
192 * if `stp` is prepped, serialize; otherwise, deserialize from `external`
193 */
194RT_EXPORT extern int rt_obj_prep_serialize(struct soltab *stp, const struct rt_db_internal *ip, struct bu_external *external, size_t *version);
195
196/**
197 * Stopping-criteria parameters for rt_crofton_shoot() and rt_crofton_sample().
198 *
199 * All fields default to zero. Behaviour when all three are zero (or the
200 * pointer is NULL) is identical to the historical default: fire 2 000 rays
201 * and stop when two successive 1 %-threshold iterations agree.
202 *
203 * When one or more non-zero fields are provided, sampling continues until
204 * the FIRST criterion that is met:
205 *
206 * n_rays > 0 Stop once this many rays have been fired in total.
207 *
208 * stability_mm > 0 Stop once the estimate is "stable" to within the
209 * given linear dimension. Stability is measured as the
210 * change in equivalent-sphere radius between successive
211 * iterations: r_sa = sqrt(SA / (4*pi)) for surface area
212 * and r_v = cbrt(3*V / (4*pi)) for volume. Sampling
213 * stops when both |Δr_sa| and |Δr_v| (for whichever
214 * outputs are requested) remain < stability_mm for
215 * consecutive stable windows.
216 *
217 * time_ms > 0 Stop once this many wall-clock milliseconds have
218 * elapsed, returning the best estimate accumulated so far.
219 *
220 * When multiple fields are non-zero the first criterion to fire wins,
221 * giving callers fine control over the accuracy / speed trade-off.
222 *
223 */
225 size_t n_rays; /**< max total rays; 0 = no limit via this criterion */
226 double stability_mm; /**< equivalent-radius stability target (mm); 0 = disabled */
227 double time_ms; /**< wall-clock time budget (ms); 0 = disabled */
228};
229
230
231/**
232 * Run the Cauchy-Crofton ray-sampling estimator on an already-prepared
233 * raytrace instance. The caller owns @p rtip and must call rt_free_rti
234 * after this function returns.
235 *
236 * @param out_surf_area Receives the estimated surface area (mm^2).
237 * @param out_volume Receives the estimated volume (mm^3).
238 * @param rtip Prepared raytrace instance (rt_prep_parallel must
239 * have been called first).
240 * @param params Stopping criteria. NULL or all-zero -> 2 000-ray default.
241 * @param bbox_min Optional focused sampling bbox minimum. Pass NULL to
242 * derive the sampling sphere from prepared soltab extents.
243 * @param bbox_max Optional focused sampling bbox maximum. Pass NULL to
244 * derive the sampling sphere from prepared soltab extents.
245 * @return The total number of ray-surface crossings accumulated during
246 * sampling (>= 0) on success; -1 on bad arguments. A return
247 * value of 0 means no geometry was intersected by the sampler.
248 *
249 * @section crofton_near_tol Near-tolerance sliver geometry: CSG vs BoT divergence
250 *
251 * The Cauchy-Crofton formula is mathematically exact for any well-defined
252 * solid, but its numerical result depends critically on how the underlying
253 * raytracer reports intersections. Two representations of what is intended
254 * to be the same geometry can yield radically different -- and both
255 * internally consistent -- surface-area estimates when the geometry contains
256 * a sub-tolerance sliver.
257 *
258 * @subsection crofton_sliver_anatomy Anatomy of the sliver
259 *
260 * Consider a window-frame region modelled as a base box minus a slightly
261 * smaller cutout box (the r.wind6 pattern in havoc.g). If the subtractor's
262 * face protrudes @e less than BN_TOL_DIST (0.0005 mm) past the base face,
263 * a thin sliver of near-zero thickness is present in the raw CSG description.
264 * The sliver has two large faces (the Z-faces of the cutout interior, each
265 * ~41 600 mm² for a 160 × 260 mm cutout) and a negligible edge band.
266 *
267 * @subsection crofton_csg_behavior CSG raytracer behavior (boolweave filtering)
268 *
269 * The BRL-CAD CSG Boolean evaluator (boolweave) discards any solid segment
270 * whose thickness is below BN_TOL_DIST. For a ray fired perpendicular to the
271 * sliver faces the two-segment chord through the sliver is 0.000340 mm thick
272 * -- shorter than BN_TOL_DIST -- so boolweave merges the entry and exit
273 * events and reports a MISS through that region.
274 *
275 * For oblique rays, however, the apparent thickness grows with the secant of
276 * the angle from normal: chord = d / cos(θ). Once θ exceeds approximately
277 * 47° (for a 0.000340 mm gap and a 0.0005 mm tolerance) the sliver segment
278 * survives boolweave filtering and contributes two crossing events. Since
279 * the Crofton bounding sphere uniformly samples all directions, roughly 68%
280 * of solid angles subtend the sliver at angles steep enough to be counted.
281 * The result is that the CSG SA estimate converges to a value that is ~74%
282 * higher than the ideal clean-frame SA -- not because sampling is insufficient,
283 * but because the CSG raytracer is correctly reflecting its own view of the
284 * geometry: the sliver is visible from the majority of directions but hidden
285 * from near-perpendicular directions. Denser sampling does not reduce this
286 * bias; experiments with up to 2 000 000 rays confirm that the CSG estimate
287 * is stable at ~89 900 mm² (vs an ideal of 51 520 mm²) within the first
288 * 500 000 rays and does not drift further regardless of sample count.
289 *
290 * @subsection crofton_bot_behavior BoT raytracer behavior (per-triangle hits)
291 *
292 * A triangle mesh (BoT) has no boolweave layer. Every ray that intersects a
293 * triangle face produces a hit event regardless of how thin the resulting
294 * segment is. A BoT tessellated from the raw CSG without any sliver
295 * correction therefore exposes both large sliver faces to the full Crofton
296 * hemisphere, converging to ~130 400 mm² -- approximately the ideal frame SA
297 * plus both full sliver faces (51 520 + 2 × 41 600 ≈ 134 720 mm²).
298 *
299 * @subsection crofton_perturb_behavior Perturbed BoT (correct result)
300 *
301 * The facetize command's variant-planning / perturb pass enlarges the
302 * subtractor just enough to eliminate the sub-tolerance sliver before Manifold
303 * performs its Boolean evaluation. The resulting BoT has no phantom interior
304 * faces and its Crofton SA converges to ~51 750 mm² -- within 0.5% of the
305 * analytic ideal -- typically in fewer than 64 000 rays (<0.1 s).
306 *
307 * @subsection crofton_summary Summary table (measured, 200 × 300 × 8 mm frame,
308 * gap = 0.000340 mm < BN_TOL_DIST = 0.0005 mm)
309 *
310 * | Representation | Converged SA (mm²) | vs ideal | Stable by |
311 * |------------------------|--------------------|----------|-------------|
312 * | CSG (raw, unperturbed) | ~89 900 | +74.5% | ~500k rays |
313 * | BoT (perturbed) | ~51 750 | +0.5% | ~64k rays |
314 * | BoT (no perturb) | ~130 400 | +153% | ~32k rays |
315 *
316 * @subsection crofton_implication Design implication
317 *
318 * The Crofton estimator is accurate and well-converged in all three cases;
319 * the divergence is caused by the geometry, not by sampling noise. When
320 * comparing a CSG Crofton SA against a BoT Crofton SA as a facetize quality
321 * check, geometry with sub-tolerance slivers will always produce a mismatch
322 * no matter how many rays are fired. For such geometry the volume estimate
323 * (which is insensitive to sliver SA: sliver volume is ~14 mm³ out of
324 * 147 200 mm³, or 0.01%) is a far more reliable cross-check metric.
325 */
326RT_EXPORT extern int rt_crofton_shoot(double *out_surf_area, double *out_volume, struct rt_i *rtip, const struct rt_crofton_params *params, const fastf_t *bbox_min, const fastf_t *bbox_max);
327
328
329/**
330 * Cauchy-Crofton surface-area and/or volume estimator for a primitive.
331 *
332 * Creates a temporary in-memory raytrace of @p ip and fires random chord
333 * rays until the stopping criteria in @p params are satisfied (or all
334 * criteria are zero / params is NULL, in which case the 2 000-ray default
335 * is used). Stores the estimated surface area in @p *area (if non-NULL)
336 * and the estimated volume in @p *vol (if non-NULL).
337 *
338 * This is the primary high-level entry point. Primitives that require
339 * controlled accuracy (e.g. TGC TEC case, triaxial ELL, EHY r1≠r2, HYP,
340 * superell, ETO/TOR spindle) should call this function directly with
341 * appropriate params. Callers that already have a prepared rt_i should
342 * call rt_crofton_shoot() instead.
343 */
345 const struct rt_db_internal *ip,
346 const struct rt_crofton_params *params);
347
349
350#endif /* RT_FUNC_H */
351
352/** @} */
353
354/*
355 * Local Variables:
356 * tab-width: 8
357 * mode: C
358 * indent-tabs-mode: t
359 * c-file-style: "stroustrup"
360 * End:
361 * ex: shiftwidth=4 tabstop=8
362 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define DEPRECATED
Definition common.h:433
int rt_obj_export(struct bu_external *ep, const struct rt_db_internal *ip, double local2mm, const struct db_i *dbip)
DEPRECATED int rt_obj_class(void)
int rt_obj_tnurb(struct nmgregion **r, struct model *m, struct rt_db_internal *ip, const struct bn_tol *tol)
int rt_obj_params(struct pc_pc_set *ps, const struct rt_db_internal *ip)
int rt_obj_prep(struct soltab *stp, struct rt_db_internal *ip, struct rt_i *rtip)
void rt_crofton_sample(fastf_t *area, fastf_t *vol, const struct rt_db_internal *ip, const struct rt_crofton_params *params)
int rt_obj_ifree(struct rt_db_internal *ip)
int rt_obj_free(struct soltab *stp)
int rt_obj_print(const struct soltab *stp)
int rt_obj_mirror(struct rt_db_internal *ip, const plane_t *plane)
int rt_obj_make(const char *label, const point_t origin, double scale, struct rt_db_internal *ip)
int rt_obj_adjust(struct bu_vls *logstr, struct rt_db_internal *ip, int argc, const char **argv)
int rt_obj_tess(struct nmgregion **r, struct model *m, struct rt_db_internal *ip, const struct bg_tess_tol *ttol, const struct bn_tol *tol)
int rt_obj_import(struct rt_db_internal *ip, const struct bu_external *ep, const mat_t mat, const struct db_i *dbip)
int rt_obj_curve(struct curvature *cvp, struct hit *hitp, struct soltab *stp)
int rt_obj_plot(struct bu_list *vhead, struct rt_db_internal *ip, const struct bg_tess_tol *ttol, const struct bn_tol *tol)
void rt_obj_make_labels(struct bu_vls *labels, const char *sep)
int rt_obj_norm(struct hit *hitp, struct soltab *stp, struct xray *rp)
int rt_obj_shot(struct soltab *stp, struct xray *rp, struct application *ap, struct seg *seghead)
int rt_obj_vshot(struct soltab *stp[], struct xray *rp[], struct seg *segp, int n, struct application *ap)
int rt_obj_get(struct bu_vls *logstr, const struct rt_db_internal *ip, const char *attr)
int rt_obj_xform(struct rt_db_internal *op, const mat_t mat, struct rt_db_internal *ip, int release, struct db_i *dbip)
int rt_obj_describe(struct bu_vls *logstr, const struct rt_db_internal *ip, int verbose, double mm2local)
int rt_crofton_shoot(double *out_surf_area, double *out_volume, struct rt_i *rtip, const struct rt_crofton_params *params, const fastf_t *bbox_min, const fastf_t *bbox_max)
int rt_obj_uv(struct application *ap, struct soltab *stp, struct hit *hitp, struct uvcoord *uvp)
int rt_obj_piece_hitsegs(struct rt_piecestate *psp, struct seg *seghead, struct application *ap)
int rt_obj_prep_serialize(struct soltab *stp, const struct rt_db_internal *ip, struct bu_external *external, size_t *version)
int rt_obj_piece_shot(struct rt_piecestate *psp, struct rt_piecelist *plp, double dist_corr, struct xray *rp, struct application *ap, struct seg *seghead)
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 point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:354
Definition tol.h:72
Definition vls.h:53
Information about where a ray hits the surface.
Definition hit.h:61
NMG topological model.
Definition topology.h:289
NMG topological region.
Definition topology.h:277
double stability_mm
Definition func.h:226
size_t n_rays
Definition func.h:225
double time_ms
Definition func.h:227
Definition seg.h:59
Definition hit.h:152
Primary ray data structure.
Definition xray.h:41