BRL-CAD
nmg_visit.c
Go to the documentation of this file.
1 /* N M G _ V I S I T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1993-2014 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 nmg */
21 /** @{ */
22 /** @file primitives/nmg/nmg_visit.c
23  *
24  * A generalized, object-oriented subroutine family to
25  * visit all the data structures "below" a given structure.
26  *
27  * The caller provides a pointer to the structure to start at,
28  * a table of "handlers" for each kind of structure,
29  * and a generic pointer for private state which will be sent along
30  * to the user's handlers.
31  * For non-leaf structures, there are two handlers, one called
32  * before any recursion starts, and the other called when
33  * recursion is finished. Either or both may be omitted.
34  *
35  */
36 /** @} */
37 
38 #include "common.h"
39 
40 #include <math.h>
41 #include "bio.h"
42 
43 #include "vmath.h"
44 #include "nmg.h"
45 #include "raytrace.h"
46 
47 
48 void
49 nmg_visit_vertex(struct vertex *v, const struct nmg_visit_handlers *htab, void *state)
50 
51 
52 /* Handler's private state */
53 {
54  NMG_CK_VERTEX(v);
55 
56  if (htab->vis_vertex) htab->vis_vertex((uint32_t *)v, state, 0);
57 
58  if (htab->vis_vertex_g && v->vg_p)
59  htab->vis_vertex_g((uint32_t *)v->vg_p, state, 0);
60 }
61 
62 
63 void
64 nmg_visit_vertexuse(struct vertexuse *vu, const struct nmg_visit_handlers *htab, void *state)
65 
66 
67 /* Handler's private state */
68 {
69  NMG_CK_VERTEXUSE(vu);
70 
71  if (htab->bef_vertexuse) htab->bef_vertexuse((uint32_t *)vu, state, 0);
72 
73  nmg_visit_vertex(vu->v_p, htab, state);
74 
75  if (htab->vis_vertexuse_a && vu->a.magic_p)
76  htab->vis_vertexuse_a((uint32_t *)vu->a.magic_p, state, 0);
77 
78  if (htab->aft_vertexuse) htab->aft_vertexuse((uint32_t *)vu, state, 1);
79 }
80 
81 
82 void
83 nmg_visit_edge(struct edge *e, const struct nmg_visit_handlers *htab, void *state)
84 
85 
86 /* Handler's private state */
87 {
88  NMG_CK_EDGE(e);
89 
90  if (htab->vis_edge) htab->vis_edge((uint32_t *)e, state, 0);
91 }
92 
93 
94 void
95 nmg_visit_edgeuse(struct edgeuse *eu, const struct nmg_visit_handlers *htab, void *state)
96 
97 
98 /* Handler's private state */
99 {
100  NMG_CK_EDGEUSE(eu);
101 
102  if (htab->bef_edgeuse) htab->bef_edgeuse((uint32_t *)eu, state, 0);
103 
104  nmg_visit_vertexuse(eu->vu_p, htab, state);
105  nmg_visit_edge(eu->e_p, htab, state);
106 
107  if (htab->vis_edge_g && eu->g.magic_p)
108  htab->vis_edge_g((uint32_t *)eu->g.magic_p, state, 0);
109 
110  if (htab->aft_edgeuse) htab->aft_edgeuse((uint32_t *)eu, state, 1);
111 }
112 
113 
114 void
115 nmg_visit_loop(struct loop *l, const struct nmg_visit_handlers *htab, void *state)
116 
117 
118 /* Handler's private state */
119 {
120  NMG_CK_LOOP(l);
121 
122  if (htab->vis_loop) htab->vis_loop((uint32_t *)l, state, 0);
123 
124  if (htab->vis_loop_g && l->lg_p)
125  htab->vis_loop_g((uint32_t *)l->lg_p, state, 0);
126 }
127 
128 
129 void
130 nmg_visit_loopuse(struct loopuse *lu, const struct nmg_visit_handlers *htab, void *state)
131 
132 
133 /* Handler's private state */
134 {
135  NMG_CK_LOOPUSE(lu);
136 
137  if (htab->bef_loopuse) htab->bef_loopuse((uint32_t *)lu, state, 0);
138 
139  if (BU_LIST_FIRST_MAGIC(&lu->down_hd) == NMG_VERTEXUSE_MAGIC) {
140  struct vertexuse *vu;
141  vu = BU_LIST_FIRST(vertexuse, &lu->down_hd);
142  nmg_visit_vertexuse(vu, htab, state);
143  } else {
144  struct edgeuse *eu;
145  for (BU_LIST_FOR(eu, edgeuse, &lu->down_hd)) {
146  nmg_visit_edgeuse(eu, htab, state);
147  }
148  }
149  nmg_visit_loop(lu->l_p, htab, state);
150 
151  if (htab->aft_loopuse) htab->aft_loopuse((uint32_t *)lu, state, 1);
152 }
153 
154 
155 void
156 nmg_visit_face(struct face *f, const struct nmg_visit_handlers *htab, void *state)
157 
158 
159 /* Handler's private state */
160 {
161 
162  if (htab->vis_face) htab->vis_face((uint32_t *)f, state, 0);
163 
164  if (htab->vis_face_g && f->g.plane_p)
165  htab->vis_face_g((uint32_t *)f->g.plane_p, state, 0);
166 }
167 
168 
169 void
170 nmg_visit_faceuse(struct faceuse *fu, const struct nmg_visit_handlers *htab, void *state)
171 
172 
173 /* Handler's private state */
174 {
175  struct loopuse *lu;
176 
177  NMG_CK_FACEUSE(fu);
178 
179  if (htab->bef_faceuse) htab->bef_faceuse((uint32_t *)fu, state, 0);
180 
181  for (BU_LIST_FOR(lu, loopuse, &fu->lu_hd)) {
182  nmg_visit_loopuse(lu, htab, state);
183  }
184 
185  nmg_visit_face(fu->f_p, htab, state);
186 
187  if (htab->aft_faceuse) htab->aft_faceuse((uint32_t *)fu, state, 1);
188 }
189 
190 
191 void
192 nmg_visit_shell(struct shell *s, const struct nmg_visit_handlers *htab, void *state)
193 
194 
195 /* Handler's private state */
196 {
197  struct faceuse *fu;
198  struct loopuse *lu;
199  struct edgeuse *eu;
200 
201  NMG_CK_SHELL(s);
202 
203  if (htab->bef_shell) htab->bef_shell((uint32_t *)s, state, 0);
204 
205  for (BU_LIST_FOR(fu, faceuse, &s->fu_hd)) {
206  nmg_visit_faceuse(fu, htab, state);
207  }
208  for (BU_LIST_FOR(lu, loopuse, &s->lu_hd)) {
209  nmg_visit_loopuse(lu, htab, state);
210  }
211  for (BU_LIST_FOR(eu, edgeuse, &s->eu_hd)) {
212  nmg_visit_edgeuse(eu, htab, state);
213  }
214  if (s->vu_p) nmg_visit_vertexuse(s->vu_p, htab, state);
215  if (htab->vis_shell_a && s->sa_p)
216  htab->vis_shell_a((uint32_t *)s->sa_p, state, 0);
217 
218  if (htab->aft_shell) htab->aft_shell((uint32_t *)s, state, 1);
219 }
220 
221 
222 void
223 nmg_visit_region(struct nmgregion *r, const struct nmg_visit_handlers *htab, void *state)
224 
225 
226 /* Handler's private state */
227 {
228  struct shell *s;
229 
230  NMG_CK_REGION(r);
231 
232  if (htab->bef_region) htab->bef_region((uint32_t *)r, state, 0);
233 
234  for (BU_LIST_FOR(s, shell, &r->s_hd)) {
235  nmg_visit_shell(s, htab, state);
236  }
237  if (htab->vis_region_a && r->ra_p)
238  htab->vis_region_a((uint32_t *)r->ra_p, state, 0);
239 
240  if (htab->aft_region) htab->aft_region((uint32_t *)r, state, 1);
241 }
242 void
243 nmg_visit_model(struct model *model, const struct nmg_visit_handlers *htab, void *state)
244 
245 
246 /* Handler's private state */
247 {
248  struct nmgregion *r;
249 
250  NMG_CK_MODEL(model);
251 
252  if (htab->bef_model) htab->bef_model((uint32_t *)model, state, 0);
253 
254  for (BU_LIST_FOR(r, nmgregion, &model->r_hd)) {
255  nmg_visit_region(r, htab, state);
256  }
257 
258  if (htab->aft_model) htab->aft_model((uint32_t *)model, state, 1);
259 }
260 
261 
262 void
263 nmg_visit(const uint32_t *magicp, const struct nmg_visit_handlers *htab, void *state)
264 /* Handler's private state */
265 {
266  switch (*magicp) {
267  default:
268  bu_log("nmg_visit() Can't visit %s directly\n", bu_identify_magic(*magicp));
269  bu_bomb("nmg_visit()\n");
270  /* NOTREACHED */
271  case NMG_MODEL_MAGIC:
272  nmg_visit_model((struct model *)magicp, htab, state);
273  break;
274  case NMG_REGION_MAGIC:
275  nmg_visit_region((struct nmgregion *)magicp, htab, state);
276  break;
277  case NMG_SHELL_MAGIC:
278  nmg_visit_shell((struct shell *)magicp, htab, state);
279  break;
280  case NMG_FACEUSE_MAGIC:
281  nmg_visit_faceuse((struct faceuse *)magicp, htab, state);
282  break;
283  case NMG_LOOPUSE_MAGIC:
284  nmg_visit_loopuse((struct loopuse *)magicp, htab, state);
285  break;
286  case NMG_EDGEUSE_MAGIC:
287  nmg_visit_edgeuse((struct edgeuse *)magicp, htab, state);
288  break;
289  case NMG_VERTEXUSE_MAGIC:
290  nmg_visit_vertexuse((struct vertexuse *)magicp, htab, state);
291  break;
292  }
293 }
294 
295 
296 /*
297  * Local Variables:
298  * mode: C
299  * tab-width: 8
300  * indent-tabs-mode: t
301  * c-file-style: "stroustrup"
302  * End:
303  * ex: shiftwidth=4 tabstop=8
304  */
#define NMG_MODEL_MAGIC
Definition: magic.h:133
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
#define NMG_EDGEUSE_MAGIC
Definition: magic.h:120
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
void nmg_visit_region(struct nmgregion *r, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:223
void nmg_visit_face(struct face *f, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:156
#define NMG_SHELL_MAGIC
Definition: magic.h:142
if lu s
Definition: nmg_mod.c:3860
lu
Definition: nmg_mod.c:3855
Header file for the BRL-CAD common definitions.
#define NMG_LOOPUSE_MAGIC
Definition: magic.h:130
NMG_CK_LOOPUSE(lu)
void nmg_visit_shell(struct shell *s, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:192
void nmg_visit_edge(struct edge *e, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:83
#define NMG_REGION_MAGIC
Definition: magic.h:137
#define BU_LIST_FIRST_MAGIC(hp)
Definition: list.h:416
#define NMG_VERTEXUSE_MAGIC
Definition: magic.h:145
void nmg_visit_vertex(struct vertex *v, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:49
void nmg_visit(const uint32_t *magicp, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:263
void nmg_visit_loop(struct loop *l, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:115
void nmg_visit_faceuse(struct faceuse *fu, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:170
void nmg_visit_model(struct model *model, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:243
NMG_CK_SHELL(s)
void nmg_visit_vertexuse(struct vertexuse *vu, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:64
#define NMG_FACEUSE_MAGIC
Definition: magic.h:124
void nmg_visit_loopuse(struct loopuse *lu, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:130
void bu_bomb(const char *str) _BU_ATTR_NORETURN
Definition: bomb.c:91
const char * bu_identify_magic(uint32_t magic)
void nmg_visit_edgeuse(struct edgeuse *eu, const struct nmg_visit_handlers *htab, void *state)
Definition: nmg_visit.c:95
#define BU_LIST_FIRST(structure, hp)
Definition: list.h:312