1 |
greg |
2.2 |
{ RCSid $Id: disk2square.cal,v 2.1 2014/07/21 15:59:47 greg Exp $ } |
2 |
greg |
2.1 |
{ |
3 |
|
|
Convert between unit square and disk, using Shirley-Chiu mapping |
4 |
|
|
|
5 |
|
|
5/20/2011 G. Ward |
6 |
|
|
|
7 |
|
|
Inputs: |
8 |
|
|
in_disk_x Input unit disk Cartesian coordiantes |
9 |
|
|
in_disk_y center at (0,0) |
10 |
|
|
or: |
11 |
|
|
in_square_x Input unit square [0,1]^2 |
12 |
|
|
in_square_y |
13 |
|
|
|
14 |
|
|
Corresponding outputs: |
15 |
|
|
out_square_x Output unit square [0,1]^2 |
16 |
|
|
out_square_y |
17 |
|
|
or: |
18 |
|
|
out_disk_x Output unit disk Cartesian coordinates |
19 |
|
|
out_disk_y center at (0,0) |
20 |
|
|
} |
21 |
|
|
{ Compute disk position from square coordinates } |
22 |
|
|
in_square_a = 2*in_square_x - 1; |
23 |
|
|
in_square_b = 2*in_square_y - 1; |
24 |
|
|
|
25 |
|
|
in_square_rgn = if(in_square_a + in_square_b, |
26 |
|
|
if(in_square_a - in_square_b, 1, 2), |
27 |
|
|
if(in_square_b - in_square_a, 3, 4)); |
28 |
|
|
|
29 |
|
|
out_disk_r = select(in_square_rgn, in_square_a, in_square_b, |
30 |
|
|
-in_square_a, -in_square_b); |
31 |
|
|
|
32 |
|
|
out_disk_phi = PI/4 * select(in_square_rgn, |
33 |
|
|
in_square_b/in_square_a, |
34 |
|
|
2 - in_square_a/in_square_b, |
35 |
|
|
4 + in_square_b/in_square_a, |
36 |
|
|
if(in_square_b*in_square_b, |
37 |
|
|
6 - in_square_a/in_square_b, |
38 |
|
|
0)); |
39 |
|
|
|
40 |
|
|
out_disk_x = out_disk_r*cos(out_disk_phi); |
41 |
|
|
out_disk_y = out_disk_r*sin(out_disk_phi); |
42 |
|
|
|
43 |
|
|
{ Compute square position from disk coordinates } |
44 |
|
|
norm_radians(p) : if(-p - PI/4, p + 2*PI, p); |
45 |
|
|
in_disk_r = sqrt(in_disk_x*in_disk_x + in_disk_y*in_disk_y); |
46 |
|
|
in_disk_phi = norm_radians(atan2(in_disk_y, in_disk_x)); |
47 |
|
|
|
48 |
|
|
in_disk_rgn = floor((in_disk_phi + PI/4)/(PI/2)) + 1; |
49 |
|
|
|
50 |
|
|
out_square_a = select(in_disk_rgn, |
51 |
|
|
in_disk_r, |
52 |
|
|
(PI/2 - in_disk_phi)*in_disk_r/(PI/4), |
53 |
|
|
-in_disk_r, |
54 |
|
|
(in_disk_phi - 3*PI/2)*in_disk_r/(PI/4)); |
55 |
|
|
|
56 |
|
|
out_square_b = select(in_disk_rgn, |
57 |
|
|
in_disk_phi*in_disk_r/(PI/4), |
58 |
|
|
in_disk_r, |
59 |
|
|
(PI - in_disk_phi)*in_disk_r/(PI/4), |
60 |
|
|
-in_disk_r); |
61 |
|
|
|
62 |
|
|
out_square_x = (out_square_a + 1)/2; |
63 |
|
|
out_square_y = (out_square_b + 1)/2; |
64 |
|
|
{ |
65 |
|
|
The following forumulas compute Shirley-Chiu bin "scbin" based on: |
66 |
|
|
|
67 |
|
|
Dx,Dy,Dz - Incident direction (normalized, towards surface front) |
68 |
|
|
Nx,Ny,Nz - Surface normal (normalized, away from surface) |
69 |
|
|
Ux,Uy,Uz - Up direction vector (does not need to be normalized) |
70 |
|
|
|
71 |
|
|
The SCdim variable assigns the square side dimension for bins, which are |
72 |
|
|
ordered with the "up" direction changing fastest. |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
{ Compute oriented axis values } |
76 |
greg |
2.2 |
inc_dz = -Dx*Nx-Dy*Ny-Dz*Nz; |
77 |
greg |
2.1 |
inc_rx = -Dx*(Uy*Nz-Uz*Ny) - Dy*(Uz*Nx-Ux*Nz) - Dz*(Ux*Ny-Uy*Nx); |
78 |
greg |
2.2 |
inc_ry = -Dx*Ux-Dy*Uy-Dz*Uz - inc_dz*(Nx*Ux+Ny*Uy+Nz*Uz); |
79 |
greg |
2.1 |
inc_den2 = inc_rx*inc_rx + inc_ry*inc_ry; |
80 |
greg |
2.2 |
inc_radf = if(inc_den2-1e-7, sqrt((1 - inc_dz* inc_dz)/inc_den2), 0); |
81 |
greg |
2.1 |
{ Pass to formulas in first section } |
82 |
|
|
in_disk_x = inc_rx*inc_radf; |
83 |
|
|
in_disk_y = inc_ry*inc_radf; |
84 |
|
|
{ Compute final bin (-1 if behind surface) } |
85 |
greg |
2.2 |
scbin = if(inc_dz, |
86 |
greg |
2.1 |
floor(out_square_x*SCdim)*SCdim + floor(out_square_y*SCdim), |
87 |
|
|
-1); |