diff options
author | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
---|---|---|
committer | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
commit | db072ad4dc181eca5a1458656b130beb43f475bf (patch) | |
tree | a3c03c7f5497cb70503e2486662fa85cfb53415a /hw2/include/vec3d.h | |
download | csci427-db072ad4dc181eca5a1458656b130beb43f475bf.tar.xz csci427-db072ad4dc181eca5a1458656b130beb43f475bf.zip |
Diffstat (limited to 'hw2/include/vec3d.h')
-rw-r--r-- | hw2/include/vec3d.h | 120 |
1 files changed, 120 insertions, 0 deletions
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 <ostream> +#include <cmath> + +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_ */ |