volmetrics

diff src/vector.cc @ 0:88d390af583f

Image loader
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sat, 11 Jan 2014 17:22:36 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vector.cc	Sat Jan 11 17:22:36 2014 +0200
     1.3 @@ -0,0 +1,213 @@
     1.4 +#include <math.h>
     1.5 +#include <stdio.h>
     1.6 +#include "vector.h"
     1.7 +#include "matrix.h"
     1.8 +
     1.9 +Vector3::Vector3() {
    1.10 +	x = 0;
    1.11 +	y = 0;
    1.12 +	z = 0;
    1.13 +}
    1.14 +
    1.15 +Vector3::Vector3(float x, float y, float z) {
    1.16 +	this->x = x;
    1.17 +	this->y = y;
    1.18 +	this->z = z;
    1.19 +}
    1.20 +
    1.21 +void Vector3::transform(const Matrix4x4 &tm) {
    1.22 +	float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
    1.23 +	float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
    1.24 +	float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
    1.25 +	x = x1;
    1.26 +	y = y1;
    1.27 +	z = z1;
    1.28 +}
    1.29 +
    1.30 +void Vector3::printv() {
    1.31 +	printf("%f\t%f\t%f\n", x, y, z);
    1.32 +}
    1.33 +
    1.34 +
    1.35 +bool operator < (const Vector3 &a, const Vector3 &b) {
    1.36 +	return a.x < b.x && a.y < b.y && a.z < b.z;
    1.37 +}
    1.38 +
    1.39 +bool operator > (const Vector3 &a, const Vector3 &b) {
    1.40 +	return a.x > b.x && a.y > b.y && a.z > b.z;
    1.41 +}
    1.42 +
    1.43 +bool operator == (const Vector3 &a, const Vector3 &b) {
    1.44 +	return a.x == b.x && a.y == b.y && a.z == b.z;
    1.45 +}
    1.46 +
    1.47 +Vector3 operator + (const Vector3 &a, const Vector3 &b) {
    1.48 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    1.49 +}
    1.50 +
    1.51 +Vector3 operator - (const Vector3 &a, const Vector3 &b) {
    1.52 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    1.53 +}
    1.54 +
    1.55 +Vector3 operator - (const Vector3 &a) {
    1.56 +	return Vector3(-a.x, -a.y, -a.z);
    1.57 +}
    1.58 +
    1.59 +Vector3 operator * (const Vector3 &a, const Vector3 &b) {
    1.60 +	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    1.61 +}
    1.62 +
    1.63 +Vector3 operator * (const Vector3 &a, float b) {
    1.64 +	return Vector3(a.x*b, a.y*b, a.z*b);
    1.65 +}
    1.66 +
    1.67 +Vector3 operator * (float b, const Vector3 &a) {
    1.68 +	return Vector3(a.x*b, a.y*b, a.z*b);
    1.69 +}
    1.70 +
    1.71 +Vector3 operator / (const Vector3 &a, float b) {
    1.72 +	return Vector3(a.x / b, a.y / b, a.z / b);
    1.73 +}
    1.74 +
    1.75 +const Vector3 &operator += (Vector3 &a, const Vector3 &b) {
    1.76 +	a.x += b.x;
    1.77 +	a.y += b.y;
    1.78 +	a.z += b.z;
    1.79 +	return a;
    1.80 +}
    1.81 +
    1.82 +float length(const Vector3 &a) {
    1.83 +	return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
    1.84 +}
    1.85 +
    1.86 +float dot(const Vector3 &a, const Vector3 &b) {
    1.87 +	return a.x*b.x + a.y*b.y + a.z*b.z;
    1.88 +}
    1.89 +
    1.90 +Vector3 cross(const Vector3 &a, const Vector3 &b) {
    1.91 +	return Vector3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
    1.92 +}
    1.93 +
    1.94 +Vector3 normalize(const Vector3 &vec) {
    1.95 +	float mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
    1.96 +	return vec / mag;
    1.97 +}
    1.98 +
    1.99 +Vector3 reflect(const Vector3 &v, const Vector3 &n) {
   1.100 +	return 2.0 * dot(v, n) * n - v;
   1.101 +}
   1.102 +
   1.103 +//////
   1.104 +//
   1.105 +/////
   1.106 +
   1.107 +Vector4::Vector4() {
   1.108 +	x = 0;
   1.109 +	y = 0;
   1.110 +	z = 0;
   1.111 +	w = 0;
   1.112 +}
   1.113 +
   1.114 +Vector4::Vector4(float x, float y, float z, float w) {
   1.115 +	this->x = x;
   1.116 +	this->y = y;
   1.117 +	this->z = z;
   1.118 +	this->w = w;
   1.119 +}
   1.120 +
   1.121 +void Vector4::transform(const Matrix4x4 &tm) {
   1.122 +	float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3];
   1.123 +	float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3];
   1.124 +	float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3];
   1.125 +	float w1 = tm.m[3][0]*x + tm.m[3][1]*y + tm.m[3][2]*z + tm.m[3][3];
   1.126 +	x = x1;
   1.127 +	y = y1;
   1.128 +	z = z1;
   1.129 +	w = w1;
   1.130 +}
   1.131 +
   1.132 +void Vector4::printv() {
   1.133 +	printf("%f\t%f\t%f\t%f\n", x, y, z, w);
   1.134 +}
   1.135 +
   1.136 +
   1.137 +bool operator < (const Vector4 &a, const Vector4 &b) {
   1.138 +	return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w;
   1.139 +}
   1.140 +
   1.141 +bool operator > (const Vector4 &a, const Vector4 &b) {
   1.142 +	return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w;
   1.143 +}
   1.144 +
   1.145 +bool operator == (const Vector4 &a, const Vector4 &b) {
   1.146 +	return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
   1.147 +}
   1.148 +
   1.149 +Vector4 operator + (const Vector4 &a, const Vector4 &b) {
   1.150 +	return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
   1.151 +}
   1.152 +
   1.153 +Vector4 operator - (const Vector4 &a, const Vector4 &b) {
   1.154 +	return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
   1.155 +}
   1.156 +
   1.157 +Vector4 operator - (const Vector4 &a) {
   1.158 +	return Vector4(-a.x, -a.y, -a.z, -a.w);
   1.159 +}
   1.160 +
   1.161 +Vector4 operator * (const Vector4 &a, const Vector4 &b) {
   1.162 +	return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
   1.163 +}
   1.164 +
   1.165 +Vector4 operator * (const Vector4 &a, float b) {
   1.166 +	return Vector4(a.x*b, a.y*b, a.z*b, a.w*b);
   1.167 +}
   1.168 +
   1.169 +Vector4 operator * (float b, const Vector4 &a) {
   1.170 +	return Vector4(a.x * b, a.y * b, a.z * b, a.w * b);
   1.171 +}
   1.172 +
   1.173 +Vector4 operator / (const Vector4 &a, float b) {
   1.174 +	return Vector4(a.x / b, a.y / b, a.z / b, a.w / b);
   1.175 +}
   1.176 +
   1.177 +const Vector4 &operator += (Vector4 &a, const Vector4 &b) {
   1.178 +	a.x += b.x;
   1.179 +	a.y += b.y;
   1.180 +	a.z += b.z;
   1.181 +	a.w += b.w;
   1.182 +	return a;
   1.183 +}
   1.184 +
   1.185 +float length(const Vector4 &a) {
   1.186 +	return sqrt(a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w);
   1.187 +}
   1.188 +
   1.189 +float dot(const Vector4 &a, const Vector4 &b) {
   1.190 +	return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
   1.191 +}
   1.192 +
   1.193 +Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
   1.194 +	float a = (v2.x * v3.y) - (v2.y * v3.x);
   1.195 +	float b = (v2.x * v3.z) - (v2.z * v3.x);
   1.196 +	float c = (v2.x * v3.w) - (v2.w * v3.x);
   1.197 +	float d = (v2.y * v3.z) - (v2.z * v3.y);
   1.198 +	float e = (v2.y * v3.w) - (v2.w * v3.y);
   1.199 +	float f = (v2.z * v3.w) - (v2.w * v3.z);
   1.200 +
   1.201 +	Vector4 result;
   1.202 +    result.x =   (v1.y * f) - (v1.z * e) + (v1.w * d);
   1.203 +    result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
   1.204 +    result.z =   (v1.x * e) - (v1.y * c) + (v1.w * a);
   1.205 +    result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
   1.206 +    return result;
   1.207 +}
   1.208 +
   1.209 +Vector4 normalize(const Vector4 &vec) {
   1.210 +	float mag = sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
   1.211 +	return vec / mag;
   1.212 +}
   1.213 +
   1.214 +Vector4 reflect(const Vector4 &v, const Vector4 &n) {
   1.215 +	return 2.0 * dot(v, n) * n - v;
   1.216 +}