Skip to content

Commit

Permalink
Visualization working.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredevb committed Aug 3, 2023
1 parent 879cda2 commit 214b7e4
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 42 deletions.
2 changes: 2 additions & 0 deletions core/include/detray/definitions/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ struct unit {
static constexpr scalar_t rad{static_cast<scalar_t>(1.0)};
static constexpr scalar_t degree{
static_cast<scalar_t>(0.017453292519943295)}; // pi / 180
static constexpr scalar_t rad_to_deg = {
static_cast<scalar_t>(57.2957795130823208768)};
};

/// Physical and mathematical constants
Expand Down
5 changes: 4 additions & 1 deletion core/include/detray/masks/cuboid3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ class cuboid3D {
typename std::enable_if_t<kDIM == e_size, bool> = true>
DETRAY_HOST inline point_t closest_surface_point(
const bounds_t<scalar_t, kDIM>& bounds, const point_t& loc_p) const {
return point_t{};
const scalar_t x = std::clamp(loc_p[0], bounds[e_min_x], bounds[e_max_x]);
const scalar_t y = std::clamp(loc_p[1], bounds[e_min_z], bounds[e_max_z]);
const scalar_t z = std::clamp(loc_p[2], bounds[e_min_z], bounds[e_max_z]);
return point_t{x, y, z};
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ namespace detray::actsvg_visualization::detector {
struct detector_options{
// The volume options
volume::volume_options v_options;
// Indexes of the visible volumes.
std::vector<int> visible_volumes;
};

/*
/// @brief Calculates the proto detector of a detray detector.
///
/// @param d_detector The detray detector.
Expand Down Expand Up @@ -47,5 +45,19 @@ auto to_svg(const typename detector_t::geometry_context& context, const view_t&
{
auto p_detector = to_proto_detector(context, detector, d_options);
return actsvg::display::detector(name, p_detector, view);
}*/

template <typename detector_t, typename view_t>
auto to_svg(const typename detector_t::geometry_context& context, const view_t& view, const detector_t& detector, const detector_options& d_options, const std::string& identification)
{
actsvg::svg::object ret;
ret._tag = "g";
ret._id = identification;
for (size_t i = 0; i < detector.volumes().size(); i++){
const auto d_volume = detector.volume_by_index(static_cast<int>(i));
const auto svg = volume::to_svg(context, view, detector, d_volume, d_options.v_options, "volume_" + std::to_string(i));
ret.add_object(svg);
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct link_start_functor {
}

struct link_options{

double length;
};

/// @returns The actsvg proto link from one point to another.
Expand All @@ -117,8 +117,8 @@ inline auto proto_links(const typename detector_t::geometry_context& context, co
typename detector_t::point3 dir{};
const auto start = d_portal.template visit_mask<link_start_functor>(d_portal.transform(context), dir);
const auto n = d_portal.normal(context, d_portal.global_to_local(context, start, dir));
const auto pos_end = (n*3.) + start;
const auto neg_end = (n*-3.) + start;
const auto pos_end = (n*l_options.length) + start;
const auto neg_end = (n*-l_options.length) + start;
return std::array{to_proto_link(start, neg_end), to_proto_link(start, pos_end)};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@

namespace detray::actsvg_visualization {

constexpr detector::detector_options default_options{};
const detector::detector_options default_options{
.v_options = volume::volume_options{
.s_options = surface::surface_options{
{{{0, 128, 0}, 0.9}, {{34, 139, 34}, 0.9}, {{60, 179, 113}, 0.9}, {{85, 107, 47}, 0.9}, {{124, 252, 0}, 0.9}, {{154, 205, 50}, 0.9}}
},
.p_options = surface::portal_options{
.s_options = surface::surface_options{
{{{128, 0, 128}, 0.9}, {{138, 43, 226}, 0.9}, {{148, 0, 211}, 0.9}, {{160, 32, 240}, 0.9}, {{186, 85, 211}, 0.9}, {{218, 112, 214}, 0.9}}
},
.l_options = link::link_options{
3.
}
}
}


};

class options_builder {
public:
Expand All @@ -25,18 +41,6 @@ constexpr detector::detector_options default_options{};
return _options;
}

/*auto& portal_visible(bool value){
return *this;
}
auto& surface_visible(bool value){
return *this;
}
auto& link_visible(bool value){
return *this;
}*/

private:
detector::detector_options _options;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void set_vertices(conversion_types::surface& p_surface, const container_t& verti
///
/// @param shape An annulus2D.
template <typename detector_t, typename bounds_t>
auto convert_surface(const typename detector_t::geometry_context& context, const detray::surface<detector_t>& d_surface, const detray::annulus2D<>& shape, const bounds_t& bounds)
auto to_proto_surface(const typename detector_t::geometry_context& context, const detray::surface<detector_t>& d_surface, const detray::annulus2D<>& shape, const bounds_t& bounds)
{
//Rotation for circular objects is currently not supported.
assert(d_surface.transform.rotation(context) == typename detector_t::transform3{});
Expand Down Expand Up @@ -67,7 +67,7 @@ auto to_proto_surface(const typename detector_t::geometry_context& context, cons
auto nhz = static_cast<actsvg::scalar>(bounds[shape.e_n_half_z]);
auto phz = static_cast<actsvg::scalar>(bounds[shape.e_p_half_z]);
p_surface._type = conversion_types::surface::type::e_cylinder;
p_surface._radii = {0., r};
p_surface._radii = {static_cast<actsvg::scalar>(0), r};
auto hz = (phz-nhz)/2;
p_surface._zparameters = {nhz + hz, hz};
return p_surface;
Expand All @@ -90,7 +90,7 @@ auto to_proto_surface(const typename detector_t::geometry_context& context, cons
auto center = transform::convert_point<3>(d_surface.center(context));
p_surface._type = conversion_types::surface::type::e_disc;
p_surface._radii = {ri, ro};
p_surface._zparameters = {center[2], 0};
p_surface._zparameters = {center[2], static_cast<actsvg::scalar>(0)};
return p_surface;
}

Expand Down Expand Up @@ -118,11 +118,18 @@ struct to_proto_surface_functor {
}
};

/// @brief Picks a random element in the container.
template <typename container_t>
auto pick_random(container_t container){
int idx = rand() % container.size();
return container[idx];
}

}

struct surface_options
{

const std::vector<actsvg::style::color> fill_colors;
};

struct portal_options
Expand All @@ -131,6 +138,11 @@ struct portal_options
link::link_options l_options;
};

void style(conversion_types::surface& p_surface, const surface_options& s_options){
auto fill_color = pick_random(s_options.fill_colors);
p_surface._fill = actsvg::style::fill(fill_color);
p_surface._stroke = actsvg::style::stroke(fill_color, 1);
}

/// @brief Calculates the proto surface of a surface.
///
Expand All @@ -146,7 +158,9 @@ auto to_proto_surface(
const detray::surface<detector_t>& d_surface,
const surface_options& s_options
) {
return d_surface.template visit_mask<to_proto_surface_functor>(context, d_surface);
auto p_surface = d_surface.template visit_mask<to_proto_surface_functor>(context, d_surface);
style(p_surface, s_options);
return p_surface;
}

/// @returns An actsvg proto portal representing the portal.
Expand All @@ -162,19 +176,20 @@ auto to_proto_portal(const typename detector_t::geometry_context& context, const
p_portal._volume_links = {l1, l2};
}
p_portal._surface = to_proto_surface(context, d_portal, p_options.s_options);
style(p_portal._surface, p_options.s_options);
return p_portal;
}

template <typename detector_t, typename view_t>
auto to_svg(const typename detector_t::geometry_context& context, const view_t& view, const detray::surface<detector_t>& d_surface, const surface_options& s_options, const portal_options& p_options, const std::string& name)
auto to_svg(const typename detector_t::geometry_context& context, const view_t& view, const detray::surface<detector_t>& d_surface, const surface_options& s_options, const portal_options& p_options, const std::string& identification)
{
if (d_surface.is_portal())
{
auto p_portal = to_proto_portal(context, d_surface, p_options);
return actsvg::display::portal(name, p_portal, view);
return actsvg::display::portal(identification, p_portal, view);
}
auto p_surface = to_proto_surface(context, d_surface, s_options);
return actsvg::display::surface(name, p_surface, view);
return actsvg::display::surface(identification, p_surface, view);
}

} // namespace detray::actsvg_visualization
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "actsvg/proto/surface.hpp"

// System include(s)
#include <assert.h>
#include <type_traits>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ inline std::array<detray::scalar, 3> mat_to_euler(
const matrix_t& matrix) {
float a = std::sqrt(matrix[0][0] * matrix[0][0] + matrix[1][0] * matrix[1][0]);
// Checking if it is singular.
if (a < 1e-6)
return {std::atan2(-matrix[1][2], matrix[1][1]), std::atan2(-matrix[2][0], a),
0};

return {std::atan2(matrix[2][1], matrix[2][2]), std::atan2(-matrix[2][0], a),
std::atan2(matrix[1][0], matrix[0][0])};
if (a < 1e-6){
return {std::atan2(-matrix[1][2], matrix[1][1]), std::atan2(-matrix[2][0], a), 0};
}
return {std::atan2(matrix[2][1], matrix[2][2]), std::atan2(-matrix[2][0], a), std::atan2(matrix[1][0], matrix[0][0])};
}

/// @brief Calculates the detray point3 as an actsvg point.
Expand Down Expand Up @@ -59,17 +57,16 @@ template <typename transform_t>
inline auto to_actsvg_transform(const transform_t& d_transform) {
auto translation = d_transform.translation();
auto euler_angles =
rotation_matrix_to_euler_angles<>(d_transform.rotation());
rotation_matrix_to_euler_angles(d_transform.rotation());

auto ret = actsvg::style::transform();
constexpr auto rad_to_deg = 180.0 / 3.14;

// The translate(<x> [<y>]) transform function moves the object by x and y.
ret._tr = {static_cast<actsvg::scalar>(translation[0]),
static_cast<actsvg::scalar>(translation[1])};

// The rotate(<a> [<x> <y>]) transform function specifies a rotation by a degrees about a given point which is (0,0) here.
ret._rot = {static_cast<actsvg::scalar>(euler_angles[2] * rad_to_deg),
ret._rot = {static_cast<actsvg::scalar>(euler_angles[2] * detray::unit<detray::scalar>::rad_to_deg),
static_cast<actsvg::scalar>(0),
static_cast<actsvg::scalar>(0)};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace detray::actsvg_visualization::volume {
struct volume_options{
surface::surface_options s_options;
surface::portal_options p_options;
// Indexes of the visible surfaces/portals.
std::vector<int> visible_surfaces;
};

/// @brief Calculates the proto volume of a detray volume.
Expand Down Expand Up @@ -54,9 +52,9 @@ const volume_options& v_options) {
}

template <typename detector_t, typename view_t>
auto to_svg(const typename detector_t::geometry_context& context, const view_t& view, const detector_t& detector, const detray::detector_volume<detector_t>& d_volume, const volume_options& v_options, const std::string& name)
auto to_svg(const typename detector_t::geometry_context& context, const view_t& view, const detector_t& detector, const detray::detector_volume<detector_t>& d_volume, const volume_options& v_options, const std::string& identification)
{
auto p_volume = volume::to_proto_volume(context, detector, d_volume, v_options);
return actsvg::display::volume(name, p_volume, view);
return actsvg::display::volume(identification, p_volume, view);
}
}

0 comments on commit 214b7e4

Please sign in to comment.