BRL-CAD
move_arb_edge.c
Go to the documentation of this file.
1 /* M O V E _ A R B _ E D G E . 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/move_arb_edge.c
21  *
22  * The move_arb_edge command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "rtgeom.h"
32 #include "rt/arb_edit.h"
33 #include "raytrace.h"
34 
35 #include "./ged_private.h"
36 
37 
38 int
39 ged_move_arb_edge(struct ged *gedp, int argc, const char *argv[])
40 {
41  struct rt_db_internal intern;
42  struct rt_arb_internal *arb;
43  plane_t planes[6]; /* ARBs defining plane equations */
44  int arb_type;
45  int arb_pt_index;
46  int edge;
47  int bad_edge_id = 0;
48  int rflag = 0;
49  point_t pt;
50  double scan[3];
51  mat_t mat;
52  char *last;
53  struct directory *dp;
54  static const char *usage = "[-r] arb edge pt";
55  const short arb8_evm[12][2] = arb8_edge_vertex_mapping;
56  const short arb7_evm[12][2] = arb7_edge_vertex_mapping;
57  const short arb6_evm[10][2] = arb6_edge_vertex_mapping;
58  const short arb5_evm[9][2] = arb5_edge_vertex_mapping;
59  const short arb4_evm[5][2] = arb4_edge_vertex_mapping;
60 
63  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
64 
65  /* initialize result */
66  bu_vls_trunc(gedp->ged_result_str, 0);
67 
68  /* must be wanting help */
69  if (argc == 1) {
70  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
71  return GED_HELP;
72  }
73 
74  if (argc < 4 || 5 < argc) {
75  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
76  return GED_ERROR;
77  }
78 
79  if (argc == 5) {
80  if (argv[1][0] != '-' || argv[1][1] != 'r' || argv[1][2] != '\0') {
81  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
82  return GED_ERROR;
83  }
84 
85  rflag = 1;
86  --argc;
87  ++argv;
88  }
89 
90  if ((last = strrchr(argv[1], '/')) == NULL)
91  last = (char *)argv[1];
92  else
93  ++last;
94 
95  if (last[0] == '\0') {
96  bu_vls_printf(gedp->ged_result_str, "illegal input - %s", argv[1]);
97  return GED_ERROR;
98  }
99 
100  if ((dp = db_lookup(gedp->ged_wdbp->dbip, last, LOOKUP_QUIET)) == RT_DIR_NULL) {
101  bu_vls_printf(gedp->ged_result_str, "%s not found", argv[1]);
102  return GED_ERROR;
103  }
104 
105  if (wdb_import_from_path2(gedp->ged_result_str, &intern, argv[1], gedp->ged_wdbp, mat) == GED_ERROR)
106  return GED_ERROR;
107 
108  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
109  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_ARB8) {
110  bu_vls_printf(gedp->ged_result_str, "Object not an ARB");
111  rt_db_free_internal(&intern);
112 
113  return GED_ERROR;
114  }
115 
116  if (sscanf(argv[2], "%d", &edge) != 1) {
117  bu_vls_printf(gedp->ged_result_str, "bad edge - %s", argv[2]);
118  rt_db_free_internal(&intern);
119 
120  return GED_ERROR;
121  }
122  edge -= 1;
123 
124  if (sscanf(argv[3], "%lf %lf %lf", &scan[X], &scan[Y], &scan[Z]) != 3) {
125  bu_vls_printf(gedp->ged_result_str, "bad point - %s", argv[3]);
126  rt_db_free_internal(&intern);
127 
128  return GED_ERROR;
129  }
130  /* convert from double to fastf_t */
131  VMOVE(pt, scan);
132 
133  arb = (struct rt_arb_internal *)intern.idb_ptr;
134  RT_ARB_CK_MAGIC(arb);
135 
136  arb_type = rt_arb_std_type(&intern, &gedp->ged_wdbp->wdb_tol);
137 
138  /* check the arb type */
139  switch (arb_type) {
140  case ARB4:
141  if (edge < 0 || 4 < edge) {
142  bad_edge_id = 1;
143  goto bad_edge;
144  }
145 
146  arb_pt_index = arb4_evm[edge][0];
147  break;
148  case ARB5:
149  if (edge < 0 || 8 < edge) {
150  bad_edge_id = 1;
151  goto bad_edge;
152  }
153 
154  arb_pt_index = arb5_evm[edge][0];
155  break;
156  case ARB6:
157  if (edge < 0 || 9 < edge) {
158  bad_edge_id = 1;
159  goto bad_edge;
160  }
161 
162  arb_pt_index = arb6_evm[edge][0];
163  break;
164  case ARB7:
165  if (edge < 0 || 11 < edge) {
166  bad_edge_id = 1;
167  goto bad_edge;
168  }
169 
170  arb_pt_index = arb7_evm[edge][0];
171  break;
172  case ARB8:
173  if (edge < 0 || 11 < edge) {
174  bad_edge_id = 1;
175  goto bad_edge;
176  }
177 
178  arb_pt_index = arb8_evm[edge][0];
179  break;
180  default:
181  bu_vls_printf(gedp->ged_result_str, "unrecognized arb type");
182  rt_db_free_internal(&intern);
183 
184  return GED_ERROR;
185  }
186 
187 bad_edge:
188 
189  /* check the edge id */
190  if (bad_edge_id) {
191  bu_vls_printf(gedp->ged_result_str, "bad edge - %s", argv[2]);
192  rt_db_free_internal(&intern);
193 
194  return GED_ERROR;
195  }
196 
197  if (rt_arb_calc_planes(gedp->ged_result_str, arb, arb_type, planes, &gedp->ged_wdbp->wdb_tol)) {
198  rt_db_free_internal(&intern);
199 
200  return GED_ERROR;
201  }
202 
203  VSCALE(pt, pt, gedp->ged_wdbp->dbip->dbi_local2base);
204 
205  if (rflag) {
206  VADD2(pt, pt, arb->pt[arb_pt_index]);
207  }
208 
209  if (rt_arb_edit(gedp->ged_result_str, arb, arb_type, edge, pt, planes, &gedp->ged_wdbp->wdb_tol)) {
210  rt_db_free_internal(&intern);
211 
212  return GED_ERROR;
213  }
214 
215  {
216  int i;
217  mat_t invmat;
218 
219  bn_mat_inv(invmat, mat);
220 
221  for (i = 0; i < 8; ++i) {
222  point_t arb_pt;
223 
224  MAT4X3PNT(arb_pt, invmat, arb->pt[i]);
225  VMOVE(arb->pt[i], arb_pt);
226  }
227 
228  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
229  }
230 
231  return GED_OK;
232 }
233 
234 
235 int
236 ged_find_arb_edge_nearest_pt(struct ged *gedp, int argc, const char *argv[])
237 {
238  static const char *usage = "arb view_xyz ptol";
239  struct rt_db_internal intern;
240  mat_t mat;
241  int edge, vi1, vi2;
242  vect_t view;
243  fastf_t ptol;
244 
245  /* must be double for scanf */
246  double scan[ELEMENTS_PER_VECT];
247  double ptol_scan;
248 
250  GED_CHECK_VIEW(gedp, GED_ERROR);
251  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
252 
253  /* initialize result */
254  bu_vls_trunc(gedp->ged_result_str, 0);
255 
256  /* must be wanting help */
257  if (argc == 1) {
258  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
259  return GED_HELP;
260  }
261 
262  if (argc != 4) {
263  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
264  return GED_ERROR;
265  }
266 
267  if (bu_sscanf(argv[2], "%lf %lf %lf", &scan[X], &scan[Y], &scan[Z]) != 3) {
268  bu_vls_printf(gedp->ged_result_str, "%s: bad view location - %s", argv[0], argv[2]);
269  return GED_ERROR;
270  }
271  VMOVE(view, scan); /* convert double to fastf_t */
272 
273  if (bu_sscanf(argv[3], "%lf", &ptol_scan) != 1) {
274  bu_vls_printf(gedp->ged_result_str, "%s: bad ptol - %s", argv[0], argv[3]);
275  return GED_ERROR;
276  }
277  ptol = ptol_scan;
278 
279  if (wdb_import_from_path2(gedp->ged_result_str, &intern, argv[1], gedp->ged_wdbp, mat) == GED_ERROR) {
280  bu_vls_printf(gedp->ged_result_str, "%s: failed to find %s", argv[0], argv[1]);
281  return GED_ERROR;
282  }
283 
284  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
285  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_ARB8) {
286  bu_vls_printf(gedp->ged_result_str, "Object is not an ARB");
287  rt_db_free_internal(&intern);
288 
289  return GED_ERROR;
290  }
291 
292  (void)rt_arb_find_e_nearest_pt2(&edge, &vi1, &vi2, &intern, view, gedp->ged_gvp->gv_model2view, ptol);
293  bu_vls_printf(gedp->ged_result_str, "%d %d %d", edge, vi1, vi2);
294 
295  rt_db_free_internal(&intern);
296  return GED_OK;
297 }
298 
299 
300 /*
301  * Local Variables:
302  * tab-width: 8
303  * mode: C
304  * indent-tabs-mode: t
305  * c-file-style: "stroustrup"
306  * End:
307  * ex: shiftwidth=4 tabstop=8
308  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int rt_arb_calc_planes(struct bu_vls *error_msg_ret, struct rt_arb_internal *arb, int type, plane_t planes[6], const struct bn_tol *tol)
Definition: arb8.c:1871
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
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 bview * ged_gvp
Definition: ged.h:361
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
int idb_major_type
Definition: raytrace.h:192
int ged_find_arb_edge_nearest_pt(struct ged *gedp, int argc, const char *argv[])
Definition: color.c:49
int ged_move_arb_edge(struct ged *gedp, int argc, const char *argv[])
Definition: move_arb_edge.c:39
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define LOOKUP_QUIET
Definition: raytrace.h:893
void bn_mat_inv(mat_t output, const mat_t input)
Editing operations for arb primitives.
int bu_sscanf(const char *src, const char *fmt,...) _BU_ATTR_SCANF23
Definition: sscanf.c:676
struct bu_vls * ged_result_str
Definition: ged.h:357
int rt_arb_edit(struct bu_vls *error_msg_ret, struct rt_arb_internal *arb, int arb_type, int edit_type, vect_t pos_model, plane_t planes[6], const struct bn_tol *tol)
Definition: arb8.c:1991
#define arb8_edge_vertex_mapping
Definition: arb_edit.h:142
#define arb4_edge_vertex_mapping
Definition: arb_edit.h:380
void * idb_ptr
Definition: raytrace.h:195
#define arb5_edge_vertex_mapping
Definition: arb_edit.h:334
struct bn_tol wdb_tol
Definition: raytrace.h:1269
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
#define RT_DIR_NULL
Definition: raytrace.h:875
#define GED_HELP
Definition: ged.h:62
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
Definition: color.c:51
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
int rt_arb_std_type(const struct rt_db_internal *ip, const struct bn_tol *tol)
Definition: arb8.c:317
double dbi_local2base
local2mm
Definition: raytrace.h:807
#define arb6_edge_vertex_mapping
Definition: arb_edit.h:275
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int rt_arb_find_e_nearest_pt2(int *edge, int *vert1, int *vert2, const struct rt_db_internal *ip, const point_t pt2, const mat_t mat, fastf_t ptol)
Definition: arb8.c:2334
double fastf_t
Definition: defines.h:300
#define arb7_edge_vertex_mapping
Definition: arb_edit.h:211
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
Definition: color.c:50