// ======================================================================== //
// Copyright 2009-2015 Intel Corporation                                    //
//                                                                          //
// Licensed under the Apache License, Version 2.0 (the "License");          //
// you may not use this file except in compliance with the License.         //
// You may obtain a copy of the License at                                  //
//                                                                          //
//     http://www.apache.org/licenses/LICENSE-2.0                           //
//                                                                          //
// Unless required by applicable law or agreed to in writing, software      //
// distributed under the License is distributed on an "AS IS" BASIS,        //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and      //
// limitations under the License.                                           //
// ======================================================================== //

#pragma once

#include "../math/vec.isph"

struct RTCRay1
{
  uniform Vec3f org;     //!< Ray origin
  uniform float align0;
  uniform Vec3f dir;     //!< Ray direction
  uniform float align1;
  uniform float tnear;   //!< Start of ray segment
  uniform float tfar;    //!< End of ray segment
  uniform float time;    //!< Time of this ray for motion blur.
  uniform int mask;      //!< used to mask out objects during traversal
  uniform Vec3f Ng;      //!< Geometric normal.
  uniform float align2;
  uniform float u;       //!< Barycentric u coordinate of hit
  uniform float v;       //!< Barycentric v coordinate of hit
  uniform int geomID;    //!< geometry ID
  uniform int primID;    //!< primitive ID
  uniform int instID;    //!< instance ID
  varying int align[0];  //!< aligns ray on stack to at least 16 bytes
};

/*! Ray structure. Contains all information about a ray including
 *  precomputed reciprocal direction. */
struct RTCRay
{
  Vec3f org;     //!< Ray origin
  Vec3f dir;     //!< Ray direction
  float tnear;   //!< Start of ray segment
  float tfar;    //!< End of ray segment
  float time;    //!< Time of this ray for motion blur.
  int mask;      //!< used to mask out objects during traversal
  Vec3f Ng;      //!< Geometric normal.
  float u;       //!< Barycentric u coordinate of hit
  float v;       //!< Barycentric v coordinate of hit
  int geomID;    //!< geometry ID
  int primID;    //!< primitive ID
  int instID;    //!< instance ID

  // ray extensions
  Vec3f transparency;
};

/*! Constructs a ray from origin, direction, and ray segment. Near
 *  has to be smaller than far. */
inline RTCRay make_Ray(const Vec3f org, 
                       const Vec3f dir, 
                       const float tnear, 
                       const float tfar,
                       const float time = 0.0f)
{
  RTCRay ray;
  ray.org = org;
  ray.dir = dir;
  ray.tnear = tnear;
  ray.tfar = tfar;
  ray.geomID = -1;
  ray.primID = -1;
  ray.mask = -1;
  ray.time = time;
  ray.instID = -1;
  return ray;
}

/*! Constructs a ray from origin, direction, and ray segment. Near
 *  has to be smaller than far. */
inline RTCRay make_Ray(const Vec3f org, 
                       const Vec3f dir)
{
  RTCRay ray;
  ray.org  = org;
  ray.dir  = dir;
  ray.tnear = 0.0f;
  ray.tfar  = inf;
  ray.geomID = -1;
  ray.primID = -1;
  ray.mask = -1;
  ray.time = 0;
  ray.instID = -1;
  return ray;
}

inline void init_Ray(RTCRay &ray,
                     const Vec3f org, 
                     const Vec3f dir, 
                     const float tnear, 
                     const float tfar)
{
  ray.org = org;
  ray.dir = dir;
  ray.tnear = tnear;
  ray.tfar = tfar;
  ray.geomID = -1;
  ray.primID = -1;
  ray.mask = -1;
  ray.time = 0;
  ray.instID = -1;
}

inline void init_Ray(RTCRay &ray,
                     const Vec3f org, 
                     const Vec3f dir)
{
  ray.org = org;
  ray.dir = dir;
  ray.tnear = 0.0f;
  ray.tfar = inf;
  ray.geomID = -1;
  ray.primID = -1;
  ray.mask = -1;
  ray.time = 0;
  ray.instID = -1;
}

inline bool noHit(const RTCRay& r) { return r.geomID < 0; }

inline bool hadHit(const RTCRay& r) { return r.geomID >= 0; }
