MOAB: Mesh Oriented datABase
(version 5.3.1)
|
00001 /*========================================================================= 00002 Original version -- Copyright (c) 2006 Sandia Corporation. 00003 All rights reserved. 00004 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00005 00006 This software is distributed WITHOUT ANY WARRANTY; without even 00007 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00008 PURPOSE. See the above copyright notice for more information. 00009 00010 =========================================================================*/ 00011 00012 #ifndef VERDICT_VECTOR 00013 #define VERDICT_VECTOR 00014 00015 #include "moab/verdict.h" 00016 #include <cmath> 00017 #include <cassert> 00018 00019 // computes the dot product of 3d vectors 00020 inline double dot_product( double vec1[], double vec2[] ) 00021 { 00022 double answer = vec1[0] * vec2[0] + 00023 vec1[1] * vec2[1] + 00024 vec1[2] * vec2[2]; 00025 return answer; 00026 } 00027 00028 // normalize a vector 00029 inline void normalize( double vec[] ) 00030 { 00031 double x = sqrt( vec[0]*vec[0] + 00032 vec[1]*vec[1] + 00033 vec[2]*vec[2] ); 00034 00035 vec[0] /= x; 00036 vec[1] /= x; 00037 vec[2] /= x; 00038 00039 } 00040 00041 // computes the cross product 00042 inline double * cross_product( double vec1[], double vec2[], double answer[] ) 00043 { 00044 answer[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; 00045 answer[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; 00046 answer[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; 00047 return answer; 00048 } 00049 00050 // computes the length of a vector 00051 inline double length ( double vec[] ) 00052 { 00053 return sqrt ( vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] ); 00054 } 00055 00056 // computes the square length of a vector 00057 inline double length_squared (double vec[] ) 00058 { 00059 return (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] ); 00060 } 00061 00062 // computes the interior angle between 2 vectors in degrees 00063 inline double interior_angle( double vec1[], double vec2[] ) 00064 { 00065 double cosAngle, angleRad; 00066 double length1 = length(vec1); 00067 double length2 = length(vec2); 00068 assert( (length1 > 0.0 && length2 > 0.0) ); 00069 00070 cosAngle = dot_product(vec1, vec2) / (length1 * length2); 00071 00072 if ((cosAngle > 1.0) && (cosAngle < 1.0001)) 00073 { 00074 cosAngle = 1.0; 00075 } 00076 else if (cosAngle < -1.0 && cosAngle > -1.0001) 00077 { 00078 cosAngle = -1.0; 00079 } 00080 else 00081 { 00082 assert(cosAngle < 1.0001 && cosAngle > -1.0001); 00083 } 00084 00085 angleRad = acos(cosAngle); 00086 return( (angleRad * 180.) / VERDICT_PI ); 00087 } 00088 00089 #endif 00090