-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvutil_basic.cc
410 lines (377 loc) · 11.8 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/************************************************************************/
/* */
/* 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 */
/* */
/* kfjahnke+envutil@gmail.com */
/* */
/* 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:
{
step = atan ( 2.0 * tan ( hfov / 2.0 ) / width ) ;
break ;
}
case BIATAN6:
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 } ;
}
// polygon filling code gleaned from:
// http://alienryderflex.com/polygon_fill/
// where a C version is available in the public domain.
// thanks :D
// I modified the code to take the winding order into account and produce
// polygon filling behaviour like panotools: if the polygon self-intersects,
// the intersections are also filled.
void fill_polygon ( const std::vector<float> & px ,
const std::vector<float> & py ,
int IMAGE_LEFT , int IMAGE_TOP ,
int IMAGE_RIGHT , int IMAGE_BOT ,
std::function < void ( int , int ) > fillPixel )
{
int N = px.size() ;
assert ( px.size() == py.size() ) ;
int nodes, nodeX[N], dir[N] ,pixelX, pixelY, i, j, swap ;
// Loop through the rows of the image.
for (int pixelY=IMAGE_TOP; pixelY<IMAGE_BOT; pixelY++)
{
// Build a list of nodes.
nodes=0; j=N-1;
for (i=0; i<N; i++)
{
// in addition to testing for the crossing, we also take note
// of the direction of the crossing: are we passing into the
// edge 'from the left' or 'from the right'?
int cross = 0 ;
if ( py[i]<(float) pixelY && py[j]>=(float) pixelY )
cross = 1 ;
else if ( py[j]<(float) pixelY && py[i]>=(float) pixelY)
cross = -1 ;
if ( cross )
{
nodeX[nodes]=(int) (px[i]+(pixelY-py[i])/(py[j]-py[i])
*(px[j]-px[i]));
dir[nodes++] = cross ;
}
j=i;
}
// Sort the nodes, via a simple “Bubble” sort.
// extended to also sort the crossing information
i=0 ;
while (i<nodes-1)
{
if (nodeX[i]>nodeX[i+1])
{
swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap;
swap=dir[i]; dir[i]=dir[i+1]; dir[i+1]=swap;
if (i) i--;
}
else
{
i++;
}
}
// Fill the pixels between node pairs if the winding order isn't zero.
// We obtain the winding order by cumulating the 'cross' values.
int w_ord = 0 ;
for (i=0; i<nodes; i++)
{
w_ord += dir[i] ;
// if the winding order is zero, skip to the next segment
if ( ! w_ord )
continue ;
// otherwise, the algorithm progresses as in the variant using
// 'alternate filling'
if ( nodeX[i] >= IMAGE_RIGHT )
break ;
if ( nodeX[i+1] > IMAGE_LEFT )
{
if ( nodeX[i] < IMAGE_LEFT )
nodeX[i] = IMAGE_LEFT ;
if ( nodeX[i+1] > IMAGE_RIGHT )
nodeX[i+1] = IMAGE_RIGHT ;
for ( pixelX = nodeX[i] ; pixelX < nodeX[i+1] ; pixelX++)
fillPixel(pixelX,pixelY);
}
}
}
}
// simple tokenizer: the input is split into words separated by white
// space. Single and double quotes are recognized and quoted strings
// may contain white space. Inside quotes, the quote sign can be
// carried into the token if it's preceded by a backslash.
// This was fun: it's a good old finite automaton :D
std::vector < std::string > tokenize ( const std::string input )
{
std::vector < std::string > result ;
enum { NO_TOKEN , IN_TOKEN , IN_Q } ;
auto state = NO_TOKEN ;
const char * cp = input.c_str() ;
std::string token ;
char quote ;
while ( *cp )
{
switch ( state )
{
case NO_TOKEN:
{
switch ( *cp )
{
case ' ' :
case '\t' :
case '\n' :
case '\r' :
break ;
case '"' :
case '\'' :
state = IN_Q ;
quote = *cp ;
break ;
default:
state = IN_TOKEN ;
token += *cp ;
break ;
}
break ;
}
case IN_TOKEN:
{
switch ( *cp )
{
case ' ' :
case '\t' :
case '\n' :
case '\r' :
result.push_back ( token ) ;
token.clear() ;
state = NO_TOKEN ;
break ;
case '"' :
case '\'' :
state = IN_Q ;
quote = *cp ;
break ;
default:
token += *cp ;
break ;
}
break ;
}
case IN_Q:
{
if ( *cp == quote )
{
state = IN_TOKEN ;
break ;
}
switch ( *cp )
{
case '\\' :
if ( *(cp+1) == quote )
++cp ;
default:
token += *cp ;
break ;
}
break ;
}
}
++cp ;
}
if ( token != std::string() )
result.push_back ( token ) ;
return result ;
}