BRL-CAD
vutil.c
Go to the documentation of this file.
1 /* V U T I L . 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/vutil.c
21  *
22  * This file contains view related utility functions.
23  *
24  */
25 
26 #include "common.h"
27 
28 
29 #include "./ged_private.h"
30 
31 
32 void
33 ged_view_update(struct bview *gvp)
34 {
35  vect_t work, work1;
36  vect_t temp, temp1;
37 
38  if (!gvp)
39  return;
40 
42  gvp->gv_rotation,
43  gvp->gv_center);
44  gvp->gv_model2view[15] = gvp->gv_scale;
46 
47  /* Find current azimuth, elevation, and twist angles */
48  VSET(work, 0.0, 0.0, 1.0); /* view z-direction */
49  MAT4X3VEC(temp, gvp->gv_view2model, work);
50  VSET(work1, 1.0, 0.0, 0.0); /* view x-direction */
51  MAT4X3VEC(temp1, gvp->gv_view2model, work1);
52 
53  /* calculate angles using accuracy of 0.005, since display
54  * shows 2 digits right of decimal point */
55  bn_aet_vec(&gvp->gv_aet[0],
56  &gvp->gv_aet[1],
57  &gvp->gv_aet[2],
58  temp, temp1, (fastf_t)0.005);
59 
60  /* Force azimuth range to be [0, 360] */
61  if ((NEAR_EQUAL(gvp->gv_aet[1], 90.0, (fastf_t)0.005) ||
62  NEAR_EQUAL(gvp->gv_aet[1], -90.0, (fastf_t)0.005)) &&
63  gvp->gv_aet[0] < 0 &&
64  !NEAR_ZERO(gvp->gv_aet[0], (fastf_t)0.005))
65  gvp->gv_aet[0] += 360.0;
66  else if (NEAR_ZERO(gvp->gv_aet[0], (fastf_t)0.005))
67  gvp->gv_aet[0] = 0.0;
68 
69  /* apply the perspective angle to model2view */
71 
72  if (gvp->gv_callback)
73  (*gvp->gv_callback)(gvp, gvp->gv_clientData);
74 }
75 
76 
77 /**
78  * FIXME: this routine is suspect and needs investigating. if run
79  * during view initialization, the shaders regression test fails.
80  */
81 void
82 _ged_mat_aet(struct bview *gvp)
83 {
84  mat_t tmat;
85  fastf_t twist;
86  fastf_t c_twist;
87  fastf_t s_twist;
88 
90  270.0 + gvp->gv_aet[1],
91  0.0,
92  270.0 - gvp->gv_aet[0]);
93 
94  twist = -gvp->gv_aet[2] * DEG2RAD;
95  c_twist = cos(twist);
96  s_twist = sin(twist);
97  bn_mat_zrot(tmat, s_twist, c_twist);
98  bn_mat_mul2(tmat, gvp->gv_rotation);
99 }
100 
101 
102 int
103 _ged_do_rot(struct ged *gedp,
104  char coord,
105  mat_t rmat,
106  int (*func)())
107 {
108  mat_t temp1, temp2;
109 
110  if (func != (int (*)())0)
111  return (*func)(gedp, coord, gedp->ged_gvp->gv_rotate_about, rmat);
112 
113  switch (coord) {
114  case 'm':
115  /* transform model rotations into view rotations */
116  bn_mat_inv(temp1, gedp->ged_gvp->gv_rotation);
117  bn_mat_mul(temp2, gedp->ged_gvp->gv_rotation, rmat);
118  bn_mat_mul(rmat, temp2, temp1);
119  break;
120  case 'v':
121  default:
122  break;
123  }
124 
125  /* Calculate new view center */
126  if (gedp->ged_gvp->gv_rotate_about != 'v') {
127  point_t rot_pt;
128  point_t new_origin;
129  mat_t viewchg, viewchginv;
130  point_t new_cent_view;
131  point_t new_cent_model;
132 
133  switch (gedp->ged_gvp->gv_rotate_about) {
134  case 'e':
135  VSET(rot_pt, 0.0, 0.0, 1.0);
136  break;
137  case 'k':
138  MAT4X3PNT(rot_pt, gedp->ged_gvp->gv_model2view, gedp->ged_gvp->gv_keypoint);
139  break;
140  case 'm':
141  /* rotate around model center (0, 0, 0) */
142  VSET(new_origin, 0.0, 0.0, 0.0);
143  MAT4X3PNT(rot_pt, gedp->ged_gvp->gv_model2view, new_origin);
144  break;
145  default:
146  return GED_ERROR;
147  }
148 
149  bn_mat_xform_about_pt(viewchg, rmat, rot_pt);
150  bn_mat_inv(viewchginv, viewchg);
151 
152  /* Convert origin in new (viewchg) coords back to old view coords */
153  VSET(new_origin, 0.0, 0.0, 0.0);
154  MAT4X3PNT(new_cent_view, viewchginv, new_origin);
155  MAT4X3PNT(new_cent_model, gedp->ged_gvp->gv_view2model, new_cent_view);
156  MAT_DELTAS_VEC_NEG(gedp->ged_gvp->gv_center, new_cent_model);
157  }
158 
159  /* pure rotation */
160  bn_mat_mul2(rmat, gedp->ged_gvp->gv_rotation);
161  ged_view_update(gedp->ged_gvp);
162 
163  return GED_OK;
164 }
165 
166 
167 int
168 _ged_do_slew(struct ged *gedp, vect_t svec)
169 {
170  point_t model_center;
171 
172  MAT4X3PNT(model_center, gedp->ged_gvp->gv_view2model, svec);
173  MAT_DELTAS_VEC_NEG(gedp->ged_gvp->gv_center, model_center);
174  ged_view_update(gedp->ged_gvp);
175 
176  return GED_OK;
177 }
178 
179 
180 int
181 _ged_do_tra(struct ged *gedp,
182  char coord,
183  vect_t tvec,
184  int (*func)())
185 {
186  point_t delta;
187  point_t work;
188  point_t vc, nvc;
189 
190  if (func != (int (*)())0)
191  return (*func)(gedp, coord, tvec);
192 
193  switch (coord) {
194  case 'm':
195  VSCALE(delta, tvec, -gedp->ged_wdbp->dbip->dbi_base2local);
196  MAT_DELTAS_GET_NEG(vc, gedp->ged_gvp->gv_center);
197  break;
198  case 'v':
199  default:
200  VSCALE(tvec, tvec, -2.0*gedp->ged_wdbp->dbip->dbi_base2local*gedp->ged_gvp->gv_isize);
201  MAT4X3PNT(work, gedp->ged_gvp->gv_view2model, tvec);
202  MAT_DELTAS_GET_NEG(vc, gedp->ged_gvp->gv_center);
203  VSUB2(delta, work, vc);
204  break;
205  }
206 
207  VSUB2(nvc, vc, delta);
208  MAT_DELTAS_VEC_NEG(gedp->ged_gvp->gv_center, nvc);
209  ged_view_update(gedp->ged_gvp);
210 
211  return GED_OK;
212 }
213 
214 /*
215  * Local Variables:
216  * tab-width: 8
217  * mode: C
218  * indent-tabs-mode: t
219  * c-file-style: "stroustrup"
220  * End:
221  * ex: shiftwidth=4 tabstop=8
222  */
void ged_view_update(struct bview *gvp)
Definition: vutil.c:33
#define GED_OK
Definition: ged.h:55
mat_t gv_pmat
perspective matrix
Definition: bview.h:225
mat_t gv_center
Definition: bview.h:221
void(* gv_callback)()
called in ged_view_update with gvp and gv_clientData
Definition: bview.h:226
int _ged_do_slew(struct ged *gedp, vect_t svec)
Definition: vutil.c:168
int _ged_do_tra(struct ged *gedp, char coord, vect_t tvec, int(*func)())
Definition: vutil.c:181
void * gv_clientData
passed to gv_callback
Definition: bview.h:227
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
vect_t gv_keypoint
Definition: bview.h:217
#define VSET(a, b, c, d)
Definition: color.c:53
mat_t gv_pmodel2view
Definition: bview.h:223
Definition: bview.h:209
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
void bn_mat_mul2(const mat_t i, mat_t o)
mat_t gv_view2model
Definition: bview.h:224
fastf_t gv_isize
1.0 / size
Definition: bview.h:213
fastf_t gv_scale
Definition: bview.h:211
void bn_mat_inv(mat_t output, const mat_t input)
#define NEAR_ZERO(val, epsilon)
Definition: color.c:55
char gv_rotate_about
indicates what point rotations are about
Definition: bview.h:219
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
void bn_mat_angles(mat_t mat, double alpha, double beta, double ggamma)
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
mat_t gv_rotation
Definition: bview.h:220
mat_t gv_model2view
Definition: bview.h:222
void bn_aet_vec(fastf_t *az, fastf_t *el, fastf_t *twist, vect_t vec_ae, vect_t vec_twist, fastf_t accuracy)
int _ged_do_rot(struct ged *gedp, char coord, mat_t rmat, int(*func)())
Definition: vutil.c:103
vect_t gv_aet
Definition: bview.h:215
void bn_mat_xform_about_pt(mat_t mat, const mat_t xform, const point_t pt)
Definition: mat.c:909
HIDDEN const point_t delta
Definition: sh_prj.c:618
double fastf_t
Definition: defines.h:300
void bn_mat_zrot(mat_t m, double sinz, double cosz)
void _ged_mat_aet(struct bview *gvp)
Definition: vutil.c:82