matrix3x3.h 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
 *
 * Matrix class (3x3)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *-
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef MATRIX3X3_H_
#define MATRIX3X3_H_

#include <math/vector2d.h>

/**
 * Class MATRIX3x3 describes a general 3x3 matrix.
 *
 * Any linear transformation in 2D can be represented
 * by a homogeneous 3x3 transformation matrix. Given a vector x, the linear transformation
 * with the transformation matrix M is given as
 *
 * x' = M * x
 *
 * To represent an affine transformation, homogeneous coordinates have to be used. That means
 * the 2D-vector (x, y) has to be extended to a 3D-vector by a third component (x, y, 1).
 *
 * Transformations can be easily combined by matrix multiplication.
 *
 * A * (B * x ) = (A * B) * x
 * ( A, B: transformation matrices, x: vector )
 *
 * This class was implemented using templates, so flexible type combinations are possible.
 *
 */

// Forward declaration for template friends
template <class T>
class MATRIX3x3;

template <class T>
std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );

template <class T>
class MATRIX3x3
{
public:
    T m_data[3][3];

    /**
     * @brief Constructor
     *
     * Initialize all matrix members to zero.
     */
    MATRIX3x3();

    /**
     * @brief Constructor
     *
     * Initialize with given matrix members
     *
     * @param a00 is the component [0,0].
     * @param a01 is the component [0,1].
     * @param a02 is the component [0,2].
     * @param a11 is the component [1,1].
     * @param a12 is the component [1,2].
     * @param a13 is the component [1,3].
     * @param a20 is the component [2,0].
     * @param a21 is the component [2,1].
     * @param a00 is the component [2,2].
     */
    MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 );

    /**
     * @brief Set the matrix to the identity matrix.
     *
     * The diagonal components of the matrix are set to 1.
     */
    void SetIdentity();

    /**
     * @brief Set the translation components of the matrix.
     *
     * @param aTranslation is the translation, specified as 2D-vector.
     */
    void SetTranslation( VECTOR2<T> aTranslation );

    /**
     * @brief Get the translation components of the matrix.
     *
     * @return is the translation (2D-vector).
     */
    VECTOR2<T> GetTranslation() const;

    /**
     * @brief Set the rotation components of the matrix.
     *
     * The angle needs to have a positive value for an anti-clockwise rotation.
     *
     * @param aAngle is the rotation angle in [rad].
     */
    void SetRotation( T aAngle );

    /**
     * @brief Set the scale components of the matrix.
     *
     * @param aScale contains the scale factors, specified as 2D-vector.
     */
    void SetScale( VECTOR2<T> aScale );

    /**
     * @brief Get the scale components of the matrix.
     *
     * @return the scale factors, specified as 2D-vector.
     */
    VECTOR2<T> GetScale() const;

    /**
     * @brief Compute the determinant of the matrix.
     *
     * @return the determinant value.
     */
    T Determinant() const;

    /**
     * @brief Determine the inverse of the matrix.
     *
     * The inverse of a transformation matrix can be used to revert a transformation.
     *
     * x = Minv * ( M * x )
     * ( M: transformation matrix, Minv: inverse transformation matrix, x: vector)
     *
     * @return the inverse matrix.
     */
    MATRIX3x3 Inverse() const;

    /**
     * @brief Get the transpose of the matrix.
     *
     * @return the transpose matrix.
     */
    MATRIX3x3 Transpose() const;

    /**
     * @brief Output to a stream.
     */
    friend std::ostream& operator<<<T>( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );

};

// Operators

//! @brief Matrix multiplication
template <class T> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB );

//! @brief Multiplication with a 2D vector, the 3rd z-component is assumed to be 1
template <class T> VECTOR2<T> const operator*( MATRIX3x3<T> const& aA, VECTOR2<T> const& aB );

//! @brief Multiplication with a scalar
template <class T, class S> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, T aScalar );
template <class T, class S> MATRIX3x3<T> const operator*( T aScalar, MATRIX3x3<T> const& aMatrix );

// ----------------------
// --- Implementation ---
// ----------------------

template <class T>
MATRIX3x3<T>::MATRIX3x3()
{
    for( int j = 0; j < 3; j++ )
    {
        for( int i = 0; i < 3; i++ )
        {
            m_data[i][j] = 0.0;
        }
    }
}


template <class T>
MATRIX3x3<T>::MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 )
{
    m_data[0][0] = a00;
    m_data[0][1] = a01;
    m_data[0][2] = a02;

    m_data[1][0] = a10;
    m_data[1][1] = a11;
    m_data[1][2] = a12;

    m_data[2][0] = a20;
    m_data[2][1] = a21;
    m_data[2][2] = a22;
}


template <class T>
void MATRIX3x3<T>::SetIdentity( void )
{
    for( int j = 0; j < 3; j++ )
    {
        for( int i = 0; i < 3; i++ )
        {
            if( i == j )
                m_data[i][j] = 1.0;
            else
                m_data[i][j] = 0.0;
        }
    }
}


