Loading...
Searching...
No Matches
py_core.h
Go to the documentation of this file.
1#ifndef _BBM_PY_CORE_H_
2#define _BBM_PY_CORE_H_
3
4#include "pybind11/pybind11.h"
5#include "pybind11/operators.h"
6
7namespace py = pybind11;
8
9#include "bbm/vec3dpair.h"
10#include "bbm/bsdfsample.h"
11#include "bbm/bsdf_ptr.h"
12#include "bbm/aggregatebsdf.h"
13#include "bbm/bsdf_enumerate.h"
14#include "util/vector_util.h"
15
16#include "python/py_fresnel.h"
17
18/***********************************************************************/
19/*! \file py_core.h
20 \brief Export of core classes to python
21************************************************************************/
22
23namespace bbm {
24
25 namespace python {
26
27 /*******************************************************************/
28 /*! \brief Define the Python interface for BBM core classes
29
30 \tparam CONF = BBM configuration
31 \param m = Python module
32
33 Exports:
34 + Ior
35 + Reflectance
36 + BsdfSample
37 + BsdfPtr
38 + AggregateBsdf
39 + BsdfFlag
40
41 All BSDFs are defined as a BsdfPtr in python.
42 ********************************************************************/
43 template<typename CONF>
44 void core(py::module& m)
45 {
46 BBM_IMPORT_CONFIG( CONF );
47 using BsdfPtr = bsdf_ptr<Config>;
48
49 // Export bsdf_flag
50 py::enum_<bsdf_flag>(m, "bsdf_flag")
51 .value("None", bsdf_flag::None)
52 .value("Diffuse", bsdf_flag::Diffuse)
53 .value("Specular", bsdf_flag::Specular)
54 .value("All", bsdf_flag::All)
55 .def(py::self | py::self)
56 .def(py::self & py::self)
57 .def(py::self ^ py::self)
58 .def(~py::self)
59 .def("__str__", [](const bsdf_flag& flag) { return bbm::toString(flag); });
60
61 // Export bsdf_attr flag
62 py::enum_<bsdf_attr>(m, "bsdf_attr")
63 .value("None", bsdf_attr::None)
64 .value("DiffuseScale", bsdf_attr::DiffuseScale)
65 .value("DiffuseParameter", bsdf_attr::DiffuseParameter)
66 .value("SpecularScale", bsdf_attr::SpecularScale)
67 .value("SpecularParameter", bsdf_attr::SpecularParameter)
68 .value("Dependent", bsdf_attr::Dependent)
69 .value("Diffuse", bsdf_attr::Diffuse)
70 .value("Specular", bsdf_attr::Specular)
71 .value("Scale", bsdf_attr::Scale)
72 .value("Parameter", bsdf_attr::Parameter)
73 .value("All", bsdf_attr::All)
74 .def(py::self | py::self)
75 .def(py::self & py::self)
76 .def(py::self ^ py::self)
77 .def(~py::self)
78 .def("__str__", [](const bsdf_attr& flag) { return bbm::toString(flag); });
79
80 // Export unit_t
81 py::enum_<unit_t>(m, "unit_t")
82 .value("Radiance", unit_t::Radiance)
83 .value("Importance", unit_t::Importance)
84 .def("__str__", [](const unit_t& unit) { return bbm::toString(unit); });
85
86 // Export IOR
87 fresnel<Value>(m);
88 fresnel<Spectrum>(m, "Spectral");
89 complex_fresnel<Value>(m);
90 complex_fresnel<Spectrum>(m, "Spectral");
91
92 // Export BsdfSample
93 py::class_<BsdfSample>(m, "BsdfSample")
94 .def_readonly("direction", &BsdfSample::direction, "Sampled direction")
95 .def_readonly("pdf", &BsdfSample::pdf, "PDF of the sampled direction")
96 .def_readonly("flag", &BsdfSample::flag, "Type of the sampled direction")
97 .def("__str__", [](const BsdfSample& sample) { return bbm::toString(sample); });
98
99 // Export Vec3dPair
100 py::class_<Vec3dPair>(m, "Vec3dPair")
101 .def_readonly("in", &Vec3dPair::in, "Incident direction")
102 .def_readonly("out", &Vec3dPair::out, "Outgoing direction")
103 .def("__str__", [](const Vec3dPair& sample) { return bbm::toString(sample); });
104
105 // Export BsdfPtr
106 py::class_<BsdfPtr>(m, "BsdfPtr")
107 .def(py::init<const BsdfPtr &>())
108 .def("eval", [](const BsdfPtr& self, const Vec3d& in, const Vec3d& out, BsdfFlag component=bsdf_flag::All, unit_t unit=unit_t::Radiance, Mask mask=true) { return self.eval(in,out,component,unit,mask); }, "Given an 'in' and 'out' direction, return the radiance/importance of the BSDF", py::arg("in"), py::arg("out"), py::arg_v("component", bsdf_flag::All), py::arg_v("unit", unit_t::Radiance), py::arg_v("mask", true))
109 .def("sample", [](const BsdfPtr& self, const Vec3d& out, const Vec2d& xi, BsdfFlag component=bsdf_flag::All, unit_t unit=unit_t::Radiance, Mask mask=true) { return self.sample(out, xi, component, unit, mask); }, "Given an 'in' direction, and 2D random variable ('xi'), generate a 'out' directions and corresponding PDF stored in a bsdfSample accordining to the radiance/importance distribution", py::arg("in"), py::arg("xi"), py::arg_v("component", bsdf_flag::All), py::arg_v("unit", unit_t::Radiance), py::arg_v("mask", true))
110 .def("pdf", [](const BsdfPtr& self, const Vec3d& in, const Vec3d& out, BsdfFlag component=bsdf_flag::All, unit_t unit=unit_t::Radiance, Mask mask=true) { return self.pdf(in, out, component, unit, mask); }, "Given an 'in' direction, produce the pdf of sampling the 'out' direction according to the radiance/importance distribution", py::arg("in"), py::arg("out"), py::arg_v("component", bsdf_flag::All), py::arg_v("unit", unit_t::Radiance), py::arg_v("mask", true))
111 .def("reflectance", [](const BsdfPtr& self, const Vec3d& out, BsdfFlag component=bsdf_flag::All, unit_t unit=unit_t::Radiance, Mask mask=true) { return self.reflectance(out, component, unit, mask); }, "Given an 'in' direction, return the approximate hemispherical reflectance (i.e., integral over all outgoing directions) of the radiance/importance of the BSDF", py::arg("in"), py::arg_v("component", bsdf_flag::All), py::arg_v("unit", unit_t::Radiance), py::arg_v("mask", true))
112 .def("__str__", &BsdfPtr::toString, "BSDF attributes to string")
113 ;
114
115 //! \brief Export aggregate BSDFs
116 m.def("Aggregate", [](py::args args)
117 {
118 bbm::vector<BsdfPtr&> arg_list;
119 for(auto& a : args)
120 arg_list.push_back( py::cast<BsdfPtr&>(*a) );
121
122 return make_bsdf_ptr( aggregate( arg_list.begin(), arg_list.end() ) );
123 }, "AggregateBsdf(BsdfPtr...) combines as many BsdfPtrs as provided.");
124
125 /******************************************************************/
126 /*! @{ \name Export parameter container
127 ******************************************************************/
128 py::class_<bbm::vector<Value&>>(m, "RefValueVector")
129 .def(py::init<const bbm::vector<Value&>& >())
130 .def("__len__", &bbm::vector<Value&>::size)
131 .def("__setitem__", [](bbm::vector<Value&>& self, size_t index, const Value& val) { self[index] = val; })
132 .def("__getitem__", [](bbm::vector<Value&>& self, size_t index) { return self[index]; })
133 .def(py::self + py::self)
134 .def(py::self + Value())
135 .def(py::self += py::self)
136 .def(py::self += Value())
137 .def(py::self - py::self)
138 .def(py::self - Value())
139 .def(py::self -= py::self)
140 .def(py::self -= Value())
141 .def(py::self * py::self)
142 .def(py::self * Value())
143 .def(py::self *= py::self)
144 .def(py::self *= Value())
145 .def(py::self / py::self)
146 .def(py::self / Value())
147 .def(py::self /= py::self)
148 .def(py::self /= Value())
149 .def("__str__", [](const bbm::vector<Value&>& param) { return bbm::toString(param); })
150 ;
151
152 py::class_<bbm::vector<Value>>(m, "ValueVector")
153 .def(py::init<const bbm::vector<Value>& >())
154 .def("__len__", &bbm::vector<Value>::size)
155 .def("__setitem__", [](bbm::vector<Value>& self, size_t index, const Value& val) { self[index] = val; })
156 .def("__getitem__", [](bbm::vector<Value>& self, size_t index) { return self[index]; })
157 .def(py::self + py::self)
158 .def(py::self + Value())
159 .def(py::self += py::self)
160 .def(py::self += Value())
161 .def(py::self - py::self)
162 .def(py::self - Value())
163 .def(py::self -= py::self)
164 .def(py::self -= Value())
165 .def(py::self * py::self)
166 .def(py::self * Value())
167 .def(py::self *= py::self)
168 .def(py::self *= Value())
169 .def(py::self / py::self)
170 .def(py::self / Value())
171 .def(py::self /= py::self)
172 .def(py::self /= Value())
173 .def("__str__", [](const bbm::vector<Value>& param) { return bbm::toString(param); })
174 ;
175 //! @}
176
177 /******************************************************************/
178 /*! @{ \brief Export methods for querying the parameters of a BSDF
179 ******************************************************************/
180 m.def("parameter_values", &parameter_values<BsdfPtr>, py::arg("bsdf"), py::arg_v("flag", bsdf_attr::All), "List all parameter values of a given BSDF");
181 m.def("parameter_default_values", &parameter_default_values<BsdfPtr>, py::arg("bsdf"), py::arg_v("flag", bsdf_attr::All), "List all default parameter values of a given BSDF");
182 m.def("parameter_lower_bound", &parameter_lower_bound<BsdfPtr>, py::arg("bsdf"), py::arg_v("flag", bsdf_attr::All), "List the lower bound of all parameter values of a given BSDF");
183 m.def("parameter_upper_bound", &parameter_upper_bound<BsdfPtr>, py::arg("bsdf"), py::arg_v("flag", bsdf_attr::All), "List the upper vound of all parameter values of a given BSDF");
184 //! @}
185 }
186
187 } // end python namespace
188} // end bbm namespace
189
190
191#endif /* BBM_PY_CORE_H_ */
A run-time aggregation of BSDFs.
A shared_ptr wrapper for bsdfs.
#define BBM_IMPORT_CONFIG(...)
Import the configs typedefs in the current scope
Definition: config.h:103
Enumerate all BSDF parameters (e.g., stack in a vector).
Structure to hold a sampled direction and corresponding pdf.
Definition: vector_util.h:27
Definition: aggregatebsdf.h:29
bsdf_flag
Reflectance Component Evaluation Flags.
Definition: bsdf_flag.h:22
std::string toString(const T &)
toString alias
Definition: stringconvert.h:594
auto aggregate(const bsdf< BSDFMODELs > &... src)
Helper methods for simplifying the creation of a aggregate bsdf.
Definition: aggregatebsdf.h:329
bsdf_ptr< get_config< BSDFTYPE > > make_bsdf_ptr(ARGS... args)
Helper method for making a bsdf_ptr from a BSDF (new construction)
Definition: bsdf_ptr.h:180
bsdf_attr
Attribute Property Flags.
Definition: bsdf_attr_flag.h:17
unit_t
Light Unit.
Definition: unit.h:21
Export ior::ior and ior::reflectance to pyton.
Structure to hold a pair of directions.
Extensions for the STL vector class.