POV-Ray : Newsgroups : povray.advanced-users : array to matrix Server Time
23 Dec 2024 02:15:04 EST (-0500)
  array to matrix (Message 1 to 7 of 7)  
From: CAD-Andi
Subject: array to matrix
Date: 18 Mar 2011 13:35:00
Message: <web.4d839726a6b29534bf952ed30@news.povray.org>
Hi!

1. Is there a simple way to populate a matrix with an array?
2. Is there a simple way to multiply two matrixes with each other and have the
result stored in an array?

Thanks!
Andi


Post a reply to this message

From: CAD-Andi
Subject: Re: array to matrix
Date: 18 Mar 2011 14:05:00
Message: <web.4d839dd09e47287bbf952ed30@news.povray.org>
What I asked above can of course be done with a lot of typing. So I did, now
here comes the real question:

Sitiuation: I have a object that has been located with this transformation
matrix somewhere in space:

matrix
<0.000934544,-0.000208202,-0.000288581,-0.000206635,-0.000977747,3.62463e-005,-0.000289705,2.5757e-005,-0.000956769,-1.
40094,-0.307333,2.40367>

Now I need to rotate that object 10 degrees around it's own Z-Axis and after
that 20 degrees around the new resulting Y-Axis.

I found out how to rotate objects around the world axis using matrixes, but I
need to rotate around the objects current axis system. How do I accomplish that?

Thanks,
Andi


Post a reply to this message

From: Le Forgeron
Subject: Re: array to matrix
Date: 18 Mar 2011 14:33:57
Message: <4d83a595$1@news.povray.org>
Le 18/03/2011 19:00, CAD-Andi nous fit lire :
> What I asked above can of course be done with a lot of typing. So I did, now
> here comes the real question:
> 
> Sitiuation: I have a object that has been located with this transformation
> matrix somewhere in space:
> 
> matrix
>
<0.000934544,-0.000208202,-0.000288581,-0.000206635,-0.000977747,3.62463e-005,-0.000289705,2.5757e-005,-0.000956769,-1.
> 40094,-0.307333,2.40367>
> 
> Now I need to rotate that object 10 degrees around it's own Z-Axis and after
> that 20 degrees around the new resulting Y-Axis.
> 
> I found out how to rotate objects around the world axis using matrixes, but I
> need to rotate around the objects current axis system. How do I accomplish that?

Translate the object to the origin, rotate, translate back.

translate -Pos
rotate 10*z
rotate 20*y
translate Pos


Post a reply to this message

From: CAD-Andi
Subject: Re: array to matrix
Date: 18 Mar 2011 15:05:00
Message: <web.4d83abaf9e47287bbf952ed30@news.povray.org>
> Translate the object to the origin, rotate, translate back.
>
> translate -Pos
> rotate 10*z
> rotate 20*y
> translate Pos

Doing this will apply the second rotation around y to the same axis system like
the previous rotation around z. I need to rotate around the "NEW" axis system
that got created while I rotated the object around z. In other words, I would
like to always rotate around a "local" axis system of an object. The values for
this I would like to store in an array for futher programmical canges.

Any idea?

Andi


Post a reply to this message

From: Le Forgeron
Subject: Re: array to matrix
Date: 18 Mar 2011 16:03:13
Message: <4d83ba81$1@news.povray.org>
Le 18/03/2011 19:59, CAD-Andi nous fit lire :
>> Translate the object to the origin, rotate, translate back.
>>
>> translate -Pos
>> rotate 10*z
>> rotate 20*y
>> translate Pos
> 
> Doing this will apply the second rotation around y to the same axis system like
> the previous rotation around z. I need to rotate around the "NEW" axis system
> that got created while I rotated the object around z. In other words, I would
> like to always rotate around a "local" axis system of an object. The values for
> this I would like to store in an array for futher programmical canges.
> 

There is rotation only in the reference system.
Your "new Y" is in fact the rotated Y. It is not new, it is in fact the
old one.
Transform it too, and voilà!

So:
translate -Pos
rotate 10*z
rotate vrotate(20*y,10*z)
translate Pos


Post a reply to this message

From: Trevor G Quayle
Subject: Re: array to matrix
Date: 18 Mar 2011 16:20:01
Message: <web.4d83bd679e47287b81c811d20@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
> Le 18/03/2011 19:59, CAD-Andi nous fit lire :
> >> Translate the object to the origin, rotate, translate back.
> >>
> >> translate -Pos
> >> rotate 10*z
> >> rotate 20*y
> >> translate Pos
> >
> > Doing this will apply the second rotation around y to the same axis system like
> > the previous rotation around z. I need to rotate around the "NEW" axis system
> > that got created while I rotated the object around z. In other words, I would
> > like to always rotate around a "local" axis system of an object. The values for
> > this I would like to store in an array for futher programmical canges.
> >
>
> There is rotation only in the reference system.
> Your "new Y" is in fact the rotated Y. It is not new, it is in fact the
> old one.
> Transform it too, and voilà!
>
> So:
> translate -Pos
> rotate 10*z
> rotate vrotate(20*y,10*z)
> translate Pos

