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

#include "Camera.ih"
#include "common/Embree.h"
#include "common/FeatureFlags.ih"
#include "math/sampling.ih"
#include "ospray/OSPEnums.h"
// c++ shared
#include "PanoramicCameraShared.h"

#include "rkcommon/math/AffineSpace.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

SYCL_EXTERNAL void PanoramicCamera_initRay(const Camera *uniform _self,
    varying Ray &ray,
    varying RayCone &rayCone,
    const varying CameraSample &sample,
    const uniform FeatureFlagsHandler &ffh)
{
  const PanoramicCamera *uniform self = (const PanoramicCamera *uniform)_self;

  vec2f screen = sample.screen;
  float pixel_center = self->stereoMode == OSP_STEREO_SIDE_BY_SIDE
      ? sample.pixel_center.x
      : sample.pixel_center.y;

  varying float *uniform split =
      self->stereoMode == OSP_STEREO_SIDE_BY_SIDE ? &screen.x : &screen.y;
  float offset = 0.f;
  switch (self->stereoMode) {
  case OSP_STEREO_LEFT:
    offset = -self->ipd_offset;
    break;
  case OSP_STEREO_RIGHT:
    offset = self->ipd_offset;
    break;
  case OSP_STEREO_SIDE_BY_SIDE:
  case OSP_STEREO_TOP_BOTTOM:
    *split *= 2.f;
    if (pixel_center < 0.5f) {
      offset = -self->ipd_offset;
    } else {
      offset = self->ipd_offset;
      *split -= 1.f;
    }
    break;
  }

  screen = Camera_subRegion(_self, screen);

  const float phi = (float)two_pi * screen.x;
  const float theta = (float)pi * screen.y;

  float sinTheta, cosTheta;
  sincos(theta, &sinTheta, &cosTheta);
  const vec3f localDir = cartesian(phi, sinTheta, cosTheta);

  const float time = Camera_shutterTime(_self, screen, sample.time);

  // transform to camera- and then to world-space
  vec3f dir;
  vec3f org;
  const uniform FeatureFlagsOther ffo = getFeatureFlagsOther(ffh);
  if (self->super.motionBlur && (ffo & FFO_CAMERA_MOTION_BLUR)) {
    AffineSpace3f xfm;
    rtcGetGeometryTransformFromScene(
        self->super.scene, 0, time, RTC_FORMAT_FLOAT3X4_COLUMN_MAJOR, &xfm);

    // we cannot just transform the final org & dir, because interpolated
    // transforms can scale (even if original transforms are without scale)
    linear3f frameMB;
    frameMB.vz = normalize(xfmVector(xfm, self->frame.vz));
    frameMB.vx = normalize(cross(xfmVector(xfm, self->frame.vy), frameMB.vz));
    frameMB.vy = cross(frameMB.vz, frameMB.vx);

    dir = frameMB * make_vec3f(-localDir.y, -localDir.z, localDir.x);
    org = xfmPoint(xfm, self->org) + offset * cross(dir, frameMB.vy);
  } else {
    dir = self->frame * make_vec3f(-localDir.y, -localDir.z, localDir.x);
    org = self->org + offset * cross(dir, self->frame.vy);
  }

  setRay(ray, org, dir, self->super.nearClip, inf, time);
  rayCone.width = 0.f;
  rayCone.dwdt = pi * sinTheta;

  const uniform vec2f subImageSize = box_size(self->super.subImage);
  rayCone.dwdt *= abs(subImageSize.y);

  if (self->stereoMode == OSP_STEREO_TOP_BOTTOM)
    rayCone.dwdt *= 2.f;
}

// Exports (called from C++) //////////////////////////////////////////////////

export void *uniform PanoramicCamera_initRay_addr()
{
  return (void *uniform)PanoramicCamera_initRay;
}

OSPRAY_END_ISPC_NAMESPACE
