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 * create a 'default' object
161 */
162RT_EXPORT extern int rt_obj_make(const struct rt_functab *ftp, struct rt_db_internal *ip);
163
164/**
165 * apply a matrix transformation to an object (translation, rotation, scale)
166 */
167RT_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);
168
169/**
170 * obtain parameters for an object in libpc form
171 */
172RT_EXPORT extern int rt_obj_params(struct pc_pc_set *ps, const struct rt_db_internal *ip);
173
174/**
175 * mirror an object about a plane
176 */
178
179/**
180 * if `stp` is prepped, serialize; otherwise, deserialize from `external`
181 */
182RT_EXPORT extern int rt_obj_prep_serialize(struct soltab *stp, const struct rt_db_internal *ip, struct bu_external *external, size_t *version);
183
184/**
185 * Stopping-criteria parameters for rt_crofton_shoot() and rt_crofton_sample().
186 *
187 * All fields default to zero. Behaviour when all three are zero (or the
188 * pointer is NULL) is identical to the historical default: fire 2 000 rays
189 * and stop when two successive 1 %-threshold iterations agree.
190 *
191 * When one or more non-zero fields are provided, sampling continues until
192 * the FIRST criterion that is met:
193 *
194 * n_rays > 0 Stop once this many rays have been fired in total.
195 *
196 * stability_mm > 0 Stop once the estimate is "stable" to within the
197 * given linear dimension. Stability is measured as the
198 * change in equivalent-sphere radius between successive
199 * iterations: r_sa = sqrt(SA / (4*pi)) for surface area
200 * and r_v = cbrt(3*V / (4*pi)) for volume. Sampling
201 * stops when both |Δr_sa| and |Δr_v| (for whichever
202 * outputs are requested) remain < stability_mm for
203 * consecutive stable windows.
204 *
205 * time_ms > 0 Stop once this many wall-clock milliseconds have
206 * elapsed, returning the best estimate accumulated so far.
207 *
208 * When multiple fields are non-zero the first criterion to fire wins,
209 * giving callers fine control over the accuracy / speed trade-off.
210 *
211 */
213 size_t n_rays; /**< max total rays; 0 = no limit via this criterion */
214 double stability_mm; /**< equivalent-radius stability target (mm); 0 = disabled */
215 double time_ms; /**< wall-clock time budget (ms); 0 = disabled */
216};
217
218
219/**
220 * Run the Cauchy-Crofton ray-sampling estimator on an already-prepared
221 * raytrace instance. The caller owns @p rtip and must call rt_free_rti
222 * after this function returns.
223 *
224 * @param out_surf_area Receives the estimated surface area (mm^2).
225 * @param out_volume Receives the estimated volume (mm^3).
226 * @param rtip Prepared raytrace instance (rt_prep_parallel must
227 * have been called first).
228 * @param params Stopping criteria. NULL or all-zero -> 2 000-ray default.
229 * @param bbox_min Optional focused sampling bbox minimum. Pass NULL to
230 * derive the sampling sphere from prepared soltab extents.
231 * @param bbox_max Optional focused sampling bbox maximum. Pass NULL to
232 * derive the sampling sphere from prepared soltab extents.
233 * @return The total number of ray-surface crossings accumulated during
234 * sampling (>= 0) on success; -1 on bad arguments. A return
235 * value of 0 means no geometry was intersected by the sampler.
236 *
237 * @section crofton_near_tol Near-tolerance sliver geometry: CSG vs BoT divergence
238 *
239 * The Cauchy-Crofton formula is mathematically exact for any well-defined
240 * solid, but its numerical result depends critically on how the underlying
241 * raytracer reports intersections. Two representations of what is intended
242 * to be the same geometry can yield radically different -- and both
243 * internally consistent -- surface-area estimates when the geometry contains
244 * a sub-tolerance sliver.
245 *
246 * @subsection crofton_sliver_anatomy Anatomy of the sliver
247 *
248 * Consider a window-frame region modelled as a base box minus a slightly
249 * smaller cutout box (the r.wind6 pattern in havoc.g). If the subtractor's
250 * face protrudes @e less than BN_TOL_DIST (0.0005 mm) past the base face,
251 * a thin sliver of near-zero thickness is present in the raw CSG description.
252 * The sliver has two large faces (the Z-faces of the cutout interior, each
253 * ~41 600 mm² for a 160 × 260 mm cutout) and a negligible edge band.
254 *
255 * @subsection crofton_csg_behavior CSG raytracer behavior (boolweave filtering)
256 *
257 * The BRL-CAD CSG Boolean evaluator (boolweave) discards any solid segment
258 * whose thickness is below BN_TOL_DIST. For a ray fired perpendicular to the
259 * sliver faces the two-segment chord through the sliver is 0.000340 mm thick
260 * -- shorter than BN_TOL_DIST -- so boolweave merges the entry and exit
261 * events and reports a MISS through that region.
262 *
263 * For oblique rays, however, the apparent thickness grows with the secant of
264 * the angle from normal: chord = d / cos(θ). Once θ exceeds approximately
265 * 47° (for a 0.000340 mm gap and a 0.0005 mm tolerance) the sliver segment
266 * survives boolweave filtering and contributes two crossing events. Since
267 * the Crofton bounding sphere uniformly samples all directions, roughly 68%
268 * of solid angles subtend the sliver at angles steep enough to be counted.
269 * The result is that the CSG SA estimate converges to a value that is ~74%
270 * higher than the ideal clean-frame SA -- not because sampling is insufficient,
271 * but because the CSG raytracer is correctly reflecting its own view of the
272 * geometry: the sliver is visible from the majority of directions but hidden
273 * from near-perpendicular directions. Denser sampling does not reduce this
274 * bias; experiments with up to 2 000 000 rays confirm that the CSG estimate
275 * is stable at ~89 900 mm² (vs an ideal of 51 520 mm²) within the first
276 * 500 000 rays and does not drift further regardless of sample count.
277 *
278 * @subsection crofton_bot_behavior BoT raytracer behavior (per-triangle hits)
279 *
280 * A triangle mesh (BoT) has no boolweave layer. Every ray that intersects a
281 * triangle face produces a hit event regardless of how thin the resulting
282 * segment is. A BoT tessellated from the raw CSG without any sliver
283 * correction therefore exposes both large sliver faces to the full Crofton
284 * hemisphere, converging to ~130 400 mm² -- approximately the ideal frame SA
285 * plus both full sliver faces (51 520 + 2 × 41 600 ≈ 134 720 mm²).
286 *
287 * @subsection crofton_perturb_behavior Perturbed BoT (correct result)
288 *
289 * The facetize command's variant-planning / perturb pass enlarges the
290 * subtractor just enough to eliminate the sub-tolerance sliver before Manifold
291 * performs its Boolean evaluation. The resulting BoT has no phantom interior
292 * faces and its Crofton SA converges to ~51 750 mm² -- within 0.5% of the
293 * analytic ideal -- typically in fewer than 64 000 rays (<0.1 s).
294 *
295 * @subsection crofton_summary Summary table (measured, 200 × 300 × 8 mm frame,
296 * gap = 0.000340 mm < BN_TOL_DIST = 0.0005 mm)
297 *
298 * | Representation | Converged SA (mm²) | vs ideal | Stable by |
299 * |------------------------|--------------------|----------|-------------|
300 * | CSG (raw, unperturbed) | ~89 900 | +74.5% | ~500k rays |
301 * | BoT (perturbed) | ~51 750 | +0.5% | ~64k rays |
302 * | BoT (no perturb) | ~130 400 | +153% | ~32k rays |
303 *
304 * @subsection crofton_implication Design implication
305 *
306 * The Crofton estimator is accurate and well-converged in all three cases;
307 * the divergence is caused by the geometry, not by sampling noise. When
308 * comparing a CSG Crofton SA against a BoT Crofton SA as a facetize quality
309 * check, geometry with sub-tolerance slivers will always produce a mismatch
310 * no matter how many rays are fired. For such geometry the volume estimate
311 * (which is insensitive to sliver SA: sliver volume is ~14 mm³ out of
312 * 147 200 mm³, or 0.01%) is a far more reliable cross-check metric.
313 */
314RT_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);
315
316
317/**
318 * Cauchy-Crofton surface-area and/or volume estimator for a primitive.
319 *
320 * Creates a temporary in-memory raytrace of @p ip and fires random chord
321 * rays until the stopping criteria in @p params are satisfied (or all
322 * criteria are zero / params is NULL, in which case the 2 000-ray default
323 * is used). Stores the estimated surface area in @p *area (if non-NULL)
324 * and the estimated volume in @p *vol (if non-NULL).
325 *
326 * This is the primary high-level entry point. Primitives that require
327 * controlled accuracy (e.g. TGC TEC case, triaxial ELL, EHY r1≠r2, HYP,
328 * superell, ETO/TOR spindle) should call this function directly with
329 * appropriate params. Callers that already have a prepared rt_i should
330 * call rt_crofton_shoot() instead.
331 */
333 const struct rt_db_internal *ip,
334 const struct rt_crofton_params *params);
335
337
338#endif /* RT_FUNC_H */
339
340/** @} */
341
342/*
343 * Local Variables:
344 * tab-width: 8
345 * mode: C
346 * indent-tabs-mode: t
347 * c-file-style: "stroustrup"
348 * End:
349 * ex: shiftwidth=4 tabstop=8
350 */
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_make(const struct rt_functab *ftp, struct rt_db_internal *ip)
int rt_obj_mirror(struct rt_db_internal *ip, const plane_t *plane)
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)
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
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:214
size_t n_rays
Definition func.h:213
double time_ms
Definition func.h:215
Definition seg.h:59
Definition hit.h:152
Primary ray data structure.
Definition xray.h:41