home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / raytrace / dbw_render / source / c / MTH < prev    next >
Encoding:
Text File  |  1992-10-23  |  4.7 KB  |  191 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                 Copyright (c) 1987, David B. Wecker                  *
  4.  *                       All Rights Reserved                            *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  
  24. #define MODULE_MATH
  25. #include "ray.h"
  26.  
  27. void veczero(v)          /* return the zero vector */
  28. vector v;
  29. {
  30.      v[0] = 0.0;
  31.      v[1] = 0.0;
  32.      v[2] = 0.0;
  33. }
  34.  
  35. void veccopy(from,to)    /* copy vector 'from' into vector 'to' */
  36. vector from,to;
  37. {
  38.      to[0] = from[0];
  39.      to[1] = from[1];
  40.      to[2] = from[2];
  41. }
  42.  
  43. void vecdump(v,str)      /* diagnostic vector printout */
  44. vector v;
  45. char *str;
  46. {
  47.      int i;
  48.  
  49.      printf("%s\t",str);
  50.      for (i = 0; i < 3; i++)
  51.           printf("%15.8f ",v[i]);
  52.      printf("\n");
  53. }
  54.  
  55. float hlsvalue(n1,n2,hue)
  56. float n1,n2,hue;
  57. {
  58.      while (hue >= 360)
  59.           hue -= 360;
  60.      while (hue < 0)
  61.           hue += 360;
  62.      if (hue < 60)
  63.           return n1 + ((n2 - n1) * hue / 60);
  64.      if (hue < 180)
  65.           return n2;
  66.      if (hue < 240)
  67.           return n1 + ((n2 - n1) * (240 - hue) / 60);
  68.      return n1;
  69. }
  70.  
  71. /* see also: macro CV */
  72. void cv(x,y,z,v)    /* convert cartesian position to vector */
  73. float   x,y,z;
  74. vector         v;
  75. {
  76.      v[0] = x;
  77.      v[1] = y;
  78.      v[2] = z;
  79. }
  80.  
  81. void hls(h,l,s,v)
  82. float h,l,s;
  83. vector v;
  84. {
  85.      float m1,m2;
  86.  
  87.      if (s == 0) 
  88.      {
  89.           CV(l,l,l,v);   /* saturation 0, colorvector luminance only */
  90.      }
  91.      else                /* we have color saturation, compute hls    */
  92.      {
  93.           m2 = (l <= .5) ? l * (1 + s) : l * (1 - s) + s;
  94.           m1 = 2 * l - m2;
  95.           CV(  hlsvalue(m1,m2,h - 120),
  96.             hlsvalue(m1,m2,h),
  97.             hlsvalue(m1,m2,h + 120),
  98.             v);
  99.      }
  100. }
  101.  
  102. void vecsub(v1,v2,r)          /* vector r = vector 1 - vector 2 */
  103. vector v1,v2,r;
  104. {
  105.      r[0] = v1[0] - v2[0];
  106.      r[1] = v1[1] - v2[1];
  107.      r[2] = v1[2] - v2[2];
  108. }
  109.  
  110. void vecsum(v1,v2,r)          /* vector r = vector 1 + vector 2 */
  111. vector v1,v2,r;
  112. {
  113.      r[0] = v1[0] + v2[0];
  114.      r[1] = v1[1] + v2[1];
  115.      r[2] = v1[2] + v2[2];
  116. }
  117.  
  118. void vecmul(v1,v2,r)          /* vector r = vector 1 * vector 2 */
  119. vector v1,v2,r;
  120. {
  121.      r[0] = v1[0] * v2[0];
  122.      r[1] = v1[1] * v2[1];
  123.      r[2] = v1[2] * v2[2];
  124. }
  125.  
  126. void vecdiv(v1,v2,r)          /* vector r = vector 1 / vector 2 */
  127. vector v1,v2,r;
  128. {
  129.      r[0] = v1[0] / v2[0];
  130.      r[1] = v1[1] / v2[1];
  131.      r[2] = v1[2] / v2[2];
  132. }
  133.  
  134. void vecscale(s,v,r)          /* vector r = vector 1 * scalar   */
  135. float s;
  136. vector v,r;
  137. {
  138.      r[0] = s * v[0];
  139.      r[1] = s * v[1];
  140.      r[2] = s * v[2];
  141. }
  142.  
  143. /* see also: macro NORM */
  144. float norm(v)                 /* scalar = |v| */
  145. vector v;
  146. {
  147.      return (float)sqrt((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));
  148. }
  149.  
  150. void normalize(v)             /* return normalized (unit) vector */
  151. vector v;
  152. {
  153.      float     n;
  154.  
  155.      n = 1.0 / NORM(v);
  156.      vecscale(n,v,v);
  157. }
  158.  
  159. float dot(v1,v2)
  160. vector v1,v2;
  161. {
  162.      float result;
  163.  
  164.      result  = v1[0] * v2[0];
  165.      result += v1[1] * v2[1];
  166.      result += v1[2] * v2[2];
  167.  
  168.      return result;
  169. }
  170.  
  171. void cross(v1,v2,r)
  172. vector v1,v2,r;
  173. {
  174.      r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);
  175.      r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);
  176.      r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);
  177. }
  178.  
  179. void direction(f,t,d)    /* return unit direction vector f->t */
  180. vector     f,t,d;
  181. {
  182.      VECSUB(t,f,d);
  183.      normalize(d);
  184. }
  185.  
  186. long curstack()          /* return stack position */
  187. {
  188.      char    dummy[5];
  189.      return((long)(dummy));
  190. }
  191.