home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / lib / vect3d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.8 KB  |  198 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /* Routines to manipulate 3 dimensional vectors.  All these routines
  38.  * should work even if the input and output vectors are the same.
  39.  */
  40.  
  41. #include <stdio.h>
  42. #include <math.h>
  43. #include <GL/gl.h>
  44. #include "3d.h"
  45.  
  46. void (*errfunc)(char *) = 0;
  47.  
  48. void seterrorfunc(void (*func)(char *))
  49. {
  50.     errfunc = func;
  51. }
  52.  
  53. void error(char *s)
  54. {
  55.     if (errfunc)
  56.     (*errfunc)(s);
  57.     else {
  58.     fprintf(stderr, s); 
  59.     fprintf(stderr, "\n");
  60.     exit(1);
  61.     }
  62. }
  63.  
  64. void diff3(GLdouble p[3], GLdouble q[3], GLdouble diff[3])
  65. {
  66.     diff[0] = p[0] - q[0];
  67.     diff[1] = p[1] - q[1];
  68.     diff[2] = p[2] - q[2];
  69. }
  70.  
  71. void add3(GLdouble p[3], GLdouble q[3], GLdouble sum[3])
  72. {
  73.     sum[0] = p[0] + q[0];
  74.     sum[1] = p[1] + q[1];
  75.     sum[2] = p[2] + q[2];
  76. }
  77.  
  78. void scalarmult(GLdouble s, GLdouble v[3], GLdouble vout[3])
  79. {
  80.     vout[0] = v[0]*s;
  81.     vout[1] = v[1]*s;
  82.     vout[2] = v[2]*s;
  83. }
  84.  
  85. GLdouble dot3(GLdouble p[3], GLdouble q[3])
  86. {
  87.     return p[0]*q[0] + p[1]*q[1] + p[2]*q[2];
  88. }
  89.  
  90. GLdouble length3(GLdouble v[3])
  91. {
  92.     return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  93. }
  94.  
  95. GLdouble dist3(GLdouble p[3], GLdouble q[3])
  96. {
  97.     GLdouble d[3];
  98.  
  99.     diff3(p, q, d);
  100.     return length3(d);
  101. }
  102.  
  103. void copy3(GLdouble old[3], GLdouble new[3])
  104. {
  105.     new[0] = old[0], new[1] = old[1], new[2] = old[2];
  106. }
  107.  
  108. void crossprod(GLdouble v1[3], GLdouble v2[3], GLdouble prod[3])
  109. {
  110.     GLdouble p[3];    /* in case prod == v1 or v2 */
  111.  
  112.     p[0] = v1[1]*v2[2] - v2[1]*v1[2];
  113.     p[1] = v1[2]*v2[0] - v2[2]*v1[0];
  114.     p[2] = v1[0]*v2[1] - v2[0]*v1[1];
  115.     prod[0] = p[0]; prod[1] = p[1]; prod[2] = p[2];
  116. }
  117.  
  118. void normalize(GLdouble v[3])
  119. {
  120.     GLdouble d;
  121.  
  122.     d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
  123.     if (d == 0.0) {
  124.         error("normalize: zero length vector");
  125.     v[0] = d = 1.0;
  126.     }
  127.     d = 1/d;
  128.     v[0] *= d; v[1] *= d; v[2] *= d;
  129. }
  130.  
  131. void print3(GLdouble v[3])
  132. {
  133.     GLdouble len;
  134.  
  135.     len = length3(v);
  136.     printf("(%g %g %g); len: %g\n", v[0], v[1], v[2], len);
  137. }
  138.  
  139. void printmat3(GLdouble m[3][3])
  140. {
  141.     int i, j;
  142.  
  143.     for (i=0; i<3; i++) {
  144.     for (j=0; j<3; j++)
  145.         printf("%7.4f  ", m[i][j]);
  146.     printf("\n");
  147.     }
  148. }
  149.  
  150. void identifymat3(GLdouble m[3][3])
  151. {
  152.     int i, j;
  153.  
  154.     for (i=0; i<3; i++)
  155.     for (j=0; j<3; j++)
  156.         m[i][j] = (i == j) ? 1.0 : 0.0;
  157. }
  158.  
  159. void copymat3(GLdouble *to, GLdouble *from)
  160. {
  161.     int i;
  162.  
  163.     for (i=0; i<9; i++) {
  164.     *to++ = *from++;
  165.     }
  166. }
  167.  
  168. void xformvec3(GLdouble v[3], GLdouble m[3][3], GLdouble vm[3])
  169. {
  170.     GLdouble result[3];    /* in case v == vm */
  171.     int i;
  172.  
  173.     for (i=0; i<3; i++) {
  174.     result[i] = v[0]*m[0][i] + v[1]*m[1][i] + v[2]*m[2][i];
  175.     }
  176.     for (i=0; i<3; i++) {
  177.     vm[i] = result[i];
  178.     }
  179. }
  180.  
  181. long samepoint(GLdouble p1[3], GLdouble p2[3])
  182. {
  183.     if (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2])
  184.     return 1;
  185.     return 0;
  186. }
  187.  
  188. void perpnorm(GLdouble p1[3], GLdouble p2[3], GLdouble p3[3], GLdouble n[3])
  189. {
  190.     GLdouble d1[3], d2[3];
  191.  
  192.     diff3(p2, p1, d1);
  193.     diff3(p2, p3, d2);
  194.     crossprod(d1, d2, n);
  195.     normalize(n);
  196. }
  197.  
  198.