Path: blob/master/thirdparty/embree/kernels/subdiv/bezier_curve.h
9913 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "../common/default.h"6//#include "../common/scene_curves.h"7#include "../common/context.h"89namespace embree10{11class BezierBasis12{13public:1415template<typename T>16static __forceinline Vec4<T> eval(const T& u)17{18const T t1 = u;19const T t0 = 1.0f-t1;20const T B0 = t0 * t0 * t0;21const T B1 = 3.0f * t1 * (t0 * t0);22const T B2 = 3.0f * (t1 * t1) * t0;23const T B3 = t1 * t1 * t1;24return Vec4<T>(B0,B1,B2,B3);25}2627template<typename T>28static __forceinline Vec4<T> derivative(const T& u)29{30const T t1 = u;31const T t0 = 1.0f-t1;32const T B0 = -(t0*t0);33const T B1 = madd(-2.0f,t0*t1,t0*t0);34const T B2 = msub(+2.0f,t0*t1,t1*t1);35const T B3 = +(t1*t1);36return T(3.0f)*Vec4<T>(B0,B1,B2,B3);37}3839template<typename T>40static __forceinline Vec4<T> derivative2(const T& u)41{42const T t1 = u;43const T t0 = 1.0f-t1;44const T B0 = t0;45const T B1 = madd(-2.0f,t0,t1);46const T B2 = madd(-2.0f,t1,t0);47const T B3 = t1;48return T(6.0f)*Vec4<T>(B0,B1,B2,B3);49}50};5152struct PrecomputedBezierBasis53{54enum { N = 16 };55public:56PrecomputedBezierBasis() {}57PrecomputedBezierBasis(int shift);5859/* basis for bezier evaluation */60public:61float c0[N+1][N+1];62float c1[N+1][N+1];63float c2[N+1][N+1];64float c3[N+1][N+1];6566/* basis for bezier derivative evaluation */67public:68float d0[N+1][N+1];69float d1[N+1][N+1];70float d2[N+1][N+1];71float d3[N+1][N+1];72};73extern PrecomputedBezierBasis bezier_basis0;74extern PrecomputedBezierBasis bezier_basis1;757677template<typename V>78struct LinearBezierCurve79{80V v0,v1;8182__forceinline LinearBezierCurve () {}8384__forceinline LinearBezierCurve (const LinearBezierCurve& other)85: v0(other.v0), v1(other.v1) {}8687__forceinline LinearBezierCurve& operator= (const LinearBezierCurve& other) {88v0 = other.v0; v1 = other.v1; return *this;89}9091__forceinline LinearBezierCurve (const V& v0, const V& v1)92: v0(v0), v1(v1) {}9394__forceinline V begin() const { return v0; }95__forceinline V end () const { return v1; }9697bool hasRoot() const;9899friend embree_ostream operator<<(embree_ostream cout, const LinearBezierCurve& a) {100return cout << "LinearBezierCurve (" << a.v0 << ", " << a.v1 << ")";101}102};103104template<> __forceinline bool LinearBezierCurve<Interval1f>::hasRoot() const {105return numRoots(v0,v1);106}107108template<typename V>109struct QuadraticBezierCurve110{111V v0,v1,v2;112113__forceinline QuadraticBezierCurve () {}114115__forceinline QuadraticBezierCurve (const QuadraticBezierCurve& other)116: v0(other.v0), v1(other.v1), v2(other.v2) {}117118__forceinline QuadraticBezierCurve& operator= (const QuadraticBezierCurve& other) {119v0 = other.v0; v1 = other.v1; v2 = other.v2; return *this;120}121122__forceinline QuadraticBezierCurve (const V& v0, const V& v1, const V& v2)123: v0(v0), v1(v1), v2(v2) {}124125__forceinline V begin() const { return v0; }126__forceinline V end () const { return v2; }127128__forceinline V interval() const {129return merge(v0,v1,v2);130}131132__forceinline BBox<V> bounds() const {133return merge(BBox<V>(v0),BBox<V>(v1),BBox<V>(v2));134}135136friend embree_ostream operator<<(embree_ostream cout, const QuadraticBezierCurve& a) {137return cout << "QuadraticBezierCurve (" << a.v0 << ", " << a.v1 << ", " << a.v2 << ")";138}139};140141142typedef QuadraticBezierCurve<float> QuadraticBezierCurve1f;143typedef QuadraticBezierCurve<Vec2fa> QuadraticBezierCurve2fa;144typedef QuadraticBezierCurve<Vec3fa> QuadraticBezierCurve3fa;145146template<typename Vertex>147struct CubicBezierCurve148{149Vertex v0,v1,v2,v3;150151__forceinline CubicBezierCurve() {}152153template<typename T1>154__forceinline CubicBezierCurve (const CubicBezierCurve<T1>& other)155: v0(other.v0), v1(other.v1), v2(other.v2), v3(other.v3) {}156157__forceinline CubicBezierCurve& operator= (const CubicBezierCurve& other) {158v0 = other.v0; v1 = other.v1; v2 = other.v2; v3 = other.v3; return *this;159}160161__forceinline CubicBezierCurve(const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)162: v0(v0), v1(v1), v2(v2), v3(v3) {}163164__forceinline Vertex begin() const {165return v0;166}167168__forceinline Vertex end() const {169return v3;170}171172__forceinline Vertex center() const {173return 0.25f*(v0+v1+v2+v3);174}175176__forceinline Vertex begin_direction() const {177return v1-v0;178}179180__forceinline Vertex end_direction() const {181return v3-v2;182}183184__forceinline CubicBezierCurve<float> xfm(const Vertex& dx) const {185return CubicBezierCurve<float>(dot(v0,dx),dot(v1,dx),dot(v2,dx),dot(v3,dx));186}187188template<int W>189__forceinline CubicBezierCurve<vfloat<W>> vxfm(const Vertex& dx) const {190return CubicBezierCurve<vfloat<W>>(dot(v0,dx),dot(v1,dx),dot(v2,dx),dot(v3,dx));191}192193__forceinline CubicBezierCurve<float> xfm(const Vertex& dx, const Vertex& p) const {194return CubicBezierCurve<float>(dot(v0-p,dx),dot(v1-p,dx),dot(v2-p,dx),dot(v3-p,dx));195}196197__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space) const198{199const Vec3fa q0 = xfmVector(space,v0);200const Vec3fa q1 = xfmVector(space,v1);201const Vec3fa q2 = xfmVector(space,v2);202const Vec3fa q3 = xfmVector(space,v3);203return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);204}205206__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p) const207{208const Vec3fa q0 = xfmVector(space,v0-p);209const Vec3fa q1 = xfmVector(space,v1-p);210const Vec3fa q2 = xfmVector(space,v2-p);211const Vec3fa q3 = xfmVector(space,v3-p);212return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);213}214215__forceinline CubicBezierCurve<Vec3ff> xfm_pr(const LinearSpace3fa& space, const Vec3fa& p) const216{217const Vec3ff q0(xfmVector(space,(Vec3fa)v0-p), v0.w);218const Vec3ff q1(xfmVector(space,(Vec3fa)v1-p), v1.w);219const Vec3ff q2(xfmVector(space,(Vec3fa)v2-p), v2.w);220const Vec3ff q3(xfmVector(space,(Vec3fa)v3-p), v3.w);221return CubicBezierCurve<Vec3ff>(q0,q1,q2,q3);222}223224__forceinline CubicBezierCurve<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p, const float s) const225{226const Vec3fa q0 = xfmVector(space,s*(v0-p));227const Vec3fa q1 = xfmVector(space,s*(v1-p));228const Vec3fa q2 = xfmVector(space,s*(v2-p));229const Vec3fa q3 = xfmVector(space,s*(v3-p));230return CubicBezierCurve<Vec3fa>(q0,q1,q2,q3);231}232233__forceinline int maxRoots() const;234235__forceinline BBox<Vertex> bounds() const {236return merge(BBox<Vertex>(v0),BBox<Vertex>(v1),BBox<Vertex>(v2),BBox<Vertex>(v3));237}238239__forceinline friend CubicBezierCurve operator +( const CubicBezierCurve& a, const CubicBezierCurve& b ) {240return CubicBezierCurve(a.v0+b.v0,a.v1+b.v1,a.v2+b.v2,a.v3+b.v3);241}242243__forceinline friend CubicBezierCurve operator -( const CubicBezierCurve& a, const CubicBezierCurve& b ) {244return CubicBezierCurve(a.v0-b.v0,a.v1-b.v1,a.v2-b.v2,a.v3-b.v3);245}246247__forceinline friend CubicBezierCurve operator -( const CubicBezierCurve& a, const Vertex& b ) {248return CubicBezierCurve(a.v0-b,a.v1-b,a.v2-b,a.v3-b);249}250251__forceinline friend CubicBezierCurve operator *( const Vertex& a, const CubicBezierCurve& b ) {252return CubicBezierCurve(a*b.v0,a*b.v1,a*b.v2,a*b.v3);253}254255__forceinline friend CubicBezierCurve cmadd( const Vertex& a, const CubicBezierCurve& b, const CubicBezierCurve& c) {256return CubicBezierCurve(madd(a,b.v0,c.v0),madd(a,b.v1,c.v1),madd(a,b.v2,c.v2),madd(a,b.v3,c.v3));257}258259__forceinline friend CubicBezierCurve clerp ( const CubicBezierCurve& a, const CubicBezierCurve& b, const Vertex& t ) {260return cmadd((Vertex(1.0f)-t),a,t*b);261}262263__forceinline friend CubicBezierCurve merge ( const CubicBezierCurve& a, const CubicBezierCurve& b ) {264return CubicBezierCurve(merge(a.v0,b.v0),merge(a.v1,b.v1),merge(a.v2,b.v2),merge(a.v3,b.v3));265}266267__forceinline void split(CubicBezierCurve& left, CubicBezierCurve& right, const float t = 0.5f) const268{269const Vertex p00 = v0;270const Vertex p01 = v1;271const Vertex p02 = v2;272const Vertex p03 = v3;273274const Vertex p10 = lerp(p00,p01,t);275const Vertex p11 = lerp(p01,p02,t);276const Vertex p12 = lerp(p02,p03,t);277const Vertex p20 = lerp(p10,p11,t);278const Vertex p21 = lerp(p11,p12,t);279const Vertex p30 = lerp(p20,p21,t);280281new (&left ) CubicBezierCurve(p00,p10,p20,p30);282new (&right) CubicBezierCurve(p30,p21,p12,p03);283}284285__forceinline CubicBezierCurve<Vec2vfx> split() const286{287const float u0 = 0.0f, u1 = 1.0f;288const float dscale = (u1-u0)*(1.0f/(3.0f*(VSIZEX-1)));289const vfloatx vu0 = lerp(u0,u1,vfloatx(StepTy())*(1.0f/(VSIZEX-1)));290Vec2vfx P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vfx(dscale);291const Vec2vfx P3 = shift_right_1(P0);292const Vec2vfx dP3du = shift_right_1(dP0du);293const Vec2vfx P1 = P0 + dP0du;294const Vec2vfx P2 = P3 - dP3du;295return CubicBezierCurve<Vec2vfx>(P0,P1,P2,P3);296}297298__forceinline CubicBezierCurve<Vec2vfx> split(const BBox1f& u) const299{300const float u0 = u.lower, u1 = u.upper;301const float dscale = (u1-u0)*(1.0f/(3.0f*(VSIZEX-1)));302const vfloatx vu0 = lerp(u0,u1,vfloatx(StepTy())*(1.0f/(VSIZEX-1)));303Vec2vfx P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vfx(dscale);304const Vec2vfx P3 = shift_right_1(P0);305const Vec2vfx dP3du = shift_right_1(dP0du);306const Vec2vfx P1 = P0 + dP0du;307const Vec2vfx P2 = P3 - dP3du;308return CubicBezierCurve<Vec2vfx>(P0,P1,P2,P3);309}310311template<int W>312__forceinline CubicBezierCurve<Vec2vf<W>> split(const BBox1f& u, int i, int N) const313{314const float u0 = u.lower, u1 = u.upper;315const float dscale = (u1-u0)*(1.0f/(3.0f*N));316const vfloat<W> vu0 = lerp(u0,u1,(vfloat<W>(i)+vfloat<W>(StepTy()))*(1.0f/N));317Vec2vf<W> P0, dP0du; evalN(vu0,P0,dP0du); dP0du = dP0du * Vec2vf<W>(dscale);318const Vec2vf<W> P3 = shift_right_1(P0);319const Vec2vf<W> dP3du = shift_right_1(dP0du);320const Vec2vf<W> P1 = P0 + dP0du;321const Vec2vf<W> P2 = P3 - dP3du;322return CubicBezierCurve<Vec2vf<W>>(P0,P1,P2,P3);323}324325__forceinline CubicBezierCurve<Vec2f> split1(const BBox1f& u, int i, int N) const326{327const float u0 = u.lower, u1 = u.upper;328const float dscale = (u1-u0)*(1.0f/(3.0f*N));329const float vu0 = lerp(u0,u1,(float(i)+0)*(1.0f/N));330const float vu1 = lerp(u0,u1,(float(i)+1)*(1.0f/N));331Vec2fa P0, dP0du; eval(vu0,P0,dP0du); dP0du = dP0du * Vec2fa(dscale);332Vec2fa P3, dP3du; eval(vu1,P3,dP3du); dP3du = dP3du * Vec2fa(dscale);333const Vec2fa P1 = P0 + dP0du;334const Vec2fa P2 = P3 - dP3du;335return CubicBezierCurve<Vec2f>(P0,P1,P2,P3);336}337338__forceinline void eval(float t, Vertex& p, Vertex& dp) const339{340const Vertex p00 = v0;341const Vertex p01 = v1;342const Vertex p02 = v2;343const Vertex p03 = v3;344345const Vertex p10 = lerp(p00,p01,t);346const Vertex p11 = lerp(p01,p02,t);347const Vertex p12 = lerp(p02,p03,t);348const Vertex p20 = lerp(p10,p11,t);349const Vertex p21 = lerp(p11,p12,t);350const Vertex p30 = lerp(p20,p21,t);351352p = p30;353dp = Vertex(3.0f)*(p21-p20);354}355356#if 0357__forceinline Vertex eval(float t) const358{359const Vertex p00 = v0;360const Vertex p01 = v1;361const Vertex p02 = v2;362const Vertex p03 = v3;363364const Vertex p10 = lerp(p00,p01,t);365const Vertex p11 = lerp(p01,p02,t);366const Vertex p12 = lerp(p02,p03,t);367const Vertex p20 = lerp(p10,p11,t);368const Vertex p21 = lerp(p11,p12,t);369const Vertex p30 = lerp(p20,p21,t);370371return p30;372}373#else374__forceinline Vertex eval(const float t) const375{376const Vec4<float> b = BezierBasis::eval(t);377return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));378}379#endif380381__forceinline Vertex eval_dt(float t) const382{383const Vertex p00 = v1-v0;384const Vertex p01 = v2-v1;385const Vertex p02 = v3-v2;386const Vertex p10 = lerp(p00,p01,t);387const Vertex p11 = lerp(p01,p02,t);388const Vertex p20 = lerp(p10,p11,t);389return Vertex(3.0f)*p20;390}391392__forceinline Vertex eval_du(const float t) const393{394const Vec4<float> b = BezierBasis::derivative(t);395return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));396}397398__forceinline Vertex eval_dudu(const float t) const399{400const Vec4<float> b = BezierBasis::derivative2(t);401return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));402}403404__forceinline void evalN(const vfloatx& t, Vec2vfx& p, Vec2vfx& dp) const405{406const Vec2vfx p00 = v0;407const Vec2vfx p01 = v1;408const Vec2vfx p02 = v2;409const Vec2vfx p03 = v3;410411const Vec2vfx p10 = lerp(p00,p01,t);412const Vec2vfx p11 = lerp(p01,p02,t);413const Vec2vfx p12 = lerp(p02,p03,t);414415const Vec2vfx p20 = lerp(p10,p11,t);416const Vec2vfx p21 = lerp(p11,p12,t);417418const Vec2vfx p30 = lerp(p20,p21,t);419420p = p30;421dp = vfloatx(3.0f)*(p21-p20);422}423424__forceinline void eval(const float t, Vertex& p, Vertex& dp, Vertex& ddp) const425{426const Vertex p00 = v0;427const Vertex p01 = v1;428const Vertex p02 = v2;429const Vertex p03 = v3;430const Vertex p10 = lerp(p00,p01,t);431const Vertex p11 = lerp(p01,p02,t);432const Vertex p12 = lerp(p02,p03,t);433const Vertex p20 = lerp(p10,p11,t);434const Vertex p21 = lerp(p11,p12,t);435const Vertex p30 = lerp(p20,p21,t);436p = p30;437dp = 3.0f*(p21-p20);438ddp = eval_dudu(t);439}440441__forceinline CubicBezierCurve clip(const Interval1f& u1) const442{443Vertex f0,df0; eval(u1.lower,f0,df0);444Vertex f1,df1; eval(u1.upper,f1,df1);445float s = u1.upper-u1.lower;446return CubicBezierCurve(f0,f0+s*(1.0f/3.0f)*df0,f1-s*(1.0f/3.0f)*df1,f1);447}448449__forceinline QuadraticBezierCurve<Vertex> derivative() const450{451const Vertex q0 = 3.0f*(v1-v0);452const Vertex q1 = 3.0f*(v2-v1);453const Vertex q2 = 3.0f*(v3-v2);454return QuadraticBezierCurve<Vertex>(q0,q1,q2);455}456457__forceinline BBox<Vertex> derivative_bounds(const Interval1f& u1) const458{459Vertex f0,df0; eval(u1.lower,f0,df0);460Vertex f3,df3; eval(u1.upper,f3,df3);461const float s = u1.upper-u1.lower;462const Vertex f1 = f0+s*(1.0f/3.0f)*df0;463const Vertex f2 = f3-s*(1.0f/3.0f)*df3;464const Vertex q0 = s*df0;465const Vertex q1 = 3.0f*(f2-f1);466const Vertex q2 = s*df3;467return merge(BBox<Vertex>(q0),BBox<Vertex>(q1),BBox<Vertex>(q2));468}469470template<int M>471__forceinline Vec4vf<M> veval(const vfloat<M>& t) const472{473const Vec4vf<M> b = BezierBasis::eval(t);474return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));475}476477template<int M>478__forceinline Vec4vf<M> veval_du(const vfloat<M>& t) const479{480const Vec4vf<M> b = BezierBasis::derivative(t);481return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));482}483484template<int M>485__forceinline Vec4vf<M> veval_dudu(const vfloat<M>& t) const486{487const Vec4vf<M> b = BezierBasis::derivative2(t);488return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));489}490491template<int M, typename Vec>492__forceinline void veval(const vfloat<M>& t, Vec& p, Vec& dp) const493{494const Vec p00 = v0;495const Vec p01 = v1;496const Vec p02 = v2;497const Vec p03 = v3;498499const Vec p10 = lerp(p00,p01,t);500const Vec p11 = lerp(p01,p02,t);501const Vec p12 = lerp(p02,p03,t);502const Vec p20 = lerp(p10,p11,t);503const Vec p21 = lerp(p11,p12,t);504const Vec p30 = lerp(p20,p21,t);505506p = p30;507dp = vfloat<M>(3.0f)*(p21-p20);508}509510template<int M, typename Vec = Vec4vf<M>>511__forceinline Vec eval0(const int ofs, const int size) const512{513assert(size <= PrecomputedBezierBasis::N);514assert(ofs <= size);515#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)516assert(size > 0);517const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+0))*rcp(float(size));518Vec p,dp; veval<M>(t,p,dp);519return p;520#else521return madd(vfloat<M>::loadu(&bezier_basis0.c0[size][ofs]), Vec(v0),522madd(vfloat<M>::loadu(&bezier_basis0.c1[size][ofs]), Vec(v1),523madd(vfloat<M>::loadu(&bezier_basis0.c2[size][ofs]), Vec(v2),524vfloat<M>::loadu(&bezier_basis0.c3[size][ofs]) * Vec(v3))));525#endif526}527528template<int M, typename Vec = Vec4vf<M>>529__forceinline Vec eval1(const int ofs, const int size) const530{531assert(size <= PrecomputedBezierBasis::N);532assert(ofs <= size);533#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)534assert(size > 0);535const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+1))*rcp(float(size));536Vec p,dp; veval<M>(t,p,dp);537return p;538#else539return madd(vfloat<M>::loadu(&bezier_basis1.c0[size][ofs]), Vec(v0),540madd(vfloat<M>::loadu(&bezier_basis1.c1[size][ofs]), Vec(v1),541madd(vfloat<M>::loadu(&bezier_basis1.c2[size][ofs]), Vec(v2),542vfloat<M>::loadu(&bezier_basis1.c3[size][ofs]) * Vec(v3))));543#endif544}545546template<int M, typename Vec = Vec4vf<M>>547__forceinline Vec derivative0(const int ofs, const int size) const548{549assert(size <= PrecomputedBezierBasis::N);550assert(ofs <= size);551#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)552assert(size > 0);553const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+0))*rcp(float(size));554Vec p,dp; veval<M>(t,p,dp);555return dp;556#else557return madd(vfloat<M>::loadu(&bezier_basis0.d0[size][ofs]), Vec(v0),558madd(vfloat<M>::loadu(&bezier_basis0.d1[size][ofs]), Vec(v1),559madd(vfloat<M>::loadu(&bezier_basis0.d2[size][ofs]), Vec(v2),560vfloat<M>::loadu(&bezier_basis0.d3[size][ofs]) * Vec(v3))));561#endif562}563564template<int M, typename Vec = Vec4vf<M>>565__forceinline Vec derivative1(const int ofs, const int size) const566{567assert(size <= PrecomputedBezierBasis::N);568assert(ofs <= size);569#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)570assert(size > 0);571const vfloat<M> t = (vfloat<M>(step) + vfloat<M>(ofs+1))*rcp(float(size));572Vec p,dp; veval<M>(t,p,dp);573return dp;574#else575return madd(vfloat<M>::loadu(&bezier_basis1.d0[size][ofs]), Vec(v0),576madd(vfloat<M>::loadu(&bezier_basis1.d1[size][ofs]), Vec(v1),577madd(vfloat<M>::loadu(&bezier_basis1.d2[size][ofs]), Vec(v2),578vfloat<M>::loadu(&bezier_basis1.d3[size][ofs]) * Vec(v3))));579#endif580}581582/* calculates bounds of bezier curve geometry */583__forceinline BBox3fa accurateBounds() const584{585const int N = 7;586const float scale = 1.0f/(3.0f*(N-1));587Vec3vfx pl(pos_inf), pu(neg_inf);588for (int i=0; i<=N; i+=VSIZEX)589{590vintx vi = vintx(i)+vintx(StepTy());591vboolx valid = vi <= vintx(N);592const Vec3vfx p = eval0<VSIZEX,Vec3vf<VSIZEX>>(i,N);593const Vec3vfx dp = derivative0<VSIZEX,Vec3vf<VSIZEX>>(i,N);594const Vec3vfx pm = p-Vec3vfx(scale)*select(vi!=vintx(0),dp,Vec3vfx(zero));595const Vec3vfx pp = p+Vec3vfx(scale)*select(vi!=vintx(N),dp,Vec3vfx(zero));596pl = select(valid,min(pl,p,pm,pp),pl); // FIXME: use masked min597pu = select(valid,max(pu,p,pm,pp),pu); // FIXME: use masked min598}599const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));600const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));601return BBox3fa(lower,upper);602}603604/* calculates bounds of bezier curve geometry */605__forceinline BBox3fa accurateRoundBounds() const606{607const int N = 7;608const float scale = 1.0f/(3.0f*(N-1));609Vec4vfx pl(pos_inf), pu(neg_inf);610for (int i=0; i<=N; i+=VSIZEX)611{612vintx vi = vintx(i)+vintx(StepTy());613vboolx valid = vi <= vintx(N);614const Vec4vfx p = eval0<VSIZEX>(i,N);615const Vec4vfx dp = derivative0<VSIZEX>(i,N);616const Vec4vfx pm = p-Vec4vfx(scale)*select(vi!=vintx(0),dp,Vec4vfx(zero));617const Vec4vfx pp = p+Vec4vfx(scale)*select(vi!=vintx(N),dp,Vec4vfx(zero));618pl = select(valid,min(pl,p,pm,pp),pl); // FIXME: use masked min619pu = select(valid,max(pu,p,pm,pp),pu); // FIXME: use masked min620}621const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));622const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));623const float r_min = reduce_min(pl.w);624const float r_max = reduce_max(pu.w);625const Vec3fa upper_r = Vec3fa(max(abs(r_min),abs(r_max)));626return enlarge(BBox3fa(lower,upper),upper_r);627}628629/* calculates bounds when tessellated into N line segments */630__forceinline BBox3fa accurateFlatBounds(int N) const631{632if (likely(N == 4))633{634const Vec4vf4 pi = eval0<4>(0,4);635const Vec3fa lower(reduce_min(pi.x),reduce_min(pi.y),reduce_min(pi.z));636const Vec3fa upper(reduce_max(pi.x),reduce_max(pi.y),reduce_max(pi.z));637const Vec3fa upper_r = Vec3fa(reduce_max(abs(pi.w)));638return enlarge(BBox3fa(min(lower,v3),max(upper,v3)),max(upper_r,Vec3fa(abs(v3.w))));639}640else641{642Vec3vfx pl(pos_inf), pu(neg_inf); vfloatx ru(0.0f);643for (int i=0; i<N; i+=VSIZEX)644{645vboolx valid = vintx(i)+vintx(StepTy()) < vintx(N);646const Vec4vfx pi = eval0<VSIZEX>(i,N);647648pl.x = select(valid,min(pl.x,pi.x),pl.x); // FIXME: use masked min649pl.y = select(valid,min(pl.y,pi.y),pl.y);650pl.z = select(valid,min(pl.z,pi.z),pl.z);651652pu.x = select(valid,max(pu.x,pi.x),pu.x); // FIXME: use masked min653pu.y = select(valid,max(pu.y,pi.y),pu.y);654pu.z = select(valid,max(pu.z,pi.z),pu.z);655656ru = select(valid,max(ru,abs(pi.w)),ru);657}658const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));659const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));660const Vec3fa upper_r(reduce_max(ru));661return enlarge(BBox3fa(min(lower,v3),max(upper,v3)),max(upper_r,Vec3fa(abs(v3.w))));662}663}664665friend __forceinline embree_ostream operator<<(embree_ostream cout, const CubicBezierCurve& curve) {666return cout << "CubicBezierCurve { v0 = " << curve.v0 << ", v1 = " << curve.v1 << ", v2 = " << curve.v2 << ", v3 = " << curve.v3 << " }";667}668};669670#if defined(__AVX__)671template<>672__forceinline CubicBezierCurve<vfloat4> CubicBezierCurve<vfloat4>::clip(const Interval1f& u1) const673{674const vfloat8 p00 = vfloat8(v0);675const vfloat8 p01 = vfloat8(v1);676const vfloat8 p02 = vfloat8(v2);677const vfloat8 p03 = vfloat8(v3);678679const vfloat8 t(vfloat4(u1.lower),vfloat4(u1.upper));680const vfloat8 p10 = lerp(p00,p01,t);681const vfloat8 p11 = lerp(p01,p02,t);682const vfloat8 p12 = lerp(p02,p03,t);683const vfloat8 p20 = lerp(p10,p11,t);684const vfloat8 p21 = lerp(p11,p12,t);685const vfloat8 p30 = lerp(p20,p21,t);686687const vfloat8 f01 = p30;688const vfloat8 df01 = vfloat8(3.0f)*(p21-p20);689690const vfloat4 f0 = extract4<0>(f01), f1 = extract4<1>(f01);691const vfloat4 df0 = extract4<0>(df01), df1 = extract4<1>(df01);692const float s = u1.upper-u1.lower;693return CubicBezierCurve(f0,f0+s*(1.0f/3.0f)*df0,f1-s*(1.0f/3.0f)*df1,f1);694}695#endif696697template<typename Vertex> using BezierCurveT = CubicBezierCurve<Vertex>;698699typedef CubicBezierCurve<float> CubicBezierCurve1f;700typedef CubicBezierCurve<Vec2fa> CubicBezierCurve2fa;701typedef CubicBezierCurve<Vec3fa> CubicBezierCurve3fa;702typedef CubicBezierCurve<Vec3fa> BezierCurve3fa;703typedef CubicBezierCurve<Vec3ff> BezierCurve3ff;704705template<> __forceinline int CubicBezierCurve<float>::maxRoots() const706{707float eps = 1E-4f;708bool neg0 = v0 <= 0.0f; bool zero0 = fabs(v0) < eps;709bool neg1 = v1 <= 0.0f; bool zero1 = fabs(v1) < eps;710bool neg2 = v2 <= 0.0f; bool zero2 = fabs(v2) < eps;711bool neg3 = v3 <= 0.0f; bool zero3 = fabs(v3) < eps;712return (neg0 != neg1 || zero0) + (neg1 != neg2 || zero1) + (neg2 != neg3 || zero2 || zero3);713}714715template<> __forceinline int CubicBezierCurve<Interval1f>::maxRoots() const {716return numRoots(v0,v1) + numRoots(v1,v2) + numRoots(v2,v3);717}718719struct CurveGeometry; // FIXME: this code should move !720template<typename CurveGeometry>721__forceinline CubicBezierCurve<Vec3ff> enlargeRadiusToMinWidth(const RayQueryContext* context, const CurveGeometry* geom, const Vec3fa& ray_org, const CubicBezierCurve<Vec3ff>& curve)722{723return CubicBezierCurve<Vec3ff>(enlargeRadiusToMinWidth(context,geom,ray_org,curve.v0),724enlargeRadiusToMinWidth(context,geom,ray_org,curve.v1),725enlargeRadiusToMinWidth(context,geom,ray_org,curve.v2),726enlargeRadiusToMinWidth(context,geom,ray_org,curve.v3));727}728}729730731