Skip to content

Instantly share code, notes, and snippets.

@markhc
Last active September 17, 2017 15:52
Show Gist options
  • Save markhc/fb0fba401f287d5149c32f22b9afbd8b to your computer and use it in GitHub Desktop.
Save markhc/fb0fba401f287d5149c32f22b9afbd8b to your computer and use it in GitHub Desktop.
#pragma once
#include <cstdint>
#include <array>
#include <string>
template<typename T, uint32_t D>
class vec
{
public:
constexpr auto size = D;
typedef vec<T, D> vec_type;
typedef T value_type;
vec();
explicit vec(value_type v);
explicit vec(const std::array<T, D>& val);
const value_type* data() const;
value_type* data();
value_type at(int i) const;
value_type& at(int i);
value_type length_sqr() const;
value_type length() const;
vec_type& normalize();
vec_type normalized() const;
value_type angle_to (const vec<T, D>& v) const;
value_type dist_to (const vec<T, D>& v) const;
value_type dot_product(const vec<T, D>& v) const;
vec_type abs() const;
vec_type ortho() const;
bool equals(const vec<T, D>& v, float tolerance) const;
bool is_zero() const;
bool is_unit() const;
bool is_normalized() const;
std::string to_string() const;
value_type& operator[](int i);
value_type operator[](int i) const;
bool operator==(const vec_type& v) const;
bool operator!=(const vec_type& v) const;
vec_type operator-() const;
vec_type operator*(float fl) const;
vec_type operator/(float fl) const;
vec_type operator-(const vec_type& v) const;
vec_type operator+(const vec_type& v) const;
vec_type operator*(const vec_type& v) const;
vec_type operator/(const vec_type& v) const;
vec_type& operator*=(float fl) const;
vec_type& operator/=(float fl) const;
vec_type& operator-=(const vec_type& v) const;
vec_type& operator+=(const vec_type& v) const;
vec_type& operator*=(const vec_type& v) const;
vec_type& operator/=(const vec_type& v) const;
private:
std::array<value_type, D> _data;
};
template<typename T, uint32_t D>
vec<T, D> operator*(float fl, const vec<T, D>& v);
#include "vec.inl"
//
// Define some free function so that we can do
// things like normalize(vec) instead of only vec.normalize()
//
#define PH_VEC_FREEFUN(name) template<typename T, uint32_t D> \
constexpr auto length(vec<T, D>& _Vec) \
-> decltype(_Vec.name()) \
{ return (_Vec.name()); }
PH_VEC_FREEFUN(length_sqr);
PH_VEC_FREEFUN(length);
PH_VEC_FREEFUN(normalize);
PH_VEC_FREEFUN(normalized);
PH_VEC_FREEFUN(angle_to);
PH_VEC_FREEFUN(dist_to);
PH_VEC_FREEFUN(dot_product);
PH_VEC_FREEFUN(abs);
PH_VEC_FREEFUN(ortho);
PH_VEC_FREEFUN(equals);
PH_VEC_FREEFUN(is_zero);
PH_VEC_FREEFUN(is_unit);
PH_VEC_FREEFUN(is_normalized);
PH_VEC_FREEFUN(to_string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment