include/vector.h
author Radek Brich <radek.brich@devl.cz>
Thu, 08 May 2008 09:21:25 +0200
branchpyrit
changeset 94 4c8abb8977dc
parent 93 96d65f841791
child 95 ca7d4c665531
permissions -rw-r--r--
update README update Doxygen docs scons option 'msvc' changed to 'mingw' as msvc is default and mingw must be turned on explicitly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     1
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     2
 * @file  vector.h
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
     3
 * @brief Vector class with Colour alias
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
 *
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
     7
 * Copyright 2006, 2007, 2008  Radek Brich
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
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.
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
 */
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#ifndef VECTOR_H
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
#define VECTOR_H
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
#include <math.h>
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
#include <iostream>
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    34
#include "common.h"
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    35
#include "simd.h"
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    36
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
using namespace std;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
46
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    39
/**
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    40
 * three cell vector
6493fb65f0b1 Doxygen
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    41
 */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    42
class Vector
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
public:
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	// data
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	union {
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    47
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    48
		mfloat4 mf4;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    49
#endif
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    50
		Float cell[4];
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    51
		struct { Float x, y, z, w; };
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    52
		struct { Float r, g, b, a; };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	// constructors
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    56
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    57
	Vector(mfloat4 m): mf4(m) {};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    58
#endif
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    59
	Vector(): x(0.0f), y(0.0f), z(0.0f), w(1.0) {};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    60
	Vector(Float ax, Float ay, Float az): x(ax), y(ay), z(az), w(1.0) {};
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
	// index operator
25
b8232edee786 tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents: 22
diff changeset
    63
	const Float &operator[](int index) const { return cell[index]; };
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    64
	Float &operator[](int index) { return cell[index]; };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    66
	bool operator==(const Vector &v) const { return x==v.x && y==v.y && z==v.z; };
21
79b516a3803d naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
    67
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    68
	/** Normalize the vector */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    69
	Vector normalize()
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    71
		const Float f = 1.0f / mag();
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    72
		*this *= f;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
		return *this;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    74
	};
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    76
	/** Get normalized copy of vector */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    77
	friend Vector normalize(const Vector &v)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	{
83
e3a2a5b26abb vectorize makeRayPacket() using SSE intrinsics
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    79
		const Float f = 1.0f / v.mag();
e3a2a5b26abb vectorize makeRayPacket() using SSE intrinsics
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    80
		return v * f;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    81
	};
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    83
	/** Square magnitude */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    84
	Float mag2() const	{ return dot(*this, *this); };
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    85
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    86
	/** Vector magnitude */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
    87
	Float mag() const	{ return sqrtf(mag2()); };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    89
	/** Get negative vector */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    90
	Vector operator-() const { return Vector(-x, -y, -z); };
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
    92
	/** Accumulate. Useful for colors. */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    93
	Vector operator+=(const Vector &v)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    95
#ifdef NO_SIMD
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
		x += v.x;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
		y += v.y;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
		z += v.z;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
    99
#else
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   100
		mf4 = madd(mf4, v.mf4);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   101
#endif
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
		return *this;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   105
	/** Multiply by scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   106
	Vector operator*=(const Float &f)
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   107
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   108
		x *= f;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   109
		y *= f;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   110
		z *= f;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   111
		return *this;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   112
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   113
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   114
	/** Cut with scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   115
	Vector operator/=(const Float &f)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   116
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   117
		Float finv = 1.0f / f;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   118
		x *= finv;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   119
		y *= finv;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   120
		z *= finv;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   121
		return *this;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   122
	};
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 25
diff changeset
   123
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   124
	/** Sum of two vectors */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   125
	friend Vector operator+(const Vector &a, const Vector &b)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   127
#ifdef NO_SIMD
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   128
		return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   129
#else
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   130
		return Vector(madd(a.mf4, b.mf4));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   131
