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

Linux Cross Reference
Tina5/tina-tools/tinatool/draw/drawTv_zutil.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_zutil.c,v $
 36  * Date    :  $Date: 2007/02/15 01:55:50 $
 37  * Version :  $Revision: 1.3 $
 38  * CVS Id  :  $Id: drawTv_zutil.c,v 1.3 2007/02/15 01:55:50 paul Exp $
 39  *
 40  * Author  : Legacy TINA
 41  *
 42  * Notes : Basic zbuff utilities:
 43  *   default color & shading functions
 44  *   buffer allocation
 45  *   visibility testing
 46  *
 47  *********
 48 */
 49 
 50 #include "drawTv_zutil.h"
 51 
 52 #if HAVE_CONFIG_H
 53   #include <config.h>
 54 #endif
 55 
 56 #include <math.h>
 57 #include <limits.h>
 58 #include <tina/sys/sysDef.h>
 59 #include <tina/sys/sysPro.h>
 60 #include <tina/math/mathDef.h>
 61 #include <tina/math/mathPro.h>
 62 #include <tina/geometry/geomDef.h>
 63 #include <tina/geometry/geomPro.h>
 64 #include <tinatool/draw/draw_TvDef.h>
 65 #include <tinatool/draw/draw_TvPro.h>
 66 #include <tina/image/imgGen_alloc.h>
 67 
 68 /* sets color function: this returns Tina color number to be associated
 69  * with intensity 0 < g < 1 */
 70 void    tv_zbuff_color_set(Tv * tv, int (*color) ())
 71 {
 72     if (tv == NULL || tv->zbuff == NULL)
 73         return;
 74     tv->zbuff->color = color;
 75 }
 76 
 77 /* default color function: */
 78 int     grey_color(double g)
 79 {
 80     return ((int) (255.0 * g));
 81 }
 82 
 83 /* sets shade function: this returns intensity at point and normal */
 84 void    tv_zbuff_shade_set(Tv * tv, double (*func) ())
 85 {
 86     if (tv == NULL || tv->zbuff == NULL)
 87         return;
 88     tv->zbuff->shade = func;
 89 }
 90 
 91 /* default shade function */
 92 /* ARGSUSED quieten lint */
 93 double  spec_shade(Tv * tv, Vec3 n, Vec3 x)
 94 {
 95     double  c = vec3_dot(tv->ez3, n);
 96     double  gl;
 97 
 98     gl = 0.2+0.4 * fabs(c) + 0.4 * c * c * c * c;
 99     gl = MIN(gl, 1.0);
100     return (gl);
101 }
102 
103 /* shade inside darker function */
104 /* ARGSUSED quieten lint */
105 double  spec_shade_inside_1(Tv * tv, Vec3 n, Vec3 x)
106 {
107     double  c = vec3_dot(tv->ez3, n);
108     double  gl;
109 
110     if (c >= 0.0)
111         gl = 0.3 * fabs(c) + 0.3 * c * c * c * c;
112     else
113         gl = 0.4 + 0.3 * fabs(c) + 0.3 * c * c * c * c;
114     gl = MIN(gl, 1.0);
115     return (gl);
116 }
117 
118 /* shade inside darker function */
119 /* ARGSUSED quieten lint */
120 double  spec_shade_inside_2(Tv * tv, Vec3 n, Vec3 x)
121 {
122     double  c = vec3_dot(tv->ez3, n);
123     double  gl;
124 
125     if (c < 0.0)
126         gl = 0.3 * fabs(c) + 0.3 * c * c * c * c;
127     else
128         gl = 0.4 + 0.3 * fabs(c) + 0.3 * c * c * c * c;
129     gl = MIN(gl, 1.0);
130     return (gl);
131 }
132 
133 Tv_zbuff *zbuff_alloc(int height, int width, double zmin, double zmax)
134 {
135     int     i, j;
136     Tv_zbuff *zbuff = ts_ralloc(Tv_zbuff);
137     Imrect *z;
138 
139     z = zbuff->z = im_alloc(height, width, (Imregion *) NULL, int_v);
140     for (i = 0; i < height; ++i)
141         for (j = 0; j < width; ++j)
142             IM_INT(z, i, j) = INT_MAX;
143 
144     zbuff->zmin = zmin;
145     zbuff->zmax = zmax;
146     zbuff->color = grey_color;
147     zbuff->shade = spec_shade;
148     zbuff->backdraw = true;
149     return (zbuff);
150 }
151 
152 void    zbuff_free(Tv_zbuff * zbuff)
153 {
154     if (zbuff == NULL)
155         return;
156     im_free(zbuff->z);
157     rfree((void *) zbuff);
158 }
159 
160 /* give tv a z-buffer z-range zmin to zmax */
161 void    tv_set_zbuff(Tv * tv, double zmin, double zmax)
162 {
163     if (tv == NULL)
164         return;
165     zbuff_free(tv->zbuff);
166     tv->zbuff = zbuff_alloc(tv->height, tv->width, zmin, zmax);
167 }
168 
169 void    tv_free_zbuff(Tv * tv)
170 {
171     if (tv == NULL)
172         return;
173     zbuff_free(tv->zbuff);
174     tv->zbuff = NULL;
175 }
176 
177 /* clear depths from z-buffer */
178 void    tv_clear_zbuff(Tv * tv)
179 {
180     int     i, j;
181     Imrect *z;
182 
183     if (tv == NULL || tv->zbuff == NULL || (z = tv->zbuff->z) == NULL)
184         return;
185     for (i = 0; i < z->height; ++i)
186         for (j = 0; j < z->width; ++j)
187             IM_INT(z, i, j) = INT_MAX;
188 }
189 
190 /* whether to draw backs of surfaces (intensities < 0) */
191 void    tv_zbuff_backdraw_set(Tv * tv, Bool flag)
192 {
193     if (tv == NULL || tv->zbuff == NULL)
194         return;
195     tv->zbuff->backdraw = flag;
196 }
197 
198 /* z-buffer without drawing whether point is visible */
199 Bool    tv_zbuff(Tv * tv, Ipos pos, int z)
200 {
201     int     z0;
202     Imrect *zbuff = tv->zbuff->z;
203     int     x = ipos_x(pos);
204     int     y = ipos_y(pos);
205 
206     if (!IN_TV(tv, pos))
207         return (false);
208     z0 = IM_INT(zbuff, y, x);
209     if (z < z0)
210     {
211         IM_INT(zbuff, y, x) = z;
212         return (true);
213     } else
214         return (false);
215 }
216 

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