Skip to content

Commit

Permalink
shapes: quarterpipe: applied suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
christophlohrmann committed Nov 9, 2020
1 parent 2d0d337 commit 4ba164e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 52 deletions.
3 changes: 1 addition & 2 deletions doc/sphinx/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ The three-dimensional shape is an extrusion of this profile.
:height: 4.00000cm

``center`` defines the center of the cylinder.
``axis`` is the axis of the cylinder,
orthogonal to the curve of the pipe.
``axis`` is the axis of the cylinder, orthogonal to the curve of the pipe.
``orientation`` points from ``center`` along the symmetry axis of the quarterpipe.
``radius`` is the radius of the pipe.
``height`` is the extent of the shape along ``axis`` (symmetric in both directions
Expand Down
96 changes: 46 additions & 50 deletions src/shapes/src/Quarterpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,71 +65,67 @@ void Quarterpipe::calculate_dist(const Utils::Vector3d &pos, double &dist,
// Zone 2
vec = pos_projected - to_point_2;
dist = vec.norm();
} else if (pos_norm < m_radius) {
// Zone 3
dist = m_radius - pos_norm;
vec = -dist * pos_projected / pos_norm;
} else {
if (pos_norm < m_radius) {
// Zone 3
dist = m_radius - pos_norm;
vec = -dist * pos_projected / pos_norm;
// Zones 4 to 9 are hard to distingish, so all distances are calculated
// and the minimum chosen

// Zone 4, 5, 7 or 9
auto dist_plane_1 =
(pos_projected * to_point_1 / to_point_1.norm()) - to_point_1.norm();
auto vec_plane_1 = dist_plane_1 * to_point_1 / to_point_1.norm();

// Zone 4, 6, 8 or 9
auto dist_plane_2 =
(pos_projected * to_point_2 / to_point_2.norm()) - to_point_2.norm();
auto vec_plane_2 = dist_plane_2 * to_point_2 / to_point_2.norm();

// Distinguish the Zones
if (dist_plane_1 >= 0. and dist_plane_2 >= 0.) {
// Zone 9
auto corner_point = std::sqrt(2.) * m_radius * m_orientation;
vec = pos_projected - corner_point;
dist = vec.norm();
} else if (dist_plane_1 < 0. and dist_plane_2 < 0.) {
// Zone 4, 5 or 6

// First, find which side is closer
// Distinguishes zones 4 and 5 from 4 and 6
bool in_zone_4_or_5 = dist_plane_1 > dist_plane_2;
auto dist_plane = in_zone_4_or_5 ? dist_plane_1 : dist_plane_2;
auto vec_plane = in_zone_4_or_5 ? vec_plane_1 : vec_plane_2;

// Now find if in 4 or 5/6
auto dist_cyl = m_radius - pos_norm;
auto vec_cyl = -dist_cyl * pos_projected / pos_norm;

// both are negative
bool in_zone_4 = dist_cyl > dist_plane;
dist = in_zone_4 ? dist_cyl : dist_plane;
vec = in_zone_4 ? vec_cyl : vec_plane;
} else {
// Zones 4 to 9 are hard to distingish, so all distances are calculated
// and the minimum chosen

// Zone 4, 5, 7 or 9
auto dist_plane_1 = (pos_projected * to_point_1 / to_point_1.norm()) -
to_point_1.norm();
auto vec_plane_1 = dist_plane_1 * to_point_1 / to_point_1.norm();

// Zone 4, 6, 8 or 9
auto dist_plane_2 = (pos_projected * to_point_2 / to_point_2.norm()) -
to_point_2.norm();
auto vec_plane_2 = dist_plane_2 * to_point_2 / to_point_2.norm();

// Distinguish the Zones
if (dist_plane_1 >= 0. and dist_plane_2 >= 0.) {
// Zone 9
auto corner_point = std::sqrt(2.) * m_radius * m_orientation;
vec = pos_projected - corner_point;
dist = vec.norm();
} else if (dist_plane_1 < 0. and dist_plane_2 < 0.) {
// Zone 4, 5 or 6

// First, find which side is closer
// Distinguishes zones 4 and 5 from 4 and 6
bool in_zone_4_or_5 =
Utils::abs(dist_plane_1) < Utils::abs(dist_plane_2);
auto dist_plane = in_zone_4_or_5 ? dist_plane_1 : dist_plane_2;
auto vec_plane = in_zone_4_or_5 ? vec_plane_1 : vec_plane_2;

// Now find if in 4 or 5/6
auto dist_cyl = m_radius - pos_norm;
auto vec_cyl = -dist_cyl * pos_projected / pos_norm;

bool in_zone_4 = Utils::abs(dist_cyl) < Utils::abs(dist_plane);
dist = in_zone_4 ? dist_cyl : dist_plane;
vec = in_zone_4 ? vec_cyl : vec_plane;
} else {
// Zone 7 or 8
bool in_zone_7 = dist_plane_1 >= 0;
dist = in_zone_7 ? dist_plane_1 : dist_plane_2;
vec = in_zone_7 ? vec_plane_1 : vec_plane_2;
}
// Zone 7 or 8
bool in_zone_7 = dist_plane_1 >= 0;
dist = in_zone_7 ? dist_plane_1 : dist_plane_2;
vec = in_zone_7 ? vec_plane_1 : vec_plane_2;
}
}
} // 2D end, now to the 3rd dimension

auto upper_lower = rel_z > 0 ? 1 : -1;
// signed distance to the closer top/bottom wall
auto dist_cover = Utils::abs(rel_z) - 0.5 * m_height;
auto dist_cover = upper_lower * rel_z - 0.5 * m_height;

// If the point is in zone A or B, nothing has to change

if (dist_cover < 0. and dist < dist_cover) {
// Zone C (abs(dist_cover) < abs(dist))
dist = dist_cover;
vec = upper_lower * dist * m_axis;
}

if (Utils::abs(rel_z) > 0.5 * m_height) {
} else if (dist_cover > 0) {
// Zone D or E
auto z_to_shape = rel_z - upper_lower * 0.5 * m_height;
if (dist < 0) {
Expand Down

0 comments on commit 4ba164e

Please sign in to comment.