nurb_poly.c
Go to the documentation of this file.
1 /* N U R B _ P O L Y . C
3  *
4  * Copyright (c) 1990-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
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  */
21 /** @{ */
22 /** @file primitives/bspline/nurb_poly.c
23  *
24  * Returns two polygons from a NURB surface.
25  * Assumes that the surface is flat.
26  *
27  */
28 /** @} */
29
30 #include "common.h"
31
32 #include "bio.h"
33
34 #include "vmath.h"
35 #include "raytrace.h"
36 #include "nurb.h"
37
38
39 /**
40  * Algorithm -
41  *
42  * From the four corners of the surface, return the two parts split by
43  * the diagonal from the first and third corner point making sure
44  * Homogeneous points are divided.
45  */
46 struct rt_nurb_poly *
47 rt_nurb_to_poly(struct face_g_snurb *srf)
48 {
49  int coords = srf->pt_type;
50  fastf_t * p1, *p2, *p3, *p4;
51  fastf_t uv1[2], uv2[2], uv3[2], uv4[2];
53
54  /* Extract the four corners from the mesh */
55
56  p1 = srf->ctl_points;
57  p2 = srf->ctl_points + coords * srf->s_size[1];
58  p3 = srf->ctl_points + (coords * srf->s_size[1] *
59  (srf->s_size[0] - 1)) +
60  ((srf->s_size[1] - 1) * coords);
61  p4 = srf->ctl_points + (coords * srf->s_size[1] *
62  (srf->s_size[0] - 1));
63
64  /* If the point is rational then divide out the w component */
65  if (RT_NURB_IS_PT_RATIONAL(srf->pt_type)) {
66  int w_index;
67
68  if (RT_NURB_EXTRACT_PT_TYPE(srf->pt_type) == RT_NURB_PT_XY)
69  w_index = 2;
70  else if (RT_NURB_EXTRACT_PT_TYPE(srf->pt_type) == RT_NURB_PT_UV)
71  w_index = 2;
72  else if (RT_NURB_EXTRACT_PT_TYPE(srf->pt_type) == RT_NURB_PT_XYZ)
73  w_index = 3;
74  else /* assume the forth coordinate */
75  w_index = 3;
76
77  p1[0] = p1[0] / p1[w_index];
78  p2[0] = p2[0] / p1[w_index];
79  p3[0] = p3[0] / p1[w_index];
80  p4[0] = p4[0] / p1[w_index];
81  }
82
83  uv1[0] = srf->u.knots[0];
84  uv1[1] = srf->v.knots[0];
85
86  uv2[0] = srf->u.knots[srf->u.k_size -1];
87  uv2[1] = srf->v.knots[0];
88
89  uv3[0] = srf->u.knots[srf->u.k_size -1];
90  uv3[1] = srf->v.knots[srf->v.k_size -1];
91
92  uv4[0] = srf->u.knots[0];
93  uv4[1] = srf->v.knots[srf->v.k_size -1];
94
95  p = rt_nurb_mk_poly(p1, p2, p3, uv1, uv2, uv3);
97  p = rt_nurb_mk_poly(p3, p4, p1, uv3, uv4, uv1);
100
102 }
103
104
105 struct rt_nurb_poly *
106 rt_nurb_mk_poly(fastf_t *v1, fastf_t *v2, fastf_t *v3, fastf_t *uv1, fastf_t *uv2, fastf_t *uv3)
107 {
108  struct rt_nurb_poly *p;
109
110  BU_ALLOC(p, struct rt_nurb_poly);
111  p->next = (struct rt_nurb_poly *) 0;
112
113  VMOVE(p->ply[0], v1);
114  VMOVE(p->ply[1], v2);
115  VMOVE(p->ply[2], v3);
116
117  p->uv[0][0] = uv1[0];
118  p->uv[0][1] = uv1[1];
119  p->uv[1][0] = uv2[0];
120  p->uv[1][1] = uv2[1];
121  p->uv[2][0] = uv3[0];
122  p->uv[2][1] = uv3[1];
123
124  return p;
125 }
126
127
128 /*
129  * Local Variables:
130  * mode: C
131  * tab-width: 8
132  * indent-tabs-mode: t
133  * c-file-style: "stroustrup"
134  * End:
135  * ex: shiftwidth=4 tabstop=8
136  */