BRL-CAD
bot_decimate.c
Go to the documentation of this file.
1 /* B O T _ D E C I M A T 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/bot_decimate.c
21  *
22  * The bot_decimate command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "bu/getopt.h"
33 #include "rtgeom.h"
34 #include "ged.h"
35 
36 
37 int
38 ged_bot_decimate(struct ged *gedp, int argc, const char *argv[])
39 {
40  int c;
41  struct rt_db_internal intern;
42  struct rt_bot_internal *bot;
43  struct directory *dp;
44  fastf_t max_chord_error=-1.0;
45  fastf_t max_normal_error=-1.0;
46  fastf_t min_edge_length=-1.0;
47  static const char *usage = "-c maximum_chord_error -n maximum_normal_error -e minimum_edge_length new_bot_name current_bot_name";
48 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /* initialize result */
54  bu_vls_trunc(gedp->ged_result_str, 0);
55 
56  /* must be wanting help */
57  if (argc == 1) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_HELP;
60  }
61 
62  if (argc < 5 || argc > 9) {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
64  return GED_ERROR;
65  }
66 
67  /* process args */
68  bu_optind = 1;
69  bu_opterr = 0;
70  while ((c=bu_getopt(argc, (char * const *)argv, "c:n:e:")) != -1) {
71  switch (c) {
72  case 'c':
73  max_chord_error = atof(bu_optarg);
74  if (max_chord_error < 0.0) {
76  "Maximum chord error cannot be less than zero");
77  return GED_ERROR;
78  }
79  break;
80  case 'n':
81  max_normal_error = atof(bu_optarg);
82  if (max_normal_error < 0.0) {
84  "Maximum normal error cannot be less than zero");
85  return GED_ERROR;
86  }
87  break;
88  case 'e':
89  min_edge_length = atof(bu_optarg);
90  if (min_edge_length < 0.0) {
92  "minimum edge length cannot be less than zero");
93  return GED_ERROR;
94  }
95  break;
96  default: {
97  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
98  return GED_ERROR;
99  }
100  }
101  }
102 
103  argc -= bu_optind;
104  argv += bu_optind;
105 
106  /* make sure new solid does not already exist */
107  GED_CHECK_EXISTS(gedp, argv[0], LOOKUP_QUIET, GED_ERROR);
108 
109  /* make sure current solid does exist */
110  GED_DB_LOOKUP(gedp, dp, argv[1], LOOKUP_QUIET, GED_ERROR);
111 
112  /* import the current solid */
113  RT_DB_INTERNAL_INIT(&intern);
114  GED_DB_GET_INTERNAL(gedp, &intern, dp, NULL, gedp->ged_wdbp->wdb_resp, GED_ERROR);
115 
116  /* make sure this is a BOT solid */
117  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
118  bu_vls_printf(gedp->ged_result_str, "%s is not a BOT solid\n", argv[1]);
119  rt_db_free_internal(&intern);
120  return GED_ERROR;
121  }
122 
123  bot = (struct rt_bot_internal *)intern.idb_ptr;
124 
125  RT_BOT_CK_MAGIC(bot);
126 
127  /* convert maximum error and edge length to mm */
128  if (max_chord_error > 0.0) {
129  max_chord_error = max_chord_error * gedp->ged_wdbp->dbip->dbi_local2base;
130  }
131  if (min_edge_length > 0.0) {
132  min_edge_length = min_edge_length * gedp->ged_wdbp->dbip->dbi_local2base;
133  }
134 
135  /* do the decimation */
136  if (rt_bot_decimate(bot, max_chord_error, max_normal_error, min_edge_length) < 0) {
137  bu_vls_printf(gedp->ged_result_str, "Decimation Error\n");
138  rt_db_free_internal(&intern);
139  return GED_ERROR;
140  }
141 
142  /* save the result to the database */
143  /* XXX - should this be rt_db_put_internal() instead? */
144  if (wdb_put_internal(gedp->ged_wdbp, argv[0], &intern, 1.0) < 0) {
146  "Failed to write decimated BOT back to database\n");
147  return GED_ERROR;
148  }
149 
150  return GED_OK;
151 }
152 
153 
154 /*
155  * Local Variables:
156  * tab-width: 8
157  * mode: C
158  * indent-tabs-mode: t
159  * c-file-style: "stroustrup"
160  * End:
161  * ex: shiftwidth=4 tabstop=8
162  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
#define GED_DB_LOOKUP(_gedp, _dp, _name, _noisy, _flags)
Definition: ged.h:223
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 rt_wdb * ged_wdbp
Definition: ged.h:340
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
#define GED_ERROR
Definition: ged.h:61
int wdb_put_internal(struct rt_wdb *wdbp, const char *name, struct rt_db_internal *ip, double local2mm)
Definition: wdb.c:218
int idb_major_type
Definition: raytrace.h:192
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define GED_CHECK_EXISTS(_gedp, _name, _noisy, _flags)
Definition: ged.h:171
struct bu_vls * ged_result_str
Definition: ged.h:357
void * idb_ptr
Definition: raytrace.h:195
int ged_bot_decimate(struct ged *gedp, int argc, const char *argv[])
Definition: bot_decimate.c:38
int bu_opterr
Definition: globals.c:88
int rt_bot_decimate(struct rt_bot_internal *bot, fastf_t max_chord_error, fastf_t max_normal_error, fastf_t min_edge_length)
Definition: bot.c:4267
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
struct resource * wdb_resp
Definition: raytrace.h:1270
double dbi_local2base
local2mm
Definition: raytrace.h:807
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
double fastf_t
Definition: defines.h:300
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216