MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Vec.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2013 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 #include <math.h>
10 #include <cassert>
11 #include "Vec.h"
12 #include "../basecode/doubleEq.h"
13 
14 Vec::Vec( double a0, double a1, double a2 )
15  : a0_( a0 ), a1_( a1 ), a2_( a2 )
16 {;}
17 
18 double Vec::length() const {
19  return sqrt( a0_*a0_ + a1_*a1_ + a2_*a2_ );
20 }
21 
22 double Vec::dotProduct( const Vec& other ) const {
23  return a0_ * other.a0_ + a1_ * other.a1_ + a2_ * other.a2_;
24 }
25 
26 Vec Vec::crossProduct( const Vec& other ) const {
27  double c0 = a1_ * other.a2_ - a2_ * other.a1_;
28  double c1 = a2_ * other.a0_ - a0_ * other.a2_;
29  double c2 = a0_ * other.a1_ - a1_ * other.a0_;
30  return Vec( c0, c1, c2 );
31 }
32 
34  double len = length();
35  assert( len > 0.0 );
36  a0_ /= len;
37  a1_ /= len;
38  a2_ /= len;
39 }
40 
41 void Vec::orthogonalAxes( Vec& u, Vec& v ) const {
42  assert( !doubleEq( length(), 0 ) );
43  if ( doubleApprox( a1_, 0 ) && doubleApprox( a2_, 0 ) ) {
44  u = crossProduct( Vec( a0_, a1_, a2_ - a0_ ) );
45  } else {
46  u = crossProduct( Vec( a0_ + a1_ + a2_, a1_, a2_ ) );
47  }
48  v = crossProduct( u );
49  u.unitLength();
50  v.unitLength();
51 }
52 
53 Vec Vec::pointOnLine( const Vec& end, double k )
54 {
55  return Vec(
56  a0_ + k * ( end.a0_ - a0_ ),
57  a1_ + k * ( end.a1_ - a1_ ),
58  a2_ + k * ( end.a2_ - a2_ ) );
59 }
60 
61 bool Vec::operator==( const Vec& other ) const {
62  return
63  doubleEq( a0_, other.a0_) &&
64  doubleEq( a1_, other.a1_) &&
65  doubleEq( a2_, other.a2_);
66 }
67 
68 Vec Vec::operator-( const Vec& other ) const {
69  return Vec( a0_ - other.a0_, a1_ - other.a1_, a2_ - other.a2_ );
70 }
71 
72 Vec Vec::operator+( const Vec& other ) const {
73  return Vec( a0_ + other.a0_, a1_ + other.a1_, a2_ + other.a2_ );
74 }
75 
76 Vec Vec::operator*( const double& other ) const {
77  return Vec( a0_ * other, a1_ * other, a2_ * other );
78 }
79 
80 
81 double Vec::distance( const Vec& other ) const
82 {
83  return ( *this - other ).length();
84 }
Vec operator*(const double &other) const
Definition: Vec.cpp:76
double a0_
Definition: Vec.h:60
double a2_
Definition: Vec.h:62
Vec operator+(const Vec &other) const
Definition: Vec.cpp:72
Vec crossProduct(const Vec &other) const
Definition: Vec.cpp:26
Vec operator-(const Vec &other) const
Definition: Vec.cpp:68
double a1_
Definition: Vec.h:61
bool doubleApprox(double x, double y)
Definition: doubleEq.cpp:24
bool operator==(const Vec &other) const
Definition: Vec.cpp:61
bool doubleEq(double x, double y)
Definition: doubleEq.cpp:16
Definition: Vec.h:13
void unitLength()
Rescales vector so it has unit length.
Definition: Vec.cpp:33
double length() const
Definition: Vec.cpp:18
Vec()
Definition: Vec.h:16
double distance(const Vec &other) const
Definition: Vec.cpp:81
double dotProduct(const Vec &other) const
Definition: Vec.cpp:22
void orthogonalAxes(Vec &u, Vec &v) const
Generates vectors u and v to form a mutually orthogonal system.
Definition: Vec.cpp:41
Vec pointOnLine(const Vec &end, double k)
Definition: Vec.cpp:53