-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.h
1410 lines (1188 loc) · 46.6 KB
/
geometry.h
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* */
/* 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 header has all the geometrical transformations used to convert
// 2D coordinates in several projections to 3D 'ray' coordinates and
// back. To make this code as versatile as possible, the functors for
// the transformations also provide a scalar 'eval' variant - when used
// with zimt::process, this is not used, but it's nice to have.
// To test the code, there is a simple test program 'geometry.cc' which
// uses all the functors in both scalar and simdized form. The actual
// code for the transformations is largely from lux, except for the
// cubemap code which is largely from the envutil project itself.
#include "envutil_basic.h"
#include "zimt/zimt.h"
#if defined(ENVUTIL_GEOMETRY_H) == defined(HWY_TARGET_TOGGLE)
#ifdef ENVUTIL_GEOMETRY_H
#undef ENVUTIL_GEOMETRY_H
#else
#define ENVUTIL_GEOMETRY_H
#endif
HWY_BEFORE_NAMESPACE() ;
BEGIN_ZIMT_SIMD_NAMESPACE(project)
// zimt types for 2D and 3D coordinates and pixels
typedef zimt::xel_t < int , 2 > v2i_t ;
typedef zimt::xel_t < int , 3 > v3i_t ;
typedef zimt::xel_t < int , 2 > index_type ;
typedef zimt::xel_t < int , 2 > shape_type ;
typedef zimt::xel_t < float , 2 > v2_t ;
typedef zimt::xel_t < float , 3 > v3_t ;
// some SIMDized types we'll use. I use 16 SIMD lanes for now,
// which is also the lane count currently supported by OIIO.
#define LANES 16
typedef zimt::simdized_type < float , LANES > f_v ;
typedef zimt::simdized_type < int , LANES > i_v ;
typedef zimt::simdized_type < v2_t , LANES > crd2_v ;
typedef zimt::simdized_type < v2i_t , LANES > v2i_v ;
typedef zimt::simdized_type < v3i_t , LANES > v3i_v ;
typedef zimt::simdized_type < v3_t , LANES > crd3_v ;
typedef zimt::simdized_type < index_type , LANES > index_v ;
// coordinate transformations, coded as templates in zimt 'act'
// functor style, returning the result via a reference argument
// code to convert lat/lon to 3D ray. Note that I am using lux
// coordinate convention ('book order': x is right, y down and
// z forward). The lat/lon values coming in are angles in radians,
// and the resulting 'ray' coordinates are in 'model space' units.
// We're using an implicit radius of 1.0 for the conversion from
// spherical to cartesian coordinates, so the output has unit length.
template < typename T = float , std::size_t L = LANES >
struct ll_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
typedef zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L > base_t ;
using typename base_t::in_type ;
using typename base_t::in_ele_v ;
using typename base_t::in_v ;
using typename base_t::out_type ;
using typename base_t::out_v ;
void eval ( const in_v & in ,
out_v & out ) const
{
// incoming, we have lon/lat coordinates.
auto const & lon ( in[0] ) ;
auto const & lat ( in[1] ) ;
// outgoing, we have a directional vector.
auto & right ( out [ RIGHT ] ) ;
auto & down ( out [ DOWN ] ) ;
auto & forward ( out [ FORWARD ] ) ;
// we measure angles so that a view to directly ahead (0,0,1)
// corresponds to latitude and longitude zero. longitude increases
// into positive values when the view moves toward the right and
// latitude increases into positive values when the view moves
// downwards from the view straight ahead.
// The code benefits from using sincos, where available. Back-ends
// lacking direct sincos support will map to use of sin and cos.
in_ele_v sinlat , coslat , sinlon , coslon ;
sincos ( lat , sinlat , coslat ) ;
sincos ( lon , sinlon , coslon ) ;
// the x component, pointing to the right in lux, is zero at
// longitude zero, which is affected by the sine term. The
// cosine term affects a scaling of this 'raw' value which
// is one for latitude zero and decreases both ways.
right = sinlon * coslat ;
// The z component, pointing forward, is one at longitude and
// latitude zero, and decreases with both increasing and decreasing
// longitude and latitude.
forward = coslon * coslat ;
// The y component, pointing down, is zero for the view straight
// ahead and increases with the latitude. For latitudes above
// the equator, we'll see negative values, and positive values
// for views into the 'southern hemisphere'. This component is not
// affected by the longitude.
down = sinlat ;
}
void eval ( const in_type & in ,
out_type & out ) const
{
// incoming, we have lon/lat coordinates.
auto const & lon ( in[0] ) ;
auto const & lat ( in[1] ) ;
// outgoing, we have a directional vector.
auto & right ( out [ RIGHT ] ) ;
auto & down ( out [ DOWN ] ) ;
auto & forward ( out [ FORWARD ] ) ;
// we measure angles so that a view to directly ahead (0,0,1)
// corresponds to latitude and longitude zero. longitude increases
// into positive values when the view moves toward the right and
// latitude increases into positive values when the view moves
// downwards from the view straight ahead.
// The code benefits from using sincos, where available. Back-ends
// lacking direct sincos support will map to use of sin and cos.
T sinlat , coslat , sinlon , coslon ;
sincos ( lat , &sinlat , &coslat ) ;
sincos ( lon , &sinlon , &coslon ) ;
// the x component, pointing to the right in lux, is zero at
// longitude zero, which is affected by the sine term. The
// cosine term affects a scaling of this 'raw' value which
// is one for latitude zero and decreases both ways.
right = sinlon * coslat ;
// The z component, pointing forward, is one at longitude and
// latitude zero, and decreases with both increasing and decreasing
// longitude and latitude.
forward = coslon * coslat ;
// The y component, pointing down, is zero for the view straight
// ahead and increases with the latitude. For latitudes above
// the equator, we'll see negative values, and positive values
// for views into the 'southern hemisphere'. This component is not
// affected by the longitude.
down = sinlat ;
}
} ;
// code to move from 3D ray coordinates to lat/lon. This is the
// reverse operation to ll_to_ray above and follows the same
// conventions. Incoming, we have 3D ray coordinates, and
// outgoing, 2D lon/lat coordinates. Note how the use of atan2
// allow us to take rays in any scale.
// The output longitude is in [-pi, pi], and it's zero for the view
// 'straight ahead' in lux convention, which coincides with the
// center of the full spherical image. Values increase as the ray
// proceeds from left to right; the wrap-around point is on the
// 'back' axis.
// The output latitude is in [-pi/2,pi/2]. Negative values pertain
// to the upper hemisphere, the values increase as the ray proceeds
// from zenith to nadir.
template < typename T = float , std::size_t L = LANES >
struct ray_to_ll_t
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 2 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 3D directional vector
auto const & right ( in[RIGHT] ) ;
auto const & down ( in[DOWN] ) ;
auto const & forward ( in[FORWARD] ) ;
// outgoing, we have a 2D lat/lon coordinate.
auto & lon ( out[0] ) ;
auto & lat ( out[1] ) ;
auto s = sqrt ( right * right + forward * forward ) ;
lat = atan2 ( down , s ) ;
lon = atan2 ( right , forward ) ;
}
} ;
template < typename T = float , std::size_t L = LANES >
struct ll_to_px_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 2 > , L >
{
T scale ;
ll_to_px_t ( std::size_t _height )
: scale ( T ( _height ) / M_PI )
{ }
template < typename in_type , typename out_type >
void eval ( const in_type & in ,
out_type & out ) const
{
const zimt::xel_t < T , 2 > shift { M_PI , M_PI_2 } ;
out = ( in + shift ) * scale ;
out -= .5f ;
}
} ;
// more transformations from 3D ray coordinates to various
// 2D projections: rectilinear, cylindrical, stereographic
// and fisheye
template < typename T = float , std::size_t L = LANES >
struct ray_to_rect_t
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 2 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 3D directional vector
auto const & right ( in[RIGHT] ) ;
auto const & down ( in[DOWN] ) ;
auto const & forward ( in[FORWARD] ) ;
// outgoing, we have a 2D rectilinear coordinate.
auto & horizontal ( out[0] ) ;
auto & vertical ( out[1] ) ;
// dividing by the z coordinate projects the rays to a plane
// at unit distance. Note that we don't handle unsuitable input:
// forward == 0 will result in Inf values, and rays pointing
// to the 'back' hemisphere (negative z coordinate) will
// also produce output.
horizontal = right / forward ;
vertical = down / forward ;
}
} ;
// reverse operation, mapping a point on a plane to a 3D ray
// coordinate. The plane is taken to be at unit distance forward.
// The resulting 3D ray coordinate is not normalized.
template < typename T = float , std::size_t L = LANES >
struct rect_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 2D planar coordinate
auto const & horizontal ( in[0] ) ;
auto const & vertical ( in[1] ) ;
// outgoing, we have a 3D ray coordinate.
auto & right ( out[RIGHT] ) ;
auto & down ( out[DOWN] ) ;
auto & forward ( out[FORWARD] ) ;
right = horizontal ;
down = vertical ;
forward = 1 ;
}
} ;
template < typename T = float , std::size_t L = LANES >
struct ray_to_cyl_t
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 2 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 3D directional vector
auto const & right ( in[RIGHT] ) ;
auto const & down ( in[DOWN] ) ;
auto const & forward ( in[FORWARD] ) ;
// outgoing, we have a 2D lat/lon coordinate.
auto & lon ( out[0] ) ;
auto & lat ( out[1] ) ;
auto s = sqrt ( right * right + forward * forward ) ;
lat = down / s ;
lon = atan2 ( right , forward ) ;
}
} ;
// reverse operation
template < typename T = float , std::size_t L = LANES >
struct cyl_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 2D planar coordinate
auto const & horizontal ( in[0] ) ;
auto const & vertical ( in[1] ) ;
// outgoing, we have a 3D ray coordinate.
auto & right ( out[RIGHT] ) ;
auto & down ( out[DOWN] ) ;
auto & forward ( out[FORWARD] ) ;
// TODO: test
forward = cos ( horizontal ) ;
right = sin ( horizontal ) ;
down = vertical ;
}
} ;
template < typename T = float , std::size_t L = LANES >
struct ray_to_ster_t
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 2 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
auto reciprocal_norm = T ( 1 ) / sqrt ( in[0] * in[0]
+ in[1] * in[1]
+ in[2] * in[2] ) ;
// project 3D view ray to unit sphere surface by applying the norm
auto right = in[RIGHT] * reciprocal_norm ;
auto down = in[DOWN] * reciprocal_norm ;
auto forward = in[FORWARD] * reciprocal_norm ;
// 'factor' projects x and y to stereographic: x+1 puts us to the point
// on the sphere opposite the center of the view, and the 2.0 accounts for
// the fact that the plane is now 2u distant instead of just 1 when seen
// from the origin. If x gets very close to -1, we produce FLT_MAX as the
// result, which shoud be outside the valid range
auto factor = T ( 2 ) / ( forward + T ( 1 ) ) ;
out[0] = right * factor ;
// TODO: out[0] ( forward <= T ( -1 ) + FLT_EPSILON ) = FLT_MAX ;
out[1] = down * factor ;
// TODO: out[1] ( forward <= T ( -1 ) + FLT_EPSILON ) = FLT_MAX ;
}
} ;
// reverse operation
template < typename T = float , std::size_t L = LANES >
struct ster_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 2D planar coordinate
auto const & horizontal ( in[0] ) ;
auto const & vertical ( in[1] ) ;
// outgoing, we have a 3D ray coordinate.
auto & right ( out[RIGHT] ) ;
auto & down ( out[DOWN] ) ;
auto & forward ( out[FORWARD] ) ;
// TODO: test
auto r = sqrt ( horizontal * horizontal + vertical * vertical ) ;
auto theta = atan ( r / 2.0 ) * 2.0f ;
auto phi = atan2 ( horizontal , - vertical ) ;
forward = cos ( theta ) ;
down = - sin ( theta ) * cos ( phi ) ;
right = sin ( theta ) * sin ( phi ) ;
}
} ;
template < typename T = float , std::size_t L = LANES >
struct ray_to_fish_t
: public zimt::unary_functor
< zimt::xel_t < T , 3 > , zimt::xel_t < T , 2 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
auto const & right ( in[RIGHT] ) ;
auto const & down ( in[DOWN] ) ;
auto const & forward ( in[FORWARD] ) ;
auto s = sqrt ( right * right + down * down ) ;
auto r = T ( M_PI_2 ) - atan2 ( forward , s ) ;
auto phi = atan2 ( down , right ) ;
out[0] = r * cos ( phi ) ;
out[1] = r * sin ( phi ) ;
}
} ;
// reverse operation
template < typename T = float , std::size_t L = LANES >
struct fish_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
template < typename in_type , typename out_type >
static void eval ( const in_type & in ,
out_type & out )
{
// incoming, we have a 2D planar coordinate
auto const & horizontal ( in[0] ) ;
auto const & vertical ( in[1] ) ;
// outgoing, we have a 3D ray coordinate.
auto & right ( out[RIGHT] ) ;
auto & down ( out[DOWN] ) ;
auto & forward ( out[FORWARD] ) ;
// TODO: test
auto r = sqrt ( horizontal * horizontal + vertical * vertical ) ;
auto phi = atan2 ( horizontal , - vertical ) ;
forward = cos ( r ) ;
down = - sin ( r ) * cos ( phi ) ;
right = sin ( r ) * sin ( phi ) ;
}
} ;
// this functor template converts incoming in-face coordinates
// to ray coordinates for a given face index, which is passed
// as a template argument - so the sixfold 'if constexpr ...' is
// not a conditional, it's just a handy way of putting the code
// into a single function without having to write partial template
// specializations for the six possible face indices.
// currently unused.
template < face_index_t F , typename T = float , std::size_t L = LANES >
struct in_face_to_ray
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
// incoming, we have a 2D in-face coordinate in model space
// units. For a cubemap with no additional support, the in-face
// coordinates are in the range [-1,1]. Maybe the naming is
// slightly misleading, because the incoming coordinates may
// well come from outside the 'cube face proper', but they
// are on the given cube face's plane. The template
// argument F fixes the face for which the calculation is
// performed. To create the ray, a third component is needed,
// which is added with untit magnitude. The resulting 3D ray
// coordinate is not normalized.
template < typename I , typename O >
static void eval ( const I & crd2 , O & crd3 )
{
if constexpr ( F == CM_FRONT )
{
crd3[RIGHT] = crd2[RIGHT] ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = 1.0f ;
}
else if constexpr ( F == CM_BACK )
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = - 1.0f ;
}
else if constexpr ( F == CM_RIGHT )
{
crd3[RIGHT] = 1.0f ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = - crd2[RIGHT] ;
}
else if constexpr ( F == CM_LEFT )
{
crd3[RIGHT] = - 1.0f ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = crd2[RIGHT] ;
}
// for bottom and top, note that we're using openEXR convention.
// to use lux convention, invert the signs.
else if constexpr ( F == CM_BOTTOM )
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = 1.0f ;
crd3[FORWARD] = crd2[DOWN] ;
}
else if constexpr ( F == CM_TOP )
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = - 1.0f ;
crd3[FORWARD] = - crd2[DOWN] ;
}
}
} ;
// this functor converts incoming 2D coordinates pertaining
// to the entire IR image to 3D ray coordinates in lux convention.
// This functor can serve to populate the IR image: set up a
// functor yielding model space coordinates pertaining to pixels
// in the IR image, pass these model space coordinates to this
// functor, receive ray coordinates, then glean pixel values
// for the given ray by evaluating some functor taking ray
// coordinates and yielding pixels.
// The c'tor takes two arguments: first the 'section size':
// this is equal to the IR image's width, expressed in model
// space units. If the IR image does not have additional support
// and holds cube face images of precisely ninety degrees fov,
// the value would be 2.0 precisely. With added support, it's
// slightly larger. The second argument is the distance, in model
// space units, from the upper left corner of a section to the
// cube face image's center. If the cube face image has even width,
// this is precisely half the section size, but with odd width,
// this isn't possible, hence the extra argument.
// To accept UL-base coordinates, instantiate with
// use_centered_coordinates false.
template < typename T = float ,
std::size_t L = LANES ,
bool use_centered_coordinates = true >
struct ir_to_ray_t
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
typedef zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L > base_t ;
using typename base_t::in_type ;
using typename base_t::in_ele_v ;
using typename base_t::in_v ;
using typename base_t::out_type ;
using typename base_t::out_v ;
const double section_md ;
const double refc_md ;
const zimt::xel_t < T , 2 > ul2c ;
ir_to_ray_t ( double _section_md = 2.0 , double _refc_md = 1.0 )
: section_md ( _section_md ) ,
refc_md ( _refc_md ) ,
ul2c { _refc_md , 3 * _section_md }
{ }
// incoming, we have 2D model space coordinates.
// This functor takes centered planar cordinates - note the
// addition of ul2c at the beginning of the eval functions, which
// converts the coordinates from to center-based to ul-based for
// processing.
void eval ( const in_v & _crd2 , out_v & crd3 ) const
{
in_v crd2 ( _crd2 ) ;
if constexpr ( use_centered_coordinates )
crd2 += ul2c ;
// The numerical constants for the cube faces/sections are set
// up so that a simple division of the y coordinate yields the
// corresponding section index.
i_v section ( crd2[1] / section_md ) ;
// The incoming coordinates are relative to the upper left
// corner of the IR image. Now we move to in-face coordinates,
// which are centered on the cube face we're dealing with.
crd2[1] -= section * section_md ;
crd2 -= refc_md ;
// the section number can also yield the 'dominant' axis
// by dividing the value by two (another property which is
// deliberate):
i_v dom ( section >> 1 ) ;
// again we use a conditional to avoid lengthy calculations
// when there aren't any populated lanes for the given predicate
if ( any_of ( dom == 0 ) )
{
auto m = ( section == CM_RIGHT ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = 1.0f ;
crd3[DOWN](m) = crd2[DOWN] ;
crd3[FORWARD](m) = - crd2[RIGHT] ;
}
m = ( section == CM_LEFT ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = - 1.0f ;
crd3[DOWN](m) = crd2[DOWN] ;
crd3[FORWARD](m) = crd2[RIGHT] ;
}
}
if ( any_of ( dom == 1 ) )
{
auto m = ( section == CM_BOTTOM ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = - crd2[RIGHT] ;
crd3[DOWN](m) = 1.0f ;
crd3[FORWARD](m) = crd2[DOWN] ;
}
m = ( section == CM_TOP ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = - crd2[RIGHT] ;
crd3[DOWN](m) = - 1.0f ;
crd3[FORWARD](m) = - crd2[DOWN] ;
}
}
if ( any_of ( dom == 2 ) )
{
auto m = ( section == CM_FRONT ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = crd2[RIGHT] ;
crd3[DOWN](m) = crd2[DOWN] ;
crd3[FORWARD](m) = 1.0f ;
}
m = ( section == CM_BACK ) ;
if ( any_of ( m ) )
{
crd3[RIGHT](m) = - crd2[RIGHT] ;
crd3[DOWN](m) = crd2[DOWN] ;
crd3[FORWARD](m) = - 1.0f ;
}
}
}
// for completeness' sake, the scalar eval
void eval ( const in_type & _crd2 , out_type & crd3 ) const
{
in_type crd2 ( _crd2 ) ;
if constexpr ( use_centered_coordinates )
crd2 += ul2c ;
// The numerical constants for the cube faces/sections are set
// up so that a simple division of the y coordinate yields the
// corresponding section index.
int section ( crd2[1] / section_md ) ;
// The incoming coordinates are relative to the upper left
// corner of the IR image. Now we move to in-face coordinates,
// which are centered on the cube face we're dealing with.
crd2[1] -= section * section_md ;
crd2 -= refc_md ;
// the section number can also yield the 'dominant' axis
// by dividing the value by two (another property which is
// deliberate):
int dom ( section >> 1 ) ;
// again we use a conditional to avoid lengthy calculations
// when there aren't any populated lanes for the given predicate
if ( dom == 0 )
{
if ( section == CM_RIGHT )
{
crd3[RIGHT] = 1.0f ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = - crd2[RIGHT] ;
}
else
{
crd3[RIGHT] = - 1.0f ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = crd2[RIGHT] ;
}
}
else if ( dom == 1 )
{
if ( section == CM_BOTTOM )
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = 1.0f ;
crd3[FORWARD] = crd2[DOWN] ;
}
else
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = - 1.0f ;
crd3[FORWARD] = - crd2[DOWN] ;
}
}
else
{
if ( section == CM_FRONT )
{
crd3[RIGHT] = crd2[RIGHT] ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = 1.0f ;
}
else
{
crd3[RIGHT] = - crd2[RIGHT] ;
crd3[DOWN] = crd2[DOWN] ;
crd3[FORWARD] = - 1.0f ;
}
}
}
} ;
// to make the conversion efficient and transparent, I refrain from
// using ir_to_ray and a subsequent coordinate transformation in
// favour of this dedicated functor, which is basically a copy of
// the one above, but with different component indexes and signs
// inverted where necessary (namely for the left and up direction,
// which are the negative of zimt's right and down).
// currently unused.
/*
template < typename T = float , std::size_t L = LANES >
struct ir_to_exr
: public zimt::unary_functor
< zimt::xel_t < T , 2 > , zimt::xel_t < T , 3 > , L >
{
const double section_md ;
const double refc_md ;
ir_to_exr ( double _section_md , double _refc_md )
: section_md ( _section_md ) ,
refc_md ( _refc_md )
{ }
// incoming, we have 2D model space coordinates, with the origin
// at the (total!) IR image's upper left corner.
template < typename I , typename O >
void eval ( const I & _crd2 , O & crd3 ) const
{
I crd2 ( _crd2 ) ;
// The numerical constants for the cube faces/sections are set
// up so that a simple division of the y coordinate yields the
// corresponding section index.
i_v section ( crd2[1] / section_md ) ;
// The incoming coordinates are relative to the upper left
// corner of the IR image. Now we move to in-face coordinates,
// which are centered on the cube face we're dealing with.
crd2[1] -= section * section_md ;
crd2 -= refc_md ;
// the section number can also yield the 'dominant' axis
// by dividing the value by two (another property which is
// deliberate):
i_v dom ( section >> 1 ) ;
// again we use a conditional to avoid lengthy calculations
// when there aren't any populated lanes for the given predicate
if ( any_of ( dom == 0 ) )
{
auto m = ( section == CM_RIGHT ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = - 1.0f ;
crd3[EXR_UP](m) = - crd2[DOWN] ;
crd3[EXR_FORWARD](m) = - crd2[RIGHT] ;
}
m = ( section == CM_LEFT ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = 1.0f ;
crd3[EXR_UP](m) = - crd2[DOWN] ;
crd3[EXR_FORWARD](m) = crd2[RIGHT] ;
}
}
if ( any_of ( dom == 1 ) )
{
auto m = ( section == CM_BOTTOM ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = crd2[RIGHT] ;
crd3[EXR_UP](m) = - 1.0f ;
crd3[EXR_FORWARD](m) = crd2[DOWN] ;
}
m = ( section == CM_TOP ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = crd2[RIGHT] ;
crd3[EXR_UP](m) = 1.0f ;
crd3[EXR_FORWARD](m) = - crd2[DOWN] ;
}
}
if ( any_of ( dom == 2 ) )
{
auto m = ( section == CM_FRONT ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = - crd2[RIGHT] ;
crd3[EXR_UP](m) = - crd2[DOWN] ;
crd3[EXR_FORWARD](m) = 1.0f ;
}
m = ( section == CM_BACK ) ;
if ( any_of ( m ) )
{
crd3[EXR_LEFT](m) = crd2[RIGHT] ;
crd3[EXR_UP](m) = - crd2[DOWN] ;
crd3[EXR_FORWARD](m) = - 1.0f ;
}
}
}
} ;
*/
// given a 3D 'ray' coordinate, find the corresponding cube face
// and the in-face coordinate - note the two references which take
// the result values. The incoming 'ray' coordinate does not have
// to be normalized. The resulting in-face coordinates are in the
// range of [-1,1] - in 'model space units' pertaining to planes
// 'draped' at unit distance from the origin and perpendicular
// to one of the axes.
// Note how the results of this functor can be fed to one of the
// get_pickup_coordinate_... functions provided in metrics.h to
// get 2D coordinates pertaining to a cubemap's IR representation.
// first, the simdized version:
template < typename T , std::size_t L >
void ray_to_cubeface
( const zimt::xel_t < zimt::simdized_type < T , L > , 3 > & c ,
zimt::simdized_type < int , L > & face ,
zimt::xel_t < zimt::simdized_type < T , L > , 2 > & in_face )
{
// form three masks with relations of the numerical values of
// the 'ray' coordinate. These are sufficient to find out which
// component has the largest absolute value (the 'dominant' one,
// along the 'dominant' axis)
auto m1 = ( abs ( c[RIGHT] ) >= abs ( c[DOWN] ) ) ;
auto m2 = ( abs ( c[RIGHT] ) >= abs ( c[FORWARD] ) ) ;
auto m3 = ( abs ( c[DOWN] ) >= abs ( c[FORWARD] ) ) ;
// form a mask which is true where a specific axis is 'dominant'.
// We start out looking at the x axis: where the numerical value
// along the x axis (pointing right) is larger than the other two,
// 'dom' will be true.
auto dom = m1 & m2 ;
// Now we can assign face indexes, stored in f. If the coordinate
// value is negative along the dominant axis, we're looking at
// the face opposite and assign a face index one higher. Note
// how this SIMD code does the job for LANES coordinates in
// parallel, avoiding conditionals and using masking instead.
// we also find in-face coordinates:
// we divide the two non-dominant coordinate values by the
// dominant one. One of the axes comes out just right when
// dividing by the absolute value - e.g. the vertical axis
// points downwards for all the four cube faces around
// the center. The other axis is divided by the 'major'
// coordinate value as-is; the resulting coordinate runs
// one way for positive major values and backwards for
// negative ones. Note that we might capture the absolute
// values (which we've used before) in variables, but the
// compiler will recognize the common subexpressions and
// do it for us. While it's generally preferable to avoid
// conditionals in inner-loop code, I use conditionals here
// because most of the time all coordinates will 'land' in
// the same cube face, so for two cases, the rather expensive
// code to calculate the face index and in-face coordinate
// can be omitted. TODO: One might test whether omitting the
// conditionals is actually slower.
if ( any_of ( dom ) )
{
// extract in-face coordinates for the right and left cube
// face. the derivation of the x coordinate uses opposites for
// the two faces, the direction of the y coordinate is equal.
// Note that some lanes in c[RIGHT] may be zero and result in
// an Inf result, but these will never be the ones which end
// up in the result, because only those where c[RIGHT] is
// 'dominant' will 'make it through', and where c[RIGHT] is
// dominant, it's certainly not zero. But we rely on the
// system not to throw a division-by-zero exception, which
// would spoil our scheme.
face = CM_RIGHT ;
face ( dom & ( c[RIGHT] < 0 ) ) = CM_LEFT ;
in_face[0] ( dom ) = - c[FORWARD] / c[RIGHT] ;
in_face[1] ( dom ) = c[DOWN] / abs ( c[RIGHT] ) ;
}
// set dom true where the z axis (pointing forward) has the
// largest numerical value
dom = ( ! m2 ) & ( ! m3 ) ;
if ( any_of ( dom ) )
{
// test for front and back faces
face ( dom ) = CM_FRONT ;
face ( dom & ( c[FORWARD] < 0 ) ) = CM_BACK ;
in_face[0] ( dom ) = c[RIGHT] / c[FORWARD] ;
in_face[1] ( dom ) = c[DOWN] / abs ( c[FORWARD] ) ;
}
// now set dom true where the y axis (pointing down) has the
// largest numerical value