Path: blob/master/thirdparty/embree/kernels/geometry/cone.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "../common/ray.h"67namespace embree8{9namespace isa10{11struct Cone12{13const Vec3fa p0; //!< start position of cone14const Vec3fa p1; //!< end position of cone15const float r0; //!< start radius of cone16const float r1; //!< end radius of cone1718__forceinline Cone(const Vec3fa& p0, const float r0, const Vec3fa& p1, const float r1)19: p0(p0), p1(p1), r0(r0), r1(r1) {}2021__forceinline bool intersect(const Vec3fa& org, const Vec3fa& dir,22BBox1f& t_o,23float& u0_o, Vec3fa& Ng0_o,24float& u1_o, Vec3fa& Ng1_o) const25{26/* calculate quadratic equation to solve */27const Vec3fa v0 = p0-org;28const Vec3fa v1 = p1-org;2930const float rl = rcp_length(v1-v0);31const Vec3fa P0 = v0, dP = (v1-v0)*rl;32const float dr = (r1-r0)*rl;33const Vec3fa O = -P0, dO = dir;3435const float dOdO = dot(dO,dO);36const float OdO = dot(dO,O);37const float OO = dot(O,O);38const float dOz = dot(dP,dO);39const float Oz = dot(dP,O);4041const float R = r0 + Oz*dr;42const float A = dOdO - sqr(dOz) * (1.0f+sqr(dr));43const float B = 2.0f * (OdO - dOz*(Oz + R*dr));44const float C = OO - (sqr(Oz) + sqr(R));4546/* we miss the cone if determinant is smaller than zero */47const float D = B*B - 4.0f*A*C;48if (D < 0.0f) return false;4950/* special case for rays that are "parallel" to the cone */51const float eps = float(1<<8)*float(ulp)*max(abs(dOdO),abs(sqr(dOz)));52if (unlikely(abs(A) < eps))53{54/* cylinder case */55if (abs(dr) < 16.0f*float(ulp)) {56if (C <= 0.0f) { t_o = BBox1f(neg_inf,pos_inf); return true; }57else { t_o = BBox1f(pos_inf,neg_inf); return false; }58}5960/* cone case */61else62{63/* if we hit the negative cone there cannot be a hit */64const float t = -C/B;65const float z0 = Oz+t*dOz;66const float z0r = r0+z0*dr;67if (z0r < 0.0f) return false;6869/* test if we start inside or outside the cone */70if (dOz*dr > 0.0f) t_o = BBox1f(t,pos_inf);71else t_o = BBox1f(neg_inf,t);72}73}7475/* standard case for "non-parallel" rays */76else77{78const float Q = sqrt(D);79const float rcp_2A = rcp(2.0f*A);80t_o.lower = (-B-Q)*rcp_2A;81t_o.upper = (-B+Q)*rcp_2A;8283/* standard case where both hits are on same cone */84if (likely(A > 0.0f)) {85const float z0 = Oz+t_o.lower*dOz;86const float z0r = r0+z0*dr;87if (z0r < 0.0f) return false;88}8990/* special case where the hits are on the positive and negative cone */91else92{93/* depending on the ray direction and the open direction94* of the cone we have a hit from inside or outside the95* cone */96if (dOz*dr > 0) t_o.upper = pos_inf;97else t_o.lower = neg_inf;98}99}100101/* calculates u and Ng for near hit */102{103u0_o = (Oz+t_o.lower*dOz)*rl;104const Vec3fa Pr = t_o.lower*dir;105const Vec3fa Pl = v0 + u0_o*(v1-v0);106const Vec3fa R = normalize(Pr-Pl);107const Vec3fa U = (p1-p0)+(r1-r0)*R;108const Vec3fa V = cross(p1-p0,R);109Ng0_o = cross(V,U);110}111112/* calculates u and Ng for far hit */113{114u1_o = (Oz+t_o.upper*dOz)*rl;115const Vec3fa Pr = t_o.upper*dir;116const Vec3fa Pl = v0 + u1_o*(v1-v0);117const Vec3fa R = normalize(Pr-Pl);118const Vec3fa U = (p1-p0)+(r1-r0)*R;119const Vec3fa V = cross(p1-p0,R);120Ng1_o = cross(V,U);121}122return true;123}124125__forceinline bool intersect(const Vec3fa& org, const Vec3fa& dir, BBox1f& t_o) const126{127float u0_o; Vec3fa Ng0_o; float u1_o; Vec3fa Ng1_o;128return intersect(org,dir,t_o,u0_o,Ng0_o,u1_o,Ng1_o);129}130131static bool verify(const size_t id, const Cone& cone, const Ray& ray, bool shouldhit, const float t0, const float t1)132{133float eps = 0.001f;134BBox1f t; bool hit;135hit = cone.intersect(ray.org,ray.dir,t);136137bool failed = hit != shouldhit;138if (shouldhit) failed |= std::isinf(t0) ? t0 != t.lower : (t0 == -1E6) ? t.lower > -1E6f : abs(t0-t.lower) > eps;139if (shouldhit) failed |= std::isinf(t1) ? t1 != t.upper : (t1 == +1E6) ? t.upper < +1E6f : abs(t1-t.upper) > eps;140if (!failed) return true;141embree_cout << "Cone test " << id << " failed: cone = " << cone << ", ray = " << ray << ", hit = " << hit << ", t = " << t << embree_endl;142return false;143}144145/* verify cone class */146static bool verify()147{148bool passed = true;149const Cone cone0(Vec3fa(0.0f,0.0f,0.0f),0.0f,Vec3fa(1.0f,0.0f,0.0f),1.0f);150passed &= verify(0,cone0,Ray(Vec3fa(-2.0f,1.0f,0.0f),Vec3fa(+1.0f,+0.0f,+0.0f),0.0f,float(inf)),true,3.0f,pos_inf);151passed &= verify(1,cone0,Ray(Vec3fa(+2.0f,1.0f,0.0f),Vec3fa(-1.0f,+0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,1.0f);152passed &= verify(2,cone0,Ray(Vec3fa(-1.0f,0.0f,2.0f),Vec3fa(+0.0f,+0.0f,-1.0f),0.0f,float(inf)),false,0.0f,0.0f);153passed &= verify(3,cone0,Ray(Vec3fa(+1.0f,0.0f,2.0f),Vec3fa(+0.0f,+0.0f,-1.0f),0.0f,float(inf)),true,1.0f,3.0f);154passed &= verify(4,cone0,Ray(Vec3fa(-1.0f,0.0f,0.0f),Vec3fa(+1.0f,+0.0f,+0.0f),0.0f,float(inf)),true,1.0f,pos_inf);155passed &= verify(5,cone0,Ray(Vec3fa(+1.0f,0.0f,0.0f),Vec3fa(-1.0f,+0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,1.0f);156passed &= verify(6,cone0,Ray(Vec3fa(+0.0f,0.0f,1.0f),Vec3fa(+0.0f,+0.0f,-1.0f),0.0f,float(inf)),true,1.0f,1.0f);157passed &= verify(7,cone0,Ray(Vec3fa(+0.0f,1.0f,0.0f),Vec3fa(-1.0f,-1.0f,+0.0f),0.0f,float(inf)),false,0.0f,0.0f);158passed &= verify(8,cone0,Ray(Vec3fa(+0.0f,1.0f,0.0f),Vec3fa(+1.0f,-1.0f,+0.0f),0.0f,float(inf)),true,0.5f,+1E6);159passed &= verify(9,cone0,Ray(Vec3fa(+0.0f,1.0f,0.0f),Vec3fa(-1.0f,+1.0f,+0.0f),0.0f,float(inf)),true,-1E6,-0.5f);160const Cone cone1(Vec3fa(0.0f,0.0f,0.0f),1.0f,Vec3fa(1.0f,0.0f,0.0f),0.0f);161passed &= verify(10,cone1,Ray(Vec3fa(-2.0f,1.0f,0.0f),Vec3fa(+1.0f,+0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,2.0f);162passed &= verify(11,cone1,Ray(Vec3fa(-1.0f,0.0f,2.0f),Vec3fa(+0.0f,+0.0f,-1.0f),0.0f,float(inf)),true,0.0f,4.0f);163const Cone cylinder(Vec3fa(0.0f,0.0f,0.0f),1.0f,Vec3fa(1.0f,0.0f,0.0f),1.0f);164passed &= verify(12,cylinder,Ray(Vec3fa(-2.0f,1.0f,0.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),true,0.0f,2.0f);165passed &= verify(13,cylinder,Ray(Vec3fa(+2.0f,1.0f,0.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),true,0.0f,2.0f);166passed &= verify(14,cylinder,Ray(Vec3fa(+2.0f,1.0f,2.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),false,0.0f,0.0f);167passed &= verify(15,cylinder,Ray(Vec3fa(+0.0f,0.0f,0.0f),Vec3fa( 1.0f, 0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,pos_inf);168passed &= verify(16,cylinder,Ray(Vec3fa(+0.0f,0.0f,0.0f),Vec3fa(-1.0f, 0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,pos_inf);169passed &= verify(17,cylinder,Ray(Vec3fa(+0.0f,2.0f,0.0f),Vec3fa( 1.0f, 0.0f,+0.0f),0.0f,float(inf)),false,pos_inf,neg_inf);170passed &= verify(18,cylinder,Ray(Vec3fa(+0.0f,2.0f,0.0f),Vec3fa(-1.0f, 0.0f,+0.0f),0.0f,float(inf)),false,pos_inf,neg_inf);171return passed;172}173174/*! output operator */175friend __forceinline embree_ostream operator<<(embree_ostream cout, const Cone& c) {176return cout << "Cone { p0 = " << c.p0 << ", r0 = " << c.r0 << ", p1 = " << c.p1 << ", r1 = " << c.r1 << "}";177}178};179180template<int N>181struct ConeN182{183typedef Vec3<vfloat<N>> Vec3vfN;184185const Vec3vfN p0; //!< start position of cone186const Vec3vfN p1; //!< end position of cone187const vfloat<N> r0; //!< start radius of cone188const vfloat<N> r1; //!< end radius of cone189190__forceinline ConeN(const Vec3vfN& p0, const vfloat<N>& r0, const Vec3vfN& p1, const vfloat<N>& r1)191: p0(p0), p1(p1), r0(r0), r1(r1) {}192193__forceinline Cone operator[] (const size_t i) const194{195assert(i<N);196return Cone(Vec3fa(p0.x[i],p0.y[i],p0.z[i]),r0[i],Vec3fa(p1.x[i],p1.y[i],p1.z[i]),r1[i]);197}198199__forceinline vbool<N> intersect(const Vec3fa& org, const Vec3fa& dir,200BBox<vfloat<N>>& t_o,201vfloat<N>& u0_o, Vec3vfN& Ng0_o,202vfloat<N>& u1_o, Vec3vfN& Ng1_o) const203{204/* calculate quadratic equation to solve */205const Vec3vfN v0 = p0-Vec3vfN(org);206const Vec3vfN v1 = p1-Vec3vfN(org);207208const vfloat<N> rl = rcp_length(v1-v0);209const Vec3vfN P0 = v0, dP = (v1-v0)*rl;210const vfloat<N> dr = (r1-r0)*rl;211const Vec3vfN O = -P0, dO = dir;212213const vfloat<N> dOdO = dot(dO,dO);214const vfloat<N> OdO = dot(dO,O);215const vfloat<N> OO = dot(O,O);216const vfloat<N> dOz = dot(dP,dO);217const vfloat<N> Oz = dot(dP,O);218219const vfloat<N> R = r0 + Oz*dr;220const vfloat<N> A = dOdO - sqr(dOz) * (vfloat<N>(1.0f)+sqr(dr));221const vfloat<N> B = 2.0f * (OdO - dOz*(Oz + R*dr));222const vfloat<N> C = OO - (sqr(Oz) + sqr(R));223224/* we miss the cone if determinant is smaller than zero */225const vfloat<N> D = B*B - 4.0f*A*C;226vbool<N> valid = D >= 0.0f;227if (none(valid)) return valid;228229/* special case for rays that are "parallel" to the cone */230const vfloat<N> eps = float(1<<8)*float(ulp)*max(abs(dOdO),abs(sqr(dOz)));231const vbool<N> validt = valid & (abs(A) < eps);232const vbool<N> validf = valid & !(abs(A) < eps);233if (unlikely(any(validt)))234{235const vboolx validtt = validt & (abs(dr) < 16.0f*float(ulp));236const vboolx validtf = validt & (abs(dr) >= 16.0f*float(ulp));237238/* cylinder case */239if (unlikely(any(validtt)))240{241t_o.lower = select(validtt, select(C <= 0.0f, vfloat<N>(neg_inf), vfloat<N>(pos_inf)), t_o.lower);242t_o.upper = select(validtt, select(C <= 0.0f, vfloat<N>(pos_inf), vfloat<N>(neg_inf)), t_o.upper);243valid &= !validtt | C <= 0.0f;244}245246/* cone case */247if (any(validtf))248{249/* if we hit the negative cone there cannot be a hit */250const vfloat<N> t = -C/B;251const vfloat<N> z0 = Oz+t*dOz;252const vfloat<N> z0r = r0+z0*dr;253valid &= !validtf | z0r >= 0.0f;254255/* test if we start inside or outside the cone */256t_o.lower = select(validtf, select(dOz*dr > 0.0f, t, vfloat<N>(neg_inf)), t_o.lower);257t_o.upper = select(validtf, select(dOz*dr > 0.0f, vfloat<N>(pos_inf), t), t_o.upper);258}259}260261/* standard case for "non-parallel" rays */262if (likely(any(validf)))263{264const vfloat<N> Q = sqrt(D);265const vfloat<N> rcp_2A = 0.5f*rcp(A);266t_o.lower = select(validf, (-B-Q)*rcp_2A, t_o.lower);267t_o.upper = select(validf, (-B+Q)*rcp_2A, t_o.upper);268269/* standard case where both hits are on same cone */270const vbool<N> validft = validf & A>0.0f;271const vbool<N> validff = validf & !(A>0.0f);272if (any(validft)) {273const vfloat<N> z0 = Oz+t_o.lower*dOz;274const vfloat<N> z0r = r0+z0*dr;275valid &= !validft | z0r >= 0.0f;276}277278/* special case where the hits are on the positive and negative cone */279if (any(validff)) {280/* depending on the ray direction and the open direction281* of the cone we have a hit from inside or outside the282* cone */283t_o.lower = select(validff, select(dOz*dr > 0.0f, t_o.lower, float(neg_inf)), t_o.lower);284t_o.upper = select(validff, select(dOz*dr > 0.0f, float(pos_inf), t_o.upper), t_o.upper);285}286}287288/* calculates u and Ng for near hit */289{290u0_o = (Oz+t_o.lower*dOz)*rl;291const Vec3vfN Pr = t_o.lower*Vec3vfN(dir);292const Vec3vfN Pl = v0 + u0_o*(v1-v0);293const Vec3vfN R = normalize(Pr-Pl);294const Vec3vfN U = (p1-p0)+(r1-r0)*R;295const Vec3vfN V = cross(p1-p0,R);296Ng0_o = cross(V,U);297}298299/* calculates u and Ng for far hit */300{301u1_o = (Oz+t_o.upper*dOz)*rl;302const Vec3vfN Pr = t_o.lower*Vec3vfN(dir);303const Vec3vfN Pl = v0 + u1_o*(v1-v0);304const Vec3vfN R = normalize(Pr-Pl);305const Vec3vfN U = (p1-p0)+(r1-r0)*R;306const Vec3vfN V = cross(p1-p0,R);307Ng1_o = cross(V,U);308}309return valid;310}311312__forceinline vbool<N> intersect(const Vec3fa& org, const Vec3fa& dir, BBox<vfloat<N>>& t_o) const313{314vfloat<N> u0_o; Vec3vfN Ng0_o; vfloat<N> u1_o; Vec3vfN Ng1_o;315return intersect(org,dir,t_o,u0_o,Ng0_o,u1_o,Ng1_o);316}317};318}319}320321322323