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 out_aabb_min Optional sampled AABB minimum; pair with out_aabb_max.
239 * @param out_aabb_max Optional sampled AABB maximum; pair with out_aabb_min.
240 * @param out_obb Optional sampled OBB in ARB8 point ordering.
241 * @param out_points Optional sampled surface points allocated with
242 * bu_malloc; the caller must free the returned array.
243 * @param out_point_count Number of returned points; pair with out_points.
244 * @param rtip Prepared raytrace instance (rt_prep_parallel must
245 * have been called first).
246 * @param params Stopping criteria. NULL or all-zero -> 2 000-ray default.
247 * @param bbox_min Optional focused sampling bbox minimum. Pass NULL to
248 * derive the sampling sphere from prepared soltab extents.
249 * @param bbox_max Optional focused sampling bbox maximum. Pass NULL to
250 * derive the sampling sphere from prepared soltab extents.
251 * At least one output must be requested. All output pointers are optional
252 * except that the AABB pair and point-array/count pair must be supplied
253 * together. Bounds and points are derived from the same converged sample set
254 * used for the surface-area estimate.
255 *
256 * @return The total number of ray-surface crossings accumulated during
257 * sampling (>= 0) on success; -1 on bad arguments or a requested
258 * bound fit failure. A return
259 * value of 0 means no geometry was intersected by the sampler.
260 *
261 * @section crofton_near_tol Near-tolerance sliver geometry: CSG vs BoT divergence
262 *
263 * The Cauchy-Crofton formula is mathematically exact for any well-defined
264 * solid, but its numerical result depends critically on how the underlying
265 * raytracer reports intersections. Two representations of what is intended
266 * to be the same geometry can yield radically different -- and both
267 * internally consistent -- surface-area estimates when the geometry contains
268 * a sub-tolerance sliver.
269 *
270 * @subsection crofton_sliver_anatomy Anatomy of the sliver
271 *
272 * Consider a window-frame region modelled as a base box minus a slightly
273 * smaller cutout box (the r.wind6 pattern in havoc.g). If the subtractor's
274 * face protrudes @e less than BN_TOL_DIST (0.0005 mm) past the base face,
275 * a thin sliver of near-zero thickness is present in the raw CSG description.
276 * The sliver has two large faces (the Z-faces of the cutout interior, each
277 * ~41 600 mm² for a 160 × 260 mm cutout) and a negligible edge band.
278 *
279 * @subsection crofton_csg_behavior CSG raytracer behavior (boolweave filtering)
280 *
281 * The BRL-CAD CSG Boolean evaluator (boolweave) discards any solid segment
282 * whose thickness is below BN_TOL_DIST. For a ray fired perpendicular to the
283 * sliver faces the two-segment chord through the sliver is 0.000340 mm thick
284 * -- shorter than BN_TOL_DIST -- so boolweave merges the entry and exit
285 * events and reports a MISS through that region.
286 *
287 * For oblique rays, however, the apparent thickness grows with the secant of
288 * the angle from normal: chord = d / cos(θ). Once θ exceeds approximately
289 * 47° (for a 0.000340 mm gap and a 0.0005 mm tolerance) the sliver segment
290 * survives boolweave filtering and contributes two crossing events. Since
291 * the Crofton bounding sphere uniformly samples all directions, roughly 68%
292 * of solid angles subtend the sliver at angles steep enough to be counted.
293 * The result is that the CSG SA estimate converges to a value that is ~74%
294 * higher than the ideal clean-frame SA -- not because sampling is insufficient,
295 * but because the CSG raytracer is correctly reflecting its own view of the
296 * geometry: the sliver is visible from the majority of directions but hidden
297 * from near-perpendicular directions. Denser sampling does not reduce this
298 * bias; experiments with up to 2 000 000 rays confirm that the CSG estimate
299 * is stable at ~89 900 mm² (vs an ideal of 51 520 mm²) within the first
300 * 500 000 rays and does not drift further regardless of sample count.
301 *
302 * @subsection crofton_bot_behavior BoT raytracer behavior (per-triangle hits)
303 *
304 * A triangle mesh (BoT) has no boolweave layer. Every ray that intersects a
305 * triangle face produces a hit event regardless of how thin the resulting
306 * segment is. A BoT tessellated from the raw CSG without any sliver
307 * correction therefore exposes both large sliver faces to the full Crofton
308 * hemisphere, converging to ~130 400 mm² -- approximately the ideal frame SA
309 * plus both full sliver faces (51 520 + 2 × 41 600 ≈ 134 720 mm²).
310 *
311 * @subsection crofton_perturb_behavior Perturbed BoT (correct result)
312 *
313 * The facetize command's variant-planning / perturb pass enlarges the
314 * subtractor just enough to eliminate the sub-tolerance sliver before Manifold
315 * performs its Boolean evaluation. The resulting BoT has no phantom interior
316 * faces and its Crofton SA converges to ~51 750 mm² -- within 0.5% of the
317 * analytic ideal -- typically in fewer than 64 000 rays (<0.1 s).
318 *
319 * @subsection crofton_summary Summary table (measured, 200 × 300 × 8 mm frame,
320 * gap = 0.000340 mm < BN_TOL_DIST = 0.0005 mm)
321 *
322 * | Representation | Converged SA (mm²) | vs ideal | Stable by |
323 * |------------------------|--------------------|----------|-------------|
324 * | CSG (raw, unperturbed) | ~89 900 | +74.5% | ~500k rays |
325 * | BoT (perturbed) | ~51 750 | +0.5% | ~64k rays |
326 * | BoT (no perturb) | ~130 400 | +153% | ~32k rays |
327 *
328 * @subsection crofton_implication Design implication
329 *
330 * The Crofton estimator is accurate and well-converged in all three cases;
331 * the divergence is caused by the geometry, not by sampling noise. When
332 * comparing a CSG Crofton SA against a BoT Crofton SA as a facetize quality
333 * check, geometry with sub-tolerance slivers will always produce a mismatch
334 * no matter how many rays are fired. For such geometry the volume estimate
335 * (which is insensitive to sliver SA: sliver volume is ~14 mm³ out of
336 * 147 200 mm³, or 0.01%) is a far more reliable cross-check metric.
337 */
339
340
341/**
342 * Cauchy-Crofton surface-area and/or volume estimator for a primitive.
343 *
344 * Creates a temporary in-memory raytrace of @p ip and fires random chord
345 * rays until the stopping criteria in @p params are satisfied (or all
346 * criteria are zero / params is NULL, in which case the 2 000-ray default
347 * is used). Stores the estimated surface area in @p *area (if non-NULL)
348 * and the estimated volume in @p *vol (if non-NULL).
349 *
350 * This is the primary high-level entry point. Primitives that require
351 * controlled accuracy (e.g. TGC TEC case, triaxial ELL, EHY r1≠r2, HYP,
352 * superell, ETO/TOR spindle) should call this function directly with
353 * appropriate params. Callers that already have a prepared rt_i should
354 * call rt_crofton_shoot() instead.
355 */
357 const struct rt_db_internal *ip,
358 const struct rt_crofton_params *params);
359
361
362#endif /* RT_FUNC_H */
363
364/** @} */
365
366/*
367 * Local Variables:
368 * tab-width: 8
369 * mode: C
370 * indent-tabs-mode: t
371 * c-file-style: "stroustrup"
372 * End:
373 * ex: shiftwidth=4 tabstop=8
374 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define DEPRECATED
Definition common.h:439
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_crofton_shoot(double *out_surf_area, double *out_volume, point_t *out_aabb_min, point_t *out_aabb_max, point_t out_obb[8], point_t **out_points, size_t *out_point_count, struct rt_i *rtip, const struct rt_crofton_params *params, const fastf_t *bbox_min, const fastf_t *bbox_max)
int rt_obj_describe(struct bu_vls *logstr, const struct rt_db_internal *ip, int verbose, double mm2local)
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:336
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition vmath.h:372
fastf_t plane_t[ELEMENTS_PER_PLANE]
Definition of a plane equation.
Definition vmath.h:399
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:357
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