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