WLinearAlgebraFunctions.cpp 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25 26
#include <vector>

Mathias Goldau's avatar
Mathias Goldau committed
27
#include "../WAssert.h"
28
#include "../WLimits.h"
29

30 31
#include "WLinearAlgebraFunctions.h"
#include "WMatrix.h"
32
#include "linearAlgebra/WVectorFixed.h"
33

34
WVector3d multMatrixWithVector3D( WMatrix<double> mat, WVector3d vec )
35
{
36
    WVector3d result;
37 38 39 40 41 42
    result[0] = mat( 0, 0 ) * vec[0] + mat( 0, 1 ) * vec[1] + mat( 0, 2 ) * vec[2];
    result[1] = mat( 1, 0 ) * vec[0] + mat( 1, 1 ) * vec[1] + mat( 1, 2 ) * vec[2];
    result[2] = mat( 2, 0 ) * vec[0] + mat( 2, 1 ) * vec[1] + mat( 2, 2 ) * vec[2];
    return result;
}

43
WVector3d transformVector3DWithMatrix4D( WMatrix<double> mat, WVector3d vec )
44 45 46 47 48 49 50 51
{
    WAssert( mat.getNbRows() == 4 && mat.getNbCols() == 4, "Matrix has wrong size." );
    std::vector< double > resultVec4D( 4 );
    resultVec4D[0] = mat( 0, 0 ) * vec[0] + mat( 0, 1 ) * vec[1] + mat( 0, 2 ) * vec[2] /* + mat( 0, 3 ) * 0 */;
    resultVec4D[1] = mat( 1, 0 ) * vec[0] + mat( 1, 1 ) * vec[1] + mat( 1, 2 ) * vec[2] /* + mat( 1, 3 ) * 0 */;
    resultVec4D[2] = mat( 2, 0 ) * vec[0] + mat( 2, 1 ) * vec[1] + mat( 2, 2 ) * vec[2] /* + mat( 2, 3 ) * 0 */;
    resultVec4D[3] = mat( 3, 0 ) * vec[0] + mat( 3, 1 ) * vec[1] + mat( 3, 2 ) * vec[2] /* + mat( 3, 3 ) * 0 */;

52
    WVector3d result;
53 54 55 56 57 58
    result[0] = resultVec4D[0] / resultVec4D[3];
    result[1] = resultVec4D[1] / resultVec4D[3];
    result[2] = resultVec4D[2] / resultVec4D[3];
    return result;
}

59
WPosition transformPosition3DWithMatrix4D( WMatrix<double> mat, WPosition vec )
60 61 62 63 64 65 66 67
{
    WAssert( mat.getNbRows() == 4 && mat.getNbCols() == 4, "Matrix has wrong size." );
    std::vector< double > resultVec4D( 4 );
    resultVec4D[0] = mat( 0, 0 ) * vec[0] + mat( 0, 1 ) * vec[1] + mat( 0, 2 ) * vec[2] + mat( 0, 3 ) * 1;
    resultVec4D[1] = mat( 1, 0 ) * vec[0] + mat( 1, 1 ) * vec[1] + mat( 1, 2 ) * vec[2] + mat( 1, 3 ) * 1;
    resultVec4D[2] = mat( 2, 0 ) * vec[0] + mat( 2, 1 ) * vec[1] + mat( 2, 2 ) * vec[2] + mat( 2, 3 ) * 1;
    resultVec4D[3] = mat( 3, 0 ) * vec[0] + mat( 3, 1 ) * vec[1] + mat( 3, 2 ) * vec[2] + mat( 3, 3 ) * 1;

68
    WPosition result;
69 70 71 72 73 74
    result[0] = resultVec4D[0] / resultVec4D[3];
    result[1] = resultVec4D[1] / resultVec4D[3];
    result[2] = resultVec4D[2] / resultVec4D[3];
    return result;
}

