BRL-CAD
get_comb.c
Go to the documentation of this file.
1 /* G E T _ C O M B . 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/get_comb.c
21  *
22  * The skel 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 int
36 ged_get_comb(struct ged *gedp, int argc, const char *argv[])
37 {
38  struct directory *dp;
39  struct rt_db_internal intern;
40  struct rt_comb_internal *comb;
42  size_t i;
43  size_t node_count;
44  size_t actual_count;
45  static const char *usage = "comb";
46 
49  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
50 
51  /* initialize result */
52  bu_vls_trunc(gedp->ged_result_str, 0);
53 
54  /* must be wanting help */
55  if (argc == 1) {
56  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
57  return GED_HELP;
58  }
59 
60  if (argc != 2) {
61  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
62  return GED_ERROR;
63  }
64 
65 
66  dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET);
67 
68  if (dp != RT_DIR_NULL) {
69  if (!(dp->d_flags & RT_DIR_COMB)) {
70  bu_vls_printf(gedp->ged_result_str, "%s is not a combination, so cannot be edited this way\n", argv[1]);
71  return GED_ERROR;
72  }
73 
74  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
75  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting\n");
76  return GED_ERROR;
77  }
78 
79  comb = (struct rt_comb_internal *)intern.idb_ptr;
80 
81  if (comb->tree && db_ck_v4gift_tree(comb->tree) < 0) {
83  if (db_ck_v4gift_tree(comb->tree) < 0) {
84  bu_vls_printf(gedp->ged_result_str, "Cannot flatten tree for editing\n");
85  return GED_ERROR;
86  }
87  }
88 
89  node_count = db_tree_nleaves(comb->tree);
90  if (node_count > 0) {
91  rt_tree_array = (struct rt_tree_array *)bu_calloc(node_count,
92  sizeof(struct rt_tree_array),
93  "tree list");
94  actual_count = (struct rt_tree_array *)db_flatten_tree(rt_tree_array,
95  comb->tree,
96  OP_UNION,
97  1,
98  &rt_uniresource) - rt_tree_array;
99  BU_ASSERT_SIZE_T(actual_count, ==, node_count);
100  comb->tree = TREE_NULL;
101  } else {
102  rt_tree_array = (struct rt_tree_array *)NULL;
103  actual_count = 0;
104  }
105 
106  bu_vls_printf(gedp->ged_result_str, "%s", dp->d_namep);
107  if (comb->region_flag) {
108  bu_vls_printf(gedp->ged_result_str, " Yes %ld %ld %ld %ld",
109  comb->region_id, comb->aircode, comb->GIFTmater, comb->los);
110  } else {
111  bu_vls_printf(gedp->ged_result_str, " No");
112  }
113 
114  if (comb->rgb_valid) {
115  bu_vls_printf(gedp->ged_result_str, " {%d %d %d}", V3ARGS(comb->rgb));
116  } else
117  bu_vls_printf(gedp->ged_result_str, " {}");
118 
119  bu_vls_printf(gedp->ged_result_str, " {%s}", bu_vls_addr(&comb->shader));
120 
121  if (comb->inherit)
122  bu_vls_printf(gedp->ged_result_str, " Yes");
123  else
124  bu_vls_printf(gedp->ged_result_str, " No");
125 
126 
127  bu_vls_printf(gedp->ged_result_str, " {");
128  for (i = 0; i < actual_count; i++) {
129  char op;
130 
131  switch (rt_tree_array[i].tl_op) {
132  case OP_UNION:
133  op = DB_OP_UNION;
134  break;
135  case OP_INTERSECT:
136  op = DB_OP_INTERSECT;
137  break;
138  case OP_SUBTRACT:
139  op = DB_OP_SUBTRACT;
140  break;
141  default:
142  bu_vls_printf(gedp->ged_result_str, "\nIllegal op code in tree\n");
143  return GED_ERROR;
144  }
145 
146  bu_vls_printf(gedp->ged_result_str, " %c %s\t", op, rt_tree_array[i].tl_tree->tr_l.tl_name);
147  _ged_vls_print_matrix(gedp->ged_result_str, rt_tree_array[i].tl_tree->tr_l.tl_mat);
148  bu_vls_printf(gedp->ged_result_str, "\n");
149  db_free_tree(rt_tree_array[i].tl_tree, &rt_uniresource);
150  }
151 
152  bu_vls_printf(gedp->ged_result_str, "}");
153  } else {
154  bu_vls_printf(gedp->ged_result_str, "%s Yes %d %d %d %d {} {} No {}",
155  argv[1],
156  gedp->ged_wdbp->wdb_item_default,
157  gedp->ged_wdbp->wdb_air_default,
158  gedp->ged_wdbp->wdb_mat_default,
159  gedp->ged_wdbp->wdb_los_default);
160  }
161 
162  return GED_OK;
163 }
164 
165 
166 void
167 _ged_vls_print_matrix(struct bu_vls *vls, matp_t matrix)
168 {
169  int k;
170  char buf[64];
171  fastf_t tmp;
172 
173  if (!matrix)
174  return;
175 
176  if (bn_mat_is_identity(matrix))
177  return;
178 
179  for (k = 0; k < 16; k++) {
180  sprintf(buf, "%g", matrix[k]);
181  tmp = atof(buf);
182  if (ZERO(tmp - matrix[k]))
183  bu_vls_printf(vls, " %g", matrix[k]);
184  else
185  bu_vls_printf(vls, " %.12e", matrix[k]);
186  if ((k&3)==3) bu_vls_printf(vls, " ");
187  }
188 }
189 
190 
191 /*
192  * Local Variables:
193  * tab-width: 8
194  * mode: C
195  * indent-tabs-mode: t
196  * c-file-style: "stroustrup"
197  * End:
198  * ex: shiftwidth=4 tabstop=8
199  */
int wdb_item_default
GIFT region ID.
Definition: raytrace.h:1278
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
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 bn_mat_is_identity(const mat_t m)
Definition: mat.c:980
char region_flag
!0 ==> this COMB is a REGION
Definition: raytrace.h:939
int wdb_mat_default
GIFT material code.
Definition: raytrace.h:1280
size_t db_tree_nleaves(const union tree *tp)
Definition: db_comb.c:99
Definition: ged.h:338
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
union tree * tl_tree
Definition: raytrace.h:1247
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
unsigned char rgb[3]
Definition: raytrace.h:948
#define GED_ERROR
Definition: ged.h:61
struct rt_tree_array * db_flatten_tree(struct rt_tree_array *rt_tree_array, union tree *tp, int op, int avail, struct resource *resp)
Definition: db_comb.c:138
if(share_geom)
Definition: nmg_mod.c:3829
#define OP_SUBTRACT
Binary: L subtract R.
Definition: raytrace.h:1129
void db_non_union_push(union tree *tp, struct resource *resp)
Definition: db_tree.c:1426
#define OP_INTERSECT
Binary: L intersect R.
Definition: raytrace.h:1128
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
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define V3ARGS(a)
Definition: color.c:56
#define TREE_NULL
Definition: raytrace.h:1181
void db_free_tree(union tree *tp, struct resource *resp)
Definition: db_tree.c:1296
matp_t tl_mat
xform matp, NULL ==> identity
Definition: raytrace.h:1173
struct bu_vls shader
Definition: raytrace.h:950
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
int wdb_los_default
Line-of-sight estimate.
Definition: raytrace.h:1281
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
struct tree::tree_db_leaf tr_l
#define ZERO(val)
Definition: units.c:38
void * idb_ptr
Definition: raytrace.h:195
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
Definition: op.h:35
void _ged_vls_print_matrix(struct bu_vls *vls, matp_t matrix)
Definition: get_comb.c:167
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
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
#define BU_ASSERT_SIZE_T(_lhs, _relation, _rhs)
Definition: defines.h:253
char rgb_valid
!0 ==> rgb[] has valid color
Definition: raytrace.h:947
int wdb_air_default
Definition: raytrace.h:1279
int ged_get_comb(struct ged *gedp, int argc, const char *argv[])
Definition: get_comb.c:36
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
Definition: vls.h:56
double fastf_t
Definition: defines.h:300
#define OP_UNION
Binary: L union R.
Definition: raytrace.h:1127
int db_ck_v4gift_tree(const union tree *tp)
Definition: db_comb.c:927