POV-Ray : Newsgroups : povray.binaries.scene-files : quaternions.inc Server Time
24 May 2024 12:42:17 EDT (-0400)
  quaternions.inc (Message 1 to 10 of 18)  
Goto Latest 10 Messages Next 8 Messages >>>
From: None
Subject: quaternions.inc
Date: 6 Oct 2003 13:15:27
Message: <Xns940C86DC9B1D4None@204.213.191.226>
As an educational exercise for myself I've created a set of macros to 
handle quaternions, which can be useful for rotations. I've compiled 
these macros into an include file which I've attached.

Povray thankfully includes the built-in "vaxis_rotate()" function. That, 
along with a number of great macros in "transforms.inc", allows you to 
handle pretty well all of your "rotational" requirements. So why would 
you want to use quaternions to do rotations in Povray?  Primarily for 
incremental and true angular interpolation rotations. And I've found a 
few other uses myself, you might as well.

Quaternions are 4D vectors. Yes, they use complex numbers and are 
difficult to understand, but you don't need to preoccupy yourself with 
any of this to take advantage of them for 3D rotations. You can think of 
a unit (normalized) quaternion as a representation of a rotation. So one 
single 4D vector can represent any rotation around any axis. When you 
multiply two unit quaternions together, you are effectively adding two 
rotations together. Other quaternion tricks are used in these macros 
which you may find useful.

If anyone else finds this useful, let me know.


Post a reply to this message


Attachments:
Download 'quaternions.inc.txt' (6 KB)

From: Dennis Miller
Subject: Re: quaternions.inc
Date: 6 Oct 2003 16:56:35
Message: <3f81d703$1@news.povray.org>
Could you post a working demo scene?
thanks very much.
D.

"None" <Non### [at] onca> wrote in message
news:Xns### [at] 204213191226...
> As an educational exercise for myself I've created a set of macros to
> handle quaternions, which can be useful for rotations. I've compiled
> these macros into an include file which I've attached.
>
> Povray thankfully includes the built-in "vaxis_rotate()" function. That,
> along with a number of great macros in "transforms.inc", allows you to
> handle pretty well all of your "rotational" requirements. So why would
> you want to use quaternions to do rotations in Povray?  Primarily for
> incremental and true angular interpolation rotations. And I've found a
> few other uses myself, you might as well.
>
> Quaternions are 4D vectors. Yes, they use complex numbers and are
> difficult to understand, but you don't need to preoccupy yourself with
> any of this to take advantage of them for 3D rotations. You can think of
> a unit (normalized) quaternion as a representation of a rotation. So one
> single 4D vector can represent any rotation around any axis. When you
> multiply two unit quaternions together, you are effectively adding two
> rotations together. Other quaternion tricks are used in these macros
> which you may find useful.
>
> If anyone else finds this useful, let me know.
>
>


Post a reply to this message

From: Jellby
Subject: Re: quaternions.inc
Date: 6 Oct 2003 17:16:22
Message: <3f81dba5@news.povray.org>
Entre otras cosas, None tuvo a bien escribir:

> As an educational exercise for myself I've created a set of macros to
> handle quaternions, which can be useful for rotations. I've compiled
> these macros into an include file which I've attached.
> 
> [...]
> 
> If anyone else finds this useful, let me know.

Interesting... I've seen quaternions in a molecular dynamics program called 
MOLDY (it simulates rigid molecules, and uses quaternions to control their 
rotations).

As has already been requested, any example of use?

-- 
light_source{9+9*x,1}camera{orthographic look_at(1-y)/4angle 30location
9/4-z*4}light_source{-9*z,1}union{box{.9-z.1+x clipped_by{plane{2+y-4*x
0}}}box{z-y-.1.1+z}box{-.1.1+x}box{.1z-.1}pigment{rgb<.8.2,1>}}//Jellby


Post a reply to this message

From: None
Subject: Re: quaternions.inc
Date: 7 Oct 2003 11:47:35
Message: <Xns940D77F7CEE17None@204.213.191.226>
"Dennis Miller" <dhm### [at] comcastnet> wrote in news:3f81d703$1
@news.povray.org:

> Could you post a working demo scene?
ok.


Post a reply to this message

From: None
Subject: Re: quaternions.inc
Date: 7 Oct 2003 11:48:10
Message: <Xns940D78117A856None@204.213.191.226>
Jellby <jel### [at] M-yahoocom> wrote in news:3f81dba5@news.povray.org:

> [...]
> As has already been requested, any example of use?

I'll post one in this thread.


Post a reply to this message

From: None
Subject: QSample1.pov - Basic sample for using quaternions.inc for rotation
Date: 7 Oct 2003 11:58:51
Message: <Xns940D79E0C270FNone@204.213.191.226>
As requested, find attached a sample file demonstrating basic use of 
quaternions.inc for rotations.  It's very simple (has more comments than 
SDL), but I thought that was the best thing to get someone started.

Cheers.


Post a reply to this message


Attachments:
Download 'QSample1.pov.txt' (3 KB)

