-
-
Notifications
You must be signed in to change notification settings - Fork 234
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
base: master
Are you sure you want to change the base?
Refactor vector3_t #1645 #1648
Changes from 7 commits
06d661f
1207b57
e3572cf
9eb7747
d8e4ad9
f1b01d4
efab6ad
eff24d8
6761e04
b87313f
d6d4eaa
7bcad39
1d86b73
47df654
73feb45
e34433b
137de48
12ddb7b
f492392
1fda9e2
b4fe960
ca02c81
849fe6a
6461dd5
91b0743
043e931
ce01a06
2ffeeba
9fb9c25
02837ed
7a810e5
dae1295
95c2624
a86bf76
9a22666
375b067
c36fab6
4c9bf90
ed0636a
52b0ce3
9f60fda
826a619
71345ea
54a26dd
ccc0212
d754acc
6484019
c76e3a6
c3211f9
19fe39d
71af4dc
6330928
1112b54
2a0eef1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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 | ||||
{ | ||||
|
@@ -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; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do have a logic based on regex working there: f3d/vtkext/private/module/vtkF3DRenderer.cxx Line 236 in 33771c5
Feel free to copy it. The advantage is it supports lowercase, omitting the + prefix, and errors out if invalid.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||
|
@@ -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` | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
#include "export.h" | ||
|
||
#include <array> | ||
#include <cmath> | ||
#include <initializer_list> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
@@ -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. | ||
*/ | ||
|
@@ -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) | ||
Yogesh9000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (vec.size() != 3) | ||
{ | ||
throw std::runtime_error("cannot create a vector3_t"); | ||
Yogesh9000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
Value[0] = vec[0]; | ||
Value[1] = vec[1]; | ||
Value[2] = vec[2]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
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 | ||
{ | ||
return Value; | ||
} | ||
const double* cbegin() const | ||
{ | ||
return Value; | ||
} | ||
double* end() | ||
{ | ||
return Value + 3; | ||
} | ||
const double* end() const | ||
{ | ||
return Value + 3; | ||
} | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#ifndef PseudoUnitTest_h | ||
#define PseudoUnitTest_h | ||
|
||
#include "types.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why this would be needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would assume it's to know about I think it would be better if it was not included from inside |
||
#include <functional> | ||
#include <iostream> | ||
#include <sstream> | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.