# HG changeset patch # User Eleni Maria Stea # Date 1389453756 -7200 # Node ID 88d390af583f28f5096111c3872107a216e1afa8 Image loader diff -r 000000000000 -r 88d390af583f Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,24 @@ +src = $(wildcard src/*.cc src/math/*.cc src/shaders/*.cc) +obj = $(src:.cc=.o) +dep = $(obj:.o=.d) +bin = test1 + +dbg = -g +opt = -O0 +inc = -Isrc -I/usr/local/include -I/usr/local/lib + +CXX = g++ +CXXFLAGS = -pedantic -Wall $(dbg) $(opt) $(inc) +LDFLAGS = -lGL -lGLU -lGLEW -lglut -limago -lm + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) + +-include $(dep) + +%.d: %.cc + @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) $(dep) diff -r 000000000000 -r 88d390af583f src/image.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/image.h Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,23 @@ +#ifndef IMAGE_H_ +#define IMAGE_H_ + +class Image { +private: + float *pixels; + int width; + int height; +public: + Image(); + ~Image(); + + bool load(const char *fname); + + float *get_pixels(); + const float *get_pixels() const; + void set_pixels(const float *pixels, int width, int height); + + int get_width() const; + int get_height() const; +}; + +#endif //IMAGE_H_ diff -r 000000000000 -r 88d390af583f src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cc Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,87 @@ +#include +#include + +#include +#include + +//static void init(void); +static void display(void); +static void reshape(int x, int y); +static void keyboard(unsigned char key, int x, int y); +static void mouse(int button, int state, int x, int y); +static void motion(int x, int y); + +static int win_xsz, win_ysz; + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(1280, 720); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + + //TODO parse arguments + + glutCreateWindow("My Colonoscopie OEO!"); + + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyboard); + glutMouseFunc(mouse); + glutMotionFunc(motion); + + glewInit(); + + //call init + + glutMainLoop(); + return 0; +} + +int init(void) +{ + //TODO + return 0; +} + +void display(void) +{ + //render + + glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); +} + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + if(x != win_xsz || y != win_ysz) { + //TODO raytex_needs_recalc = 1; + win_xsz = x; + win_ysz = y; + } +} + +void keyboard(unsigned char key, int x, int y) +{ + switch(key) { + case 27: + exit(0); + default: + break; + } +} + +static int prev_x, prev_y; +void mouse(int bn, int state, int x, int y) +{ + prev_x = x; + prev_y = y; +} + +void motion(int x, int y) +{ + int dx = x - prev_x; + int dy = y - prev_y; + prev_x = x; + prev_y = y; +} diff -r 000000000000 -r 88d390af583f src/matrix.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/matrix.cc Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,393 @@ +#include +#include +#include +#include "matrix.h" + +Matrix4x4 Matrix4x4::identity = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); + +Matrix4x4::Matrix4x4() +{ + *this = identity; +} + +Matrix4x4::Matrix4x4( float m11, float m12, float m13, float m14, + float m21, float m22, float m23, float m24, + float m31, float m32, float m33, float m34, + float m41, float m42, float m43, float m44) +{ + m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14; + m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24; + m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34; + m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44; +} + +Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2) +{ + Matrix4x4 res; + const float *op1 = m1.m[0], *op2 = m2.m[0]; + float *dest = res.m[0]; + + for(int i=0; i<16; i++) { + *dest++ = *op1++ + *op2++; + } + return res; +} + +Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2) +{ + Matrix4x4 res; + const float *op1 = m1.m[0], *op2 = m2.m[0]; + float *dest = res.m[0]; + + for(int i=0; i<16; i++) { + *dest++ = *op1++ - *op2++; + } + return res; +} + +Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) { + Matrix4x4 res; + + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j] + m1.m[i][3] * m2.m[3][j]; + } + } + + return res; +} + +void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2) +{ + float *op1 = m1.m[0]; + const float *op2 = m2.m[0]; + + for(int i=0; i<16; i++) { + *op1++ += *op2++; + } +} + +void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2) +{ + float *op1 = m1.m[0]; + const float *op2 = m2.m[0]; + + for(int i=0; i<16; i++) { + *op1++ -= *op2++; + } +} + +void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2) +{ + Matrix4x4 res; + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j] + m1.m[i][3] * m2.m[3][j]; + } + } + memcpy(m1.m, res.m, 16 * sizeof(float)); +} + +Matrix4x4 operator *(const Matrix4x4 &mat, float scalar) +{ + Matrix4x4 res; + const float *mptr = mat.m[0]; + float *dptr = res.m[0]; + + for(int i=0; i<16; i++) { + *dptr++ = *mptr++ * scalar; + } + return res; +} + +Matrix4x4 operator *(float scalar, const Matrix4x4 &mat) +{ + Matrix4x4 res; + const float *mptr = mat.m[0]; + float *dptr = res.m[0]; + + for(int i=0; i<16; i++) { + *dptr++ = *mptr++ * scalar; + } + return res; +} + +void operator *=(Matrix4x4 &mat, float scalar) +{ + float *mptr = mat.m[0]; + + for(int i=0; i<16; i++) { + *mptr++ *= scalar; + } +} + +void Matrix4x4::translate(const Vector3 &trans) +{ + Matrix4x4 tmat(1, 0, 0, trans.x, 0, 1, 0, trans.y, 0, 0, 1, trans.z, 0, 0, 0, 1); + *this *= tmat; +} + +void Matrix4x4::set_translation(const Vector3 &trans) +{ + *this = Matrix4x4(1, 0, 0, trans.x, 0, 1, 0, trans.y, 0, 0, 1, trans.z, 0, 0, 0, 1); +} + +void Matrix4x4::rotate(const Vector3 &euler) +{ + float sinx = sin(euler.x); + float cosx = cos(euler.x); + float siny = sin(euler.y); + float cosy = cos(euler.y); + float sinz = sin(euler.z); + float cosz = cos(euler.z); + + Matrix4x4 xrot = Matrix4x4( + 1, 0, 0, 0, + 0, cosx, sinx, 0, + 0, -sinx, cosx, 0, + 0, 0, 0, 1); + + Matrix4x4 yrot = Matrix4x4( + cosy, 0, -siny, 0, + 0, 1, 0, 0, + siny, 0, cosy, 0, + 0, 0, 0, 1); + + Matrix4x4 zrot = Matrix4x4( + cosz, sinz, 0, 0, + -sinz, cosz, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + + *this *= (xrot * yrot * zrot); +} + +void Matrix4x4::rotate(const Vector3 &axis, float angle) +{ + float sina = (float)sin(angle); + float cosa = (float)cos(angle); + float invcosa = 1-cosa; + float nxsq = axis.x * axis.x; + float nysq = axis.y * axis.y; + float nzsq = axis.z * axis.z; + + Matrix4x4 xform; + xform[0][0] = nxsq + (1-nxsq) * cosa; + xform[0][1] = axis.x * axis.y * invcosa - axis.z * sina; + xform[0][2] = axis.x * axis.z * invcosa + axis.y * sina; + xform[1][0] = axis.x * axis.y * invcosa + axis.z * sina; + xform[1][1] = nysq + (1-nysq) * cosa; + xform[1][2] = axis.y * axis.z * invcosa - axis.x * sina; + xform[2][0] = axis.x * axis.z * invcosa - axis.y * sina; + xform[2][1] = axis.y * axis.z * invcosa + axis.x * sina; + xform[2][2] = nzsq + (1-nzsq) * cosa; + + *this *= Matrix4x4(xform); +} + +void Matrix4x4::set_rotation(const Vector3 &axis, float angle) +{ + float sina = (float)sin(angle); + float cosa = (float)cos(angle); + float invcosa = 1-cosa; + float nxsq = axis.x * axis.x; + float nysq = axis.y * axis.y; + float nzsq = axis.z * axis.z; + + reset_identity(); + m[0][0] = nxsq + (1-nxsq) * cosa; + m[0][1] = axis.x * axis.y * invcosa - axis.z * sina; + m[0][2] = axis.x * axis.z * invcosa + axis.y * sina; + m[1][0] = axis.x * axis.y * invcosa + axis.z * sina; + m[1][1] = nysq + (1-nysq) * cosa; + m[1][2] = axis.y * axis.z * invcosa - axis.x * sina; + m[2][0] = axis.x * axis.z * invcosa - axis.y * sina; + m[2][1] = axis.y * axis.z * invcosa + axis.x * sina; + m[2][2] = nzsq + (1-nzsq) * cosa; +} + +void Matrix4x4::scale(const Vector3 &scale_vec) +{ + Matrix4x4 smat( scale_vec.x, 0, 0, 0, + 0, scale_vec.y, 0, 0, + 0, 0, scale_vec.z, 0, + 0, 0, 0, 1.0); + *this *= smat; +} + +void Matrix4x4::set_scaling(const Vector3 &scale_vec) +{ + *this = Matrix4x4( scale_vec.x, 0, 0, 0, + 0, scale_vec.y, 0, 0, + 0, 0, scale_vec.z, 0, + 0, 0, 0, 1); +} + +void Matrix4x4::set_column_vector(const Vector3 &vec, unsigned int col_index) +{ + m[0][col_index] = vec.x; + m[1][col_index] = vec.y; + m[2][col_index] = vec.z; + m[3][col_index] = 1; +} + +void Matrix4x4::set_row_vector(const Vector3 &vec, unsigned int row_index) +{ + m[row_index][0] = vec.x; + m[row_index][1] = vec.y; + m[row_index][2] = vec.z; + m[row_index][3] = 1; +} + +Vector3 Matrix4x4::get_column_vector(unsigned int col_index) const +{ + return Vector3(m[0][col_index], m[1][col_index], m[2][col_index]); +} + +Vector3 Matrix4x4::get_row_vector(unsigned int row_index) const +{ + return Vector3(m[row_index][0], m[row_index][1], m[row_index][2]); +} + +void Matrix4x4::transpose() +{ + Matrix4x4 tmp = *this; + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + m[i][j] = tmp[j][i]; + } + } +} + +Matrix4x4 Matrix4x4::transposed() const +{ + Matrix4x4 res; + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + res[i][j] = m[j][i]; + } + } + return res; +} + +float Matrix4x4::determinant() const +{ + float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + + float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + + float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14; +} + + +Matrix4x4 Matrix4x4::adjoint() const +{ + Matrix4x4 coef; + + coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - + (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) + + (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])); + coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - + (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])); + coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) - + (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); + coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) - + (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) + + (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); + + coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - + (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) + + (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])); + coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - + (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])); + coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) - + (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); + coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) - + (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) + + (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); + + coef.transpose(); + + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + coef.m[i][j] = j%2 ? -coef.m[i][j] : coef.m[i][j]; + if(i%2) coef.m[i][j] = -coef.m[i][j]; + } + } + + return coef; +} + +Matrix4x4 Matrix4x4::inverse() const +{ + Matrix4x4 adj = adjoint(); + + return adj * (1.0f / determinant()); +} + +inline float *Matrix4x4::operator [](int index) +{ + return m[index]; +} + +inline const float *Matrix4x4::operator [](int index) const +{ + return m[index]; +} + +inline void Matrix4x4::reset_identity() +{ + *this = identity; +} + +std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat) +{ + for(int i=0; i<4; i++) { + char str[100]; + sprintf(str, "[ %12.5f %12.5f %12.5f %12.5f ]\n", (float)mat.m[i][0], (float)mat.m[i][1], (float)mat.m[i][2], (float)mat.m[i][3]); + out << str; + } + return out; +} diff -r 000000000000 -r 88d390af583f src/matrix.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/matrix.h Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,81 @@ +#ifndef MATRIX_H_ +#define MATRIX_H_ + +#include +#include "vector.h" + +class Matrix4x4 { +public: + float m[4][4]; + + static Matrix4x4 identity; + + Matrix4x4(); + Matrix4x4( float m11, float m12, float m13, float m14, + float m21, float m22, float m23, float m24, + float m31, float m32, float m33, float m34, + float m41, float m42, float m43, float m44); + + /* binary operations matrix (op) matrix */ + friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2); + friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2); + friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2); + + friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2); + friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2); + friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2); + + /* binary operations matrix (op) scalar and scalar (op) matrix */ + friend Matrix4x4 operator *(const Matrix4x4 &mat, float scalar); + friend Matrix4x4 operator *(float scalar, const Matrix4x4 &mat); + + friend void operator *=(Matrix4x4 &mat, float scalar); + + inline float *operator [](int index); + inline const float *operator [](int index) const; + + inline void reset_identity(); + + void translate(const Vector3 &trans); + void set_translation(const Vector3 &trans); + + void rotate(const Vector3 &euler); /* 3d rotation with euler angles */ + void rotate(const Vector3 &axis, float angle); /* 3d axis/angle rotation */ + void set_rotation(const Vector3 &euler_angles); + void set_rotation(const Vector3 &axis, float angle); + + void scale(const Vector3 &scale_vec); + void set_scaling(const Vector3 &scale_vec); + + void set_column_vector(const Vector3 &vec, unsigned int col_index); + void set_row_vector(const Vector3 &vec, unsigned int row_index); + Vector3 get_column_vector(unsigned int col_index) const; + Vector3 get_row_vector(unsigned int row_index) const; + + void transpose(); + Matrix4x4 transposed() const; + float determinant() const; + Matrix4x4 adjoint() const; + Matrix4x4 inverse() const; +}; + +/* binary operations matrix (op) matrix */ +Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2); +Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2); +Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2); + +void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2); +void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2); +inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2); + +/* binary operations matrix (op) scalar and scalar (op) matrix */ +Matrix4x4 operator *(const Matrix4x4 &mat, float scalar); +Matrix4x4 operator *(float scalar, const Matrix4x4 &mat); + +void operator *=(Matrix4x4 &mat, float scalar); + +std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat); + + +#endif + diff -r 000000000000 -r 88d390af583f src/vector.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vector.cc Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,213 @@ +#include +#include +#include "vector.h" +#include "matrix.h" + +Vector3::Vector3() { + x = 0; + y = 0; + z = 0; +} + +Vector3::Vector3(float x, float y, float z) { + this->x = x; + this->y = y; + this->z = z; +} + +void Vector3::transform(const Matrix4x4 &tm) { + float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3]; + float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3]; + float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3]; + x = x1; + y = y1; + z = z1; +} + +void Vector3::printv() { + printf("%f\t%f\t%f\n", x, y, z); +} + + +bool operator < (const Vector3 &a, const Vector3 &b) { + return a.x < b.x && a.y < b.y && a.z < b.z; +} + +bool operator > (const Vector3 &a, const Vector3 &b) { + return a.x > b.x && a.y > b.y && a.z > b.z; +} + +bool operator == (const Vector3 &a, const Vector3 &b) { + return a.x == b.x && a.y == b.y && a.z == b.z; +} + +Vector3 operator + (const Vector3 &a, const Vector3 &b) { + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +Vector3 operator - (const Vector3 &a, const Vector3 &b) { + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +Vector3 operator - (const Vector3 &a) { + return Vector3(-a.x, -a.y, -a.z); +} + +Vector3 operator * (const Vector3 &a, const Vector3 &b) { + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); +} + +Vector3 operator * (const Vector3 &a, float b) { + return Vector3(a.x*b, a.y*b, a.z*b); +} + +Vector3 operator * (float b, const Vector3 &a) { + return Vector3(a.x*b, a.y*b, a.z*b); +} + +Vector3 operator / (const Vector3 &a, float b) { + return Vector3(a.x / b, a.y / b, a.z / b); +} + +const Vector3 &operator += (Vector3 &a, const Vector3 &b) { + a.x += b.x; + a.y += b.y; + a.z += b.z; + return a; +} + +float length(const Vector3 &a) { + return sqrt(a.x*a.x + a.y*a.y + a.z*a.z); +} + +float dot(const Vector3 &a, const Vector3 &b) { + return a.x*b.x + a.y*b.y + a.z*b.z; +} + +Vector3 cross(const Vector3 &a, const Vector3 &b) { + 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); +} + +Vector3 normalize(const Vector3 &vec) { + float mag = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z); + return vec / mag; +} + +Vector3 reflect(const Vector3 &v, const Vector3 &n) { + return 2.0 * dot(v, n) * n - v; +} + +////// +// +///// + +Vector4::Vector4() { + x = 0; + y = 0; + z = 0; + w = 0; +} + +Vector4::Vector4(float x, float y, float z, float w) { + this->x = x; + this->y = y; + this->z = z; + this->w = w; +} + +void Vector4::transform(const Matrix4x4 &tm) { + float x1 = tm.m[0][0]*x + tm.m[0][1]*y + tm.m[0][2]*z + tm.m[0][3]; + float y1 = tm.m[1][0]*x + tm.m[1][1]*y + tm.m[1][2]*z + tm.m[1][3]; + float z1 = tm.m[2][0]*x + tm.m[2][1]*y + tm.m[2][2]*z + tm.m[2][3]; + float w1 = tm.m[3][0]*x + tm.m[3][1]*y + tm.m[3][2]*z + tm.m[3][3]; + x = x1; + y = y1; + z = z1; + w = w1; +} + +void Vector4::printv() { + printf("%f\t%f\t%f\t%f\n", x, y, z, w); +} + + +bool operator < (const Vector4 &a, const Vector4 &b) { + return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w; +} + +bool operator > (const Vector4 &a, const Vector4 &b) { + return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w; +} + +bool operator == (const Vector4 &a, const Vector4 &b) { + return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; +} + +Vector4 operator + (const Vector4 &a, const Vector4 &b) { + return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); +} + +Vector4 operator - (const Vector4 &a, const Vector4 &b) { + return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); +} + +Vector4 operator - (const Vector4 &a) { + return Vector4(-a.x, -a.y, -a.z, -a.w); +} + +Vector4 operator * (const Vector4 &a, const Vector4 &b) { + return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); +} + +Vector4 operator * (const Vector4 &a, float b) { + return Vector4(a.x*b, a.y*b, a.z*b, a.w*b); +} + +Vector4 operator * (float b, const Vector4 &a) { + return Vector4(a.x * b, a.y * b, a.z * b, a.w * b); +} + +Vector4 operator / (const Vector4 &a, float b) { + return Vector4(a.x / b, a.y / b, a.z / b, a.w / b); +} + +const Vector4 &operator += (Vector4 &a, const Vector4 &b) { + a.x += b.x; + a.y += b.y; + a.z += b.z; + a.w += b.w; + return a; +} + +float length(const Vector4 &a) { + return sqrt(a.x*a.x + a.y*a.y + a.z*a.z + a.w*a.w); +} + +float dot(const Vector4 &a, const Vector4 &b) { + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; +} + +Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) { + float a = (v2.x * v3.y) - (v2.y * v3.x); + float b = (v2.x * v3.z) - (v2.z * v3.x); + float c = (v2.x * v3.w) - (v2.w * v3.x); + float d = (v2.y * v3.z) - (v2.z * v3.y); + float e = (v2.y * v3.w) - (v2.w * v3.y); + float f = (v2.z * v3.w) - (v2.w * v3.z); + + Vector4 result; + result.x = (v1.y * f) - (v1.z * e) + (v1.w * d); + result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b); + result.z = (v1.x * e) - (v1.y * c) + (v1.w * a); + result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a); + return result; +} + +Vector4 normalize(const Vector4 &vec) { + float mag = sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w); + return vec / mag; +} + +Vector4 reflect(const Vector4 &v, const Vector4 &n) { + return 2.0 * dot(v, n) * n - v; +} diff -r 000000000000 -r 88d390af583f src/vector.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vector.h Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,68 @@ +#ifndef VECTOR_H_ +#define VECTOR_H_ + +class Matrix4x4; + +class Vector3 { +public: + float x,y,z; + Vector3(); + Vector3(float x, float y, float z); + void transform(const Matrix4x4 &tm); + void printv(); +}; + +bool operator < (const Vector3 &a, const Vector3 &b); +bool operator > (const Vector3 &a, const Vector3 &b); +bool operator == (const Vector3 &a, const Vector3 &b); + +Vector3 operator + (const Vector3 &a, const Vector3 &b); +Vector3 operator - (const Vector3 &a, const Vector3 &b); +Vector3 operator - (const Vector3 &a); +Vector3 operator * (const Vector3 &a, const Vector3 &b); +Vector3 operator * (const Vector3 &a, float b); +Vector3 operator * (float b, const Vector3 &a); +Vector3 operator / (const Vector3 &a, float b); + +const Vector3 &operator += (Vector3 &a, const Vector3 &b); + +float length (const Vector3 &a); +float dot (const Vector3 &a, const Vector3 &b); +Vector3 cross (const Vector3 &a, const Vector3 &b); +Vector3 normalize (const Vector3 &a); + +Vector3 reflect(const Vector3 &v, const Vector3 &n); + +class Vector4 { + public: + float x, y, z, w; + Vector4(); + Vector4(float x, float y, float z, float w); + void transform(const Matrix4x4 &tm); + void printv(); +}; + +bool operator < (const Vector4 &a, const Vector4 &b); +bool operator > (const Vector4 &a, const Vector4 &b); +bool operator == (const Vector4 &a, const Vector4 &b); + +Vector4 operator + (const Vector4 &a, const Vector4 &b); +Vector4 operator - (const Vector4 &a, const Vector4 &b); +Vector4 operator - (const Vector4 &a); +Vector4 operator * (const Vector4 &a, const Vector4 &b); +Vector4 operator * (const Vector4 &a, float b); +Vector4 operator * (float b, const Vector4 &a); +Vector4 operator / (const Vector4 &a, float b); + +const Vector4 &operator += (Vector4 &a, const Vector4 &b); + +float length (const Vector4 &a); +float dot (const Vector4 &a, const Vector4 &b); +Vector4 cross (const Vector4 &v1, const Vector4 &v2, const Vector4 &v3); +Vector4 normalize (const Vector4 &a); + +Vector4 reflect(const Vector4 &v, const Vector4 &n); + + +#endif + diff -r 000000000000 -r 88d390af583f src/volume.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/volume.cc Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,65 @@ +#include + +#include "image.h" + +Image::Image() +{ + pixels = 0; + width = 0; + height = 0; +} + +Image::~Image() +{ + delete [] pixels; +} + +bool Image::load(const char *fname) +{ + int new_width, new_height; + float *new_pixels = (float*)img_load_pixels(fname, &new_width, &new_height, IMG_FMT_GREYF); + + if(!new_pixels) { + fprintf(stderr, "Failed to load image: %s\n", fname); + return false; + } + + set_pixels(new_pixels, new_width, new_height); + img_free_pixels(new_pixels); + return true; +} + +float *Image::get_pixels() +{ + return pixels; +} + +const float *Image::get_pixels() const +{ + return pixels; +} + +void Image::set_pixels(const float *pixels, int width, int height) +{ + if(!pixels) + return; + + delete [] this->pixels; + + this->pixels = new float[width * height]; + for(int i=0; ipixels[i] = pixels[i]; + } + this->width = width; + this->height = height; +} + +int Image::get_width() const +{ + return width; +} + +int Image::get_height() const +{ + return height; +} diff -r 000000000000 -r 88d390af583f src/volume.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/volume.h Sat Jan 11 17:22:36 2014 +0200 @@ -0,0 +1,4 @@ +#ifndef VOLUME_H_ +#define VOLUME_H_ + +#endif // VOLUME_H_