| 1 |
{ RCSid $Id: disk2square.cal,v 2.2 2014/07/22 23:21:56 greg Exp $ } |
| 2 |
{ |
| 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 |
rNx,rNy,rNz - 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 |
inc_dz = -Dx*rNx-Dy*rNy-Dz*rNz; |
| 77 |
inc_rx = -Dx*(Uy*rNz-Uz*rNy) - Dy*(Uz*rNx-Ux*rNz) - Dz*(Ux*rNy-Uy*rNx); |
| 78 |
inc_ry = -Dx*Ux-Dy*Uy-Dz*Uz - inc_dz*(rNx*Ux+rNy*Uy+rNz*Uz); |
| 79 |
inc_den2 = inc_rx*inc_rx + inc_ry*inc_ry; |
| 80 |
inc_radf = if(inc_den2-1e-7, sqrt((1 - inc_dz*inc_dz)/inc_den2), 0); |
| 81 |
{ 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 |
scbin = if(inc_dz, |
| 86 |
floor(out_square_x*SCdim)*SCdim + floor(out_square_y*SCdim), |
| 87 |
-1); |