HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #ifndef VECTOR_H 00022 #define VECTOR_H 1 00023 00024 #include <cstdarg> 00025 #include <cassert> 00026 00027 #include <iostream> 00028 #include <iomanip> 00029 #include <string> 00030 #include <sstream> 00031 00032 # if (!defined __PI) 00033 # include <cmath> 00034 const double pi = 4.*atan (1.); 00035 # define __PI 3_14 00036 # endif 00037 00039 00041 namespace VecMath { 00042 00044 00048 template <unsigned D, typename N = double> class Vector { 00049 00050 public: 00051 00052 //---------- management ---------- 00054 Vector<D, N> (); 00056 Vector<D, N> (const N &x); 00058 Vector<D, N> (N x0, N x1, ... ); 00059 00060 //---------- access ---------- 00061 00063 N &operator[] (unsigned i); 00065 N operator[] (unsigned i) const; 00066 00067 static unsigned dimension (void); 00068 00069 //---------- arithmetics ---------- 00070 00071 Vector<D, N> &operator+= (const Vector<D, N> &Y); 00072 Vector<D, N> &operator-= (const Vector<D, N> &Y); 00073 Vector<D, N> &operator*= (const N &s); 00074 00076 const N *data () const; 00077 00079 template <typename T> operator const T * () const; 00080 00082 std::string toString() const; 00083 00084 private: 00085 00086 N _x[D]; 00087 00088 }; 00089 00091 template <unsigned D, typename N> 00092 Vector<D, N> operator-(const Vector<D, N> &v); 00094 template <unsigned D, typename N> 00095 Vector<D, N> operator+(const Vector<D, N> &x, const Vector<D, N> &y); 00097 template <unsigned D, typename N> 00098 Vector<D, N> operator-(const Vector<D, N> &x, const Vector<D, N> &y); 00099 00100 template <unsigned D, typename N> 00101 Vector<D, N> operator* (const Vector<D, N> &x, const N &s); 00102 template <unsigned D, typename N> 00103 Vector<D, N> operator* (const N &s, const Vector<D, N> &x); 00104 00106 template <unsigned D, typename N> 00107 N operator* (const Vector<D, N> &x, const Vector<D, N> &y); 00108 00109 template <unsigned D, typename N> 00110 Vector<D, N> operator/ (const Vector<D, N> &x, const N &s); 00111 00113 template <unsigned D, typename N> 00114 Vector<D, N> operator/ (const Vector<D, N> &x, const Vector<D, N> &y); 00115 00117 template <unsigned D, typename N> 00118 Vector<D, N> scale (const Vector<D, N> &x, const Vector<D, N> &y); 00119 00121 template <unsigned D, typename N> 00122 bool operator==(const Vector<D, N> &one, const Vector<D, N> &other); 00123 00125 template <unsigned D, typename N> 00126 bool operator!=(const Vector<D, N> &one, const Vector<D, N> &other); 00127 00129 template <unsigned D, typename N> 00130 bool operator<(const Vector<D, N> &one, const Vector<D, N> &other); 00131 00132 template <unsigned D, typename N> 00133 N sqnorm (const Vector<D, N> &x); 00134 00135 00137 template <unsigned D, typename N> 00138 std::ostream &operator << (std::ostream &s, const Vector<D, N> &v); 00139 00141 template <unsigned D, typename N> 00142 std::istringstream &operator >> (std::istringstream &in, Vector<D, N> &v); 00143 00145 template <typename N> 00146 Vector<3, N> vcross (Vector<3, N> a, Vector<3, N> b); 00147 00149 template <typename N> 00150 Vector<4, N> vcross (Vector<4, N> a, Vector<4, N> b, Vector<4, N> c); 00151 00153 template <unsigned D, typename N> 00154 Vector<D, N> vnormalize( const Vector<D, N> &x ); 00155 00157 template <typename N> Vector<2, N> makeVector(N const &x0, N const &x1); 00158 00160 template <typename N> 00161 Vector<3, N> makeVector(N const &x0, N const &x1, N const &x2); 00162 00164 template <typename N> 00165 Vector<4, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3); 00166 00168 template <typename N> 00169 Vector<5, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4); 00170 00172 template <typename N> 00173 Vector<6, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4, 00174 N const &x5); 00175 00177 template <typename N> 00178 Vector<7, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4, 00179 N const &x5, N const &x6); 00180 00182 template <typename N> 00183 Vector<8, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4, 00184 N const &x5, N const &x6, N const &x7); 00185 00187 template <typename N> 00188 Vector<9, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4, 00189 N const &x5, N const &x6, N const &x7, N const &x8); 00190 00192 template <typename N> 00193 Vector<10, N> makeVector(N const &x0, N const &x1, N const &x2, N const &x3, N const &x4, 00194 N const &x5, N const &x6, N const &x7, N const &x8, N const &x9); 00195 00196 00197 } 00198 #endif // VECTOR_H