Skip to content

Commit

Permalink
Merge pull request #2282 from itischler/torus
Browse files Browse the repository at this point in the history
Added a torus shape
  • Loading branch information
fweik authored Sep 25, 2018
2 parents 97f7df4 + 2cfc028 commit 960f332
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/shapes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(Shapes_SRC
Sphere.cpp
SpheroCylinder.cpp
Stomatocyte.cpp
Torus.cpp
Wall.cpp
)
add_library(Shapes SHARED ${Shapes_SRC})
Expand Down
24 changes: 24 additions & 0 deletions src/core/shapes/Torus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "Torus.hpp"
#include "utils.hpp"
#include <cmath>

namespace Shapes {

int Torus::calculate_dist(const double *ppos, double *dist, double *vec) const {
/* Coordinate transform to cylinder coords
with origin at m_center. */
Vector3d const c_dist = Vector3d(ppos, ppos + 3) - m_center;
auto const z = e_z * c_dist;
auto const r_vec = c_dist - z * e_z;
auto const r = r_vec.norm();

*dist = (sqrt(Utils::sqr(r - m_rad) + z * z) - m_tube_rad) * m_direction;
Vector3d const dir_vec = c_dist - r_vec * m_rad / r;
auto const dir_vec_norm = dir_vec / dir_vec.norm();
vec[0] = dir_vec_norm[0] * m_direction;
vec[1] = dir_vec_norm[1] * m_direction;
vec[2] = dir_vec_norm[2] * m_direction;

return 0;
}
} // namespace Shapes
59 changes: 59 additions & 0 deletions src/core/shapes/Torus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef __TORUS_HPP
#define __TORUS_HPP

#include "Shape.hpp"
#include "Vector.hpp"

namespace Shapes {
class Torus : public Shape {
public:
/* center of the cylinder. */
Vector3d m_center;
/* Normal axis of the cylinder. */
Vector3d m_normal;
/* radius. */
double m_rad;
/* tube radius. */
double m_tube_rad;
/* direction -1: inside, +1 outside */
double m_direction;

/* Unit vector in z direction */
Vector3d e_z;

/** @brief Calculate derived parameters. */
void precalc() { e_z = m_normal / m_normal.norm(); }

public:
Torus()
: m_center({0.0, 0.0, 0.0}), m_normal({1.0, 0.0, 0.0}), m_rad(0.0),
m_tube_rad(0.0), m_direction(1.0) {
precalc();
}

double radius() const { return m_rad; }
void set_radius(double const &radius) {
m_rad = radius;
precalc();
}

double tube_radius() const { return m_tube_rad; }
void set_tube_radius(double const &tube_rad) {
m_tube_rad = tube_rad;
precalc();
}

Vector3d const &normal() const { return m_normal; }
void set_normal(Vector3d const &normal) {
m_normal = normal;
precalc();
}

Vector3d &center() { return m_center; }
double &direction() { return m_direction; }

int calculate_dist(const double *ppos, double *dist,
double *vec) const override;
};
} // namespace Shapes
#endif
23 changes: 23 additions & 0 deletions src/python/espressomd/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,29 @@ class Stomatocyte(ScriptInterfaceHelper):
_so_name = "Shapes::Stomatocyte"


@script_interface_register
class Torus(ScriptInterfaceHelper):

"""
A torus shape.
Attributes
----------
center : array_like :obj:`float`
Coordinates of the center of the torus.
normal : array_like :obj:`float`
Normal axis of the torus.
radius : :obj:`float`
Radius of the torus.
tube_radius : :obj:`float`
Radius of the tube.
direction : :obj:`int`
Surface orientation, for +1 the normal points
out of the mantel, for -1 it points inward.
"""
_so_name = "Shapes::Torus"


@script_interface_register
class Wall(ScriptInterfaceHelper):

Expand Down
31 changes: 31 additions & 0 deletions src/script_interface/shapes/Torus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef SCRIPT_INTERFACE_TORUS_WALL_HPP
#define SCRIPT_INTERFACE_TORUS_WALL_HPP

#include "Shape.hpp"
#include "core/shapes/Torus.hpp"

namespace ScriptInterface {
namespace Shapes {

class Torus : public Shape {
using CoreShape = ::Shapes::Torus;
std::shared_ptr<::Shapes::Torus> m_torus;

public:
Torus() : m_torus(new ::Shapes::Torus()) {
add_parameters(
{{"radius", m_torus, &CoreShape::set_radius, &CoreShape::radius},
{"tube_radius", m_torus, &CoreShape::set_tube_radius,
&CoreShape::tube_radius},
{"normal", m_torus, &CoreShape::set_normal, &CoreShape::normal},
{"center", m_torus, &CoreShape::center},
{"direction", m_torus, &CoreShape::direction}});
}

std::shared_ptr<::Shapes::Shape> shape() const override { return m_torus; }
};

} /* namespace Shapes */
} /* namespace ScriptInterface */

#endif
3 changes: 3 additions & 0 deletions src/script_interface/shapes/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Sphere.hpp"
#include "SpheroCylinder.hpp"
#include "Stomatocyte.hpp"
#include "Torus.hpp"
#include "Wall.hpp"

namespace ScriptInterface {
Expand Down Expand Up @@ -57,6 +58,8 @@ void initialize() {
"Shapes::Stomatocyte");
ScriptInterface::register_new<ScriptInterface::Shapes::SimplePore>(
"Shapes::SimplePore");
ScriptInterface::register_new<ScriptInterface::Shapes::Torus>(
"Shapes::Torus");
}
} /* namespace Shapes */
} /* namespace ScriptInterface */

0 comments on commit 960f332

Please sign in to comment.