BRL-CAD
poly_mirror.c
Go to the documentation of this file.
1 /* P O L Y _ 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/poly/poly_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_poly_mirror(struct rt_db_internal *ip, register const plane_t plane)
38 {
39  struct rt_pg_internal *poly;
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  size_t i, j;
51  fastf_t *verts;
52  fastf_t *norms;
53 
54  static point_t origin = {0.0, 0.0, 0.0};
55 
57 
58  poly = (struct rt_pg_internal *)ip->idb_ptr;
59  RT_PG_CK_MAGIC(poly);
60 
61  MAT_IDN(mirmat);
62 
63  VMOVE(mirror_dir, plane);
64  VSCALE(mirror_pt, plane, plane[W]);
65 
66  /* Build mirror transform matrix, for those who need it. */
67  /* First, perform a mirror down the X axis */
68  mirmat[0] = -1.0;
69 
70  /* Create the rotation matrix */
71  VSET(xvec, 1, 0, 0);
72  VCROSS(nvec, xvec, mirror_dir);
73  VUNITIZE(nvec);
74  ang = -acos(VDOT(xvec, mirror_dir));
75  bn_mat_arb_rot(rmat, origin, nvec, ang*2.0);
76 
77  /* Add the rotation to mirmat */
78  MAT_COPY(temp, mirmat);
79  bn_mat_mul(mirmat, temp, rmat);
80 
81  /* Add the translation to mirmat */
82  mirmat[3 + X*4] += mirror_pt[X] * mirror_dir[X];
83  mirmat[3 + Y*4] += mirror_pt[Y] * mirror_dir[Y];
84  mirmat[3 + Z*4] += mirror_pt[Z] * mirror_dir[Z];
85 
86  verts = (fastf_t *)bu_calloc(poly->max_npts*3, sizeof(fastf_t), "rt_mirror: verts");
87  norms = (fastf_t *)bu_calloc(poly->max_npts*3, sizeof(fastf_t), "rt_mirror: norms");
88 
89  for (i=0; i<poly->npoly; i++) {
90  size_t last;
91 
92  last = (poly->poly[i].npts - 1)*3;
93  /* mirror coords and temporarily store in reverse order */
94  for (j=0; j<poly->poly[i].npts*3; j += 3) {
95  point_t pt;
96  vect_t n1;
97  vect_t n2;
98  mat_t mat;
99 
100  VMOVE(pt, &poly->poly[i].verts[j]);
101  MAT4X3PNT(&poly->poly[i].verts[j], mirmat, pt);
102 
103  VMOVE(n1, &poly->poly[i].norms[j]);
104  VUNITIZE(n1);
105 
106  VCROSS(n2, mirror_dir, &poly->poly[i].norms[j]);
107  VUNITIZE(n2);
108  ang = M_PI_2 - acos(VDOT(n1, mirror_dir));
109  bn_mat_arb_rot(mat, origin, n2, ang*2);
110  MAT4X3VEC(&poly->poly[i].norms[j], mat, n1);
111 
112  VMOVE(&norms[last-j], &poly->poly[i].norms[j]);
113  }
114 
115  /* write back mirrored and reversed face loop */
116  for (j=0; j<poly->poly[i].npts*3; j += 3) {
117  VMOVE(&poly->poly[i].norms[j], &norms[j]);
118  VMOVE(&poly->poly[i].verts[j], &verts[j]);
119  }
120  }
121 
122  bu_free((char *)verts, "rt_mirror: verts");
123  bu_free((char *)norms, "rt_mirror: norms");
124 
125  return 0;
126 }
127 
128 
129 /*
130  * Local Variables:
131  * tab-width: 8
132  * mode: C
133  * indent-tabs-mode: t
134  * c-file-style: "stroustrup"
135  * End:
136  * ex: shiftwidth=4 tabstop=8
137  */
#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 * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
void * idb_ptr
Definition: raytrace.h:195
int rt_poly_mirror(struct rt_db_internal *ip, register const plane_t plane)
Definition: poly_mirror.c:37
Definition: color.c:51
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
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