~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/covira/splinesurf3.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *
  3  * splinesurf.c
  4  *
  5  * Uniform cubic B-spline surface (x(s,t), y(s,t), z(s,t)) functions.
  6  *
  7  */
  8 
  9 #include <tina/all_tina.h>
 10 #include <tina/brain.h>
 11 #include <tina/brainfuncs.h>
 12 
 13 /*
 14 Allocate and return a 3D surface spline with nu by nv independent knots.
 15 utype and vtype specify spline types (SPLINE_NATURAL, SPLINE_PERIODIC, ...)
 16 in u and v parameters, see spline_make.
 17 */
 18 Splinesurf3 *splinesurf3_make(int utype, int vtype, int nu, int nv)
 19 {
 20     Splinesurf3 *ss = talloc(Splinesurf3);
 21     ss->utype = utype;
 22     ss->vtype = vtype;
 23     ss->nu = nu;
 24     ss->nv = nv;
 25     ss->x = splinesurf_make(utype, vtype, nu, nv);
 26     ss->y = splinesurf_make(utype, vtype, nu, nv);
 27     ss->z = splinesurf_make(utype, vtype, nu, nv);
 28     return (ss);
 29 }
 30 
 31 /*
 32 Allocate and return a copy of a 3D-surface spline.
 33 */
 34 Splinesurf3 *splinesurf3_copy(Splinesurf3 * ss)
 35 {
 36     Splinesurf3 *newss;
 37     if (ss == NULL)
 38         return (NULL);
 39     newss = talloc(Splinesurf3);
 40     newss->utype = ss->vtype;
 41     newss->vtype = ss->vtype;
 42     newss->nv = ss->nu;
 43     newss->nv = ss->nv;
 44     newss->x = splinesurf_copy(ss->x);
 45     newss->y = splinesurf_copy(ss->y);
 46     newss->z = splinesurf_copy(ss->z);
 47     return (newss);
 48 }
 49 
 50 /*
 51 Free a 3D-surface spline and associated memory.
 52 */
 53 void splinesurf3_free(Splinesurf3 * ss)
 54 {
 55     if (ss == NULL)
 56         return;
 57     splinesurf_free(ss->x);
 58     splinesurf_free(ss->y);
 59     splinesurf_free(ss->z);
 60     rfree(ss);
 61 }
 62 
 63 /*
 64 Return point on a 3D-surface spline
 65 */
 66 Vec3 splinesurf3_eval(Splinesurf3 * ss, double s, double t)
 67 {
 68     double x = splinesurf_eval(ss->x, s, t);
 69     double y = splinesurf_eval(ss->y, s, t);
 70     double z = splinesurf_eval(ss->z, s, t);
 71     return (vec3(x, y, z));
 72 }
 73 
 74 Vec3 splinesurf3_eval_du(Splinesurf3 * ss, double s, double t)
 75 {
 76     double dx = splinesurf_eval_du(ss->x, s, t);
 77     double dy = splinesurf_eval_du(ss->y, s, t);
 78     double dz = splinesurf_eval_du(ss->z, s, t);
 79     return (vec3(dx, dy, dz));
 80 }
 81 
 82 Vec3 splinesurf3_eval_dv(Splinesurf3 * ss, double s, double t)
 83 {
 84     double dx = splinesurf_eval_dv(ss->x, s, t);
 85     double dy = splinesurf_eval_dv(ss->y, s, t);
 86     double dz = splinesurf_eval_dv(ss->z, s, t);
 87     return (vec3(dx, dy, dz));
 88 }
 89 
 90 Vec3 splinesurf3_eval_normal(Splinesurf3 * ss, double s, double t)
 91 {
 92     Vec3 du = splinesurf3_eval_du(ss, s, t);
 93     Vec3 dv = splinesurf3_eval_dv(ss, s, t);
 94     return(vec3_unitcross(du, dv));
 95 }
 96 
 97 Vec3 splinesurf3_control(Splinesurf3 *ss, int u, int v)
 98 {
 99     return(vec3(ss->x->p[u][v], ss->y->p[u][v], ss->z->p[u][v]));
100 }
101 
102 void splinesurf3_interpolate(Splinesurf3 *ss, Vec3 **points)
103 {
104     int u, v, nu = ss->nu, nv = ss->nv;
105     double **data = tarray_alloc(0, 0, ss->nu, ss->nv, double);
106 
107     for(u = 0; u < nu; u++)
108         for(v = 0; v < nv; v++)
109             data[u][v] = vec3_x(points[u][v]);
110     splinesurf_interpolate(ss->x, data);
111 
112     for(u = 0; u < nu; u++)
113         for(v = 0; v < nv; v++)
114             data[u][v] = vec3_y(points[u][v]);
115     splinesurf_interpolate(ss->y, data);
116 
117     for(u = 0; u < nu; u++)
118         for(v = 0; v < nv; v++)
119             data[u][v] = vec3_z(points[u][v]);
120     splinesurf_interpolate(ss->z, data);
121     tarray_free(data, 0, 0, nu, nv, double);
122 }
123 
124 void splinesurf3_print(FILE *fp, Splinesurf3 *ss)
125 {
126     int u, v;
127     double **x = ss->x->p;
128     double **y = ss->y->p;
129     double **z = ss->z->p;
130     fprintf(fp, "%d %d %d %d\n", ss->utype, ss->vtype, ss->nu, ss->nv);
131     for(u = -1; u < ss->nu+1; u++)
132     {
133         for(v = -1; v < ss->nv+1; v++)
134             fprintf(fp, "%12.6f%12.6f%12.6f\n", x[u][v], y[u][v], z[u][v]);
135     fprintf(fp, "\n");
136     }
137 }
138 
139 Splinesurf3 *splinesurf3_read(FILE *fp)
140 {
141     int u, v, utype, vtype, nu, nv;
142     Splinesurf3 *ss;
143     double **x, **y, **z;
144     if(fscanf(fp, "%d %d %d %d\n", &utype, &vtype, &nu, &nv) != 4)
145         return(NULL);
146     ss = splinesurf3_make(utype, vtype, nu, nv);
147     x = ss->x->p; y = ss->y->p; z = ss->z->p;
148     for(u = -1; u < nu+1; u++)
149     {
150         for(v = -1; v < nv+1; v++)
151             fscanf(fp, "%lf%lf%lf", &x[u][v], &y[u][v], &z[u][v]);
152         x[u][nv+1] = x[u][1];
153         y[u][nv+1] = y[u][1];
154         z[u][nv+1] = z[u][1];
155     }
156 /* memory overwriting fixed NAT 27/2/2000 */
157     for(v = -1; v < nv+2; v++)
158     {
159         x[nu+1][v] = x[1][v];
160         y[nu+1][v] = y[1][v];
161         z[nu+1][v] = z[1][v];
162     }
163     return(ss);
164 }
165 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.