Skip to content

Commit

Permalink
Merge pull request #90 from asalzburger/feat-introduce-group-constructor
Browse files Browse the repository at this point in the history
feat: with commit this time
  • Loading branch information
asalzburger authored Oct 7, 2024
2 parents 4af91f7 + 1c1d0fd commit 3667032
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 103 deletions.
8 changes: 8 additions & 0 deletions core/include/actsvg/core/svg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ struct object {
/// Summary object
summary _summary = summary{};

/// Static constructor for a group
static object create_group(const std::string &id_) {
object g;
g._tag = "g";
g._id = id_;
return g;
}

/// An object is defined if a tag is set
bool is_defined() const;

Expand Down
64 changes: 18 additions & 46 deletions core/src/core/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ svg::object bezier(const std::string &id_,
const style::stroke &stroke_,
const style::transform &transform_) {

svg::object c;
c._tag = "g";
c._id = id_;
svg::object c = svg::object::create_group(id_);

point2 lx = {0, 0};
point2 ld = {0, 0};
Expand Down Expand Up @@ -396,15 +394,16 @@ svg::object text(const std::string &id_, const point2 &p_,
t._attribute_map["font-size"] = std::to_string(font_._size);
t._field_span = font_._size * font_._line_spacing;

// Get the longest text line
size_t l = 0;
for (const auto &tl : text_) {
l = l > tl.size() ? l : tl.size();
}

scalar fs = font_._size;

detail::adapt_range(
t, {{x, y - l}, {static_cast<scalar>(x + 0.7 * fs * l), y + l}});
detail::adapt_range(t, {{x, y - fs * text_.size()},
{static_cast<scalar>(x + 0.7 * fs * l), y + l}});

t._barycenter = utils::barycenter<std::array<scalar, 2>>(
{{x, y - l}, {static_cast<scalar>(x + 0.7 * fs * l), y + l}});
Expand Down Expand Up @@ -451,10 +450,7 @@ svg::object image_box(const std::string &id_, const std::string &href_,
const svg::object &object_,
const std::vector<std::string> &highlight_,
const std::string &onerror_) {
svg::object i;

i._tag = "g";
i._id = id_;
svg::object i = svg::object::create_group(id_);

// Image box object
svg::object imgb;
Expand Down Expand Up @@ -506,9 +502,7 @@ svg::object connected_info_box(
const style::font &text_font_, const style::stroke &stroke_,
const svg::object &object_, const std::vector<std::string> &highlight_) {

svg::object ib;
ib._tag = "g";
ib._id = id_;
svg::object ib = svg::object::create_group(id_);

size_t tew = 0;
for (const auto &t : text_) {
Expand Down Expand Up @@ -585,9 +579,7 @@ svg::object cartesian_grid(const std::string &id_,
const style::stroke &stroke_,
const style::transform &transform_) {
// The grid group object
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

scalar l0_min = l0_edges_[0];
scalar l0_max = l0_edges_[l0_edges_.size() - 1];
Expand Down Expand Up @@ -615,9 +607,7 @@ svg::object tiled_cartesian_grid(const std::string &id_,
const style::stroke &stroke_,
const style::transform &transform_) {
// The grid group object
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

// The list of grid sectors
for (size_t il0 = 1; il0 < l0_edges_.size(); ++il0) {
Expand Down Expand Up @@ -652,9 +642,7 @@ svg::object fan_grid(const std::string &id_,
const style::stroke &stroke_,
const style::transform &transform_) noexcept(false) {
// The list of grid sectors
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

if (x_low_edges_.size() != x_high_edges_.size()) {
throw std::invalid_argument(
Expand Down Expand Up @@ -698,9 +686,7 @@ svg::object tiled_fan_grid(const std::string &id_,
const style::fill &fill_,
const style::stroke &stroke_,
const style::transform &transform_) noexcept(false) {
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

// The list of grid sectors
std::vector<svg::object> grid_tiles;
Expand Down Expand Up @@ -761,9 +747,7 @@ svg::object polar_grid(const std::string &id_,
const style::stroke &stroke_,
const style::transform &transform_) {
// The list of grid sectors
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

scalar r_min = r_edges_[0];
scalar r_max = r_edges_[r_edges_.size() - 1];
Expand Down Expand Up @@ -802,9 +786,7 @@ svg::object tiled_polar_grid(const std::string &id_,
const style::fill &fill_,
const style::stroke &stroke_,
const style::transform &transform_) {
svg::object grid;
grid._tag = "g";
grid._id = id_;
svg::object grid = svg::object::create_group(id_);

for (size_t ir = 1; ir < r_edges_.size(); ++ir) {
// Grid svg object
Expand All @@ -830,8 +812,7 @@ svg::object tiled_polar_grid(const std::string &id_,

svg::object marker(const std::string &id_, const point2 &at_,
const style::marker &marker_, scalar rot_) {
svg::object marker_group;
marker_group._tag = "g";
svg::object marker_group = svg::object::create_group(id_);

std::vector<point2> arrow_head;
auto size = marker_._size;
Expand Down Expand Up @@ -901,9 +882,7 @@ svg::object measure(const std::string &id_, const point2 &start_,
const style::marker &end_marker_, const style::font &font_,
const std::string &label_, const point2 &label_pos_) {
// Measure group here we go
svg::object measure_group;
measure_group._tag = "g";
measure_group._id = id_;
svg::object measure_group = svg::object::create_group(id_);

auto mline = line(id_ + "_line", start_, end_, stroke_);
measure_group.add_object(mline);
Expand Down Expand Up @@ -934,9 +913,7 @@ svg::object arc_measure(const std::string &id_, scalar r_, const point2 &start_,
const style::font &font_, const std::string &label_,
const point2 &label_pos_) {
// Measure group here we go
svg::object measure_group;
measure_group._tag = "g";
measure_group._id = id_;
svg::object measure_group = svg::object::create_group(id_);

measure_group.add_object(
arc((id_ + "_arc"), r_, start_, end_, style::fill(), stroke_));
Expand Down Expand Up @@ -977,9 +954,7 @@ svg::object x_y_axes(const std::string &id_,
const style::stroke &stroke_, const std::string &x_label_,
const std::string &y_label_, const style::font &font_,
const style::axis_markers<2u> &markers_) {
svg::object axes;
axes._tag = "g";
axes._id = id_;
svg::object axes = svg::object::create_group(id_);

auto x =
line(id_ + "_x_axis", {x_range_[0], 0.}, {x_range_[1], 0.}, stroke_);
Expand Down Expand Up @@ -1055,8 +1030,7 @@ svg::object gradient_box(
const style::label &label_, const style::stroke &stroke_,
const style::font &font_, const style::transform t_) {

svg::object g;
g._tag = "g";
svg::object g = svg::object::create_group(id_);
g._sterile = true;

auto [w_, h_] = w_h_;
Expand Down Expand Up @@ -1141,10 +1115,8 @@ svg::object from_template(const std::string &id_, const svg::object &ro_,
const style::fill &f_, const style::stroke &s_,
const style::transform t_) {
// Create new svg object
svg::object nsvg;
svg::object nsvg = svg::object::create_group(id_);
nsvg._sterile = true;
nsvg._id = id_;
nsvg._tag = "g";

// Refer to as the linker object
svg::object use_obj;
Expand Down
16 changes: 4 additions & 12 deletions meta/include/actsvg/display/datamodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ std::pair<svg::object, svg::object> cluster(
const style::fill& fill_m_ = style::fill{style::color{{0, 0, 255}}},
const std::array<unsigned int, 2>& expand_ = {2, 2}) {

svg::object cluster_group;
cluster_group._tag = "g";
cluster_group._id = id_;
svg::object cluster_group = svg::object::create_group(id_);

// Grid indices
unsigned int i_min = std::numeric_limits<unsigned int>::max();
Expand Down Expand Up @@ -293,9 +291,7 @@ std::pair<svg::object, svg::object> cluster(
}

// Get the focussed grid
svg::object grid_area;
grid_area._tag = "g";
grid_area._id = id_ + std::string("_focussed_grid");
svg::object grid_area = svg::object::create_group(id_ + "_focussed_grid");

// Expand
i_min = (i_min >= expand_[0]) ? i_min - expand_[0] : 0;
Expand Down Expand Up @@ -331,9 +327,7 @@ template <typename trajectory_type, typename view_type>
svg::object trajectory(const std::string& id,
const trajectory_type& trajectory_,
const view_type& view_, bool bezier_ = false) {
svg::object trajectory_group;
trajectory_group._tag = "g";
trajectory_group._id = id;
svg::object trajectory_group = svg::object::create_group(id);

std::vector<point2> points;
points.reserve(trajectory_._path.size());
Expand Down Expand Up @@ -393,9 +387,7 @@ svg::object trajectory(const std::string& id,
template <typename seed_type, typename view_type>
svg::object seed(const std::string& id_, const seed_type& seed_,
const view_type& view_) {
svg::object seed_group;
seed_group._tag = "g";
seed_group._id = id_;
svg::object seed_group = svg::object::create_group(id_);

// Draw the tranjectory if it exist
if (seed_._trajectory.has_value()) {
Expand Down
28 changes: 7 additions & 21 deletions meta/include/actsvg/display/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ svg::object surface(const std::string& id_, const surface_type& s_,
if (o_._label_measures) {
// make a copy & a group out of the object
auto sc = s;
s = svg::object{};
s._tag = "g";
s._id = id_ + "_labeled_group";
s = svg::object::create_group(id_ + "_labeled_group");
s.add_object(sc);

// Draw min / max circles
Expand Down Expand Up @@ -515,9 +513,7 @@ svg::object surface(const std::string& id_, const surface_type& s_,
template <typename surface_type, typename view_type>
svg::object oriented_polygon(const std::string& id_, const surface_type& s_,
const view_type& v_) {
svg::object s;
s._tag = "g";
s._id = id_;
svg::object s = svg::object::create_group(id_);
s._fill._sterile = true;
s._stroke._sterile = true;

Expand Down Expand Up @@ -574,9 +570,7 @@ svg::object portal_link(const std::string& id_,
[[maybe_unused]] const portal_type& p_,
const typename portal_type::link& link_,
const view_type& v_) {
svg::object l;
l._tag = "g";
l._id = id_;
svg::object l = svg::object::create_group(id_);

scalar d_z = static_cast<scalar>(link_._end[2u] - link_._start[2u]);
// View activation / deactivation
Expand All @@ -593,9 +587,7 @@ svg::object portal_link(const std::string& id_,

if (std::is_same_v<view_type, views::x_y> and
d_r <= std::numeric_limits<scalar>::epsilon()) {
svg::object arr_xy;
arr_xy._tag = "g";
arr_xy._id = id_ + "_arrow";
svg::object arr_xy = svg::object::create_group(id_ + "_arrow");
arr_xy.add_object(draw::circle(id_ + "_arrow_top",
{static_cast<scalar>(link_._start[0u]),
static_cast<scalar>(link_._start[1u])},
Expand Down Expand Up @@ -653,9 +645,7 @@ svg::object portal_link(const std::string& id_,
template <typename portal_type, typename view_type>
svg::object portal(const std::string& id_, const portal_type& p_,
const view_type& v_) {
svg::object p;
p._tag = "g";
p._id = id_;
svg::object p = svg::object::create_group(id_);
p._fill._sterile = true;
p._stroke._sterile = true;

Expand All @@ -681,9 +671,7 @@ svg::object portal(const std::string& id_, const portal_type& p_,
template <typename volume_type, typename view_type>
svg::object volume(const std::string& id_, const volume_type& dv_,
const view_type& v_, bool p_ = true, bool s_ = true) {
svg::object v;
v._tag = "g";
v._id = id_;
svg::object v = svg::object::create_group(id_);
v._fill._sterile = true;
v._stroke._sterile = true;

Expand Down Expand Up @@ -751,9 +739,7 @@ svg::object volume(const std::string& id_, const volume_type& dv_,
template <typename detector_type, typename view_type>
svg::object detector(const std::string& id_, const detector_type& d_,
const view_type& v_) {
svg::object d;
d._tag = "g";
d._id = id_;
svg::object d = svg::object::create_group(id_);
d._fill._sterile = true;
d._stroke._sterile = true;

Expand Down
18 changes: 6 additions & 12 deletions meta/include/actsvg/display/sheets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ svg::object surface_sheet_xy(const std::string& id_,
const proto::surface<point3_container>& s_,
const std::array<scalar, 2>& sh_ = {400., 400.},
bool fs_ = false) noexcept(false) {
svg::object so;
so._tag = "g";
so._id = id_;
svg::object so = svg::object::create_group(id_);

views::x_y x_y_view;

Expand Down Expand Up @@ -550,9 +548,7 @@ svg::object sheet(const std::string& id_,
sheet_type t_ = e_module_info,
const std::array<scalar, 2>& s_sh_ = {600., 600.}) {

svg::object o_sheet;
o_sheet._tag = "g";
o_sheet._id = id_;
svg::object o_sheet = svg::object::create_group(id_);

view_type view;

Expand Down Expand Up @@ -598,9 +594,8 @@ svg::object sheet(const std::string& id_,
s_fill._fc._opacity = s._fill._fc._opacity;
s._fill = s_fill;
// The template sheet
svg::object s_sheet;
s_sheet._id = s._name + "_surface_sheet";
s_sheet._tag = "g";
svg::object s_sheet =
svg::object::create_group(s._name + "_surface_sheet");
// Background panel
auto bg_panel =
draw::rectangle(s._name + "_surface_sheet_bg", {0, 0},
Expand Down Expand Up @@ -667,9 +662,8 @@ svg::object sheet(const std::string& id_,
// Add the modules & eventual extra objects
for (auto [ib, module_batch] : utils::enumerate(modules)) {

svg::object sub_sheet;
sub_sheet._tag = "g";
sub_sheet._id = id_ + "_sub_sheet_" + std::to_string(ib);
svg::object sub_sheet =
svg::object::create_group(id_ + "_sub_sheet" + std::to_string(ib));
sub_sheet.add_objects(module_batch);
// Add the axes on top @todo add autmated font size adaption
auto axis_font = __a_font;
Expand Down
4 changes: 1 addition & 3 deletions meta/src/display/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ svg::object eta_lines(
style::font>>& els_,
const style::transform& tr_) {

svg::object e;
e._tag = "g";
e._id = id_;
svg::object e = svg::object::create_group(id_);
e._transform = tr_;

auto theta_from_eta = [](scalar eta) -> scalar {
Expand Down
4 changes: 1 addition & 3 deletions meta/src/display/grids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ namespace display {
svg::object grid(const std::string& id_, const proto::grid& g_) {

// The grid object
svg::object g;
g._id = id_;
g._tag = "g";
svg::object g = svg::object::create_group(id_);

// Get the tiles
std::vector<svg::object> grid_tiles;
Expand Down
4 changes: 1 addition & 3 deletions meta/src/display/materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ namespace display {
svg::object surface_material(const std::string& id_,
const proto::surface_material& m_) {

svg::object m;
m._tag = "g";
m._id = id_;
svg::object m = svg::object::create_group(id_);
m._sterile = true;

// Create the grid object
Expand Down
Loading

0 comments on commit 3667032

Please sign in to comment.