// L============================================================================= // L This software is distributed under the MIT license. // L Copyright 2021 Péter Kardos // L============================================================================= #pragma once #include "../Matrix/MatrixImpl.hpp" #include "../Vector.hpp" namespace mathter { template class Rotation2DBuilder { public: Rotation2DBuilder(const T& angle) : angle(angle) {} Rotation2DBuilder& operator=(const Rotation2DBuilder&) = delete; template operator Matrix() const { Matrix m; Set(m); return m; } template operator Matrix() const { Matrix m; Set(m); return m; } template operator Matrix() const { Matrix m; Set(m); return m; } template operator Matrix() const { Matrix m; Set(m); return m; } private: template void Set(Matrix& m) const { T C = cos(angle); T S = sin(angle); auto elem = [&m](int i, int j) -> U& { return Order == eMatrixOrder::FOLLOW_VECTOR ? m(i, j) : m(j, i); }; // Indices according to follow vector order elem(0, 0) = U(C); elem(0, 1) = U(S); elem(1, 0) = U(-S); elem(1, 1) = U(C); // Rest for (int j = 0; j < m.ColumnCount(); ++j) { for (int i = (j < 2 ? 2 : 0); i < m.RowCount(); ++i) { m(i, j) = U(j == i); } } } const T angle; }; /// Creates a 2D rotation matrix. /// Counter-clockwise angle in radians. template auto Rotation(const T& angle) { return Rotation2DBuilder{ angle }; } } // namespace mathter