User Manual, Developers Guide and API Documentation

Point.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 <WNS/Position.hpp>
00029 #include <stdexcept>
00030 
00031 using namespace std;
00032 using namespace wns::geometry;
00033 
00034 Point::Point() :
00035     point(valarray<double>(0.0,3))
00036 {
00037 }
00038 
00039 Point::Point(double x, double y, double z) :
00040     point(valarray<double>(0.0,3))
00041 {
00042     if (x < 0.0 || y < 0.0 || z < 0.0) {
00043         throw out_of_range("x, y, z must be > 0.0!");
00044     }
00045     point[0] = x;
00046     point[1] = y;
00047     point[2] = z;
00048 }
00049 
00050 Point::Point(const valarray<double>& other) :
00051     point(other)
00052 {
00053 }
00054 
00055 Point::Point(const wns::pyconfig::View& view) :
00056     point(valarray<double>(0.0,3))
00057 {
00058     point[0] = view.get<double>("x");
00059     point[1] = view.get<double>("y");
00060     point[2] = view.get<double>("z");
00061     if (point[0] < 0.0 || point[1] < 0.0 || point[2] < 0.0) {
00062         throw out_of_range("x, y, z must be > 0.0!");
00063     }
00064 }
00065 
00066 Point::~Point()
00067 {
00068 }
00069 
00070 double
00071 Point::getX() const
00072 {
00073     return point[0];
00074 }
00075 
00076 double
00077 Point::getY() const
00078 {
00079     return point[1];
00080 }
00081 
00082 double
00083 Point::getZ() const
00084 {
00085     return point[2];
00086 }
00087 
00088 const std::valarray<double>&
00089 Point::get() const
00090 {
00091     return point;
00092 }
00093 
00094 void
00095 Point::set(double x, double y, double z)
00096 {
00097     point[0] = x;
00098     point[1] = y;
00099     point[2] = z;
00100 }
00101 
00102 void
00103 Point::setPolar(double r, double phi, double theta)
00104 {
00105     point[0] = r * sin(theta) * cos(phi);
00106     point[1] = r * sin(theta) * sin(phi);
00107     point[2] = r * cos(theta);
00108 }
00109 
00110 void
00111 Point::set(const valarray<double>& other)
00112 {
00113     point = other;
00114 }
00115 
00116 
00117 void
00118 Point::setX(double x)
00119 {
00120     // in Antenna::drawAntennaPattern() it is ok to have negative coordinates
00121     point[0] = x;
00122     if (x < 0.0) {
00123         throw out_of_range("x must be > 0.0!");
00124     }
00125 }
00126 
00127 void
00128 Point::setY(double y)
00129 {
00130     // in Antenna::drawAntennaPattern() it is ok to have negative coordinates
00131     point[1] = y;
00132     if (y < 0.0) {
00133         throw out_of_range("y must be > 0.0!");
00134     }
00135 }
00136 
00137 void
00138 Point::setZ(double z)
00139 {
00140     // in Antenna::drawAntennaPattern() it is ok to have negative coordinates
00141     point[2] = z;
00142     if (z < 0.0) {
00143         throw out_of_range("z must be > 0.0!");
00144     }
00145 }
00146 
00147 Point
00148 Point::operator+(const Vector& v) const
00149 {
00150     return Point(get()+v.get());
00151 }
00152 
00153 void
00154 Point::operator+=(const Vector& v)
00155 {
00156     point += v.get();
00157 }
00158 
00159 Vector
00160 Point::operator-(const Point& other) const
00161 {
00162     return Vector(*this, other);
00163 }
00164 
00165 void
00166 Point::operator=(const Point& other)
00167 {
00168     point = other.point;
00169 }
00170 
00171 bool
00172 Point::operator==(const Point &other) const
00173 {
00174     return
00175         get()[0] == other.get()[0] &&
00176         get()[1] == other.get()[1] &&
00177         get()[2] == other.get()[2];
00178 }
00179 
00180 bool
00181 Point::operator!=(const Point &other) const
00182 {
00183     return !(*this == other);
00184 }
00185 
00186 std::ostream&
00187 operator<<(std::ostream &str, const Point& p)
00188 {
00189     str
00190         << "[ x=" << p.getX()
00191         << ", y=" << p.getY()
00192         << ", z=" << p.getZ() << "]";
00193     return str;
00194 }
00195 
00196 bool
00197 Point::operator<(const Point& other) const
00198 {
00199     // This is a valid definition of a comparision criterion that achieves
00200     // "strict weak ordering" as needed by the STL:
00201 
00202     return (get()[0] < other.get()[0]) ||
00203            ((get()[0] == other.get()[0]) && (get()[1] < other.get()[1]) ||
00204             ((get()[1] == other.get()[1]) && get()[2] < other.get()[2]));
00205 
00206 
00207 // Alternative using boost tuples:
00208 
00209 //#include <boost/tuple/tuple.hpp>
00210 //#include <boost/tuple/tuple_comparison.hpp>
00211 
00212 //  return boost::tie(get()[0], get()[1], get()[2]) < boost::tie(other.get()[0], other.get()[1], other.get()[2]);
00213 }
00214 

Generated on Fri May 25 03:31:39 2012 for openWNS by  doxygen 1.5.5