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