BRL-CAD
region_end.c
Go to the documentation of this file.
1 /* R E G I O N _ E N D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-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 /** @file libgcv/region_end.c
21  *
22  * Routines to process regions during a db_walk_tree.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "bu/parallel.h"
29 #include "gcv.h"
30 
31 
32 union tree *
33 _gcv_cleanup(int state, union tree *tp)
34 {
35  /* restore previous debug state */
36  RTG.NMG_debug = state;
37 
38  /* Dispose of original tree, so that all associated dynamic memory
39  * is released now, not at the end of all regions. A return of
40  * TREE_NULL from this routine signals an error, and there is no
41  * point to adding _another_ message to our output, so we need to
42  * cons up an OP_NOP node to return.
43  */
44  db_free_tree(tp, &rt_uniresource); /* Does an nmg_kr() */
45 
46  BU_ALLOC(tp, union tree);
47  RT_TREE_INIT(tp);
48  tp->tr_op = OP_NOP;
49  return tp;
50 }
51 
52 
53 union tree *
54 gcv_region_end(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *client_data)
55 {
56  union tree *tp = NULL;
57  union tree *ret_tree = NULL;
58  struct nmgregion *r = NULL;
59  struct shell *s = NULL;
60  struct bu_list vhead;
61 
62  int empty_region = 0;
63  int empty_model = 0;
64  int NMG_debug_state = 0;
65 
66  struct gcv_region_end_data *data = (struct gcv_region_end_data *)client_data;
67 
68  if (!tsp || !curtree || !pathp || !client_data) {
69  bu_log("INTERNAL ERROR: gcv_region_end missing parameters\n");
70  return TREE_NULL;
71  }
72 
73  if (!data->write_region) {
74  bu_log("INTERNAL ERROR: gcv_region_end missing conversion callback function\n");
75  return TREE_NULL;
76  }
77 
78  RT_CK_FULL_PATH(pathp);
79  RT_CK_TREE(curtree);
80  RT_CK_TESS_TOL(tsp->ts_ttol);
81  BN_CK_TOL(tsp->ts_tol);
82  NMG_CK_MODEL(*tsp->ts_m);
83 
84  BU_LIST_INIT(&vhead);
85 
86  if (curtree->tr_op == OP_NOP)
87  return curtree;
88 
89  /* get a copy to play with as the parameters might get clobbered
90  * by a longjmp. FIXME: db_dup_subtree() doesn't create real copies
91  */
92  tp = db_dup_subtree(curtree, &rt_uniresource);
93 
94  /* FIXME: we can't free curtree until we get a "real" copy form
95  * db_dup_subtree(). right now we get a fake copy just so we can
96  * keep the compiler quiet about clobbering curtree during longjmp
97  */
98  /* db_free_tree(curtree, &rt_uniresource); */
99 
100  /* Sometimes the NMG library adds debugging bits when it detects
101  * an internal error, before bombing. Stash.
102  */
103  NMG_debug_state = RTG.NMG_debug;
104 
105  /* Begin bomb protection */
106  if (BU_SETJUMP) {
107  /* Error, bail out */
108  char *sofar;
109 
110  /* Relinquish bomb protection */
111  BU_UNSETJUMP;
112 
113  sofar = db_path_to_string(pathp);
114  bu_log("FAILED in boolean evaluation: %s\n", sofar);
115  bu_free((char *)sofar, "sofar");
116 
117  /* Release any intersector 2d tables */
119 
120  /* Get rid of (m)any other intermediate structures */
121  if ((*tsp->ts_m)->magic == NMG_MODEL_MAGIC)
122  nmg_km(*tsp->ts_m);
123  else
124  bu_log("WARNING: tsp->ts_m pointer corrupted, ignoring it.\n");
125 
126  /* Now, make a new, clean model structure for next pass. */
127  *tsp->ts_m = nmg_mm();
128 
129  return _gcv_cleanup(NMG_debug_state, tp);
130  } else {
131 
132  /* perform boolean evaluation on the NMG, presently modifies
133  * curtree to an evaluated result and returns it if the evaluation
134  * is successful.
135  */
136  ret_tree = nmg_booltree_evaluate(tp, tsp->ts_tol, &rt_uniresource);
137 
138  } BU_UNSETJUMP; /* Relinquish bomb protection */
139 
140  r = (struct nmgregion *)NULL;
141  if (ret_tree)
142  r = ret_tree->tr_d.td_r;
143 
144  if (r == (struct nmgregion *)NULL)
145  return _gcv_cleanup(NMG_debug_state, tp);
146 
147  /* Kill cracks */
148  s = BU_LIST_FIRST(shell, &r->s_hd);
149  while (BU_LIST_NOT_HEAD(&s->l, &r->s_hd)) {
150  struct shell *next_s;
151 
152  next_s = BU_LIST_PNEXT(shell, &s->l);
153  if (nmg_kill_cracks(s)) {
154  if (nmg_ks(s)) {
155  empty_region = 1;
156  break;
157  }
158  }
159  s = next_s;
160  }
161  if (empty_region)
162  return _gcv_cleanup(NMG_debug_state, tp);
163 
164  /* kill zero length edgeuses */
165  empty_model = nmg_kill_zero_length_edgeuses(*tsp->ts_m);
166  if (empty_model)
167  return _gcv_cleanup(NMG_debug_state, tp);
168 
169  if (BU_SETJUMP) {
170  /* Error, bail out */
171  char *sofar;
172 
173  /* Relinquish bomb protection */
174  BU_UNSETJUMP;
175 
176  sofar = db_path_to_string(pathp);
177  bu_log("FAILED in triangulator: %s\n", sofar);
178  bu_free((char *)sofar, "sofar");
179 
180  /* Release any intersector 2d tables */
182 
183  /* Get rid of (m)any other intermediate structures */
184  if ((*tsp->ts_m)->magic == NMG_MODEL_MAGIC)
185  nmg_km(*tsp->ts_m);
186  else
187  bu_log("WARNING: tsp->ts_m pointer corrupted, ignoring it.\n");
188 
189  /* Now, make a new, clean model structure for next pass. */
190  *tsp->ts_m = nmg_mm();
191  nmg_kr(r);
192 
193  return _gcv_cleanup(NMG_debug_state, tp);
194  } else {
195 
196  /* Write the region out */
197  data->write_region(r, pathp, tsp->ts_regionid, tsp->ts_gmater, tsp->ts_mater.ma_color, data->client_data);
198 
199  } BU_UNSETJUMP; /* Relinquish bomb protection */
200 
201  nmg_kr(r);
202 
203  return _gcv_cleanup(NMG_debug_state, tp);
204 }
205 
206 
207 /*
208  * Local Variables:
209  * tab-width: 8
210  * mode: C
211  * indent-tabs-mode: t
212  * c-file-style: "stroustrup"
213  * End:
214  * ex: shiftwidth=4 tabstop=8
215  */
#define NMG_MODEL_MAGIC
Definition: magic.h:133
struct model ** ts_m
ptr to ptr to NMG "model"
Definition: raytrace.h:1072
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
struct mater_info ts_mater
material properties
Definition: raytrace.h:1047
Definition: list.h:118
struct nmgregion * td_r
ptr to NMG region
Definition: raytrace.h:1168
int ts_gmater
GIFT compat material code.
Definition: raytrace.h:1045
#define OP_NOP
Leaf with no effect.
Definition: raytrace.h:1132
if lu s
Definition: nmg_mod.c:3860
int nmg_kr(struct nmgregion *r)
Definition: nmg_mk.c:1595
Header file for the BRL-CAD common definitions.
#define RT_TREE_INIT(_p)
Definition: raytrace.h:1189
int nmg_ks(struct shell *s)
Definition: nmg_mk.c:1546
COMPLEX data[64]
Definition: fftest.c:34
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
uint32_t NMG_debug
debug bits for NMG's see nmg.h
Definition: raytrace.h:1699
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define TREE_NULL
Definition: raytrace.h:1181
#define RT_CK_TESS_TOL(_p)
Definition: raytrace.h:184
void nmg_km(struct model *m)
Definition: nmg_mk.c:1634
void db_free_tree(union tree *tp, struct resource *resp)
Definition: db_tree.c:1296
#define BU_LIST_PNEXT(structure, p)
Definition: list.h:422
int nmg_kill_zero_length_edgeuses(struct model *m)
Definition: nmg_misc.c:8351
union tree * gcv_region_end(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *client_data)
Definition: region_end.c:54
#define BN_CK_TOL(_p)
Definition: tol.h:82
char * db_path_to_string(const struct db_full_path *pp)
Definition: db_fullpath.c:191
struct tree::tree_nmgregion tr_d
union tree * _gcv_cleanup(int state, union tree *tp)
Definition: region_end.c:33
float ma_color[3]
explicit color: 0..1
Definition: raytrace.h:522
#define RT_CK_FULL_PATH(_p)
Definition: db_fullpath.h:59
struct model * nmg_mm(void)
Definition: nmg_mk.c:235
#define BU_LIST_INIT(_hp)
Definition: list.h:148
void nmg_isect2d_final_cleanup(void)
Definition: nmg_inter.c:647
int nmg_kill_cracks(struct shell *s)
Definition: nmg_misc.c:8187
const struct rt_tess_tol * ts_ttol
Tessellation tolerance.
Definition: raytrace.h:1070
union tree * db_dup_subtree(const union tree *tp, struct resource *resp)
Definition: db_tree.c:1190
#define BU_UNSETJUMP
Definition: parallel.h:193
union tree * nmg_booltree_evaluate(register union tree *tp, const struct bn_tol *tol, struct resource *resp)
Definition: nmg_bool.c:1307
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define RT_CK_TREE(_p)
Definition: raytrace.h:1182
int ts_regionid
GIFT compat region ID code.
Definition: raytrace.h:1043
#define BU_SETJUMP
Definition: parallel.h:192
const struct bn_tol * ts_tol
Math tolerance.
Definition: raytrace.h:1071
#define BU_LIST_NOT_HEAD(p, hp)
Definition: list.h:324
#define BU_LIST_FIRST(structure, hp)
Definition: list.h:312
struct rt_g RTG
Definition: globals.c:39