#endif
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   134
	/** Difference of two vectors */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   135
	friend Vector operator-(const Vector &a, const Vector &b)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
	{
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   137
#if defined(NO_SIMD) || defined(MSVC)
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   138
		return Vector(a.x - b.x, a.y - b.y, a.z - b.z);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   139
#else
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   140
		// this faults in MSVC, for unknown reason
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   141
		return Vector(msub(a.mf4, b.mf4));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   142
#endif
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   145
	/** Dot product */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   146
	friend Float dot(const Vector &a, const Vector &b)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
	{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
		return a.x * b.x + a.y * b.y + a.z * b.z;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   151
	/** Cross product */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   152
	friend Vector cross(const Vector &a, const Vector &b)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   154
		return Vector(a.y * b.z - a.z * b.y,
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
			a.z * b.x - a.x * b.z,
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
			a.x * b.y - a.y * b.x);
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   159
	/** Get vector multiplied by scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   160
	friend Vector operator*(const Vector &v, const Float &f)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   162
		return Vector(f * v.x, f * v.y, f * v.z);
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   163
	};
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   165
	/** Get vector multiplied by scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   166
	friend Vector operator*(const Float &f, const Vector &v)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
	{
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
		return v * f;
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   169
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   171
	/** Get vector divided by scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   172
	friend Vector operator/(const Vector &v, const Float &f)
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   173
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   174
		const Float finv = 1.0f / f;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   175
		return Vector(v.x * finv, v.y * finv, v.z * finv);
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   176
	};
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   177
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   178
	/** Get f/v, i.e. inverted vector multiplied by scalar */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   179
	friend Vector operator/(const Float &f, const Vector &v)
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   180
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   181
#ifdef NO_SIMD
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   182
		return Vector(f / v.x, f / v.y, f / v.z);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   183
#else
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   184
		return Vector(mdiv(mset1(f), v.mf4));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   185
#endif
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   186
	};
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   187
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   188
	/** Add scalar to the vector */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   189
	friend Vector operator+(const Vector &v, const Float &f)
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 12
diff changeset
   190
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   191
		return Vector(v.x + f, v.y + f, v.z + f);
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   192
	};
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 12
diff changeset
   193
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   194
	/** Subtract scalar from the vector */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   195
	friend Vector operator-(const Vector &v, const Float &f)
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 12
diff changeset
   196
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   197
		return Vector(v.x - f, v.y - f, v.z - f);
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   198
	};
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 12
diff changeset
   199
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   200
	/** Cell by cell product (only useful for colors) */
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   201
	friend Vector operator*(const Vector &a, const Vector &b)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   203
#ifdef NO_SIMD
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   204
		return Vector(a.x * b.x, a.y * b.y, a.z * b.z);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   205
#else
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   206
		return Vector(mmul(a.mf4, b.mf4));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   207
