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

Add a new option type: direction_t #2007

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 10 additions & 5 deletions application/F3DStarter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class F3DStarter::F3DInternals
std::vector<double> CameraFocalPoint;
std::vector<double> CameraViewUp;
double CameraViewAngle;
std::vector<double> CameraDirection;
std::optional<f3d::direction_t> CameraDirection;
double CameraZoomFactor;
double CameraAzimuthAngle;
double CameraElevationAngle;
Expand Down Expand Up @@ -133,10 +133,10 @@ class F3DStarter::F3DInternals

bool reset = false;
double zoomFactor = 0.9;
if (camConf.CameraPosition.size() != 3 && camConf.CameraDirection.size() == 3)
if (camConf.CameraPosition.size() != 3 && camConf.CameraDirection.has_value())
{
f3d::vector3_t dir;
std::copy_n(camConf.CameraDirection.begin(), 3, dir.begin());
std::copy_n(camConf.CameraDirection->data(), 3, dir.begin());
f3d::point3_t foc;
f3d::point3_t pos;
cam.getFocalPoint(foc);
Expand Down Expand Up @@ -571,12 +571,17 @@ class F3DStarter::F3DInternals
this->AppOptions.ColorMapFile =
f3d::options::parse<std::string>(appOptions.at("colormap-file"));

std::optional<f3d::direction_t> camDir;
if (!appOptions.at("camera-direction").empty())
{
camDir = f3d::options::parse<f3d::direction_t>(appOptions.at("camera-direction"));
}

this->AppOptions.CamConf = { f3d::options::parse<std::vector<double>>(
appOptions.at("camera-position")),
f3d::options::parse<std::vector<double>>(appOptions.at("camera-focal-point")),
f3d::options::parse<std::vector<double>>(appOptions.at("camera-view-up")),
f3d::options::parse<double>(appOptions.at("camera-view-angle")),
f3d::options::parse<std::vector<double>>(appOptions.at("camera-direction")),
f3d::options::parse<double>(appOptions.at("camera-view-angle")), camDir,
f3d::options::parse<double>(appOptions.at("camera-zoom-factor")),
f3d::options::parse<double>(appOptions.at("camera-azimuth-angle")),
f3d::options::parse<double>(appOptions.at("camera-elevation-angle")) };
Expand Down
3 changes: 2 additions & 1 deletion application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ f3d_test(NAME TestPTS DATA samplePTS.pts)
f3d_test(NAME TestColormap DATA IM-0001-1983.dcm ARGS --scalar-coloring --roughness=1 --colormap=0,1,0,0,1,0,1,0)
f3d_test(NAME TestCameraConfiguration DATA suzanne.obj ARGS --camera-position=0,0,-10 -x --camera-view-up=1,0,0 --camera-focal-point=1,0,0 --camera-view-angle=20 --camera-azimuth-angle=40 --camera-elevation-angle=-80 --camera-direction=12,34,56 --camera-zoom-factor=78)
f3d_test(NAME TestCameraDirection DATA suzanne.obj ARGS --camera-direction=-1,-1,1)
f3d_test(NAME TestCameraDirectionZ DATA suzanne.obj ARGS --camera-direction=+Z)
f3d_test(NAME TestCameraClipping DATA checkerboard_colorful.obj CONFIG ${F3D_SOURCE_DIR}/testing/configs/checkerboard_colorful.json RESOLUTION 800,600)
f3d_test(NAME TestCameraOrthographic ARGS --camera-orthographic DATA cow.vtp)
f3d_test(NAME TestToneMapping DATA suzanne.ply ARGS -t TONE_MAPPING)
Expand Down Expand Up @@ -245,7 +246,7 @@ f3d_test(NAME TestMultiFilePositionals DATA mb/recursive/mb_0_0.vtu mb/recursive
f3d_test(NAME TestMultiFileNonCoherentComponentNames DATA bluntfin.vts bluntfin_t.vtu ARGS --multi-file-mode=all --scalar-coloring --coloring-array=Momentum --coloring-component=2 --coloring-scalar-bar)
f3d_test(NAME TestMultiInputArg ARGS --input ${F3D_SOURCE_DIR}/testing/data/mb/recursive/mb_0_0.vtu ${F3D_SOURCE_DIR}/testing/data/mb/recursive/mb_1_0.vtp --multi-file-mode=all -s --coloring-array=Polynomial -b)
f3d_test(NAME TestMultiInputMultiArgs ARGS --input ${F3D_SOURCE_DIR}/testing/data/mb/recursive/mb_0_0.vtu --input ${F3D_SOURCE_DIR}/testing/data/mb/recursive/mb_1_0.vtp --multi-file-mode=all -s --coloring-array=Polynomial -b)
f3d_test(NAME TestInvalidUpDirection DATA suzanne.ply ARGS -g --up=W REGEXP "W is not a valid up direction" NO_BASELINE)
f3d_test(NAME TestInvalidUpDirection DATA suzanne.ply ARGS -g --up=1,1,0 REGEXP "is not a valid up direction" NO_BASELINE)
f3d_test(NAME TestUpDirectionNoSign DATA suzanne.ply ARGS --up=X)
f3d_test(NAME TestTextureMatCap DATA suzanne.ply ARGS --texture-matcap=${F3D_SOURCE_DIR}/testing/data/skin.png)
f3d_test(NAME TestTextureMatCapWithTCoords DATA WaterBottle.glb ARGS --texture-matcap=${F3D_SOURCE_DIR}/testing/data/skin.png)
Expand Down
15 changes: 13 additions & 2 deletions cmake/f3dOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function(_parse_json_option _top_json)
# Identify types
set(_option_actual_type ${_option_type})
set(_option_variant_type ${_option_type})
set(_option_explicit_constr "")
set(_option_default_value_start "")
set(_option_default_value_end "")

Expand All @@ -128,9 +129,19 @@ function(_parse_json_option _top_json)
set(_option_default_value_end "\"")
elseif(_option_type STREQUAL "ratio")
set(_option_actual_type "f3d::ratio_t")
set(_option_explicit_constr "f3d::ratio_t")
set(_option_variant_type "double")
set(_option_default_value_start "{")
set(_option_default_value_end "}")
elseif(_option_type STREQUAL "color")
set(_option_actual_type "f3d::color_t")
set(_option_explicit_constr "f3d::color_t")
set(_option_variant_type "std::vector<double>")
set(_option_default_value_start "{")
set(_option_default_value_end "}")
elseif(_option_type STREQUAL "direction")
set(_option_actual_type "f3d::direction_t")
set(_option_explicit_constr "f3d::direction_t")
set(_option_variant_type "std::vector<double>")
set(_option_default_value_start "{")
set(_option_default_value_end "}")
Expand All @@ -140,7 +151,7 @@ function(_parse_json_option _top_json)

if(_default_value_error STREQUAL "NOTFOUND")
# Use default_value
set(_optional_default_value_initialize "${_option_default_value_start}${_option_default_value}${_option_default_value_end}")
set(_optional_default_value_initialize "${_option_explicit_constr}${_option_default_value_start}${_option_default_value}${_option_default_value_end}")
string(APPEND _options_struct "${_option_indent} ${_option_actual_type} ${_member_name} = ${_optional_default_value_initialize};\n")
set(_optional_getter "")
list(APPEND _options_is_optional "if (name == \"${_option_name}\") return false")
Expand All @@ -153,7 +164,7 @@ function(_parse_json_option _top_json)
list(APPEND _options_reset "if (name == \"${_option_name}\") opt.${_option_name}.reset()")
endif()

list(APPEND _options_setter "if (name == \"${_option_name}\") opt.${_option_name} = std::get<${_option_variant_type}>(value)")
list(APPEND _options_setter "if (name == \"${_option_name}\") opt.${_option_name} = ${_option_explicit_constr}${_option_default_value_start}std::get<${_option_variant_type}>(value)${_option_default_value_end}")
list(APPEND _options_getter "if (name == \"${_option_name}\") return opt.${_option_name}${_optional_getter}")
list(APPEND _options_string_setter "if (name == \"${_option_name}\") opt.${_option_name} = options_tools::parse<${_option_actual_type}>(str)")
list(APPEND _options_string_getter "if (name == \"${_option_name}\") return options_tools::format(opt.${_option_name}${_optional_getter})")
Expand Down
2 changes: 1 addition & 1 deletion doc/libf3d/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ See the [APIs](#APIs) details below for more info.
| scene.animation.speed_factor | ratio<br>1<br>render | Set the animation speed factor to slow, speed up or even invert animation. | \-\-animation-speed-factor |
| scene.animation.time | double<br>optional<br>load | Set the animation time to load. | \-\-animation-time |
| scene.camera.index | int<br>optional<br>load | Select the scene camera to use when available in the file.<br>The default scene always uses automatic camera. | \-\-camera-index |
| scene.up_direction | string<br>+Y<br>load | Define the Up direction. It impacts the grid, the axis, the HDRI and the camera. | \-\-up |
| scene.up_direction | direction<br>+Y<br>load | Define the Up direction. Only axis-aligned directions are valid. It impacts the grid, the axis, the HDRI and the camera. | \-\-up |
| scene.camera.orthographic | bool<br>optional<br>load | Set to true to force orthographic projection. Model specified by default, which is false if not specified. | \-\-camera\-orthographic |

## Interactor Options
Expand Down
4 changes: 2 additions & 2 deletions doc/user/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ F3D behavior can be fully controlled from the command line using the following o
| \-\-progress | bool<br>false | Show a _progress bar_ when loading the file. |
| \-\-animation-progress | bool<br>false | Show a _progress bar_ when playing the animation. |
| \-\-multi-file-mode=\<single \| all\> | string<br>single | When opening multiple files, select if they should be grouped (`all`) or alone (`single`). Configuration files for all loaded files will be used in the order they are provided. |
| \-\-up=\<[+\|-][X\|Y\|Z]\> | string<br>+Y | Define the Up direction. |
| \-\-up=\<[+\|-][X\|Y\|Z]\> | direction<br>+Y | Define the Up direction. Only axis-aligned directions are valid. |
| -x, \-\-axis | bool<br>false | Show _axes_ as a trihedron in the scene. |
| -g, \-\-grid | bool<br>false | Show _a grid_ aligned with the horizontal (orthogonal to the Up direction) plane. |
| \-\-grid\-unit=\<length\> | double<br>- | Set the size of the _unit square_ for the grid. If not set (the default) a suitable value will be automatically computed. |
Expand Down Expand Up @@ -113,7 +113,7 @@ F3D behavior can be fully controlled from the command line using the following o
| \-\-camera-focal-point=\<X,Y,Z\> | vector\<double\><br>- | Set the camera focal point. |
| \-\-camera-view-up=\<X,Y,Z\> | vector\<double\><br>- | Set the camera view up vector. Will be orthogonalized. |
| \-\-camera-view-angle=\<angle\> | double<br>- | Set the camera view angle, a strictly positive value in degrees. |
| \-\-camera-direction=\<X,Y,Z\> | vector\<double\><br>- | Set the camera direction, looking at the focal point. |
| \-\-camera-direction=\<X,Y,Z\> | direction<br>- | Set the camera direction, looking at the focal point. |
| \-\-camera-zoom-factor=\<factor\> | double<br>- | Set the camera zoom factor relative to the autozoom on data, a strictly positive value. |
| \-\-camera-azimuth-angle=\<angle\> | double<br>0.0 | Apply an azimuth transformation to the camera, in degrees, added after other camera options. |
| \-\-camera-elevation-angle=\<angle\> | double<br>0.0 | Apply an elevation transformation to the camera, in degrees, added after other camera options. |
Expand Down
10 changes: 10 additions & 0 deletions doc/user/PARSING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The following types are supported:
- ratio: A double dividend over a double divisor, stored in a double.
- string: A string of characters.
- color: A RGB color
- direction: A 3D vector representing a direction

As well as a list for bool, int, double, ratio, string, noted as

Expand Down Expand Up @@ -67,3 +68,12 @@ When formatting a vector into a string, individual token are formatted according
## Color

Color are parsed and formatted as a vector of double.

## Direction

The following formats are supported when parsing a string into a direction:

- [+/-][X/Y/Z]
- vector of three doubles

When formatting a direction into a string, it is formatted as a vector of doubles.
4 changes: 2 additions & 2 deletions library/options.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"scene": {
"up_direction": {
"type": "string",
"default_value": "+Y"
"type": "direction",
"default_value": "0,1,0"
},
"animation": {
"autoplay": {
Expand Down
54 changes: 51 additions & 3 deletions library/private/options_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "types.h"

#include <algorithm>
#include <cassert>
#include <regex>
#include <sstream>

namespace f3d
Expand Down Expand Up @@ -159,16 +161,16 @@ ratio_t parse(const std::string& str)
{
if (!str.empty() && str.at(str.size() - 1) == '%')
{
return stodStrict(str.substr(0, str.size() - 1)) / 100;
return f3d::ratio_t(stodStrict(str.substr(0, str.size() - 1)) / 100);
}

const std::size_t sep = str.find_first_of(":/");
if (sep != std::string::npos)
{
return stodStrict(str.substr(0, sep)) / stodStrict(str.substr(sep + 1));
return f3d::ratio_t(stodStrict(str.substr(0, sep)) / stodStrict(str.substr(sep + 1)));
}

return stodStrict(str);
return f3d::ratio_t(stodStrict(str));
}
catch (std::invalid_argument const&)
{
Expand Down Expand Up @@ -201,6 +203,41 @@ color_t parse(const std::string& str)
}
}

//----------------------------------------------------------------------------
/**
* Parse provided string into a direction_t.
* Supported formats: "X,Y,Z", "[+|-][X|Y|Z]"
* rely on parse<std::vector<double>>(str)
* Can throw options::parsing_exception in case of failure to parse
*/
template<>
direction_t parse(const std::string& str)
{
try
{
const std::regex re("([-+]?)([XYZ])", std::regex_constants::icase);
std::smatch match;
if (std::regex_match(str, match, re))
{
const double sign = match[1].str() == "-" ? -1.0 : +1.0;
const int index = std::toupper(match[2].str()[0]) - 'X';
assert(index >= 0 && index < 3);

direction_t dir;
dir[index] = sign;
return dir;
}
else
{
return direction_t(options_tools::parse<std::vector<double>>(str));
}
}
catch (const f3d::type_construction_exception& ex)
{
throw options::parsing_exception("Cannot parse " + str + " into a direction_t: " + ex.what());
}
}

//----------------------------------------------------------------------------
/**
* Return provided string stripped of leading and trailing spaces.
Expand Down Expand Up @@ -294,6 +331,17 @@ std::string format(color_t var)
return options_tools::format(static_cast<std::vector<double>>(var));
}

//----------------------------------------------------------------------------
/**
* Format provided var into a string from provided direction_t
* rely on format(std::vector<double>&)
*/
std::string format(direction_t var)
{
// TODO generate a proper direction string
return options_tools::format(static_cast<std::vector<double>>(var));
}

} // option_tools
} // f3d
#endif // f3d_options_tools_h
5 changes: 5 additions & 0 deletions library/public/options.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,10 @@ inline std::ostream& operator<<(std::ostream& os, const f3d::color_t& color)
os << f3d::options::format(color);
return os;
}
inline std::ostream& operator<<(std::ostream& os, const f3d::direction_t& direction)
{
os << f3d::options::format(direction);
return os;
}

#endif
Loading