BRL-CAD
region_end_mc.c
Go to the documentation of this file.
1 /* R E G I O N _ E N D _ M C . 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 
21 /** @file libgcv/region_end.c
22  *
23  * Routines to process regions during a db_walk_tree using the marching cubes
24  * algorithm.
25  *
26  */
27 
28 #include "common.h"
29 
30 #include "bu/parallel.h"
31 #include "gcv.h"
32 
33 
34 /* in region_end.c */
35 union tree * _gcv_cleanup(int state, union tree *tp);
36 
37 union tree *
38 gcv_region_end_mc(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *client_data)
39 {
40  union tree *tp = NULL;
41  struct model *m = NULL;
42  struct nmgregion *r = NULL;
43  struct shell *s = NULL;
44  struct bu_list vhead;
45 
46  int empty_region = 0;
47  int empty_model = 0;
48  int NMG_debug_state = 0;
49  int count = 0;
50 
51  struct gcv_region_end_data *data = (struct gcv_region_end_data *)client_data;
52 
53  if (!tsp || !pathp || !client_data) {
54  bu_log("INTERNAL ERROR: gcv_region_end_mc missing parameters\n");
55  return TREE_NULL;
56  }
57 
58  if (!data->write_region) {
59  bu_log("INTERNAL ERROR: gcv_region_end missing conversion callback function\n");
60  return TREE_NULL;
61  }
62 
63  RT_CK_FULL_PATH(pathp);
64  RT_CK_TREE(curtree);
65  RT_CK_TESS_TOL(tsp->ts_ttol);
66  BN_CK_TOL(tsp->ts_tol);
67  NMG_CK_MODEL(*tsp->ts_m);
68 
69  BU_LIST_INIT(&vhead);
70 
71  /*
72  if (curtree->tr_op == OP_NOP)
73  return 0;
74  */
75 
76 
77  /* get a copy to play with as the parameters might get clobbered
78  * by a longjmp. FIXME: db_dup_subtree() doesn't create real copies
79  */
80  tp = db_dup_subtree(curtree, &rt_uniresource);
81 
82  /* FIXME: we can't free curtree until we get a "real" copy form
83  * db_dup_subtree(). right now we get a fake copy just so we can
84  * keep the compiler quiet about clobbering curtree during longjmp
85  */
86  /* db_free_tree(curtree, &rt_uniresource); */
87 
88  /* Sometimes the NMG library adds debugging bits when it detects
89  * an internal error, before bombing. Stash.
90  */
91  NMG_debug_state = RTG.NMG_debug;
92 
93  m = nmg_mmr();
94  r = nmg_mrsv(m);
95  s = BU_LIST_FIRST(shell, &r->s_hd);
96 
97  if (tsp->ts_rtip == NULL)
98  tsp->ts_rtip = rt_new_rti(tsp->ts_dbip);
99 
100  count += nmg_mc_evaluate (s, tsp->ts_rtip, pathp, tsp->ts_ttol, tsp->ts_tol);
101 
102  /* empty region? */
103  if (count == 0) {
104  bu_log("Region %s appears to be empty.\n", db_path_to_string(pathp));
105  return TREE_NULL;
106  }
107 
108  /*
109  bu_log("Target is shot, %d triangles seen.\n", count);
110 
111  bu_log("Fusing\n"); fflush(stdout);
112  nmg_model_fuse(m, tsp->ts_tol);
113  bu_log("Done\n"); fflush(stdout);
114  */
115 
116  /* Kill cracks */
117  while (BU_LIST_NOT_HEAD(&s->l, &r->s_hd)) {
118  struct shell *next_s;
119 
120  next_s = BU_LIST_PNEXT(shell, &s->l);
121  if (nmg_kill_cracks(s)) {
122  if (nmg_ks(s)) {
123  empty_region = 1;
124  break;
125  }
126  }
127  /*
128  nmg_shell_coplanar_face_merge(s, tsp->ts_tol, 42);
129  */
130  s = next_s;
131  }
132  if (empty_region)
133  return _gcv_cleanup(NMG_debug_state, tp);
134 
135  /* kill zero length edgeuses */
136  empty_model = nmg_kill_zero_length_edgeuses(*tsp->ts_m);
137  if (empty_model)
138  return _gcv_cleanup(NMG_debug_state, tp);
139 
140  if (BU_SETJUMP) {
141  /* Error, bail out */
142  char *sofar;
143 
144  /* Relinquish bomb protection */
145  BU_UNSETJUMP;
146 
147  sofar = db_path_to_string(pathp);
148  bu_log("FAILED in triangulator: %s\n", sofar);
149  bu_free((char *)sofar, "sofar");
150 
151  /* Release any intersector 2d tables */
153 
154  /* Get rid of (m)any other intermediate structures */
155  if ((*tsp->ts_m)->magic == NMG_MODEL_MAGIC)
156  nmg_km(*tsp->ts_m);
157  else
158  bu_log("WARNING: tsp->ts_m pointer corrupted, ignoring it.\n");
159 
160  /* Now, make a new, clean model structure for next pass. */
161  *tsp->ts_m = nmg_mm();
162  nmg_kr(r);
163 
164  return _gcv_cleanup(NMG_debug_state, tp);
165  } else {
166  /* Write the region out */
167  data->write_region(r, pathp, tsp->ts_regionid, tsp->ts_gmater, tsp->ts_mater.ma_color, data->client_data);
168 
169  } BU_UNSETJUMP; /* Relinquish bomb protection */
170 
171  nmg_kr(r);
172 
173  return _gcv_cleanup(NMG_debug_state, tp);
174 }
175 
176 
177 /*
178  * Local Variables:
179  * tab-width: 8
180  * mode: C
181  * indent-tabs-mode: t
182  * c-file-style: "stroustrup"
183  * End:
184  * ex: shiftwidth=4 tabstop=8
185  */
#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
int ts_gmater
GIFT compat material code.
Definition: raytrace.h:1045
if lu s
Definition: nmg_mod.c:3860
union tree * gcv_region_end_mc(struct db_tree_state *tsp, const struct db_full_path *pathp, union tree *curtree, void *client_data)
Definition: region_end_mc.c:38
int nmg_kr(struct nmgregion *r)
Definition: nmg_mk.c:1595
struct model * nmg_mmr(void)
Definition: nmg_mk.c:268
Header file for the BRL-CAD common definitions.
int nmg_ks(struct shell *s)
Definition: nmg_mk.c:1546
int nmg_mc_evaluate(struct shell *s, struct rt_i *rtip, const struct db_full_path *pathp, const struct rt_tess_tol *ttol, const struct bn_tol *tol)
Definition: nmg_tri_mc.c:737
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
struct db_i * ts_dbip
Definition: raytrace.h:1040
#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
#define BU_LIST_PNEXT(structure, p)
Definition: list.h:422
int nmg_kill_zero_length_edgeuses(struct model *m)
Definition: nmg_misc.c:8351
#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 nmgregion * nmg_mrsv(struct model *m)
Definition: nmg_mk.c:306
float ma_color[3]
explicit color: 0..1
Definition: raytrace.h:522
struct rt_i * rt_new_rti(struct db_i *dbip)
Definition: prep.c:58
#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
struct rt_i * ts_rtip
Helper for rt_gettrees()
Definition: raytrace.h:1073
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
union tree * _gcv_cleanup(int state, union tree *tp)
Definition: region_end.c:33
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