home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- //////////////////////////////////////////////////////////////////////
- // MiscMath.c++ - miscellaneous math routines
- //////////////////////////////////////////////////////////////////////
-
- #include "MiscMath.h"
-
- // Check equality within range of EPSILON
- Boolean equal(const float f1, const float f2)
- {
- if (f2 > f1)
- return ((f2 - f1) < EPSILON);
- else
- return ((f1 - f2) < EPSILON);
- }
-
-
- // Returns the legth of v1 which is parallel to v2
- // In other words, projects v1 onto v2
- float parallel_length(const SbVec3f v1, const SbVec3f v2)
- {
- // the dot product of the two is |v1||v2|cos(angle b/w them)
- return v1.dot(v2)/v2.length();
- }
-
-
- // Returns the vector formed by projectiong v1 onto v2. The new
- // vector will have the same direction as v2
- SbVec3f project(const SbVec3f v1, const SbVec3f v2)
- {
- SbVec3f uv2 = v2;
- uv2.normalize();
-
- return (parallel_length(v1,uv2)*uv2);
- }
-
-
- // returns the angle (radians) between the two (not necessarily unit) vectors
- float angle_between(const SbVec3f v1, const SbVec3f v2)
- {
- float l1 = v1.length();
- float l2 = v2.length();
-
- if ((l1 == 0.0) || (l2 == 0.0))
- return 0.0;
- else
- // the dot product of the two is |v1||v2|cos(angle b/w them)
- return facos(v1.dot(v2)/(v1.length()*v2.length()));
- }
-
- // Returns the distance from the point to the vector. The vector has its
- // start at origin.
- float point_vec_distance(
- SbVec3f point, SbVec3f vector, SbVec3f origin)
- {
- SbVec3f point_vec = point - origin;
- float angle = angle_between(vector, point_vec);
- /*
- printf("point = "); print_vec(point);
- printf("origin = "); print_vec(origin);
- printf("vector = "); print_vec(vector);
- printf("p_vec = "); print_vec(point_vec);
- printf("angle = %f\n", angle*180.0/M_PI);
- printf("result = %f\n", (fsin(angle)*point_vec.length()));
- */
- return (fsin(angle)*point_vec.length());
- }
-
-
- // are two points on the same side of a line
- // straight outta Sedgewick's Algorithms
- // XXX Assuming X-Z plane.
- Boolean same_side(SbVec3f p1, SbVec3f p2, SbLine line)
- {
- float dx, dz, dx1, dx2, dz1, dz2;
-
- dx = line.getDirection()[0];
- dz = line.getDirection()[2];
-
- dx1 = p1[0] - line.getPosition()[0];
- dz1 = p1[2] - line.getPosition()[2];
-
- dx2 = p2[0] - (line.getPosition() + line.getDirection())[0];
- dz2 = p2[2] - (line.getPosition() + line.getDirection())[2];
-
- return (((dx*dz1 - dz*dx1)*(dx*dz2 - dz*dx2)) > 0.0);
- }
-
- // Is a point within a convex polygon?
- // The points of the quat should be ordered counter-clockwise.
- // XXX Only looks in X-Z plane.
- Boolean point_within(SbVec3f poly[], int num_verts, SbVec3f point)
- {
- int i = 0;
-
- SbVec3f middle_point(0,0,0);
- for (i = 0; i < num_verts; i++)
- middle_point += poly[i];
-
- middle_point /= num_verts;
-
- i = 0;
- while(i < num_verts)
- {
- SbLine line(poly[i], poly[(i+1)%num_verts]);
- if (! same_side(point, middle_point, line))
- return FALSE;
- i++;
- }
-
- return TRUE;
- }
-
-
-