BRL-CAD
joint2.c
Go to the documentation of this file.
1 /* J O I N T 2 . C
2  * BRL-CAD
3  *
4  * Copyright (c) 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/joint2.c
21  *
22  * The joint2 command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 
32 #include "raytrace.h"
33 #include "rtgeom.h"
34 #include "wdb.h"
35 
36 #include "./ged_private.h"
37 
38 static struct _ged_funtab joint_subcommand_table[] = {
39  {"joint ", "", "Joint command table", NULL, 0, 0, FALSE},
40  {"?", "[commands]", "summary of available joint commands", NULL, 0, _GED_FUNTAB_UNLIMITED, FALSE},
41  {"accept", "[joints]", "accept a series of moves", NULL, 1, _GED_FUNTAB_UNLIMITED, FALSE},
42  {"debug", "[hex code]", "Show/set debugging bit vector for joints", NULL, 1, 2, FALSE},
43  {"help", "[commands]", "give usage message for given joint commands", NULL, 0, _GED_FUNTAB_UNLIMITED, FALSE},
44  {"holds", "[names]", "list constraints", NULL, 1, _GED_FUNTAB_UNLIMITED, FALSE},
45  {"list", "[names]", "list joints.", NULL, 1, _GED_FUNTAB_UNLIMITED, FALSE},
46  {"load", "file_name", "load a joint/constraint file", NULL, 2, _GED_FUNTAB_UNLIMITED, FALSE},
47  {"mesh", "", "Build the grip mesh", NULL, 0, 1, FALSE},
48  {"move", "joint_name p1 [p2...p6]", "Manual adjust a joint", NULL, 3, 8, FALSE},
49  {"reject", "[joint_names]", "reject joint motions", NULL, 1, _GED_FUNTAB_UNLIMITED, FALSE},
50  {"save", "file_name", "Save joints and constraints to disk", NULL, 2, 2, FALSE},
51  {"solve", "constraint", "Solve a or all constraints", NULL, 1, _GED_FUNTAB_UNLIMITED, FALSE},
52  {"unload", "", "Unload any joint/constraints that have been loaded", NULL, 1, 1, FALSE},
53  {NULL, NULL, NULL, NULL, 0, 0, FALSE}
54 };
55 
56 int
58  struct ged *gedp,
59  struct rt_db_internal *ip,
60  int argc,
61  const char *argv[])
62 {
63  int i;
64  struct rt_selection_set *selection_set;
65  struct bu_ptbl *selections;
66  struct rt_selection *new_selection;
67  struct rt_selection_query query;
68  const char *cmd, *solid_name, *selection_name;
69 
70  /* 0 1 2 3
71  * joint2 <solid_name> selection subcommand
72  */
73  if (argc < 4) {
74  bu_vls_printf(gedp->ged_result_str, "not enough args for selection replace\n");
75  return GED_ERROR;
76  }
77 
78  solid_name = argv[1];
79  cmd = argv[3];
80 
81  if (BU_STR_EQUAL(cmd, "replace")) {
82  /* append to named selection - selection is created if it doesn't exist */
83  void (*free_selection)(struct rt_selection *);
84 
85  /* 4 5 6 7 8 9 10
86  * selection_name startx starty startz dirx diry dirz
87  */
88  if (argc != 11) {
89  bu_vls_printf(gedp->ged_result_str, "wrong args for selection replace\n");
90  return GED_ERROR;
91  }
92  selection_name = argv[4];
93 
94  /* find matching selections */
95  query.start[X] = atof(argv[5]);
96  query.start[Y] = atof(argv[6]);
97  query.start[Z] = atof(argv[7]);
98  query.dir[X] = atof(argv[8]);
99  query.dir[Y] = atof(argv[9]);
100  query.dir[Z] = atof(argv[10]);
102 
103  selection_set = ip->idb_meth->ft_find_selections(ip, &query);
104  if (!selection_set) {
105  bu_vls_printf(gedp->ged_result_str, "no matching selections");
106  return GED_OK;
107  }
108 
109  /* could be multiple options, just grabbing the first and
110  * freeing the rest
111  */
112  selections = &selection_set->selections;
113  new_selection = (struct rt_selection *)BU_PTBL_GET(selections, 0);
114 
115  free_selection = selection_set->free_selection;
116  for (i = BU_PTBL_LEN(selections) - 1; i > 0; --i) {
117  long *s = BU_PTBL_GET(selections, i);
118  free_selection((struct rt_selection *)s);
119  bu_ptbl_rm(selections, s);
120  }
121  bu_ptbl_free(selections);
122  BU_FREE(selection_set, struct rt_selection_set);
123 
124  /* get existing/new selections set in gedp */
125  selection_set = ged_get_selection_set(gedp, solid_name, selection_name);
126  selection_set->free_selection = free_selection;
127  selections = &selection_set->selections;
128 
129  for (i = BU_PTBL_LEN(selections) - 1; i >= 0; --i) {
130  long *s = BU_PTBL_GET(selections, i);
131  free_selection((struct rt_selection *)s);
132  bu_ptbl_rm(selections, s);
133  }
134  bu_ptbl_ins(selections, (long *)new_selection);
135  } else if (BU_STR_EQUAL(cmd, "translate")) {
136  struct rt_selection_operation operation;
137 
138  /* 4 5 6 7
139  * selection_name dx dy dz
140  */
141  if (argc != 8) {
142  return GED_ERROR;
143  }
144  selection_name = argv[4];
145 
146  selection_set = ged_get_selection_set(gedp, solid_name, selection_name);
147  selections = &selection_set->selections;
148 
149  if (BU_PTBL_LEN(selections) < 1) {
150  return GED_OK;
151  }
152 
153  for (i = 0; i < (int)BU_PTBL_LEN(selections); ++i) {
154  int ret;
155  operation.type = RT_SELECTION_TRANSLATION;
156  operation.parameters.tran.dx = atof(argv[5]);
157  operation.parameters.tran.dy = atof(argv[6]);
158  operation.parameters.tran.dz = atof(argv[7]);
159 
160  ret = ip->idb_meth->ft_process_selection(ip, gedp->ged_wdbp->dbip,
161  (struct rt_selection *)BU_PTBL_GET(selections, i), &operation);
162 
163  if (ret != 0) {
164  return GED_ERROR;
165  }
166  }
167  }
168 
169  return 0;
170 }
171 
172 int
173 ged_joint2(struct ged *gedp, int argc, const char *argv[])
174 {
175  struct directory *ndp;
176  struct rt_db_internal intern;
177  /*struct rt_joint_internal *ji; */
178 
181  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
182 
183  /* initialize result */
184  bu_vls_trunc(gedp->ged_result_str, 0);
185 
186  /* must be wanting help */
187  if (argc < 2) {
188  struct _ged_funtab *ftp;
189  bu_vls_printf(gedp->ged_result_str, "joint <obj> [subcommand]\n");
190  bu_vls_printf(gedp->ged_result_str, "The following subcommands are available:\n");
191  for (ftp = joint_subcommand_table+1; ftp->ft_name; ftp++) {
192  vls_col_item(gedp->ged_result_str, ftp->ft_name);
193  }
195  bu_vls_printf(gedp->ged_result_str,"\n");
196  return GED_HELP;
197  }
198 
199  if ((ndp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY)) == RT_DIR_NULL) {
200  bu_vls_printf(gedp->ged_result_str, "Error: %s is not a solid or does not exist in database", argv[1]);
201  return GED_ERROR;
202  }
203 
205 
206 
207  RT_CK_DB_INTERNAL(&intern);
208  /*
209  ji = (struct rt_joint_internal*)intern.idb_ptr;
210 
211  if (ji->magic != RT_JOINT_INTERNAL_MAGIC) {
212  bu_vls_printf(gedp->ged_result_str, "Error: %s is not a joint primitive.", ndp->d_namep);
213  return GED_ERROR;
214  }
215  */
216 
217  /* check for selection command */
218  if (BU_STR_EQUAL(argv[2], "selection")) {
219  int ret = joint_selection(gedp, &intern, argc, argv);
220  if (BU_STR_EQUAL(argv[3], "translate") && ret == 0) {
221  GED_DB_PUT_INTERNAL(gedp, ndp, &intern, &rt_uniresource, GED_ERROR);
222  }
223  rt_db_free_internal(&intern);
224  return ret;
225  }
226 
227  rt_db_free_internal(&intern);
228 
229  return GED_OK;
230 }
231 
232 
233 /*
234  * Local Variables:
235  * tab-width: 8
236  * mode: C
237  * indent-tabs-mode: t
238  * c-file-style: "stroustrup"
239  * End:
240  * ex: shiftwidth=4 tabstop=8
241  */
#define GED_OK
Definition: ged.h:55
#define FALSE
point_t start
start point of query ray
Definition: raytrace.h:1990
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
struct db_i * dbip
Definition: raytrace.h:1266
if lu s
Definition: nmg_mod.c:3860
int(* ft_process_selection)(struct rt_db_internal *, struct db_i *, const struct rt_selection *, const struct rt_selection_operation *)
Definition: raytrace.h:2231
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
int bu_ptbl_rm(struct bu_ptbl *b, const long *p)
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_ptbl_ins(struct bu_ptbl *b, long *p)
struct rt_selection_set *(* ft_find_selections)(const struct rt_db_internal *, const struct rt_selection_query *)
Definition: raytrace.h:2217
int ged_joint2(struct ged *gedp, int argc, const char *argv[])
Definition: joint2.c:173
#define GED_ERROR
Definition: ged.h:61
struct bu_ptbl selections
holds struct rt_selection
Definition: raytrace.h:1959
Definition: ptbl.h:62
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
vect_t dir
direction of query ray
Definition: raytrace.h:1991
Definition: color.c:49
void vls_col_eol(struct bu_vls *str)
Definition: columns.c:73
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
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
void(* free_selection)(struct rt_selection *)
Definition: raytrace.h:1964
#define BU_PTBL_GET(ptbl, i)
Definition: ptbl.h:108
const struct rt_functab * idb_meth
for ft_ifree(), etc.
Definition: raytrace.h:194
#define RT_SORT_CLOSEST_TO_START
Definition: raytrace.h:1994
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
#define _GED_FUNTAB_UNLIMITED
Definition: ged_private.h:72
struct bu_vls * ged_result_str
Definition: ged.h:357
#define BU_FREE(_ptr, _type)
Definition: malloc.h:229
#define RT_SELECTION_TRANSLATION
Definition: raytrace.h:2018
#define BU_PTBL_LEN(ptbl)
Definition: ptbl.h:107
void bu_ptbl_free(struct bu_ptbl *b)
Definition: ptbl.c:226
struct rt_selection_translation tran
Definition: raytrace.h:2021
int joint_selection(struct ged *gedp, struct rt_db_internal *ip, int argc, const char *argv[])
Definition: joint2.c:57
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 vls_col_item(struct bu_vls *str, const char *cp)
Definition: columns.c:45
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
char * ft_name
Definition: ged_private.h:78
Definition: color.c:51
union rt_selection_operation::@10 parameters
struct rt_selection_set * ged_get_selection_set(struct ged *gedp, const char *object_name, const char *selection_name)
Definition: select.c:298
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
Definition: color.c:50
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126