User Manual, Developers Guide and API Documentation

Vector.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include "Point.hpp"
00029 #include "Vector.hpp"
00030 
00031 using namespace std;
00032 using namespace wns::geometry;
00033 
00034 
00035 Vector::Vector() :
00036     vec(valarray<double>(0.0,3))
00037 {
00038 }
00039 
00040 Vector::Vector(double dx, double dy, double dz) :
00041     vec(valarray<double>(0.0,3))
00042 {
00043     vec[0] = dx;
00044     vec[1] = dy;
00045     vec[2] = dz;
00046 }
00047 
00048 Vector::Vector(const valarray<double>& other) :
00049     vec(other)
00050 {
00051 }
00052 
00053 Vector::~Vector()
00054 {
00055 }
00056 
00057 void
00058 Vector::set(double dx, double dy, double dz)
00059 {
00060     vec[0] = dx;
00061     vec[1] = dy;
00062     vec[2] = dz;
00063 }
00064 
00065 void
00066 Vector::setPolar(double r, double phi, double theta)
00067 {
00068     vec[0] = r * sin(theta) * cos(phi);
00069     vec[1] = r * sin(theta) * sin(phi);
00070     vec[2] = r * cos(theta);
00071 }
00072 
00073 void
00074 Vector::set(const valarray<double>& other)
00075 {
00076     vec = other;
00077 }
00078 
00079 double
00080 Vector::getDeltaX() const
00081 {
00082     return vec[0];
00083 }
00084 
00085 double
00086 Vector::getDeltaY() const
00087 {
00088     return vec[1];
00089 }
00090 
00091 double
00092 Vector::getDeltaZ() const
00093 {
00094     return vec[2];
00095 }
00096 
00097 void
00098 Vector::setDeltaX(double x)
00099 {
00100     vec[0] = x;
00101 }
00102 
00103 void Vector::setDeltaY(double y)
00104 {
00105     vec[1] = y;
00106 }
00107 
00108 void
00109 Vector::setDeltaZ(double z)
00110 {
00111     vec[2] = z;
00112 }
00113 
00114 double
00115 Vector::getR() const
00116 {
00117     return abs();
00118 }
00119 
00120 double
00121 Vector::getAzimuth() const
00122 {
00123     return atan2(vec[1], vec[0]);
00124 }
00125 
00126 double
00127 Vector::getElevation() const
00128 {
00129     return atan2(sqrt(vec[0]*vec[0]+vec[1]*vec[1]), vec[2]);
00130 }
00131 
00132 const std::valarray<double>&
00133 Vector::get() const
00134 {
00135     return vec;
00136 }
00137 
00138 void
00139 Vector::setR(double r)
00140 {
00141     double phi = getAzimuth();
00142     double theta = getElevation();
00143 
00144     setPolar(r, phi, theta);
00145 }
00146 
00147 void
00148 Vector::setAzimuth(double phi)
00149 {
00150     double r = getR();
00151     double theta = getElevation();
00152 
00153     setPolar(r, phi, theta);
00154 }
00155 
00156 void
00157 Vector::setElevation(double theta)
00158 {
00159     double r = getR();
00160     double phi = getAzimuth();
00161 
00162     setPolar(r, phi, theta);
00163 }
00164 
00165 
00166 double
00167 Vector::getPhi() const
00168 {
00169     return getAzimuth();
00170 }
00171 
00172 double
00173 Vector::getTheta() const
00174 {
00175     return getElevation();
00176 }
00177 
00178 Vector
00179 Vector::cross(const Vector& other) const
00180 {
00181     const double x = vec[1] * other.vec[2] - vec[2] * other.vec[1];
00182     const double y = vec[2] * other.vec[0] - vec[0] * other.vec[2];
00183     const double z = vec[0] * other.vec[1] - vec[1] * other.vec[0];
00184     return Vector(x, y, z);
00185 }
00186 
00187 double
00188 Vector::dot(const Vector& other) const
00189 {
00190     return (vec*other.vec).sum();
00191 }
00192 
00193 double
00194 Vector::abs() const
00195 {
00196     return sqrt(dot(*this));
00197 }
00198 
00199 Point
00200 Vector::operator+(const Point& point) const
00201 {
00202     return Point(get()+point.get());
00203 }
00204 
00205 Vector
00206 Vector::operator+(const Vector& other) const
00207 {
00208     return Vector(get()+other.get());
00209 }
00210 
00211 Vector
00212 Vector::operator*(const int scale) const
00213 {
00214     return (*this)*double(scale);
00215 }
00216 
00217 Vector
00218 Vector::operator*(const double scale) const
00219 {
00220     Vector retval(*this);
00221     retval.vec[0] *= scale;
00222     retval.vec[1] *= scale;
00223     retval.vec[2] *= scale;
00224     return retval;
00225 }
00226 
00227 void
00228 Vector::operator=(const Vector& other)
00229 {
00230     vec = other.vec;
00231 }
00232 
00233 bool
00234 Vector::operator==(const Vector& other) const
00235 {
00236     return
00237         vec[0] == other.vec[0] &&
00238         vec[1] == other.vec[1] &&
00239         vec[2] == other.vec[2];
00240 }
00241 
00242 bool
00243 Vector::operator!=(const Vector& other) const
00244 {
00245     return !(*this == other);
00246 }
00247 
00248 std::ostream&
00249 operator<<(std::ostream &str, const Vector& other)
00250 {
00251     str
00252         << "[ dx=" << other.getDeltaX()
00253         << ", dy=" << other.getDeltaY()
00254         << ", dz=" << other.getDeltaZ() << "]";
00255     return str;
00256 }
00257 
00258 

Generated on Sat May 26 03:31:37 2012 for openWNS by  doxygen 1.5.5