Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #6

Merged
merged 9 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyradiance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)

# from .lib import RadianceAPI
from .lib import BSDF, read_rad, get_view_resolu
from .lib import ABASELIST, BSDF, read_rad, get_view_resolu

from .model import (
Sensor,
Expand Down Expand Up @@ -95,6 +95,7 @@
os.environ["PATH"] = str(BINPATH) + os.pathsep + os.environ["PATH"]

__all__ = [
"ABASELIST",
"BSDF",
"bsdf2klems",
"bsdf2ttree",
Expand Down
47 changes: 41 additions & 6 deletions pyradiance/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from math import radians, sin, cos
import os
from random import randint
import sys
import tempfile
from typing import List, Optional, Tuple

from .model import Primitive, View, Resolu
Expand Down Expand Up @@ -113,6 +115,24 @@
"-Y-X",
]

# bsdf_m.h
MAXLATS = 46
MAXBASES = 7

class _Lat(Structure):
_fields_ = [
("tmin", c_float),
("nphis", c_int),
]


class _AngleBasis(Structure):
_fields_ = [
("name", c_char * 64),
("nangles", c_int),
("lat", _Lat * (MAXLATS + 1)),
]


class C_COLOR(Structure):
_fields_ = [
Expand Down Expand Up @@ -154,7 +174,7 @@ class OBJREC(Structure):
]


class VIEW(Structure):
class _View(Structure):
_fields_ = [
("type", c_char),
("vp", FVECT),
Expand Down Expand Up @@ -250,10 +270,13 @@ class SDData(Structure):
LIBRC.readobj.restype = POINTER(OBJREC)
LIBRC.freeobjects.argtypes = [c_int, c_int]
LIBRC.freeobjects.restype = None
LIBRC.viewfile.argtypes = [c_char_p, POINTER(VIEW), POINTER(RESOLU)]
LIBRC.viewfile.argtypes = [c_char_p, POINTER(_View), POINTER(RESOLU)]
LIBRC.viewfile.restype = None


ABASELIST = (_AngleBasis * MAXBASES).in_dll(LIBRC, "abase_list")


def vec_from_deg(theta: float, phi: float) -> Tuple[float, float, float]:
theta = radians(theta)
phi = radians(phi)
Expand Down Expand Up @@ -426,7 +449,7 @@ def samples(self, theta: float, phi: float, nsamp: int, sflag: str) -> Tuple[Lis
return vecs, values


def read_rad(*paths: str):
def read_rad(*paths: str, inbytes=None):
"""
Read Radiance files and return a list of Primitives. Files order matters.
Args:
Expand All @@ -437,8 +460,16 @@ def read_rad(*paths: str):
>>> import pyradiance as pr
>>> pr.read_rad("scene.rad")
"""
for path in paths:
LIBRC.readobj(path.encode("ascii"))

if inbytes is None:
for path in paths:
LIBRC.readobj(path.encode("ascii"))
else:
# slow
with tempfile.NamedTemporaryFile() as f:
f.write(inbytes)
f.flush()
LIBRC.readobj(f.name.encode("ascii"))
objblocks = (POINTER(OBJREC) * 131071).in_dll(LIBRC, "objblock")
nobjects = c_int.in_dll(LIBRC, "nobjects").value
primitives = []
Expand Down Expand Up @@ -473,7 +504,7 @@ def get_view_resolu(path) -> Tuple[View, Resolu]:
>>> import pyradiance as pr
>>> pr.get_view_resolu("view.vf")
"""
_view = VIEW()
_view = _View()
_res = RESOLU()
LIBRC.viewfile(path.encode(), byref(_view), byref(_res))
view = View(
Expand All @@ -488,6 +519,10 @@ def get_view_resolu(path) -> Tuple[View, Resolu]:
vfore=_view.vfore,
vaft=_view.vaft,
vdist=_view.vdist,
hvec=(_view.hvec[0], _view.hvec[1], _view.hvec[2]),
vvec=(_view.vvec[0], _view.vvec[1], _view.vvec[2]),
hn2=_view.hn2,
vn2=_view.vn2,
)
resolu = Resolu(ORIENT_FLAG[_res.rp], _res.xr, _res.yr)
return view, resolu
4 changes: 4 additions & 0 deletions pyradiance/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class View:
vfore: float = 0
vaft: float = 0
vdist: float = 0
hvec: Tuple[float, float, float] = (0, 0, 0)
vvec: Tuple[float, float, float] = (0, 0, 0)
hn2: float = 0
vn2: float = 0

def args(self):
return [
Expand Down
30 changes: 0 additions & 30 deletions pyradiance/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,6 @@
BINPATH = Path(__file__).parent / "bin"


# def build_scene(scene: Scene):
# """Build and write the scene octree file.
# to the current working directory.
# """
# if not scene.changed:
# print("Scene has not changed since last build.")
# return
# scene.changed = False
# stdin = None
# mstdin = [
# str(mat) for mat in scene.materials.values() if isinstance(mat, Primitive)
# ]
# inp = [mat for mat in scene.materials.values() if isinstance(mat, str)]
# if mstdin:
# stdin = "".join(mstdin).encode()
# moctname = f"{scene.sid}mat.oct"
# with open(moctname, "wb") as wtr:
# wtr.write(oconv(*inp, warning=False, stdin=stdin))
# sstdin = [str(srf) for srf in scene.surfaces.values() if isinstance(srf, Primitive)]
# sstdin.extend(
# [str(src) for src in scene.sources.values() if isinstance(src, Primitive)]
# )
# inp = [path for path in scene.surfaces.values() if isinstance(path, str)]
# inp.extend([path for path in scene.sources.values() if isinstance(path, str)])
# if sstdin:
# stdin = "".join(sstdin).encode()
# with open(f"{scene.sid}.oct", "wb") as wtr:
# wtr.write(oconv(*inp, stdin=stdin, warning=False, octree=moctname))


def dctimestep(
*mtx,
nstep: Optional[int] = None,
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@
"objblock",
"nobjects",
"freeobjects",
"viewfile"
"viewfile",
"abase_list",
]

class PyradianceBDistWheel(bdist_wheel):
Expand Down Expand Up @@ -472,7 +473,7 @@ def get_ext_filename(self, ext_name):
name="pyradiance",
author="LBNL",
author_email="taoningwang@lbl.gov",
version="0.0.6a1",
version="0.0.7a1",
description="Python interface for Radiance command-line tools",
long_description=Path("README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
Expand All @@ -492,7 +493,7 @@ def get_ext_filename(self, ext_name):
py_modules=["pyradiance.lib"],
ext_modules=[
CTypesExtension(
name="pyradiance.libraycalls",
name="pyradiance.libraycalls",
include_dirs=["Radiance/src/common", "Radiance/src/rt"],
sources=csources,
),
Expand Down
15 changes: 7 additions & 8 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ def test_get_view_resolu():

def test_BSDF():
"""Test the bsdf function."""
bsdf = pr.BSDF("./Resources/t3.xml")
_t = bsdf.direct_hemi(30, 0, "t")
assert pytest.approx(_t, 0.0001) == 7.6456e-2
_a = bsdf.size(30, 0)
assert pytest.approx(_a[0], 0.0001) == 7.6699e-4
x, y, z = bsdf.eval(0, 0, 180, 0)
assert pytest.approx(y, 0.001) == 4.997

with pr.BSDF("./Resources/t3.xml") as bsdf:
_t = bsdf.direct_hemi(30, 0, "t")
assert pytest.approx(_t, 0.0001) == 7.6456e-2
_a = bsdf.size(30, 0)
assert pytest.approx(_a[0], 0.0001) == 7.6699e-4
_sv = bsdf.eval(0, 0, 180, 0)
assert pytest.approx(_sv[1], 0.001) == 4.997