The only addition to this is that if what is referred to as the 'local' axis
system in not parallel with the world axis system, not just at a different
origin.  This is still relatively simple if you know these rotations, just
another level of rotates and unrotates to move to the global origin and back
again.

-tgq


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: array to matrix
Date: 21 Mar 2011 21:10:01
Message: <web.4d87f5959e47287bc734aecd0@news.povray.org>
"CAD-Andi" <nomail@nomail> wrote:
> Hi!
>
> 1. Is there a simple way to populate a matrix with an array?
> 2. Is there a simple way to multiply two matrixes with each other and have the
> result stored in an array?

Hi Andi

Perhaps the code below will answer those questions.

Tor Olav


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// Copyright 2011 by Tor Olav Kristensen, http://subcube.com
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#macro PrintArray(AA)

  #local DimR = dimension_size(AA, 1);
  #local DimC = dimension_size(AA, 2);
  #debug concat("array[", str(DimR, 0, 0), "]")
  #debug concat("[", str(DimC,0, 0), "] {\n")
  #local CntR = 0;
  #while (CntR < DimR)
    #debug " { "
    #local CntC = 0;
    #while (CntC < DimC)
      #debug str(AA[CntR][CntC], 0, -1)
      #if (CntC < DimC - 1)
        #debug ", "
      #end // if
      #local CntC = CntC + 1;
    #end // while
    #debug " }"
    #if (CntR < DimR - 1)
      #debug ","
    #end // if
    #debug "\n"
    #local CntR = CntR + 1;
  #end // while
  #debug "}\n"

#end // macro PrintArray

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#macro TransformFromArray(AA)

  transform {
    matrix <
      AA[0][0], AA[0][1], AA[0][2],
      AA[1][0], AA[1][1], AA[1][2],
      AA[2][0], AA[2][1], AA[2][2],
      AA[3][0], AA[3][1], AA[3][2]
    >
  }

#end // macro TransformFromArray


#macro ArrayFromTransform(TT)

  #local ProbeFn = function { transform { TT } }
  #local vT = ProbeFn(0, 0, 0);
  #local v0 = ProbeFn(1, 0, 0) - vT;
  #local v1 = ProbeFn(0, 1, 0) - vT;
  #local v2 = ProbeFn(0, 0, 1) - vT;

  array[4][4] {
    { v0.x, v0.y, v0.z, 0 },
    { v1.x, v1.y, v1.z, 0 },
    { v2.x, v2.y, v2.z, 0 },
    { vT.x, vT.y, vT.z, 1 }
  }

#end // macro ArrayFromTransform

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// A little test

PrintArray(
  ArrayFromTransform(
    TransformFromArray(
      ArrayFromTransform(
        transform {
          translate <5, 6, 7>
          scale <2, 3, 4>
        }
      )
    )
  )
)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// The first matrix

#declare Angle1 = 10;
#declare Cos1 = cos(radians(Angle1));
#declare Sin1 = sin(radians(Angle1));

#declare T1 =
  transform {
    matrix <
          1,     0,     0,
          0,  Cos1,  Sin1,
          0, -Sin1,  Cos1,
          0,     0,     0
    >
  }

//#declare T1 = transform { rotate Angle1*x }

#declare A1 = ArrayFromTransform(T1)
PrintArray(A1)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// The second matrix

#declare Angle2 = 20;
#declare Cos2 = cos(radians(Angle2));
#declare Sin2 = sin(radians(Angle2));

#declare T2 =
  transform {
    matrix <
       Cos2,     0, -Sin2,
          0,     1,     0,
       Sin2,     0,  Cos2,
          0,     0,     0
    >
  }

//#declare T2 = transform { rotate Angle2*y }

#declare A2 = ArrayFromTransform(T2)
PrintArray(A2)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// Mmultiply

#declare T1T2 =
  transform {
    T1
    T2
  }

#declare T1T2 =
  transform {
    T1
    T2
  }

/*
#declare T1T2 =
  transform {
    rotate Angle1*x
    rotate Angle2*y
  }
*/

#declare A1A2 = ArrayFromTransform(T1T2)
PrintArray(A1A2)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// The third matrix

#declare Angle3 = 70;
#declare Cos3 = cos(radians(Angle3));
#declare Sin3 = sin(radians(Angle3));

#declare T3 =
  transform {
    matrix <
       Cos3,  Sin3,     0,
      -Sin3,  Cos3,     0,
          0,     0,     1,
          0,     0,     0
    >
  }

//#declare T3 = transform { rotate Angle3*z }

#declare A3 = ArrayFromTransform(T3)
PrintArray(A3)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// Multiply again

#declare T1T2T3 =
  transform {
    T1
    T2
    T3
  }

/*
#declare T1T2T3 =
  transform {
    rotate Angle1*x
    rotate Angle2*y
    rotate Angle3*z
  }
*/

#declare A1A2A3 = ArrayFromTransform(T1T2T3)
PrintArray(A1A2A3)

// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.