View Full Version : quaternion indices

ugluk

10-19-2012, 02:05 AM

If you check math books, the scalar part of a quaternion is usually at index 0 of a quaternion list, but in some code I've looked up, the scalar part is at index 3. Do there exist any practical benefits to either convention?

Dark Photon

10-19-2012, 05:27 AM

I've seen both orderings as well. Indices aside, in math contexts you typically see <s,v> where s is the scalar part and v is the vector part (thus scalar being first). However in 3D graphics contexts, almost always .xyz is the vector part (not surprisingly) and .w is the scalar part (as we just stuff quats into normal 4-vectors which we deal with all the time). Even Shoemake (http://www.cs.caltech.edu/courses/cs171/quatut.pdf) used this symbology in his quaternion write-up.

The index is a derived thing. We graphics guys index vectors .xyzw not alphabetically .wxyz. Thus the scalar part being index 3.

Definitely a convention but not something you have to do. You could use .x for the scalar part and .yzw for the 3-vector part, though it's not as intuitive for graphics folks.

Also beyond conventions, and more to your question, the vector part is really and truly a 3D vector which is the axis of rotation. There are cases where you want to cross another 3D vector with this axis to generate an orthonormal basis or transform coordinates in some way. There you can intuitively write cross ( quat.xyz, v.xyz ) or similar. This would be less readable with the vector part in .yzw.

ugluk

10-20-2012, 06:55 AM

Too bad I haven't thought of this myself. Certainly, it's thanks to the shaders, we have this scalar part = w convention. I've written an expression template library (as TVMET is slowly starting to become too old for my taste) and needed to know at what index to put the scalar part.

http://code.google.com/p/etemlib/

Alfonse Reinheart

10-20-2012, 11:10 AM

Be careful with expression templates, as new C++11 features like `auto` and `decltype` can really ruin your day.

Dark Photon

10-20-2012, 01:07 PM

Certainly, it's thanks to the shaders, we have this scalar part = w convention.

Actually, no. It's way pre-shaders. Here's Shoemake's SIGGRAPH 1985 paper:

* Animating Rotation with Quaternion Curves (http://run.usc.edu/cs520-s12/assign2/p245-shoemake.pdf)

See the 2nd paragraph at the top of page 4. And I'm not saying this is the first such paper to use .xyz for vector and .w for scalar. Might be, but don't know for sure.

ugluk

10-21-2012, 01:58 AM

But (GLSL) shaders had their precursors too, if you can call them precursors. Maybe one of those early shading languages influenced the paper.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.