BRL-CAD
move_arb_face.c
Go to the documentation of this file.
1 /* M O V E _ A R B _ F A C 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_face.c
21  *
22  * The move_arb_face 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 "raytrace.h"
33 
34 #include "./ged_private.h"
35 
36 
37 /* The arbX_faces arrays are used for relative face movement. */
38 static const int arb8_faces_first_vertex[6] = {
39  0, 4, 0, 1, 0, 2
40 };
41 
42 
43 static const int arb7_faces_first_vertex[6] = {
44  0, 0, 1, 1, 1
45 };
46 
47 
48 static const int arb6_faces_first_vertex[5] = {
49  0, 1, 0, 0, 2
50 };
51 
52 
53 static const int arb5_faces_first_vertex[5] = {
54  0, 0, 1, 2, 0
55 };
56 
57 
58 static const int arb4_faces_first_vertex[4] = {
59  0, 0, 1, 0
60 };
61 
62 static const int ARB4_MAX_FACE_INDEX = (int)(sizeof(arb4_faces_first_vertex) - 1);
63 static const int ARB5_MAX_FACE_INDEX = (int)(sizeof(arb5_faces_first_vertex) - 1);
64 static const int ARB6_MAX_FACE_INDEX = (int)(sizeof(arb6_faces_first_vertex) - 1);
65 static const int ARB7_MAX_FACE_INDEX = (int)(sizeof(arb7_faces_first_vertex) - 1);
66 static const int ARB8_MAX_FACE_INDEX = (int)(sizeof(arb8_faces_first_vertex) - 1);
67 
68 int
69 ged_move_arb_face(struct ged *gedp, int argc, const char *argv[])
70 {
71  struct rt_db_internal intern;
72  struct rt_arb_internal *arb;
73  fastf_t planes[7][4]; /* ARBs defining plane equations */
74  fastf_t save_tol_dist;
75  int arb_type;
76  int face;
77  int rflag = 0;
78  point_t pt;
79  double scan[3];
80  mat_t mat;
81  char *last;
82  struct directory *dp;
83  static const char *usage = "[-r] arb face pt";
84 
87  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
88 
89  /* initialize result */
90  bu_vls_trunc(gedp->ged_result_str, 0);
91 
92  /* must be wanting help */
93  if (argc == 1) {
94  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
95  return GED_HELP;
96  }
97 
98  if (argc < 4 || 5 < argc) {
99  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
100  return GED_ERROR;
101  }
102 
103  if (argc == 5) {
104  if (argv[1][0] != '-' || argv[1][1] != 'r' || argv[1][2] != '\0') {
105  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
106  return GED_ERROR;
107  }
108 
109  rflag = 1;
110  --argc;
111  ++argv;
112  }
113 
114  if ((last = strrchr(argv[1], '/')) == NULL)
115  last = (char *)argv[1];
116  else
117  ++last;
118 
119  if (last[0] == '\0') {
120  bu_vls_printf(gedp->ged_result_str, "illegal input - %s", argv[1]);
121  return GED_ERROR;
122  }
123 
124  if ((dp = db_lookup(gedp->ged_wdbp->dbip, last, LOOKUP_QUIET)) == RT_DIR_NULL) {
125  bu_vls_printf(gedp->ged_result_str, "%s not found", argv[1]);
126  return GED_ERROR;
127  }
128 
129  if (wdb_import_from_path2(gedp->ged_result_str, &intern, argv[1], gedp->ged_wdbp, mat) == GED_ERROR)
130  return GED_ERROR;
131 
132  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
133  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_ARB8) {
134  bu_vls_printf(gedp->ged_result_str, "Object not an ARB");
135  rt_db_free_internal(&intern);
136 
137  return TCL_OK;
138  }
139 
140  if (sscanf(argv[2], "%d", &face) != 1) {
141  bu_vls_printf(gedp->ged_result_str, "bad face - %s", argv[2]);
142  rt_db_free_internal(&intern);
143 
144  return GED_ERROR;
145  }
146 
147  face -= 1;
148 
149  if (sscanf(argv[3], "%lf %lf %lf", &scan[X], &scan[Y], &scan[Z]) != 3) {
150  bu_vls_printf(gedp->ged_result_str, "bad point - %s", argv[3]);
151  rt_db_free_internal(&intern);
152 
153  return GED_ERROR;
154  }
155  /* convert from double to fastf_t */
156  VMOVE(pt, scan);
157 
158  arb = (struct rt_arb_internal *)intern.idb_ptr;
159  RT_ARB_CK_MAGIC(arb);
160 
161  arb_type = rt_arb_std_type(&intern, &gedp->ged_wdbp->wdb_tol);
162 
163  if (rt_arb_calc_planes(gedp->ged_result_str, arb, arb_type, planes, &gedp->ged_wdbp->wdb_tol)) {
164  rt_db_free_internal(&intern);
165 
166  return GED_ERROR;
167  }
168 
169  VSCALE(pt, pt, gedp->ged_wdbp->dbip->dbi_local2base);
170 
171 #define CHECK_FACE(face_idx, max_idx) \
172 if (face_idx > max_idx) { \
173  bu_vls_printf(gedp->ged_result_str, "bad face - %s", argv[2]); \
174  rt_db_free_internal(&intern); \
175  return GED_ERROR; \
176 }
177 
178  if (rflag) {
179  int arb_pt_index;
180 
181  switch (arb_type) {
182  case ARB4:
183  CHECK_FACE(face, ARB4_MAX_FACE_INDEX);
184  arb_pt_index = arb4_faces_first_vertex[face];
185  break;
186  case ARB5:
187  CHECK_FACE(face, ARB5_MAX_FACE_INDEX);
188  arb_pt_index = arb5_faces_first_vertex[face];
189  break;
190  case ARB6:
191  CHECK_FACE(face, ARB6_MAX_FACE_INDEX);
192  arb_pt_index = arb6_faces_first_vertex[face];
193  break;
194  case ARB7:
195  CHECK_FACE(face, ARB7_MAX_FACE_INDEX);
196  arb_pt_index = arb7_faces_first_vertex[face];
197  break;
198  case ARB8:
199  CHECK_FACE(face, ARB8_MAX_FACE_INDEX);
200  arb_pt_index = arb8_faces_first_vertex[face];
201  break;
202  default:
203  bu_vls_printf(gedp->ged_result_str, "unrecognized arb type");
204  rt_db_free_internal(&intern);
205 
206  return GED_ERROR;
207  }
208 
209  VADD2(pt, pt, arb->pt[arb_pt_index]);
210  }
211 
212  /* change D of planar equation */
213  planes[face][3] = VDOT(&planes[face][0], pt);
214 
215  /* calculate new points for the arb */
216  save_tol_dist = gedp->ged_wdbp->wdb_tol.dist;
217  gedp->ged_wdbp->wdb_tol.dist = gedp->ged_wdbp->wdb_tol.dist * 2;
218  if (rt_arb_calc_points(arb, arb_type, (const plane_t *)planes, &gedp->ged_wdbp->wdb_tol) < 0) {
219  gedp->ged_wdbp->wdb_tol.dist = save_tol_dist;
220  rt_db_free_internal(&intern);
221  return GED_ERROR;
222  }
223  gedp->ged_wdbp->wdb_tol.dist = save_tol_dist;
224 
225  {
226  int i;
227  mat_t invmat;
228 
229  bn_mat_inv(invmat, mat);
230 
231  for (i = 0; i < 8; ++i) {
232  point_t arb_pt;
233 
234  MAT4X3PNT(arb_pt, invmat, arb->pt[i]);
235  VMOVE(arb->pt[i], arb_pt);
236  }
237 
238  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
239  }
240 
241  return GED_OK;
242 }
243 
244 
245 /*
246  * Local Variables:
247  * tab-width: 8
248  * mode: C
249  * indent-tabs-mode: t
250  * c-file-style: "stroustrup"
251  * End:
252  * ex: shiftwidth=4 tabstop=8
253  */
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
double dist
>= 0
Definition: tol.h:73
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
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
int idb_major_type
Definition: raytrace.h:192
int ged_move_arb_face(struct ged *gedp, int argc, const char *argv[])
Definition: move_arb_face.c:69
Definition: color.c:49
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 LOOKUP_QUIET
Definition: raytrace.h:893
void bn_mat_inv(mat_t output, const mat_t input)
struct bu_vls * ged_result_str
Definition: ged.h:357
void * idb_ptr
Definition: raytrace.h:195
struct bn_tol wdb_tol
Definition: raytrace.h:1269
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
#define CHECK_FACE(face_idx, max_idx)
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 GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
double fastf_t
Definition: defines.h:300
int rt_arb_calc_points(struct rt_arb_internal *arb, int cgtype, const plane_t planes[6], const struct bn_tol *tol)
Definition: arb8.c:1740
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
Definition: color.c:50