Skip to content

Commit

Permalink
add raw version of getting parameters in helium, demonstrate in helide
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffamstutz committed Mar 17, 2023
1 parent e52fb58 commit f5ae12a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/helide/camera/Perspective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ void Perspective::commit()
{
Camera::commit();

float fovy = getParam<float>("fovy", radians(60.f));
// NOTE: demonstrate alternative 'raw' method for getting parameter values
float fovy = 0.f;
if (!getParam("fovy", ANARI_FLOAT32, &fovy))
fovy = radians(60.f);
float aspect = getParam<float>("aspect", 1.f);

float2 imgPlaneSize;
Expand Down
16 changes: 16 additions & 0 deletions libs/helium/utility/ParameterizedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

#include "ParameterizedObject.h"
// std
#include <cstring>

namespace helium {

Expand All @@ -16,6 +18,20 @@ void ParameterizedObject::setParam(
findParam(name, true)->second = AnariAny(type, v);
}

bool ParameterizedObject::getParam(
const std::string &name, ANARIDataType type, void *v)
{
if (type == ANARI_STRING || anari::isObject(type))
return false;

auto *p = findParam(name, false);
if (!p || !p->second.is(type))
return false;

std::memcpy(v, p->second.data(), anari::sizeOf(type));
return true;
}

std::string ParameterizedObject::getParamString(
const std::string &name, const std::string &valIfNotFound)
{
Expand Down
6 changes: 6 additions & 0 deletions libs/helium/utility/ParameterizedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ struct ParameterizedObject
template <typename T>
T getParam(const std::string &name, T valIfNotFound);

// Get the value of the parameter associated with 'name' and write it to
// location 'v', returning whether the was actually read. Just like the
// templated version above, this requires that 'type' exactly match what the
// application set. This function also cannot get objects or strings.
bool getParam(const std::string &name, ANARIDataType type, void *v);

// Get the pointer to an object parameter (returns null if not present). While
// ParameterizedObject will track object lifetime appropriately, accessing
// an object parameter does _not_ influence lifetime considerations -- devices
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_helium_ParameterizedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ SCENARIO(
REQUIRE(obj.hasParam(name));
REQUIRE(obj.getParam<int>(name, 4) == v);
REQUIRE(obj.getParam<short>(name, 4) == 4);

int v2 = 0;
bool success = obj.getParam(name, ANARI_INT32, &v2);
REQUIRE(success == true);
REQUIRE(v2 == v);
}

WHEN("The parameter is removed")
Expand All @@ -68,6 +73,11 @@ SCENARIO(
REQUIRE(!obj.hasParam(name));
REQUIRE(obj.getParam<int>(name, 4) == 4);
REQUIRE(obj.getParam<short>(name, 4) == 4);

int v2 = 0;
bool success = obj.getParam(name, ANARI_INT32, &v2);
REQUIRE(success == false);
REQUIRE(v2 == 0);
}
}
}
Expand Down

0 comments on commit f5ae12a

Please sign in to comment.