BRL-CAD
qvrot.c
Go to the documentation of this file.
1 /* Q V R O T . 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/qvrot.c
21  *
22  * The qvrot command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "./ged_private.h"
33 
34 
35 /*
36  * Apply the "joystick" delta rotation to the viewing direction,
37  * where the delta is specified in terms of the *viewing* axes.
38  * Rotation is performed about the view center, for now.
39  * Angles are in radians.
40  */
41 static void
42 usejoy(struct ged *gedp, double xangle, double yangle, double zangle)
43 {
44  mat_t newrot; /* NEW rot matrix, from joystick */
45 
46  /* NORMAL CASE.
47  * Apply delta viewing rotation for non-edited parts.
48  * The view rotates around the VIEW CENTER.
49  */
50  MAT_IDN(newrot);
51  bn_mat_angles_rad(newrot, xangle, yangle, zangle);
52  bn_mat_mul2(newrot, gedp->ged_gvp->gv_rotation);
53 }
54 
55 
56 int
57 ged_qvrot(struct ged *gedp, int argc, const char *argv[])
58 {
59  double dx, dy, dz;
60  double az;
61  double el;
62  double theta;
63  static const char *usage = "x y z angle";
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 (sscanf(argv[1], "%lf", &dx) != 1) {
84  bu_vls_printf(gedp->ged_result_str, "%s: bad X value - %s\n", argv[0], argv[1]);
85  return GED_ERROR;
86  }
87 
88  if (sscanf(argv[2], "%lf", &dy) != 1) {
89  bu_vls_printf(gedp->ged_result_str, "%s: bad Y value - %s\n", argv[0], argv[1]);
90  return GED_ERROR;
91  }
92 
93  if (sscanf(argv[3], "%lf", &dz) != 1) {
94  bu_vls_printf(gedp->ged_result_str, "%s: bad Z value - %s\n", argv[0], argv[1]);
95  return GED_ERROR;
96  }
97 
98  if (sscanf(argv[4], "%lf", &theta) != 1) {
99  bu_vls_printf(gedp->ged_result_str, "%s: bad angle - %s\n", argv[0], argv[1]);
100  return GED_ERROR;
101  }
102 
103  if (NEAR_ZERO(dy, 0.00001) && NEAR_ZERO(dx, 0.00001)) {
104  if (NEAR_ZERO(dz, 0.00001)) {
105  bu_vls_printf(gedp->ged_result_str, "%s: (dx, dy, dz) may not be the zero vector\n", argv[0]);
106  return GED_ERROR;
107  }
108  az = 0.0;
109  } else
110  az = atan2(dy, dx);
111 
112  el = atan2(dz, sqrt(dx * dx + dy * dy));
113 
114  bn_mat_angles(gedp->ged_gvp->gv_rotation, 270.0 + el * RAD2DEG, 0.0, 270.0 - az * RAD2DEG);
115  usejoy(gedp, 0.0, 0.0, theta*DEG2RAD);
116  ged_view_update(gedp->ged_gvp);
117 
118  return GED_OK;
119 }
120 
121 
122 /*
123  * Local Variables:
124  * tab-width: 8
125  * mode: C
126  * indent-tabs-mode: t
127  * c-file-style: "stroustrup"
128  * End:
129  * ex: shiftwidth=4 tabstop=8
130  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
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
Header file for the BRL-CAD common definitions.
void ged_view_update(struct bview *gvp)
Definition: vutil.c:33
#define GED_ERROR
Definition: ged.h:61
struct bview * ged_gvp
Definition: ged.h:361
void bn_mat_mul2(const mat_t i, mat_t o)
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define NEAR_ZERO(val, epsilon)
Definition: color.c:55
struct bu_vls * ged_result_str
Definition: ged.h:357
void bn_mat_angles(mat_t mat, double alpha, double beta, double ggamma)
void bn_mat_angles_rad(mat_t mat, double alpha, double beta, double ggamma)
mat_t gv_rotation
Definition: bview.h:220
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
int ged_qvrot(struct ged *gedp, int argc, const char *argv[])
Definition: qvrot.c:57