Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
v_vector.h
Go to the documentation of this file.
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] + vec1[1] * vec2[1] + vec1[2] * vec2[2];
00023     return answer;
00024 }
00025 
00026 // normalize a vector
00027 inline void normalize( double vec[] )
00028 {
00029     double x = sqrt( vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] );
00030 
00031     vec[0] /= x;
00032     vec[1] /= x;
00033     vec[2] /= x;
00034 }
00035 
00036 // computes the cross product
00037 inline double* cross_product( double vec1[], double vec2[], double answer[] )
00038 {
00039     answer[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
00040     answer[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
00041     answer[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
00042     return answer;
00043 }
00044 
00045 // computes the length of a vector
00046 inline double length( double vec[] )
00047 {
00048     return sqrt( vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] );
00049 }
00050 
00051 // computes the square length of a vector
00052 inline double length_squared( double vec[] )
00053 {
00054     return ( vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] );
00055 }
00056 
00057 // computes the interior angle between 2 vectors in degrees
00058 inline double interior_angle( double vec1[], double vec2[] )
00059 {
00060     double cosAngle, angleRad;
00061     double length1 = length( vec1 );
00062     double length2 = length( vec2 );
00063     assert( ( length1 > 0.0 && length2 > 0.0 ) );
00064 
00065     cosAngle = dot_product( vec1, vec2 ) / ( length1 * length2 );
00066 
00067     if( ( cosAngle > 1.0 ) && ( cosAngle < 1.0001 ) )
00068     {
00069         cosAngle = 1.0;
00070     }
00071     else if( cosAngle < -1.0 && cosAngle > -1.0001 )
00072     {
00073         cosAngle = -1.0;
00074     }
00075     else
00076     {
00077         assert( cosAngle < 1.0001 && cosAngle > -1.0001 );
00078     }
00079 
00080     angleRad = acos( cosAngle );
00081     return ( ( angleRad * 180. ) / VERDICT_PI );
00082 }
00083 
00084 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines