English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. General (https://eab.abime.net/forumdisplay.php?f=37)
-   -   Matrix calculation questions (HowToCode) (https://eab.abime.net/showthread.php?t=62764)

kae 19 January 2012 09:19

Matrix calculation questions (HowToCode)
 
Hi.
First post and already som questions :)

I'm trying to understand the calculation of the rotation matrix in "How to code" (http://www.mways.co.uk/amiga/howtoco...s.php#rotation).
In the calculations of zx he has ...;s2*c1+c3*s1 and also a shorter one for zy. I undstand all the other calculations, but I can't understand what is meant with this. Is it a shortened version of some kind of the first one? If so, how?

xx=c2*c1
xy=c2*s1
xz=s2
yx=c3*s1+s3*s2*c1
yy=-c3*c1+s3*s2*s1
yz=-s3*c2
zx=s3*s1-c3*s2*c1;s2*c1+c3*s1
zy=-s3*c1-c3*s2*s1;c3*c1-s2*s1
zz=c3*c2

c1=cos(angle1), c2=cos(angle2), c3=cos(angle3)
s1=sin(angle1), s2=sin(angle2), s3=sin(angle3)

Thanks for any insight :)

Kalms 19 January 2012 09:47

This section in the text identifies the values for xx,xy,.. etc:

Code:

  x''= (x*c1+y*s1)*c2+z*s2= c2*c1 *x + c2*s1 *y + s2 *z
        ^^^^^^^^^^^=x'      ^^^^^ xx  ^^^^^ xy  ^^ xz

  y''= (x*s1-y*c1)*c3+((x*c1+y*s1)*s2-z*c2)*s3=
        c3*s1 *x - c3*c1 *y + s3*s2*c1 *x + s3*s2*s1 *y - s3*c2 *z=

        (s3*s2*c1+c3*s1) *x + (s3*s2*s1-c3*c1) *y + (-s3*c2) *z
        ^^^^^^^^^^^^^^^^ yx  ^^^^^^^^^^^^^^^^ yy  ^^^^^^^^ yz

  z''= (x*s1-y*c1)*s3-((x*c1+y*s1)*s2-z*c2)*c3=
        s3*s1 *x - s3*c1 *y - c3*s2*c1 *x - c3*s2*s1 *y + c3*c2 *z=

        (-c3*s2*c1+s3*s1) *x + (-c3*s2*s1-c3*c1) *y + (c3*c2) *z
        ^^^^^^^^^^^^^^^^^ zx  ^^^^^^^^^^^^^^^^^ zy  ^^^^^^^ zz

So the parts after the ; that you're wondering about are erroneous and can be removed.

Even with that change, there are small differences between the section that I posted and the section that you posted so there are probably some transcription errors hiding in there. You may end up having to redo the maths from the rotation formulae yourself.

kae 19 January 2012 10:29

Thanks for clearing that up :)
That was my initial thought, that the parts after ; was erroneous and commented out or something as the first part was what I got when I did the math myself. But I had to ask if it was some kind of notation I didn't know.
I'm redoing the math just so I can understand better what I'm doing. I noticed some differences too when I did, a sin that should've been cos for instance.

NovaCoder 20 January 2012 02:42

This might help:

Code:

// Polygon Nova 3D Engine.
// Filename:amath.hpp
/***************************************************************
Copyright (C) 1999 Novasoft Consulting

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************/


#ifndef __AMATH_HPP
#define __AMATH_HPP


/*
** Matrix Definitions
*/
class        AstMatrix3x3;
class AstMatrix4x3;
/******************************************************************************/
class AstMatrix3x3
{
  protected:
        float data[3][3];

  public:

        void Transpose( void );

        const float * operator[] ( int a ) const { return data[a]; }
        float      * operator[] ( int a ) { return data[a]; }
};
/******************************************************************************/
class AstMatrix4x3
{
  protected:
        float data[4][3];

  public:

        void MakeIdentity( void );

        void MakeXTranslation( float amount );
        void MakeYTranslation( float amount );
        void MakeZTranslation( float amount );

        void MakeXRotation( float amount );
        void MakeYRotation( float amount );
        void MakeZRotation( float amount );

        void NormalizeDirectionVectors( void );

        void GetRotationMatrix( AstMatrix3x3 &rm ) const;
        void SetRotationMatrix( const AstMatrix3x3 &rm );
        void SetTranslation( float x, float y, float z )
        {
                data[3][0] = x;
                data[3][1] = y;
                data[3][2] = z;
        }
        void SetTranslation(const NovaVector &v)
        {
                SetTranslation( v.X, v.Y, v.Z );
        }

        void GetTranslation(NovaVector &anv_dest) const
        {
          anv_dest.X = data[3][0];
          anv_dest.Y = data[3][1];
          anv_dest.Z = data[3][2];
        }

        NovaVector GetTranslation(void) const
        {
          return NovaVector( data[3][0], data[3][1], data[3][2] );
        }

        void GetVU( NovaVector &v ) const
        {
                v.X = data[0][1];
                v.Y = data[1][1];
                v.Z = data[2][1];
        }
        void GetVR( NovaVector &v ) const
        {
                v.X = data[0][0];
                v.Y = data[1][0];
                v.Z = data[2][0];
        }
        void GetVF( NovaVector &v ) const
        {
                v.X = data[0][2];
                v.Y = data[1][2];
                v.Z = data[2][2];
        }

        void SetVU( const NovaVector &v )
        {
                data[0][1] = v.X;
                data[1][1] = v.Y;
                data[2][1] = v.Z;
        }
        void SetVR( const NovaVector &v )
        {
                data[0][0] = v.X;
                data[1][0] = v.Y;
                data[2][0] = v.Z;
        }
        void SetVF( const NovaVector &v )
        {
                data[0][2] = v.X;
                data[1][2] = v.Y;
                data[2][2] = v.Z;
        }

        const float * operator[] ( int a ) const { return data[a]; }
        float      * operator[] ( int a ) { return data[a]; }
};

AstMatrix4x3 operator * ( const AstMatrix4x3 &a, const AstMatrix4x3 &b );



#endif

Code:

// Polygon Nova 3D Engine.
// Filename:Mat3x3.cpp
/***************************************************************
Copyright (C) 1999 Novasoft Consulting

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************/



#include "../p_nova.h"


/*
** void AstMatrix3x3::Transpose( void )
**
** The inverse of a 3x3 rotation matrix is its transpose, and thus
** this function transposes its data (flips it over the diagonal)
** to facilitate optimized inversion.
*/
void AstMatrix3x3::Transpose( void )
{
float tmp;

  tmp = data[0][1];
  data[0][1] = data[1][0];
  data[1][0] = tmp;

  tmp = data[0][2];
  data[0][2] = data[2][0];
  data[2][0] = tmp;

  tmp = data[1][2];
  data[1][2] = data[2][1];
  data[2][1] = tmp;
}

Code:

// Polygon Nova 3D Engine.
// Filename:Mat4x3.cpp
/***************************************************************
Copyright (C) 1999 Novasoft Consulting

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************/



#include "../p_nova.h"


/*
** void AstAstMatrix4x3::MakeXRotation( float degrees )
**
** Sets the rotation indices to the correct values to make
** this matrix a rotation matrix of about the given axis.
*/
void
AstMatrix4x3::MakeXRotation( float degrees )
{
 double radian;
 radian = (M_PI / 180) * degrees;

 data[1][1] =  (float)cos( radian );
 data[2][2] =  (float)cos( radian );
 data[2][1] = (float)-sin( radian );
 data[1][2] =  (float)sin( radian );
}

void
AstMatrix4x3::MakeYRotation( float degrees )
{
 double radian;
 radian = (M_PI / 180) * degrees;

 data[0][0] =  (float)cos( radian );
 data[0][2] = (float)-sin( radian );
 data[2][0] =  (float)sin( radian );
 data[2][2] =  (float)cos( radian );
}

