From db072ad4dc181eca5a1458656b130beb43f475bf Mon Sep 17 00:00:00 2001 From: 53hornet <53hornet@gmail.com> Date: Sat, 2 Feb 2019 23:33:15 -0500 Subject: Init. --- hw2/include/boundingBox.h | 77 ++++++++++++++++++++++++++ hw2/include/camera.h | 80 +++++++++++++++++++++++++++ hw2/include/color.h | 105 ++++++++++++++++++++++++++++++++++++ hw2/include/constants.h | 15 ++++++ hw2/include/errorMessage.h | 17 ++++++ hw2/include/image.h | 89 ++++++++++++++++++++++++++++++ hw2/include/imageIO.h | 19 +++++++ hw2/include/imageIO.pfm.h | 18 +++++++ hw2/include/imageIO.ppm.h | 18 +++++++ hw2/include/interval.h | 77 ++++++++++++++++++++++++++ hw2/include/mat3d.h | 112 ++++++++++++++++++++++++++++++++++++++ hw2/include/ray.h | 69 ++++++++++++++++++++++++ hw2/include/transformation3d.h | 77 ++++++++++++++++++++++++++ hw2/include/triangle.h | 115 +++++++++++++++++++++++++++++++++++++++ hw2/include/util.h | 19 +++++++ hw2/include/vec2d.h | 117 ++++++++++++++++++++++++++++++++++++++++ hw2/include/vec3d.h | 120 +++++++++++++++++++++++++++++++++++++++++ 17 files changed, 1144 insertions(+) create mode 100644 hw2/include/boundingBox.h create mode 100644 hw2/include/camera.h create mode 100644 hw2/include/color.h create mode 100644 hw2/include/constants.h create mode 100644 hw2/include/errorMessage.h create mode 100644 hw2/include/image.h create mode 100644 hw2/include/imageIO.h create mode 100644 hw2/include/imageIO.pfm.h create mode 100644 hw2/include/imageIO.ppm.h create mode 100644 hw2/include/interval.h create mode 100644 hw2/include/mat3d.h create mode 100644 hw2/include/ray.h create mode 100644 hw2/include/transformation3d.h create mode 100644 hw2/include/triangle.h create mode 100644 hw2/include/util.h create mode 100644 hw2/include/vec2d.h create mode 100644 hw2/include/vec3d.h (limited to 'hw2/include') diff --git a/hw2/include/boundingBox.h b/hw2/include/boundingBox.h new file mode 100644 index 0000000..522672e --- /dev/null +++ b/hw2/include/boundingBox.h @@ -0,0 +1,77 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _BOUNDINGBOX_H_ +#define _BOUNDINGBOX_H_ + +#include + +#include "ray.h" +#include "vec3d.h" +#include "transformation3d.h" + +class boundingBox { + public: + ///////////////// + // Constructor // + ///////////////// + boundingBox(void); + boundingBox(const vec3d& lfb, const vec3d& rbt); + boundingBox(const boundingBox& bb); + + ////////////// + // Operator // + ////////////// + boundingBox& operator=(const boundingBox& src); + + boundingBox& operator+=(const boundingBox& bb); + boundingBox& operator+=(const vec3d& point); + + boundingBox operator+(const boundingBox& bb) const; + + ///////////// + // Methods // + ///////////// + bool isHit(const ray& r) const; + + vec3d center(void) const; + vec3d corner(bool left, bool front, bool bottom) const; + + ////////////// + // Mutators // + ////////////// + boundingBox& transform(const transformation3d& t); + boundingBox& inverseTransform(const transformation3d& t); + + ///////////// + // Friends // + ///////////// + friend void swap(boundingBox& a, boundingBox& b) { a._swap(b); } + friend boundingBox transform(const boundingBox& bb, const transformation3d& t) { return boundingBox(bb).transform(t); } + friend boundingBox inverseTransform(const boundingBox& bb, const transformation3d& t) { return boundingBox(bb).inverseTransform(t); } + friend std::ostream& operator<<(std::ostream& s, const boundingBox& bb) + { + s << bb._lfb << " - " << bb._rbt; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _swap(boundingBox& bb); + void _assign(const boundingBox& bb); + + ////////////////// + // Data Members // + ////////////////// + vec3d _lfb, _rbt; +}; + + +#endif /* _BOUNDINGBOX_H_ */ diff --git a/hw2/include/camera.h b/hw2/include/camera.h new file mode 100644 index 0000000..9e3be04 --- /dev/null +++ b/hw2/include/camera.h @@ -0,0 +1,80 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _CAMERA_H_ +#define _CAMERA_H_ + +#include + +#include "ray.h" +#include "vec2d.h" +#include "boundingBox.h" + +/**************************************************************/ +/* Note: Field of View (fov) is expressed in degrees, and */ +/* represents the angular spread in the horizontal direction. */ +/**************************************************************/ +class camera { + public: + ///////////////// + // Constructor // + ///////////////// + camera(void); + camera(const vec3d& eye, const vec3d& viewDirection, const vec3d& up, float fov, size_t xres, size_t yres); + camera(const camera& cam); + + /////////////// + // Operators // + /////////////// + camera& operator=(const camera& cam); + + ray operator()(float x, float y) const; + + //////////////// + // Inspectors // + //////////////// + size_t width(void) const { return _width; } + size_t height(void) const { return _height; } + + ////////////// + // Mutators // + ////////////// + void frameBoundingBox(const boundingBox& bb); + + ///////////// + // Friends // + ///////////// + friend void swap(camera& a, camera& b) { a._swap(b); } + friend std::ostream& operator<<(std::ostream& s, const camera& cam) + { + s << "Camera: {"; + s << "eye=" << cam._eye << ", "; + s << "view=" << cam._view << ", "; + s << "up=" << cam._up << "}, "; + s << "fov=" << cam._fov << ", "; + s << "resolution=[" << cam._width << "x" << cam._height << "]"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _swap(camera& cam); + void _assign(const camera& cam); + + ////////////////// + // Data Members // + ////////////////// + vec3d _eye, _view, _up; + float _fov; + size_t _width, _height; +}; + +#endif /* _CAMERA_H_ */ + diff --git a/hw2/include/color.h b/hw2/include/color.h new file mode 100644 index 0000000..b2ca490 --- /dev/null +++ b/hw2/include/color.h @@ -0,0 +1,105 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _COLOR_H_ +#define _COLOR_H_ + +#include + +class color { + public: + ///////////// + // Typedef // + ///////////// + typedef float value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef value_type* iterator; + typedef const value_type* const_iterator; + + ////////////////// + // Data Members // + ////////////////// + union { + struct { value_type r, g, b; }; + struct { value_type red, green, blue; }; + value_type data[3]; + }; + + public: + ///////////////// + // Constructor // + ///////////////// + explicit color(const_reference value=0.0f); + color(const_reference r, const_reference g, const_reference b); + color(const color& col); + + //////////////// + // Inspectors // + //////////////// + const_reference operator[](size_t index) const; + reference operator[](size_t index); + + size_t size(void) const { return 3; } + + iterator begin(void); + const_iterator begin(void) const; + iterator end(void); + const_iterator end(void) const; + + /////////////// + // Operators // + /////////////// + color& operator=(const color& col); + + bool operator==(const color& col) const; + bool operator!=(const color& col) const; + + color operator+(const color& col) const; + color operator-(const color& col) const; + color operator*(const color& col) const; + color operator*(const_reference scale) const; + color operator/(const color& col) const; + color operator/(const_reference scale) const; + + color& operator+=(const color& col); + color& operator-=(const color& col); + color& operator*=(const color& col); + color& operator*=(const_reference scale); + color& operator/=(const color& col); + color& operator/=(const_reference scale); + + /////////////// + // Modifiers // + /////////////// + color& abs(void); + color& clamp(const_reference lowerBound=0.0f, const_reference upperBounds=1.0f); + + ///////////// + // Friends // + ///////////// + friend void swap(color& a, color& b) { return a._swap(b); } + friend color abs(const color& c) { return color(c).abs(); } + friend color clamp(const color& c, const_reference lowerBound=0.0f, const_reference upperBound=1.0f) { return color(c).clamp(lowerBound, upperBound); } + friend color operator*(const_reference scale, const color& col) { return (col*scale); } + + friend std::ostream& operator<<(std::ostream& s, const color& col) + { + s << "(" << col.r << "," << col.g << "," << col.b << ")"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _assign(const color& col); + void _swap(color& col); +}; + +#endif /* _COLOR_H_ */ diff --git a/hw2/include/constants.h b/hw2/include/constants.h new file mode 100644 index 0000000..330e89e --- /dev/null +++ b/hw2/include/constants.h @@ -0,0 +1,15 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _CONSTANTS_H_ +#define _CONSTANTS_H_ + +static const float EPSILON = 10e-6; +static const float LARGE = 10e10; + +#endif /* _CONSTANTS_H_ */ diff --git a/hw2/include/errorMessage.h b/hw2/include/errorMessage.h new file mode 100644 index 0000000..7ec99d1 --- /dev/null +++ b/hw2/include/errorMessage.h @@ -0,0 +1,17 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _ERROR_MESSAGE_H_ +#define _ERROR_MESSAGE_H_ + +#include + +void errorMessage(const char* msg, ...); +void warningMessage(const char* msg, ...); + +#endif /* _ERROR_MESSAGE_H_ */ diff --git a/hw2/include/image.h b/hw2/include/image.h new file mode 100644 index 0000000..938e6c0 --- /dev/null +++ b/hw2/include/image.h @@ -0,0 +1,89 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _IMAGE_H_ +#define _IMAGE_H_ + +#include +#include +#include + +#include "color.h" + +class image { + public: + ////////////// + // Typedefs // + ////////////// + typedef color value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef std::unique_ptr::pointer iterator; + typedef std::unique_ptr::pointer const_iterator; + typedef size_t difference_type; + typedef size_t size_type; + + ////////////////// + // Constructors // + ////////////////// + image(size_type width=0, size_type height=0); + image(size_type width, size_type height, const_reference col); + image(const image& src); + image(image&& src); + + virtual ~image(void) { _data.reset(); } + + //////////////// + // Inspectors // + //////////////// + iterator begin(void); + const_iterator begin(void) const; + + iterator end(void); + const_iterator end(void) const; + + size_type size(void) const { return width()*height(); } + size_type width(void) const { return _width; } + size_type height(void) const { return _height; } + + + /////////////// + // Operators // + /////////////// + image& operator=(const image& src); + image& operator=(image&& src); + + reference operator()(size_type x, size_type y); + const_reference operator()(size_type x, size_type y) const; + + ///////////// + // Friends // + ///////////// + friend void swap(image& a, image& b) { a._swap(b); } + friend std::ostream& operator<<(std::ostream& s, const image& img) + { + s << "Image: (" << img.width() << ", " << img.height() << ")"; + return s; + } + +protected: + ///////////////////// + // Private Methods // + ///////////////////// + void _swap(image& img); + void _assign(const image& src); + +private: + ////////////////////////// + // Private Data Members // + ////////////////////////// + size_type _width, _height; + std::unique_ptr _data; +}; + +#endif /* _IMAGE_H_ */ diff --git a/hw2/include/imageIO.h b/hw2/include/imageIO.h new file mode 100644 index 0000000..9e1ecfa --- /dev/null +++ b/hw2/include/imageIO.h @@ -0,0 +1,19 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _IMAGEIO_H_ +#define _IMAGEIO_H_ + +#include +#include "image.h" + +void importImage(const std::string& name, image& img); +void exportImage(const std::string& name, const image& img); + +#endif /* _IMAGEIO_H_ */ + diff --git a/hw2/include/imageIO.pfm.h b/hw2/include/imageIO.pfm.h new file mode 100644 index 0000000..d09531a --- /dev/null +++ b/hw2/include/imageIO.pfm.h @@ -0,0 +1,18 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _IMAGEIO_PFM_H_ +#define _IMAGEIO_PFM_H_ + +#include +#include "image.h" + +void importPFM(const std::string& name, image& img); +void exportPFM(const std::string& name, const image& img); + +#endif /* _IMAGEIO_PFM_H_ */ diff --git a/hw2/include/imageIO.ppm.h b/hw2/include/imageIO.ppm.h new file mode 100644 index 0000000..7a8db42 --- /dev/null +++ b/hw2/include/imageIO.ppm.h @@ -0,0 +1,18 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _IMAGEIO_PPM_H_ +#define _IMAGEIO_PPM_H_ + +#include +#include "image.h" + +void importPPM(const std::string& name, image& img); +void exportPPM(const std::string& name, const image& img); + +#endif /* _IMAGEIO_PPM_H_ */ diff --git a/hw2/include/interval.h b/hw2/include/interval.h new file mode 100644 index 0000000..ccb720a --- /dev/null +++ b/hw2/include/interval.h @@ -0,0 +1,77 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _INTERVAL_H_ +#define _INTERVAL_H_ + +#include + +#include "constants.h" + +class interval +{ + public: + ////////////////// + // Constrructor // + ////////////////// + interval(float lower=-LARGE, float upper=+LARGE); + interval(const interval& i); + + /////////////// + // Operators // + /////////////// + interval& operator=(const interval& i); + + interval operator+(float v) const; + interval operator-(float v) const; + interval operator*(float v) const; + interval operator/(float v) const; + + interval& operator+=(float v); + interval& operator-=(float v); + interval& operator*=(float v); + interval& operator/=(float v); + + //////////////// + // Inspectors // + //////////////// + float lower(void) const; + float upper(void) const; + + bool empty(void) const; + + ////////////// + // Mutators // + ////////////// + void intersect(const interval& i); + + ///////////// + // Friends // + ///////////// + friend void swap(interval& a, interval& b) { a._swap(b); } + + friend std::ostream& operator<<(std::ostream& s, const interval& i) + { + s << "[" << i.lower() << ", " << i.upper() << "]"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _assign(const interval& i); + void _swap(interval& i); + + ////////////////////////// + // Private Data Members // + ////////////////////////// + float _lower, _upper; +}; + +#endif /* _INTERVAL_H_ */ diff --git a/hw2/include/mat3d.h b/hw2/include/mat3d.h new file mode 100644 index 0000000..e70ece4 --- /dev/null +++ b/hw2/include/mat3d.h @@ -0,0 +1,112 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _MAT3D_H_ +#define _MAT3D_H_ + +#include +#include + +#include "vec3d.h" + +class mat3d { + public: + ///////////// + // Typedef // + ///////////// + typedef float value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef std::array::iterator iterator; + typedef std::array::const_iterator const_iterator; + typedef std::size_t difference; + typedef std::size_t size_type; + + ///////////////// + // Constructor // + ///////////////// + mat3d(void); // all elements are set to 0 + explicit mat3d(value_type diag); // all elements are set to 0, the diagonal elements are set to 'diag' + mat3d(const vec3d& X, const vec3d& Y, const vec3d& Z); // set columns to X, Y, and Z + mat3d(const mat3d& m); // copy constructor + + //////////////// + // Inspectors // + //////////////// + static size_type width(void) { return 3; } + static size_type height(void) { return 3; } + static size_type size(void) { return 9; } + + iterator begin(void); + const_iterator begin(void) const; + iterator end(void); + const_iterator end(void) const; + + ////////////// + // Mutators // + ////////////// + void clear(value_type value=0.0f); + void setDiagonal(value_type value=1.0f); + + mat3d& transpose(void); + + /////////////// + // Operators // + /////////////// + mat3d& operator=(const mat3d& m); + + reference operator()(size_type row, size_type col); + const_reference operator()(size_type row, size_type col) const; + + mat3d operator+(const mat3d& m) const; + mat3d operator-(const mat3d& m) const; + mat3d operator*(const mat3d& m) const; + vec3d operator*(const vec3d& v) const; + mat3d operator*(value_type scale) const; + mat3d operator/(value_type scale) const; + + mat3d& operator+=(const mat3d& m); + mat3d& operator-=(const mat3d& m); + mat3d& operator*=(const mat3d& m); + mat3d& operator*=(value_type scale); + mat3d& operator/=(value_type scale); + + ///////////// + // Friends // + ///////////// + friend void swap(mat3d& a, mat3d& b) { a._swap(b); } + + friend vec3d operator*(const vec3d& v, const mat3d& m) { return m._premultiply(v); } + friend mat3d operator*(value_type scale, const mat3d& m) { return (m * scale); } + + friend mat3d transpose(mat3d& m) { mat3d n(m); return n.transpose(); } + + friend std::ostream& operator<<(std::ostream& s, const mat3d& m) + { + s << "[[" << m(0,0) << ", " << m(0,1) << ", " << m(0,2) << "],["; + s << m(1,0) << ", " << m(1,1) << ", " << m(1,2) << "],["; + s << m(2,0) << ", " << m(2,1) << ", " << m(2,2) << "]]"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _swap(mat3d& m); + void _assign(const mat3d& m); + + vec3d _premultiply(const vec3d& v) const; + + ////////////////////////// + // Private Data Members // + ////////////////////////// + std::array _data; +}; + +#endif /* _MAT3D_H_ */ diff --git a/hw2/include/ray.h b/hw2/include/ray.h new file mode 100644 index 0000000..743375f --- /dev/null +++ b/hw2/include/ray.h @@ -0,0 +1,69 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _RAY_H_ +#define _RAY_H_ + +#include +#include "vec3d.h" +#include "transformation3d.h" + +class ray { + public: + ////////////////// + // Constructors // + ////////////////// + ray(const vec3d& origin = vec3d(), const vec3d& direction = vec3d()); + ray(const ray& src); + + //////////////// + // Inspectors // + //////////////// + const vec3d& origin(void) const { return _origin; } + const vec3d& direction(void) const { return _direction; } + + /////////////// + // Operators // + /////////////// + ray& operator=(const ray& r); + vec3d operator()(float t) const; + float operator()(const vec3d& point) const; + + ////////////// + // Mutators // + ////////////// + ray& transform(const transformation3d& t); + ray& inverseTransform(const transformation3d& t); + + ///////////// + // Friends // + ///////////// + friend void swap(ray& a, ray& b) { a._swap(b); } + friend ray transform(const ray& r, const transformation3d& t) { return ray(r).transform(t); } + friend ray inverseTransform(const ray& r, const transformation3d& t) { return ray(r).inverseTransform(t); } + friend std::ostream& operator<<(std::ostream& s, const ray& r) + { + s << r.origin() << "->" << r.direction(); + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _swap(ray& r); + void _assign(const ray& r); + + ////////////////// + // Data Members // + ////////////////// + vec3d _origin; + vec3d _direction; +}; + +#endif /* _RAY_H_ */ diff --git a/hw2/include/transformation3d.h b/hw2/include/transformation3d.h new file mode 100644 index 0000000..7b61ecc --- /dev/null +++ b/hw2/include/transformation3d.h @@ -0,0 +1,77 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _TRANSFORMATION3D_H_ +#define _TRANSFORMATION3D_H_ + +#include "vec3d.h" +#include "mat3d.h" + +class transformation3d { + public: + ////////////////// + // Constructors // + ////////////////// + transformation3d(void); + transformation3d(const vec3d& translation, const mat3d& transformation, const mat3d& inverseTransform); + transformation3d(const transformation3d& t); + + ////////////// + // Operator // + ////////////// + transformation3d& operator=(const transformation3d& t); + + transformation3d operator*(const transformation3d& t) const; + transformation3d& operator*=(const transformation3d& t); + + ////////////// + // Mutators // + ////////////// + transformation3d& invert(void); + + ///////////// + // Methods // + ///////////// + vec3d transformPoint(const vec3d& p) const; + vec3d transformDirection(const vec3d& d) const; + vec3d transformNormal(const vec3d& n) const; + + vec3d inverseTransformPoint(const vec3d& p) const; + vec3d inverseTransformDirection(const vec3d& d) const; + vec3d inverseTransformNormal(const vec3d& n) const; + + ///////////// + // Friends // + ///////////// + friend void swap(transformation3d& a, transformation3d& b) { a._swap(b); } + + friend transformation3d inverse(const transformation3d& t) { transformation3d i(t); return i.invert(); } + + friend std::ostream& operator<<(std::ostream& s, const transformation3d& t) + { + t._print(s); + return s; + } + + protected: + /////////////////////// + // Protected Methods // + /////////////////////// + void _swap(transformation3d& t); + void _assign(const transformation3d& t); + virtual void _print(std::ostream& s) const; + + //////////////////////////// + // Protected Data Members // + //////////////////////////// + vec3d _translation; + mat3d _transformation; + mat3d _inverseTransformation; +}; + +#endif /* _TRANSFORMATION3D_H_ */ diff --git a/hw2/include/triangle.h b/hw2/include/triangle.h new file mode 100644 index 0000000..f7d85bf --- /dev/null +++ b/hw2/include/triangle.h @@ -0,0 +1,115 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _TRIANGLE_H_ +#define _TRIANGLE_H_ + +#include +#include +#include +#include + +#include "ray.h" +#include "vec2d.h" +#include "vec3d.h" +#include "boundingBox.h" + +class triangle { + public: + ////////////////// + // Constructors // + ////////////////// + triangle(void); + triangle(const triangle& t); + triangle(triangle&& t); + + triangle(const vec3d& v1, const vec3d& v2, const vec3d& v3); + triangle(size_t v1_idx, size_t v2_idx, size_t v3_idx, const std::shared_ptr>& vertex_list); + + triangle(const vec3d& v1, const vec3d& v2, const vec3d& v3, + const vec3d& n1, const vec3d& n2, const vec3d& n3); + triangle(size_t v1_idx, size_t v2_idx, size_t v3_idx, const std::shared_ptr>& vertex_list, + size_t n1_idx, size_t n2_idx, size_t n3_idx, const std::shared_ptr>& normal_list); + + triangle(const vec3d& v1, const vec3d& v2, const vec3d& v3, + const vec2d& t1, const vec2d& t2, const vec2d& t3); + triangle(size_t v1_idx, size_t v2_idx, size_t v3_idx, const std::shared_ptr>& vertex_list, + size_t t1_idx, size_t t2_idx, size_t t3_idx, const std::shared_ptr>& texcoord_list); + + triangle(const vec3d& v1, const vec3d& v2, const vec3d& v3, + const vec3d& n1, const vec3d& n2, const vec3d& n3, + const vec2d& t1, const vec2d& t2, const vec2d& t3); + triangle(size_t v1_idx, size_t v2_idx, size_t v3_idx, const std::shared_ptr>& vertex_list, + size_t n1_idx, size_t n2_idx, size_t n3_idx, const std::shared_ptr>& normal_list, + size_t t1_idx, size_t t2_idx, size_t t3_idx, const std::shared_ptr>& texcoord_list); + + //////////////// + // Inspectors // + //////////////// + const vec3d& vertex(size_t index) const; + const vec3d& normal(size_t index) const; + const vec2d& textureCoordinate(size_t index) const; + + bool hasPerVertexNormals(void) const; + bool hasPerVertexTextureCoordinates(void) const; + + /////////////// + // Operators // + /////////////// + triangle& operator=(const triangle& t); + triangle& operator=(triangle&& t); + + ///////////// + // Methods // + ///////////// + bool intersect(const ray& r, vec3d& barycentricCoord, float& t) const; + + boundingBox boundingbox(void) const; + vec3d vertex(const vec3d& barycentricCoord) const; + vec3d normal(void) const; + vec3d shadingAxis(void) const; + vec3d normal(const vec3d& barycentricCoord) const; + vec2d textureCoordinate(const vec3d& barycentricCoord) const; + + vec3d sample(float r1, float r2, vec3d& barycentricCoord, float& pdf) const; + float area(void) const; + + ///////////// + // Friends // + ///////////// + friend void swap(triangle& a, triangle& b) { a._swap(b); } + + friend std::ostream& operator<<(std::ostream& s, const triangle& t) + { + s << "Triangle: v=(" << t.vertex(0) << "," << t.vertex(1) << "," << t.vertex(2) << ")"; + if(t._normal_list) + s << ", n=(" << t.normal(0) << "," << t.normal(1) << "," << t.normal(2) << ")"; + if(t._textureCoord_list) + s << ", t=(" << t.textureCoordinate(0) << "," << t.textureCoordinate(1) << "," << t.textureCoordinate(2) << ")"; + return s; + } + +private: + ///////////////////// + // Private Methods // + ///////////////////// + void _assign(const triangle& t); + void _swap(triangle& t); + + ////////////////// + // Data Members // + ////////////////// + std::array _vertex_idx; + std::array _normal_idx; + std::array _textureCoord_idx; + std::shared_ptr> _vertex_list; + std::shared_ptr> _normal_list; + std::shared_ptr> _textureCoord_list; +}; + +#endif /* _TRIANGLE_H_ */ diff --git a/hw2/include/util.h b/hw2/include/util.h new file mode 100644 index 0000000..2146464 --- /dev/null +++ b/hw2/include/util.h @@ -0,0 +1,19 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ + +#ifndef _UTIL_H_ +#define _UTIL_H_ + +#include + +std::string getFilename(const std::string& path); +std::string getExtension(const std::string& path); +std::string getDirectory(const std::string& path); + +#endif /* _UTIL_H_ */ diff --git a/hw2/include/vec2d.h b/hw2/include/vec2d.h new file mode 100644 index 0000000..36e0e5c --- /dev/null +++ b/hw2/include/vec2d.h @@ -0,0 +1,117 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _VEC2D_H_ +#define _VEC2D_H_ + +#include +#include + +class vec2d { + public: + ///////////// + // Typedef // + ///////////// + typedef float value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef value_type* iterator; + typedef const value_type* const_iterator; + + ////////////////// + // Data Members // + ////////////////// + union { + struct { value_type x, y; }; + struct { value_type u, v; }; + value_type data[2]; + }; + + public: + ///////////////// + // Constructor // + ///////////////// + explicit vec2d(const_reference value=0.0f); + vec2d(const_reference x, const_reference y); + vec2d(const vec2d& v); + + //////////////// + // Inspectors // + //////////////// + static size_t size(void) { return 2; } + + iterator begin(void); + const_iterator begin(void) const; + iterator end(void); + const_iterator end(void) const; + + const_reference operator[](size_t index) const; + reference operator[](size_t index); + + /////////////// + // Operators // + /////////////// + vec2d& operator=(const vec2d& v); + + bool operator==(const vec2d& v) const; + bool operator!=(const vec2d& v) const; + + vec2d operator-(void) const; + + vec2d operator+(const vec2d& v) const; + vec2d operator-(const vec2d& v) const; + vec2d operator*(const vec2d& v) const; + vec2d operator*(const_reference scale) const; + vec2d operator/(const vec2d& v) const; + vec2d operator/(const_reference scale) const; + + vec2d& operator+=(const vec2d& v); + vec2d& operator-=(const vec2d& v); + vec2d& operator*=(const vec2d& v); + vec2d& operator*=(const_reference scale); + vec2d& operator/=(const vec2d& v); + vec2d& operator/=(const_reference scale); + + ///////////// + // Methods // + ///////////// + value_type dot(const vec2d& v) const; + value_type squared_length(void) const; + value_type length(void) const; + value_type squared_distance(const vec2d& v) const; + value_type distance(const vec2d& v) const; + + /////////////// + // Modifiers // + /////////////// + vec2d& abs(void); + vec2d& normalize(void); + + ///////////// + // Friends // + ///////////// + friend void swap(vec2d& a, vec2d& b) { return a._swap(b); } + friend vec2d normalize(const vec2d& v) { return vec2d(v).normalize(); } + friend vec2d abs(const vec2d& v) { return vec2d(v).abs(); } + friend vec2d operator*(const_reference scale, const vec2d& v) { return (v*scale); } + + friend std::ostream& operator<<(std::ostream& s, const vec2d& v) + { + s << "[" << v.x << "," << v.y << "]"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _assign(const vec2d& v); + void _swap(vec2d& v); +}; + +#endif /* _VEC2D_H_ */ diff --git a/hw2/include/vec3d.h b/hw2/include/vec3d.h new file mode 100644 index 0000000..50d918f --- /dev/null +++ b/hw2/include/vec3d.h @@ -0,0 +1,120 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#ifndef _VEC3D_H_ +#define _VEC3D_H_ + +#include +#include + +class vec3d { + public: + ///////////// + // Typedef // + ///////////// + typedef float value_type; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef value_type* iterator; + typedef const value_type* const_iterator; + + ////////////////// + // Data Members // + ////////////////// + union { + struct { value_type x, y, z; }; + value_type data[3]; + }; + + public: + ///////////////// + // Constructor // + ///////////////// + explicit vec3d(const_reference value=0.0f); + vec3d(const_reference x, const_reference y, const_reference z); + vec3d(const vec3d& v); + + //////////////// + // Inspectors // + //////////////// + static size_t size(void) { return 3; } + + iterator begin(void); + const_iterator begin(void) const; + iterator end(void); + const_iterator end(void) const; + + const_reference operator[](size_t index) const; + reference operator[](size_t index); + + /////////////// + // Operators // + /////////////// + vec3d& operator=(const vec3d& v); + + bool operator==(const vec3d& v) const; + bool operator!=(const vec3d& v) const; + + vec3d operator-(void) const; + + vec3d operator+(const vec3d& v) const; + vec3d operator-(const vec3d& v) const; + vec3d operator*(const vec3d& v) const; + vec3d operator*(const_reference scale) const; + vec3d operator/(const vec3d& v) const; + vec3d operator/(const_reference scale) const; + + vec3d& operator+=(const vec3d& v); + vec3d& operator-=(const vec3d& v); + vec3d& operator*=(const vec3d& v); + vec3d& operator*=(const_reference scale); + vec3d& operator/=(const vec3d& v); + vec3d& operator/=(const_reference scale); + + ///////////// + // Methods // + ///////////// + value_type dot(const vec3d& v) const; + value_type squared_length(void) const; + value_type length(void) const; + value_type squared_distance(const vec3d& v) const; + value_type distance(const vec3d& v) const; + + vec3d cross(const vec3d& v) const; + + /////////////// + // Modifiers // + /////////////// + vec3d& abs(void); + vec3d& clamp(value_type lower=0.0f, value_type upper=1.0f); + vec3d& normalize(void); + + ///////////// + // Friends // + ///////////// + friend void swap(vec3d& a, vec3d& b) { return a._swap(b); } + friend vec3d normalize(const vec3d& v) { return vec3d(v).normalize(); } + friend vec3d abs(const vec3d& v) { return vec3d(v).abs(); } + friend vec3d clamp(const vec3d& v, value_type lower=0.0f, value_type upper=1.0f) { return vec3d(v).clamp(lower, upper); } + friend vec3d operator*(const_reference scale, const vec3d& v) { return (v*scale); } + + friend std::ostream& operator<<(std::ostream& s, const vec3d& v) + { + s << "[" << v.x << "," << v.y << "," << v.z << "]"; + return s; + } + + private: + ///////////////////// + // Private Methods // + ///////////////////// + void _assign(const vec3d& v); + void _swap(vec3d& v); +}; + +#endif /* _VEC3D_H_ */ -- cgit v1.2.3