BRL-CAD
arbn_mirror.c
Go to the documentation of this file.
1 /* A R B N _ 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/arbn/arbn_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_arbn_mirror(struct rt_db_internal *ip, register const plane_t plane)
38 {
39  struct rt_arbn_internal *arbn;
40 
41  size_t i;
42  mat_t mirmat;
43  mat_t rmat;
44  mat_t temp;
45  vect_t nvec;
46  vect_t xvec;
47  vect_t mirror_dir;
48  point_t mirror_pt;
49  fastf_t ang;
50 
51  static point_t origin = {0.0, 0.0, 0.0};
52 
54 
55  arbn = (struct rt_arbn_internal *)ip->idb_ptr;
56  RT_ARBN_CK_MAGIC(arbn);
57 
58  MAT_IDN(mirmat);
59 
60  VMOVE(mirror_dir, plane);
61  VSCALE(mirror_pt, plane, plane[W]);
62 
63  /* Build mirror transform matrix, for those who need it. */
64  /* First, perform a mirror down the X axis */
65  mirmat[0] = -1.0;
66 
67  /* Create the rotation matrix */
68  VSET(xvec, 1, 0, 0);
69  VCROSS(nvec, xvec, mirror_dir);
70  VUNITIZE(nvec);
71  ang = -acos(VDOT(xvec, mirror_dir));
72  bn_mat_arb_rot(rmat, origin, nvec, ang*2.0);
73 
74  /* Add the rotation to mirmat */
75  MAT_COPY(temp, mirmat);
76  bn_mat_mul(mirmat, temp, rmat);
77 
78  /* Add the translation to mirmat */
79  mirmat[3 + X*4] += mirror_pt[X] * mirror_dir[X];
80  mirmat[3 + Y*4] += mirror_pt[Y] * mirror_dir[Y];
81  mirmat[3 + Z*4] += mirror_pt[Z] * mirror_dir[Z];
82 
83  for (i=0; i<arbn->neqn; i++) {
84  point_t orig_pt;
85  point_t pt;
86  vect_t norm;
87  fastf_t factor;
88 
89  /* unitize the plane equation first */
90  factor = 1.0 / MAGNITUDE(arbn->eqn[i]);
91  VSCALE(arbn->eqn[i], arbn->eqn[i], factor);
92  arbn->eqn[i][W] = arbn->eqn[i][W] * factor;
93 
94  /* Pick a point on the original halfspace */
95  VSCALE(orig_pt, arbn->eqn[i], arbn->eqn[i][W]);
96 
97  /* Transform the point, and the normal */
98  MAT4X3VEC(norm, mirmat, arbn->eqn[i]);
99  MAT4X3PNT(pt, mirmat, orig_pt);
100 
101  /* Measure new distance from origin to new point */
102  VUNITIZE(norm);
103  VMOVE(arbn->eqn[i], norm);
104  arbn->eqn[i][W] = VDOT(pt, norm);
105  }
106 
107  return 0;
108 }
109 
110 
111 /*
112  * Local Variables:
113  * tab-width: 8
114  * mode: C
115  * indent-tabs-mode: t
116  * c-file-style: "stroustrup"
117  * End:
118  * ex: shiftwidth=4 tabstop=8
119  */
#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
int rt_arbn_mirror(struct rt_db_internal *ip, register const plane_t plane)
Definition: arbn_mirror.c:37
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