void
AstMatrix4x3::MakeZRotation( float degrees )
{
 double radian;

 radian = (M_PI / 180) * degrees;

 data[0][0] =  (float)cos( radian );
 data[1][1] =  (float)cos( radian );
 data[0][1] =  (float)sin( radian );
 data[1][0] = (float)-sin( radian );
}

/*
** void AstMatrix4x3::Make?Translation( amount )
**
** Sets the correct matrix elements to make a translation matrix
** of "amount" units along the given axis.
*/
void AstMatrix4x3::MakeXTranslation( float amount )
{
        data[3][0] = amount;
        data[3][1] = 0;
        data[3][2] = 0;
}
void AstMatrix4x3::MakeYTranslation( float amount )
{
        data[3][0] = 0;
        data[3][1] = amount;
        data[3][2] = 0;
}
void AstMatrix4x3::MakeZTranslation( float amount )
{
        data[3][0] = 0;
        data[3][1] = 0;
        data[3][2] = amount;
}

/*
** Private identity matrix data set
*/
const static float _identity_matrix_4x3[4][3] = {
        { 1, 0, 0 },
        { 0, 1, 0 },
        { 0, 0, 1 },
        { 0, 0, 0 }
};

/*
** void AstMatrix4x3::MakeIdentity( void )
**
** Makes the matrix into an identity matrix by copying the identity
** matrix source data values into its own data set.
*/
void AstMatrix4x3::MakeIdentity( void )
{
        memcpy( data, _identity_matrix_4x3, sizeof( _identity_matrix_4x3 ) );
}

/*
** AstMatrix4x3 operator * ( const AstMatrix4x3 &a, const AstMatrix4x3 &b )
**
** Overloaded * operator that multiplies two matrices together.
*/
AstMatrix4x3 operator * ( const AstMatrix4x3 &a, const AstMatrix4x3 &b )
{
 static AstMatrix4x3 m;

        /*
        **  m[0][0] = a[0][0] * b[0][0] +
        **            a[0][1] * b[1][0] +
        **            a[0][2] * b[2][0] +
        **          ( a[0][3] * b[3][0] ) = 0;
        */
        m[0][0] = ( a[0][0] * b[0][0] ) +
                                ( a[0][1] * b[1][0] ) +
                                ( a[0][2] * b[2][0] );

        /*
        **  m[0][1] = a[0][0] * b[0][1] +
        **            a[0][1] * b[1][1] +
        **            a[0][2] * b[2][1] +
        **          ( a[0][3] * b[3][1] ) = 0;
        */
        m[0][1] = ( a[0][0] * b[0][1] ) +
                                ( a[0][1] * b[1][1] ) +
                                ( a[0][2] * b[2][1] );

        /*
        **  m[0][2] = a[0][0] * b[0][2] +
        **            a[0][1] * b[1][2] +
        **            a[0][2] * b[2][2] +
        **          ( a[0][3] * b[3][2] ) = 0;
        */
        m[0][2] = ( a[0][0] * b[0][2] ) +
                                ( a[0][1] * b[1][2] ) +
                                ( a[0][2] * b[2][2] );

        /*
        **  m[1][0] = a[1][0] * b[0][0] +
        **            a[1][1] * b[1][0] +
        **            a[1][2] * b[2][0] +
        **          ( a[1][3] * b[3][0] ) = 0;
        */
        m[1][0] = ( a[1][0] * b[0][0] ) +
                                ( a[1][1] * b[1][0] ) +
                                ( a[1][2] * b[2][0] );

        /*
        **  m[1][1] = a[1][0] * b[0][1] +
        **            a[1][1] * b[1][1] +
        **            a[1][2] * b[2][1] +
        **          ( a[1][3] * b[3][1] ) = 0;
        */
        m[1][1] = ( a[1][0] * b[0][1] ) +
                                ( a[1][1] * b[1][1] ) +
                                ( a[1][2] * b[2][1] );

        /*
        **  m[1][2] = a[1][0] * b[0][2] +
        **            a[1][1] * b[1][2] +
        **            a[1][2] * b[2][2] +
        **          ( a[1][3] * b[3][2] ) = 0;
        */
        m[1][2] = ( a[1][0] * b[0][2] ) +
                                ( a[1][1] * b[1][2] ) +
                                ( a[1][2] * b[2][2] );

        /*
        **  m[2][0] = a[2][0] * b[0][0] +
        **            a[2][1] * b[1][0] +
        **            a[2][2] * b[2][0] +
        **          ( a[2][3] * b[3][0] ) = 0;
        */
        m[2][0] = ( a[2][0] * b[0][0] ) +
                                ( a[2][1] * b[1][0] ) +
                                ( a[2][2] * b[2][0] );

        /*
        **  m[2][1] = a[2][0] * b[0][1] +
        **            a[2][1] * b[1][1] +
        **            a[2][2] * b[2][1] +
        **          ( a[2][3] * b[3][1] ) = 0;
        */
        m[2][1] = ( a[2][0] * b[0][1] ) +
                                ( a[2][1] * b[1][1] ) +
                                ( a[2][2] * b[2][1] );

        /*
        **  m[2][2] = a[2][0] * b[0][2] +
        **            a[2][1] * b[1][2] +
        **            a[2][2] * b[2][2] +
        **          ( a[2][3] * b[3][2] ) = 0;
        */
        m[2][2] = ( a[2][0] * b[0][2] ) +
                                ( a[2][1] * b[1][2] ) +
                                ( a[2][2] * b[2][2] );

        /*
        **  m[3][0] = a[3][0] * b[0][0] +
        **            a[3][1] * b[1][0] +
        **            a[3][2] * b[2][0] +
        **          ( a[3][3] * b[3][0] ) = b[3][0];
        */
        m[3][0] = ( a[3][0] * b[0][0] ) +
                                ( a[3][1] * b[1][0] ) +
                                ( a[3][2] * b[2][0] ) +
                                b[3][0];

        /*
        **  m[3][1] = a[3][0] * b[0][1] +
        **            a[3][1] * b[1][1] +
        **            a[3][2] * b[2][1] +
        **          ( a[3][3] * b[3][1] ) = b[3][1];
        */
        m[3][1] = ( a[3][0] * b[0][1] ) +
                                ( a[3][1] * b[1][1] ) +
                                ( a[3][2] * b[2][1] ) +
                                b[3][1];

        /*
        **  m[3][2] = a[3][0] * b[0][2] +
        **            a[3][1] * b[1][2] +
        **            a[3][2] * b[2][2] +
        **          ( a[3][3] * b[3][2] ) = b[3][2];
        */
        m[3][2] = ( a[3][0] * b[0][2] ) +
                                ( a[3][1] * b[1][2] ) +
                                ( a[3][2] * b[2][2] ) +
                                b[3][2];

        return m;
}

/*
** void AstMatrix4x3::GetRotationMatrix( Matrix3x3 &m ) const
**
** This routine copies the rotation submatrix of the matrix into
** the 3x3 rotation matrix passed to the function.
*/
void AstMatrix4x3::GetRotationMatrix(AstMatrix3x3 &m) const
{
 for(int i = 0; i < 3; i++)
  memcpy(m[i], data[i], sizeof(float) * 3);
}

/*
** void AstMatrix4x3::SetRotationMatrix( const Matrix3x3 &m )
**
** Sets the rotation submatrix of the given matrix to the 3x3
** rotation matrix passed to this member function.
*/
void AstMatrix4x3::SetRotationMatrix(const AstMatrix3x3 &m)
{
 for(int i = 0; i < 3; i++)
  memcpy(data[i], m[i], sizeof(float) * 3);
}

/*
** void AstMatrix4x3::NormalizeDirectionVectors( void )
**
** This function normalizes the direction vectors of the matrix,
** preventing unwanted spurious scaling from creeping in as a
** a result of accumulated error.
*/
void
AstMatrix4x3::NormalizeDirectionVectors(void)
{
NovaVector v;

        GetVU(v);
        v.Normalize();
        SetVU(v);

        GetVR(v);
        v.Normalize();
        SetVR(v);

        GetVF(v);
        v.Normalize();
        SetVF(v);
}



All times are GMT +2. The time now is 06:05.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.05446 seconds with 11 queries