BRL-CAD
bot_smooth.c
Go to the documentation of this file.
1 /* B O T _ S M O O T H . 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_smooth.c
21  *
22  * The bot_smooth command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "bu/cmd.h"
32 #include "rtgeom.h"
33 
34 #include "./ged_private.h"
35 
36 
37 int
38 ged_bot_smooth(struct ged *gedp, int argc, const char *argv[])
39 {
40  char *new_bot_name, *old_bot_name;
41  struct directory *dp_old, *dp_new;
42  struct rt_bot_internal *old_bot;
43  struct rt_db_internal intern;
44  fastf_t tolerance_angle=180.0;
45  int arg_index=1;
46  static const char *usage = "[-t ntol] new_bot old_bot";
47 
50  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
51 
52  dp_old = dp_new = RT_DIR_NULL;
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  /* check that we are using a version 5 database */
64  if (db_version(gedp->ged_wdbp->dbip) < 5) {
65  bu_vls_printf(gedp->ged_result_str, "This is an older database version.\nIt does not support BOT surface normals.\nUse \"dbupgrade\" to upgrade this database to the current version.\n");
66  return GED_ERROR;
67  }
68 
69  if (argc < 3) {
70  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
71  return GED_ERROR;
72  }
73 
74  while (*argv[arg_index] == '-') {
75  /* this is an option */
76  if (BU_STR_EQUAL(argv[arg_index], "-t")) {
77  arg_index++;
78  tolerance_angle = atof(argv[arg_index]);
79  } else {
80  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
81  return GED_ERROR;
82  }
83  arg_index++;
84  }
85 
86  if (arg_index >= argc) {
87  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
88  return GED_ERROR;
89  }
90 
91  new_bot_name = (char *)argv[arg_index++];
92  old_bot_name = (char *)argv[arg_index];
93 
94  GED_DB_LOOKUP(gedp, dp_old, old_bot_name, LOOKUP_QUIET, GED_ERROR);
95 
96  if (!BU_STR_EQUAL(old_bot_name, new_bot_name)) {
97  GED_CHECK_EXISTS(gedp, new_bot_name, LOOKUP_QUIET, GED_ERROR);
98  } else {
99  dp_new = dp_old;
100  }
101 
102  GED_DB_GET_INTERNAL(gedp, &intern, dp_old, NULL, gedp->ged_wdbp->wdb_resp, GED_ERROR);
103 
104  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
105  bu_vls_printf(gedp->ged_result_str, "%s is not a BOT primitive\n", old_bot_name);
106  rt_db_free_internal(&intern);
107  return GED_ERROR;
108  }
109 
110  old_bot = (struct rt_bot_internal *)intern.idb_ptr;
111  RT_BOT_CK_MAGIC(old_bot);
112 
113  if (rt_bot_smooth(old_bot, old_bot_name, gedp->ged_wdbp->dbip, tolerance_angle*DEG2RAD)) {
114  bu_vls_printf(gedp->ged_result_str, "Failed to smooth %s\n", old_bot_name);
115  rt_db_free_internal(&intern);
116  return GED_ERROR;
117  }
118 
119  if (dp_new == RT_DIR_NULL) {
120  GED_DB_DIRADD(gedp, dp_new, new_bot_name, RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&intern.idb_type, GED_ERROR);
121  }
122 
123  GED_DB_PUT_INTERNAL(gedp, dp_new, &intern, gedp->ged_wdbp->wdb_resp, GED_ERROR);
124  rt_db_free_internal(&intern);
125 
126  return GED_OK;
127 }
128 
129 
130 /*
131  * Local Variables:
132  * tab-width: 8
133  * mode: C
134  * indent-tabs-mode: t
135  * c-file-style: "stroustrup"
136  * End:
137  * ex: shiftwidth=4 tabstop=8
138  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_DB_DIRADD(_gedp, _dp, _name, _laddr, _len, _dirflags, _ptr, _flags)
Definition: ged.h:213
#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
int db_version(struct db_i *dbip)
Definition: db5_scan.c:414
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
int idb_major_type
Definition: raytrace.h:192
#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 LOOKUP_QUIET
Definition: raytrace.h:893
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
#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
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 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
int ged_bot_smooth(struct ged *gedp, int argc, const char *argv[])
Definition: bot_smooth.c:38
int rt_bot_smooth(struct rt_bot_internal *bot, const char *bot_name, struct db_i *dbip, fastf_t norm_tol_angle)
Definition: bot.c:4450
#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
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126