BRL-CAD
hyp_mirror.c
Go to the documentation of this file.
1 /* H Y P _ 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/hyp/hyp_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_hyp_mirror(struct rt_db_internal *ip, register const plane_t plane)
38 {
39  struct rt_hyp_internal *hyp;
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  point_t pt;
51  vect_t h;
52  vect_t a;
53  vect_t n;
54 
55  static point_t origin = {0.0, 0.0, 0.0};
56 
58 
59  hyp = (struct rt_hyp_internal *)ip->idb_ptr;
60  RT_HYP_CK_MAGIC(hyp);
61 
62  MAT_IDN(mirmat);
63 
64  VMOVE(mirror_dir, plane);
65  VSCALE(mirror_pt, plane, plane[W]);
66 
67  /* Build mirror transform matrix, for those who need it. */
68  /* First, perform a mirror down the X axis */
69  mirmat[0] = -1.0;
70 
71  /* Create the rotation matrix */
72  VSET(xvec, 1, 0, 0);
73  VCROSS(nvec, xvec, mirror_dir);
74  VUNITIZE(nvec);
75  ang = -acos(VDOT(xvec, mirror_dir));
76  bn_mat_arb_rot(rmat, origin, nvec, ang*2.0);
77 
78  /* Add the rotation to mirmat */
79  MAT_COPY(temp, mirmat);
80  bn_mat_mul(mirmat, temp, rmat);
81 
82  /* Add the translation to mirmat */
83  mirmat[3 + X*4] += mirror_pt[X] * mirror_dir[X];
84  mirmat[3 + Y*4] += mirror_pt[Y] * mirror_dir[Y];
85  mirmat[3 + Z*4] += mirror_pt[Z] * mirror_dir[Z];
86 
87  VMOVE(pt, hyp->hyp_Vi);
88  MAT4X3PNT(hyp->hyp_Vi, mirmat, pt);
89 
90  VMOVE(h, hyp->hyp_Hi);
91  VUNITIZE(h);
92  VCROSS(n, mirror_dir, hyp->hyp_Hi);
93  VUNITIZE(n);
94  ang = M_PI_2 - acos(VDOT(h, mirror_dir));
95  bn_mat_arb_rot(rmat, origin, n, ang*2);
96  VMOVE(h, hyp->hyp_Hi);
97  MAT4X3VEC(hyp->hyp_Hi, rmat, h);
98 
99  VMOVE(a, hyp->hyp_A);
100  VUNITIZE(a);
101  VCROSS(n, mirror_dir, hyp->hyp_A);
102  VUNITIZE(n);
103  ang = M_PI_2 - acos(VDOT(a, mirror_dir));
104  bn_mat_arb_rot(rmat, origin, n, ang*2);
105  VMOVE(a, hyp->hyp_A);
106  MAT4X3VEC(hyp->hyp_A, rmat, a);
107 
108  return 0;
109 }
110 
111 
112 /*
113  * Local Variables:
114  * tab-width: 8
115  * mode: C
116  * indent-tabs-mode: t
117  * c-file-style: "stroustrup"
118  * End:
119  * ex: shiftwidth=4 tabstop=8
120  */
int rt_hyp_mirror(struct rt_db_internal *ip, register const plane_t plane)
Definition: hyp_mirror.c:37
#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
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