Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util_boundbox.h « util « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed94ca20211d1e3ca9d695f2bf6c2c9a291546ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright 2011-2013 Blender Foundation
 *
 * 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.
 */

#ifndef __UTIL_BOUNDBOX_H__
#define __UTIL_BOUNDBOX_H__

#include <math.h>
#include <float.h>

#include "util/util_math.h"
#include "util/util_string.h"
#include "util/util_transform.h"
#include "util/util_types.h"

CCL_NAMESPACE_BEGIN

/* 3D BoundBox */

class BoundBox
{
public:
	float3 min, max;

	__forceinline BoundBox()
	{
	}

	__forceinline BoundBox(const float3& pt)
	: min(pt), max(pt)
	{
	}

	__forceinline BoundBox(const float3& min_, const float3& max_)
	: min(min_), max(max_)
	{
	}

	enum empty_t { empty = 0};

	__forceinline BoundBox(empty_t)
	: min(make_float3(FLT_MAX, FLT_MAX, FLT_MAX)), max(make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX))
	{
	}

	__forceinline void grow(const float3& pt)  
	{
		/* the order of arguments to min is such that if pt is nan, it will not
		 * influence the resulting bounding box */
		min = ccl::min(pt, min);
		max = ccl::max(pt, max);
	}

	__forceinline void grow(const float3& pt, float border)  
	{
		float3 shift = make_float3(border, border, border);
		min = ccl::min(pt - shift, min);
		max = ccl::max(pt + shift, max);
	}

	__forceinline void grow(const BoundBox& bbox)
	{
		grow(bbox.min);
		grow(bbox.max);
	}

	__forceinline void grow_safe(const float3& pt)  
	{
		/* the order of arguments to min is such that if pt is nan, it will not
		 * influence the resulting bounding box */
		if(isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z)) {
			min = ccl::min(pt, min);
			max = ccl::max(pt, max);
		}
	}

	__forceinline void grow_safe(const float3& pt, float border)  
	{
		if(isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z) && isfinite(border)) {
			float3 shift = make_float3(border, border, border);
			min = ccl::min(pt - shift, min);
			max = ccl::max(pt + shift, max);
		}
	}

	__forceinline void grow_safe(const BoundBox& bbox)
	{
		grow_safe(bbox.min);
		grow_safe(bbox.max);
	}

	__forceinline void intersect(const BoundBox& bbox) 
	{
		min = ccl::max(min, bbox.min);
		max = ccl::min(max, bbox.max);
	}

	/* todo: avoid using this */
	__forceinline float safe_area() const
	{
		if(!((min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z)))
			return 0.0f;

		return area();
	}

	__forceinline float area() const
	{
		return half_area()*2.0f;
	}

	__forceinline float half_area() const
	{
		float3 d = max - min;
		return (d.x*d.z + d.y*d.z + d.x*d.y);
	}

	__forceinline float3 center() const
	{
		return 0.5f*(min + max);
	}

	__forceinline float3 center2() const
	{
		return min + max;
	}

	__forceinline float3 size() const
	{
		return max - min;
	}

	__forceinline bool valid() const
	{
		return (min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z) &&
		       (isfinite(min.x) && isfinite(min.y) && isfinite(min.z)) &&
		       (isfinite(max.x) && isfinite(max.y) && isfinite(max.z));
	}

	BoundBox transformed(const Transform *tfm) const
	{
		BoundBox result = BoundBox::empty;

		for(int i = 0; i < 8; i++) {
			float3 p;

			p.x = (i & 1)? min.x: max.x;
			p.y = (i & 2)? min.y: max.y;
			p.z = (i & 4)? min.z: max.z;

			result.grow(transform_point(tfm, p));
		}

		return result;
	}

	__forceinline bool intersects(const BoundBox& other)
	{
		float3 center_diff = center() - other.center(),
		       total_size = (size() + other.size()) * 0.5f;
		return fabsf(center_diff.x) <= total_size.x &&
		       fabsf(center_diff.y) <= total_size.y &&
		       fabsf(center_diff.z) <= total_size.z;
	}
};

__forceinline BoundBox merge(const BoundBox& bbox, const float3& pt)
{
	return BoundBox(min(bbox.min, pt), max(bbox.max, pt));
}

__forceinline BoundBox merge(const BoundBox& a, const BoundBox& b)
{
	return BoundBox(min(a.min, b.min), max(a.max, b.max));
}

__forceinline BoundBox merge(const BoundBox& a, const BoundBox& b, const BoundBox& c, const BoundBox& d)
{
	return merge(merge(a, b), merge(c, d));
}

__forceinline BoundBox intersect(const BoundBox& a, const BoundBox& b)
{
	return BoundBox(max(a.min, b.min), min(a.max, b.max));
}

__forceinline BoundBox intersect(const BoundBox& a, const BoundBox& b, const BoundBox& c)
{
	return intersect(a, intersect(b, c));
}

/* 2D BoundBox */

class BoundBox2D {
public:
	float left;
	float right;
	float bottom;
	float top;

	BoundBox2D()
	: left(0.0f), right(1.0f), bottom(0.0f), top(1.0f)
	{
	}

	bool operator==(const BoundBox2D& other) const
	{
		return (left == other.left && right == other.right &&
		        bottom == other.bottom && top == other.top);
	}

	float width()
	{
		return right - left;
	}

	float height()
	{
		return top - bottom;
	}

	BoundBox2D operator*(float f) const
	{
		BoundBox2D result;

		result.left = left*f;
		result.right = right*f;
		result.bottom = bottom*f;
		result.top = top*f;

		return result;
	}

	BoundBox2D subset(const BoundBox2D& other) const
	{
		BoundBox2D subset;

		subset.left = left + other.left*(right - left);
		subset.right = left + other.right*(right - left);
		subset.bottom = bottom + other.bottom*(top - bottom);
		subset.top = bottom + other.top*(top - bottom);

		return subset;
	}

	BoundBox2D make_relative_to(const BoundBox2D& other) const
	{
		BoundBox2D result;

		result.left = ((left - other.left) / (other.right - other.left));
		result.right = ((right - other.left) / (other.right - other.left));
		result.bottom = ((bottom - other.bottom) / (other.top - other.bottom));
		result.top = ((top - other.bottom) / (other.top - other.bottom));

		return result;
	}

	BoundBox2D clamp(float mn = 0.0f, float mx = 1.0f)
	{
		BoundBox2D result;

		result.left = ccl::clamp(left, mn, mx);
		result.right = ccl::clamp(right, mn, mx);
		result.bottom = ccl::clamp(bottom, mn, mx);
		result.top = ccl::clamp(top, mn, mx);

		return result;
	}
};

CCL_NAMESPACE_END

#endif /* __UTIL_BOUNDBOX_H__ */