project: COLLADA 2 BIN

Details

Program that converts geometric and animation 3D scene data, from a COLLADA "*.dae" file to a binary file, with a custom format.

  • current version: 1.0
  • last updated: February, 2019
Download source

src > math > vec3.h

View raw
#ifndef MATH_VEC3_H_
#define MATH_VEC3_H_

#include "../def.hpp"

#include "./vec2.h"
#include "./functions.h"

namespace math
{

  struct IVEC3
  {
    int x, y, z;
    IVEC3() {x= y= z= 0;}
    IVEC3(int s): x(s), y(s), z(s) {}
    IVEC3(int x, int y, int z) : x(x), y(y), z(z) {}

    inline int& operator [] (int index) const { ASSERT(index >= 0 && index <= 2); return ((int*)this)[index]; }

    inline bool operator == (const IVEC3 &v) const { return x == v.x && y == v.y && z == v.z; }
    inline bool operator != (const IVEC3 &v) const { return !(*this == v); }
    inline bool operator == (int s) const { return x == s && y == s && z == s; }
    inline bool operator != (int s) const { return !(*this == s); }
    inline bool operator < (const IVEC3 &v) const { return x < v.x && y < v.y && z < v.z; }
    inline bool operator > (const IVEC3 &v) const { return x > v.x && y > v.y && z > v.z; }
    inline IVEC3 operator - () const { return IVEC3(-x, -y, -z); }

    IVEC3& operator += (const IVEC3 &v) { x += v.x; y += v.y; z += v.z; return *this; }
    IVEC3& operator -= (const IVEC3 &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
    IVEC3& operator *= (const IVEC3 &v) { x *= v.x; y *= v.y; z *= v.z; return *this; }
    IVEC3& operator /= (const IVEC3 &v) { x /= v.x; y /= v.y; z /= v.z; return *this; }
    IVEC3& operator += (int s) { x += s;  y += s;  z += s;  return *this; }
    IVEC3& operator -= (int s) { x -= s;  y -= s;  z -= s;  return *this; }
    IVEC3& operator *= (int s) { x *= s;  y *= s;  z *= s;  return *this; }
    IVEC3& operator /= (int s) { x /= s;  y /= s;  z /= s;  return *this; }

    IVEC3 operator + (const IVEC3 &v) const { return IVEC3(x + v.x, y + v.y, z + v.z); }
    IVEC3 operator - (const IVEC3 &v) const { return IVEC3(x - v.x, y - v.y, z - v.z); }
    IVEC3 operator * (const IVEC3 &v) const { return IVEC3(x * v.x, y * v.y, z * v.z); }
    IVEC3 operator / (const IVEC3 &v) const { return IVEC3(x / v.x, y / v.y, z / v.z); }
    IVEC3 operator + (int s) const { return IVEC3(x + s,  y + s,  z + s); }
    IVEC3 operator - (int s) const { return IVEC3(x - s,  y - s,  z - s); }
    IVEC3 operator * (int s) const { return IVEC3(x * s,  y * s,  z * s); }
    IVEC3 operator / (int s) const { return IVEC3(x / s,  y / s,  z / s); }
  };
  
  struct VEC3 
  {
    float x, y, z;

    VEC3() {}
    VEC3(float s) : x(s), y(s), z(s) {}
    VEC3(float x, float y, float z) : x(x), y(y), z(z) {}
    VEC3(const VEC2 &xy, float z = 0.0f) : x(xy.x), y(xy.y), z(z) {}
    VEC3(float lng, float lat) : x(fFAST_SIN(lat) * fFAST_COS(lng)), y(-fFAST_SIN(lng)), z(fFAST_COS(lat) * fFAST_COS(lng)) {}

    VEC2& xy() const { return *((VEC2*)&x); }
    VEC2& yz() const { return *((VEC2*)&y); }

    inline float& operator [] (int index) const { ASSERT(index >= 0 && index <= 2); return ((float*)this)[index]; }

    inline bool operator == (const VEC3 &v) const { return x == v.x && y == v.y && z == v.z; }
    inline bool operator != (const VEC3 &v) const { return !(*this == v); }
    inline bool operator == (float s) const { return x == s && y == s && z == s; }
    inline bool operator != (float s) const { return !(*this == s); }
    inline bool operator < (const VEC3 &v) const { return x < v.x && y < v.y && z < v.z; }
    inline bool operator > (const VEC3 &v) const { return x > v.x && y > v.y && z > v.z; }
    inline VEC3 operator - () const { return VEC3(-x, -y, -z); }

    VEC3& operator += (const VEC3 &v) { x += v.x; y += v.y; z += v.z; return *this; }
    VEC3& operator -= (const VEC3 &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
    VEC3& operator *= (const VEC3 &v) { x *= v.x; y *= v.y; z *= v.z; return *this; }
    VEC3& operator /= (const VEC3 &v) { x /= v.x; y /= v.y; z /= v.z; return *this; }
    VEC3& operator += (float s) { x += s;  y += s;  z += s;  return *this; }
    VEC3& operator -= (float s) { x -= s;  y -= s;  z -= s;  return *this; }
    VEC3& operator *= (float s) { x *= s;  y *= s;  z *= s;  return *this; }
    VEC3& operator /= (float s) { x /= s;  y /= s;  z /= s;  return *this; }

    VEC3 operator + (const VEC3 &v) const { return VEC3(x + v.x, y + v.y, z + v.z); }
    VEC3 operator - (const VEC3 &v) const { return VEC3(x - v.x, y - v.y, z - v.z); }
    VEC3 operator * (const VEC3 &v) const { return VEC3(x * v.x, y * v.y, z * v.z); }
    VEC3 operator / (const VEC3 &v) const { return VEC3(x / v.x, y / v.y, z / v.z); }
    VEC3 operator + (float s) const { return VEC3(x + s,  y + s,  z + s);  }
    VEC3 operator - (float s) const { return VEC3(x - s,  y - s,  z - s);  }
    VEC3 operator * (float s) const { return VEC3(x * s,  y * s,  z * s);  }
    VEC3 operator / (float s) const { return VEC3(x / s,  y / s,  z / s);  }

    float dot(const VEC3 &v)  const { return x * v.x + y * v.y + z * v.z; }
    VEC3 cross(const VEC3 &v) const { return VEC3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }

    float length2() const { return dot(*this); }
    float length() const { return fSQRTF(length2()); }
    VEC3 abs()   const { return VEC3(fFABS(x), fFABS(y), fFABS(z)); }
    VEC3 normal() const { float s = length(); return s == 0.0f ? (*this) : (*this)*(1.0f/s); }
    VEC3 axisXZ() const { return (fFABS(x) > fFABS(z)) ? VEC3(float(sign(x)), 0, 0) : VEC3(0, 0, float(sign(z))); }

    VEC3 reflect(const VEC3 &n) const {
      return *this - n * (dot(n) * 2.0f);
    }

    VEC3 lerp(const VEC3 &v, const float t) const {
      if (t <= 0.0f) return *this;
      if (t >= 1.0f) return v;
      return *this + (v - *this) * t; 
    }

    VEC3 rotateY(float angle) const {
      float s, c;
      fFAST_SINCOS(angle, &s, &c);
      return VEC3(x*c - z*s, y, x*s + z*c);
    }

    float angle(const VEC3 &v) const {
      return dot(v) / (length() * v.length());
    }

    //float angleY() const { return atan2f(z, x); }
  };

}

#endif