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

Cli reader options #1860

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/F3DOptionsTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static inline const std::array<CLIGroup, 8> CLIOptions = {{
{ "no-background", "", "No background when render to file", "<bool>", "1" },
{ "help", "h", "Print help", "", "" }, { "version", "", "Print version details", "", "" },
{ "list-readers", "", "Print the list of readers", "", "" },
{"reader", "", "Enforce a specific reader", "<reader>", ""},
0xfedcafe marked this conversation as resolved.
Show resolved Hide resolved
{ "list-bindings", "", "Print the list of interaction bindings and exits, ignored with `--no-render`, only considers the first file group.", "", "" },
{ "config", "", "Specify the configuration file to use. absolute/relative path or filename/filestem to search in configuration file locations", "<filePath/filename/fileStem>", "" },
{ "no-config", "", "Do not read the configuration file", "<bool>", "1" },
Expand Down
1 change: 1 addition & 0 deletions application/F3DOptionsTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static inline const std::map<std::string_view, std::string_view> LibOptionsNames
{ "anti-aliasing", "render.effect.anti_aliasing" },
{ "tone-mapping", "render.effect.tone_mapping" },
{ "final-shader", "render.effect.final_shader" },
{ "reader", "render.reader" },
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
1 change: 1 addition & 0 deletions doc/user/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Options|Type<br>Default|Description
-h, \-\-help||Print *help* and exit. Ignore `--verbose`.
\-\-version||Show *version* information and exit. Ignore `--verbose`.
\-\-list-readers||List available *readers* and exit. Ignore `--verbose`.
\-\-reader=\<reader\>|string<br>-|Enforce a specific reader.
\-\-list-bindings||List available *bindings* and exit. Ignore `--verbose`.
\-\-list-rendering-backends||List available *rendering backends* and exit. Ignore `--verbose`.
\-\-config=\<config file path/name/stem\>|string<br>config|Specify the [configuration file](CONFIGURATION_FILE.md) to use. Supports absolute/relative path but also filename/filestem to search for in standard configuration file locations.
Expand Down
3 changes: 3 additions & 0 deletions library/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
}
},
"render": {
"reader": {
"type": "string"
},
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
"show_edges": {
"type": "bool"
},
Expand Down
13 changes: 13 additions & 0 deletions library/private/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "reader.h"

#include <map>
#include <optional>
#include <vector>

namespace f3d
Expand Down Expand Up @@ -57,6 +58,16 @@ class factory
*/
plugin_initializer_t getStaticInitializer(const std::string& pluginName);

/**
* Set preferred reader
*/
bool setPreferredReader(const std::string& preferredReader);

/**
* Get preferred reader
*/
std::optional<std::string_view> getPreferredReader();

protected:
factory();
virtual ~factory() = default;
Expand All @@ -66,6 +77,8 @@ class factory
std::vector<plugin*> Plugins;

std::map<std::string, plugin_initializer_t> StaticPluginInitializers;

std::optional<std::string> Reader;
};
}
#endif
5 changes: 5 additions & 0 deletions library/public/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ class F3D_EXPORT engine
*/
[[nodiscard]] interactor& getInteractor();

/**
* Get the enforced readers name
*/
[[nodiscard]] std::string_view getReader();

/**
* List rendering backends supported by libf3d.
* All backends have an associated boolean flag indicating if it can be used.
Expand Down
8 changes: 8 additions & 0 deletions library/src/engine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ std::vector<engine::readerInformation> engine::getReadersInfo()
return readersInfo;
}

//----------------------------------------------------------------------------
std::string_view engine::getReader()
{
static const std::string empty_str = ""; // used to avoid dangling pointer; a bit strange way
auto prefReader = factory::instance()->getPreferredReader();
return prefReader.has_value() ? *prefReader : empty_str;
}

//----------------------------------------------------------------------------
engine& engine::setCachePath(const fs::path& cachePath)
{
Expand Down
26 changes: 25 additions & 1 deletion library/src/factory.cxx.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ reader* factory::getReader(const std::string& fileName)
{
for (auto r : p->getReaders())
{
if (r->getScore() > bestScore && r->canRead(fileName))
if (this->Reader.has_value() && r->getName() == *(this->Reader))
{
return r->canRead(fileName) ? r.get() : nullptr;
}
if (r->canRead(fileName) && r->getScore() > bestScore)
{
bestScore = r->getScore();
bestReader = r.get();
Expand Down Expand Up @@ -99,4 +103,24 @@ bool factory::registerOnce(plugin* plug)
}
return false;
}

//----------------------------------------------------------------------------
bool factory::setPreferredReader(const std::string& preferredReader)
{
if (preferredReader == "")
{
return false;
}
else
{
this->Reader = preferredReader;
return true;
}
}

//----------------------------------------------------------------------------
std::optional<std::string_view> factory::getPreferredReader()
{
return this->Reader;
}
}
11 changes: 10 additions & 1 deletion library/src/scene_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "vtkF3DMemoryMesh.h"
#include "vtkF3DMetaImporter.h"

#include <optional>
#include <vtkCallbackCommand.h>
#include <vtkProgressBarRepresentation.h>
#include <vtkProgressBarWidget.h>
Expand Down Expand Up @@ -231,8 +232,16 @@ scene& scene_impl::add(const std::vector<fs::path>& filePaths)
throw scene::load_failure_exception(filePath.string() + " does not exists");
}

auto* factory = f3d::factory::instance();

if (auto reader = this->Internals->Options.render.reader; reader.has_value())
{
log::debug("Current value of a reader:" + *reader + "\n");
factory->setPreferredReader(*reader);
}

// Recover the importer for the provided file path
f3d::reader* reader = f3d::factory::instance()->getReader(filePath.string());
f3d::reader* reader = factory->getReader(filePath.string());
if (reader)
{
log::debug(
Expand Down
1 change: 1 addition & 0 deletions library/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ list(APPEND libf3dSDKTests_list
TestSDKMultiColoring.cxx
TestSDKOptions.cxx
TestSDKOptionsIO.cxx
TestSDKReaderSelection.cxx
TestSDKRenderFinalShader.cxx
TestSDKUtils.cxx
TestSDKWindowAuto.cxx
Expand Down
46 changes: 46 additions & 0 deletions library/testing/TestSDKReaderSelection.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "PseudoUnitTest.h"
#include "TestSDKHelpers.h"

#include <engine.h>
#include <log.h>
#include <scene.h>
#include <window.h>

namespace fs = std::filesystem;
int TestSDKReaderSelection(int argc, char* argv[])
{
PseudoUnitTest test;

f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);
f3d::engine::autoloadPlugins();

// Test file path setup
std::string monkey = std::string(argv[1]) + "data/red_translucent_monkey.gltf";

// Test default reader (no preference)
{
f3d::engine engine = f3d::engine::create(true);
f3d::scene& scene = engine.getScene();
test("add with a single path", [&]() { scene.add(fs::path(monkey)); });
}

// Test Draco reader
{
f3d::engine engine = f3d::engine::create(true);
engine.getOptions().render.reader = "GLTFDraco";
f3d::scene& scene = engine.getScene();
test("Draco reader works", [&]() { scene.add(fs::path(monkey)); });
test("Reader is GLTFDraco", engine.getReader() == "GLTFDraco");
}

// Test GLTF reader
{
f3d::engine engine = f3d::engine::create(true);
engine.getOptions().render.reader = "GLTF";
f3d::scene& scene = engine.getScene();
test("GLTF reader works", [&]() { scene.add(fs::path(monkey)); });
test("Reader is GLTF", engine.getReader() == "GLTF");
}

return test.result();
}
Loading