Skip to content

Commit

Permalink
Add EmitterFlags enumeration
Browse files Browse the repository at this point in the history
- `Emitter::flags()` accessor
- Set flags
- Express `Emitter::is_environment()` in terms of the emitter flags
- Python bindings
  • Loading branch information
merlinND committed Jan 30, 2020
1 parent 58004c5 commit c6e9b83
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 20 deletions.
64 changes: 63 additions & 1 deletion include/mitsuba/render/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,80 @@

NAMESPACE_BEGIN(mitsuba)


/**
* \brief This list of flags is used to classify the different types of emitters.
*/
enum class EmitterFlags : uint32_t {
// =============================================================
// Emitter types
// =============================================================

/// No flags set (default value)
None = 0x00000,

/// The emitter lies at a single point in space
DeltaPosition = 0x00001,

/// The emitter emits light in a single direction
DeltaDirection = 0x00002,

/// The emitter is placed at infinity (e.g. environment maps)
Infinite = 0x00004,

/// The emitter is attached to a surface (e.g. area emitters)
Surface = 0x00008,

// =============================================================
//! Other lobe attributes
// =============================================================

/// The emission depends on the UV coordinates
SpatiallyVarying = 0x00010,

// =============================================================
//! Compound lobe attributes
// =============================================================

/// Delta function in either position or direction
Delta = DeltaPosition | DeltaDirection,
};

constexpr uint32_t operator |(EmitterFlags f1, EmitterFlags f2) { return (uint32_t) f1 | (uint32_t) f2; }
constexpr uint32_t operator |(uint32_t f1, EmitterFlags f2) { return f1 | (uint32_t) f2; }
constexpr uint32_t operator &(EmitterFlags f1, EmitterFlags f2) { return (uint32_t) f1 & (uint32_t) f2; }
constexpr uint32_t operator &(uint32_t f1, EmitterFlags f2) { return f1 & (uint32_t) f2; }
constexpr uint32_t operator ~(EmitterFlags f1) { return ~(uint32_t) f1; }
constexpr uint32_t operator +(EmitterFlags e) { return (uint32_t) e; }
template <typename UInt32>
constexpr auto has_flag(UInt32 flags, EmitterFlags f) { return neq(flags & (uint32_t) f, 0u); }



template <typename Float, typename Spectrum>
class MTS_EXPORT_RENDER Emitter : public Endpoint<Float, Spectrum> {
public:
MTS_IMPORT_BASE(Endpoint)

/// Is this an environment map light emitter?
virtual bool is_environment() const;
bool is_environment() const {
return has_flag(m_flags, EmitterFlags::Infinite);
}

/// Flags for all components combined.
uint32_t flags(mask_t<Float> /*active*/ = true) const { return m_flags; }


ENOKI_CALL_SUPPORT_FRIEND()
MTS_DECLARE_CLASS()
protected:
Emitter(const Properties &props);

virtual ~Emitter();

protected:
/// Combined flags for all properties of this emitter.
uint32_t m_flags;
};

MTS_EXTERN_CLASS_RENDER(Emitter)
Expand All @@ -36,6 +97,7 @@ ENOKI_CALL_SUPPORT_TEMPLATE_BEGIN(mitsuba::Emitter)
ENOKI_CALL_SUPPORT_METHOD(sample_direction)
ENOKI_CALL_SUPPORT_METHOD(pdf_direction)
ENOKI_CALL_SUPPORT_METHOD(is_environment)
ENOKI_CALL_SUPPORT_GETTER(flags, m_flags)
ENOKI_CALL_SUPPORT_TEMPLATE_END(mitsuba::Emitter)

//! @}
Expand Down
4 changes: 3 additions & 1 deletion src/emitters/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NAMESPACE_BEGIN(mitsuba)
template <typename Float, typename Spectrum>
class AreaLight final : public Emitter<Float, Spectrum> {
public:
MTS_IMPORT_BASE(Emitter, m_shape, m_medium)
MTS_IMPORT_BASE(Emitter, m_flags, m_shape, m_medium)
MTS_IMPORT_TYPES(Scene, Shape, Texture)

AreaLight(const Properties &props) : Base(props) {
Expand All @@ -20,6 +20,8 @@ class AreaLight final : public Emitter<Float, Spectrum> {
"shape.");

m_radiance = props.texture<Texture>("radiance", Texture::D65(1.f));
// TODO: detect if underlying spectrum really is spatially varying
m_flags = EmitterFlags::Surface | EmitterFlags::SpatiallyVarying;
}

void set_shape(Shape *shape) override {
Expand Down
7 changes: 2 additions & 5 deletions src/emitters/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NAMESPACE_BEGIN(mitsuba)
template <typename Float, typename Spectrum>
class ConstantBackgroundEmitter final : public Emitter<Float, Spectrum> {
public:
MTS_IMPORT_BASE(Emitter)
MTS_IMPORT_BASE(Emitter, m_flags)
MTS_IMPORT_TYPES(Scene, Shape, Texture)

ConstantBackgroundEmitter(const Properties &props) : Base(props) {
Expand All @@ -21,6 +21,7 @@ class ConstantBackgroundEmitter final : public Emitter<Float, Spectrum> {
m_bsphere = ScalarBoundingSphere3f(ScalarPoint3f(0.f), 1.f);

m_radiance = props.texture<Texture>("radiance", Texture::D65(1.f));
m_flags = +EmitterFlags::Infinite;
}

void set_scene(const Scene *scene) override {
Expand Down Expand Up @@ -86,10 +87,6 @@ class ConstantBackgroundEmitter final : public Emitter<Float, Spectrum> {
return ScalarBoundingBox3f();
}

bool is_environment() const override {
return true;
}

void traverse(TraversalCallback *callback) override {
callback->put_object("radiance", m_radiance.get());
}
Expand Down
7 changes: 2 additions & 5 deletions src/emitters/envmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NAMESPACE_BEGIN(mitsuba)
template <typename Float, typename Spectrum>
class EnvironmentMapEmitter final : public Emitter<Float, Spectrum> {
public:
MTS_IMPORT_BASE(Emitter, m_world_transform)
MTS_IMPORT_BASE(Emitter, m_flags, m_world_transform)
MTS_IMPORT_TYPES(Scene, Shape, Texture)

using Warp = Hierarchical2D<Float, 0>;
Expand Down Expand Up @@ -74,6 +74,7 @@ class EnvironmentMapEmitter final : public Emitter<Float, Spectrum> {
m_scale = props.float_("scale", 1.f);
m_warp = Warp(luminance.get(), m_resolution);
m_d65 = Texture::D65(1.f);
m_flags = EmitterFlags::Infinite | EmitterFlags::SpatiallyVarying;
}

void parameters_changed() override {
Expand Down Expand Up @@ -176,10 +177,6 @@ class EnvironmentMapEmitter final : public Emitter<Float, Spectrum> {
return oss.str();
}

bool is_environment() const override {
return true;
}

protected:
UnpolarizedSpectrum eval_spectrum(Point2f uv, const Wavelength &wavelengths, Mask active) const {
uv *= Vector2f(m_resolution - 1u);
Expand Down
3 changes: 2 additions & 1 deletion src/emitters/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NAMESPACE_BEGIN(mitsuba)
template <typename Float, typename Spectrum>
class PointLight final : public Emitter<Float, Spectrum> {
public:
MTS_IMPORT_BASE(Emitter, m_medium, m_needs_sample_3, m_world_transform)
MTS_IMPORT_BASE(Emitter, m_flags, m_medium, m_needs_sample_3, m_world_transform)
MTS_IMPORT_TYPES(Scene, Shape, Texture)

PointLight(const Properties &props) : Base(props) {
Expand All @@ -25,6 +25,7 @@ class PointLight final : public Emitter<Float, Spectrum> {

m_intensity = props.texture<Texture>("intensity", Texture::D65(1.f));
m_needs_sample_3 = false;
m_flags = +EmitterFlags::DeltaPosition;
}

std::pair<Ray3f, Spectrum> sample_ray(Float time, Float wavelength_sample,
Expand Down
3 changes: 0 additions & 3 deletions src/librender/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ NAMESPACE_BEGIN(mitsuba)

MTS_VARIANT Emitter<Float, Spectrum>::Emitter(const Properties &props) : Base(props) { }
MTS_VARIANT Emitter<Float, Spectrum>::~Emitter() { }
MTS_VARIANT bool Emitter<Float, Spectrum>::is_environment() const {
return false;
}

MTS_IMPLEMENT_CLASS_VARIANT(Emitter, Endpoint, "emitter")
MTS_INSTANTIATE_CLASS(Emitter)
Expand Down
1 change: 1 addition & 0 deletions src/librender/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ foreach (MTS_VARIANT ${MTS_VARIANTS})
endforeach()

add_mitsuba_python_library(render_ext
emitter.cpp
main.cpp
bsdf.cpp
microfacet.cpp
Expand Down
24 changes: 24 additions & 0 deletions src/librender/python/emitter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <mitsuba/python/python.h>
#include <mitsuba/render/emitter.h>
#include <mitsuba/core/properties.h>

MTS_PY_EXPORT(EmitterExtras) {
py::enum_<EmitterFlags>(m, "EmitterFlags", D(EmitterFlags))
.value("None", EmitterFlags::None, D(EmitterFlags, None))
.value("DeltaPosition", EmitterFlags::DeltaPosition, D(EmitterFlags, DeltaPosition))
.value("DeltaDirection", EmitterFlags::DeltaDirection, D(EmitterFlags, DeltaDirection))
.value("Infinite", EmitterFlags::Infinite, D(EmitterFlags, Infinite))
.value("Surface", EmitterFlags::Surface, D(EmitterFlags, Surface))
.value("SpatiallyVarying", EmitterFlags::SpatiallyVarying, D(EmitterFlags, SpatiallyVarying))
.value("Delta", EmitterFlags::Delta, D(EmitterFlags, Delta))
.def(py::self == py::self)
.def(py::self | py::self)
.def(int() | py::self)
.def(py::self & py::self)
.def(int() & py::self)
.def(+py::self)
.def(~py::self)
.def("__pos__", [](const EmitterFlags &f) {
return static_cast<uint32_t>(f);
}, py::is_operator());
}
6 changes: 2 additions & 4 deletions src/librender/python/emitter_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ MTS_VARIANT class PyEmitter : public Emitter<Float, Spectrum> {
PYBIND11_OVERLOAD_PURE(ScalarBoundingBox3f, Emitter, bbox,);
}

bool is_environment() const override {
PYBIND11_OVERLOAD_PURE(bool, Emitter, is_environment,);
}

std::string to_string() const override {
PYBIND11_OVERLOAD_PURE(std::string, Emitter, to_string,);
Expand All @@ -54,7 +51,8 @@ MTS_PY_EXPORT(Emitter) {

auto emitter = py::class_<Emitter, PyEmitter, Endpoint, ref<Emitter>>(m, "Emitter", D(Emitter))
.def(py::init<const Properties&>())
.def_method(Emitter, is_environment);
.def_method(Emitter, is_environment)
.def_method(Emitter, flags);

if constexpr (is_cuda_array_v<Float>)
pybind11_type_alias<UInt64, EmitterPtr>();
Expand Down

0 comments on commit c6e9b83

Please sign in to comment.