Skip to content

Commit

Permalink
improve python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal committed Oct 4, 2024
1 parent e1cbbc7 commit 0b42471
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
9 changes: 6 additions & 3 deletions library/public/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ class F3D_EXPORT loader
virtual loader& add(const std::vector<std::string>& filePathStrings) = 0;
virtual loader& add(const mesh_t& mesh) = 0;

template<typename T>
loader& add(const std::initializer_list<T>& list)
/**
* TODO
*/
loader& add(std::initializer_list<std::string> list)
{
return this->add(std::vector<T>(list));
return this->add(std::vector<std::string>(list));
}

/**
* TODO
*/
Expand Down
5 changes: 4 additions & 1 deletion python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,16 @@ PYBIND11_MODULE(pyf3d, module)
.def_readwrite("face_indices", &f3d::mesh_t::face_indices);

// f3d::loader
// TODO full bindings
py::class_<f3d::loader, std::unique_ptr<f3d::loader, py::nodelete>> loader(module, "Loader");
loader //
.def("supported", &f3d::loader::supported)
.def("clear", &f3d::loader::clear)
.def("add", py::overload_cast<const std::filesystem::path&>(&f3d::loader::add),
"Add a file the scene", py::arg("file_path"))
.def("add", py::overload_cast<const std::vector<std::filesystem::path>&>(&f3d::loader::add),
"Add multiple filepaths to the scene", py::arg("file_path_vector"))
.def("add", py::overload_cast<const std::vector<std::string>&>(&f3d::loader::add),
"Add multiple filenames to the scene", py::arg("file_name_vector"))
.def("add", py::overload_cast<const f3d::mesh_t&>(&f3d::loader::add),
"Add a surfacic mesh from memory into the scene", py::arg("mesh"));

Expand Down
28 changes: 26 additions & 2 deletions python/testing/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def test_load_memory():
testing_dir = Path(__file__).parent.parent.parent / "testing"
reference = f"{testing_dir}/baselines/TestPythonLoadMemory.png"
output = tempfile.gettempdir() + "/TestPythonLoadMemory.png"
outputDiff = tempfile.gettempdir() + "/TestPythonLoadMemory.diff.png"

engine = f3d.Engine(f3d.Window.NATIVE_OFFSCREEN)
engine.window.size = 300, 300
Expand All @@ -27,4 +26,29 @@ def test_load_memory():

error = 0.0

assert img.compare(f3d.Image(reference), 50, error)
assert img.compare(f3d.Image(reference), 0.05, error)

def test_loader():
testing_dir = Path(__file__).parent.parent.parent / "testing"
world = f"{testing_dir}/data/world.obj"
logo = f"{testing_dir}/data/f3d.glb"
sphere1 = f"{testing_dir}/data/mb/recursive/mb_1_0.vtp"
sphere2 = f"{testing_dir}/data/mb/recursive/mb_2_0.vtp"
cube = f"{testing_dir}/data/mb/recursive/mb_0_0.vtu"
cube = f"{testing_dir}/data/f3d.glb"
reference = f"{testing_dir}/baselines/TestPythonLoader.png"
output = tempfile.gettempdir() + "/TestPythonLoader.png"

engine = f3d.Engine(f3d.Window.NATIVE_OFFSCREEN)
engine.window.size = 300, 300

engine.loader.add([world, logo])
engine.loader.add(Path(sphere1))
engine.loader.add([Path(sphere2), Path(cube)])

img = engine.window.render_to_image()
img.save(output)

error = 0.0

assert img.compare(f3d.Image(reference), 0.05, error)
3 changes: 3 additions & 0 deletions testing/baselines/TestPythonLoader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0b42471

Please sign in to comment.