BRL-CAD
tgc_mirror.c
Go to the documentation of this file.
1 /* T G C _ M I R R O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2009-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 primitives/tgc/tgc_mirror.c
21  *
22  * mirror support
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "raytrace.h"
29 #include "rtgeom.h"
30 
31 
32 /**
33  * Given a pointer to an internal GED database object, mirror the
34  * object's values about the given transformation matrix.
35  */
36 int
37 rt_tgc_mirror(struct rt_db_internal *ip, register const plane_t plane)
38 {
39  struct rt_tgc_internal *tgc;
40 
41  mat_t mirmat;
42  mat_t rmat;
43  mat_t temp;
44  vect_t nvec;
45  vect_t xvec;
46  vect_t mirror_dir;
47  point_t mirror_pt;
48  fastf_t ang;
49 
50  mat_t mat;
51  point_t pt;
52  vect_t h;
53  vect_t a, b, c, d;
54  vect_t n;
55 
56  static point_t origin = {0.0, 0.0, 0.0};
57 
59 
60  tgc = (struct rt_tgc_internal *)ip->idb_ptr;
61  RT_TGC_CK_MAGIC(tgc);
62 
63  MAT_IDN(mirmat);
64 
65  VMOVE(mirror_dir, plane);
66  VSCALE(mirror_pt, plane, plane[W]);
67 
68  /* Build mirror transform matrix, for those who need it. */
69  /* First, perform a mirror down the X axis */
70  mirmat[0] = -1.0;
71 
72  /* Create the rotation matrix */
73  VSET(xvec, 1, 0, 0);
74  VCROSS(nvec, xvec, mirror_dir);
75  VUNITIZE(nvec);
76  ang = -acos(VDOT(xvec, mirror_dir));
77  bn_mat_arb_rot(rmat, origin, nvec, ang*2.0);
78 
79  /* Add the rotation to mirmat */
80  MAT_COPY(temp, mirmat);
81  bn_mat_mul(mirmat, temp, rmat);
82 
83  /* Add the translation to mirmat */
84  mirmat[3 + X*4] += mirror_pt[X] * mirror_dir[X];
85  mirmat[3 + Y*4] += mirror_pt[Y] * mirror_dir[Y];
86  mirmat[3 + Z*4] += mirror_pt[Z] * mirror_dir[Z];
87 
88  VMOVE(pt, tgc->v);
89  MAT4X3PNT(tgc->v, mirmat, pt);
90 
91  VMOVE(h, tgc->h);
92  VUNITIZE(h);
93  VCROSS(n, mirror_dir, tgc->h);
94  VUNITIZE(n);
95  ang = M_PI_2 - acos(VDOT(h, mirror_dir));
96  bn_mat_arb_rot(mat, origin, n, ang*2);
97  VMOVE(h, tgc->h);
98  MAT4X3VEC(tgc->h, mat, h);
99 
100  VMOVE(a, tgc->a);
101  VUNITIZE(a);
102  VCROSS(n, mirror_dir, tgc->a);
103  VUNITIZE(n);
104  ang = M_PI_2 - acos(VDOT(a, mirror_dir));
105  bn_mat_arb_rot(mat, origin, n, ang*2);
106  VMOVE(a, tgc->a);
107  MAT4X3VEC(tgc->a, mat, a);
108  VMOVE(c, tgc->c);
109  MAT4X3VEC(tgc->c, mat, c);
110 
111  VMOVE(b, tgc->b);
112  VUNITIZE(b);
113  VCROSS(n, mirror_dir, tgc->b);
114  VUNITIZE(n);
115  ang = M_PI_2 - acos(VDOT(b, mirror_dir));
116  bn_mat_arb_rot(mat, origin, n, ang*2);
117  VMOVE(b, tgc->b);
118  MAT4X3VEC(tgc->b, mat, b);
119  VMOVE(d, tgc->d);
120  MAT4X3VEC(tgc->d, mat, d);
121 
122  return 0;
123 }
124 
125 
126 /*
127  * Local Variables:
128  * tab-width: 8
129  * mode: C
130  * indent-tabs-mode: t
131  * c-file-style: "stroustrup"
132  * End:
133  * ex: shiftwidth=4 tabstop=8
134  */
#define VSET(a, b, c, d)
Definition: color.c:53
Header file for the BRL-CAD common definitions.
Definition: color.c:49
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
void * idb_ptr
Definition: raytrace.h:195
Definition: color.c:51
int rt_tgc_mirror(struct rt_db_internal *ip, register const plane_t plane)
Definition: tgc_mirror.c:37
double fastf_t
Definition: defines.h:300
void bn_mat_arb_rot(mat_t m, const point_t pt, const vect_t dir, const fastf_t ang)
Definition: mat.c:987
Definition: color.c:50