BRL-CAD
bspline_mirror.c
Go to the documentation of this file.
1 /* B S P L I N E _ 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/bspline/bspline_mirror.c
21  *
22  * mirror support
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "raytrace.h"
29 #include "rtgeom.h"
30 #include "nurb.h"
31 
32 
33 /**
34  * Given a pointer to an internal GED database object, mirror the
35  * object's values about the given transformation matrix.
36  */
37 int
38 rt_nurb_mirror(struct rt_db_internal *ip, register const plane_t plane)
39 {
40  struct rt_nurb_internal *nurb;
41 
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  int i;
52  int j;
53 
54  static point_t origin = {0.0, 0.0, 0.0};
55 
57 
58  nurb = (struct rt_nurb_internal *)ip->idb_ptr;
59  RT_PG_CK_MAGIC(nurb);
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  for (i=0; i<nurb->nsrf; i++) {
87  fastf_t *ptr;
88  int tmp;
89  int orig_size[2];
90  int ncoords;
91  int m;
92  int l;
93 
94  /* swap knot vectors between u and v */
95  ptr = nurb->srfs[i]->u.knots;
96  tmp = nurb->srfs[i]->u.k_size;
97 
98  nurb->srfs[i]->u.knots = nurb->srfs[i]->v.knots;
99  nurb->srfs[i]->u.k_size = nurb->srfs[i]->v.k_size;
100  nurb->srfs[i]->v.knots = ptr;
101  nurb->srfs[i]->v.k_size = tmp;
102 
103  /* swap order */
104  tmp = nurb->srfs[i]->order[0];
105  nurb->srfs[i]->order[0] = nurb->srfs[i]->order[1];
106  nurb->srfs[i]->order[1] = tmp;
107 
108  /* swap mesh size */
109  orig_size[0] = nurb->srfs[i]->s_size[0];
110  orig_size[1] = nurb->srfs[i]->s_size[1];
111 
112  nurb->srfs[i]->s_size[0] = orig_size[1];
113  nurb->srfs[i]->s_size[1] = orig_size[0];
114 
115  /* allocate memory for a new control mesh */
116  ncoords = RT_NURB_EXTRACT_COORDS(nurb->srfs[i]->pt_type);
117  ptr = (fastf_t *)bu_calloc(orig_size[0]*orig_size[1]*ncoords, sizeof(fastf_t), "rt_mirror: ctl mesh ptr");
118 
119  /* mirror each control point */
120  for (j=0; j<orig_size[0]*orig_size[1]; j++) {
121  point_t pt;
122 
123  VMOVE(pt, &nurb->srfs[i]->ctl_points[j*ncoords]);
124  MAT4X3PNT(&nurb->srfs[i]->ctl_points[j*ncoords], mirmat, pt);
125  }
126 
127  /* copy mirrored control points into new mesh
128  * while swapping u and v */
129  m = 0;
130  for (j=0; j<orig_size[0]; j++) {
131  for (l=0; l<orig_size[1]; l++) {
132  VMOVEN(&ptr[(l*orig_size[0]+j)*ncoords], &nurb->srfs[i]->ctl_points[m*ncoords], ncoords);
133  m++;
134  }
135  }
136 
137  /* free old mesh */
138  bu_free((char *)nurb->srfs[i]->ctl_points, "rt_mirror: ctl points");
139 
140  /* put new mesh in place */
141  nurb->srfs[i]->ctl_points = ptr;
142  }
143 
144  return 0;
145 }
146 
147 
148 /*
149  * Local Variables:
150  * tab-width: 8
151  * mode: C
152  * indent-tabs-mode: t
153  * c-file-style: "stroustrup"
154  * End:
155  * ex: shiftwidth=4 tabstop=8
156  */
int rt_nurb_mirror(struct rt_db_internal *ip, register const plane_t plane)
#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
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