75 76
WMatrix<double> invertMatrix3x3( WMatrix<double> mat )
{
77 78
    WAssert( mat.getNbRows(), "Zero rows found." );
    WAssert( mat.getNbCols(), "Zero columns found." );
79 80 81 82 83 84 85
    double det = mat( 0, 0 ) * mat( 1, 1 ) * mat( 2, 2 ) +
                mat( 0, 1 ) * mat( 1, 2 ) * mat( 2, 0 ) +
                mat( 0, 2 ) * mat( 1, 0 ) * mat( 2, 1 ) -
                mat( 0, 2 ) * mat( 1, 1 ) * mat( 2, 0 ) -
                mat( 0, 1 ) * mat( 1, 0 ) * mat( 2, 2 ) -
                mat( 0, 0 ) * mat( 1, 2 ) * mat( 2, 1 );

Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
86
    WAssert( det != 0, "Determinant is zero. This matrix can not be inverted." );
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101
    WMatrix<double> r( 3, 3 );

    r( 0, 0 ) = ( mat( 1, 1 ) * mat( 2, 2 ) - mat(  1, 2 ) * mat( 2, 1 ) ) / det;
    r( 1, 0 ) = ( mat( 1, 2 ) * mat( 2, 0 ) - mat(  1, 0 ) * mat( 2, 2 ) ) / det;
    r( 2, 0 ) = ( mat( 1, 0 ) * mat( 2, 1 ) - mat(  1, 1 ) * mat( 2, 0 ) ) / det;
    r( 0, 1 ) = ( mat( 0, 2 ) * mat( 2, 1 ) - mat(  0, 1 ) * mat( 2, 2 ) ) / det;
    r( 1, 1 ) = ( mat( 0, 0 ) * mat( 2, 2 ) - mat(  0, 2 ) * mat( 2, 0 ) ) / det;
    r( 2, 1 ) = ( mat( 0, 1 ) * mat( 2, 0 ) - mat(  0, 0 ) * mat( 2, 1 ) ) / det;
    r( 0, 2 ) = ( mat( 0, 1 ) * mat( 1, 2 ) - mat(  0, 2 ) * mat( 1, 1 ) ) / det;
    r( 1, 2 ) = ( mat( 0, 2 ) * mat( 1, 0 ) - mat(  0, 0 ) * mat( 1, 2 ) ) / det;
    r( 2, 2 ) = ( mat( 0, 0 ) * mat( 1, 1 ) - mat(  0, 1 ) * mat( 1, 0 ) ) / det;

    return r;
}
102 103 104

