include/quaternion.h
author Radek Brich <radek.brich@devl.cz>
Sat, 10 May 2008 14:29:37 +0200
branchpyrit
changeset 95 ca7d4c665531
parent 94 4c8abb8977dc
child 103 3b3257a410fe
permissions -rw-r--r--
build script fixes, add ldflags build option update and enhance demos fix bug in 4x grid oversampling warn if writePNG called while compiled without libpng make shapes in ShapeList const and add many other const needed due to snowball effect slightly optimize Camera::makeRayPacket using _mm_shuffle_ps make Vector SIMD vectorization disabled by default (causes problems) fix bug in implicit reflection of transmissive surfaces, when surface's reflection parameter is set to zero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
     1
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
     2
 * @file  quaternion.h
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
     3
 * @brief Quaternion class
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     4
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     5
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     6
 *
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
     7
 * Copyright 2007, 2008  Radek Brich
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    10
 * of this software and associated documentation files (the "Software"), to deal
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    11
 * in the Software without restriction, including without limitation the rights
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    13
 * copies of the Software, and to permit persons to whom the Software is
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    14
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    15
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    16
 * The above copyright notice and this permission notice shall be included in
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    17
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    18
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    25
 * THE SOFTWARE.
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
 */
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#ifndef QUATERNION_H
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
#define QUATER_H
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    31
/**
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    32
 * quaternion class
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    33
 */
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
class Quaternion
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
public:
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
	union {
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		struct {
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
			Float a,b,c,d;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		struct {
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
			Float t,x,y,z;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	Quaternion(): a(0), b(0), c(0), d(0) {};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	Quaternion(const Float aa, const Float ab, const Float ac, const Float ad):
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
		a(aa), b(ab), c(ac), d(ad) {};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    48
	Quaternion(const Vector& v): a(0), b(v.x), c(v.y), d(v.z) {};
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 60
diff changeset
    50
	Vector toVector() { return Vector(b, c, d); };
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	Quaternion normalize()
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	{
60
a23b5089b9c3 moving to SCons build system
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    54
		Float f = (Float)1.0f / sqrtf(a * a + b * b + c * c + d * d);
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
		a *= f;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		b *= f;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		c *= f;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		d *= f;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		return *this;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
	Float mag2() const
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
	{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
		return (a*a + b*b + c*c + d*d);
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
	Float mag() const
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
	{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
		return sqrtf(mag());
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
	friend Quaternion operator*(const Quaternion &q1, const Quaternion &q2)
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
		return Quaternion(
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
			q1.a*q2.a - q1.b*q2.b - q1.c*q2.c - q1.d*q2.d,
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
			q1.a*q2.b + q1.b*q2.a + q1.c*q2.d - q1.d*q2.c,
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
			q1.a*q2.c - q1.b*q2.d + q1.c*q2.a + q1.d*q2.b,
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
			q1.a*q2.d + q1.b*q2.c - q1.c*q2.b + q1.d*q2.a);
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
	friend Float dot(const Quaternion &q1, const Quaternion &q2)
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
		return q1.a*q2.a + q1.b*q2.b + q1.c*q2.c + q1.d*q2.d;
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
	friend Quaternion conjugate(const Quaternion &q)
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	{
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
		return Quaternion(q.a, -q.b, -q.c, -q.d);
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
	};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
};
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
#endif