BRL-CAD
decompose.c
Go to the documentation of this file.
1 /* D E C O M P O S E . 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 libged/decompose.c
21  *
22  * The decompose command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "./ged_private.h"
33 
34 int
35 ged_decompose(struct ged *gedp, int argc, const char *argv[])
36 {
37  int count;
38  struct bu_vls solid_name = BU_VLS_INIT_ZERO;
39  char *nmg_solid_name;
40  char *prefix;
41  char *def_prefix="sh";
42  struct model *m;
43  struct nmgregion *r;
44  struct model *new_m;
45  struct nmgregion *tmp_r;
46  struct shell *kill_s;
47  struct directory *dp;
48  struct rt_db_internal nmg_intern;
49  static const char *usage = "nmg [prefix]";
50 
53  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
54 
55  /* initialize result */
56  bu_vls_trunc(gedp->ged_result_str, 0);
57 
58  /* must be wanting help */
59  if (argc == 1) {
60  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
61  return GED_HELP;
62  }
63 
64  if (argc < 2 || 3 < argc) {
65  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
66  return GED_ERROR;
67  }
68 
69  count = 0;
70  nmg_solid_name = (char *)argv[1];
71 
72  if (argc > 2) {
73  prefix = (char *)argv[2];
74  if (db_version(gedp->ged_wdbp->dbip) < 5 && strlen(prefix) > NAMESIZE) {
75  bu_vls_printf(gedp->ged_result_str, "%s: Prefix %s is too long", argv[0], prefix);
76  return GED_ERROR;
77  }
78  } else {
79  prefix = def_prefix;
80  }
81 
82  if ((dp=db_lookup(gedp->ged_wdbp->dbip, nmg_solid_name, LOOKUP_NOISY)) == RT_DIR_NULL)
83  return GED_ERROR;
84 
85  if (rt_db_get_internal(&nmg_intern, dp, gedp->ged_wdbp->dbip, bn_mat_identity, &rt_uniresource) < 0) {
86  bu_vls_printf(gedp->ged_result_str, "%s: rt_db_get_internal(%s) error\n", argv[0], nmg_solid_name);
87  return GED_ERROR;
88  }
89 
90  if (nmg_intern.idb_type != ID_NMG) {
91  bu_vls_printf(gedp->ged_result_str, "%s: %s is not an NMG solid!", argv[0], nmg_solid_name);
92  return GED_ERROR;
93  }
94 
95  m = (struct model *)nmg_intern.idb_ptr;
96  NMG_CK_MODEL(m);
97 
98  /* create temp region to hold duplicate shell */
99  tmp_r = nmg_mrsv(m); /* temp nmgregion to hold dup shells */
100  kill_s = BU_LIST_FIRST(shell, &tmp_r->s_hd);
101  (void)nmg_ks(kill_s);
102 
103  for (BU_LIST_FOR(r, nmgregion, &m->r_hd)) {
104  struct shell *s;
105 
106  if (r == tmp_r)
107  continue;
108 
109  for (BU_LIST_FOR(s, shell, &r->s_hd)) {
110  struct shell *tmp_s;
111  struct shell *decomp_s;
112  long **trans_tbl;
113 
114  /* duplicate shell */
115  tmp_s = (struct shell *)nmg_dup_shell(s, &trans_tbl, &gedp->ged_wdbp->wdb_tol);
116  bu_free((char *)trans_tbl, "trans_tbl");
117 
118  /* move duplicate to temp region */
119  (void) nmg_mv_shell_to_region(tmp_s, tmp_r);
120 
121  /* decompose this shell */
122  (void) nmg_decompose_shell(tmp_s, &gedp->ged_wdbp->wdb_tol);
123 
124  /* move each decomposed shell to yet another region */
125  decomp_s = BU_LIST_FIRST(shell, &tmp_r->s_hd);
126  while (BU_LIST_NOT_HEAD(&decomp_s->l, &tmp_r->s_hd)) {
127  struct shell *next_s;
128  struct shell *new_s;
129  struct rt_db_internal new_intern;
130  struct directory *new_dp;
131  struct nmgregion *decomp_r;
132  char shell_no[32];
133  size_t end_prefix;
134 
135  next_s = BU_LIST_NEXT(shell, &decomp_s->l);
136 
137  decomp_r = nmg_mrsv(m);
138  kill_s = BU_LIST_FIRST(shell, &decomp_r->s_hd);
139  (void)nmg_ks(kill_s);
140  nmg_shell_a(decomp_s, &gedp->ged_wdbp->wdb_tol);
141  new_s = (struct shell *)nmg_dup_shell(decomp_s, &trans_tbl, &gedp->ged_wdbp->wdb_tol);
142  (void)nmg_mv_shell_to_region(new_s, decomp_r);
143 
144  /* move this region to a different model */
145  new_m = (struct model *)nmg_mk_model_from_region(decomp_r, 1);
146  (void)nmg_rebound(new_m, &gedp->ged_wdbp->wdb_tol);
147 
148  /* create name for this shell */
149  count++;
150  bu_vls_strcpy(&solid_name, prefix);
151  sprintf(shell_no, "_%d", count);
152  if (db_version(gedp->ged_wdbp->dbip) < 5) {
153  end_prefix = strlen(prefix);
154  if (end_prefix + strlen(shell_no) > NAMESIZE)
155  end_prefix = NAMESIZE - strlen(shell_no);
156  bu_vls_trunc(&solid_name, (int)end_prefix);
157  bu_vls_strncat(&solid_name, shell_no, NAMESIZE-bu_vls_strlen(&solid_name));
158  } else {
159  bu_vls_strcat(&solid_name, shell_no);
160  }
161 
162  if (db_lookup(gedp->ged_wdbp->dbip, bu_vls_addr(&solid_name), LOOKUP_QUIET) != RT_DIR_NULL) {
163  bu_vls_printf(gedp->ged_result_str, "%s: cannot create unique solid name (%s)",
164  argv[0], bu_vls_addr(&solid_name));
165  return GED_ERROR;
166  }
167 
168  /* write this model as a separate nmg solid */
169  RT_DB_INTERNAL_INIT(&new_intern);
170  new_intern.idb_major_type = DB5_MAJORTYPE_BRLCAD;
171  new_intern.idb_type = ID_NMG;
172  new_intern.idb_meth = &OBJ[ID_NMG];
173  new_intern.idb_ptr = (void *)new_m;
174 
175  new_dp=db_diradd(gedp->ged_wdbp->dbip, bu_vls_addr(&solid_name), RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&new_intern.idb_type);
176  if (new_dp == RT_DIR_NULL) {
177  bu_vls_free(&solid_name);
178  bu_vls_printf(gedp->ged_result_str, "%s: Database alloc error, aborting", argv[0]);
179  return GED_ERROR;
180  }
181 
182  if (rt_db_put_internal(new_dp, gedp->ged_wdbp->dbip, &new_intern, &rt_uniresource) < 0) {
183  (void)nmg_km(new_m);
184  bu_vls_printf(gedp->ged_result_str, "%s: rt_db_put_internal(%s) failure\n",
185  argv[0], bu_vls_addr(&solid_name));
186  bu_vls_free(&solid_name);
187  return GED_ERROR;
188  }
189 
190  (void)nmg_ks(decomp_s);
191  decomp_s = next_s;
192  }
193  }
194  }
195 
196  rt_db_free_internal(&nmg_intern);
197  bu_vls_free(&solid_name);
198 
199  return GED_OK;
200 }
201 
202 
203 /*
204  * Local Variables:
205  * tab-width: 8
206  * mode: C
207  * indent-tabs-mode: t
208  * c-file-style: "stroustrup"
209  * End:
210  * ex: shiftwidth=4 tabstop=8
211  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
void bu_vls_strncat(struct bu_vls *vp, const char *s, size_t n)
Definition: vls.c:390
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
struct model * nmg_mk_model_from_region(struct nmgregion *r, int reindex)
Definition: nmg_misc.c:3416
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
struct db_i * dbip
Definition: raytrace.h:1266
if lu s
Definition: nmg_mod.c:3860
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
void nmg_rebound(struct model *m, const struct bn_tol *tol)
Definition: nmg_misc.c:2072
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
int nmg_ks(struct shell *s)
Definition: nmg_mk.c:1546
int idb_major_type
Definition: raytrace.h:192
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define ID_NMG
n-Manifold Geometry solid
Definition: raytrace.h:469
void nmg_km(struct model *m)
Definition: nmg_mk.c:1634
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
int nmg_decompose_shell(struct shell *s, const struct bn_tol *tol)
Definition: nmg_misc.c:4082
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
int nmg_mv_shell_to_region(struct shell *s, struct nmgregion *r)
Definition: nmg_misc.c:4690
struct nmgregion * nmg_mrsv(struct model *m)
Definition: nmg_mk.c:306
struct directory * db_diradd(struct db_i *, const char *name, off_t laddr, size_t len, int flags, void *ptr)
Definition: db_lookup.c:190
void * idb_ptr
Definition: raytrace.h:195
struct bn_tol wdb_tol
Definition: raytrace.h:1269
const struct rt_functab OBJ[]
Definition: table.c:159
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
struct shell * nmg_dup_shell(struct shell *s, long int ***trans_tbl, const struct bn_tol *tol)
Definition: nmg_misc.c:2673
#define GED_HELP
Definition: ged.h:62
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
void nmg_shell_a(struct shell *s, const struct bn_tol *tol)
Definition: nmg_mk.c:2474
int ged_decompose(struct ged *gedp, int argc, const char *argv[])
Definition: decompose.c:35
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
#define BU_LIST_NEXT(structure, hp)
Definition: list.h:316
#define BU_LIST_NOT_HEAD(p, hp)
Definition: list.h:324
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_LIST_FIRST(structure, hp)
Definition: list.h:312