Skip to content

Commit

Permalink
Merge pull request #79 from asalzburger/feat-python-for-annulus-json
Browse files Browse the repository at this point in the history
feat: python for annulus
  • Loading branch information
asalzburger authored Aug 7, 2024
2 parents dc39131 + e9e8c5b commit 18ef062
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 29 deletions.
7 changes: 6 additions & 1 deletion meta/include/actsvg/display/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ svg::object surface(const std::string& id_, const surface_type& s_,

// Dedicated path drawing of the annulus bounds
s._tag = "path";
s._id = id_;
std::string path = "M " + std::to_string(in_right_s_xy[0]) + " " +
std::to_string(-in_right_s_xy[1]);
path += " A " + std::to_string(min_r) + " " + std::to_string(min_r);
Expand All @@ -106,7 +107,11 @@ svg::object surface(const std::string& id_, const surface_type& s_,
s._attribute_map["d"] = path;
s._fill = s_._fill;
s._stroke = s_._stroke;

s._transform = draw_transform;
if (not fs_) {
s._x_range = {-max_r, max_r};
s._y_range = {-max_r, max_r};
}
} else if (s_._type == surface_type::type::e_disc) {

// x-y view for discs
Expand Down
5 changes: 4 additions & 1 deletion meta/include/actsvg/proto/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct surface {
point3 _translation = {0., 0., 0.};
// original rotation
std::array<point3, 3u> _rotation = {
{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}};
point3{1., 0., 0.}, point3{0., 1., 0.}, point3{0., 0., 1.}};
// Rotate a point to the global frame
point3 rotate(const point3& p_) const {

Expand Down Expand Up @@ -70,6 +70,9 @@ struct surface {
ret[2] += _translation[2];
return ret;
}

// The identity tranform
static transform3 identity() { return transform3{}; }
};

enum class type {
Expand Down
8 changes: 8 additions & 0 deletions meta/include/actsvg/styles/defaults.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ extern style::transform __t_identity;
extern style::fill __w_fill;
extern style::stroke __w_stroke;

// Red fill, red stroke
extern style::fill __r_fill;
extern style::stroke __r_stroke;

// Black fill, black stroke
extern style::fill __bl_fill;
extern style::stroke __bl_stroke;

// No fill, no stroke
extern style::fill __nn_fill;
extern style::stroke __nn_stroke;
Expand Down
12 changes: 12 additions & 0 deletions meta/src/styles/defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ style::stroke __bg_stroke;
style::transform __t_identity;
style::fill __w_fill;
style::stroke __w_stroke;
style::fill __r_fill;
style::stroke __r_stroke;
style::fill __bl_fill;
style::stroke __bl_stroke;
style::fill __nn_fill;
style::stroke __nn_stroke;
style::gradient __rgb_gradient;
Expand Down Expand Up @@ -91,6 +95,14 @@ static bool create_defaults() {
__w_fill._fc._rgb = {255, 255, 255};
__w_stroke._sc._rgb = {255, 255, 255};

// Reds
__r_fill._fc._rgb = {1, 0, 0};
__r_stroke._sc._rgb = {1, 0, 0};

// Blacks
__bl_fill._fc._rgb = {0, 0, 0};
__bl_stroke._sc._rgb = {0, 0, 0};

// Nulls
__nn_fill = style::fill();

Expand Down
41 changes: 27 additions & 14 deletions python/python/actsvg/actsvg_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
""" read a grid axis from a json file"""


@staticmethod
def read_grid_axis(json_grid_axis):
edges = []
if json_grid_axis["type"] == 0:
Expand All @@ -20,7 +19,6 @@ def read_grid_axis(json_grid_axis):
""" read a grid from a json file"""


@staticmethod
def read_grid(json_grid):
grid = proto.grid()
try:
Expand All @@ -38,7 +36,6 @@ def read_grid(json_grid):
""" read surface mateiral from a json file"""


@staticmethod
def read_surface_material(json_surface_material):
psm = proto.surface_material()
try:
Expand Down Expand Up @@ -90,7 +87,6 @@ def read_surface_material(json_surface_material):
""" read surface material maps from a json file """


@staticmethod
def read_surface_material_maps(json_surface_material_maps):
proto_maps = []
try:
Expand All @@ -107,7 +103,6 @@ def read_surface_material_maps(json_surface_material_maps):
""" read a surface from a json file"""


@staticmethod
def read_surface(json_surface, apply_transform=True):

# Construct the name
Expand Down Expand Up @@ -154,6 +149,15 @@ def read_surface(json_surface, apply_transform=True):
(hx_maxy, hy, 0.0),
(-hx_maxy, hy, 0.0),
]
# Trapezoid translates into a polygon
ps = proto.surface.polygon_from_vertices_and_transform(
surface_name,
surface_vertices,
surface_translation,
surface_rotation,
style.defaults.sensitive_fill(),
style.defaults.sensitive_stroke(),
)
elif bounds_type == "RectangleBounds":
hx = bounds_values[0]
hy = bounds_values[1]
Expand All @@ -163,17 +167,26 @@ def read_surface(json_surface, apply_transform=True):
(hx, hy, 0.0),
(-hx, hy, 0.0),
]
# Rectangle translates into a polygon
ps = proto.surface.polygon_from_vertices_and_transform(
surface_name,
surface_vertices,
surface_translation,
surface_rotation,
style.defaults.sensitive_fill(),
style.defaults.sensitive_stroke(),
)
elif bounds_type == "AnnulusBounds":
ps = proto.surface.annulus_from_bounds_and_transform(
surface_name,
bounds_values,
surface_translation,
surface_rotation,
style.defaults.sensitive_fill(),
style.defaults.sensitive_stroke(),
)
else:
print("** pyactsvg **: `json.read_surface` bounds type not (yet) supported")
raise ValueError

ps = proto.surface.polygon_from_vertices_and_transform(
surface_name,
surface_vertices,
surface_translation,
surface_rotation,
style.defaults.sensitive_fill(),
style.defaults.sensitive_stroke(),
)

return ps
49 changes: 44 additions & 5 deletions python/src/proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,57 @@ void add_proto_module(context& ctx) {
const style::fill& f, const style::stroke& s) {
// Create the surface
surface sf{};
// Set the predefined transform
sf._surface_transform =
surface::transform3{translation, rotation};
sf._name = name;
auto identity = surface::transform3::identity();
if (translation != identity._translation ||
rotation != identity._rotation) {
// Set the predefined transform
sf._surface_transform =
surface::transform3{translation, rotation};
}
sf._vertices = pcs;
sf._fill = f;
sf._stroke = s;

return sf;
},
py::arg("name"), py::arg("vertices"),
py::arg("translation"), py::arg("rotation"),
py::arg("fill"), py::arg("stroke"));
py::arg("fill"), py::arg("stroke"))
.def_static(
"annulus_from_bounds_and_transform",
[](const std::string& name,
const std::vector<scalar>& bounds,
const point3& translation,
const std::array<point3, 3u>& rotation,
const style::fill& f, const style::stroke& s) {
// Create the surface
surface sf{};
sf._name = name;
sf._type = surface::type::e_annulus;
sf._measures = bounds;
auto identity = surface::transform3::identity();
if (translation != identity._translation ||
rotation != identity._rotation) {
// Set the predefined transform
sf._surface_transform =
surface::transform3{translation, rotation};
// Only x, y view will be transformed correctly
point3 sfx = rotation[0];
scalar tr_y = (sfx[1] > 0.) ? -translation[1]
: translation[1];
// Calculate the rotation angle
sf._transform._tr = {translation[0], tr_y};
scalar alpha = std::acos(sfx[0]) / M_PI * 180;
alpha += (sfx[1] > 0.) ? 180 : 0;
sf._transform._rot = {alpha, 0., 0.};
}
sf._fill = f;
sf._stroke = s;

return sf;
},
py::arg("name"), py::arg("boounds"), py::arg("translation"),
py::arg("rotation"), py::arg("fill"), py::arg("stroke"));
}

