BRL-CAD
lt.c
Go to the documentation of this file.
1 /* L T . 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/lt.c
21  *
22  * The lt command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 static int
36 list_children(struct ged *gedp, struct directory *dp)
37 {
38  size_t i;
39  struct rt_db_internal intern;
40  struct rt_comb_internal *comb;
41 
42  if (!(dp->d_flags & RT_DIR_COMB))
43  return GED_OK;
44 
45  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
46  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting");
47  return GED_ERROR;
48  }
49  comb = (struct rt_comb_internal *)intern.idb_ptr;
50 
51  if (comb->tree) {
52  struct bu_vls vls = BU_VLS_INIT_ZERO;
53  size_t node_count;
54  size_t actual_count;
56 
57  if (db_ck_v4gift_tree(comb->tree) < 0) {
59  if (db_ck_v4gift_tree(comb->tree) < 0) {
60  bu_vls_printf(gedp->ged_result_str, "Cannot flatten tree for listing");
61  return GED_ERROR;
62  }
63  }
64  node_count = db_tree_nleaves(comb->tree);
65  if (node_count > 0) {
66  rt_tree_array = (struct rt_tree_array *)bu_calloc(node_count,
67  sizeof(struct rt_tree_array), "tree list");
68  actual_count = (struct rt_tree_array *)db_flatten_tree(
69  rt_tree_array, comb->tree, OP_UNION,
70  1, &rt_uniresource) - rt_tree_array;
71  BU_ASSERT_SIZE_T(actual_count, ==, node_count);
72  comb->tree = TREE_NULL;
73  } else {
74  actual_count = 0;
75  rt_tree_array = NULL;
76  }
77 
78  for (i = 0; i < actual_count; i++) {
79  char op;
80 
81  switch (rt_tree_array[i].tl_op) {
82  case OP_UNION:
83  op = DB_OP_UNION;
84  break;
85  case OP_INTERSECT:
86  op = DB_OP_INTERSECT;
87  break;
88  case OP_SUBTRACT:
89  op = DB_OP_SUBTRACT;
90  break;
91  default:
92  op = '?';
93  break;
94  }
95 
96  bu_vls_printf(gedp->ged_result_str, "{%c %s} ", op, rt_tree_array[i].tl_tree->tr_l.tl_name);
97  db_free_tree(rt_tree_array[i].tl_tree, &rt_uniresource);
98  }
99  bu_vls_free(&vls);
100 
101  if (rt_tree_array)
102  bu_free((char *)rt_tree_array, "printnode: rt_tree_array");
103  }
104  rt_db_free_internal(&intern);
105 
106  return GED_OK;
107 }
108 
109 
110 int
111 ged_lt(struct ged *gedp, int argc, const char *argv[])
112 {
113  struct directory *dp;
114  static const char *usage = "object";
115 
117  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
118 
119  /* initialize result */
120  bu_vls_trunc(gedp->ged_result_str, 0);
121 
122  /* must be wanting help */
123  if (argc == 1) {
124  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
125  return GED_HELP;
126  }
127 
128  if (argc != 2) {
129  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
130  return GED_ERROR;
131  }
132 
133  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL) {
134  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
135  return GED_ERROR;
136  }
137 
138  return list_children(gedp, dp);
139 }
140 
141 
142 /*
143  * Local Variables:
144  * tab-width: 8
145  * mode: C
146  * indent-tabs-mode: t
147  * c-file-style: "stroustrup"
148  * End:
149  * ex: shiftwidth=4 tabstop=8
150  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
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
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.
#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
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#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 TREE_NULL
Definition: raytrace.h:1181
void db_free_tree(union tree *tp, struct resource *resp)
Definition: db_tree.c:1296
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_lt(struct ged *gedp, int argc, const char *argv[])
Definition: lt.c:111
struct tree::tree_db_leaf tr_l
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
Definition: op.h:35
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 LOOKUP_NOISY
Definition: raytrace.h:892
#define GED_HELP
Definition: ged.h:62
#define BU_ASSERT_SIZE_T(_lhs, _relation, _rhs)
Definition: defines.h:253
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
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
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216