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

Linux Cross Reference
Tina6/tina-tools/tinatool/draw/drawTv_zdraw.c

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

  1 /*********
  2  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  3  * University of Manchester, UK.  All rights reserved.
  4  * 
  5  * Redistribution and use in source and binary forms, with or without modification, 
  6  * are permitted provided that the following conditions are met:
  7  * 
  8  *   . Redistributions of source code must retain the above copyright notice, 
  9  *     this list of conditions and the following disclaimer.
 10  *    
 11  *   . Redistributions in binary form must reproduce the above copyright notice,
 12  *     this list of conditions and the following disclaimer in the documentation 
 13  *     and/or other materials provided with the distribution.
 14  * 
 15  *   . Neither the name of the University of Manchester nor the names of its
 16  *     contributors may be used to endorse or promote products derived from this 
 17  *     software without specific prior written permission.
 18  * 
 19  * 
 20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 30  * POSSIBILITY OF SUCH DAMAGE.
 31  *
 32  **********
 33  *
 34  * Program :    TINA
 35  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/draw/drawTv_zdraw.c,v $
 36  * Date    :  $Date: 2003/10/01 16:02:47 $
 37  * Version :  $Revision: 1.2 $
 38  * CVS Id  :  $Id: drawTv_zdraw.c,v 1.2 2003/10/01 16:02:47 tony Exp $
 39  *
 40  * Author  : Legacy TINA
 41  *
 42  * Notes :
 43  *
 44  *********
 45 */
 46 
 47 #include "drawTv_zdraw.h"
 48 
 49 #if HAVE_CONFIG_H
 50   #include <config.h>
 51 #endif
 52 
 53 #include <math.h>
 54 #include <tina/sys/sysDef.h>
 55 #include <tina/sys/sysPro.h>
 56 #include <tina/math/mathDef.h>
 57 #include <tina/math/mathPro.h>
 58 #include <tina/geometry/geomDef.h>
 59 #include <tina/geometry/geomPro.h>
 60 #include <tinatool/draw/draw_TvDef.h>
 61 #include <tinatool/draw/draw_TvPro.h>
 62 
 63 static double epsilon = 2.0e-6;
 64 
 65 void    tv_zbuff_epsilon_set(double eps)
 66 {
 67     epsilon = eps;
 68 }
 69 
 70 /* z-buffer draw 3D point */
 71 void    tv_zbuff_point3(Tv * tv, Vec3 p)
 72 {
 73     Ipos    pos = {Ipos_id};
 74     int     z;
 75 
 76     if (tv == NULL)
 77         return;
 78     pos = tv_zbuff_proj3(tv, p, &z);
 79     (void) tv_zbuff_point(tv, pos, z);
 80 }
 81 
 82 /* z-buffer draw flat 3D dot */
 83 void    tv_zbuff_dot3(Tv * tv, Vec3 p)
 84 {
 85     Ipos    pos = {Ipos_id};
 86     int     x, y, z;
 87 
 88     if (tv == NULL)
 89         return;
 90     pos = tv_zbuff_proj3(tv, p, &z);
 91     x = ipos_x(pos);
 92     y = ipos_y(pos);
 93     (void) tv_zbuff_point(tv, ipos(x - 1, y - 1), z);
 94     (void) tv_zbuff_point(tv, ipos(x, y - 1), z);
 95     (void) tv_zbuff_point(tv, ipos(x + 1, y - 1), z);
 96     (void) tv_zbuff_point(tv, ipos(x - 1, y), z);
 97     (void) tv_zbuff_point(tv, ipos(x, y), z);
 98     (void) tv_zbuff_point(tv, ipos(x + 1, y), z);
 99     (void) tv_zbuff_point(tv, ipos(x - 1, y + 1), z);
100     (void) tv_zbuff_point(tv, ipos(x, y + 1), z);
101     (void) tv_zbuff_point(tv, ipos(x + 1, y + 1), z);
102 }
103 
104 /* z-buffer draw parametrised 3D-curve section curve(t, data) */
105 void    tv_zbuff_curve3(Tv * tv, Vec3(*curve) (), void *data, double t1, double t2)
106 {
107     double  tm = 0.5 * (t1 + t2);
108 
109     tv_zbuff_seg3(tv, curve, data, t1, tm, false);
110     tv_zbuff_seg3(tv, curve, data, tm, t2, true);
111 }
112 
113 static Vec3 line_point(Vec3 * line, double t)
114 {
115     return (vec3_sum(vec3_times((1.0 - t), line[0]),
116                      vec3_times(t, line[1])));
117 }
118 
119 /* z-buffer draw line segment p1, p2 */
120 void    tv_zbuff_line3(Tv * tv, Vec3 p1, Vec3 p2)
121 {
122     Vec3    line[2] = {{Vec3_id}, {Vec3_id}};
123 
124     line[0] = p1;
125     line[1] = p2;
126     tv_zbuff_curve3(tv, line_point, (void *) line, 0.0, 1.0);
127 }
128 
129 /* z-buffer draw flat text string at p */
130 void    tv_zbuff_text3(Tv * tv, char *string, Vec3 p)
131 {
132     void   *mask;
133     int     x, y, xmin, xmax, ymin, ymax, z;
134     Ipos    pos = {Ipos_id};
135 
136     tv_get_text_extent(tv, string, &xmin, &xmax, &ymin, &ymax);
137     pos = tv_zbuff_proj3(tv, p, &z);
138     x = ipos_x(pos);
139     y = ipos_y(pos);
140     xmin += x;
141     ymin += y;
142     xmax += x;
143     ymax += y;
144     mask = tv_mask_get(tv, xmin, ymin, xmax, ymax);
145     for(x = xmin; x < xmax; x++)
146         for(y = ymin; y < ymax; y++)
147         {
148             pos = ipos(x, y);
149             if(tv_zbuff(tv, pos, z))
150                 tv_mask_point(tv, mask, pos, 1); 
151         }
152     tv_mask_use(tv, mask);
153     tv_text3(tv, string, p);
154     tv_mask_free(tv, mask);
155 }
156 
157 /* z-buffer draw interpolated intensities in convex 3D quadrilateral */
158 void    tv_zbuff_interp_quad3(Tv * tv, Vec3 p00, Vec3 p10, Vec3 p01, Vec3 p11, double g00, double g10, double g01, double g11)
159 {
160     Ipos    p[4] = {{Ipos_id}, {Ipos_id}, {Ipos_id}, {Ipos_id}};
161     int     z[4];
162     double  g[4];
163 
164     p[0] = tv_zbuff_proj3(tv, p00, &z[0]);
165     p[1] = tv_zbuff_proj3(tv, p10, &z[1]);
166     p[2] = tv_zbuff_proj3(tv, p11, &z[2]);
167     p[3] = tv_zbuff_proj3(tv, p01, &z[3]);
168     g[0] = g00;
169     g[1] = g10;
170     g[3] = g01;
171     g[2] = g11;
172 
173     tv_zbuff_interp_convex_poly(tv, 4, p, z, g);
174 }
175 
176 /* z-buffer draw interpolated image intensities in convex 3D
177  * quadrilateral */
178 void    tv_zbuff_image_quad3(Tv * tv, Vec3 p00, Vec3 p10, Vec3 p01, Vec3 p11, double r00, double r10, double r01, double r11, double c00, double c10, double c01, double c11, Imrect * im)
179 {
180     Ipos    p[4] = {{Ipos_id}, {Ipos_id}, {Ipos_id}, {Ipos_id}};
181     int     z[4];
182     float   r[4];
183     float   c[4];
184 
185     p[0] = tv_zbuff_proj3(tv, p00, &z[0]);
186     p[1] = tv_zbuff_proj3(tv, p10, &z[1]);
187     p[2] = tv_zbuff_proj3(tv, p11, &z[2]);
188     p[3] = tv_zbuff_proj3(tv, p01, &z[3]);
189     r[0] = (float)r00;
190     r[1] = (float)r10;
191     r[3] = (float)r01;
192     r[2] = (float)r11;
193     c[0] = (float)c00;
194     c[1] = (float)c10;
195     c[3] = (float)c01;
196     c[2] = (float)c11;
197 
198     tv_zbuff_image_convex_poly(tv, 4, p, z, r, c, im);
199 }
200 
201 /* default formula for normal */
202 Vec3    interp_normal(Vec3(*surf) (), void *data, double u, double v, Vec3 x)
203 {
204     double  delta = epsilon / 2.0;
205     Vec3    x1 = {Vec3_id};
206     Vec3    x2 = {Vec3_id};
207 
208     x1 = surf(data, u + delta, v);
209     x2 = surf(data, u, v + delta);
210     x1 = vec3_times(1.0 / delta, vec3_diff(x1, x));
211     x2 = vec3_times(1.0 / delta, vec3_diff(x2, x));
212     return (vec3_unitcross(x1, x2));
213 }
214 
215 /* z-buffer draw  surface patch (up to limit ni sub-divisions) surf(u,
216  * v) returns surface point norm(u, v) returns surface normal range
217  * [u1, u2] by [v1, v2] nu, nv reasonable subdivisions to get patch
218  * very roughly a rectangle (or may not all be drawn) ni max number of
219  * patch subdivisions */
220 void    tv_zbuff_surf3(Tv * tv, Vec3(*surf) (), Vec3(*normal) (), void *data, double u1, double v1, double u2, double v2, int nu, int nv, int ni)
221 {
222     int     i, j;
223     double  du = (u2 - u1) / nu;
224     double  dv = (v2 - v1) / nv;
225 
226     if (u2 > u1)
227     {
228         SWAP(double, u1, u2);
229         du = -du;
230     }
231     if (v2 > v1)
232     {
233         SWAP(double, v1, v2);
234         dv = -dv;
235     }
236     if (normal == NULL)
237     {
238         u1 += epsilon;
239         v1 += epsilon;
240         u2 -= epsilon;
241         v2 -= epsilon;
242     }
243     for (i = 0; i < nu; i++)
244         for (j = 0; j < nv; j++)
245         {
246             double  u = u1 + i * du;
247             double  v = v1 + j * dv;
248 
249             tv_zbuff_patch3(tv, surf, normal, data, u, v, u + du, v + dv, ni);
250         }
251 }
252 
253 /* parametrisation of ruled surface */
254 static Vec3 ruled_point(Vec3 * r, double u, double v)
255 {
256     Vec3    p1 = {Vec3_id};
257     Vec3    p2 = {Vec3_id};
258 
259     p1 = vec3_sum(vec3_times((1.0 - u), r[0]), vec3_times(u, r[1]));
260     p2 = vec3_sum(vec3_times((1.0 - u), r[2]), vec3_times(u, r[3]));
261     return (vec3_sum(vec3_times((1.0 - v), p1), vec3_times(v, p2)));
262 }
263 
264 /* z-buffer draws ruled quadrilateral */
265 void    tv_zbuff_ruled3(Tv * tv, Vec3 p11, Vec3 p21, Vec3 p12, Vec3 p22)
266 {
267     Vec3    r[4] = {{Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}};
268 
269     r[0] = p11;
270     r[1] = p21;
271     r[2] = p12;
272     r[3] = p22;
273     tv_zbuff_patch3(tv, ruled_point, (Vec3(*) ()) NULL, (void *) r,
274                     0.0, 0.0, 1.0, 1.0, 1);
275 }
276 
277 /* bi-linear normal estimate for quadrilateral, corner normals packed
278  * into r[4]->r[7] */
279 static Vec3 gouraud_normal(Vec3 * r, double u, double v)
280 {
281     Vec3    n1 = {Vec3_id};
282     Vec3    n2 = {Vec3_id};
283 
284     n1 = vec3_sum(vec3_times((1.0 - u), r[4]), vec3_times(u, r[5]));
285     n2 = vec3_sum(vec3_times((1.0 - u), r[6]), vec3_times(u, r[7]));
286     return (vec3_unit(vec3_sum(vec3_times((1.0 - v), n1), vec3_times(v, n2))));
287 }
288 
289 /* z-buffer draw gouraud shaded quadrilateral */
290 void    tv_zbuff_gouraud(Tv * tv, Vec3 p11, Vec3 p21, Vec3 p12, Vec3 p22, Vec3 n11, Vec3 n21, Vec3 n12, Vec3 n22)
291 {
292     Vec3    r[8] = {{Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}, {Vec3_id}};
293 
294     r[0] = p11;
295     r[1] = p21;
296     r[2] = p12;
297     r[3] = p22;
298     r[4] = n11;
299     r[5] = n21;
300     r[6] = n12;
301     r[7] = n22;
302     tv_zbuff_patch3(tv, ruled_point, gouraud_normal, (void *) r,
303                     0.0, 0.0, 1.0, 1.0, 1);
304 }
305 

~ [ 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.