Add a nice 'isNullVector' function.
[UnsignedByte.git] / src / Resource / Coordinate.cpp
blob8f5d407f3ace302b2ff9a6e0b175a17324c0e53c
1 /***************************************************************************
2 * Copyright (C) 2007 by Daniel Brody *
3 * erasnode@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "Coordinate.h"
22 #include "StringUtilities.h"
24 Coordinate Coordinate::NORTH (0, 1, 0);
25 Coordinate Coordinate::NUP (0, 1, 1);
26 Coordinate Coordinate::NDOWN (0, 1, -1);
28 Coordinate Coordinate::SOUTH (0, -1, 0);
29 Coordinate Coordinate::SUP (0, -1, 1);
30 Coordinate Coordinate::SDOWN (0, -1, -1);
32 Coordinate Coordinate::EAST (1, 0, 0);
33 Coordinate Coordinate::EUP (1, 0, 1);
34 Coordinate Coordinate::EDOWN (1, 0, -1);
36 Coordinate Coordinate::WEST (-1, 0, 0);
37 Coordinate Coordinate::WUP (-1, 0, 1);
38 Coordinate Coordinate::WDOWN (-1, 0, -1);
40 Coordinate Coordinate::NW (-1, 1, 0);
41 Coordinate Coordinate::NWUP (-1, 1, 1);
42 Coordinate Coordinate::NWDOWN (-1, 1, -1);
44 Coordinate Coordinate::NE (1, 1, 0);
45 Coordinate Coordinate::NEUP (1, 1, 1);
46 Coordinate Coordinate::NEDOWN (1, 1, -1);
48 Coordinate Coordinate::SW (-1, -1, 0);
49 Coordinate Coordinate::SWUP (-1, -1, 1);
50 Coordinate Coordinate::SWDOWN (-1, -1, -1);
52 Coordinate Coordinate::SE (1, -1, 0);
53 Coordinate Coordinate::SEUP (1, -1, 1);
54 Coordinate Coordinate::SEDOWN (1, -1, -1);
56 Coordinate Coordinate::UP (0, 0, 1);
57 Coordinate Coordinate::DOWN (0, 0, -1);
59 Coordinate::Coordinate(signed long X, signed long Y, signed long Z) :
60 m_xcoordinate(X),
61 m_ycoordinate(Y),
62 m_zcoordinate(Z)
67 Coordinate::~Coordinate()
72 Coordinate::Coordinate(const Coordinate& rhs) :
73 m_xcoordinate(rhs.getXCoordinate()),
74 m_ycoordinate(rhs.getYCoordinate()),
75 m_zcoordinate(rhs.getZCoordinate())
80 Coordinate Coordinate::operator+= (const Coordinate & rhs)
82 m_xcoordinate = getXCoordinate()+rhs.getXCoordinate();
83 m_ycoordinate = getYCoordinate()+rhs.getYCoordinate();
84 m_zcoordinate = getZCoordinate()+rhs.getZCoordinate();
86 return *this;
89 Coordinate Coordinate::operator+ (const Coordinate & rhs) const
91 return Coordinate( getXCoordinate()+rhs.getXCoordinate(),
92 getYCoordinate()+rhs.getYCoordinate(),
93 getZCoordinate()+rhs.getZCoordinate() );
96 Coordinate Coordinate::operator-= (const Coordinate & rhs)
98 m_xcoordinate = getXCoordinate()-rhs.getXCoordinate();
99 m_ycoordinate = getYCoordinate()-rhs.getYCoordinate();
100 m_zcoordinate = getZCoordinate()-rhs.getZCoordinate();
102 return *this;
105 Coordinate Coordinate::operator- (const Coordinate & rhs) const
107 return Coordinate( getXCoordinate()-rhs.getXCoordinate(),
108 getYCoordinate()-rhs.getYCoordinate(),
109 getZCoordinate()-rhs.getZCoordinate() );
112 signed long Coordinate::getXCoordinate() const
114 return m_xcoordinate;
117 signed long Coordinate::getYCoordinate() const
119 return m_ycoordinate;
122 signed long Coordinate::getZCoordinate() const
124 return m_zcoordinate;
127 bool Coordinate::isDirection() const
129 if(abs(m_xcoordinate) > 1) return false;
130 if(abs(m_ycoordinate) > 1) return false;
131 if(abs(m_zcoordinate) > 1) return false;
133 if(isNullVector()) return false;
135 return true;
137 std::string Coordinate::toString() const
139 std::string result;
141 result.append("x: ");
142 result.append(String::Get()->fromLong(getXCoordinate()));
144 result.append(", y: ");
145 result.append(String::Get()->fromLong(getYCoordinate()));
148 result.append(", z: ");
149 result.append(String::Get()->fromLong(getZCoordinate()));
151 return result;
154 std::string Coordinate::toDirectionString() const
156 Assert(isDirection());
158 std::string result;
160 int x = getXCoordinate();
161 int y = getYCoordinate();
162 int z = getZCoordinate();
164 if(x > 0)
165 result.append("north");
166 if(x < 0)
167 result.append("south");
169 if(y > 0)
170 result.append("east");
171 if(y < 0)
172 result.append("west");
174 if(z > 0)
175 result.append("up");
176 if(z < 0)
177 result.append("down");
179 return result;
182 bool Coordinate::isNullVector() const
184 return (!m_xcoordinate && !m_ycoordinate && !m_zcoordinate);