{
Expand Down
26 changes: 18 additions & 8 deletions python/src/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "actsvg/core/style.hpp"
#include "actsvg/styles/defaults.hpp"

#include <pybind11/eval.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <memory>

#include "actsvg/styles/defaults.hpp"
#include "utilities.hpp"

namespace py = pybind11;
Expand Down Expand Up @@ -106,15 +106,25 @@ void add_style_module(context& ctx) {
.def_readwrite("scale", &style::transform::_scale)
.def_readwrite("skew", &style::transform::_skew);
}
{
// The style defaults
auto d = s.def_submodule("defaults");
{
// The style defaults
auto d = s.def_submodule("defaults");

d.def("sensitive_fill", [](){ return defaults::__s_fill; });
d.def("sensitive_stroke", [](){ return defaults::__s_stroke; });

}
d.def("sensitive_fill", []() { return defaults::__s_fill; });
d.def("sensitive_stroke", []() { return defaults::__s_stroke; });

// Some handy defaults
d.def("black_fill", []() { return defaults::__bl_fill; });
d.def("black_stroke", []() { return defaults::__bl_stroke; });

d.def("red_fill", []() { return defaults::__r_fill; });
d.def("red_stroke", []() { return defaults::__r_stroke; });

d.def("white_fill", []() { return defaults::__w_fill; });
d.def("white_stroke", []() { return defaults::__w_stroke; });

d.def("font", []() { return defaults::__t_font; });
}
}
} // namespace python
} // namespace actsvg

0 comments on commit 18ef062

Please sign in to comment.