// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_vec3f
#define tools_vec3f

#include "vec3"
#include "../S_STRING"
#include <cmath> //sqrt

namespace tools {

class vec3f : public vec3<float> {
  typedef vec3<float> parent;
public:
  TOOLS_SCLASS(tools::vec3f) //for stype()
public:
  vec3f():parent(){}
  vec3f(const float a_vec[3]):parent(a_vec){}
  vec3f(float a0,float a1,float a2):parent(a0,a1,a2){}
  virtual ~vec3f() {}
public:
  vec3f(const vec3f& a_from):parent(a_from){}
  vec3f& operator=(const vec3f& a_from){
    parent::operator=(a_from);
    return *this;
  }

  vec3f(const parent& a_from):parent(a_from){}

public: //operators
  vec3f operator*(float a_v) const {
    return vec3f(m_data[0]*a_v,
                 m_data[1]*a_v,
                 m_data[2]*a_v);
  }
  vec3f operator+(const vec3f& a_v) const {
    return vec3f(m_data[0]+a_v.m_data[0],
                 m_data[1]+a_v.m_data[1],
                 m_data[2]+a_v.m_data[2]);
  }
  vec3f operator-(const vec3f& a_v) const {
    return vec3f(m_data[0]-a_v.m_data[0],
                 m_data[1]-a_v.m_data[1],
                 m_data[2]-a_v.m_data[2]);
  }
  vec3f& operator+=(const vec3f& a_v) {
    m_data[0] += a_v.m_data[0];
    m_data[1] += a_v.m_data[1];
    m_data[2] += a_v.m_data[2];
    return *this;
  }
  vec3f& operator-=(const vec3f& a_v) {
    m_data[0] -= a_v.m_data[0];
    m_data[1] -= a_v.m_data[1];
    m_data[2] -= a_v.m_data[2];
    return *this;
  }
  vec3f& operator*=(float a_v) {
    m_data[0] *= a_v;
    m_data[1] *= a_v;
    m_data[2] *= a_v;
    return *this;
  }
  vec3f operator-() const {
    return vec3f(-m_data[0],-m_data[1],-m_data[2]);
  }
public:
  float length() const {
    return float(::sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2]));
  }
  float normalize() {
    float norme = length();
    if(!norme) return 0;
    divide(norme);
    return norme;
  }
  bool cos_angle(const vec3f& a_v,float& a_cos) const {
    //WARNING : if ret false, a_cos is not set.
    float this_length = length();
    if(this_length==0.0f) return false;
    float a_v_length = a_v.length();
    if(a_v_length==0.0f) return false;
    a_cos = dot(a_v)/(this_length*a_v_length);
    return true;
  }

  bool theta_phi(float& a_theta,float& a_phi) const {
    return parent::theta_phi(a_theta,a_phi,::sqrtf,::atan2f);
  }
public: //iv2sg
  bool equals(const vec3f& a_v,const float a_epsil) const {
    //if(a_epsil<0.0f))
    float d0 = m_data[0]-a_v.m_data[0];
    float d1 = m_data[1]-a_v.m_data[1];
    float d2 = m_data[2]-a_v.m_data[2];
    return ((d0*d0+d1*d1+d2*d2)<=a_epsil);
  }
  void negate() {
    m_data[0] = -m_data[0];
    m_data[1] = -m_data[1];
    m_data[2] = -m_data[2];
  }

private:static void check_instantiation() {vec3f v(0,0,0);v.set_value(1,1,1);}
};

inline vec3f operator*(float a_f,const vec3f& a_v) {
  vec3f res(a_v);
  res *= a_f;
  return res;
}

inline void get_normal(const vec3f& a_p0,const vec3f& a_p1,const vec3f& a_p2,vec3f& a_nm,
                       vec3f& a_tmp_1,vec3f& a_tmp_2) {
  // Used to optimize sg::bin().
  //(a_p1-a_p0).cross(a_p2-a_p1,a_nm);

  a_tmp_1 = a_p1;
  a_tmp_1.subtract(a_p0);

  a_tmp_2 = a_p2;
  a_tmp_2.subtract(a_p1);

  a_tmp_1.cross(a_tmp_2,a_nm);

  a_nm.normalize();
}

}

#include <vector>

namespace tools {

#ifndef SWIG
//for sf, mf :
inline bool set_from_vec(vec3f& a_v,const std::vector<float>& a_sv) {
  if(a_sv.size()!=3) return false;
  a_v[0] = a_sv[0];
  a_v[1] = a_sv[1];
  a_v[2] = a_sv[2];
  return true;
}
#endif

}

#endif
