-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathextent.cpp
More file actions
172 lines (147 loc) · 5.43 KB
/
Copy pathextent.cpp
File metadata and controls
172 lines (147 loc) · 5.43 KB
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
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// Copyright (c) 2003, 2004 by Jonathan Brandmeyer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
#include "util/extent.hpp"
#include <algorithm>
#include <iostream>
#include <limits>
#include <float.h>
#define QNAN (std::numeric_limits<double>::quiet_NaN())
namespace cvisual {
extent_data::extent_data(double tan_hfov)
: mins(QNAN,QNAN,QNAN),
maxs(QNAN,QNAN,QNAN),
camera_z(0),
buffer_depth(0)
{
cot_hfov = 1.0 / tan_hfov;
sin_hfov = sin( atan(tan_hfov) );
cos_hfov = sqrt( 1 - sin_hfov*sin_hfov );
invsin_hfov = 1.0 / sin_hfov;
}
bool extent_data::is_empty() const { return !(mins.x == mins.x); } //< return isnan(mins.x)
vector extent_data::get_center() const {
if (is_empty()) return vector();
return (mins + maxs) * 0.5;
}
void extent_data::get_near_and_far( const vector& forward, double& nearest, double& farthest ) const
{
if (is_empty() || (mins == maxs)) {
// The only way that this should happen is if the scene is empty.
nearest = 1.0;
farthest = 10.0;
return;
}
double corners[] = {
maxs.dot(forward), // front upper right
vector( mins.x, mins.y, maxs.z).dot(forward), // front lower left
vector( mins.x, maxs.y, maxs.z).dot(forward), // front upper left
vector( maxs.x, mins.y, maxs.z).dot(forward), // front lower right
vector( mins.x, maxs.y, mins.z).dot(forward), // back upper left
vector( maxs.x, mins.y, mins.z).dot(forward), // back lower right
vector( maxs.x, maxs.y, mins.z).dot(forward) // back upper right
};
nearest = farthest = mins.dot(forward); // back lower left
for (size_t i = 0; i < 7; ++i) {
if (corners[i] < nearest) {
nearest = corners[i];
}
if (corners[i] > farthest) {
farthest = corners[i];
}
}
}
vector extent_data::get_range( vector center) const {
if (is_empty()) return vector(0,0,0);
return vector(
std::max( fabs( center.x - mins.x), fabs( center.x - maxs.x)),
std::max( fabs( center.y - mins.y), fabs( center.y - maxs.y)),
std::max( fabs( center.z - mins.z), fabs( center.z - maxs.z)));
}
//////////////////////////////////
extent::extent( extent_data& data, const tmatrix& local_to_centered_world )
: data(data), l_cw( local_to_centered_world ), frame_depth(0)
{
}
extent::extent( extent& parent, const tmatrix& local_to_parent )
: data( parent.data ), frame_depth(parent.frame_depth+1)
{
l_cw = parent.l_cw * local_to_parent;
}
extent::~extent() {
}
void
extent::add_point( vector point)
{
point = l_cw * point;
// std::min(a,NAN) is defined as (NAN<a)?NAN:a, which is a. So these will select point on the
// first call!
data.mins.x = std::min( point.x, data.mins.x);
data.maxs.x = std::max( point.x, data.maxs.x);
data.mins.y = std::min( point.y, data.mins.y);
data.maxs.y = std::max( point.y, data.maxs.y);
data.mins.z = std::min( point.z, data.mins.z);
data.maxs.z = std::max( point.z, data.maxs.z);
// TODO: it might be more elegant (and even faster) to compute extents along
// specified axes (normal to the sides of the view frustum) and then display_kernel
// could compute camera_z from that. The downside is that taking the fabs(point.z) out
// means 4 axes are needed instead of 2.
data.camera_z = std::max( data.camera_z,
std::max(fabs(point.x),fabs(point.y))*data.cot_hfov + fabs(point.z) );
}
void
extent::add_sphere( vector center, double radius)
{
radius = fabs(radius); //<TODO: why?
center = l_cw * center;
data.mins.x = std::min( center.x - radius, data.mins.x );
data.maxs.x = std::max( center.x + radius, data.maxs.x );
data.mins.y = std::min( center.y - radius, data.mins.y );
data.maxs.y = std::max( center.y + radius, data.maxs.y );
data.mins.z = std::min( center.z - radius, data.mins.z );
data.maxs.z = std::max( center.z + radius, data.maxs.z );
data.camera_z = std::max( data.camera_z,
std::max(fabs(center.x),fabs(center.y))*data.cot_hfov
+ fabs(center.z)
+ radius * data.invsin_hfov );
}
void
extent::add_box( const tmatrix& fwt, const vector& a, const vector& b ) {
add_point( fwt * a );
add_point( fwt * vector(a.x,a.y,b.z) );
add_point( fwt * vector(a.x,b.y,a.z) );
add_point( fwt * vector(a.x,b.y,b.z) );
add_point( fwt * vector(b.x,a.y,a.z) );
add_point( fwt * vector(b.x,a.y,b.z) );
add_point( fwt * vector(b.x,b.y,a.z) );
add_point( fwt * b );
}
void extent::add_circle( const vector& center, const vector& normal, double r ) {
vector c = l_cw * center;
vector n = l_cw.times_v(normal);
vector n2( n.x*n.x, n.y*n.y, n.z*n.z );
vector r_proj( r*sqrt(1.0 - n2.x), r*sqrt(1.0 - n2.y), r*sqrt(1.0 - n2.z) );
data.mins.x = std::min( c.x - r_proj.x, data.mins.x );
data.maxs.x = std::max( c.x + r_proj.x, data.maxs.x );
data.mins.y = std::min( c.y - r_proj.y, data.mins.y );
data.maxs.y = std::max( c.y + r_proj.y, data.maxs.y );
data.mins.z = std::min( c.z - r_proj.z, data.mins.z );
data.maxs.z = std::max( c.z + r_proj.z, data.maxs.z );
double nxd = n.z*data.sin_hfov - n.x*data.cos_hfov;
double nyd = n.z*data.sin_hfov - n.y*data.cos_hfov;
data.camera_z = std::max( data.camera_z,
fabs(c.x)*data.cot_hfov
+ fabs(c.z)
+ r * sqrt(1.0 - nxd*nxd) * data.invsin_hfov );
data.camera_z = std::max( data.camera_z,
fabs(c.y)*data.cot_hfov
+ fabs(c.z)
+ r * sqrt(1.0 - nyd*nyd) * data.invsin_hfov );
}
void
extent::add_body()
{
data.buffer_depth += 4 + frame_depth;
}
} // !namespace cvisual