diff options
author | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:34:35 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:34:35 +0000 |
commit | 69f7ffa70863bef2be4cae08c466b5d97a627277 (patch) | |
tree | 9945e1c5cb29e10afda66af3274c3ef87886225a | |
parent | a94049b1a43f83d750b9b5dee031c19a6b1fafb0 (diff) |
accomodate the new modules
-rw-r--r-- | src/common/Makefile.am | 10 | ||||
-rw-r--r-- | src/common/color.cc | 84 | ||||
-rw-r--r-- | src/common/color.h | 50 | ||||
-rw-r--r-- | src/common/common.cc | 20 | ||||
-rw-r--r-- | src/common/common.h | 26 | ||||
-rw-r--r-- | src/common/console.cc | 24 | ||||
-rw-r--r-- | src/common/console.h | 51 | ||||
-rw-r--r-- | src/common/file.cc | 69 | ||||
-rw-r--r-- | src/common/file.h | 40 | ||||
-rw-r--r-- | src/common/functions.cc | 64 | ||||
-rw-r--r-- | src/common/functions.h | 53 | ||||
-rw-r--r-- | src/common/inifile.cc | 93 | ||||
-rw-r--r-- | src/common/inifile.h | 66 | ||||
-rw-r--r-- | src/common/path.cc | 40 | ||||
-rw-r--r-- | src/common/path.h | 26 | ||||
-rw-r--r-- | src/common/vector3f.cc | 161 | ||||
-rw-r--r-- | src/common/vector3f.h | 149 |
17 files changed, 100 insertions, 926 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 431cb8c..dd5c5d1 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -1,10 +1,6 @@ - METASOURCES = AUTO +libcommon_la_SOURCES = common.cc console.cc libcommon_la_LDFLAGS = -avoid-version -no-undefined noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = color.cc console.cc file.cc functions.cc inifile.cc \ - path.cc vector3f.cc - - -noinst_HEADERS = color.h console.h file.h functions.h inifile.h path.h \ - vector3f.h +noinst_HEADERS = common.h console.h +INCLUDES = -I$(top_srcdir)/src diff --git a/src/common/color.cc b/src/common/color.cc deleted file mode 100644 index da43efc..0000000 --- a/src/common/color.cc +++ /dev/null @@ -1,84 +0,0 @@ -/* - common/color.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "color.h" - -namespace common { - -Color::Color() { - _r = _g = _b = 0.0f; - _a = 1.0f; -} - -Color::Color(const float red, const float green , const float blue , const float alpha) { - _r = red; - _g = green; - _b = blue; - _a = alpha; -} - -Color::Color(const float grey, const float alpha) { - _r = _g = _b = grey; - _a = alpha; -} - -Color::Color(const Color &other) { - this->operator=(other); -} - -const Color & Color::operator=(const Color &other) { - this->_r = other._r; - this->_g = other._g; - this->_b = other._b; - this->_a = other._a; - return (*this); -} - -void Color::normalize() { - float tmp = _r; - - if (_g > tmp) - tmp = _g; - if ( _b > tmp) - tmp = _b; - - if (tmp > 1) { - _r /= tmp; - _g /= tmp; - _b /= tmp; - } -} - -float Color::red() const { - return _r; -} - -float Color::green() const { - return _g; -} - -float Color::blue() const { - return _b; -} - -float Color::alpha() const { - return _a; -} - -Color Color::operator*(float scalar) const { - return Color(red()*scalar, green()*scalar, blue()*scalar, alpha()); -} - -Color operator*(float scalar, const Color& color) { - return color * scalar; -} -std::ostream &operator<<(std::ostream &os, const Color &c) { - os << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha(); - return os; -} - -} // namespace common diff --git a/src/common/color.h b/src/common/color.h deleted file mode 100644 index e6aa9eb..0000000 --- a/src/common/color.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - common/color.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __COLOR_HEADER__ -#define __COLOR_HEADER__ - -#include <iostream> - -namespace common { - -/// a class representing an RGBA color value -class Color { - public: - Color(); - Color(const float, float const, const float, const float=1.0f); - Color(const float, const float=1.0f); - Color(const Color &); - - float red() const; - float green() const; - float blue() const; - float alpha() const; - - const Color &operator=(const Color &); - - Color operator*(const float scalar) const; - - // Some default colors - static const Color Black() { return Color(0.0f); }; - static const Color White() { return Color(1.0f); }; - static const Color Red() { return Color(1.0f,0.0f,0.0f); }; - static const Color Green() { return Color(0.0f,1.0f,0.0f); }; - static const Color Blue() { return Color(0.0f, 0.0f, 1.0f); }; - static const Color Yellow() { return Color(1.0f, 1.0f, 0.0f); }; - - private: - void normalize(); - float _r, _g, _b, _a; -}; - -std::ostream &operator<<(std::ostream &os, const Color &c); - -Color operator*(const float scalar, const Color& color); - -} // namespace commmon - -#endif // ___HEADER__ diff --git a/src/common/common.cc b/src/common/common.cc new file mode 100644 index 0000000..cb968c0 --- /dev/null +++ b/src/common/common.cc @@ -0,0 +1,20 @@ +/* + common/common.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "common/common.h" + +namespace common { + +void init() { + con_debug << "Initializing common..." << std::endl; +} + +void shutdown() { + con_debug << "Shutting down common..." << std::endl; +} + +} + diff --git a/src/common/common.h b/src/common/common.h new file mode 100644 index 0000000..c47b09f --- /dev/null +++ b/src/common/common.h @@ -0,0 +1,26 @@ +/* + common/common.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_COMMON_H__ +#define __INCLUDED_COMMON_H__ + +#include "config.h" + +/// common functions and components that are used by the other subsytems +namespace common { + + // initialize common components + void init(); + + // shutdown common components + void shutdown(); + +} // namespace common + +#include "common/console.h" + +#endif // __INCLUDED_COMMON_H__ + diff --git a/src/common/console.cc b/src/common/console.cc index 7641865..e4fd215 100644 --- a/src/common/console.cc +++ b/src/common/console.cc @@ -1,14 +1,28 @@ /* common/console.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 */ -#include "console.h" +#include "common/console.h" + +// TODO enforce singleton namespace common { - - Console *Console::instance = 0; + +Console *Console::console_instance = 0; + +Console::Console() { + console_instance = this; +} + +Console::~Console() { + console_instance = 0; +} + +Console *Console::instance() { + return console_instance; +} } // namespace common diff --git a/src/common/console.h b/src/common/console.h index b661afc..fbda5da 100644 --- a/src/common/console.h +++ b/src/common/console.h @@ -1,47 +1,60 @@ /* common/console.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2. */ -#ifndef __INCLUDED_CONSOLE_H__ -#define __INCLUDED_CONSOLE_H__ +#ifndef __INCLUDED_COMMON_CONSOLE_H__ +#define __INCLUDED_COMMON_CONSOLE_H__ + +// project headers +#include "common/common.h" // C++ headers #include <iostream> /// global define to send a message to the system console -#define conmesg common::Console::instance->message() +#define con_print common::Console::instance()->messagestream() /// global define to send a warning message to the system console -#define conwarn common::Console::instance->warning() - -#define DEBUG 1 +#define con_warn common::Console::instance()->warningstream() -#ifdef DEBUG +#ifdef HAVE_DEBUG_MESSAGES /// global define to send a debug message to the system console -#define condebug common::Console::instance->debug() +#define con_debug common::Console::instance()->debugstream() #else -#define condebug if (0) *(std::ostream*)(0) -#endif // DEBUG +#define con_debug if (0) *(std::ostream*)(0) +#endif namespace common { -/// Interface for a console object that writes messages on the screen +/// interface for a console object that writes messages on the screen class Console { public: + /// default constructor + Console(); + + /// default destructor + virtual ~Console(); + /// stream to send normal messages too - virtual std::ostream & message() = 0; + virtual std::ostream & messagestream() = 0; /// stream to send warning messages too - virtual std::ostream & warning() = 0; + virtual std::ostream & warningstream() = 0; /// stream to send debug messages too - virtual std::ostream & debug() = 0 ; + virtual std::ostream & debugstream() = 0; + + /// a pointer to the current console implementation + static Console *instance(); - static Console *instance; -}; // class Console +private: + /// console singleton + static Console *console_instance; +} +; // class Console } // namespace common -#endif // __INCLUDED_CONSOLE_H__ +#endif // __INCLUDED_COMMON_CONSOLE_H__ diff --git a/src/common/file.cc b/src/common/file.cc deleted file mode 100644 index 6f4d6d4..0000000 --- a/src/common/file.cc +++ /dev/null @@ -1,69 +0,0 @@ -/* - common/file.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "file.h" -#include "console.h" - -namespace common { - -std::string File::datadir = ""; -std::string File::homedir = ""; -std::string File::basedir = ""; -std::string File::moddir = ""; - -void File::open(const char * filename, ios_base::openmode mode) -{ - file_name.assign(filename); - std::string fn; - - // if moddir is set, try the mods subdir first - if (moddir.size() > 0) { - // try homedir+moddir - fn = homedir; - fn.append(moddir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - condebug << "File opened " << fn << std::endl; - return; - } - - // try datadir + moddir - fn = datadir; - fn.append(moddir); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - condebug << "File opened " << fn << std::endl; - return; - } - } - - // try homedir+basedir - fn = homedir; - fn.append(basedir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - if (this->is_open()) { - condebug << "File opened " << fn << std::endl; - return; - } - - // try datadir+basedir - fn = datadir; - fn.append(basedir); - fn.append(filename); - std::ifstream::open(fn.c_str(), mode); - - if (!this->is_open()) { - conwarn << "Could not open " << filename << std::endl; - } else { - condebug << "File opened " << fn << std::endl; - } -} - -} // namespace common - diff --git a/src/common/file.h b/src/common/file.h deleted file mode 100644 index 842450e..0000000 --- a/src/common/file.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - common/file.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_FILE_H__ -#define __INCLUDED_FILE_H__ - -// C++ headers -#include <string> -#include <fstream> - -namespace common { - -/// a class to open data files -class File : public std::ifstream -{ -public: - /// open the file for reading - virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in ); - - /// current filename - inline std::string name() { return file_name; } - - /// location of the main data files, includes trailing / - static std::string datadir; - /// location of the personal data files, includes trailing / - static std::string homedir; - /// subdirectory with the base data files, includes trailing / - static std::string basedir; - /// subdirectory for the current mod, includes trailing / - static std::string moddir; -private: - std::string file_name; -}; // class File - -} // namespace common - -#endif // __INCLUDED_GAME_H__ diff --git a/src/common/functions.cc b/src/common/functions.cc deleted file mode 100644 index 343084a..0000000 --- a/src/common/functions.cc +++ /dev/null @@ -1,64 +0,0 @@ -/* - common/functions.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "functions.h" - -namespace common { - -float min(float a, float b) { - return (a < b ? a : b); -} - -float max(float a, float b) { - return (a > b ? a : b); -} - -int min(int a, int b) { - return (a < b ? a : b); -} - -int max(int a, int b) { - return (a > b ? a : b); -} - -float randomf(const float max) { - return ((float) rand() / (float) RAND_MAX) * max; -} - -unsigned randomi(const unsigned int max) { - return ((unsigned int)(rand() % max)); -} - -float degrees180f(float angle) { - float r = angle; - while (r <= -180.0f) - r += 360.0f; - while (r >= 180.0f) - r -= 360.0f; - return r; -} - -float degrees360f(float angle) { - float r = angle; - while (r < 0.0f) - r += 360.0f; - while (r >= 360.0f) - r -= 360.0f; - return r; -} - -float sgnf(float value) -{ - if (value < 0 ) - return -1; - else if (value == 0 ) - return 0; - - return 1; -} - -} // namespace common diff --git a/src/common/functions.h b/src/common/functions.h deleted file mode 100644 index 18dfc82..0000000 --- a/src/common/functions.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - common/functions.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_FUNCTIONS_H__ -#define __INCLUDED_FUNCTIONS_H__ - -// C++ headers -#include <cstdlib> -#include <cmath> - -/// Support functions for the game engine -namespace common { - -/// return the smallest of two float values -float min(float a, float b); - -/// return the smallest of two integers -int min(int a, int b); - -/// return the largest of two float values -float max(float a, float b); - -/// return the largest of two integers -int max(int a, int b); - -/// returns a random float -/*! The value returned will be in the interval [0-max] - * @param max the maximum value returned -**/ -float randomf(const float max = 1); - -/// returns a random integer -/*! The value returned will be in the interval [0-(max-1)] - * @param max the maximum value returned -**/ -unsigned int randomi(const unsigned int max); - -/// return the sign of a float value -float sgnf(float value); - -/// return an angle in the ]-180,180] range -float degrees180f(float angle); - -/// return an angle in the [0,360[ range -float degrees360f(float angle); - -} // namespace common - -#endif // __INCLUDED_FUNCTIONS_H__ - diff --git a/src/common/inifile.cc b/src/common/inifile.cc deleted file mode 100644 index c96b85b..0000000 --- a/src/common/inifile.cc +++ /dev/null @@ -1,93 +0,0 @@ -/* - common/inifile.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "inifile.h" -#include "console.h" - -namespace common { - -void IniFile::open(const char * filename, std::ios_base::openmode mode ) -{ - last_read_was_section = false; - last_read_was_key = false; - key_current = ""; - value_current = ""; - section_current = ""; - line_number = 0; - - File::open(filename, mode); -} - - -bool IniFile::got_section() const { - return last_read_was_section; -} - -bool IniFile::got_section(const char * sectionlabel) const { - return (last_read_was_section && section_current == sectionlabel); -} - -IniFile & IniFile::getline() -{ - char line[1024]; - - last_read_was_section = false; - last_read_was_key = false; - key_current = ""; - value_current = ""; - - while ((*this)) { - File::getline(line, 1024); - std::string s(line); - line_number++; - - if (s.size() == 0) { - // empty line - } else - // comment - if (s[0] == '#' || s[0] == ';') { - // condebug << "IniFile got comment " << s << std::endl; - } else - // section header - if (s.size() > 2 && s[0] == '[' && s[s.size()-1] == ']' ) { - // condebug << "Inifile got section header " << s << std::endl; - section_current = s.substr(1, s.size()-2); - last_read_was_section = true; - break; - } else { - // key=value pair - size_t found = s.find('='); - if (found !=std::string::npos) { - // FIXME strip spaces, make lowercase - key_current = s.substr(0, found); - - if (key_current.size() > 0 ) { - value_current = s.substr(found+1, s.size() - found - 1); - last_read_was_key = true; - break; - } - - } - } - } - - return (*this); -} - -bool IniFile::got_key_string(char * const keylabel, std::string & valuestring) { - //condebug << "IniFile got_value_string " << keylabel << " " << last_read_was_key << std::endl; - if (last_read_was_key && key_current == keylabel) { - valuestring.assign(value_current); - return true; - } else { - return false; - } -} - - -} // namespace common - diff --git a/src/common/inifile.h b/src/common/inifile.h deleted file mode 100644 index 934da58..0000000 --- a/src/common/inifile.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - common/inifile.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_INIFILE_H__ -#define __INCLUDED_INIFILE_H__ - -// project headers -#include "file.h" - -// C++ headers -#include <string> -#include <fstream> - -namespace common { - -/// a class to read .ini files -/*! The IniFile class provides functions to read .ini files. A .ini file - * consists of one or more [section] headers followed by one or more key=value - * pairs. Lines starting with # or ; are considered comments - */ -class IniFile : public File -{ -public: - /// open the file for reading - virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in ); - - IniFile & getline(); - - /// current section label - inline std::string section() const { return section_current; } - - /// current key - inline std::string key() const { return key_current; } - - /// current value - inline std::string value() const { return value_current; } - - /// true if the last read statement was a section header - bool got_section() const; - /// true if the last read statement was a certain section header - bool got_section(const char * sectionlabel) const; - - /// true if the last read statement was a key=value pair - inline bool got_key() const { return last_read_was_key; } - /// check if the last read key=value pair matches keylabel and store the value in valuestring - bool got_key_string(char * const keylabel, std::string & valuestring); - - inline unsigned int line() const { return line_number; } - -private: - std::string section_current; - std::string key_current; - std::string value_current; - - bool last_read_was_key; - bool last_read_was_section; - - unsigned int line_number; -}; // class IniFile - -} // namespace common - -#endif // __INCLUDED_INIFILE_H__ diff --git a/src/common/path.cc b/src/common/path.cc deleted file mode 100644 index d9fc332..0000000 --- a/src/common/path.cc +++ /dev/null @@ -1,40 +0,0 @@ -/* - common/path.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "path.h" -#include "console.h" - -#ifdef _WIN32 -#include <dlfcn.h> -#else -#include <sys/stat.h> -#endif - -namespace common { - -void Path::create(std::string path) -{ - std::string tmp(path); - if (tmp[tmp.size()-1] == '/') - tmp = tmp.substr(0, tmp.size() - 1); - -#ifdef _WIN32 - mkdir(tmp.c_str()); -#else - if (!mkdir(tmp.c_str(), 0777)) - conwarn << "Could not create directory " << tmp << std::endl; - else - condebug << "Path created " << tmp << std::endl; -#endif -} - -bool Path::exists(std::string path) -{ - return false; -} - -} diff --git a/src/common/path.h b/src/common/path.h deleted file mode 100644 index 43b07d2..0000000 --- a/src/common/path.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - common/path.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_PATH_H__ -#define __INCLUDED_PATH_H__ - -// C++ headers -#include <string> -#include <fstream> - -namespace common { - -/// a class to create directories -class Path -{ -public: - static bool exists(std::string path); - static void create(std::string path); -}; // class Path - -} // namespace common - -#endif // __INCLUDED_PATH_H__ diff --git a/src/common/vector3f.cc b/src/common/vector3f.cc deleted file mode 100644 index 86bdb36..0000000 --- a/src/common/vector3f.cc +++ /dev/null @@ -1,161 +0,0 @@ -/* - common/vector3f.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -// project headers -#include "vector3f.h" - -// C++ headers -#include <cmath> - -namespace common { - -Vector3f::Vector3f() : - x(coord[0]), y(coord[1]), z(coord[2]) -{ - for (int i=0; i < 3; i++) - coord[i] = 0; -} - - -Vector3f::Vector3f(const Vector3f &other) : - x(coord[0]), y(coord[1]), z(coord[2]) -{ - for (int i=0; i < 3; i++) - coord[i] = other.coord[i]; -} - -Vector3f::Vector3f(const float xv, const float yv, const float zv) : - x(coord[0]), y(coord[1]), z(coord[2]) -{ - coord[0] = xv; - coord[1] = yv; - coord[2] = zv; -} - -Vector3f::~Vector3f() -{ -} - - -Vector3f & Vector3f::operator=(const Vector3f & other) -{ - for (int i=0; i < 3; i++) - coord[i] = other.coord[i]; - return (*this); -} - - -Vector3f & Vector3f::operator*=(const float scalar) -{ - for (int i=0; i < 3; i++) - coord[i] *= scalar; - return (*this); -} - - -Vector3f & Vector3f::operator/=(const float scalar) { - for (int i=0; i < 3; i++) - coord[i] /= scalar; - return (*this); -} - - -Vector3f &Vector3f::operator-=(const Vector3f & other) { - for (int i=0; i < 3; i++) - coord[i] -= other[i]; - return (*this); -} - - -Vector3f &Vector3f::operator+=(const Vector3f &other) { - for (int i=0; i < 3; i++) - coord[i] += other[i]; - return (*this); -} - - -Vector3f Vector3f::operator*(const float scalar) const { - Vector3f r(*this); - for (int i=0; i < 3; i++) - r.coord[i] *= scalar; - return (r); - } - - -Vector3f Vector3f::operator/(const float scalar) const { - Vector3f r(*this); - for (int i=0; i < 3; i++) - r.coord[i] /= scalar; - return (r); -} - - -Vector3f Vector3f::operator-(const Vector3f& other) const { - Vector3f r(*this); - for (int i=0; i < 3; i++) - r.coord[i] -= other.coord[i]; - return (r); -} - - -Vector3f Vector3f::operator+(const Vector3f& other) const { - Vector3f r(*this); - for (int i=0; i < 3; i++) - r.coord[i] += other.coord[i]; - return (r); -} - - -float Vector3f::operator*(const Vector3f& other) const { - float r = 0; - for (int i=0; i < 3; i++) - r += coord[i] * other.coord[i]; - return (r); -} - - -bool Vector3f::operator==(const Vector3f& other) const { - for (int i=0; i < 3; i++) - if (coord[i] != other.coord[i]) - return (false); - return (true); -} - -float Vector3f::lengthsquared() const { - double r = 0; - for (int i=0; i < 3; i++) - r += coord[i]*coord[i]; - return ((float) r); -} - -float Vector3f::length() const { - double r = 0; - for (int i=0; i < 3; i++) - r += coord[i]*coord[i]; - - return ((float) sqrt(r)); -} - -void Vector3f::normalize() { - (*this) /= this->length(); -} - -std::ostream &operator<<(std::ostream & os, const Vector3f & vector) { - os << vector[0] << " " << vector[1] << " " << vector[2]; - return os; -} - -std::istream &operator>>(std::istream & is, Vector3f & vector) { - for (int i=0; i < 3; i++) - is >> vector[i]; - return is; -} - -Vector3f operator*(float scalar, const Vector3f& vector) { - return vector * scalar; -} - -} // namespace common diff --git a/src/common/vector3f.h b/src/common/vector3f.h deleted file mode 100644 index e943bd4..0000000 --- a/src/common/vector3f.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - common/vector3f.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_VECTOR3F_H__ -#define __INCLUDED_VECTOR3F_H__ - -// project headers -#include "vector3f.h" - -// C++ headers -#include <iostream> - -namespace common { - -/// A point or vector in 3D-space -/*! - An instance of this class represents a point in 3D-space or a 3D-vector - and forms the basic building block for all spatial calculations. - */ -class Vector3f { - -public: - /// Default constructor, creates a Vector3f (0,0,0) - Vector3f(); - - /// Copy constructor - /*! Create a new Vector3f that is a copy from an other - * @param other the vector to copy values from - */ - Vector3f(const Vector3f &other); - - /// Create a Vector3f with given coordinates - /*! Create a new vector at a given position in a 3D-space - * @param xv the x-coordinate of the location - * @param yv the y-coordinate of the location - * @param zv the z-coordinate of the location - */ - Vector3f(const float xv, const float yv, const float zv); - - /// Destructor - ~Vector3f(); - - /* -- Assignment operators -- */ - /// assignment operator - Vector3f& operator=(const Vector3f &other); - - /// multiplicate each element of the vector with a given value - /// @param scalar multiplication factor - Vector3f& operator*=(const float scalar); - - /// divide each element of the vector by a given value - /// @param scalar divider - Vector3f& operator/=(const float scalar); - - /// perform an element-wise subtraction - Vector3f& operator-=(const Vector3f &other); - - /// perform ann element-wise addition - Vector3f& operator+=(const Vector3f &other); - - /* -- Mathematical operators -- */ - - /// return this Vector multiplied with scalar - Vector3f operator*(const float scalar) const; - - /// return this vector divided by a scalar - Vector3f operator/(const float scalar) const; - - /// return the element-wise difference between two vectors - Vector3f operator-(const Vector3f &other) const; - - /// return the element-wise sumn of two vectors - Vector3f operator+(const Vector3f &other) const; - - /// return the vector cross-product - float operator*(const Vector3f &other) const; - - /// comparison operator - bool operator==(const Vector3f &other) const; - - /// assign a value to an element of this vector - /*! WARNING: range is not checked - * @param index the index of the element to assign to ( 0 <= index < 3 ) - */ - inline float& operator[](const unsigned int index) { - return coord[index]; - } - - /// returns the value of an element of this vector - /*! WARNING: range is not checked - * @param index the index of the element to return ( 0 <= index < 3 ) - */ - inline float operator[](const unsigned int index) const { - return coord[index]; - } - - - float &x; - float &y; - float &z; - - /// Return the cartesian length of this vector - float length() const; - - /// Return the cartesian length squared (to speed up calculations) - float lengthsquared() const; - - /// Divide this Vector by it's length - /// @see normalized() - /// WARNING: vector must not be (0, 0, 0) - void normalize(); - - /* static functions */ - - /// Returns the unity vector on the X-axis - static inline Vector3f Xaxis() { return Vector3f(1.0f, 0.0f, 0.0f); } - - /// Returns the unity vector on the Y-axis - static inline Vector3f Yaxis() { return Vector3f(0.0f, 1.0f, 0.0f); } - - /// Returns the unity vector on the Z-axis - static inline Vector3f Zaxis() { return Vector3f(0.0f, 0.0f, 1.0f); } - - /// Return the cartesian length of a vector - static inline float length(const Vector3f& vector) { return vector.length(); } - - /// Return a vector divided by it's length - /// @see normalize() - /// WARNING: vector must not be (0, 0, 0) - static inline Vector3f normalized(const Vector3f& vector) { return (vector / vector.length()); } - - float coord[3]; -}; - -/// Write a Vector3f to a std::ostream -std::ostream &operator<<(std::ostream & os, const Vector3f & vector); - -/// Read a Vector3d from a std::istream -std::istream &operator>>(std::istream & is, Vector3f& vector); - -/// scalar*Vector3f operators -Vector3f operator*(float scalar, const Vector3f& vector); - -} // namespace common - -#endif // __INCLUDED_VECTOR3F_H__ |