# The C and CXX include file search paths:
SET(CMAKE_C_INCLUDE_PATH
"../../src"
- "../BOOST_INCLUDE_DIR"
)
SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
# The C and CXX include file search paths:
SET(CMAKE_C_INCLUDE_PATH
"../../src"
- "../BOOST_INCLUDE_DIR"
)
SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -O3 -DNDEBUG -std=c++0x -I/home/yasutomo/devel/moflib/sample/../src -I/home/yasutomo/devel/moflib/sample/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -O3 -DNDEBUG -std=c++0x -I/home/yasutomo/devel/moflib/sample/../src
CXX_DEFINES =
-Start testing: Sep 03 15:16 PDT
+Start testing: Sep 05 19:53 PDT
----------------------------------------------------------
1/5 Testing: vector2_test
1/5 Test: vector2_test
Command: "/home/yasutomo/devel/moflib/src/mof/math/test/testbin/vector2_test"
Directory: /home/yasutomo/devel/moflib/src/build_unix/mof/math
-"vector2_test" start time: Sep 03 15:16 PDT
+"vector2_test" start time: Sep 05 19:53 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.01 sec
+Test time = 0.00 sec
----------------------------------------------------------
Test Passed.
-"vector2_test" end time: Sep 03 15:16 PDT
+"vector2_test" end time: Sep 05 19:53 PDT
"vector2_test" time elapsed: 00:00:00
----------------------------------------------------------
2/5 Test: vector3_test
Command: "/home/yasutomo/devel/moflib/src/mof/math/test/testbin/vector3_test"
Directory: /home/yasutomo/devel/moflib/src/build_unix/mof/math
-"vector3_test" start time: Sep 03 15:16 PDT
+"vector3_test" start time: Sep 05 19:53 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.02 sec
+Test time = 0.01 sec
----------------------------------------------------------
Test Passed.
-"vector3_test" end time: Sep 03 15:16 PDT
+"vector3_test" end time: Sep 05 19:53 PDT
"vector3_test" time elapsed: 00:00:00
----------------------------------------------------------
3/5 Test: matrix2_test
Command: "/home/yasutomo/devel/moflib/src/mof/math/test/testbin/matrix2_test"
Directory: /home/yasutomo/devel/moflib/src/build_unix/mof/math
-"matrix2_test" start time: Sep 03 15:16 PDT
+"matrix2_test" start time: Sep 05 19:53 PDT
Output:
----------------------------------------------------------
<end of output>
Test time = 0.01 sec
----------------------------------------------------------
Test Passed.
-"matrix2_test" end time: Sep 03 15:16 PDT
+"matrix2_test" end time: Sep 05 19:53 PDT
"matrix2_test" time elapsed: 00:00:00
----------------------------------------------------------
4/5 Test: matrix3_test
Command: "/home/yasutomo/devel/moflib/src/mof/math/test/testbin/matrix3_test"
Directory: /home/yasutomo/devel/moflib/src/build_unix/mof/math
-"matrix3_test" start time: Sep 03 15:16 PDT
+"matrix3_test" start time: Sep 05 19:53 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.02 sec
+Test time = 0.01 sec
----------------------------------------------------------
Test Passed.
-"matrix3_test" end time: Sep 03 15:16 PDT
+"matrix3_test" end time: Sep 05 19:53 PDT
"matrix3_test" time elapsed: 00:00:00
----------------------------------------------------------
5/5 Test: make_matrix_test
Command: "/home/yasutomo/devel/moflib/src/mof/math/test/testbin/make_matrix_test"
Directory: /home/yasutomo/devel/moflib/src/build_unix/mof/math
-"make_matrix_test" start time: Sep 03 15:16 PDT
+"make_matrix_test" start time: Sep 05 19:53 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.01 sec
+Test time = 0.02 sec
----------------------------------------------------------
Test Passed.
-"make_matrix_test" end time: Sep 03 15:16 PDT
+"make_matrix_test" end time: Sep 05 19:53 PDT
"make_matrix_test" time elapsed: 00:00:00
----------------------------------------------------------
-End testing: Sep 03 15:16 PDT
+End testing: Sep 05 19:53 PDT
# The C and CXX include file search paths:
SET(CMAKE_C_INCLUDE_PATH
".."
- "../mof/math/BOOST_INCLUDE_DIR"
)
SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
-/usr/bin/c++ -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/make_matrix_test.dir/test/make_matrix_test.cpp.o -o ../../../mof/math/test/testbin/make_matrix_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
+/usr/bin/c++ -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/make_matrix_test.dir/test/make_matrix_test.cpp.o -o ../../../mof/math/test/testbin/make_matrix_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
-/usr/bin/c++ -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/matrix2_test.dir/test/matrix2_test.cpp.o -o ../../../mof/math/test/testbin/matrix2_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
+/usr/bin/c++ -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/matrix2_test.dir/test/matrix2_test.cpp.o -o ../../../mof/math/test/testbin/matrix2_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
-/usr/bin/c++ -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/matrix3_test.dir/test/matrix3_test.cpp.o -o ../../../mof/math/test/testbin/matrix3_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
+/usr/bin/c++ -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/matrix3_test.dir/test/matrix3_test.cpp.o -o ../../../mof/math/test/testbin/matrix3_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
-/usr/bin/c++ -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/vector2_test.dir/test/vector2_test.cpp.o -o ../../../mof/math/test/testbin/vector2_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
+/usr/bin/c++ -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/vector2_test.dir/test/vector2_test.cpp.o -o ../../../mof/math/test/testbin/vector2_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/math/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
-/usr/bin/c++ -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/vector3_test.dir/test/vector3_test.cpp.o -o ../../../mof/math/test/testbin/vector3_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
+/usr/bin/c++ -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x CMakeFiles/vector3_test.dir/test/vector3_test.cpp.o -o ../../../mof/math/test/testbin/vector3_test -rdynamic -L/home/yasutomo/devel/moflib/src/../lib /home/yasutomo/devel/moflib/lib/libmofmath.a -Wl,-rpath,/home/yasutomo/devel/moflib/src/../lib
# The C and CXX include file search paths:
SET(CMAKE_C_INCLUDE_PATH
".."
- "../mof/util/BOOST_INCLUDE_DIR"
)
SET(CMAKE_CXX_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
SET(CMAKE_Fortran_INCLUDE_PATH ${CMAKE_C_INCLUDE_PATH})
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
-CXX_FLAGS = -Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src -I/home/yasutomo/devel/moflib/src/mof/util/BOOST_INCLUDE_DIR
+CXX_FLAGS = -Wall -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x -I/home/yasutomo/devel/moflib/src
CXX_DEFINES =
//{{{ operator *=
Derived& operator*=(const Derived& rhs)
{
-<<<<<<< HEAD
Derived M;
for (size_t i = 0; i < Dim; ++i) {
for (size_t j = 0; j <= Dim; ++j) {
sum += at(i, k) * rhs.at(k, j);
}
M.elements_[i][j] = sum;
-=======
- Derived retval;
- const int SIZE = Dim + 1;
-
- // calculate the last element previously
- int b = last_index() - Dim;
- int c = Dim;
- float last_sum = 0;
- for (int i = 0; i < SIZE; ++i) {
- last_sum += elements_[b + i] * rhs.elements_[c + i * SIZE];
- }
- retval.elements_[last_index()] = 1;
-
- for (int a = last_index() - 1; a >= 0; --a) {
- int b = a / SIZE * SIZE;
- int c = a % SIZE;
- float sum = 0;
- for (int i = 0; i < SIZE; ++i) {
- sum += elements_[b + i] * rhs.elements_[c + i * SIZE];
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
}
}
*this = M;
for (size_t i = 0; i < Dim; ++i) {
float sum = 0;
for (size_t k = 0; k <= Dim; ++k) {
- sum += (*this)[i][k] * rhs[k];
+ sum += elements_[i][k] * rhs[k];
}
arr[i] = sum;
}
--- /dev/null
+#pragma once
+#include <mof/base/mofdef.hpp>
+#include <mof/math/threshold.hpp>
+#include <boost/operators.hpp>
+#include <ostream>
+#include <iomanip>
+#include <cmath>
+
+namespace mof
+{
+namespace math
+{
+ template <size_t Dim> class row_of_matrix;
+
+ /**
+ * @brief 同次座標変換行列テンプレートクラス
+ * @note このクラスはアフィン変換行列を表す
+ * @note このテンプレートから直接特殊化することは想定していない.
+ * あくまでmatrixxを実装するための補助テンプレートである.
+ * このクラスは不変クラスである.
+ * @tparam Dim 行列の次元(要素数はこの数値の2乗)
+ * @tparam Derived 特殊化されたテンプレートの派生クラス(matrixx)の型
+ * @tparam Coordinate 対応するベクトルクラス(vectorx)の型
+ */
+ template <size_t Dim, typename Derived, typename Coordinate>
+ class basic_matrix
+ : boost::addable< Derived
+ , boost::addable2< Derived, float
+ , boost::subtractable< Derived
+ , boost::subtractable2< Derived, float
+ , boost::multipliable< Derived
+ , boost::multipliable2< Derived, float
+ , boost::dividable2< Derived, float
+ , boost::equality_comparable< Derived
+ > > > > > > > >
+ {
+ protected:
+//{{{ size
+ /**
+ * @brief components_のサイズを返す
+ */
+ size_t size() const
+ {
+ return Dim * (Dim + 1);
+ }
+//}}}
+ union
+ {
+ float components_[Dim * (Dim + 1)];
+ float elements_[Dim][Dim + 1];
+ };///< 要素の配列(アフィン行列なので,最後の行は省略している)
+ public:
+ // コンストラクタ,デストラクタはデフォルトのものを使う
+ // 代入演算子,コピーコンストラクタはデフォルトのものを使う
+//{{{ swap
+ void swap(Derived& rhs) throw()
+ {
+ using std::swap;
+ for (size_t i = 0; i < size(); ++i) {
+ swap(components_[i], rhs.components_[i]);
+ }
+ }
+//}}}
+//{{{ operator +=
+ Derived& operator+=(const Derived& rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] += rhs.components_[i];
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Derived& operator+=(float rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] += rhs;
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ friend Derived operator+(float rhs1, Derived& rhs2)
+ {
+ float tmp[size()];
+ for (size_t i = 0; i < size(); ++i) {
+ tmp[i] = rhs1 + rhs2.components_[i];
+ }
+ return Derived(tmp);
+ }
+//}}}
+//{{{ operator -=
+ Derived& operator-=(const Derived& rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] -= rhs.components_[i];
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Derived& operator-=(float rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] -= rhs;
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ friend Derived operator-(float rhs1, Derived& rhs2)
+ {
+ float tmp[size()];
+ for (size_t i = 0; i < size(); ++i) {
+ tmp[i] = rhs1 - rhs2.components_[i];
+ }
+ return Derived(tmp);
+ }
+//}}}
+//{{{ operator *=
+ Derived& operator*=(const Derived& rhs)
+ {
+<<<<<<< HEAD
+ Derived M;
+ for (size_t i = 0; i < Dim; ++i) {
+ for (size_t j = 0; j <= Dim; ++j) {
+ float sum = 0;
+ for (size_t k = 0; k <= Dim; ++k) {
+ sum += at(i, k) * rhs.at(k, j);
+ }
+ M.elements_[i][j] = sum;
+=======
+ Derived retval;
+ const int SIZE = Dim + 1;
+
+ // calculate the last element previously
+ int b = last_index() - Dim;
+ int c = Dim;
+ float last_sum = 0;
+ for (int i = 0; i < SIZE; ++i) {
+ last_sum += elements_[b + i] * rhs.elements_[c + i * SIZE];
+ }
+ retval.elements_[last_index()] = 1;
+
+ for (int a = last_index() - 1; a >= 0; --a) {
+ int b = a / SIZE * SIZE;
+ int c = a % SIZE;
+ float sum = 0;
+ for (int i = 0; i < SIZE; ++i) {
+ sum += elements_[b + i] * rhs.elements_[c + i * SIZE];
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ }
+ }
+ *this = M;
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Derived& operator*=(float rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] *= rhs;
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Coordinate operator*(const Coordinate& rhs)
+ {
+ float arr[Dim];
+ for (size_t i = 0; i < Dim; ++i) {
+ float sum = 0;
+ for (size_t k = 0; k <= Dim; ++k) {
+ sum += (*this)[i][k] * rhs[k];
+ }
+ arr[i] = sum;
+ }
+ return Coordinate(arr);
+ }
+//}}}
+//{{{ operator /=
+ Derived& operator/=(float rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] /= rhs;
+ }
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+//}}}
+//{{{ operator ==
+ bool operator==(const Derived& rhs) const
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ if (std::abs(components_[i] - rhs.components_[i]) > MOF_ERROR_THRESHOLD) return false;
+ }
+ return true;
+ }
+//}}}
+//{{{ operator []
+ /**
+ * @note M[i][j]のように参照可能
+ * @note この方法による複数の要素への参照は非効率
+ */
+ row_of_matrix<Dim> const operator [](size_t i) const
+ {
+ return row_of_matrix<Dim>(i, elements_);
+ }
+//}}}
+//{{{ operator <<
+ /**
+ * @brief デバッグ出力用ストリーム演算子
+ * @param [in] stream 出力ストリーム
+ * @param [in] rhs 出力対象となる行列オブジェクト
+ * @return 引数で与えられた出力ストリーム
+ */
+ friend std::ostream& operator<<
+ (
+ std::ostream& stream,
+ const Derived& rhs
+ )
+ {
+ for (size_t i = 0; i < Dim + 1; ++i) {
+ for (size_t j = 0; j < Dim + 1; ++j) {
+ if (j != 0) stream << ", ";
+ else if (j != Dim) stream << "\n";
+ stream << std::setw(5) << rhs[i][j];
+ }
+ }
+ return stream;
+ }
+//}}}
+//{{{ at
+ /**
+ * @brief 行列の要素にアクセス
+ * @param[in] 行番号
+ * @param[in] 列番号
+ * @return 要素
+ */
+ const float at(size_t i, size_t j) const
+ {
+ if (i < Dim ) return elements_[i][j];
+ if (j == Dim) return 1;
+ else return 0;
+ }
+//}}}
+ };
+//{{{ swap
+ template <size_t Dim, typename Derived, typename Coordinate>
+ void swap
+ (
+ basic_matrix<Dim, Derived, Coordinate>& a,
+ basic_matrix<Dim, Derived, Coordinate>& b
+ ) throw()
+ {
+ a.swap(b);
+ }
+//}}}
+//{{{ row_of_matrix
+ /**
+ * @brief M[i][j]のように行列の要素を取得するための補助クラス
+ */
+ template <size_t Dim>
+ class row_of_matrix
+ {
+ size_t index_;
+ const float (&elements_)[Dim][Dim+1];
+ public:
+ row_of_matrix(size_t i, const float (&elements)[Dim][Dim+1])
+ : index_(i), elements_(elements)
+ {
+ }
+
+ float operator[](size_t j) const
+ {
+ if (Dim != index_) return elements_[index_][j];
+ else if (Dim == j) return 1;
+ else return 0;
+ }
+ };
+//}}}
+}// namespace math
+}// namespace mof
for (size_t i = 0; i < Dim; ++i) components_[i] *= rhs;
return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
}
-<<<<<<< HEAD
-
- friend Derived operator*(float rhs1, Derived& rhs2)
- {
- Derived retval;
- for (size_t i = 0; i < Dim; ++i) retval.components_[i] = rhs1 * rhs2.components_[i];
- return retval;
- }
-=======
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
//}}}
//{{{ operator /=
Derived& operator/=(float rhs)
}// namespace math
}// namespace mof
+
--- /dev/null
+#pragma once
+#include <mof/base/mofdef.hpp>
+#include <mof/math/threshold.hpp>
+#include <boost/operators.hpp>
+#include <ostream>
+#include <cmath>
+
+
+namespace mof
+{
+namespace math
+{
+ /**
+ * @brief 同次座標ベクトルテンプレートクラス
+ * @note このテンプレートから直接特殊化することは想定していない.
+ * あくまでvectorxを実装するための補助テンプレートである.
+ * このクラスは不変クラスである.
+ * @tparam Dim ベクトルの次元(要素数)
+ * @tparam Derived 特殊化されたテンプレートの派生クラス(vectorx)の型
+ */
+ template <size_t Dim, typename Derived>
+ class basic_vector
+ : boost::addable< Derived
+ , boost::addable2< Derived, float
+ , boost::subtractable< Derived
+ , boost::subtractable2< Derived, float
+ , boost::multipliable2< Derived, float
+ , boost::dividable2< Derived, float
+ , boost::equality_comparable< Derived
+ > > > > > > >
+ {
+ protected:
+ float components_[Dim];///< 要素の配列
+ public:
+ // コンストラクタ,デストラクタはデフォルトのものを使う
+ // 代入演算子,コピーコンストラクタはデフォルトのものを使う
+//{{{ swap
+ void swap(Derived& rhs) throw()
+ {
+ for (size_t i = 0; i < Dim; ++i) std::swap(rhs.components_[i], components_[i]);
+ }
+//}}}
+//{{{ operator +=
+ Derived& operator+=(const Derived& rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] += rhs.components_[i];
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Derived& operator+=(float rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] += rhs;
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ friend Derived operator+(float rhs1, Derived& rhs2)
+ {
+ Derived retval;
+ for (size_t i = 0; i < Dim; ++i) retval.components_[i] = rhs1 + rhs2.components_[i];
+ return retval;
+ }
+//}}}
+//{{{ operator -=
+ Derived& operator-=(const Derived& rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] -= rhs.components_[i];
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ Derived& operator-=(float rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] -= rhs;
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+
+ friend Derived operator-(float rhs1, Derived& rhs2)
+ {
+ Derived retval;
+ for (size_t i = 0; i < Dim; ++i) retval.components_[i] = rhs1 - rhs2.components_[i];
+ return retval;
+ }
+//}}}
+//{{{ operator *=
+ Derived& operator*=(float rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] *= rhs;
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+<<<<<<< HEAD
+
+ friend Derived operator*(float rhs1, Derived& rhs2)
+ {
+ Derived retval;
+ for (size_t i = 0; i < Dim; ++i) retval.components_[i] = rhs1 * rhs2.components_[i];
+ return retval;
+ }
+=======
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+//}}}
+//{{{ operator /=
+ Derived& operator/=(float rhs)
+ {
+ for (size_t i = 0; i < Dim; ++i) components_[i] /= rhs;
+ return *reinterpret_cast<Derived*>(this);//thisがDerived型であることは保証されている.
+ }
+//}}}
+//{{{ operator ==
+ bool operator==(const Derived& rhs) const
+ {
+ for (size_t i = 0; i < Dim; ++i) {
+ if (std::abs(components_[i] - rhs.components_[i]) > MOF_ERROR_THRESHOLD) return false;
+ }
+ return true;
+ }
+//}}}
+//{{{ operator []
+ /**
+ * @note Dim+1番目の要素は常に1
+ * @param[in] index 添字
+ */
+ float operator[](size_t index) const
+ {
+ if (index < Dim) return components_[index];
+ else if (index == Dim) return 1;
+ // TODO exception
+ return 0;
+ }
+//}}}
+//{{{ operator <<
+ /**
+ * @brief デバッグ出力用ストリーム演算子
+ * @param[in] stream 出力ストリーム
+ * @param[in] rhs 出力対象となるベクトルオブジェクト
+ * @return 引数で与えられた出力ストリーム
+ */
+ friend std::ostream& operator<<
+ (
+ std::ostream& stream,
+ const Derived& rhs
+ )
+ {
+ for (size_t i = 0; i < Dim; ++i)
+ {
+ if (i != 0) stream << ", ";
+ stream << rhs.components_[i];
+ }
+ return stream;
+ }
+//}}}
+ };
+//{{{ swap
+ template <size_t Dim, typename Derived>
+ void swap(basic_vector<Dim, Derived>& a, basic_vector<Dim, Derived>& b) throw()
+ {
+ a.swap(b);
+ }
+//}}}
+
+}// namespace math
+}// namespace mof
//{{{ operator =
matrix2& operator = (const matrix2& rhs)
{
-<<<<<<< HEAD
for (size_t i = 0; i < size(); ++i) {
components_[i] = rhs.components_[i];
-=======
- for (size_t i = 0; i <= last_index() - 1; ++i) {
- elements_[i] = rhs.elements_[i];
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
}
return *this;
}
--- /dev/null
+#pragma once
+#include <mof/math/basic_matrix.hpp>
+
+namespace mof
+{
+namespace math
+{
+ class vector2;
+
+ /**
+ * @brief 2次元同次座標変換行列クラス
+ */
+ class matrix2 : public basic_matrix<2, matrix2, vector2>
+ {
+ public:
+//{{{ constructor
+ /**
+ * @brief デフォルトコンストラクタ
+ * @note 効率のため,初期化は行われない
+ */
+ matrix2()
+ {
+ }
+
+ /**
+ * @brief 指定した値で初期化する.
+ * @tparam T 配列型(operator[]をオーバーロードしていること)
+ * @param[in] arr 初期化用配列
+ */
+ template <class T>
+ explicit matrix2(const T& arr)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = arr[i];
+ }
+ }
+
+ /**
+ * @brief 指定した値で初期化する.
+ */
+ matrix2
+ (
+ float m11, float m12, float m13,
+ float m21, float m22, float m23
+ )
+ {
+ const float* table[] =
+ {
+ &m11, &m12, &m13,
+ &m21, &m22, &m23
+ };
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = *table[i];
+ }
+ }
+
+//}}}
+//{{{ copy constructor
+ matrix2(const matrix2& rhs)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = rhs.components_[i];
+ }
+ }
+//}}}
+//{{{ operator =
+ matrix2& operator = (const matrix2& rhs)
+ {
+<<<<<<< HEAD
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = rhs.components_[i];
+=======
+ for (size_t i = 0; i <= last_index() - 1; ++i) {
+ elements_[i] = rhs.elements_[i];
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ }
+ return *this;
+ }
+//}}}
+ };
+
+}// namespace math
+}// namespace mof
//{{{ copy constructor
matrix3(const matrix3& rhs)
{
-<<<<<<< HEAD
for (size_t i = 0; i < size(); ++i) {
components_[i] = rhs.components_[i];
-=======
- for (size_t i = 0; i <= last_index(); ++i) {
- elements_[i] = rhs.elements_[i];
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
}
}
//}}}
*/
matrix3& operator = (const matrix3& rhs)
{
-<<<<<<< HEAD
for (size_t i = 0; i < size(); ++i) {
components_[i] = rhs.components_[i];
-=======
- for (size_t i = 0; i <= last_index() -1; ++i) {
- elements_[i] = rhs.elements_[i];
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
}
return *this;
}
--- /dev/null
+#pragma once
+#include <mof/math/basic_matrix.hpp>
+
+namespace mof
+{
+namespace math
+{
+ class vector3;
+
+ /**
+ * @brief 3次元同次座標変換行列クラス
+ */
+ class matrix3 : public basic_matrix<3, matrix3, vector3>
+ {
+ public:
+//{{{ constructor
+ /**
+ * @brief デフォルトコンストラクタ
+ * @note 効率のため,初期化は行われない
+ */
+ matrix3()
+ {
+ }
+
+ /**
+ * @brief 指定した値で初期化する.
+ * @tparam T 配列型(operator[]をオーバーロードしていること)
+ * @param[in] arr 初期化用配列
+ */
+ template <class T>
+ explicit matrix3(const T& arr)
+ {
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = arr[i];
+ }
+ }
+
+ /**
+ * @brief 指定した値で初期化する.
+ */
+ matrix3
+ (
+ float m11, float m12, float m13, float m14,
+ float m21, float m22, float m23, float m24,
+ float m31, float m32, float m33, float m34
+ )
+ {
+ const float* table[] =
+ {
+ &m11, &m12, &m13, &m14,
+ &m21, &m22, &m23, &m24,
+ &m31, &m32, &m33, &m34
+ };
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = *table[i];
+ }
+ }
+
+//}}}
+//{{{ copy constructor
+ matrix3(const matrix3& rhs)
+ {
+<<<<<<< HEAD
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = rhs.components_[i];
+=======
+ for (size_t i = 0; i <= last_index(); ++i) {
+ elements_[i] = rhs.elements_[i];
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ }
+ }
+//}}}
+//{{{ operator =
+ /**
+ * @note コピーのパフォーマンスのためにこの関数の定義は重要
+ */
+ matrix3& operator = (const matrix3& rhs)
+ {
+<<<<<<< HEAD
+ for (size_t i = 0; i < size(); ++i) {
+ components_[i] = rhs.components_[i];
+=======
+ for (size_t i = 0; i <= last_index() -1; ++i) {
+ elements_[i] = rhs.elements_[i];
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ }
+ return *this;
+ }
+//}}}
+ };
+
+}// namespace math
+}// namespace mof
matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
matrix3 N2(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
matrix3 N = N1 * N2;
-<<<<<<< HEAD
matrix3 A(44, 50, 56, 66, 116, 134, 152, 178, 188, 218, 248, 290);
-=======
- matrix3 A(44, 50, 56, 66, 116, 134, 152, 178, 188, 218, 248, 290, 0, 0, 0);
- cout << N << endl;
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
if (N != A) {
cerr << "Failed:" << "Affine matrix N1 * N2 test" << endl;
failed_count++;
}
{
-<<<<<<< HEAD
matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
-=======
- matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
- matrix3 N2(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
- matrix3 N = N1 * N2;
- matrix3 A
- (
- 0.25840f, 0.28424f, 0.31008f, 0.17054f,
- 0.58915f, 0.65633f, 0.72351f, 0.45995f,
- 0.91990f, 1.02842f, 1.13695f, 0.74935f,
- 0.70801f, 0.81912f, 0.93023f
- );
- if (N != A) {
- cerr << "Failed:" << "Projective matrix N1 * N2 test" << endl;
- failed_count++;
- }
- }
-
- {
- matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
->>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
vector3 v(1, 2, 3);
vector3 w = M * v;
vector3 a(18, 46, 74);
--- /dev/null
+#include <mof/math/matrix3.hpp>
+#include <mof/math/vector3.hpp>
+#include <iostream>
+#include <sstream>
+
+int main()
+{
+ using namespace mof::math;
+ using namespace std;
+ int failed_count = 0;
+
+ {
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ bool failed = false;
+ for (size_t i = 0; i < 12; ++i) {
+ if (M[i / 4][i % 4] != i + 1) failed = true;
+ }
+ if (failed || M[3][0] != 0 || M[3][1] != 0 || M[3][2] != 0 || M[3][3] != 1) {
+ cerr << "Failed:" << " initialize test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ float arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+ matrix3 M(arr);
+ bool failed = false;
+ for (size_t i = 0; i < 12; ++i) {
+ if (M[i / 4][i % 4] != i + 1) failed = true;
+ }
+ if (failed || M[3][0] != 0 || M[3][1] != 0 || M[3][2] != 0 || M[3][3] != 1) {
+ cerr << "Failed:" << " initialize by array test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ if (M != N) {
+ cerr << "Failed:" << " M == N test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N(0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ if (M == N) {
+ cerr << "Failed:" << " M != N test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N2(29, 27, 25, 23, 21, 19, 17, 15, 13, 11, 9, 7);
+ matrix3 N = N1 + N2;
+ matrix3 A(30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19);
+ if (N != A) {
+ cerr << "Failed:" << " addition test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N2(29, 27, 25, 23, 21, 19, 17, 15, 13, 11, 9, 7);
+ matrix3 N = N1 - N2;
+ matrix3 A
+ (
+ -28, -25, -22, -19,
+ -16, -13, -10, -7,
+ -4, -1, 2, 5
+ );
+ if (N != A) {
+ cerr << "Failed:" << " subtraction test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N2(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
+ matrix3 N = N1 * N2;
+<<<<<<< HEAD
+ matrix3 A(44, 50, 56, 66, 116, 134, 152, 178, 188, 218, 248, 290);
+=======
+ matrix3 A(44, 50, 56, 66, 116, 134, 152, 178, 188, 218, 248, 290, 0, 0, 0);
+ cout << N << endl;
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ if (N != A) {
+ cerr << "Failed:" << "Affine matrix N1 * N2 test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+<<<<<<< HEAD
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+=======
+ matrix3 N1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
+ matrix3 N2(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
+ matrix3 N = N1 * N2;
+ matrix3 A
+ (
+ 0.25840f, 0.28424f, 0.31008f, 0.17054f,
+ 0.58915f, 0.65633f, 0.72351f, 0.45995f,
+ 0.91990f, 1.02842f, 1.13695f, 0.74935f,
+ 0.70801f, 0.81912f, 0.93023f
+ );
+ if (N != A) {
+ cerr << "Failed:" << "Projective matrix N1 * N2 test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
+>>>>>>> 01b4978a6b6cd046ad399644ea70ddec977a688a
+ vector3 v(1, 2, 3);
+ vector3 w = M * v;
+ vector3 a(18, 46, 74);
+ if (a != w) {
+ cerr << "Failed:" << "M * v test" << endl;
+ failed_count++;
+ }
+ }
+
+
+ {
+ matrix3 M(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ matrix3 N = 2 * M * 3;
+ matrix3 A(6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72);
+ if (N != A) {
+ cerr << "Failed:" << "s * M * s test" << endl;
+ failed_count++;
+ }
+ }
+
+ {
+ matrix3 M(6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72);
+ matrix3 N = M / 6;
+ matrix3 A(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+ if (N != A) {
+ cerr << "Failed:" << "M / s test" << endl;
+ failed_count++;
+ }
+ }
+
+ return failed_count;
+}