WMatrix<double> invertMatrix4x4( WMatrix<double> mat )
{
105 106
    WAssert( mat.getNbRows(), "Zero rows found." );
    WAssert( mat.getNbCols(), "Zero columns found." );
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
    double det =
        mat( 0, 0 ) * mat( 1, 1 ) * mat( 2, 2 ) * mat( 3, 3 ) +
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 2, 3 ) * mat( 3, 1 ) +
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 2, 1 ) * mat( 3, 2 ) +

        mat( 0, 1 ) * mat( 1, 0 ) * mat( 2, 3 ) * mat( 3, 2 ) +
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 2, 0 ) * mat( 3, 3 ) +
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 2, 2 ) * mat( 3, 0 ) +

        mat( 0, 2 ) * mat( 1, 0 ) * mat( 2, 1 ) * mat( 3, 3 ) +
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 2, 3 ) * mat( 3, 0 ) +
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 2, 0 ) * mat( 3, 1 ) +

        mat( 0, 3 ) * mat( 1, 0 ) * mat( 2, 2 ) * mat( 3, 1 ) +
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 2, 0 ) * mat( 3, 2 ) +
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 2, 1 ) * mat( 3, 0 ) -

        mat( 0, 0 ) * mat( 1, 1 ) * mat( 2, 3 ) * mat( 3, 2 ) -
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 2, 1 ) * mat( 3, 3 ) -
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 2, 2 ) * mat( 3, 1 ) -

        mat( 0, 1 ) * mat( 1, 0 ) * mat( 2, 2 ) * mat( 3, 3 ) -
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 2, 3 ) * mat( 3, 0 ) -
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 2, 0 ) * mat( 3, 2 ) -

        mat( 0, 2 ) * mat( 1, 0 ) * mat( 2, 3 ) * mat( 3, 1 ) -
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 2, 0 ) * mat( 3, 3 ) -
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 2, 1 ) * mat( 3, 0 ) -

        mat( 0, 3 ) * mat( 1, 0 ) * mat( 2, 1 ) * mat( 3, 2 ) -
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 2, 2 ) * mat( 3, 0 ) -
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 2, 0 ) * mat( 3, 1 );

    WMatrix<double> result( 4, 4 );

    result( 0, 0 ) =
        mat( 1, 1 ) * mat( 2, 2 ) * mat( 3, 3 ) +
        mat( 1, 2 ) * mat( 2, 3 ) * mat( 3, 1 ) +
        mat( 1, 3 ) * mat( 2, 1 ) * mat( 3, 2 ) -
        mat( 1, 1 ) * mat( 2, 3 ) * mat( 3, 2 ) -
        mat( 1, 2 ) * mat( 2, 1 ) * mat( 3, 3 ) -
        mat( 1, 3 ) * mat( 2, 2 ) * mat( 3, 1 );

    result( 0, 1 ) =
        mat( 0, 1 ) * mat( 2, 3 ) * mat( 3, 2 ) +
        mat( 0, 2 ) * mat( 2, 1 ) * mat( 3, 3 ) +
        mat( 0, 3 ) * mat( 2, 2 ) * mat( 3, 1 ) -
        mat( 0, 1 ) * mat( 2, 2 ) * mat( 3, 3 ) -
        mat( 0, 2 ) * mat( 2, 3 ) * mat( 3, 1 ) -
        mat( 0, 3 ) * mat( 2, 1 ) * mat( 3, 2 );

    result( 0, 2 ) =
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 3, 3 ) +
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 3, 1 ) +
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 3, 2 ) -
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 3, 2 ) -
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 3, 3 ) -
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 3, 1 );

    result( 0, 3 ) =
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 2, 2 ) +
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 2, 3 ) +
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 2, 1 ) -
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 2, 3 ) -
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 2, 1 ) -
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 2, 2 );

    result( 1, 0 ) =
        mat( 1, 0 ) * mat( 2, 3 ) * mat( 3, 2 ) +
        mat( 1, 2 ) * mat( 2, 0 ) * mat( 3, 3 ) +
        mat( 1, 3 ) * mat( 2, 2 ) * mat( 3, 0 ) -
        mat( 1, 0 ) * mat( 2, 2 ) * mat( 3, 3 ) -
        mat( 1, 2 ) * mat( 2, 3 ) * mat( 3, 0 ) -
        mat( 1, 3 ) * mat( 2, 0 ) * mat( 3, 2 );

    result( 1, 1 ) =
        mat( 0, 0 ) * mat( 2, 2 ) * mat( 3, 3 ) +
        mat( 0, 2 ) * mat( 2, 3 ) * mat( 3, 0 ) +
        mat( 0, 3 ) * mat( 2, 0 ) * mat( 3, 2 ) -
        mat( 0, 0 ) * mat( 2, 3 ) * mat( 3, 2 ) -
        mat( 0, 2 ) * mat( 2, 0 ) * mat( 3, 3 ) -
        mat( 0, 3 ) * mat( 2, 2 ) * mat( 3, 0 );

    result( 1, 2 ) =
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 3, 2 ) +
        mat( 0, 2 ) * mat( 1, 0 ) * mat( 3, 3 ) +
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 3, 0 ) -
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 3, 3 ) -
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 3, 0 ) -
        mat( 0, 3 ) * mat( 1, 0 ) * mat( 3, 2 );

    result( 1, 3 ) =
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 2, 3 ) +
        mat( 0, 2 ) * mat( 1, 3 ) * mat( 2, 0 ) +
        mat( 0, 3 ) * mat( 1, 0 ) * mat( 2, 2 ) -
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 2, 2 ) -
        mat( 0, 2 ) * mat( 1, 0 ) * mat( 2, 3 ) -
        mat( 0, 3 ) * mat( 1, 2 ) * mat( 2, 0 );

    result( 2, 0 ) =
        mat( 1, 0 ) * mat( 2, 1 ) * mat( 3, 3 ) +
        mat( 1, 1 ) * mat( 2, 3 ) * mat( 3, 0 ) +
        mat( 1, 3 ) * mat( 2, 0 ) * mat( 3, 1 ) -
        mat( 1, 0 ) * mat( 2, 3 ) * mat( 3, 1 ) -
        mat( 1, 1 ) * mat( 2, 0 ) * mat( 3, 3 ) -
        mat( 1, 3 ) * mat( 2, 1 ) * mat( 3, 0 );

    result( 2, 1 ) =
        mat( 0, 0 ) * mat( 2, 3 ) * mat( 3, 1 ) +
        mat( 0, 1 ) * mat( 2, 0 ) * mat( 3, 3 ) +
        mat( 0, 3 ) * mat( 2, 1 ) * mat( 3, 0 ) -
        mat( 0, 0 ) * mat( 2, 1 ) * mat( 3, 3 ) -
        mat( 0, 1 ) * mat( 2, 3 ) * mat( 3, 0 ) -
        mat( 0, 3 ) * mat( 2, 0 ) * mat( 3, 1 );

    result( 2, 2 ) =
        mat( 0, 0 ) * mat( 1, 1 ) * mat( 3, 3 ) +
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 3, 0 ) +
        mat( 0, 3 ) * mat( 1, 0 ) * mat( 3, 1 ) -
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 3, 1 ) -
        mat( 0, 1 ) * mat( 1, 0 ) * mat( 3, 3 ) -
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 3, 0 );

    result( 2, 3 ) =
        mat( 0, 0 ) * mat( 1, 3 ) * mat( 2, 1 ) +
        mat( 0, 1 ) * mat( 1, 0 ) * mat( 2, 3 ) +
        mat( 0, 3 ) * mat( 1, 1 ) * mat( 2, 0 ) -
        mat( 0, 0 ) * mat( 1, 1 ) * mat( 2, 3 ) -
        mat( 0, 1 ) * mat( 1, 3 ) * mat( 2, 0 ) -
        mat( 0, 3 ) * mat( 1, 0 ) * mat( 2, 1 );

    result( 3, 0 ) =
        mat( 1, 0 ) * mat( 2, 2 ) * mat( 3, 1 ) +
        mat( 1, 1 ) * mat( 2, 0 ) * mat( 3, 2 ) +
        mat( 1, 2 ) * mat( 2, 1 ) * mat( 3, 0 ) -
        mat( 1, 0 ) * mat( 2, 1 ) * mat( 3, 2 ) -
        mat( 1, 1 ) * mat( 2, 2 ) * mat( 3, 0 ) -
        mat( 1, 2 ) * mat( 2, 0 ) * mat( 3, 1 );

    result( 3, 1 ) =
        mat( 0, 0 ) * mat( 2, 1 ) * mat( 3, 2 ) +
        mat( 0, 1 ) * mat( 2, 2 ) * mat( 3, 0 ) +
        mat( 0, 2 ) * mat( 2, 0 ) * mat( 3, 1 ) -
        mat( 0, 0 ) * mat( 2, 2 ) * mat( 3, 1 ) -
        mat( 0, 1 ) * mat( 2, 0 ) * mat( 3, 2 ) -
        mat( 0, 2 ) * mat( 2, 1 ) * mat( 3, 0 );

    result( 3, 2 ) =
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 3, 1 ) +
        mat( 0, 1 ) * mat( 1, 0 ) * mat( 3, 2 ) +
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 3, 0 ) -
        mat( 0, 0 ) * mat( 1, 1 ) * mat( 3, 2 ) -
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 3, 0 ) -
        mat( 0, 2 ) * mat( 1, 0 ) * mat( 3, 1 );

    result( 3, 3 ) =
        mat( 0, 0 ) * mat( 1, 1 ) * mat( 2, 2 ) +
        mat( 0, 1 ) * mat( 1, 2 ) * mat( 2, 0 ) +
        mat( 0, 2 ) * mat( 1, 0 ) * mat( 2, 1 ) -
        mat( 0, 0 ) * mat( 1, 2 ) * mat( 2, 1 ) -
        mat( 0, 1 ) * mat( 1, 0 ) * mat( 2, 2 ) -
        mat( 0, 2 ) * mat( 1, 1 ) * mat( 2, 0 );

    WAssert( det != 0, "Determinat is zero. This matrix can not be inverted." );

    double detInv = 1. / det;
    for( size_t r = 0; r < 4; ++r)
    {
        for( size_t c = 0; c < 4; ++c )
        {
            result( c, r ) *= detInv;
        }
    }

    return result;
}
283

284
bool linearIndependent( const WVector3d& u, const WVector3d& v )
285
{
286
    WVector3d cp = cross( u, v );
287
    if( std::fabs( cp[0] ) < wlimits::DBL_EPS && std::fabs( cp[1] ) < wlimits::DBL_EPS && std::fabs( cp[2] ) < wlimits::DBL_EPS )
288 289 290 291 292
    {
        return false;
    }
    return true;
}