From: Tor Olav Kristensen
Subject: Re: QSample1.pov - Basic sample for using quaternions.inc for rotation
Date: 7 Oct 2003 13:09:27
Message: <Xns940DC2ACA17AAtorolavkhotmailcom@204.213.191.226>
None <Non### [at] onca> wrote in news:Xns### [at] 204213191226:

> As requested, find attached a sample file demonstrating basic use of 
> quaternions.inc for rotations.  It's very simple (has more comments than 
> SDL), but I thought that was the best thing to get someone started.


Alain, your quaternions.inc file has some problems: Just look at all
the "Divide by zero" warnings generated when parsing your sample file.

I can help you with some of them if you are intereseted.


Tor Olav


Post a reply to this message

From: None
Subject: Re: QSample1.pov - Basic sample for using quaternions.inc for rotation
Date: 7 Oct 2003 14:15:01
Message: <Xns940D90F66401None@204.213.191.226>
Tor Olav Kristensen <tor_olav_kCURLYAhotmail.com> wrote in
news:Xns### [at] 204213191226: 

> Alain, your quaternions.inc file has some problems: Just look at all
> the "Divide by zero" warnings generated when parsing your sample file.
> 
> I can help you with some of them if you are intereseted.
> 
> 
> Tor Olav

Yes, the divide by zero messages are coming from the Qln() & QExp() macros 
where I have attempted to trap them with a conditional, but they still 
occur, it's strange.  But I think the end result is the same.

If you are interested in helping it would be greatly appreciated.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: QSample1.pov - Basic sample for using quaternions.inc for rotation
Date: 7 Oct 2003 16:07:46
Message: <Xns940DE0E98C34torolavkhotmailcom@204.213.191.226>
None <Non### [at] onca> wrote in news:Xns### [at] 204213191226:

> Tor Olav Kristensen <tor_olav_kCURLYAhotmail.com> wrote in
> news:Xns### [at] 204213191226: 
> 
>> Alain, your quaternions.inc file has some problems: Just look at all
>> the "Divide by zero" warnings generated when parsing your sample
>> file. 
>> 
>> I can help you with some of them if you are intereseted.
>> 
>> 
>> Tor Olav
> 
> Yes, the divide by zero messages are coming from the Qln() & QExp()
> macros where I have attempted to trap them with a conditional, but
> they still occur, it's strange.  But I think the end result is the
> same. 
> 
> If you are interested in helping it would be greatly appreciated.
> 

Note that in this expression:

(C ? A : B)

- both A and B are evaluated.

(I find this odd. IIRC this has been discussed somewhere in
these groups before. But I cannot remember if any good reasons
for this behaviour have been given.)

So your attempt to trap any division by zero errors by using
this conditional expression does not work smoothly.

It is my opinion that section '6.1.3.3 Float Operators' in the
manual should mention this behaviour.

My suggestion is to use #if #then and #else instead in such
cases.

But even if you do this in your Qln() and QExp() macros, there
is still one divide by zero warning left. It is triggered by
the QAxAn() macro. The 4th component (t) of a quaternion passed
to the macro is set to 1. - And this causes this expression to
be evaulated to zero: sqrt(1-Q.t*Q.t)

(It seems to me that it is the QBezier() macro that sets this
4th component to 1.)


Note that it is not advisable for users to use variable names
that are all lovercase letters (especially not for single
letter names).

(This is because these may be used for reserved keywords in
later versions of POV-Ray.)

See code below for my suggestions. 


P.S.: POV-Ray has an epsilon constant (= 1.0E-10) that it uses
in float comparisons. So it might not be necessary to use one
yourself, like you do when checking if the R variable is close
to zero.

Btw.: Are you sure that log() and not ln() should be used in
the QExp() macro ?


Tor Olav



#include "functions.inc" // For f_r()


#macro Qln(Q)

  #local R = f_r(Q.x, Q.y, Q.z);
  #if (R > 0)
    #local At = atan2(R, Q.t)/R;
  #else
    #local At = 0;
  #end // if

  <At*Q.x, At*Q.y, At*Q.z, 0.5*log(Qsc(Q))>

#end // macro Qln


#macro QExp(Q)

  #local R = f_r(Q.x, Q.y, Q.z);
  #local Et = exp(Q.t);
  #if (R > 0)
    #local S = sin(R)/R;
  #else
    #local S = 0;
  #end // if

  (Et*<S*Q.x, S*Q.y, S*Q.z, cos(R)>)

#end // macro QExp


#macro QAxAn(Q, An)

  #declare An = acos(Q.t)*2; 

  (<Q.x, Q.y, Q.z>/sqrt(1-Q.t*Q.t))

#end // macro QAxAn


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: QSample1.pov - Basic sample for using quaternions.inc for rotation
Date: 7 Oct 2003 16:13:51
Message: <Xns940DE1F1744Etorolavkhotmailcom@204.213.191.226>
Tor Olav Kristensen <tor_olav_kCURLYAhotmail.com> wrote in 
news:Xns### [at] 204213191226:

...
> Btw.: Are you sure that log() and not ln() should be used in
> the QExp() macro ?
...

Sorry. I meant the Qln() macro.


Tor Olav


Post a reply to this message

Goto Latest 10 Messages Next 8 Messages >>>

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