<commit_before>// ======================================================================== //
// 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 "../../common/ray.h"
#include "filter.h"

namespace embree
{
  namespace isa
  {
    static __forceinline std::pair<float,float> intersect_half_plane(const Vec3fa& ray_org, const Vec3fa& ray_dir, const Vec3fa& N, const Vec3fa& P)
    {
      Vec3fa O = Vec3fa(ray_org) - P;
      Vec3fa D = Vec3fa(ray_dir);
      float ON = dot(O,N);
      float DN = dot(D,N);
      float t = -ON*rcp(abs(DN) < min_rcp_input ? min_rcp_input : DN );
      float lower = select(DN < 0.0f, float(neg_inf), t);
      float upper = select(DN < 0.0f, t, float(pos_inf));
      return std::make_pair(lower,upper);
    }

    static __forceinline bool intersect_cone(const Vec3fa& org_i, const Vec3fa& dir, 
                                             const Vec3fa& v0_i, const float r0, 
                                             const Vec3fa& v1_i, const float r1,
                                             float& t0_o, //float& u0_o, Vec3fa& Ng0_o,
                                             float& t1_o)
      
    {
      const float tb = dot(0.5f*(v0_i+v1_i)-org_i,normalize(dir));
      const Vec3fa org = org_i+tb*dir;
      const Vec3fa v0 = v0_i-org;
      const Vec3fa v1 = v1_i-org;
      
      const float rl = rcp_length(v1-v0);
      const Vec3fa P0 = v0, dP = (v1-v0)*rl;
      const float dr = (r1-r0)*rl;
      const Vec3fa O = -P0, dO = dir;
      
      const float dOdO = dot(dO,dO);
      const float OdO = dot(dO,O);
      const float OO = dot(O,O);
      const float dOz = dot(dP,dO);
      const float Oz = dot(dP,O);
      
      const float R = r0 + Oz*dr;          
      const float A = dOdO - sqr(dOz) * (1.0f+sqr(dr));
      const float B = 2.0f * (OdO - dOz*(Oz + R*dr));
      const float C = OO - (sqr(Oz) + sqr(R));
      
      const float D = B*B - 4.0f*A*C;
      if (D < 0.0f) return false;
      
      const float Q = sqrt(D);
      const float rcp_2A = rcp(2.0f*A);
      t0_o = (-B-Q)*rcp_2A;
      t1_o = (-B+Q)*rcp_2A;
      
      //u0_o = (Oz+t0_o*dOz)*rl;
      //const Vec3fa Pr = t0_o*dir;
      //const Vec3fa Pl = v0 + u0_o*(v1-v0);
      //Ng0_o = Pr-Pl;
      t0_o += tb;
      t1_o += tb;
      return true;
    }

    static __forceinline bool intersect_cone(const Vec3fa& dir, 
                                             const Vec3fa& v0, const float r0, 
                                             const Vec3fa& v1, const float r1,
                                             float& t0_o,
                                             float& t1_o,
                                             Vec3fa& Ng)
      
    {
      const float rl = rcp_length(v1-v0);
      const Vec3fa P0 = v0, dP = (v1-v0)*rl;
      const float dr = (r1-r0)*rl;
      const Vec3fa O = -P0, dO = dir;
      
      const float dOdO = dot(dO,dO);
      const float OdO = dot(dO,O);
      const float OO = dot(O,O);
      const float dOz = dot(dP,dO);
      const float Oz = dot(dP,O);
      
      const float R = r0 + Oz*dr;          
      const float A = dOdO - sqr(dOz) * (1.0f+sqr(dr));
      const float B = 2.0f * (OdO - dOz*(Oz + R*dr));
      const float C = OO - (sqr(Oz) + sqr(R));
      
      const float D = B*B - 4.0f*A*C;
      if (D < 0.0f) return false;
      
      const float Q = sqrt(D);
      if (unlikely(A < min_rcp_input)) {
        t0_o = float(neg_inf);
        t1_o = float(pos_inf);
        return true;
      }

      const float rcp_2A = 0.5f*rcp(A);
      t0_o = (-B-Q)*rcp_2A;
      t1_o = (-B+Q)*rcp_2A;
      
      float u0_o = (Oz+t0_o*dOz)*rl;
      const Vec3fa Pr = t0_o*dir;
      const Vec3fa Pl = v0 + u0_o*(v1-v0);
      Ng = Pr-Pl;
      //t0_o += tb;
      //t1_o += tb;
      return true;
    }

