BRL-CAD
showmats.c
Go to the documentation of this file.
1 /* S H O W M A T S . 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/showmats.c
21  *
22  * The showmats 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 struct showmats_data {
36  struct ged *smd_gedp;
37  int smd_count;
38  char *smd_child;
39  mat_t smd_mat;
40 };
41 
42 
43 static void
44 Do_showmats(struct db_i *dbip, struct rt_comb_internal *UNUSED(comb), union tree *comb_leaf, void *user_ptr1, void *user_ptr2, void *UNUSED(user_ptr3), void *UNUSED(user_ptr4))
45 {
46  struct showmats_data *smdp;
47  int aflag;
48  char obuf[1024];
49 
50  RT_CK_DBI(dbip);
51  RT_CK_TREE(comb_leaf);
52 
53  smdp = (struct showmats_data *)user_ptr1;
54  aflag = *((int *)user_ptr2);
55 
56  if (!BU_STR_EQUAL(comb_leaf->tr_l.tl_name, smdp->smd_child))
57  return;
58 
59  smdp->smd_count++;
60  if (!aflag) {
61  if (smdp->smd_count > 1) {
62  bu_vls_printf(smdp->smd_gedp->ged_result_str, "\n\tOccurrence #%d:\n", smdp->smd_count);
63  }
64 
65  bn_mat_print_guts("", comb_leaf->tr_l.tl_mat, obuf, 1024);
66  bu_vls_printf(smdp->smd_gedp->ged_result_str, "%s", obuf);
67  }
68 
69  if (smdp->smd_count == 1) {
70  mat_t tmp_mat;
71  if (comb_leaf->tr_l.tl_mat) {
72  bn_mat_mul(tmp_mat, smdp->smd_mat, comb_leaf->tr_l.tl_mat);
73  MAT_COPY(smdp->smd_mat, tmp_mat);
74  }
75  }
76 }
77 
78 
79 static int
80 Run_showmats(struct ged *gedp, const char *path, int aflag)
81 {
82  struct showmats_data sm_data;
83  char *parent;
84  struct directory *dp;
85  int max_count=1;
86 
87  sm_data.smd_gedp = gedp;
88  MAT_IDN(sm_data.smd_mat);
89 
90  parent = strtok((char *)path, "/");
91  while ((sm_data.smd_child = strtok((char *)NULL, "/")) != NULL) {
92  struct rt_db_internal intern;
93  struct rt_comb_internal *comb;
94 
95  if ((dp = db_lookup(gedp->ged_wdbp->dbip, parent, LOOKUP_NOISY)) == RT_DIR_NULL)
96  return TCL_ERROR;
97 
98  if (!aflag)
99  bu_vls_printf(gedp->ged_result_str, "%s\n", parent);
100 
101  if (!(dp->d_flags & RT_DIR_COMB)) {
102  if (!aflag)
103  bu_vls_printf(gedp->ged_result_str, "\tThis is not a combination\n");
104  break;
105  }
106 
107  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
108  bu_vls_printf(gedp->ged_result_str, "Database read error, aborting.\n");
109  return GED_ERROR;
110  }
111  comb = (struct rt_comb_internal *)intern.idb_ptr;
112 
113  sm_data.smd_count = 0;
114 
115  if (comb->tree)
116  db_tree_funcleaf(gedp->ged_wdbp->dbip, comb, comb->tree, Do_showmats,
117  (void *)&sm_data, (void *)&aflag, (void *)NULL, (void *)NULL);
118  rt_db_free_internal(&intern);
119 
120  if (!sm_data.smd_count) {
121  bu_vls_printf(gedp->ged_result_str, "%s is not a member of %s\n", sm_data.smd_child, parent);
122  return GED_ERROR;
123  }
124  if (sm_data.smd_count > max_count)
125  max_count = sm_data.smd_count;
126 
127  parent = sm_data.smd_child;
128  }
129 
130  if (!aflag) {
131  char obuf[1024];
132 
133  bu_vls_printf(gedp->ged_result_str, "%s\n", parent);
134 
135  if (max_count > 1)
136  bu_vls_printf(gedp->ged_result_str, "\nAccumulated matrix (using first occurrence of each object):\n");
137  else
138  bu_vls_printf(gedp->ged_result_str, "\nAccumulated matrix:\n");
139 
140  bn_mat_print_guts("", sm_data.smd_mat, obuf, 1024);
141  bu_vls_printf(gedp->ged_result_str, "%s", obuf);
142  } else {
143  int i;
144 
145  for (i = 0; i < 16; ++i)
146  bu_vls_printf(gedp->ged_result_str, " %lf", sm_data.smd_mat[i]);
147  }
148 
149  return GED_OK;
150 }
151 
152 
153 int
154 ged_showmats(struct ged *gedp, int argc, const char *argv[])
155 {
156  int aflag = 0;
157  static const char *usage = "path";
158 
160  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
161 
162  /* initialize result */
163  bu_vls_trunc(gedp->ged_result_str, 0);
164 
165  /* must be wanting help */
166  if (argc == 1) {
167  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
168  return GED_HELP;
169  }
170 
171  if (argc == 3 && argv[1][0] == '-' && argv[1][1] == 'a' && argv[1][2] == '\0') {
172  aflag = 1;
173  ++argv;
174  } else if (argc != 2) {
175  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
176  return GED_ERROR;
177  }
178 
179  return Run_showmats(gedp, argv[1], aflag);
180 }
181 
182 
183 /*
184  * Local Variables:
185  * tab-width: 8
186  * mode: C
187  * indent-tabs-mode: t
188  * c-file-style: "stroustrup"
189  * End:
190  * ex: shiftwidth=4 tabstop=8
191  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: raytrace.h:800
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
mat_t smd_mat
Definition: showmats.c:39
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
char * smd_child
Definition: showmats.c:38
struct ged * smd_gedp
Definition: showmats.c:36
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int smd_count
Definition: showmats.c:37
#define GED_ERROR
Definition: ged.h:61
if(share_geom)
Definition: nmg_mod.c:3829
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
char * strtok(char *s, const char *delim)
matp_t tl_mat
xform matp, NULL ==> identity
Definition: raytrace.h:1173
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
#define UNUSED(parameter)
Definition: common.h:239
int ged_showmats(struct ged *gedp, int argc, const char *argv[])
Definition: showmats.c:154
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
struct bu_vls * ged_result_str
Definition: ged.h:357
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
struct tree::tree_db_leaf tr_l
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
void bn_mat_print_guts(const char *title, const mat_t m, char *buf, int buflen)
Definition: mat.c:50
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
void db_tree_funcleaf(struct db_i *dbip, struct rt_comb_internal *comb, union tree *comb_tree, void(*leaf_func)(struct db_i *, struct rt_comb_internal *, union tree *, void *, void *, void *, void *), void *user_ptr1, void *user_ptr2, void *user_ptr3, void *user_ptr4)
Definition: db_tree.c:640
#define RT_CK_TREE(_p)
Definition: raytrace.h:1182
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
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126