Skip to content

Commit

Permalink
added exception and other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogesh9000 committed Oct 13, 2024
1 parent d6d4eaa commit 7bcad39
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
42 changes: 26 additions & 16 deletions library/public/types.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
#ifndef f3d_types_h
#define f3d_types_h

#include "exception.h"
#include "export.h"

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

namespace f3d
{

/**
* An exception that can be thrown when we fail to create a type
*/
struct type_creation_exception : public exception
{
explicit type_creation_exception(const std::string& what = "")
: exception(what)
{
}
};

/**
* Describe a 3D point.
*/
Expand Down Expand Up @@ -89,7 +101,7 @@ struct F3D_EXPORT vector3_t
{
if (vec.size() != 3)
{
throw std::runtime_error("cannot create a vector3_t");
throw type_creation_exception("cannot create a vector3_t");
}
Value[0] = vec[0];
Value[1] = vec[1];
Expand All @@ -111,7 +123,7 @@ struct F3D_EXPORT vector3_t
{
if (l.size() != 3)
{
throw std::runtime_error("cannot create a vector3_t");
throw type_creation_exception("cannot create a vector3_t");
}
std::copy(l.begin(), l.end(), std::begin(Value));
}
Expand Down Expand Up @@ -154,10 +166,10 @@ struct F3D_EXPORT vector3_t
{
return Value.begin();
}
auto begin() const
{
return Value.begin();
}
// auto begin() const
// {
// return Value.begin();
// }
auto cbegin() const
{
return Value.cbegin();
Expand All @@ -166,10 +178,10 @@ struct F3D_EXPORT vector3_t
{
return Value.end();
}
auto end() const
{
return Value.end();
}
// auto end() const
// {
// return Value.end();
// }
auto cend() const
{
return Value.cend();
Expand All @@ -178,8 +190,7 @@ struct F3D_EXPORT vector3_t
static vector3_t fromSphericalCoordinates(double theta, double phi)
{
auto sinPhi = std::sin(phi);
auto cosTheta = std::cos(theta);
return { sinPhi * cosTheta, sinPhi * cosTheta, std::cos(phi) };
return { sinPhi * std::cos(theta), sinPhi * std::sin(theta), std::cos(phi) };
}
static vector3_t x()
{
Expand All @@ -204,10 +215,9 @@ struct F3D_EXPORT vector3_t

inline std::ostream& operator<<(std::ostream& os, const f3d::vector3_t& vec)
{
size_t i = 0;
for (auto val : vec)
for (size_t i = 0; i < 3; ++i)
{
os << (i++ ? ", " : "{ ") << val;
os << (i == 0 ? ", " : "{ ") << vec[i];
}
os << " }";
return os;
Expand Down
4 changes: 3 additions & 1 deletion library/testing/TestSDKOptionsIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ int TestSDKOptionsIO(int argc, char* argv[])
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));
test.parse<f3d::vector3_t>("vector3_t", "0, 0", { 0, 0, 1 });
test.expect<f3d::type_creation_exception>(
"cannot create a vector3_t", [&]() { f3d::vector3_t{ 1., 2., 3., 4. }; });

return test.result();
}
5 changes: 2 additions & 3 deletions python/F3DPythonBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool load_array(const py::handle& src, bool convert, std::array<T, S>& value)
return false;
}
const py::sequence l = py::reinterpret_borrow<py::sequence>(src);
if (l.size() != S)
if (l.size() != 3)
{
return false;
}
Expand Down Expand Up @@ -387,8 +387,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

0 comments on commit 7bcad39

Please sign in to comment.