    static __noinline bool intersect_fill_cone(const Ray& ray,
                                               const Vec3fa& p0_i, const Vec3fa& n0, const float r0,
                                               const Vec3fa& p1_i, const Vec3fa& n1, const float r1,
                                               float& u, float& t, Vec3fa& Ng)
    {
      STAT(Stat::get().user[0]++); 

      /* move closer to geometry to make intersection stable */
      //const Vec3fa C = Vec3fa(-10.5103f, 24.6401f, -4.18285f);
      const Vec3fa C = 0.5f*(p0_i+p1_i);
      const float tb = dot(C-ray.org,normalize(ray.dir));
      const Vec3fa org = ray.org+tb*normalize(ray.dir);
      const Vec3fa dir = ray.dir;
      const Vec3fa p0 = p0_i-org;
      const Vec3fa p1 = p1_i-org;

      float maxR = max(r0,r1);
      float t_term = 0.0001f*maxR;
      float t_err = 0.0001f*maxR;
      const float r01 = maxR+t_err;
      float tc_lower,tc_upper;
      Vec3fa NgA;
      if (!intersect_cone(dir,p0,r01,p1,r01,tc_lower,tc_upper,NgA)) {
        STAT(Stat::get().user[1]++); 
        return false;
      }
      //PRINT(tc_lower);
      //PRINT(tc_upper);

      auto tp0 = intersect_half_plane(zero,dir,+n0,p0);
      auto tp1 = intersect_half_plane(zero,dir,-n1,p1);
      //PRINT2(tp0.first,tp0.second);
      //PRINT2(tp1.first,tp1.second);
      
      float td_lower = max(tc_lower,tp0.first ,tp1.first );
      float td_upper = min(tc_upper,tp0.second,tp1.second);
      //PRINT(td_lower);
      //PRINT(td_upper);
      if (td_lower > td_upper) {
        STAT(Stat::get().user[2]++); 
        return false;
      }
      
      tc_lower = max(tp0.first ,tp1.first );
      tc_upper = min(tc_upper+0.1f*maxR,tp0.second,tp1.second);

#if 0
      if (tc_lower > ray.tnear-tb && tc_lower < ray.tfar-tb) 
      {
        u = 0.0f;
        t = tb+tc_lower;
        Ng = NgA;
        return true;
      }
#endif

      const Vec3fa p1p0 = p1-p0;
      const Vec3fa norm_p1p0 = normalize(p1p0);

      
      float A0 = abs(dot(norm_p1p0,normalize(n0)));
      float A1 = abs(dot(norm_p1p0,normalize(n1)));
      float rcpMaxDerivative = max(0.01f,min(A0,A1));
      
      STAT(Stat::get().user[3]++);
      t = td_lower; float dt = inf;
      Vec3fa p = t*dir;
      //PRINT(td_lower);
      
      for (size_t i=0;; i++) 
      {
        //__asm("nop2");
        //PRINT(i);
        STAT(Stat::get().user[4]++); 
        if (unlikely(i == 2000)) {
          STAT(Stat::get().user[5]++); 
          //PRINT("miss2");<commit_msg>bugfix for not normalized ray direction<commit_after>// ======================================================================== //
// 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 "../../common/ray.h"
#include "filter.h"

namespace embree
{
  namespace isa
  {
    static __forceinline std::pair<float,float> intersect_half_plane(const Vec3fa& ray_org, const Vec3fa& ray_dir, const Vec3fa& N, const Vec3fa& P)
    {
      Vec3fa O = Vec3fa(ray_org) - P;
      Vec3fa D = Vec3fa(ray_dir);
      float ON = dot(O,N);
      float DN = dot(D,N);
      float t = -ON*rcp(abs(DN) < min_rcp_input ? min_rcp_input : DN );
      float lower = select(DN < 0.0f, float(neg_inf), t);
      float upper = select(DN < 0.0f, t, float(pos_inf));
      return std::make_pair(lower,upper);
    }

    static __forceinline bool intersect_cone(const Vec3fa& org_i, const Vec3fa& dir, 
                                             const Vec3fa& v0_i, const float r0, 
                                             const Vec3fa& v1_i, const float r1,
                                             float& t0_o, //float& u0_o, Vec3fa& Ng0_o,
                                             float& t1_o)
      
    {
      const float tb = dot(0.5f*(v0_i+v1_i)-org_i,normalize(dir));
      const Vec3fa org = org_i+tb*dir;
      const Vec3fa v0 = v0_i-org;
      const Vec3fa v1 = v1_i-org;
      
      const float rl = rcp_length(v1-v0);
      const Vec3fa P0 = v0, dP = (v1-v0)*rl;
      const float dr = (r1-r0)*rl;
      const Vec3fa O = -P0, dO = dir;
      
      const float dOdO = dot(dO,dO);
      const float OdO = dot(dO,O);
      const float OO = dot(O,O);
      const float dOz = dot(dP,dO);
      const float Oz = dot(dP,O);
      
      const float R = r0 + Oz*dr;          
      const float A = dOdO - sqr(dOz) * (1.0f+sqr(dr));
      const float B = 2.0f * (OdO - dOz*(Oz + R*dr));
      const float C = OO - (sqr(Oz) + sqr(R));
      
      const float D = B*B - 4.0f*A*C;
      if (D < 0.0f) return false;
      
      const float Q = sqrt(D);
      const float rcp_2A = rcp(2.0f*A);
      t0_o = (-B-Q)*rcp_2A;
      t1_o = (-B+Q)*rcp_2A;
      
      //u0_o = (Oz+t0_o*dOz)*rl;
      //const Vec3fa Pr = t0_o*dir;
      //const Vec3fa Pl = v0 + u0_o*(v1-v0);
      //Ng0_o = Pr-Pl;
      t0_o += tb;
      t1_o += tb;
      return true;
    }

    static __forceinline bool intersect_cone(const Vec3fa& dir, 
                                             const Vec3fa& v0, const float r0, 
                                             const Vec3fa& v1, const float r1,
                                             float& t0_o,
                                             float& t1_o,
                                             Vec3fa& Ng)
      
    {
      const float rl = rcp_length(v1-v0);
      const Vec3fa P0 = v0, dP = (v1-v0)*rl;
      const float dr = (r1-r0)*rl;
      const Vec3fa O = -P0, dO = dir;
      
      const float dOdO = dot(dO,dO);
      const float OdO = dot(dO,O);
      const float OO = dot(O,O);
      const float dOz = dot(dP,dO);
      const float Oz = dot(dP,O);
      
      const float R = r0 + Oz*dr;          
      const float A = dOdO - sqr(dOz) * (1.0f+sqr(dr));
      const float B = 2.0f * (OdO - dOz*(Oz + R*dr));
      const float C = OO - (sqr(Oz) + sqr(R));
      
      const float D = B*B - 4.0f*A*C;
      if (D < 0.0f) return false;
      
      const float Q = sqrt(D);
      if (unlikely(A < min_rcp_input)) {
        t0_o = float(neg_inf);
        t1_o = float(pos_inf);
        return true;
      }

      const float rcp_2A = 0.5f*rcp(A);
      t0_o = (-B-Q)*rcp_2A;
      t1_o = (-B+Q)*rcp_2A;
      
      float u0_o = (Oz+t0_o*dOz)*rl;
      const Vec3fa Pr = t0_o*dir;
      const Vec3fa Pl = v0 + u0_o*(v1-v0);
      Ng = Pr-Pl;
      //t0_o += tb;
      //t1_o += tb;
      return true;
    }

    static __noinline bool intersect_fill_cone(const Ray& ray,
                                               const Vec3fa& p0_i, const Vec3fa& n0, const float r0,
                                               const Vec3fa& p1_i, const Vec3fa& n1, const float r1,
                                               float& u, float& t, Vec3fa& Ng)
    {
      STAT(Stat::get().user[0]++); 

      /* move closer to geometry to make intersection stable */
      //const Vec3fa C = Vec3fa(-10.5103f, 24.6401f, -4.18285f);
      const Vec3fa C = 0.5f*(p0_i+p1_i);
      const float tb = dot(C-ray.org,normalize(ray.dir));
      const Vec3fa org = ray.org+tb*normalize(ray.dir);
      const float dirlen = length(ray.dir);
      const Vec3fa dir = ray.dir;
      const Vec3fa p0 = p0_i-org;
      const Vec3fa p1 = p1_i-org;

      float maxR = max(r0,r1);
      float t_term = 0.0001f*maxR;
      float t_err = 0.0001f*maxR;
      const float r01 = maxR+t_err;
      float tc_lower,tc_upper;
      Vec3fa NgA;
      if (!intersect_cone(dir,p0,r01,p1,r01,tc_lower,tc_upper,NgA)) {
        STAT(Stat::get().user[1]++); 
        return false;
      }
      //PRINT(tc_lower);
      //PRINT(tc_upper);

      auto tp0 = intersect_half_plane(zero,dir,+n0,p0);
      auto tp1 = intersect_half_plane(zero,dir,-n1,p1);
      //PRINT2(tp0.first,tp0.second);
      //PRINT2(tp1.first,tp1.second);
      
      float td_lower = max(tc_lower,tp0.first ,tp1.first );
      float td_upper = min(tc_upper,tp0.second,tp1.second);
      //PRINT(td_lower);
      //PRINT(td_upper);
      if (td_lower > td_upper) {
        STAT(Stat::get().user[2]++); 
        return false;
      }
      
      tc_lower = max(tp0.first ,tp1.first );
      tc_upper = min(tc_upper+0.1f*maxR,tp0.second,tp1.second);

#if 0
      if (tc_lower > ray.tnear-tb && tc_lower < ray.tfar-tb) 
      {
        u = 0.0f;
        t = tb+tc_lower;
        Ng = NgA;
        return true;
      }
#endif

      const Vec3fa p1p0 = p1-p0;
      const Vec3fa norm_p1p0 = normalize(p1p0);

      
      float A0 = abs(dot(norm_p1p0,normalize(n0)));
      float A1 = abs(dot(norm_p1p0,normalize(n1)));
      float rcpMaxDerivative = max(0.01f,min(A0,A1))/dirlen;
      
      STAT(Stat::get().user[3]++);
      t = td_lower; float dt = inf;
      Vec3fa p = t*dir;
      //PRINT(td_lower);
      
      for (size_t i=0;; i++) 
      {
        //__asm("nop2");
        //PRINT(i);
        STAT(Stat::get().user[4]++); 
        if (unlikely(i == 2000)) {
          STAT(Stat::get().user[5]++); 
          //PRINT("miss2");
{'fraction_non_alphanumeric': 0.14601087029457158, 'fraction_numerical': 0.03542910823324163, 'mean_word_length': 1.5120512388336422, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 74, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544425', 'n_tokens_mistral': 5338, 'n_tokens_neox': 5124, 'n_words': 1220}