BRL-CAD
nmg_collapse.c
Go to the documentation of this file.
1 /* N M G _ C O L L A P 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/nmg_collapse.c
21  *
22  * The nmg_collapse command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "bu/cmd.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_nmg_collapse(struct ged *gedp, int argc, const char *argv[])
38 {
39  char *new_name;
40  struct model *m;
41  struct rt_db_internal intern;
42  struct directory *dp;
43  struct bu_ptbl faces;
44  struct face *fp;
45  size_t count;
46  fastf_t tol_coll;
47  fastf_t min_angle;
48  static const char *usage = "nmg_prim new_prim max_err_dist [min_angle]";
49 
52  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
53 
54  /* initialize result */
55  bu_vls_trunc(gedp->ged_result_str, 0);
56 
57  /* must be wanting help */
58  if (argc == 1) {
59  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
60  return GED_HELP;
61  }
62 
63  if (argc < 4) {
64  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
65  return GED_ERROR;
66  }
67 
68  if (strchr(argv[2], '/')) {
69  bu_vls_printf(gedp->ged_result_str, "Do not use '/' in solid names: %s\n", argv[2]);
70  return GED_ERROR;
71  }
72 
73  new_name = (char *)argv[2];
74 
75  if (db_lookup(gedp->ged_wdbp->dbip, new_name, LOOKUP_QUIET) != RT_DIR_NULL) {
76  bu_vls_printf(gedp->ged_result_str, "%s already exists\n", new_name);
77  return GED_ERROR;
78  }
79 
80  if ((dp=db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL)
81  return GED_ERROR;
82 
83  if (dp->d_flags & RT_DIR_COMB) {
84  bu_vls_printf(gedp->ged_result_str, "%s is a combination, only NMG primitives are allowed here\n", argv[1]);
85  return GED_ERROR;
86  }
87 
88  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (matp_t)NULL, &rt_uniresource) < 0) {
89  bu_vls_printf(gedp->ged_result_str, "Failed to get internal form of %s!!!!\n", argv[1]);
90  return GED_ERROR;
91  }
92 
93  if (intern.idb_type != ID_NMG) {
94  bu_vls_printf(gedp->ged_result_str, "%s is not an NMG solid!!!!\n", argv[1]);
95  rt_db_free_internal(&intern);
96  return GED_ERROR;
97  }
98 
99  tol_coll = atof(argv[3]) * gedp->ged_wdbp->dbip->dbi_local2base;
100  if (tol_coll <= 0.0) {
101  bu_vls_printf(gedp->ged_result_str, "tolerance distance too small\n");
102  return GED_ERROR;
103  }
104 
105  if (argc == 5) {
106  min_angle = atof(argv[4]);
107  if (min_angle < 0.0) {
108  bu_vls_printf(gedp->ged_result_str, "Minimum angle cannot be less than zero\n");
109  return GED_ERROR;
110  }
111  } else
112  min_angle = 0.0;
113 
114  m = (struct model *)intern.idb_ptr;
115  NMG_CK_MODEL(m);
116 
117  /* check that all faces are planar */
118  nmg_face_tabulate(&faces, &m->magic);
119  for (BU_PTBL_FOR(fp, (struct face *), &faces)) {
120  if (fp->g.magic_p != NULL && *(fp->g.magic_p) != NMG_FACE_G_PLANE_MAGIC) {
121  bu_log("\tnot planar\n");
122  bu_ptbl_free(&faces);
124  "nmg_collapse can only be applied to NMG primitives with planar faces\n");
125  return GED_ERROR;
126  }
127  }
128  bu_ptbl_free(&faces);
129 
130  /* triangulate model */
132 
133  count = (size_t)nmg_edge_collapse(m, &gedp->ged_wdbp->wdb_tol, tol_coll, min_angle);
134 
135  dp=db_diradd(gedp->ged_wdbp->dbip, new_name, RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&intern.idb_type);
136  if (dp == RT_DIR_NULL) {
137  bu_vls_printf(gedp->ged_result_str, "Cannot add %s to directory\n", new_name);
138  rt_db_free_internal(&intern);
139  return GED_ERROR;
140  }
141 
142  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource) < 0) {
143  rt_db_free_internal(&intern);
144  bu_vls_printf(gedp->ged_result_str, "Database write error, aborting.\n");
145  return GED_ERROR;
146  }
147 
148  rt_db_free_internal(&intern);
149 
150  bu_vls_printf(gedp->ged_result_str, "%zu edges collapsed\n", count);
151 
152  return GED_OK;
153 }
154 
155 
156 /*
157  * Local Variables:
158  * tab-width: 8
159  * mode: C
160  * indent-tabs-mode: t
161  * c-file-style: "stroustrup"
162  * End:
163  * ex: shiftwidth=4 tabstop=8
164  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
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
#define BU_PTBL_FOR(ip, cast, ptbl)
Definition: ptbl.h:125
struct db_i * dbip
Definition: raytrace.h:1266
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
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int nmg_edge_collapse(struct model *m, const struct bn_tol *tol, const fastf_t tol_coll, const fastf_t min_angle)
Definition: nmg_misc.c:10201
#define GED_ERROR
Definition: ged.h:61
Definition: ptbl.h:62
char * strchr(const char *sp, int c)
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
int ged_nmg_collapse(struct ged *gedp, int argc, const char *argv[])
Definition: nmg_collapse.c:37
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define ID_NMG
n-Manifold Geometry solid
Definition: raytrace.h:469
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
void nmg_triangulate_model(struct model *m, const struct bn_tol *tol)
Definition: nmg_tri.c:3818
struct bu_vls * ged_result_str
Definition: ged.h:357
#define NMG_FACE_G_PLANE_MAGIC
Definition: magic.h:125
void bu_ptbl_free(struct bu_ptbl *b)
Definition: ptbl.c:226
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
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
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
double dbi_local2base
local2mm
Definition: raytrace.h:807
void nmg_face_tabulate(struct bu_ptbl *tab, const uint32_t *magic_p)
Definition: nmg_info.c:2247
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216