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

Refactor vector3_t #1645 #1648

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
06d661f
refactored vector3_t and added a naive parse function
Yogesh9000 Oct 5, 2024
1207b57
added a new option and updated struct vector3_t
Yogesh9000 Oct 5, 2024
e3572cf
vector3_t does not inherit from std::array now
Yogesh9000 Oct 5, 2024
9eb7747
added tests and related functions: currently build fails
Yogesh9000 Oct 8, 2024
d8e4ad9
added tests and related functions: currently build passes
Yogesh9000 Oct 9, 2024
f1b01d4
updated python bindings
Yogesh9000 Oct 9, 2024
efab6ad
removed unnecessary includes
Yogesh9000 Oct 9, 2024
eff24d8
minor fixes
Yogesh9000 Oct 9, 2024
6761e04
fix formating and python binding
Yogesh9000 Oct 9, 2024
b87313f
fix for f3d::vector3_t iterators
Yogesh9000 Oct 9, 2024
d6d4eaa
minor fix
Yogesh9000 Oct 9, 2024
7bcad39
added exception and other minor fixes
Yogesh9000 Oct 13, 2024
1d86b73
fixed formatting
Yogesh9000 Oct 13, 2024
47df654
fixed formatting
Yogesh9000 Oct 13, 2024
73feb45
fixed formatting
Yogesh9000 Oct 13, 2024
e34433b
fixed formatting
Yogesh9000 Oct 13, 2024
137de48
changed type from size_t to int
Yogesh9000 Oct 13, 2024
12ddb7b
Update library/public/types.h
Yogesh9000 Oct 13, 2024
f492392
Tests: Fix incorrect threshold in image comparisons (#1647)
mwestphal Oct 4, 2024
1fda9e2
Deps: Require VTK 9.2.6 (#1649)
mwestphal Oct 6, 2024
b4fe960
Fix grid display on main axes (#1627)
rhysaelliott Oct 6, 2024
ca02c81
Add a meta importer (#1609)
mwestphal Oct 8, 2024
849fe6a
Rename loader into scene (#1660)
mwestphal Oct 8, 2024
6461dd5
Camera: Fix ResetCamera with camera index (#1662)
mwestphal Oct 9, 2024
91b0743
CMake: Add a check for VTK_READER/VTK_IMPORTER in plugin CMake logic …
mwestphal Oct 9, 2024
043e931
vtkext: Move vtkF3DRendererWithColoring into vtkF3DRenderer
mwestphal Oct 10, 2024
ce01a06
vtkext: Merge ConfigureColoringActorsProperties and ConfigureActorsPr…
mwestphal Oct 10, 2024
2ffeeba
options: Use optional for edges and orthographic
mwestphal Oct 10, 2024
9fb9c25
vtkext: Remove unused member
mwestphal Oct 10, 2024
02837ed
Fix wasm bindings (#1666)
Meakk Oct 14, 2024
7a810e5
Fix wasm grid rendering (#1667)
Meakk Oct 16, 2024
dae1295
Fix compatibility with VTK and glad (#1631)
Meakk Oct 16, 2024
95c2624
CI: Fix nightly workflow (#1668)
mwestphal Oct 17, 2024
a86bf76
Disable external tests requiring a X server (#1671)
Meakk Oct 22, 2024
9a22666
CI: Fixup nightly logic (#1673)
mwestphal Oct 23, 2024
375b067
Improve quickstart guide (#1563)
stepperpig Oct 24, 2024
c36fab6
Clear scene before adding model in f3d-web (#1677)
Meakk Oct 24, 2024
4c9bf90
Cleanup gitignore (#1679)
mwestphal Oct 27, 2024
ed0636a
Move coloring info logic into a dedicated handler (#1672)
mwestphal Oct 28, 2024
52b0ce3
Doc: Fix many small issues (#1683)
mwestphal Oct 29, 2024
9f60fda
log: Cleanup incorrect eol usages (#1684)
mwestphal Oct 30, 2024
826a619
Test: Fix an issue with drop hdri tests (#1688)
mwestphal Nov 3, 2024
71345ea
Test: Fix another interaction test (#1689)
mwestphal Nov 3, 2024
54a26dd
VTK: Update sha (#1691)
mwestphal Nov 5, 2024
ccc0212
Testing: Fix an issue with SDK image testing (#1693)
mwestphal Nov 7, 2024
d754acc
Add a command API (#1680)
mwestphal Nov 7, 2024
6484019
Interactor: Make SetViewOrbit not static (#1695)
mwestphal Nov 7, 2024
c76e3a6
don't add filename to window title if there's no file (#1699)
snoyer Nov 9, 2024
c3211f9
Add an interaction api (#1687)
mwestphal Nov 12, 2024
19fe39d
Add interaction command for dropping files (#1701)
mwestphal Nov 12, 2024
71af4dc
Patch VTK CMP0174 policy in CI (#1709)
Meakk Nov 14, 2024
6330928
Add changes commented in the pr
Yogesh9000 Nov 15, 2024
1112b54
Format code
Yogesh9000 Nov 15, 2024
2a0eef1
Remove attribute F3D_EXPORT from struct type_creation_exception
Yogesh9000 Nov 15, 2024
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
5 changes: 5 additions & 0 deletions cmake/f3dOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ function(_parse_json_option _top_json)
elseif(_option_type STREQUAL "ratio")
set(_option_actual_type "f3d::ratio_t")
set(_option_variant_type "double")
elseif(_option_type STREQUAL "vector")
set(_option_actual_type "f3d::vector3_t")
set(_option_variant_type "std::vector<double>")
set(_option_default_value_start "{")
set(_option_default_value_end "}")
endif()

# Add option to struct and methods
Expand Down
4 changes: 4 additions & 0 deletions library/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"type": "string",
"default_value": "+Y"
},
"up_direction2": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test in TestSDKOption.cxx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what am I supposed to here exactly. there doesn't seems to be test for other options in TestSDKOption.cxx

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestSDKOptions.cxx test all types of options, you are adding a new type of option, you should test it.

"type": "vector",
"default_value": "0, 1, 0"
},
"animation": {
"autoplay": {
"type": "bool",
Expand Down
85 changes: 85 additions & 0 deletions library/private/options_tools.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "types.h"

#include <algorithm>
#include <cctype>
#include <sstream>
#include <vector>
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved

namespace f3d
{
Expand Down Expand Up @@ -181,6 +183,78 @@ ratio_t parse(const std::string& str)
}
}

//----------------------------------------------------------------------------
/**
* Parse provided string into a vector3_t.
*/
template<>
vector3_t parse(const std::string& str)
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
{
auto parseCommaSeparated = [](const std::string& s) -> vector3_t
{
std::vector vec = parse<std::vector<double>>(s);
if (vec.size() == 2)
{
return f3d::vector3_t::fromSphericalCoordinates(vec[0], vec[1]);
}
if (vec.size() == 3)
{
return { vec[0], vec[1], vec[2] };
}
else
{
throw options::parsing_exception("cannot parse " + s + " to a vector3_t");
}
};
auto parseYSyntax = [](const std::string& s) -> vector3_t
{
auto ss = trim(s);
if (ss.size() != 2)
{
throw options::parsing_exception("cannot parse " + s + " to a vector3_t");
}
int sign = +1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have a logic based on regex working there:

if (std::regex_match(upString, match, re))

Feel free to copy it.
The advantage is it supports lowercase, omitting the + prefix, and errors out if invalid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yogesh9000 please take this into account.

if (ss[0] == '-')
{
sign = -1;
}
else if (ss[0] == '+')
{
sign = +1;
}
else
{
throw options::parsing_exception("cannot parse " + s + " to a vector3_t");
}
if (ss[1] == 'X')
{
return { double(sign) * 1, 0, 0 };
}
else if (ss[1] == 'Y')
{
return { 0, double(sign) * 1, 0 };
}
else if (ss[1] == 'Z')
{
return { 0, 0, double(sign) * 1 };
}
else
{
throw options::parsing_exception("cannot parse " + s + " to a vector3_t");
}
};
bool isNotCommaSeparated = std::find_if(str.begin(), str.end(),
[](unsigned char c) { return std::isalpha(c); }) != str.end();
if (!isNotCommaSeparated)
{
return parseCommaSeparated(str);
}
else
{
return parseYSyntax(str);
}
}

//----------------------------------------------------------------------------
/**
* Return provided string stripped of leading and trailing spaces.
Expand Down Expand Up @@ -270,6 +344,17 @@ std::string format(const std::vector<double>& var)
return stream.str();
}

//----------------------------------------------------------------------------
/**
* Format provided var into a string from provided vector3_t
* rely on format(double&)
*/
template<>
std::string format(const vector3_t& var)
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
{
return format(static_cast<std::vector<double>>(var));
}

//----------------------------------------------------------------------------
/**
* Generated method, see `options::set`
Expand Down
142 changes: 130 additions & 12 deletions library/public/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "export.h"

#include <array>
#include <cmath>
#include <initializer_list>
#include <stdexcept>
#include <string>
#include <vector>

Expand All @@ -21,18 +24,6 @@
}
};

/**
* Describe a 3D vector.
*/
struct F3D_EXPORT vector3_t : std::array<double, 3>
{
template<typename... Args>
vector3_t(Args&&... args)
: array({ double(std::forward<Args>(args))... })
{
}
};

/**
* Describe an angle in degrees.
*/
Expand Down Expand Up @@ -82,6 +73,133 @@
*/
F3D_EXPORT std::pair<bool, std::string> isValid() const;
};

/**
* Describe a 3D vector.
*/
struct F3D_EXPORT vector3_t
{
vector3_t() = default;
vector3_t(double x, double y, double z)
: Value{ x, y, z }
{
}
vector3_t(const std::vector<double>& vec)

Check warning on line 87 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L87

Added line #L87 was not covered by tests
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
{
if (vec.size() != 3)

Check warning on line 89 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L89

Added line #L89 was not covered by tests
{
throw std::runtime_error("cannot create a vector3_t");

Check warning on line 91 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L91

Added line #L91 was not covered by tests
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
}
Value[0] = vec[0];
Value[1] = vec[1];
Value[2] = vec[2];

Check warning on line 95 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L93-L95

Added lines #L93 - L95 were not covered by tests
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all above should be covered by tests

vector3_t(const std::array<double, 3>& arr)
{
Value[0] = arr[0];
Value[1] = arr[1];
Value[2] = arr[2];
}
vector3_t(double* ptr)
{
Value[0] = ptr[0];
Value[1] = ptr[1];
Value[2] = ptr[2];
}
vector3_t(std::initializer_list<double> l)
{
if (l.size() != 3)
{
throw std::runtime_error("cannot create a vector3_t");

Check warning on line 113 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L113

Added line #L113 was not covered by tests
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
}
std::copy(l.begin(), l.end(), std::begin(Value));
}

double* data()
{
return Value;
}
const double* data() const
{
return Value;
}

double& operator[](int idx)
{
return Value[idx];
}
double operator[](int idx) const
{
return Value[idx];
}
operator std::vector<double>() const
{
return { Value[0], Value[1], Value[2] };
}
operator std::array<double, 3>() const
{
return { Value[0], Value[1], Value[2] };
}
bool operator==(const vector3_t& vec) const
{
return Value[0] == vec.Value[0] && Value[1] == vec.Value[1] && Value[2] == vec.Value[2];
}
bool operator!=(const vector3_t& vec) const
{
return !(*this == vec);
}

double* begin()
{
return Value;
}
const double* begin() const

Check warning on line 156 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L156

Added line #L156 was not covered by tests
{
return Value;

Check warning on line 158 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L158

Added line #L158 was not covered by tests
}
const double* cbegin() const
{
return Value;
}
double* end()
{
return Value + 3;
}
const double* end() const

Check warning on line 168 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L168

Added line #L168 was not covered by tests
{
return Value + 3;

Check warning on line 170 in library/public/types.h

View check run for this annotation

Codecov / codecov/patch

library/public/types.h#L170

Added line #L170 was not covered by tests
}
const double* cend() const
{
return Value + 3;
}

static vector3_t fromSphericalCoordinates(double theta, double phi)
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
{
auto sinPhi = std::sin(phi);
auto cosTheta = std::cos(theta);
return { sinPhi * cosTheta, sinPhi * cosTheta, std::cos(phi) };
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
}
static vector3_t x()
{
return { 1, 0, 0 };
}
static vector3_t y()
{
return { 0, 1, 0 };
}
static vector3_t z()
{
return { 0, 0, 1 };
}
static vector3_t zero()
{
return { 0, 0, 0 };
}

private:
double Value[3];
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
};
}

#endif
2 changes: 2 additions & 0 deletions library/src/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "export.h"
#include "init.h"
#include "log.h"
#include "types.h"
#include "utils.h"

#include "vtkF3DConfigure.h"
Expand Down Expand Up @@ -178,6 +179,7 @@ F3D_DECL_TYPE(bool);
F3D_DECL_TYPE(int);
F3D_DECL_TYPE(double);
F3D_DECL_TYPE(f3d::ratio_t);
F3D_DECL_TYPE(f3d::vector3_t);
F3D_DECL_TYPE(std::string);

//----------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions library/testing/PseudoUnitTest.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PseudoUnitTest_h
#define PseudoUnitTest_h

#include "types.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this would be needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume it's to know about std::ostream& operator<<(std::ostream& os, const f3d::vector3_t& vec).

I think it would be better if it was not included from inside PseudoUnitTest.h (to keep that bit of code as "standalone" as possible) but I don't know if it's doable. Would it work to have #include "types.h" before #include "PseudoUnitTest.h" in TestSDKOptionsIO.cxx?

#include <functional>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -129,6 +130,18 @@ class PseudoUnitTest
return ss.str();
}

std::string toString(const f3d::vector3_t& value)
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
{
std::stringstream ss;
size_t i = 0;
for (const double& item : value)
{
ss << (i++ ? ", " : "{ ") << this->toString(item);
}
ss << " }";
return ss.str();
}

protected:
template<typename T1, typename T2>
std::string comparisonMessage(const T1& actual, const T2& expected, const std::string& comp)
Expand Down
12 changes: 12 additions & 0 deletions library/testing/TestSDKOptionsIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <options.h>

#include "PseudoUnitTest.h"
#include "types.h"

#include <iostream>

Expand Down Expand Up @@ -71,5 +72,16 @@ int TestSDKOptionsIO(int argc, char* argv[])
test.parse<std::vector<std::string>>(
"std::vector<std::string>", " foo, bar , baz ", { "foo", "bar", "baz" });

test.parse<f3d::vector3_t>("vector3_t", "1, 2, 3", { 1, 2, 3 });
test.parse<f3d::vector3_t>("vector3_t", " 1, 2, 3 ", { 1, 2, 3 });
test.parse<f3d::vector3_t>("vector3_t", "+Y", { 0, 1, 0 });
test.parse<f3d::vector3_t>("vector3_t", " +Y ", { 0, 1, 0 });
test.parse<f3d::vector3_t>("vector3_t", "-Y", { 0, -1, 0 });
test.parse<f3d::vector3_t>("vector3_t", "+X", { 1, 0, 0 });
test.parse<f3d::vector3_t>("vector3_t", "-X", { -1, 0, 0 });
test.parse<f3d::vector3_t>("vector3_t", "+Z", { 0, 0, 1 });
test.parse<f3d::vector3_t>("vector3_t", "-Z", { 0, 0, -1 });
test.parse<f3d::vector3_t>("vector3_t", "1, 2", f3d::vector3_t::fromSphericalCoordinates(1, 2));
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved

return test.result();
}
11 changes: 5 additions & 6 deletions python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@

namespace py = pybind11;

template<typename T, size_t S>
bool load_array(const py::handle& src, bool convert, std::array<T, S>& value)
template<typename T>
bool load_array(const py::handle& src, bool convert, T& value)
{
if (!py::isinstance<py::sequence>(src))
{
return false;
}
const py::sequence l = py::reinterpret_borrow<py::sequence>(src);
if (l.size() != S)
if (l.size() != 3)
mwestphal marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}

size_t i = 0;
for (auto it : l)
{
value[i++] = py::cast<T>(it);
Yogesh9000 marked this conversation as resolved.
Show resolved Hide resolved
value[i++] = py::cast<double>(it);
}
return true;
}
Expand Down Expand Up @@ -367,8 +367,7 @@ PYBIND11_MODULE(pyf3d, module)
.def_static("set_verbose_level", &f3d::log::setVerboseLevel, py::arg("level"),
py::arg("force_std_err") = false)
.def_static("set_use_coloring", &f3d::log::setUseColoring)
.def_static("print",
[](f3d::log::VerboseLevel& level, const std::string& message)
.def_static("print", [](f3d::log::VerboseLevel& level, const std::string& message)
{ f3d::log::print(level, message); });

py::enum_<f3d::log::VerboseLevel>(log, "VerboseLevel")
Expand Down
Loading