-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvutil_basic.cc
226 lines (211 loc) · 7.33 KB
/
envutil_basic.cc
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
/************************************************************************/
/* */
/* envutil - utility to convert between environment formats */
/* */
/* Copyright 2024 by Kay F. Jahnke */
/* */
/* The git repository for this software is at */
/* */
/* https://github.com/kfjahnke/envutil */
/* */
/* Please direct questions, bug reports, and contributions to */
/* */
/* [email protected] */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
// this file has some helper functions which don't use zimt and which
// aren't performance-critical. The code is largely from lux.
#include "envutil_basic.h"
// assuming isotropic sampling (same sampling resolution in the horizontal
// and vertical), calculate the vertical field of view from the horizontal
// field of view, under the given projection.
// Note that this function is for centered images only
// (x0 == -x1), (y0 == -y1)
double get_vfov ( projection_t projection ,
int width ,
int height ,
double hfov )
{
double vfov = 0.0 ;
switch ( projection )
{
case RECTILINEAR:
{
// as a one-liner, this is probably clearer than the code below
vfov = 2.0 * atan ( height * tan ( hfov / 2.0 ) / width ) ;
break ;
}
case CYLINDRICAL:
{
double pixels_per_rad = width / hfov ;
double h_rad = height / pixels_per_rad ;
vfov = 2.0 * atan ( h_rad / 2.0 ) ;
break ;
}
case STEREOGRAPHIC:
{
double w_rad = 2.0 * tan ( hfov / 4.0 ) ;
double pixels_per_rad = width / w_rad ;
double h_rad = height / pixels_per_rad ;
vfov = 4.0 * atan ( h_rad / 2.0 ) ;
break ;
}
case SPHERICAL:
case FISHEYE:
{
vfov = hfov * height / width ;
break ;
}
case CUBEMAP:
case BIATAN6:
{
vfov = 2.0 * M_PI ;
}
default:
{
vfov = hfov ; // debatable...
break ;
}
}
return vfov ;
}
// the 'step' of an image is the angle - in radians - which
// corresponds to the width of one pixel in the image center.
// for some projections and in certain directions, this value
// will be usable at non-central points (e.g. for spherical
// images along the horizon). In any case it can be used as a
// 'rule of thumb' indicator of the image's resolution.
// If we have the 'extent' of a spherical or cylindrical image
// already, we can calculate the step as hfov / width;
// for other projections this simple formula doesn't apply.
// Note that this function is for centered images only
// (x0 == -x1), (y0 == -y1)
// currently unused.
double get_step ( projection_t projection ,
int width ,
int height ,
double hfov )
{
double step = 0.0 ;
switch ( projection )
{
case RECTILINEAR:
case CUBEMAP:
case BIATAN6:
{
step = atan ( 2.0 * tan ( hfov / 2.0 ) / width ) ;
break ;
}
case SPHERICAL:
case CYLINDRICAL:
case FISHEYE:
{
step = hfov / width ;
break ;
}
case STEREOGRAPHIC:
{
step = atan ( 4.0 * tan ( hfov / 4.0 ) / width ) ;
break ;
}
default:
{
break ;
}
}
return step ;
}
// extract internally uses the notion of an image's 'extent' in 'model
// space'. The image is thought to be 'draped' to an 'archetypal 2D
// manifold' - the surface of a sphere or cylinder with unit radius
// or a plane at unit distance forward - where the sample points are
// placed on the 2D manifold so that rays from the origin to the
// scene point which corresponds with the sample point intersect there.
// To put it differently: the sample point cloud is scaled and shifted
// to come to lie on the 'archetypal' 2D manifolds. This makes for
// efficient calculations. The image is taken to be centered on the
// 'forward' ray.
extent_type get_extent ( projection_t projection ,
int width ,
int height ,
double hfov )
{
double x0 , x1 , y0 , y1 ;
double alpha_x = - hfov / 2.0 ;
double beta_x = hfov / 2.0 ;
double beta_y = get_vfov ( projection , width , height , hfov ) / 2.0 ;
double alpha_y = - beta_y ;
switch ( projection )
{
case SPHERICAL:
case FISHEYE:
{
x0 = alpha_x ;
x1 = beta_x ;
y0 = alpha_y ;
y1 = beta_y ;
break ;
}
case CYLINDRICAL:
{
x0 = alpha_x ;
x1 = beta_x ;
y0 = tan ( alpha_y ) ;
y1 = tan ( beta_y ) ;
break ;
}
case RECTILINEAR:
{
x0 = tan ( alpha_x ) ;
x1 = tan ( beta_x ) ;
y0 = tan ( alpha_y ) ;
y1 = tan ( beta_y ) ;
break ;
}
case STEREOGRAPHIC:
{
x0 = 2.0 * tan ( alpha_x / 2.0 ) ;
x1 = 2.0 * tan ( beta_x / 2.0 ) ;
y0 = 2.0 * tan ( alpha_y / 2.0 ) ;
y1 = 2.0 * tan ( beta_y / 2.0 ) ;
break ;
}
case CUBEMAP:
case BIATAN6:
{
x0 = tan ( alpha_x ) ;
x1 = tan ( beta_x ) ;
y0 = 6 * x0 ;
y1 = 6 * x1 ;
break ;
}
default:
{
x0 = x1 = y0 = y1 = 0.0 ;
break ;
}
}
return { x0 , x1 , y0 , y1 } ;
}