home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / MiscMath.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.5 KB  |  130 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. //////////////////////////////////////////////////////////////////////
  18. // MiscMath.c++ - miscellaneous math routines
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21. #include "MiscMath.h"
  22.  
  23. // Check equality within range of EPSILON
  24. Boolean equal(const float f1, const float f2)
  25. {
  26.     if (f2 > f1)
  27.         return ((f2 - f1) < EPSILON);
  28.     else
  29.         return ((f1 - f2) < EPSILON);
  30. }
  31.  
  32.  
  33. // Returns the legth of v1 which is parallel to v2
  34. // In other words, projects v1 onto v2
  35. float parallel_length(const SbVec3f v1, const SbVec3f v2)
  36. {
  37.     // the dot product of the two is |v1||v2|cos(angle b/w them)
  38.     return v1.dot(v2)/v2.length();
  39. }
  40.  
  41.  
  42. // Returns the vector formed by projectiong v1 onto v2. The new
  43. // vector will have the same direction as v2
  44. SbVec3f project(const SbVec3f v1, const SbVec3f v2)
  45. {
  46.     SbVec3f uv2 = v2;
  47.     uv2.normalize();
  48.     
  49.     return (parallel_length(v1,uv2)*uv2);
  50. }
  51.  
  52.  
  53. // returns the angle (radians) between the two (not necessarily unit) vectors
  54. float angle_between(const SbVec3f v1, const SbVec3f v2)
  55. {
  56.     float l1 = v1.length();
  57.     float l2 = v2.length();
  58.  
  59.     if ((l1 == 0.0) || (l2 == 0.0))
  60.         return 0.0;
  61.     else
  62.         // the dot product of the two is |v1||v2|cos(angle b/w them)
  63.         return facos(v1.dot(v2)/(v1.length()*v2.length()));
  64. }
  65.  
  66. // Returns the distance from the point to the vector. The vector has its
  67. // start at origin.
  68. float point_vec_distance(
  69.     SbVec3f point, SbVec3f vector, SbVec3f origin)
  70. {
  71.     SbVec3f point_vec = point - origin;
  72.     float angle = angle_between(vector, point_vec);
  73. /*    
  74. printf("point  = "); print_vec(point);
  75. printf("origin = "); print_vec(origin);
  76. printf("vector = "); print_vec(vector);
  77. printf("p_vec  = "); print_vec(point_vec);
  78. printf("angle  = %f\n", angle*180.0/M_PI);
  79. printf("result  = %f\n", (fsin(angle)*point_vec.length()));
  80. */
  81.     return (fsin(angle)*point_vec.length());
  82. }
  83.  
  84.  
  85. // are two points on the same side of a line
  86. // straight outta Sedgewick's Algorithms
  87. // XXX Assuming X-Z plane.
  88. Boolean same_side(SbVec3f p1, SbVec3f p2, SbLine line)
  89. {
  90.     float dx, dz, dx1, dx2, dz1, dz2;
  91.  
  92.     dx = line.getDirection()[0];
  93.     dz = line.getDirection()[2];
  94.     
  95.     dx1 = p1[0] - line.getPosition()[0];
  96.     dz1 = p1[2] - line.getPosition()[2];
  97.  
  98.     dx2 = p2[0] - (line.getPosition() + line.getDirection())[0];
  99.     dz2 = p2[2] - (line.getPosition() + line.getDirection())[2];
  100.  
  101.     return (((dx*dz1 - dz*dx1)*(dx*dz2 - dz*dx2)) > 0.0);
  102. }
  103.  
  104. // Is a point within a convex polygon?
  105. // The points of the quat should be ordered counter-clockwise.
  106. // XXX Only looks in X-Z plane.
  107. Boolean point_within(SbVec3f poly[], int num_verts, SbVec3f point)
  108. {
  109.     int i = 0;
  110.  
  111.     SbVec3f middle_point(0,0,0);
  112.     for (i = 0; i < num_verts; i++)
  113.         middle_point += poly[i];
  114.  
  115.     middle_point /= num_verts;
  116.     
  117.     i = 0;
  118.     while(i < num_verts)
  119.     {
  120.         SbLine line(poly[i], poly[(i+1)%num_verts]);
  121.         if (! same_side(point, middle_point, line))
  122.             return FALSE;
  123.         i++;
  124.     }
  125.  
  126.     return TRUE;
  127. }
  128.  
  129.  
  130.