BRL-CAD
orotate.c
Go to the documentation of this file.
1 /* O R O T A T 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/orotate.c
21  *
22  * The orotate command.
23  *
24  */
25 
26 #include "common.h"
27 
28 
29 #include "./ged_private.h"
30 
31 
32 int
33 ged_orotate(struct ged *gedp, int argc, const char *argv[])
34 {
35  struct directory *dp;
36  struct _ged_trace_data gtd;
37  struct rt_db_internal intern;
38 
39  /* intentionally double for scan */
40  double xrot, yrot, zrot;
41 
42  mat_t rmat;
43  mat_t pmat;
44  mat_t emat;
45  mat_t tmpMat;
46  mat_t invXform;
47  point_t rpp_min;
48  point_t rpp_max;
49  point_t keypoint;
50  static const char *usage = "obj rX rY rZ [kX kY kZ]";
51 
54  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
55 
56  /* initialize result */
57  bu_vls_trunc(gedp->ged_result_str, 0);
58 
59  /* must be wanting help */
60  if (argc == 1) {
61  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
62  return GED_HELP;
63  }
64 
65  if (argc != 5 && argc != 8) {
66  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
67  return GED_ERROR;
68  }
69 
70  if (sscanf(argv[2], "%lf", &xrot) != 1) {
71  bu_vls_printf(gedp->ged_result_str, "%s: bad rX value - %s", argv[0], argv[2]);
72  return GED_ERROR;
73  }
74 
75  if (sscanf(argv[3], "%lf", &yrot) != 1) {
76  bu_vls_printf(gedp->ged_result_str, "%s: bad rY value - %s", argv[0], argv[3]);
77  return GED_ERROR;
78  }
79 
80  if (sscanf(argv[4], "%lf", &zrot) != 1) {
81  bu_vls_printf(gedp->ged_result_str, "%s: bad rZ value - %s", argv[0], argv[4]);
82  return GED_ERROR;
83  }
84 
85  if (argc == 5) {
86  /* Use the object's center as the keypoint. */
87 
88  if (_ged_get_obj_bounds2(gedp, 1, argv+1, &gtd, rpp_min, rpp_max) == GED_ERROR)
89  return GED_ERROR;
90 
91  dp = gtd.gtd_obj[gtd.gtd_objpos-1];
92  if (!(dp->d_flags & RT_DIR_SOLID)) {
93  if (ged_get_obj_bounds(gedp, 1, argv+1, 1, rpp_min, rpp_max) == GED_ERROR)
94  return GED_ERROR;
95  }
96 
97  VADD2(keypoint, rpp_min, rpp_max);
98  VSCALE(keypoint, keypoint, 0.5);
99  } else {
100  double scan[3];
101 
102  /* The user has provided the keypoint. */
103  MAT_IDN(gtd.gtd_xform);
104 
105  if (sscanf(argv[5], "%lf", &scan[X]) != 1) {
106  bu_vls_printf(gedp->ged_result_str, "%s: bad kX value - %s", argv[0], argv[5]);
107  return GED_ERROR;
108  }
109 
110  if (sscanf(argv[6], "%lf", &scan[Y]) != 1) {
111  bu_vls_printf(gedp->ged_result_str, "%s: bad kY value - %s", argv[0], argv[6]);
112  return GED_ERROR;
113  }
114 
115  if (sscanf(argv[7], "%lf", &scan[Z]) != 1) {
116  bu_vls_printf(gedp->ged_result_str, "%s: bad kZ value - %s", argv[0], argv[7]);
117  return GED_ERROR;
118  }
119 
120  VSCALE(keypoint, scan, gedp->ged_wdbp->dbip->dbi_local2base);
121 
122  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_QUIET)) == RT_DIR_NULL) {
123  bu_vls_printf(gedp->ged_result_str, "%s: %s not found", argv[0], argv[1]);
124  return GED_ERROR;
125  }
126  }
127 
128  bn_mat_angles(rmat, xrot, yrot, zrot);
129  bn_mat_xform_about_pt(pmat, rmat, keypoint);
130 
131  bn_mat_inv(invXform, gtd.gtd_xform);
132  bn_mat_mul(tmpMat, invXform, pmat);
133  bn_mat_mul(emat, tmpMat, gtd.gtd_xform);
134 
135  GED_DB_GET_INTERNAL(gedp, &intern, dp, emat, &rt_uniresource, GED_ERROR);
136  RT_CK_DB_INTERNAL(&intern);
137  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
138 
139  return GED_OK;
140 }
141 
142 
143 /*
144  * Local Variables:
145  * tab-width: 8
146  * mode: C
147  * indent-tabs-mode: t
148  * c-file-style: "stroustrup"
149  * End:
150  * ex: shiftwidth=4 tabstop=8
151  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
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
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 RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define LOOKUP_QUIET
Definition: raytrace.h:893
void bn_mat_inv(mat_t output, const mat_t input)
int ged_get_obj_bounds(struct ged *gedp, int argc, const char *argv[], int use_air, point_t rpp_min, point_t rpp_max)
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
struct bu_vls * ged_result_str
Definition: ged.h:357
void bn_mat_angles(mat_t mat, double alpha, double beta, double ggamma)
mat_t gtd_xform
Definition: ged.h:1917
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 GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
Definition: color.c:51
int ged_orotate(struct ged *gedp, int argc, const char *argv[])
Definition: orotate.c:33
double dbi_local2base
local2mm
Definition: raytrace.h:807
void bn_mat_xform_about_pt(mat_t mat, const mat_t xform, const point_t pt)
Definition: mat.c:909
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
struct directory * gtd_obj[_GED_MAX_LEVELS]
Definition: ged.h:1916
int _ged_get_obj_bounds2(struct ged *gedp, int argc, const char *argv[], struct _ged_trace_data *gtdp, point_t rpp_min, point_t rpp_max)
This version works if the last member of the path is a primitive.
Definition: color.c:50
int gtd_objpos
Definition: ged.h:1918