-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Taoning Wang
committed
Oct 23, 2023
1 parent
d978e89
commit c6811c8
Showing
6 changed files
with
161 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
from typing import Tuple | ||
from math import floor, sqrt, radians, sin, cos, tan, atan2, degrees, pi | ||
|
||
import pyradiance as pr | ||
|
||
def lrand(x: float) -> float: | ||
"""Psudo-random number generator.""" | ||
x *= 1.0/(1.0 + x*x) + 2.71828182845904 | ||
x += .785398163397447 - floor(x) | ||
x = 1e5 / x | ||
return x - floor(x); | ||
|
||
def vec_from_deg(theta: float, phi: float) -> Tuple[float, float, float]: | ||
theta = radians(theta) | ||
phi = radians(phi) | ||
v0 = v1 = sin(theta) | ||
v0 *= cos(phi) | ||
v1 *= sin(phi) | ||
v2 = round(cos(theta), 2) | ||
return v0, v1, v2 | ||
|
||
def deg_from_vec(x: float, y: float, z: float) -> Tuple[float, float]: | ||
theta = atan2(sqrt(x*x + y*y), z) | ||
phi = atan2(y, x) | ||
return degrees(theta), degrees(phi) | ||
|
||
|
||
def apex_to_solid_angle(apex: float) -> float: | ||
"""Convert apex angle to solid angle. | ||
Args: | ||
apex: Apex angle in radians. | ||
Returns: | ||
Solid angle in steradians. | ||
""" | ||
return 2 * pi * (1 - cos(apex)) | ||
|
||
# Input file | ||
xml_path = "t3.xml" | ||
|
||
# Cone radius | ||
max_gamma = 2.5 | ||
|
||
# Incident directions, theta, phi | ||
idirs = [ | ||
[0, 0], | ||
[10, 0], | ||
[20, 0], | ||
[30, 0], | ||
[40, 0], | ||
[50, 0], | ||
[60, 0], | ||
[70, 0], | ||
[80, 0], | ||
[87.5, 0], | ||
] | ||
|
||
# Number of samples per incident direction | ||
nsamps = 10000 | ||
|
||
# Incident direction vectors | ||
idirsn = [vec_from_deg(*d) for d in idirs] | ||
|
||
isdir_bundles = [] | ||
|
||
nsamps_root = sqrt(nsamps) | ||
max_gamma_r = radians(max_gamma) | ||
for id, d in enumerate(idirsn): | ||
isdirs = [] | ||
for ins in range(nsamps): | ||
recno = id * nsamps + ins + 1 | ||
og = max_gamma_r / nsamps_root * sqrt(ins+.5) | ||
refDx = -d[0] | ||
refDy = -d[1] | ||
refDz = -d[2] | ||
randDx = 1 - 2 * lrand(0.5813*recno-18.387) | ||
randDy = 1 - 2 * lrand(-7.3885*recno + 5.6192) | ||
randDz = 1 - 2 * lrand(3.57174*recno + 6.261943) | ||
upDx = randDy * refDz - randDz * refDy | ||
upDy = randDz * refDx - randDx * refDz | ||
upDz =randDx * refDy - randDy * refDx | ||
tanTn = tan(og) / sqrt(upDx**2+upDy**2+upDz**2+1e-10) | ||
sDx = refDx + tanTn * upDx | ||
sDy = refDy + tanTn * upDy | ||
sDz = refDz + tanTn * upDz | ||
isdirs.append([*idirs[id], *deg_from_vec(sDx, sDy, sDz)]) | ||
isdir_bundles.append(isdirs) | ||
|
||
assert len(isdir_bundles[0][0]) == 4 | ||
assert len(isdir_bundles[0]) == nsamps | ||
|
||
with pr.BSDF(xml_path) as sd: | ||
for bundle in isdir_bundles: | ||
sample_results = [sd.eval(*d)[1] for d in bundle] | ||
print(bundle[0][:2], sum(sample_results) / nsamps * apex_to_solid_angle(max_gamma_r)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
0.0 0.0, 1.0 | ||
0.17364817766693033 0.0, 0.98 | ||
0.3420201433256687 0.0, 0.94 | ||
0.49999999999999994 0.0, 0.87 | ||
0.6427876096865393 0.0, 0.77 | ||
0.766044443118978 0.0, 0.64 | ||
0.8660254037844386 0.0, 0.5 | ||
0.9396926207859083 0.0, 0.34 | ||
0.984807753012208 0.0, 0.17) | ||
0.9998476951563913 0.0, 0.02 | ||
1.0 0.0, 0.0 | ||
0.0 1.0, 0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
""" | ||
|
||
from pathlib import Path | ||
import math | ||
|
||
import pyradiance as pr | ||
|
||
def get_dir_dir(fpath: str | Path, idirs: list[list[float]]) -> float: | ||
if not isinstance(fpath, Path): | ||
fpath = Path(fpath) | ||
if fpath.suffix != '.xml' and fpath.suffix != ".sir": | ||
raise ValueError(f"File {fpath} must be an XML or SIR file") | ||
ndirs = len(idirs) | ||
nsamps: int = 10000 | ||
cnt = pr.cnt(ndirs, nsamps) | ||
pr.rcalc(cnt, source="", expr=rcalc_e) | ||
res = pr.bsdfquery() | ||
pr.total(res) | ||
res = pr.rcalc() | ||
return float(res.decode()) | ||
|
||
rcalc_e = ( | ||
'nf=1/sqrt(iDx($1+1)^2+iDy($1+1)^2+iDz($1+1)^2);' | ||
'inDx=nf*iDx($1+1);inDy=nf*iDy($1+1);inDz=nf*iDz($1+1);' | ||
'refDx=-inDx;refDy=-inDy;refDz=-inDz;' | ||
'randDx=1-2*rand(.5813*recno-18.387);randDy=1-2*rand(-7.3885*recno+5.6192);' | ||
'randDz=1-2*rand(3.57174*recno+6.261943);' | ||
'upDx=randDy*refDz-randDz*refDy;' | ||
'upDy=randDz*refDx-randDx*refDz;upDz=randDx*refDy-randDy*refDx' | ||
'tanTn=tan(og)/sqrt(upDx*upDx+upDy*upDy+upDz*upDz+1e-10)' | ||
'sDx=refDx+tanTn*upDx;sDy=refDy+tanTn*upDy;sDz=refDz+tanTn*upDz' | ||
'$1=inDx;$2=inDy;$3=inDz;$4=sDx;$5=sDy;$6=sDz' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pyradiance import lib | ||
|
||
|
||
lib.LIBRC.initotypes() | ||
lib.LIBRC.readoct(octname, loadflags, &thescene, NULL) | ||
|
||
lib.LIBRC.ray_init_pmap() | ||
lib.LIBRC.marksources() | ||
lib.LIBRC.setambient() | ||
|
||
lib.LIBRC.rtrace |