/******************************************************************/ /* 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_ */