BRL-CAD
rotate_arb_face.c
Go to the documentation of this file.
1 /* R O T A T 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/rotate_arb_face.c
21  *
22  * The rotate_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 
33 #include "./ged_private.h"
34 
35 
36 static const short int arb_vertices[5][24] = {
37  { 1, 2, 3, 0, 1, 2, 4, 0, 2, 3, 4, 0, 1, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* arb4 */
38  { 1, 2, 3, 4, 1, 2, 5, 0, 2, 3, 5, 0, 3, 4, 5, 0, 1, 4, 5, 0, 0, 0, 0, 0 }, /* arb5 */
39  { 1, 2, 3, 4, 2, 3, 6, 5, 1, 5, 6, 4, 1, 2, 5, 0, 3, 4, 6, 0, 0, 0, 0, 0 }, /* arb6 */
40  { 1, 2, 3, 4, 5, 6, 7, 0, 1, 4, 5, 0, 2, 3, 7, 6, 1, 2, 6, 5, 4, 3, 7, 5 }, /* arb7 */
41  { 1, 2, 3, 4, 5, 6, 7, 8, 1, 5, 8, 4, 2, 3, 7, 6, 1, 2, 6, 5, 4, 3, 7, 8 } /* arb8 */
42 };
43 
44 
45 int
46 ged_rotate_arb_face(struct ged *gedp, int argc, const char *argv[])
47 {
48  struct rt_db_internal intern;
49  struct rt_arb_internal *arb;
50  struct directory *dp;
51  fastf_t planes[7][4]; /* ARBs defining plane equations */
52  int arb_type;
53  int face;
54  int vi;
55  mat_t mat;
56  int i;
57  int pnt5; /* special arb7 case */
58  char *last;
59 
60  /* intentionally double for scan */
61  double pt[3];
62 
63  static const char *usage = "arb face pt rvec";
64 
67  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
68 
69  /* initialize result */
70  bu_vls_trunc(gedp->ged_result_str, 0);
71 
72  /* must be wanting help */
73  if (argc == 1) {
74  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
75  return GED_HELP;
76  }
77 
78  if (argc != 5) {
79  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
80  return GED_ERROR;
81  }
82 
83  if ((last = strrchr(argv[1], '/')) == NULL)
84  last = (char *)argv[1];
85  else
86  ++last;
87 
88  if (last[0] == '\0') {
89  bu_vls_printf(gedp->ged_result_str, "illegal input - %s", argv[1]);
90  return GED_ERROR;
91  }
92 
93  if ((dp = db_lookup(gedp->ged_wdbp->dbip, last, LOOKUP_QUIET)) == RT_DIR_NULL) {
94  bu_vls_printf(gedp->ged_result_str, "%s not found", argv[1]);
95  return GED_ERROR;
96  }
97 
98  if (wdb_import_from_path2(gedp->ged_result_str, &intern, argv[1], gedp->ged_wdbp, mat) == GED_ERROR)
99  return GED_ERROR;
100 
101  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD ||
102  intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_ARB8) {
103  bu_vls_printf(gedp->ged_result_str, "Object not an ARB");
104  rt_db_free_internal(&intern);
105 
106  return GED_OK;
107  }
108 
109  if (sscanf(argv[2], "%d", &face) != 1) {
110  bu_vls_printf(gedp->ged_result_str, "bad face - %s", argv[2]);
111  rt_db_free_internal(&intern);
112 
113  return GED_ERROR;
114  }
115 
116  /*XXX need better checking of the face */
117  face -= 1;
118  if (face < 0 || 5 < face) {
119  bu_vls_printf(gedp->ged_result_str, "bad face - %s", argv[2]);
120  rt_db_free_internal(&intern);
121 
122  return GED_ERROR;
123  }
124 
125  if (sscanf(argv[3], "%d", &vi) != 1) {
126  bu_vls_printf(gedp->ged_result_str, "bad vertex index - %s", argv[2]);
127  rt_db_free_internal(&intern);
128 
129  return GED_ERROR;
130  }
131 
132 
133  /*XXX need better checking of the vertex index */
134  vi -= 1;
135  if (vi < 0 || 7 < vi) {
136  bu_vls_printf(gedp->ged_result_str, "bad vertex - %s", argv[2]);
137  rt_db_free_internal(&intern);
138 
139  return GED_ERROR;
140  }
141 
142  if (sscanf(argv[4], "%lf %lf %lf", &pt[X], &pt[Y], &pt[Z]) != 3) {
143  bu_vls_printf(gedp->ged_result_str, "bad point - %s", argv[3]);
144  rt_db_free_internal(&intern);
145 
146  return GED_ERROR;
147  }
148 
149  arb = (struct rt_arb_internal *)intern.idb_ptr;
150  RT_ARB_CK_MAGIC(arb);
151 
152  arb_type = rt_arb_std_type(&intern, &gedp->ged_wdbp->wdb_tol);
153 
154  if (rt_arb_calc_planes(gedp->ged_result_str, arb, arb_type, planes, &gedp->ged_wdbp->wdb_tol)) {
155  rt_db_free_internal(&intern);
156 
157  return GED_ERROR;
158  }
159 
160  /* special case for arb4 */
161  if (arb_type == ARB4 && vi >= 3)
162  vi = 4;
163 
164  /* special case for arb6 */
165  if (arb_type == ARB6 && vi >= 5)
166  vi = 6;
167 
168  /* special case for arb7 */
169  if (arb_type == ARB7) {
170  /* check if point 5 is in the face */
171  pnt5 = 0;
172  for (i = 0; i < 4; i++) {
173  if (arb_vertices[arb_type-4][face*4+i]==5)
174  pnt5=1;
175  }
176 
177  if (pnt5)
178  vi = 4;
179  }
180 
181  {
182  /* Apply incremental changes */
183  vect_t tempvec;
184  vect_t work;
185  fastf_t *plane;
186  mat_t rmat;
187 
188  bn_mat_angles(rmat, pt[X], pt[Y], pt[Z]);
189 
190  plane = &planes[face][0];
191  VMOVE(work, plane);
192  MAT4X3VEC(plane, rmat, work);
193 
194  /* point notation of fixed vertex */
195  VMOVE(tempvec, arb->pt[vi]);
196 
197  /* set D of planar equation to anchor at fixed vertex */
198  planes[face][3]=VDOT(plane, tempvec);
199  }
200 
201  /* calculate new points for the arb */
202  (void)rt_arb_calc_points(arb, arb_type, (const plane_t *)planes, &gedp->ged_wdbp->wdb_tol);
203 
204  {
205  mat_t invmat;
206 
207  bn_mat_inv(invmat, mat);
208 
209  for (i = 0; i < 8; ++i) {
210  point_t arb_pt;
211 
212  MAT4X3PNT(arb_pt, invmat, arb->pt[i]);
213  VMOVE(arb->pt[i], arb_pt);
214  }
215 
216  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
217  }
218 
219  rt_db_free_internal(&intern);
220  return GED_OK;
221 }
222 
223 
224 /*
225  * Local Variables:
226  * tab-width: 8
227  * mode: C
228  * indent-tabs-mode: t
229  * c-file-style: "stroustrup"
230  * End:
231  * ex: shiftwidth=4 tabstop=8
232  */
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
int ged_rotate_arb_face(struct ged *gedp, int argc, const char *argv[])
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
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 bn_mat_angles(mat_t mat, double alpha, double beta, double ggamma)
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
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
#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