BRL-CAD
select.c
Go to the documentation of this file.
1 /* S E L E C 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/select.c
21  *
22  * The select command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 
31 #include "bu/getopt.h"
32 #include "./ged_private.h"
33 
34 
35 /*
36  * Returns a list of items within the specified rectangle or circle.
37  * If bot is specified, the bot points within the specified area are returned.
38  *
39  * Usage:
40  * select [-b bot] [-p] [-z vminz] vx vy {vr | vw vh}
41  *
42  */
43 int
44 ged_select(struct ged *gedp, int argc, const char *argv[])
45 {
46  int c;
47  double vx, vy, vw, vh, vr;
48  static const char *usage = "[-b bot] [-p] [-z vminz] vx vy {vr | vw vh}";
49  const char *cmd = argv[0];
50  struct rt_db_internal intern;
51  struct rt_bot_internal *botip = NULL;
52  int pflag = 0;
53  double vminz = -1000.0;
54 
58  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
59 
60  /* initialize result */
61  bu_vls_trunc(gedp->ged_result_str, 0);
62 
63  /* Get command line options. */
64  bu_optind = 1;
65  while ((c = bu_getopt(argc, (char * const *)argv, "b:pz:")) != -1) {
66  switch (c) {
67  case 'b':
68  {
69  mat_t mat;
70 
71  /* skip subsequent bot specifications */
72  if (botip != (struct rt_bot_internal *)NULL)
73  break;
74 
75  if (wdb_import_from_path2(gedp->ged_result_str, &intern, bu_optarg, gedp->ged_wdbp, mat) == GED_ERROR) {
76  bu_vls_printf(gedp->ged_result_str, "%s: failed to find %s", cmd, bu_optarg);
77  return GED_ERROR;
78  }
79 
80  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
81  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
82  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a BOT", cmd, bu_optarg);
83  rt_db_free_internal(&intern);
84 
85  return GED_ERROR;
86  }
87 
88  botip = (struct rt_bot_internal *)intern.idb_ptr;
89  }
90 
91  break;
92  case 'p':
93  pflag = 1;
94  break;
95  case 'z':
96  if (sscanf(bu_optarg, "%lf", &vminz) != 1) {
97  if (botip != (struct rt_bot_internal *)NULL)
98  rt_db_free_internal(&intern);
99 
100  bu_vls_printf(gedp->ged_result_str, "%s: bad vminz - %s", cmd, bu_optarg);
101  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
102  }
103 
104  break;
105  default:
106  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
107  return GED_ERROR;
108  }
109  }
110 
111  argc -= (bu_optind - 1);
112  argv += (bu_optind - 1);
113 
114  if (argc < 4 || 5 < argc) {
115  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
116  return GED_ERROR;
117  }
118 
119  if (argc == 4) {
120  if (sscanf(argv[1], "%lf", &vx) != 1 ||
121  sscanf(argv[2], "%lf", &vy) != 1 ||
122  sscanf(argv[3], "%lf", &vr) != 1) {
123  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
124  return GED_ERROR;
125  }
126 
127  if (botip != (struct rt_bot_internal *)NULL) {
128  int ret;
129 
130  ret = _ged_select_botpts(gedp, botip, vx, vy, vr, vr, vminz, 1);
131  rt_db_free_internal(&intern);
132 
133  return ret;
134  } else {
135  if (pflag)
136  return dl_select_partial(gedp->ged_gdp->gd_headDisplay, gedp->ged_gvp->gv_model2view, gedp->ged_result_str, vx, vy, vr, vr, 1);
137  else
138  return dl_select(gedp->ged_gdp->gd_headDisplay, gedp->ged_gvp->gv_model2view, gedp->ged_result_str, vx, vy, vr, vr, 1);
139  }
140  } else {
141  if (sscanf(argv[1], "%lf", &vx) != 1 ||
142  sscanf(argv[2], "%lf", &vy) != 1 ||
143  sscanf(argv[3], "%lf", &vw) != 1 ||
144  sscanf(argv[4], "%lf", &vh) != 1) {
145  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
146  return GED_ERROR;
147  }
148 
149  if (botip != (struct rt_bot_internal *)NULL) {
150  int ret;
151 
152  ret = _ged_select_botpts(gedp, botip, vx, vy, vw, vh, vminz, 0);
153  rt_db_free_internal(&intern);
154 
155  return ret;
156  } else {
157  if (pflag)
158  return dl_select_partial(gedp->ged_gdp->gd_headDisplay, gedp->ged_gvp->gv_model2view, gedp->ged_result_str, vx, vy, vw, vh, 0);
159  else
160  return dl_select(gedp->ged_gdp->gd_headDisplay, gedp->ged_gvp->gv_model2view, gedp->ged_result_str, vx, vy, vw, vh, 0);
161  }
162  }
163 }
164 
165 
166 /*
167  * Returns a list of items within the previously defined rectangle.
168  *
169  * Usage:
170  * rselect [-b bot] [-p] [-z vminz]
171  *
172  */
173 int
174 ged_rselect(struct ged *gedp, int argc, const char *argv[])
175 {
176  int c;
177  static const char *usage = "[-b bot] [-p] [-z vminz]";
178  const char *cmd = argv[0];
179  struct rt_db_internal intern;
180  struct rt_bot_internal *botip = (struct rt_bot_internal *)NULL;
181  int pflag = 0;
182  double vminz = -1000.0;
183 
186  GED_CHECK_VIEW(gedp, GED_ERROR);
187  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
188 
189  /* initialize result */
190  bu_vls_trunc(gedp->ged_result_str, 0);
191 
192  /* Get command line options. */
193  bu_optind = 1;
194  while ((c = bu_getopt(argc, (char * const *)argv, "b:pz:")) != -1) {
195  switch (c) {
196  case 'b':
197  {
198  mat_t mat;
199 
200  /* skip subsequent bot specifications */
201  if (botip != (struct rt_bot_internal *)NULL)
202  break;
203 
204  if (wdb_import_from_path2(gedp->ged_result_str, &intern, bu_optarg, gedp->ged_wdbp, mat) == GED_ERROR) {
205  bu_vls_printf(gedp->ged_result_str, "%s: failed to find %s", cmd, bu_optarg);
206  return GED_ERROR;
207  }
208 
209  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
210  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
211  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a BOT", cmd, bu_optarg);
212  rt_db_free_internal(&intern);
213 
214  return GED_ERROR;
215  }
216 
217  botip = (struct rt_bot_internal *)intern.idb_ptr;
218  }
219 
220  break;
221  case 'p':
222  pflag = 1;
223  break;
224  case 'z':
225  if (sscanf(bu_optarg, "%lf", &vminz) != 1) {
226  if (botip != (struct rt_bot_internal *)NULL)
227  rt_db_free_internal(&intern);
228 
229  bu_vls_printf(gedp->ged_result_str, "%s: bad vminz - %s", cmd, bu_optarg);
230  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
231  }
232 
233  break;
234  default:
235  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
236  return GED_ERROR;
237  }
238  }
239 
240  argc -= (bu_optind - 1);
241  argv += (bu_optind - 1);
242 
243  if (argc != 1) {
244  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
245  return GED_ERROR;
246  }
247 
248  if (botip != (struct rt_bot_internal *)NULL) {
249  int ret;
250 
251  ret = _ged_select_botpts(gedp, botip,
252  gedp->ged_gvp->gv_rect.x,
253  gedp->ged_gvp->gv_rect.y,
254  gedp->ged_gvp->gv_rect.width,
255  gedp->ged_gvp->gv_rect.height,
256  vminz,
257  0);
258 
259  rt_db_free_internal(&intern);
260  return ret;
261  } else {
262  if (pflag)
263 
265  gedp->ged_gvp->gv_rect.y,
266  gedp->ged_gvp->gv_rect.width,
267  gedp->ged_gvp->gv_rect.height,
268  0);
269  else
271  gedp->ged_gvp->gv_rect.y,
272  gedp->ged_gvp->gv_rect.width,
273  gedp->ged_gvp->gv_rect.height,
274  0);
275  }
276 }
277 
278 struct rt_object_selections *
279 ged_get_object_selections(struct ged *gedp, const char *object_name)
280 {
281  int int_new;
282  struct bu_hash_entry *entry;
283 
284  entry = bu_hash_tbl_add(gedp->ged_selections, (unsigned char *)object_name,
285  strlen(object_name), &int_new);
286 
287  if (int_new) {
288  struct rt_object_selections *obj_selections;
289  BU_ALLOC(obj_selections, struct rt_object_selections);
290  obj_selections->sets = bu_hash_tbl_create(0);
291  bu_set_hash_value(entry, (unsigned char *)obj_selections);
292  }
293 
294  return (struct rt_object_selections *)bu_get_hash_value(entry);
295 }
296 
297 struct rt_selection_set *
298 ged_get_selection_set(struct ged *gedp, const char *object_name, const char *selection_name)
299 {
300  struct rt_object_selections *obj_selections;
301  struct bu_hash_entry *entry;
302  int int_new;
303 
304  obj_selections = ged_get_object_selections(gedp, object_name);
305  entry = bu_hash_tbl_add(obj_selections->sets,
306  (const unsigned char *)selection_name, strlen(selection_name), &int_new);
307 
308  if (int_new) {
309  struct rt_selection_set *set;
310  BU_ALLOC(set, struct rt_selection_set);
311  BU_PTBL_INIT(&set->selections);
312  bu_set_hash_value(entry, (unsigned char *)set);
313  }
314 
315  return (struct rt_selection_set *)bu_get_hash_value(entry);
316 }
317 
318 /*
319  * Local Variables:
320  * tab-width: 8
321  * mode: C
322  * indent-tabs-mode: t
323  * c-file-style: "stroustrup"
324  * End:
325  * ex: shiftwidth=4 tabstop=8
326  */
void usage(struct ged *gedp)
Definition: coil.c:315
unsigned char * bu_get_hash_value(const struct bu_hash_entry *hsh_entry)
Definition: hash.c:170
int ged_rselect(struct ged *gedp, int argc, const char *argv[])
Definition: select.c:174
Definition: ged.h:338
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
void bu_set_hash_value(struct bu_hash_entry *hsh_entry, unsigned char *value)
Definition: hash.c:160
struct rt_wdb * ged_wdbp
Definition: ged.h:340
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
Definition: hash.h:44
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#define GED_ERROR
Definition: ged.h:61
struct bview_interactive_rect_state gv_rect
Definition: bview.h:254
struct bu_ptbl selections
holds struct rt_selection
Definition: raytrace.h:1959
struct bview * ged_gvp
Definition: ged.h:361
int idb_major_type
Definition: raytrace.h:192
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
int ged_select(struct ged *gedp, int argc, const char *argv[])
Definition: select.c:44
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
int _ged_select_botpts(struct ged *gedp, struct rt_bot_internal *botip, double vx, double vy, double vwidth, double vheight, double vminz, int rflag)
Definition: edbot.c:688
struct bu_hash_tbl * ged_selections
object name -> struct rt_object_selections
Definition: ged.h:363
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
struct bu_hash_entry * bu_hash_tbl_add(struct bu_hash_tbl *hsh_tbl, const unsigned char *key, int key_len, int *new_entry)
Definition: hash.c:188
#define BU_PTBL_INIT(_p)
Definition: ptbl.h:80
int dl_select(struct bu_list *hdlp, mat_t model2view, struct bu_vls *vls, double vx, double vy, double vwidth, double vheight, int rflag)
struct rt_selection_set * ged_get_selection_set(struct ged *gedp, const char *object_name, const char *selection_name)
Definition: select.c:298
struct bu_vls * ged_result_str
Definition: ged.h:357
struct ged_drawable * ged_gdp
Definition: ged.h:360
void * idb_ptr
Definition: raytrace.h:195
mat_t gv_model2view
Definition: bview.h:222
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
struct rt_object_selections * ged_get_object_selections(struct ged *gedp, const char *object_name)
Definition: select.c:279
struct bu_hash_tbl * sets
Definition: raytrace.h:1978
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
int dl_select_partial(struct bu_list *hdlp, mat_t model2view, struct bu_vls *vls, double vx, double vy, double vwidth, double vheight, int rflag)
int wdb_import_from_path2(struct bu_vls *logstr, struct rt_db_internal *ip, const char *path, struct rt_wdb *wdb, matp_t matp)
Definition: wdb.c:364
struct bu_hash_tbl * bu_hash_tbl_create(unsigned long tbl_size)
Definition: hash.c:48
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216