+いらないfriend削除
実装の理由のメモ
+make_zero
test using boost::test
make_rotation make_scaling *** test
make_rotation make_scaling *** impl
set(CMAKE_CXX_FLAGS "/EHsc /nologo /W4 /WX")
elseif (CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_CXX_FLAGS " -Wall -O3 -DNDEBUG -std=c++0x")
- #set(CMAKE_CXX_FLAGS "-Wall -Werror -g -DPSTSDK_VALIDATION_LEVEL_FULL -std=c++0x")
+ #set(CMAKE_CXX_FLAGS "-Wall -g -std=c++0x")
endif()
-Start testing: Sep 01 12:40 PDT
+Start testing: Sep 03 15:16 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 01 12:40 PDT
+"vector2_test" start time: Sep 03 15:16 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.09 sec
+Test time = 0.01 sec
----------------------------------------------------------
Test Passed.
-"vector2_test" end time: Sep 01 12:40 PDT
+"vector2_test" end time: Sep 03 15:16 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 01 12:40 PDT
+"vector3_test" start time: Sep 03 15:16 PDT
Output:
----------------------------------------------------------
<end of output>
-Test time = 0.01 sec
+Test time = 0.02 sec
----------------------------------------------------------
Test Passed.
-"vector3_test" end time: Sep 01 12:40 PDT
+"vector3_test" end time: Sep 03 15:16 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 01 12:40 PDT
+"matrix2_test" start time: Sep 03 15:16 PDT
Output:
----------------------------------------------------------
-Failed: default constructor makes zero matrix
-Failed: initialize by array test
-Failed: addition test
-Failed: subtraction test
-Failed:Affine matrix N1 * N2 test
-Failed:Projective matrix N1 * N2 test
-Failed:M * v test
-Failed:s * M * s test
-Failed:M / s test
<end of output>
Test time = 0.01 sec
----------------------------------------------------------
-Test Failed.
-"matrix2_test" end time: Sep 01 12:40 PDT
+Test Passed.
+"matrix2_test" end time: Sep 03 15:16 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 01 12:40 PDT
+"matrix3_test" start time: Sep 03 15:16 PDT
Output:
----------------------------------------------------------
-Failed: initialize test
-Failed: initialize by array test
-Failed:Affine matrix N1 * N2 test
-Failed:M * v test
-Failed:s * M * s test
<end of output>
-Test time = 0.01 sec
+Test time = 0.02 sec
----------------------------------------------------------
-Test Failed.
-"matrix3_test" end time: Sep 01 12:40 PDT
+Test Passed.
+"matrix3_test" end time: Sep 03 15:16 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 01 12:40 PDT
+"make_matrix_test" start time: Sep 03 15:16 PDT
Output:
----------------------------------------------------------
-Failed --- make_identity2
<end of output>
Test time = 0.01 sec
----------------------------------------------------------
-Test Failed.
-"make_matrix_test" end time: Sep 01 12:40 PDT
+Test Passed.
+"make_matrix_test" end time: Sep 03 15:16 PDT
"make_matrix_test" time elapsed: 00:00:00
----------------------------------------------------------
-End testing: Sep 01 12:40 PDT
+End testing: Sep 03 15:16 PDT
-3:matrix2_test
4:matrix3_test
-5:make_matrix_test
{
namespace math
{
+ template <size_t Dim> class row_of_matrix;
+
/**
- * @brief アフィン同次変換行列テンプレートクラス
+ * @brief 同次座標変換行列テンプレートクラス
+ * @note このクラスはアフィン変換行列を表す
* @note このテンプレートから直接特殊化することは想定していない.
* あくまでmatrixxを実装するための補助テンプレートである.
* このクラスは不変クラスである.
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) {
- M.elements_[i][j] += (*this)[i][k] * rhs[k][j];
+ sum += at(i, k) * rhs.at(k, j);
}
+ M.elements_[i][j] = sum;
}
}
*this = M;
{
float arr[Dim];
for (size_t i = 0; i < Dim; ++i) {
+ float sum = 0;
for (size_t k = 0; k <= Dim; ++k) {
- arr[i] += (*this)[i][k] * rhs[k];
+ sum += (*this)[i][k] * rhs[k];
}
+ arr[i] = sum;
}
return Coordinate(arr);
}
* @note M[i][j]のように参照可能
* @note この方法による複数の要素への参照は非効率
*/
- const float* const operator [](size_t i) const
+ row_of_matrix<Dim> const operator [](size_t i) const
{
- static float arr[] = {0, 0, 0, 1};
- if (i < Dim) return &components_[i * Dim];
- return arr;
- // TODO index bounds check
+ return row_of_matrix<Dim>(i, elements_);
}
//}}}
//{{{ operator <<
*/
const float at(size_t i, size_t j) const
{
- return elements_[i][j];
+ if (i < Dim ) return elements_[i][j];
+ if (j == Dim) return 1;
+ else return 0;
}
//}}}
};
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
namespace math
{
/**
- * @brief å\90\8c次å\88\97ベクトルテンプレートクラス
+ * @brief å\90\8c次座æ¨\99ベクトルテンプレートクラス
* @note このテンプレートから直接特殊化することは想定していない.
* あくまでvectorxを実装するための補助テンプレートである.
* このクラスは不変クラスである.
class vector2;
/**
- * @brief 2次元アフィン同次座標変換行列クラス
+ * @brief 2次元同次座標変換行列クラス
*/
class matrix2 : public basic_matrix<2, matrix2, vector2>
{
public:
//{{{ constructor
/**
- * @brief デフォルトコンストラクタ.最後の要素以外を0で初期化する.
+ * @brief デフォルトコンストラクタ
+ * @note 効率のため,初期化は行われない
*/
matrix2()
{
- for (size_t i = 0; i < size(); ++i) {
- components_[i] = 0;
- }
}
/**
&m11, &m12, &m13,
&m21, &m22, &m23
};
- for (size_t i = 0; i < size() - 1; ++i) {
+ for (size_t i = 0; i < size(); ++i) {
components_[i] = *table[i];
}
}
class vector3;
/**
- * @brief 3次元アフィン同次座標変換行列クラス
+ * @brief 3次元同次座標変換行列クラス
*/
class matrix3 : public basic_matrix<3, matrix3, vector3>
{
public:
//{{{ constructor
/**
- * @brief デフォルトコンストラクタ.最後の要素以外を0で初期化する.
+ * @brief デフォルトコンストラクタ
+ * @note 効率のため,初期化は行われない
*/
matrix3()
{
- for (size_t i = 0; i < size(); ++i) {
- components_[i] = 0;
- }
}
/**
int failed_count = 0;
{
- matrix2 M;
- bool failed = false;
- for (size_t i = 0; i < 8; ++i) {
- if (M[i / 3][i % 3] != 0) failed = true;
- }
- if (failed || M[2][2] != 1) {
- cerr << "Failed:" << " default constructor makes zero matrix" << endl;
- failed_count++;
- }
- }
-
- {
float arr[] = {1, 2, 3, 4, 5, 6};
matrix2 M(arr);
bool failed = false;
}
}
- // any operations keeps the last component 1
{
matrix2 M(1, 2, 3, 4, 5, 6);
matrix2 N(1, 2, 3, 4, 5, 6);
}
{
- matrix2 N1(1, 2, 3, 4, 5, 6);
- matrix2 N2(2, 3, 4, 5, 6, 7);
- matrix2 N = N1 * N2;
- matrix2 A(0.42353f, 0.49412f, 0.24706f, 0.95294f, 1.12941f, 0.67059f);
- if (N != A) {
- cerr << "Failed:" << "Projective matrix N1 * N2 test" << endl;
- failed_count++;
- }
- }
-
- {
matrix2 M(1, 2, 3, 4, 5, 6);
vector2 v(1, 2);
vector2 w = M * v;
- vector2 a(0.33333f, 0.83333f);
+ vector2 a(8, 20);
if (a != w) {
cerr << "Failed:" << "M * v test" << endl;
failed_count++;
int failed_count = 0;
{
- matrix3 M;
- bool failed = false;
- for (size_t i = 0; i < 15; ++i) {
- if (M[i / 4][i % 4] != 0) failed = true;
- }
- if (failed || M[3][3] != 1) {
- cerr << "Failed:" << " default constructor makes zero matrix" << endl;
- failed_count++;
- }
- }
-
- {
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] != 1 || M[3][2] != 2 || M[3][3] != 1) {
+ 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++;
}
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] != 1 || M[3][2] != 2 || M[3][3] != 1) {
+ 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);
vector3 v(1, 2, 3);
vector3 w = M * v;
- vector3 a(0.20690f, 0.52874f, 0.85057f);
+ 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, 125);
+ 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) {
vector2 v;
int failed_count = 0;
- if (v.x() != 0 || v.y() != 0 || v.z() != 1) {
- cerr << "Failed:" << " default constructor makes zero vector" << endl;
- failed_count++;
- }
-
v = vector2(1, 2);
if (v[0] != 1 || v[1] != 2 || v[2] != 1) {
cerr << "Failed:" << " initialize by array" << endl;
vector3 v;
int failed_count = 0;
- if (v.x() != 0 || v.y() != 0 || v.z() != 0 || v.w() != 1) {
- cerr << "Failed:" << " default constructor makes zero vector" << endl;
- failed_count++;
- }
-
v = vector3(1, 2, 3);
if (v[0] != 1 || v[1] != 2 || v[2] != 3 || v[3] != 1) {
cerr << "Failed:" << " initialize by array" << endl;
namespace math
{
/**
- * @brief 2次å\85\83å\90\8c次å\88\97ベクトルクラス
+ * @brief 2次å\85\83å\90\8c次座æ¨\99ベクトルクラス
*/
class vector2 : public basic_vector<2, vector2>
{
public:
//{{{ constructor
/**
- * @brief デフォルトコンストラクタ.全ての要素を0で初期化する.
+ * @brief デフォルトコンストラクタ.
+ * @note 効率のためは初期化されない
*/
vector2()
{
- components_[0] = 0;
- components_[1] = 0;
}
/**
namespace math
{
/**
- * @brief 3次å\85\83å\90\8c次å\88\97ベクトルクラス
+ * @brief 3次å\85\83å\90\8c次座æ¨\99ベクトルクラス
*/
class vector3 : public basic_vector<3, vector3>
{
public:
//{{{ constructor
/**
- * @brief デフォルトコンストラクタ.全ての要素を0で初期化する.
+ * @brief デフォルトコンストラクタ.
+ * @note 効率のためは初期化されない
*/
vector3()
{
- components_[0] = 0;
- components_[1] = 0;
- components_[2] = 0;
}
/**