BRL-CAD
Loading...
Searching...
No Matches
ray.h
Go to the documentation of this file.
1/* R A Y . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-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
21/*----------------------------------------------------------------------*/
22/** @addtogroup nmg_ray
23 *
24 * TODO - these structs and ray_in_rpp are versions of librt functionality,
25 * and we need to think about how/where to merge them into a common function
26 * and struct that are available to both libraries without introducing a
27 * coupling dependency.
28 */
29/** @{ */
30/** @file nmg/ray.h */
31
32#ifndef NMG_RAY_H
33#define NMG_RAY_H
34
35#include "common.h"
36
37#include "vmath.h"
38#include "bu/list.h"
39#include "bu/parallel.h"
40#include "nmg/defines.h"
41//#include "nmg/model.h"
42
44
45struct model;
47struct bu_ptbl;
48
49NMG_EXPORT extern struct bu_list re_nmgfree; /**< @brief head of NMG hitmiss freelist */
50
51#define NMG_HIT_LIST 0
52#define NMG_MISS_LIST 1
54/* These values are for the hitmiss "in_out" variable and indicate the
55 * nature of the hit when known
56 */
57#define HMG_INBOUND_STATE(_hm) (((_hm)->in_out & 0x0f0) >> 4)
58#define HMG_OUTBOUND_STATE(_hm) ((_hm)->in_out & 0x0f)
60
61#define NMG_RAY_STATE_INSIDE 1
62#define NMG_RAY_STATE_ON 2
63#define NMG_RAY_STATE_OUTSIDE 4
64#define NMG_RAY_STATE_ANY 8
66#define HMG_HIT_IN_IN 0x11 /**< @brief hit internal structure */
67#define HMG_HIT_IN_OUT 0x14 /**< @brief breaking out */
68#define HMG_HIT_OUT_IN 0x41 /**< @brief breaking in */
69#define HMG_HIT_OUT_OUT 0x44 /**< @brief edge/vertex graze */
70#define HMG_HIT_IN_ON 0x12
71#define HMG_HIT_ON_IN 0x21
72#define HMG_HIT_ON_ON 0x22
73#define HMG_HIT_OUT_ON 0x42
74#define HMG_HIT_ON_OUT 0x24
75#define HMG_HIT_ANY_ANY 0x88 /**< @brief hit on non-3-manifold */
77#define NMG_VERT_ENTER 1
78#define NMG_VERT_ENTER_LEAVE 0
79#define NMG_VERT_LEAVE -1
80#define NMG_VERT_UNKNOWN -2
82#define NMG_HITMISS_SEG_IN 0x696e00 /**< @brief "in" */
83#define NMG_HITMISS_SEG_OUT 0x6f757400 /**< @brief "out" */
85#define NMG_CK_RD(_rd) NMG_CKMAG(_rd, NMG_RAY_DATA_MAGIC, "ray data");
87#ifdef NO_BOMBING_MACROS
88# define NMG_CK_HITMISS(hm) (void)(hm)
89#else
90# define NMG_CK_HITMISS(hm) \
91 {\
92 switch (hm->l.magic) { \
93 case NMG_RT_HIT_MAGIC: \
94 case NMG_RT_HIT_SUB_MAGIC: \
95 case NMG_RT_MISS_MAGIC: \
96 break; \
97 case NMG_MISS_LIST: \
98 bu_log(CPP_FILELINE ": struct hitmiss has NMG_MISS_LIST magic #\n"); \
99 bu_bomb("NMG_CK_HITMISS: going down in flames\n"); \
100 break; \
101 case NMG_HIT_LIST: \
102 bu_log(CPP_FILELINE ": struct hitmiss has NMG_MISS_LIST magic #\n"); \
103 bu_bomb("NMG_CK_HITMISS: going down in flames\n"); \
104 break; \
105 default: \
106 bu_log(CPP_FILELINE ": bad struct hitmiss magic: %u:(0x%08x)\n", \
107 hm->l.magic, hm->l.magic); \
108 bu_bomb("NMG_CK_HITMISS: going down in flames\n"); \
109 }\
110 if (!hm->hit.hit_private) { \
111 bu_log(CPP_FILELINE ": NULL hit_private in hitmiss struct\n"); \
112 bu_bomb("NMG_CK_HITMISS: going down in flames\n"); \
113 } \
114 }
115#endif
116
117#ifdef NO_BOMBING_MACROS
118# define NMG_CK_HITMISS_LISTS(rd) (void)(rd)
119#else
120# define NMG_CK_HITMISS_LISTS(rd) \
121 { \
122 struct nmg_hitmiss *_a_hit; \
123 for (BU_LIST_FOR(_a_hit, nmg_hitmiss, &rd->rd_hit)) {NMG_CK_HITMISS(_a_hit);} \
124 for (BU_LIST_FOR(_a_hit, nmg_hitmiss, &rd->rd_miss)) {NMG_CK_HITMISS(_a_hit);} \
125 }
126#endif
127
128/* librt supplies a prep-owned, worker-local freelist. Standalone libnmg
129 * callers leave hitmiss_free NULL and retain the serialized global fallback. */
130#define NMG_GET_HITMISS_RD(_rd, _p) { \
131 struct bu_list *_hmfree = (_rd)->hitmiss_free; \
132 if (_hmfree) { \
133 (_p) = BU_LIST_FIRST(nmg_hitmiss, _hmfree); \
134 if (UNLIKELY(BU_LIST_IS_HEAD((_p), _hmfree))) \
135 bu_bomb("NMG hitmiss scratch exhausted\n"); \
136 else \
137 BU_LIST_DEQUEUE(&((_p)->l)); \
138 } else { \
139 bu_semaphore_acquire(BU_SEM_GENERAL); \
140 (_p) = BU_LIST_FIRST(nmg_hitmiss, &(re_nmgfree)); \
141 if (BU_LIST_IS_HEAD((_p), &(re_nmgfree))) \
142 BU_ALLOC((_p), struct nmg_hitmiss); \
143 else \
144 BU_LIST_DEQUEUE(&((_p)->l)); \
145 bu_semaphore_release(BU_SEM_GENERAL); \
146 } \
147 }
148
149
150#define NMG_FREE_HITLIST_RD(_rd, _p) { \
151 BU_CK_LIST_HEAD((_p)); \
152 if ((_rd)->hitmiss_free) { \
153 BU_LIST_APPEND_LIST((_rd)->hitmiss_free, (_p)); \
154 } else { \
155 bu_semaphore_acquire(BU_SEM_GENERAL); \
156 BU_LIST_APPEND_LIST(&(re_nmgfree), (_p)); \
157 bu_semaphore_release(BU_SEM_GENERAL); \
158 } \
159 }
160
161/* Source-compatible global-freelist forms for existing libnmg callers. */
162#define NMG_GET_HITMISS(_p) { \
163 bu_semaphore_acquire(BU_SEM_GENERAL); \
164 (_p) = BU_LIST_FIRST(nmg_hitmiss, &(re_nmgfree)); \
165 if (BU_LIST_IS_HEAD((_p), &(re_nmgfree))) \
166 BU_ALLOC((_p), struct nmg_hitmiss); \
167 else \
168 BU_LIST_DEQUEUE(&((_p)->l)); \
169 bu_semaphore_release(BU_SEM_GENERAL); \
170 }
171
172#define NMG_FREE_HITLIST(_p) { \
173 BU_CK_LIST_HEAD((_p)); \
174 bu_semaphore_acquire(BU_SEM_GENERAL); \
175 BU_LIST_APPEND_LIST(&(re_nmgfree), (_p)); \
176 bu_semaphore_release(BU_SEM_GENERAL); \
177 }
178
179#ifdef NO_BOMBING_MACROS
180# define nmg_bu_bomb(rd, vlfree, str) (void)(rd)
181#else
182# define nmg_bu_bomb(rd, vlfree, str) { \
183 bu_log("%s", str); \
184 if (nmg_debug & NMG_DEBUG_NMGRT) bu_bomb("End of diagnostics"); \
185 BU_LIST_INIT(&rd->rd_hit); \
186 BU_LIST_INIT(&rd->rd_miss); \
187 nmg_debug |= NMG_DEBUG_NMGRT; \
188 nmg_isect_ray_model(rd,vlfree); \
189 bu_bomb("Should have bombed before this\n"); \
190 }
191#endif
192
193
194#define HIT 1 /**< @brief a hit on a face */
195#define MISS 0 /**< @brief a miss on the face */
197struct nmg_ray {
199 point_t r_pt; /**< @brief Point at which ray starts */
200 vect_t r_dir; /**< @brief Direction of ray (UNIT Length) */
201 fastf_t r_min; /**< @brief entry dist to bounding sphere */
202 fastf_t r_max; /**< @brief exit dist from bounding sphere */
204
205struct nmg_hit {
207 fastf_t hit_dist; /**< @brief dist from r_pt to hit_point */
208 point_t hit_point; /**< @brief DEPRECATED: Intersection point, use VJOIN1 hit_dist */
209 vect_t hit_normal; /**< @brief DEPRECATED: Surface Normal at hit_point, use RT_HIT_NORMAL */
210 vect_t hit_vpriv; /**< @brief PRIVATE vector for xxx_*() */
211 void * hit_private; /**< @brief PRIVATE handle for xxx_shot() */
212 int hit_surfno; /**< @brief solid-specific surface indicator */
213 struct nmg_ray * hit_rayp; /**< @brief pointer to defining ray */
215
216struct nmg_seg {
217 struct bu_list l;
218 struct nmg_hit seg_in; /**< @brief IN information */
219 struct nmg_hit seg_out; /**< @brief OUT information */
220 void * seg_stp; /**< @brief pointer back to soltab */
222
223struct nmg_hitmiss {
224 struct bu_list l;
225 struct nmg_hit hit;
226 fastf_t dist_in_plane; /**< @brief distance from plane intersect */
227 int in_out; /**< @brief status of ray as it transitions
228 * this hit point.
229 */
230 long *inbound_use;
234 int start_stop; /**< @brief is this a seg_in or seg_out */
235 struct nmg_hitmiss *other; /**< @brief for keeping track of the other
236 * end of the segment when we know
237 * it
238 */
239};
240
241/**
242 * Ray Data structure
243 *
244 * A) the hitmiss table has one element for each nmg structure in the
245 * nmgmodel. The table keeps track of which elements have been
246 * processed before and which haven't. Elements in this table will
247 * either be: (NULL) item not previously processed hitmiss ptr item
248 * previously processed
249 *
250 * the 0th item in the array is a pointer to the head of the "hit"
251 * list. The 1th item in the array is a pointer to the head of the
252 * "miss" list.
253 *
254 * B) If plane_pt is non-null then we are currently processing a face
255 * intersection. The plane_dist and ray_dist_to_plane are valid. The
256 * ray/edge intersector should check the distance from the plane
257 * intercept to the edge and update "plane_closest" if the current
258 * edge is closer to the intercept than the previous closest object.
259 */
260struct nmg_ray_data {
262 struct model *rd_m;
263 char *manifolds; /**< @brief structure 1-3manifold table */
265 struct nmg_ray *rp;
266 void * *ap;
268 void * *stp;
269 const struct bn_tol *tol;
270 struct nmg_hitmiss **hitmiss; /**< @brief 1 struct hitmiss ptr per elem. */
271 struct bu_list rd_hit; /**< @brief list of hit elements */
272 struct bu_list rd_miss; /**< @brief list of missed/sub-hit elements */
274/* The following are to support isect_ray_face() */
275
276 /**
277 * plane_pt is the intercept point of the ray with the plane of
278 * the face.
279 */
280 point_t plane_pt; /**< @brief ray/plane(face) intercept point */
282 /**
283 * ray_dist_to_plane is the parametric distance along the ray from
284 * the ray origin (rd->rp->r_pt) to the ray/plane intercept point
285 */
286 fastf_t ray_dist_to_plane; /**< @brief ray parametric dist to plane */
288 /**
289 * the "face_subhit" element is a boolean used by isect_ray_face
290 * and [e|v]u_touch_func to record the fact that the
291 * ray/(plane/face) intercept point was within tolerance of an
292 * edge/vertex of the face. In such instances, isect_ray_face
293 * does NOT need to generate a hit point for the face, as the hit
294 * point for the edge/vertex will suffice.
295 */
296 int face_subhit;
298 /**
299 * the "classifying_ray" flag indicates that this ray is being
300 * used to classify a point, so that the "eu_touch" and "vu_touch"
301 * functions should not be called.
302 */
303 int classifying_ray;
305 struct bu_list *hitmiss_free; /**< @brief optional worker-local freelist */
306 struct nmg_class_scratch *class_scratch; /**< @brief optional point-classifier scratch */
307 struct bu_ptbl *hitstate[2]; /**< @brief optional state-transition scratch */
309
310int
311ray_in_rpp(struct nmg_ray *rp,
312 const fastf_t *invdir, /* inverses of rp->r_dir[] */
313 const fastf_t *min,
314 const fastf_t *max);
315
316NMG_EXPORT extern int nmg_class_ray_vs_shell(struct nmg_ray *rp,
317 const struct shell *s,
318 const int in_or_out_only,
319 struct bu_list *vlfree,
320 const struct bn_tol *tol);
321
322NMG_EXPORT extern void nmg_isect_ray_model(struct nmg_ray_data *rd, struct bu_list *vlfree);
325
326#endif /* NMG_RAY_H */
327/** @} */
328/*
329 * Local Variables:
330 * mode: C
331 * tab-width: 8
332 * indent-tabs-mode: t
333 * c-file-style: "stroustrup"
334 * End:
335 * ex: shiftwidth=4 tabstop=8
336 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
int nmg_class_ray_vs_shell(struct nmg_ray *rp, const struct shell *s, const int in_or_out_only, struct bu_list *vlfree, const struct bn_tol *tol)
void nmg_isect_ray_model(struct nmg_ray_data *rd, struct bu_list *vlfree)
struct bu_list re_nmgfree
head of NMG hitmiss freelist
int ray_in_rpp(struct nmg_ray *rp, const fastf_t *invdir, const fastf_t *min, const fastf_t *max)
fastf_t vect_t[ELEMENTS_PER_VECT]
3-tuple vector
Definition vmath.h:351
double fastf_t
fastest 64-bit (or larger) floating point type
Definition vmath.h:336
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:357
Definition tol.h:72
Definition ptbl.h:53
Information about where a ray hits the surface.
Definition hit.h:61
NMG topological model.
Definition topology.h:289
Definition ray.h:206
vect_t hit_normal
DEPRECATED: Surface Normal at hit_point, use RT_HIT_NORMAL.
Definition ray.h:210
struct nmg_ray * hit_rayp
pointer to defining ray
Definition ray.h:214
point_t hit_point
DEPRECATED: Intersection point, use VJOIN1 hit_dist.
Definition ray.h:209
uint32_t hit_magic
Definition ray.h:207
int hit_surfno
solid-specific surface indicator
Definition ray.h:213
vect_t hit_vpriv
PRIVATE vector for xxx_*()
Definition ray.h:211
fastf_t hit_dist
dist from r_pt to hit_point
Definition ray.h:208
void * hit_private
PRIVATE handle for xxx_shot()
Definition ray.h:212
long * inbound_use
Definition ray.h:231
vect_t inbound_norm
Definition ray.h:232
struct nmg_hitmiss * other
for keeping track of the other end of the segment when we know it
Definition ray.h:236
fastf_t dist_in_plane
distance from plane intersect
Definition ray.h:227
long * outbound_use
Definition ray.h:233
vect_t outbound_norm
Definition ray.h:234
int start_stop
is this a seg_in or seg_out
Definition ray.h:235
struct bu_list l
Definition ray.h:225
int in_out
status of ray as it transitions this hit point.
Definition ray.h:228
int face_subhit
Definition ray.h:297
struct model * rd_m
Definition ray.h:263
struct bu_list rd_hit
list of hit elements
Definition ray.h:272
struct bu_list * hitmiss_free
optional worker-local freelist
Definition ray.h:306
void ** stp
Definition ray.h:269
struct nmg_hitmiss ** hitmiss
1 struct hitmiss ptr per elem.
Definition ray.h:271
uint32_t magic
Definition ray.h:262
struct nmg_class_scratch * class_scratch
optional point-classifier scratch
Definition ray.h:307
struct nmg_ray * rp
Definition ray.h:266
char * manifolds
structure 1-3manifold table
Definition ray.h:264
const struct bn_tol * tol
Definition ray.h:270
struct bu_list rd_miss
list of missed/sub-hit elements
Definition ray.h:273
struct bu_ptbl * hitstate[2]
optional state-transition scratch
Definition ray.h:308
void ** ap
Definition ray.h:267
fastf_t ray_dist_to_plane
ray parametric dist to plane
Definition ray.h:287
vect_t rd_invdir
Definition ray.h:265
int classifying_ray
Definition ray.h:304
struct nmg_seg * seghead
Definition ray.h:268
point_t plane_pt
ray/plane(face) intercept point
Definition ray.h:281
Definition ray.h:198
point_t r_pt
Point at which ray starts.
Definition ray.h:200
fastf_t r_max
exit dist from bounding sphere
Definition ray.h:203
uint32_t magic
Definition ray.h:199
vect_t r_dir
Direction of ray (UNIT Length)
Definition ray.h:201
fastf_t r_min
entry dist to bounding sphere
Definition ray.h:202
Definition ray.h:217
struct nmg_hit seg_out
OUT information.
Definition ray.h:220
struct nmg_hit seg_in
IN information.
Definition ray.h:219
void * seg_stp
pointer back to soltab
Definition ray.h:221
struct bu_list l
Definition ray.h:218
NMG topological shell.
Definition topology.h:261
fundamental vector, matrix, quaternion math macros