-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2282 from itischler/torus
Added a torus shape
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ¢er() { return m_center; } | ||
double &direction() { return m_direction; } | ||
|
||
int calculate_dist(const double *ppos, double *dist, | ||
double *vec) const override; | ||
}; | ||
} // namespace Shapes | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters