// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "linearspace.isph"

struct AffineSpace3f
{
  LinearSpace3f l;
  Vec3f p;
};

struct AffineSpace3fa
{
  LinearSpace3fa l;
  Vec3fa p;
};

////////////////////////////////////////////////////////////////////////////////
/// Constructors
////////////////////////////////////////////////////////////////////////////////

inline uniform AffineSpace3f make_AffineSpace3f(const uniform LinearSpace3f l) {
  uniform AffineSpace3f xfm; xfm.l = l; xfm.p = make_Vec3f(0,0,0); return xfm;
}
inline varying AffineSpace3f make_AffineSpace3f(const varying LinearSpace3f l) {
  varying AffineSpace3f xfm; xfm.l = l; xfm.p = make_Vec3f(0,0,0); return xfm;
}
inline uniform AffineSpace3f make_AffineSpace3f(const uniform LinearSpace3f l, const uniform Vec3f p) {
  uniform AffineSpace3f xfm; xfm.l = l; xfm.p = p; return xfm;
}
inline varying AffineSpace3f make_AffineSpace3f(const varying LinearSpace3f l, const varying Vec3f p) {
  varying AffineSpace3f xfm; xfm.l = l; xfm.p = p; return xfm;
}

inline uniform AffineSpace3f make_AffineSpace3f(const uniform AffineSpace3fa l) {
  uniform AffineSpace3f xfm; xfm.l = make_LinearSpace3f(l.l); xfm.p = make_Vec3f(l.p); return xfm;
}

inline varying AffineSpace3f make_AffineSpace3f(const varying AffineSpace3fa l) {
  varying AffineSpace3f xfm; xfm.l = make_LinearSpace3f(l.l); xfm.p = make_Vec3f(l.p); return xfm;
}

inline uniform AffineSpace3f make_AffineSpace3f(const uniform Vec3f x, const uniform Vec3f y, const uniform Vec3f z, const uniform Vec3f p) {
  uniform AffineSpace3f xfm; xfm.l.vx = x; xfm.l.vy = y; xfm.l.vz = z; xfm.p = p; return xfm;
}
inline varying AffineSpace3f make_AffineSpace3f(const varying Vec3f x, const varying Vec3f y, const varying Vec3f z, const varying Vec3f p) {
  varying AffineSpace3f xfm; xfm.l.vx = x; xfm.l.vy = y; xfm.l.vz = z; xfm.p = p; return xfm;
}

////////////////////////////////////////////////////////////////////////////////
// Unary Operators
////////////////////////////////////////////////////////////////////////////////

inline uniform AffineSpace3f neg(const uniform AffineSpace3f a) { return make_AffineSpace3f(neg(a.l),neg(a.p)); }
inline varying AffineSpace3f neg(const varying AffineSpace3f a) { return make_AffineSpace3f(neg(a.l),neg(a.p)); }

inline uniform AffineSpace3f rcp( const uniform AffineSpace3f a) { uniform LinearSpace3f il = rcp(a.l); return make_AffineSpace3f(il,neg(il*a.p)); }
inline varying AffineSpace3f rcp( const varying AffineSpace3f a) { varying LinearSpace3f il = rcp(a.l); return make_AffineSpace3f(il,neg(il*a.p)); }

////////////////////////////////////////////////////////////////////////////////
/// Binary Operators
////////////////////////////////////////////////////////////////////////////////

inline uniform AffineSpace3f operator+(const uniform AffineSpace3f a, const uniform AffineSpace3f b) { return make_AffineSpace3f(a.l+b.l, a.p+b.p); }
inline varying AffineSpace3f operator+(const varying AffineSpace3f a, const varying AffineSpace3f b) { return make_AffineSpace3f(a.l+b.l, a.p+b.p); }

inline uniform AffineSpace3f operator-(const uniform AffineSpace3f a, const uniform AffineSpace3f b) { return make_AffineSpace3f(a.l-b.l, a.p-b.p); }
inline varying AffineSpace3f operator-(const varying AffineSpace3f a, const varying AffineSpace3f b) { return make_AffineSpace3f(a.l-b.l, a.p-b.p); }

inline uniform AffineSpace3f operator*( const uniform float         a, const uniform AffineSpace3f b ) { return make_AffineSpace3f(a*b.l,a*b.p); }
inline uniform AffineSpace3f operator*( const uniform AffineSpace3f a, const uniform float         b ) { return make_AffineSpace3f(a.l*b,a.p*b); }
inline uniform AffineSpace3f operator*( const uniform AffineSpace3f a, const uniform AffineSpace3f b ) { return make_AffineSpace3f(a.l*b.l,a.l*b.p+a.p); }

inline varying AffineSpace3f operator*( const varying float         a, const varying AffineSpace3f b ) { return make_AffineSpace3f(a*b.l,a*b.p); }
inline varying AffineSpace3f operator*( const varying AffineSpace3f a, const varying float         b ) { return make_AffineSpace3f(a.l*b,a.p*b); }
inline varying AffineSpace3f operator*( const varying AffineSpace3f a, const varying AffineSpace3f b ) { return make_AffineSpace3f(a.l*b.l,a.l*b.p + a.p); }

inline varying AffineSpace3f operator*( const varying float         a, const uniform AffineSpace3f b ) { return make_AffineSpace3f(a*b.l,a*b.p); }
inline varying AffineSpace3f operator*( const uniform AffineSpace3f a, const varying float         b ) { return make_AffineSpace3f(a.l*b,a.p*b); }

inline uniform Vec3f xfmPoint (const uniform AffineSpace3f a, const uniform Vec3f v) { return xfmVector(a.l,v)+a.p; }
inline varying Vec3f xfmPoint (const uniform AffineSpace3f a, const varying Vec3f v) { return xfmVector(a.l,v)+a.p; }
inline varying Vec3f xfmPoint (const varying AffineSpace3f a, const varying Vec3f v) { return xfmVector(a.l,v)+a.p; }

inline uniform Vec3f xfmVector(const uniform AffineSpace3f a, const uniform Vec3f v) { return xfmVector(a.l,v); }
inline varying Vec3f xfmVector(const uniform AffineSpace3f a, const varying Vec3f v) { return xfmVector(a.l,v); }
inline varying Vec3f xfmVector(const varying AffineSpace3f a, const varying Vec3f v) { return xfmVector(a.l,v); }

inline uniform Vec3f xfmNormal(const uniform AffineSpace3f a, const uniform Vec3f v) { return xfmNormal(a.l,v); }
inline varying Vec3f xfmNormal(const uniform AffineSpace3f a, const varying Vec3f v) { return xfmNormal(a.l,v); }
inline varying Vec3f xfmNormal(const varying AffineSpace3f a, const varying Vec3f v) { return xfmNormal(a.l,v); }

////////////////////////////////////////////////////////////////////////////////
/// Comparison Operators
////////////////////////////////////////////////////////////////////////////////

inline uniform bool eq(const uniform AffineSpace3f a, const uniform AffineSpace3f b) { return eq(a.l,b.l) & eq(a.p,b.p); }
inline varying bool eq(const varying AffineSpace3f a, const varying AffineSpace3f b) { return eq(a.l,b.l) & eq(a.p,b.p); }

inline uniform bool ne(const uniform AffineSpace3f a, const uniform AffineSpace3f b) { return ne(a.l,b.l) | ne(a.p,b.p); }
inline varying bool ne(const varying AffineSpace3f a, const varying AffineSpace3f b) { return ne(a.l,b.l) | ne(a.p,b.p); }

////////////////////////////////////////////////////////////////////////////////
// Interpolation
////////////////////////////////////////////////////////////////////////////////

inline uniform AffineSpace3f lerp(uniform float factor, const uniform AffineSpace3f& a, const uniform AffineSpace3f& b) {
  return make_AffineSpace3f(lerp(factor, a.l, b.l), lerp(factor, a.p, b.p));
}

inline varying AffineSpace3f lerp(varying float factor, const uniform AffineSpace3f& a, const uniform AffineSpace3f& b) {
  return make_AffineSpace3f(lerp(factor, a.l, b.l), lerp(factor, a.p, b.p));
}

/*! return scale matrix */
inline uniform AffineSpace3f make_AffineSpace3f_scale(const uniform Vec3f& s) {
  return make_AffineSpace3f(make_LinearSpace3f_scale(s));
}

/*! return translation matrix */
inline uniform AffineSpace3f make_AffineSpace3f_translate(const uniform Vec3f& p) { 
  return make_AffineSpace3f(make_Vec3f(1,0,0),make_Vec3f(0,1,0),make_Vec3f(0,0,1),p); 
}

/*! return translation matrix */
inline AffineSpace3f make_AffineSpace3f_translate(const Vec3f& p) {
  return make_AffineSpace3f(make_Vec3f(1,0,0),make_Vec3f(0,1,0),make_Vec3f(0,0,1),p);
}

/*! return matrix for rotation around arbitrary axis and point */
inline uniform AffineSpace3f make_AffineSpace3f_rotate   (const uniform Vec3f& p, const uniform Vec3f& u, const uniform float& r) { 
  return (make_AffineSpace3f_translate(+p) * make_AffineSpace3f(make_LinearSpace3f_rotate(u,r))) * make_AffineSpace3f_translate(neg(p));  
}