template <class T>
void MATRIX3x3<T>::SetTranslation( VECTOR2<T> aTranslation )
{
    m_data[0][2] = aTranslation.x;
    m_data[1][2] = aTranslation.y;
}


template <class T>
VECTOR2<T> MATRIX3x3<T>::GetTranslation() const
{
    VECTOR2<T> result;
    result.x = m_data[0][2];
    result.y = m_data[1][2];

    return result;
}


template <class T>
void MATRIX3x3<T>::SetRotation( T aAngle )
{
    T cosValue = cos( aAngle );
    T sinValue = sin( aAngle );
    m_data[0][0] = cosValue;
    m_data[0][1] = -sinValue;
    m_data[1][0] = sinValue;
    m_data[1][1] = cosValue;
}


template <class T>
void MATRIX3x3<T>::SetScale( VECTOR2<T> aScale )
{
    m_data[0][0] = aScale.x;
    m_data[1][1] = aScale.y;
}


template <class T>
VECTOR2<T> MATRIX3x3<T>::GetScale() const
{
    VECTOR2<T> result( m_data[0][0], m_data[1][1] );

    return result;
}


template <class T>
MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB )
{
    MATRIX3x3<T> result;

    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
        {
            result.m_data[i][j] = aA.m_data[i][0] * aB.m_data[0][j] +
                                  aA.m_data[i][1] * aB.m_data[1][j] +
                                  aA.m_data[i][2] * aB.m_data[2][j];
        }
    }

    return result;
}


template <class T>
VECTOR2<T> const operator*( MATRIX3x3<T> const& aMatrix, VECTOR2<T> const& aVector )
{
    VECTOR2<T> result( 0, 0 );
    result.x = aMatrix.m_data[0][0] * aVector.x + aMatrix.m_data[0][1] * aVector.y
               + aMatrix.m_data[0][2];
    result.y = aMatrix.m_data[1][0] * aVector.x + aMatrix.m_data[1][1] * aVector.y
               + aMatrix.m_data[1][2];

    return result;
}


template <class T>
T MATRIX3x3<T>::Determinant() const
{
    return m_data[0][0] * ( m_data[1][1] * m_data[2][2] - m_data[1][2] * m_data[2][1] )
           - m_data[0][1] * ( m_data[1][0] * m_data[2][2] - m_data[1][2] * m_data[2][0] )
           + m_data[0][2] * ( m_data[1][0] * m_data[2][1] - m_data[1][1] * m_data[2][0] );
}


template <class T, class S>
MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aMatrix, S aScalar )
{
    MATRIX3x3<T> result;

    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
        {
            result.m_data[i][j] = aMatrix.m_data[i][j] * aScalar;
        }
    }

    return result;
}


template <class T, class S>
MATRIX3x3<T> const operator*( S aScalar, MATRIX3x3<T> const& aMatrix )
{
    return aMatrix * aScalar;
}


template <class T>
MATRIX3x3<T> MATRIX3x3<T>::Inverse() const
{
    MATRIX3x3<T> result;

    result.m_data[0][0] = m_data[1][1] * m_data[2][2] - m_data[2][1] * m_data[1][2];
    result.m_data[0][1] = m_data[0][2] * m_data[2][1] - m_data[2][2] * m_data[0][1];
    result.m_data[0][2] = m_data[0][1] * m_data[1][2] - m_data[1][1] * m_data[0][2];

    result.m_data[1][0] = m_data[1][2] * m_data[2][0] - m_data[2][2] * m_data[1][0];
    result.m_data[1][1] = m_data[0][0] * m_data[2][2] - m_data[2][0] * m_data[0][2];
    result.m_data[1][2] = m_data[0][2] * m_data[1][0] - m_data[1][2] * m_data[0][0];

    result.m_data[2][0] = m_data[1][0] * m_data[2][1] - m_data[2][0] * m_data[1][1];
    result.m_data[2][1] = m_data[0][1] * m_data[2][0] - m_data[2][1] * m_data[0][0];
    result.m_data[2][2] = m_data[0][0] * m_data[1][1] - m_data[1][0] * m_data[0][1];

    return result * ( 1.0 / Determinant() );
}


template <class T>
MATRIX3x3<T> MATRIX3x3<T>::Transpose() const
{
    MATRIX3x3<T> result;

    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
        {
            result.m_data[j][i] = m_data[i][j];
        }
    }

    return result;
}


template <class T>
std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix )
{
    for( int i = 0; i < 3; i++ )
    {
        aStream << "| ";

        for( int j = 0; j < 3; j++ )
        {
            aStream << aMatrix.m_data[i][j];
            aStream << " ";
        }

        aStream << "|";
        aStream << "\n";
    }

    return aStream;
}


/* Default specializations */
typedef MATRIX3x3<double> MATRIX3x3D;

#endif /* MATRIX3X3_H_ */