BRL-CAD
fracture.c
Go to the documentation of this file.
1 /* F R A C T U R 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/fracture.c
21  *
22  * The fracture 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 
35 static int frac_stat;
36 
37 
38 static void
39 fracture_add_nmg_part(struct ged *gedp, char *newname, struct model *m)
40 {
41  struct rt_db_internal new_intern;
42  struct directory *new_dp;
43  struct nmgregion *r;
44 
45  if (db_lookup(gedp->ged_wdbp->dbip, newname, LOOKUP_QUIET) != RT_DIR_NULL) {
46  bu_vls_printf(gedp->ged_result_str, "%s: already exists\n", newname);
47  /* Free memory here */
48  nmg_km(m);
49  frac_stat = 1;
50  return;
51  }
52 
53  new_dp=db_diradd(gedp->ged_wdbp->dbip, newname, RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&new_intern.idb_type);
54  if (new_dp == RT_DIR_NULL) {
56  "Failed to add new object name (%s) to directory - aborting!!\n",
57  newname);
58  return;
59  }
60 
61  /* make sure the geometry/bounding boxes are up to date */
62  for (BU_LIST_FOR(r, nmgregion, &m->r_hd))
63  nmg_region_a(r, &gedp->ged_wdbp->wdb_tol);
64 
65 
66  /* Export NMG as a new solid */
67  RT_DB_INTERNAL_INIT(&new_intern);
68  new_intern.idb_major_type = DB5_MAJORTYPE_BRLCAD;
69  new_intern.idb_type = ID_NMG;
70  new_intern.idb_meth = &OBJ[ID_NMG];
71  new_intern.idb_ptr = (void *)m;
72 
73  if (rt_db_put_internal(new_dp, gedp->ged_wdbp->dbip, &new_intern, &rt_uniresource) < 0) {
74  /* Free memory */
75  nmg_km(m);
76  bu_vls_printf(gedp->ged_result_str, "rt_db_put_internal() failure\n");
77  frac_stat = 1;
78  return;
79  }
80  /* Internal representation has been freed by rt_db_put_internal */
81  new_intern.idb_ptr = (void *)NULL;
82  frac_stat = 0;
83 }
84 
85 
86 int
87 ged_fracture(struct ged *gedp, int argc, const char *argv[])
88 {
89  int i;
90  struct directory *old_dp;
91  struct rt_db_internal old_intern;
92  struct model *m, *new_model;
93  char newname[32];
94  char prefix[32];
95  int maxdigits;
96  struct nmgregion *r, *new_r;
97  struct shell *s, *new_s;
98  struct faceuse *fu;
99  struct vertex *v_new, *v;
100  size_t tw, tf, tp;
101  static const char *usage = "nmg_solid [prefix]";
102 
105  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
106 
107  /* initialize result */
108  bu_vls_trunc(gedp->ged_result_str, 0);
109 
110  /* must be wanting help */
111  if (argc == 1) {
112  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
113  return GED_HELP;
114  }
115 
116  if (3 < argc) {
117  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
118  return GED_ERROR;
119  }
120 
121  bu_vls_printf(gedp->ged_result_str, "fracture:");
122  for (i = 0; i < argc; i++)
123  bu_vls_printf(gedp->ged_result_str, " %s", argv[i]);
124  bu_vls_printf(gedp->ged_result_str, "\n");
125 
126  if ((old_dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL)
127  return GED_ERROR;
128 
129  if (rt_db_get_internal(&old_intern, old_dp, gedp->ged_wdbp->dbip, bn_mat_identity, &rt_uniresource) < 0) {
130  bu_vls_printf(gedp->ged_result_str, "rt_db_get_internal() error\n");
131  return GED_ERROR;
132  }
133 
134  if (old_intern.idb_type != ID_NMG) {
135  bu_vls_printf(gedp->ged_result_str, " is not an NMG solid!!\n");
136  rt_db_free_internal(&old_intern);
137  return GED_ERROR;
138  }
139 
140  m = (struct model *)old_intern.idb_ptr;
141  NMG_CK_MODEL(m);
142 
143  /* how many characters of the solid names do we reserve for digits? */
144  nmg_count_shell_kids(m, &tf, &tw, &tp);
145 
146  maxdigits = (int)(log10((double)(tf+tw+tp)) + 1.0);
147 
148  bu_vls_printf(gedp->ged_result_str, "%zu = %d digits\n", tf+tw+tp, maxdigits);
149 
150  /* for (maxdigits=1, i = tf + tw + tp; i > 0; i /= 10)
151  * maxdigits++;
152  */
153 
154  /* get the prefix for the solids to be created. */
155  memset(prefix, 0, sizeof(prefix));
156  bu_strlcpy(prefix, argv[argc-1], sizeof(prefix));
157  bu_strlcat(prefix, "_", sizeof(prefix));
158 
159  /* Bust it up here */
160 
161  i = 1;
162  for (BU_LIST_FOR(r, nmgregion, &m->r_hd)) {
163  NMG_CK_REGION(r);
164  for (BU_LIST_FOR(s, shell, &r->s_hd)) {
165  NMG_CK_SHELL(s);
166  if (s->vu_p) {
167  NMG_CK_VERTEXUSE(s->vu_p);
168  NMG_CK_VERTEX(s->vu_p->v_p);
169  v = s->vu_p->v_p;
170 
171  new_model = nmg_mm();
172  nmg_mrsv(new_model);
173  new_s = BU_LIST_FIRST(shell, &r->s_hd);
174  v_new = new_s->vu_p->v_p;
175  if (v->vg_p) {
176  nmg_vertex_gv(v_new, v->vg_p->coord);
177  }
178 
179  snprintf(newname, 32, "%s%0*d", prefix, maxdigits, i++);
180 
181  fracture_add_nmg_part(gedp, newname, new_model);
182  if (frac_stat) return GED_ERROR;
183  continue;
184  }
185  for (BU_LIST_FOR(fu, faceuse, &s->fu_hd)) {
186  if (fu->orientation != OT_SAME)
187  continue;
188 
189  NMG_CK_FACEUSE(fu);
190 
191  new_model = nmg_mm();
192  NMG_CK_MODEL(new_model);
193  new_r = nmg_mrsv(new_model);
194  NMG_CK_REGION(new_r);
195  new_s = BU_LIST_FIRST(shell, &new_r->s_hd);
196 
197  NMG_CK_SHELL(new_s);
198  nmg_dup_face(fu, new_s);
199 
200  snprintf(newname, 32, "%s%0*d", prefix, maxdigits, i++);
201  fracture_add_nmg_part(gedp, newname, new_model);
202  if (frac_stat) return GED_ERROR;
203  }
204  }
205  }
206 
207  return GED_OK;
208 }
209 
210 
211 /*
212  * Local Variables:
213  * tab-width: 8
214  * mode: C
215  * indent-tabs-mode: t
216  * c-file-style: "stroustrup"
217  * End:
218  * ex: shiftwidth=4 tabstop=8
219  */
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
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
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_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
void nmg_vertex_gv(struct vertex *v, const fastf_t *pt)
Definition: nmg_mk.c:1668
int ged_fracture(struct ged *gedp, int argc, const char *argv[])
Definition: fracture.c:87
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
struct faceuse * nmg_dup_face(struct faceuse *fu, struct shell *s)
Definition: nmg_mod.c:1827
#define GED_ERROR
Definition: ged.h:61
void * memset(void *s, int c, size_t n)
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
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
#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
struct bu_vls * ged_result_str
Definition: ged.h:357
struct nmgregion * nmg_mrsv(struct model *m)
Definition: nmg_mk.c:306
struct model * nmg_mm(void)
Definition: nmg_mk.c:235
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
#define GED_HELP
Definition: ged.h:62
NMG_CK_SHELL(s)
void nmg_count_shell_kids(const struct model *m, size_t *total_faces, size_t *total_wires, size_t *total_points)
Definition: nmg_misc.c:2141
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_LIST_FIRST(structure, hp)
Definition: list.h:312
#define bu_strlcat(dst, src, size)
Definition: str.h:50
void nmg_region_a(struct nmgregion *r, const struct bn_tol *tol)
Definition: nmg_mk.c:2557