#endif
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
	};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   209
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   210
	/** Write textual representation of the vector to stream */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   211
	friend ostream & operator<<(ostream &st, const Vector &v)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
	{
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   213
		return st << "(" << v.x << "," << v.y  << "," << v.z << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   214
	};
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   215
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   216
	/** Read the vector from stream */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   217
	friend istream & operator>>(istream &st, Vector &v)
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   218
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   219
		char s[10];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   220
		st.getline(s, 10, '(');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   221
		st >> v.x;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   222
		st.getline(s, 10, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   223
		st >> v.y;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   224
		st.getline(s, 10, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   225
		st >> v.z;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   226
		st.getline(s, 10, ')');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   227
		return st;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 46
diff changeset
   228
	};
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   229
};
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   230
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   231
/** Colour is a alias name of Vector. */
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   232
typedef Vector Colour;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   233
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   234
#ifndef NO_SIMD
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   235
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   236
  * Packet of four Vectors
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   237
  */
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   238
class VectorPacket
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   239
{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   240
public:
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   241
	union {
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   242
		mfloat4 ma[3];
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   243
		struct {
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   244
			mfloat4 mx;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   245
			mfloat4 my;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   246
			mfloat4 mz;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   247
		};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   248
		struct {
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   249
			float x[4];
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   250
			float y[4];
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   251
			float z[4];
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   252
		};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   253
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   254
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   255
	VectorPacket() {};
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   256
	VectorPacket(mfloat4 ax, mfloat4 ay, mfloat4 az):
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   257
		mx(ax), my(ay), mz(az) {};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   258
	VectorPacket(const Vector &v):
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   259
		mx(mset1(v.x)), my(mset1(v.y)), mz(mset1(v.z)) {};
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   260
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   261
	Vector getVector(int i) const
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   262
	{
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   263
		return Vector(x[i], y[i], z[i]);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   264
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   265
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   266
	void setVector(int i, const Vector &v)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   267
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   268
		x[i] = v.x; y[i] = v.y; z[i] = v.z;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   269
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   270
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   271
	void normalize()
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   272
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   273
		mfloat4 m,x,y,z;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   274
		x = mmul(mx, mx); // x*x
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   275
		y = mmul(my, my); // y*y
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   276
		z = mmul(mz, mz); // z*z
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   277
		m = madd(madd(x, y), z);     // x*x + y*y + z*z
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   278
		m = mdiv(mOne, msqrt(m));   // m = 1/sqrt(m)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   279
		mx = mmul(mx, m);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   280
		my = mmul(my, m);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   281
		mz = mmul(mz, m);
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   282
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   283
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   284
	// accumulate
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   285
	VectorPacket operator+=(const VectorPacket &v)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   286
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   287
		mx = madd(mx, v.mx);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   288
		my = madd(my, v.my);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   289
		mz = madd(mz, v.mz);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   290
		return *this;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   291
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   292
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   293
	// add to non-masked components
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   294
	VectorPacket selectiveAdd(const mfloat4 &mask, const VectorPacket &v)
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   295
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   296
		mx = mselect(mask, madd(mx, v.mx), mx);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   297
		my = mselect(mask, madd(my, v.my), my);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   298
		mz = mselect(mask, madd(mz, v.mz), mz);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   299
		return *this;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   300
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   301
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   302
	// add scalar to non-masked components
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   303
	VectorPacket selectiveAdd(const mfloat4 &mask, const mfloat4 &m)
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   304
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   305
		mx = mselect(mask, madd(mx, m), mx);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   306
		my = mselect(mask, madd(my, m), my);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   307
		mz = mselect(mask, madd(mz, m), mz);
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   308
		return *this;
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   309
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   310
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   311
	// dot product
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   312
	friend mfloat4 dot(const VectorPacket &a, const VectorPacket &b)
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   313
	{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   314
		return madd(madd(
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   315
			mmul(a.mx, b.mx),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   316
			mmul(a.my, b.my)),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   317
			mmul(a.mz, b.mz));
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   318
	};
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   319
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   320
	friend VectorPacket operator+(const VectorPacket &a, const VectorPacket &b)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   321
	{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   322
		return VectorPacket(
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   323
			madd(a.mx, b.mx),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   324
			madd(a.my, b.my),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   325
			madd(a.mz, b.mz));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   326
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   327
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   328
	friend VectorPacket operator-(const VectorPacket &a, const VectorPacket &b)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   329
	{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   330
		return VectorPacket(
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   331
			msub(a.mx, b.mx),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   332
			msub(a.my, b.my),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   333
			msub(a.mz, b.mz));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   334
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   335
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   336
	friend VectorPacket operator*(const VectorPacket &v,  const mfloat4 &m)
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   337
	{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   338
		return VectorPacket(
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   339
			mmul(v.mx, m),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   340
			mmul(v.my, m),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   341
			mmul(v.mz, m));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   342
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   343
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   344
	friend VectorPacket operator/(const mfloat4 &m, const VectorPacket &v)
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   345
	{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   346
		return VectorPacket(
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   347
			mdiv(m, v.mx),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   348
			mdiv(m, v.my),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   349
			mdiv(m, v.mz));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   350
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   351
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   352
	// cell by cell product (only usable for colours)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   353
	friend VectorPacket operator*(const VectorPacket &a,  const VectorPacket &b)
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   354
	{
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   355
		return VectorPacket(
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   356
			mmul(a.mx, b.mx),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   357
			mmul(a.my, b.my),
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   358
			mmul(a.mz, b.mz));
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   359
	};
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   360
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   361
	// write to character stream
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   362
	friend ostream & operator<<(ostream &st, const VectorPacket &v)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   363
	{
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   364
		return st << "[" << v.getVector(0) << "," << v.getVector(1)
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   365
			<< "," << v.getVector(2) << "," << v.getVector(3) << ")";
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   366
	};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   367
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   368
};
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 87
diff changeset
   369
#endif
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 83
diff changeset
   370
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   371
#endif