BRL-CAD
qray.c
Go to the documentation of this file.
1 /* Q R A Y . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1998-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 /** @addtogroup libged */
21 /** @{ */
22 /** @file libged/qray.c
23  *
24  * Routines to set and get "Query Ray" variables.
25  *
26  */
27 
28 #include "common.h"
29 
30 #include <string.h>
31 
32 
33 #include "vmath.h"
34 #include "ged.h"
35 
36 #include "./qray.h"
37 
38 
39 static void qray_print_fmts(struct ged *gedp);
40 static void qray_print_vars(struct ged *gedp);
41 static int qray_get_fmt_index(struct ged *gedp, char c);
42 
43 static struct ged_qray_color def_qray_odd_color = { 0, 255, 255 };
44 static struct ged_qray_color def_qray_even_color = { 255, 255, 0 };
45 static struct ged_qray_color def_qray_void_color = { 255, 0, 255 };
46 static struct ged_qray_color def_qray_overlap_color = { 255, 255, 255 };
47 
48 static struct qray_fmt_data def_qray_fmt_data[] = {
49  {'r', "\"Origin (x y z) = (%.2f %.2f %.2f) (h v d) = (%.2f %.2f %.2f)\\nDirection (x y z) = (%.4f %.4f %.4f) (az el) = (%.2f %.2f)\\n\" x_orig y_orig z_orig h v d_orig x_dir y_dir z_dir a e"},
50  {'h', "\" Region Name Entry (x y z) LOS Obliq_in\\n\""},
51  {'p', "\"%-20s (%9.3f %9.3f %9.3f) %8.2f %8.3f\\n\" reg_name x_in y_in z_in los obliq_in"},
52  {'f', "\"\""},
53  {'m', "\"You missed the target\\n\""},
54  {'o', "\"OVERLAP: '%s' and '%s' xyz_in=(%g %g %g) los=%g\\n\" ov_reg1_name ov_reg2_name ov_x_in ov_y_in ov_z_in ov_los"},
55  {'g', "\"\""},
56  {(char)0, (char *)NULL}
57 };
58 
59 
60 static void
61 usage(struct ged *gedp, const char *argv0)
62 {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s\n", argv0);
64  bu_vls_printf(gedp->ged_result_str, " qray vars print a list of all variables (i.e. var = val)\n");
65  bu_vls_printf(gedp->ged_result_str, " qray basename [str] set or get basename for query ray primitives\n");
66  bu_vls_printf(gedp->ged_result_str, " qray effects [t|g|b] set or get effects (i.e. text, graphical or both)\n");
67  bu_vls_printf(gedp->ged_result_str, " qray echo [0|1] set or get command echo\n");
68  bu_vls_printf(gedp->ged_result_str, " qray oddcolor [r g b] set or get color of odd partitions\n");
69  bu_vls_printf(gedp->ged_result_str, " qray evencolor [r g b] set or get color of even partitions\n");
70  bu_vls_printf(gedp->ged_result_str, " qray voidcolor [r g b] set or get color of void areas\n");
71  bu_vls_printf(gedp->ged_result_str, " qray overlapcolor [r g b] set or get color of overlap areas\n");
72  bu_vls_printf(gedp->ged_result_str, " qray fmt [r|h|p|f|m|o|g [str]] set or get format string(s)\n");
73  bu_vls_printf(gedp->ged_result_str, " qray script [str] set or get the nirt script string\n");
74  bu_vls_printf(gedp->ged_result_str, " qray [help] print this help message\n");
75 }
76 
77 
78 static void
79 qray_print_fmts(struct ged *gedp)
80 {
81  int i;
82 
83  for (i = 0; gedp->ged_gdp->gd_qray_fmts[i].type != (char)0; ++i)
84  bu_vls_printf(gedp->ged_result_str, "%s\n", bu_vls_addr(&gedp->ged_gdp->gd_qray_fmts[i].fmt));
85 }
86 
87 
88 static void
89 qray_print_vars(struct ged *gedp)
90 {
91  bu_vls_printf(gedp->ged_result_str, "basename = %s\n", bu_vls_addr(&gedp->ged_gdp->gd_qray_basename));
92  bu_vls_printf(gedp->ged_result_str, "script = %s\n", bu_vls_addr(&gedp->ged_gdp->gd_qray_script));
93  bu_vls_printf(gedp->ged_result_str, "effects = %c\n", gedp->ged_gdp->gd_qray_effects);
94  bu_vls_printf(gedp->ged_result_str, "echo = %d\n", gedp->ged_gdp->gd_qray_cmd_echo);
95  bu_vls_printf(gedp->ged_result_str, "oddcolor = %d %d %d\n",
97  bu_vls_printf(gedp->ged_result_str, "evencolor = %d %d %d\n",
99  bu_vls_printf(gedp->ged_result_str, "voidcolor = %d %d %d\n",
101  bu_vls_printf(gedp->ged_result_str, "overlapcolor = %d %d %d\n",
103 
104  qray_print_fmts(gedp);
105 }
106 
107 
108 static int
109 qray_get_fmt_index(struct ged *gedp,
110  char c)
111 {
112  int i;
113 
114  for (i = 0; gedp->ged_gdp->gd_qray_fmts[i].type != (char)0; ++i)
115  if (c == gedp->ged_gdp->gd_qray_fmts[i].type)
116  return i;
117 
118  return -1;
119 }
120 
121 
122 int
123 ged_qray(struct ged *gedp,
124  int argc,
125  const char *argv[])
126 {
128  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
129 
130  /* initialize result */
131  bu_vls_trunc(gedp->ged_result_str, 0);
132 
133  /* must be wanting help */
134  if (argc == 1) {
135  usage(gedp, argv[0]);
136  return GED_HELP;
137  }
138 
139  if (argc > 6) {
140  usage(gedp, argv[0]);
141  return GED_ERROR;
142  }
143 
144  if (BU_STR_EQUAL(argv[1], "fmt")) {
145  int i;
146 
147  if (argc == 2) {
148  /* get all format strings */
149  qray_print_fmts(gedp);
150  return GED_OK;
151  } else if (argc == 3) {
152  /* get particular format string */
153  if ((i = qray_get_fmt_index(gedp, *argv[2])) < 0) {
154  bu_vls_printf(gedp->ged_result_str, "qray: unrecognized format type: '%s'\n", argv[2]);
155  usage(gedp, argv[0]);
156 
157  return GED_ERROR;
158  }
159 
161  return GED_OK;
162  } else if (argc == 4) {
163  /* set value */
164  if ((i = qray_get_fmt_index(gedp, *argv[2])) < 0) {
165  bu_vls_printf(gedp->ged_result_str, "qray: unrecognized format type: '%s'\n", argv[2]);
166  usage(gedp, argv[0]);
167 
168  return GED_ERROR;
169  }
170 
171  bu_vls_trunc(&gedp->ged_gdp->gd_qray_fmts[i].fmt, 0);
172  bu_vls_printf(&gedp->ged_gdp->gd_qray_fmts[i].fmt, "%s", argv[3]);
173  return GED_OK;
174  }
175 
176  bu_vls_printf(gedp->ged_result_str, "The 'qray fmt' command accepts 0, 1 or 2 arguments\n");
177  return GED_ERROR;
178  }
179 
180  if (BU_STR_EQUAL(argv[1], "basename")) {
181  if (argc == 2) {
182  /* get value */
184 
185  return GED_OK;
186  } else if (argc == 3) {
187  /* set value */
188  bu_vls_strcpy(&gedp->ged_gdp->gd_qray_basename, argv[2]);
189  return GED_OK;
190  }
191 
192  bu_vls_printf(gedp->ged_result_str, "The 'qray basename' command accepts 0 or 1 argument\n");
193  return GED_ERROR;
194  }
195 
196  if (BU_STR_EQUAL(argv[1], "script")) {
197  if (argc == 2) {
198  /* get value */
200 
201  return GED_OK;
202  } else if (argc == 3) {
203  /* set value */
204  bu_vls_strcpy(&gedp->ged_gdp->gd_qray_script, argv[2]);
205  return GED_OK;
206  }
207 
208  bu_vls_printf(gedp->ged_result_str, "The 'qray scripts' command accepts 0 or 1 argument\n");
209  return GED_ERROR;
210  }
211 
212  if (BU_STR_EQUAL(argv[1], "effects")) {
213  if (argc == 2) {
214  /* get value */
216 
217  return TCL_OK;
218  } else if (argc == 3) {
219  /* set value */
220  if (*argv[2] != 't' && *argv[2] != 'g' && *argv[2] != 'b') {
221  bu_vls_printf(gedp->ged_result_str, "qray effects: bad value - %s", argv[2]);
222 
223  return GED_ERROR;
224  }
225 
226  gedp->ged_gdp->gd_qray_effects = *argv[2];
227 
228  return GED_OK;
229  }
230 
231  bu_vls_printf(gedp->ged_result_str, "The 'qray effects' command accepts 0 or 1 argument\n");
232  return GED_ERROR;
233  }
234 
235  if (BU_STR_EQUAL(argv[1], "echo")) {
236  if (argc == 2) {
237  /* get value */
238  if (gedp->ged_gdp->gd_qray_cmd_echo)
239  bu_vls_printf(gedp->ged_result_str, "1");
240  else
241  bu_vls_printf(gedp->ged_result_str, "0");
242 
243  return GED_OK;
244  } else if (argc == 3) {
245  /* set value */
246  int ival;
247 
248  if (sscanf(argv[2], "%d", &ival) < 1) {
249  bu_vls_printf(gedp->ged_result_str, "qray echo: bad value - %s", argv[2]);
250 
251  return GED_ERROR;
252  }
253 
254  if (ival)
255  gedp->ged_gdp->gd_qray_cmd_echo = 1;
256  else
257  gedp->ged_gdp->gd_qray_cmd_echo = 0;
258 
259  return GED_OK;
260  }
261 
262  bu_vls_printf(gedp->ged_result_str, "The 'qray echo' command accepts 0 or 1 argument\n");
263  return GED_ERROR;
264  }
265 
266  if (BU_STR_EQUAL(argv[1], "oddcolor")) {
267  if (argc == 2) {
268  /* get value */
269  bu_vls_printf(gedp->ged_result_str, "%d %d %d",
270  gedp->ged_gdp->gd_qray_odd_color.r,
271  gedp->ged_gdp->gd_qray_odd_color.g,
272  gedp->ged_gdp->gd_qray_odd_color.b);
273 
274  return GED_OK;
275  } else if (argc == 5) {
276  /* set value */
277  int r, g, b;
278 
279  if (sscanf(argv[2], "%d", &r) != 1 ||
280  sscanf(argv[3], "%d", &g) != 1 ||
281  sscanf(argv[4], "%d", &b) != 1 ||
282  r < 0 || g < 0 || b < 0 ||
283  255 < r || 255 < g || 255 < b) {
284  bu_vls_printf(gedp->ged_result_str, "qray oddcolor %s %s %s - bad value",
285  argv[2], argv[3], argv[4]);
286 
287  return GED_ERROR;
288  }
289 
290  gedp->ged_gdp->gd_qray_odd_color.r = r;
291  gedp->ged_gdp->gd_qray_odd_color.g = g;
292  gedp->ged_gdp->gd_qray_odd_color.b = b;
293 
294  return GED_OK;
295  }
296 
297  bu_vls_printf(gedp->ged_result_str, "The 'qray oddcolor' command accepts 0 or 3 arguments\n");
298  return GED_ERROR;
299  }
300 
301  if (BU_STR_EQUAL(argv[1], "evencolor")) {
302  if (argc == 2) {
303  /* get value */
304  bu_vls_printf(gedp->ged_result_str, "%d %d %d",
307  gedp->ged_gdp->gd_qray_even_color.b);
308 
309  return GED_OK;
310  } else if (argc == 5) {
311  /* set value */
312  int r, g, b;
313 
314  if (sscanf(argv[2], "%d", &r) != 1 ||
315  sscanf(argv[3], "%d", &g) != 1 ||
316  sscanf(argv[4], "%d", &b) != 1 ||
317  r < 0 || g < 0 || b < 0 ||
318  255 < r || 255 < g || 255 < b) {
319  bu_vls_printf(gedp->ged_result_str, "qray evencolor %s %s %s - bad value",
320  argv[2], argv[3], argv[4]);
321 
322  return GED_ERROR;
323  }
324 
325  gedp->ged_gdp->gd_qray_even_color.r = r;
326  gedp->ged_gdp->gd_qray_even_color.g = g;
327  gedp->ged_gdp->gd_qray_even_color.b = b;
328 
329  return GED_OK;
330  }
331 
332  bu_vls_printf(gedp->ged_result_str, "The 'qray evencolor' command accepts 0 or 3 arguments\n");
333  return GED_ERROR;
334  }
335 
336  if (BU_STR_EQUAL(argv[1], "voidcolor")) {
337  if (argc == 2) {
338  /* get value */
339  bu_vls_printf(gedp->ged_result_str, "%d %d %d",
342  gedp->ged_gdp->gd_qray_void_color.b);
343 
344  return GED_OK;
345  } else if (argc == 5) {
346  /* set value */
347  int r, g, b;
348 
349  if (sscanf(argv[2], "%d", &r) != 1 ||
350  sscanf(argv[3], "%d", &g) != 1 ||
351  sscanf(argv[4], "%d", &b) != 1 ||
352  r < 0 || g < 0 || b < 0 ||
353  255 < r || 255 < g || 255 < b) {
354  bu_vls_printf(gedp->ged_result_str, "qray voidcolor %s %s %s - bad value",
355  argv[2], argv[3], argv[4]);
356 
357  return GED_ERROR;
358  }
359 
360  gedp->ged_gdp->gd_qray_void_color.r = r;
361  gedp->ged_gdp->gd_qray_void_color.g = g;
362  gedp->ged_gdp->gd_qray_void_color.b = b;
363 
364  return GED_OK;
365  }
366 
367  bu_vls_printf(gedp->ged_result_str, "The 'qray voidcolor' command accepts 0 or 3 arguments\n");
368  return GED_ERROR;
369  }
370 
371  if (BU_STR_EQUAL(argv[1], "overlapcolor")) {
372  if (argc == 2) {
373  /* get value */
374  bu_vls_printf(gedp->ged_result_str, "%d %d %d",
378 
379  return GED_OK;
380  } else if (argc == 5) {
381  /* set value */
382  int r, g, b;
383 
384  if (sscanf(argv[2], "%d", &r) != 1 ||
385  sscanf(argv[3], "%d", &g) != 1 ||
386  sscanf(argv[4], "%d", &b) != 1 ||
387  r < 0 || g < 0 || b < 0 ||
388  255 < r || 255 < g || 255 < b) {
390  "qray overlapcolor %s %s %s - bad value",
391  argv[2], argv[3], argv[4]);
392  return GED_ERROR;
393  }
394 
395  gedp->ged_gdp->gd_qray_overlap_color.r = r;
396  gedp->ged_gdp->gd_qray_overlap_color.g = g;
397  gedp->ged_gdp->gd_qray_overlap_color.b = b;
398 
399  return GED_OK;
400  }
401 
402  bu_vls_printf(gedp->ged_result_str, "The 'qray overlapcolor' command accepts 0 or 3 arguments\n");
403  return GED_ERROR;
404  }
405 
406  if (BU_STR_EQUAL(argv[1], "vars")) {
407  qray_print_vars(gedp);
408  return GED_OK;
409  }
410 
411  if (BU_STR_EQUAL(argv[1], "help")) {
412  usage(gedp, argv[0]);
413  return GED_HELP;
414  }
415 
416  bu_vls_printf(gedp->ged_result_str, "qray: unrecognized command: '%s'\n", argv[1]);
417  usage(gedp, argv[0]);
418 
419  return GED_ERROR;
420 }
421 
422 
423 void
425 {
426  int i;
427  int n = 0;
428  struct qray_fmt_data *qfdp;
429 
433 
434  gdp->gd_qray_effects = 'b';
435  gdp->gd_qray_cmd_echo = 0;
436  gdp->gd_qray_odd_color = def_qray_odd_color;
437  gdp->gd_qray_even_color = def_qray_even_color;
438  gdp->gd_qray_void_color = def_qray_void_color;
439  gdp->gd_qray_overlap_color = def_qray_overlap_color;
440 
441  /* count the number of default format types */
442  for (qfdp = def_qray_fmt_data; qfdp->fmt != (char *)NULL; ++qfdp)
443  ++n;
444 
445  gdp->gd_qray_fmts = (struct ged_qray_fmt *)bu_calloc(n+1, sizeof(struct ged_qray_fmt), "qray_fmts");
446 
447  for (i = 0; i < n; ++i) {
448  gdp->gd_qray_fmts[i].type = def_qray_fmt_data[i].type;
449  bu_vls_init(&gdp->gd_qray_fmts[i].fmt);
450  bu_vls_strcpy(&gdp->gd_qray_fmts[i].fmt, def_qray_fmt_data[i].fmt);
451  }
452 
453  gdp->gd_qray_fmts[i].type = (char)0;
454 }
455 
456 
457 void
459 {
460  int i;
461 
464  for (i = 0; gdp->gd_qray_fmts[i].type != (char)0; ++i)
465  bu_vls_free(&gdp->gd_qray_fmts[i].fmt);
466  bu_free(gdp->gd_qray_fmts, "dgo_free_qray");
467 }
468 
469 
470 void
471 qray_data_to_vlist(struct ged *gedp,
472  struct bn_vlblock *vbp,
473  struct qray_dataList *headp,
474  vect_t dir,
475  int do_overlaps)
476 {
477  int i = 1; /* start out odd */
478  struct bu_list *vhead;
479  struct qray_dataList *ndlp;
480  vect_t in_pt, out_pt;
481  vect_t last_out_pt = { 0, 0, 0 };
482 
483  for (BU_LIST_FOR(ndlp, qray_dataList, &headp->l)) {
484  if (do_overlaps)
485  vhead = rt_vlblock_find(vbp,
489  else if (i % 2)
490  vhead = rt_vlblock_find(vbp,
491  gedp->ged_gdp->gd_qray_odd_color.r,
492  gedp->ged_gdp->gd_qray_odd_color.g,
493  gedp->ged_gdp->gd_qray_odd_color.b);
494  else
495  vhead = rt_vlblock_find(vbp,
498  gedp->ged_gdp->gd_qray_even_color.b);
499 
500  VSET(in_pt, ndlp->x_in, ndlp->y_in, ndlp->z_in);
501  VJOIN1(out_pt, in_pt, ndlp->los, dir);
502  VSCALE(in_pt, in_pt, gedp->ged_wdbp->dbip->dbi_local2base);
503  VSCALE(out_pt, out_pt, gedp->ged_wdbp->dbip->dbi_local2base);
504  RT_ADD_VLIST(vhead, in_pt, BN_VLIST_LINE_MOVE);
505  RT_ADD_VLIST(vhead, out_pt, BN_VLIST_LINE_DRAW);
506 
507  if (!do_overlaps && i > 1 && !VNEAR_EQUAL(last_out_pt, in_pt, SQRT_SMALL_FASTF)) {
508  vhead = rt_vlblock_find(vbp,
511  gedp->ged_gdp->gd_qray_void_color.b);
512  RT_ADD_VLIST(vhead, last_out_pt, BN_VLIST_LINE_MOVE);
513  RT_ADD_VLIST(vhead, in_pt, BN_VLIST_LINE_DRAW);
514  }
515 
516  VMOVE(last_out_pt, out_pt);
517  ++i;
518  }
519 }
520 
521 
522 /** @} */
523 /*
524  * Local Variables:
525  * mode: C
526  * tab-width: 8
527  * indent-tabs-mode: t
528  * c-file-style: "stroustrup"
529  * End:
530  * ex: shiftwidth=4 tabstop=8
531  */
void usage(struct ged *gedp)
Definition: coil.c:315
void bu_vls_init(struct bu_vls *vp)
Definition: vls.c:56
#define GED_OK
Definition: ged.h:55
#define BU_LIST_FOR(p, structure, hp)
Definition: list.h:365
struct bu_vls fmt
Definition: ged.h:288
Definition: list.h:118
struct ged_qray_color gd_qray_odd_color
Definition: ged.h:326
char * fmt
Definition: qray.h:47
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
#define VSET(a, b, c, d)
Definition: color.c:53
fastf_t x_in
Definition: qray.h:53
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 rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
unsigned char r
Definition: ged.h:280
struct ged_qray_fmt * gd_qray_fmts
Definition: ged.h:325
#define GED_ERROR
Definition: ged.h:61
int ged_qray(struct ged *gedp, int argc, const char *argv[])
Definition: qray.c:123
void qray_init(struct ged_drawable *gdp)
Definition: qray.c:424
struct bu_vls gd_qray_script
query ray script
Definition: ged.h:322
void qray_data_to_vlist(struct ged *gedp, struct bn_vlblock *vbp, struct qray_dataList *headp, vect_t dir, int do_overlaps)
Definition: qray.c:471
struct bu_list l
Definition: qray.h:52
struct bu_list * rt_vlblock_find(struct bn_vlblock *vbp, int r, int g, int b)
Definition: vlist.c:98
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
struct bu_vls gd_qray_basename
basename of query ray vlist
Definition: ged.h:321
unsigned char g
Definition: ged.h:281
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define RT_ADD_VLIST(hd, pnt, draw)
Definition: raytrace.h:1865
char type
Definition: ged.h:287
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
char gd_qray_effects
t for text, g for graphics or b for both
Definition: ged.h:323
#define BN_VLIST_LINE_MOVE
Definition: vlist.h:82
fastf_t z_in
Definition: qray.h:55
#define SQRT_SMALL_FASTF
Definition: defines.h:346
#define BN_VLIST_LINE_DRAW
Definition: vlist.h:83
#define DG_QRAY_BASENAME
Definition: qray.h:37
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
char type
Definition: qray.h:46
struct ged_drawable * ged_gdp
Definition: ged.h:360
void qray_free(struct ged_drawable *gdp)
Definition: qray.c:458
int gd_qray_cmd_echo
0 - don't echo command, 1 - echo command
Definition: ged.h:324
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
fastf_t y_in
Definition: qray.h:54
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
unsigned char b
Definition: ged.h:282
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
struct ged_qray_color gd_qray_even_color
Definition: ged.h:327
double dbi_local2base
local2mm
Definition: raytrace.h:807
struct ged_qray_color gd_qray_void_color
Definition: ged.h:328
struct ged_qray_color gd_qray_overlap_color
Definition: ged.h:329
fastf_t los
Definition: qray.h:56
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126