1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "vec2.h"
7#include "vec3.h"
8#include "bbox.h"
9
10namespace embree
11{
12 template<typename V>
13 struct Interval
14 {
15 V lower, upper;
16
17 __forceinline Interval() {}
18 __forceinline Interval ( const Interval& other ) { lower = other.lower; upper = other.upper; }
19 __forceinline Interval& operator=( const Interval& other ) { lower = other.lower; upper = other.upper; return *this; }
20
21 __forceinline Interval(const V& a) : lower(a), upper(a) {}
22 __forceinline Interval(const V& lower, const V& upper) : lower(lower), upper(upper) {}
23 __forceinline Interval(const BBox<V>& a) : lower(a.lower), upper(a.upper) {}
24
25 /*! tests if box is empty */
26 //__forceinline bool empty() const { return lower > upper; }
27
28 /*! computes the size of the interval */
29 __forceinline V size() const { return upper - lower; }
30
31 __forceinline V center() const { return 0.5f*(lower+upper); }
32
33 __forceinline const Interval& extend(const Interval& other) { lower = min(lower,other.lower); upper = max(upper,other.upper); return *this; }
34 __forceinline const Interval& extend(const V & other) { lower = min(lower,other ); upper = max(upper,other ); return *this; }
35
36 __forceinline friend Interval operator +( const Interval& a, const Interval& b ) {
37 return Interval(a.lower+b.lower,a.upper+b.upper);
38 }
39
40 __forceinline friend Interval operator -( const Interval& a, const Interval& b ) {
41 return Interval(a.lower-b.upper,a.upper-b.lower);
42 }
43
44 __forceinline friend Interval operator -( const Interval& a, const V& b ) {
45 return Interval(a.lower-b,a.upper-b);
46 }
47
48 __forceinline friend Interval operator *( const Interval& a, const Interval& b )
49 {
50 const V ll = a.lower*b.lower;
51 const V lu = a.lower*b.upper;
52 const V ul = a.upper*b.lower;
53 const V uu = a.upper*b.upper;
54 return Interval(min(ll,lu,ul,uu),max(ll,lu,ul,uu));
55 }
56
57 __forceinline friend Interval merge( const Interval& a, const Interval& b) {
58 return Interval(min(a.lower,b.lower),max(a.upper,b.upper));
59 }
60
61 __forceinline friend Interval merge( const Interval& a, const Interval& b, const Interval& c) {
62 return merge(merge(a,b),c);
63 }
64
65 __forceinline friend Interval merge( const Interval& a, const Interval& b, const Interval& c, const Interval& d) {
66 return merge(merge(a,b),merge(c,d));
67 }
68
69 /*! intersect bounding boxes */
70 __forceinline friend const Interval intersect( const Interval& a, const Interval& b ) { return Interval(max(a.lower, b.lower), min(a.upper, b.upper)); }
71 __forceinline friend const Interval intersect( const Interval& a, const Interval& b, const Interval& c ) { return intersect(a,intersect(b,c)); }
72 __forceinline friend const Interval intersect( const Interval& a, const Interval& b, const Interval& c, const Interval& d ) { return intersect(intersect(a,b),intersect(c,d)); }
73
74 friend embree_ostream operator<<(embree_ostream cout, const Interval& a) {
75 return cout << "[" << a.lower << ", " << a.upper << "]";
76 }
77
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Constants
80 ////////////////////////////////////////////////////////////////////////////////
81
82 __forceinline Interval( EmptyTy ) : lower(pos_inf), upper(neg_inf) {}
83 __forceinline Interval( FullTy ) : lower(neg_inf), upper(pos_inf) {}
84 };
85
86 __forceinline bool isEmpty(const Interval<float>& v) {
87 return v.lower > v.upper;
88 }
89
90 __forceinline vboolx isEmpty(const Interval<vfloatx>& v) {
91 return v.lower > v.upper;
92 }
93
94 /*! subset relation */
95 template<typename T> __forceinline bool subset( const Interval<T>& a, const Interval<T>& b ) {
96 return (a.lower > b.lower) && (a.upper < b.upper);
97 }
98
99 template<typename T> __forceinline bool subset( const Vec2<Interval<T>>& a, const Vec2<Interval<T>>& b ) {
100 return subset(a.x,b.x) && subset(a.y,b.y);
101 }
102
103 template<typename T> __forceinline const Vec2<Interval<T>> intersect( const Vec2<Interval<T>>& a, const Vec2<Interval<T>>& b ) {
104 return Vec2<Interval<T>>(intersect(a.x,b.x),intersect(a.y,b.y));
105 }
106
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Select
109 ////////////////////////////////////////////////////////////////////////////////
110
111 template<typename T> __forceinline Interval<T> select ( bool s, const Interval<T>& t, const Interval<T>& f ) {
112 return Interval<T>(select(s,t.lower,f.lower),select(s,t.upper,f.upper));
113 }
114
115 template<typename T> __forceinline Interval<T> select ( const typename T::Bool& s, const Interval<T>& t, const Interval<T>& f ) {
116 return Interval<T>(select(s,t.lower,f.lower),select(s,t.upper,f.upper));
117 }
118
119 __forceinline int numRoots(const Interval<float>& p0, const Interval<float>& p1)
120 {
121 float eps = 1E-4f;
122 bool neg0 = p0.lower < eps; bool pos0 = p0.upper > -eps;
123 bool neg1 = p1.lower < eps; bool pos1 = p1.upper > -eps;
124 return (neg0 && pos1) || (pos0 && neg1) || (neg0 && pos0) || (neg1 && pos1);
125 }
126
127 typedef Interval<float> Interval1f;
128 typedef Vec2<Interval<float>> Interval2f;
129 typedef Vec3<Interval<float>> Interval3f;
130
131inline void swap(float& a, float& b) { float tmp = a; a = b; b = tmp; }
132
133inline Interval1f shift(const Interval1f& v, float shift) { return Interval1f(v.lower + shift, v.upper + shift); }
134
135#define TWO_PI (2.0*M_PI)
136inline Interval1f sin(Interval1f interval)
137{
138 if (interval.upper-interval.lower >= M_PI) { return Interval1f(-1.0, 1.0); }
139 if (interval.upper > TWO_PI) { interval = shift(interval, -TWO_PI*floor(interval.upper/TWO_PI)); }
140 if (interval.lower < 0) { interval = shift(interval, -TWO_PI*floor(interval.lower/TWO_PI)); }
141 float sinLower = sin(interval.lower);
142 float sinUpper = sin(interval.upper);
143 if (sinLower > sinUpper) swap(sinLower, sinUpper);
144 if (interval.lower < M_PI / 2.0 && interval.upper > M_PI / 2.0) sinUpper = 1.0;
145 if (interval.lower < 3.0 * M_PI / 2.0 && interval.upper > 3.0 * M_PI / 2.0) sinLower = -1.0;
146 return Interval1f(sinLower, sinUpper);
147}
148
149inline Interval1f cos(Interval1f interval)
150{
151 if (interval.upper-interval.lower >= M_PI) { return Interval1f(-1.0, 1.0); }
152 if (interval.upper > TWO_PI) { interval = shift(interval, -TWO_PI*floor(interval.upper/TWO_PI)); }
153 if (interval.lower < 0) { interval = shift(interval, -TWO_PI*floor(interval.lower/TWO_PI)); }
154 float cosLower = cos(interval.lower);
155 float cosUpper = cos(interval.upper);
156 if (cosLower > cosUpper) swap(cosLower, cosUpper);
157 if (interval.lower < M_PI && interval.upper > M_PI) cosLower = -1.0;
158 return Interval1f(cosLower, cosUpper);
159}
160#undef TWO_PI
161}
162