solving issues with greedy viewpoint generation and adding additional checks

This commit is contained in:
Jensen committed 2025-02-12 12:29:29 -07:00
1 parent aa4b28b9da
commit 39317e615b
4 files changed
+376 -309

No files matched your search

@@ -6,10 +6,6 @@
#include "visualization_msgs/msg/marker.hpp"
#include "visualization_msgs/msg/marker_array.hpp"
#include "geometry_msgs/msg/polygon.hpp"
// #include "tsp_solvers/greedy.hpp"
// #include "tsp_solvers/bnb.hpp"
// #include "tsp_solvers/hill_climb.hpp"
// #include "tsp_solvers/simulation_annealing.hpp"
#include "aerial_inspection_planner/bvh.hpp"
#include "tsp_solvers/ortools.hpp"
@@ -20,6 +20,10 @@ struct Triangle {
std::array<Eigen::Vector3f, 3> vertices;
};
bool ray_intersects_triangle(const Eigen::Vector3f& ray_origin, const Eigen::Vector3f& ray_direction, const Triangle& triangle);
bool is_point_inside_mesh(const Eigen::Vector3f& rayOrigin, const Eigen::Vector3f& rayDirection, const std::vector<Triangle>& mesh);
bool segment_intersects_triangle(const Eigen::Vector3f& P0, const Eigen::Vector3f& P1, const Triangle& triangle);
std::unique_ptr<std::vector<Triangle>> readSTLfile(std::string name, rclcpp::Logger logger_);
Eigen::Vector3f calculate_surface_normal(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1, const Eigen::Vector3f& v2);
@@ -58,10 +62,4 @@ float closest_distance_between_segments(
const Eigen::Vector3f& a0, const Eigen::Vector3f& a1,
const Eigen::Vector3f& b0, const Eigen::Vector3f& b1);
float closest_distance_between_segments(
const Eigen::Vector3f& a0, const Eigen::Vector3f& a1,
const Eigen::Vector3f& b0, const Eigen::Vector3f& b1,
bool clampAll, bool clampA0, bool clampA1,
bool clampB0, bool clampB1);
}
+230 -169
View File
@@ -105,10 +105,19 @@ visualization_msgs::msg::Marker InspectionPlanner::create_mesh_visualization_msg
if (color_as_redundancy)
{
int vp_count = mesh_to_vp_map[j].size();
double view_mixer = std::min(vp_count, (int) max_view_color)/max_view_color;
triangle_color.r = view_mixer;
triangle_color.g = 0;
triangle_color.b = 1 - view_mixer;
if (vp_count == 0)
{
triangle_color.r = 0;
triangle_color.g = 1;
triangle_color.b = 0;
}
else
{
double view_mixer = std::min(vp_count, (int) max_view_color)/max_view_color;
triangle_color.r = view_mixer;
triangle_color.g = 0;
triangle_color.b = 1 - view_mixer;
}
triangle_color2 = triangle_color;
triangle_color3 = triangle_color;
}
@@ -273,52 +282,73 @@ visualization_msgs::msg::MarkerArray InspectionPlanner::create_vps_visualization
void InspectionPlanner::determine_visibility(bool save_mesh_to_vp_map = true)
{
RCLCPP_INFO(this->get_logger(), "Calculating visibility for vps");
auto start_time = this->now();
// Resize the vectors before filling for faster execution
RCLCPP_INFO(this->get_logger(), "Calculating visibility for vps");
auto start_time = this->now();
// Resize the vectors before filling for faster execution
mesh_to_vp_map.resize(mesh->size());
vp_to_mesh_map.resize(vp_positions.size());
vp_to_mesh_map.resize(vp_positions.size());
// Determine visibility
for (size_t i = 0; i < vp_positions.size(); ++i) // Viewpoints
{
{
const auto& vp_pose = vp_positions[i];
const auto& vp_orient = vp_orientation_vec[i];
// TODO Or already computed below? Just need to evaluate? Just look at all visible meshes and make sure they don't occlude each other?
// actually still more complex. Need to look at all triangles, not just those passing the test
const auto& vp_orient = vp_orientation_vec[i];
// bool no_meshes_visible = true;
// TODO Or already computed below? Just need to evaluate? Just look at all visible meshes and make sure they don't occlude each other?
// actually still more complex. Need to look at all triangles, not just those passing the test
// TODO triangles can be viewed even if the ray from vp to mesh passes through other meshes
for (size_t j = 0; j < mesh->size(); ++j) // Mesh triangles
{
{
const mesh_utils::Triangle& triangle = (*mesh)[j];
bool all_corners_visible = true;
for (size_t k = 0; k < triangle.vertices.size(); ++k) // Check all three corners
{
Eigen::Vector3f ray_direction(triangle.vertices[k] - vp_pose);
ray_direction.normalize();
if (!mesh_utils::ray_views_point(vp_pose, vp_orient, ray_direction, mesh_surface_normals[j],
triangle.vertices[k], max_incidence_angle_, max_view_distance_,
camera_half_fov_))
{
all_corners_visible = false;
break;
}
}
bool all_corners_visible = true;
for (size_t k = 0; k < triangle.vertices.size(); ++k) // Check all three corners
{
Eigen::Vector3f ray_direction(triangle.vertices[k] - vp_pose);
ray_direction.normalize();
if (!mesh_utils::ray_views_point(vp_pose, vp_orient, ray_direction, mesh_surface_normals[j],
triangle.vertices[k], max_incidence_angle_, max_view_distance_,
camera_half_fov_))
{
all_corners_visible = false;
break;
}
}
if (all_corners_visible) // Check if the segment from the viewpoint to the vertex intersects any other triangle
{
for (size_t k = 0; k < triangle.vertices.size(); ++k) // Check all three corners
{
for (size_t l = 0; l < mesh->size(); ++l) {
if (l == j) continue; // Skip the current triangle
// const mesh_utils::Triangle& other_triangle = (*mesh)[l]
if (mesh_utils::segment_intersects_triangle(vp_pose, triangle.vertices[k], (*mesh)[l])) {
all_corners_visible = false;
goto exitloops;
}
}
}
}
exitloops:
if (all_corners_visible)
{
if (save_mesh_to_vp_map)
{
mesh_to_vp_map[j].push_back(i);
}
vp_to_mesh_map[i].push_back(j);
}
}
{
// no_meshes_visible = false;
if (save_mesh_to_vp_map)
{
mesh_to_vp_map[j].push_back(i);
}
vp_to_mesh_map[i].push_back(j);
}
}
}
RCLCPP_INFO_STREAM(this->get_logger(), "Time to calc visibility: " << this->now().seconds() - start_time.seconds());
RCLCPP_INFO_STREAM(this->get_logger(), "Time to calc visibility: " << this->now().seconds() - start_time.seconds());
}
bool InspectionPlanner::create_vps_for_all_triangles()
{
RCLCPP_INFO(this->get_logger(), "**Creating all vps**");
int in_collision_count = 0;
int inside_mesh_count = 0;
int occlusions_count = 0;
for (std::vector<mesh_utils::Triangle>::iterator it = mesh->begin(); it != mesh->end(); it++)
{
int polygon_size = it->vertices.size();
@@ -337,28 +367,69 @@ bool InspectionPlanner::create_vps_for_all_triangles()
Eigen::Vector3f surface_normal = mesh_utils::calculate_surface_normal(v0, v1, v2);
mesh_surface_normals.push_back(surface_normal);
Eigen::Vector3f centroid = mesh_utils::calculate_centroid(v0, v1, v2);
Eigen::Vector3f new_vp;
for (int i = 0; i<2; i++)
{
surface_normal = (1-2*i)*surface_normal;
// Find viewpoint out from mesh, set the viewing angle as opposite of the normal.
new_vp = centroid + surface_normal*distance_from_surface_;
// Find viewpoint out from mesh, set the viewing angle as opposite of the normal.
Eigen::Vector3f new_vp = centroid + surface_normal*distance_from_surface_;
// Check for collisions
// TODO adjust vp if in collision.
if (is_collision(new_vp))
{
in_collision_count++;
continue;
}
// Check for collisions
// TODO adjust vp if in collision.
if (is_collision(new_vp))
{
in_collision_count++;
continue;
}
///////////////// check for occlusions
// TODO adjust vp if occluded
bool all_corners_visible = true;
for (size_t k = 0; k < it->vertices.size(); ++k) // Check all three corners
{
for (size_t l = 0; l < mesh->size(); ++l) {
if (l == static_cast<size_t>(std::distance(mesh->begin(), it))) continue; // Skip the current triangle
// const mesh_utils::Triangle& other_triangle = (*mesh)[l]
if (mesh_utils::segment_intersects_triangle(new_vp, it->vertices[k], (*mesh)[l])) {
all_corners_visible = false;
goto exitloops;
}
}
}
exitloops:
if (!all_corners_visible)
{
occlusions_count++;
continue;
}
// TODO check for visibility of the mesh it cares about
////////////////////// check for inside mesh
if (mesh_utils::is_point_inside_mesh(new_vp, surface_normal, *mesh))
{
inside_mesh_count++;
continue;
}
vp_positions.push_back(new_vp);
vp_orientation_vec.push_back(-surface_normal); // pointing to surface normal, so negative
}
vp_positions.push_back(new_vp);
vp_orientation_vec.push_back(-surface_normal); // pointing to surface normal, so negative
}
vp_keep.resize(vp_positions.size(), true);
if (in_collision_count > 0)
{
RCLCPP_WARN_STREAM(this->get_logger(), "Didn't create "
<< in_collision_count << " vps because of collisions\n");
<< in_collision_count << " vps because of collisions");
}
if (occlusions_count > 0)
{
RCLCPP_WARN_STREAM(this->get_logger(), "Didn't create "
<< occlusions_count << " vps because of occlusions");
}
if (inside_mesh_count > 0)
{
RCLCPP_WARN_STREAM(this->get_logger(), "Didn't create "
<< inside_mesh_count << " vps because inside mesh");
}
return true;
}
@@ -389,6 +460,18 @@ bool InspectionPlanner::mesh_to_iterative_vps()
// Calculate visibility and view redundancy for each surface.
determine_visibility();
// check how many meshes can't be seen
int mesh_not_seen_count = 0;
for (size_t j = 0; j < mesh->size(); ++j)
{
int vp_count = mesh_to_vp_map[j].size();
if (vp_count == 0)
{
mesh_not_seen_count++;
}
}
RCLCPP_WARN(this->get_logger(), "%d Meshes not seen by view points before reduction", mesh_not_seen_count);
// Visualize with redundancy as color.
if (this->visualize_mesh_)
{
@@ -454,6 +537,18 @@ bool InspectionPlanner::mesh_to_iterative_vps()
<< " -> "
<< vp_positions.size() - count_removed);
// check how many meshes can't be seen
mesh_not_seen_count = 0;
for (size_t j = 0; j < mesh->size(); ++j)
{
int vp_count = mesh_to_vp_map[j].size();
if (vp_count == 0)
{
mesh_not_seen_count++;
}
}
RCLCPP_WARN(this->get_logger(), "%d Meshes not seen by view points after reduction", mesh_not_seen_count);
// Visualize the updates after trimming
if (this->visualize_mesh_)
{
@@ -493,7 +588,21 @@ bool InspectionPlanner::mesh_to_greedy_vps()
}
// Calculate visibility and view redundancy for each surface.
determine_visibility(false);
determine_visibility(true);
// check how many meshes can't be seen
int mesh_not_seen_count = 0;
for (size_t j = 0; j < mesh->size(); ++j)
{
int vp_count = mesh_to_vp_map[j].size();
if (vp_count == 0)
{
mesh_not_seen_count++;
}
}
mesh_to_vp_map.clear();
mesh_to_vp_map.resize(mesh->size());
RCLCPP_WARN(this->get_logger(), "%d Meshes not seen by view points before reduction", mesh_not_seen_count);
// Visualize with redundancy as color.
if (this->visualize_mesh_)
@@ -540,71 +649,67 @@ bool InspectionPlanner::mesh_to_greedy_vps()
RCLCPP_INFO_STREAM(this->get_logger(), "High vp index and value: " << vp_to_value_pair.front().first << ", " << vp_to_value_pair.front().second);
RCLCPP_INFO_STREAM(this->get_logger(), "Low vp index and value: " << vp_to_value_pair.back().first << ", " << vp_to_value_pair.back().second);
// Make copies of the maps
auto vp_to_mesh_map_copy = vp_to_mesh_map;
auto mesh_to_vp_map_copy = mesh_to_vp_map;
// Initialize true maps to keep track of selected viewpoints and meshes
std::vector<std::vector<int>> true_vp_to_mesh_map(vp_to_mesh_map.size());
std::vector<std::vector<int>> true_mesh_to_vp_map(mesh_to_vp_map.size());
while (vp_to_value_pair.size() > 0 && vp_to_value_pair.front().second != 0)
{
bool top_val_updated_and_sorted = false;
while (!top_val_updated_and_sorted)
{
// Update top entry
int new_count_val = 0;
int current_vp_index = vp_to_value_pair.front().first;
for (size_t i = 0; i < vp_to_mesh_map[current_vp_index].size(); i++)
{
if (mesh_to_vp_map[vp_to_mesh_map[current_vp_index].at(i)].size() < min_view_redundancy_)
{
new_count_val++;
}
}
vp_to_value_pair.front().second = new_count_val;
// Main loop to greedily select viewpoints
while (!vp_to_value_pair.empty()) {
// Find the largest vp_to_value_pair
auto max_it = std::max_element(vp_to_value_pair.begin(), vp_to_value_pair.end(),
[](const std::pair<int, int>& a, const std::pair<int, int>& b) {
return a.second < b.second;
});
// The case where only one value left
if (vp_to_value_pair.size() == 1)
{
top_val_updated_and_sorted = true;
break;
}
// Check if the maximum element's second value is zero
if (max_it->second == 0) {
break;
}
// Resort top entry in the list
bool sorted = false;
for (size_t i = 1; i < vp_to_value_pair.size(); i++)
{
if (vp_to_value_pair[i].second <= vp_to_value_pair.front().second)
{
if (i == 1)
{
top_val_updated_and_sorted = true;
sorted = true;
break;
}
// Move to i-1 location
auto new_pos = vp_to_value_pair.begin() + i - 1;
std::rotate(vp_to_value_pair.begin(), vp_to_value_pair.begin() + 1, new_pos);
sorted = true;
}
}
// Commit it to vp_keep
int top_vp_index = max_it->first;
vp_keep[top_vp_index] = true;
// The case where it is the smallest value
if (!sorted)
{
std::rotate(vp_to_value_pair.begin(), vp_to_value_pair.begin() + 1, vp_to_value_pair.end());
}
}
// RCLCPP_INFO_STREAM_ONCE(this->get_logger(), "High vp index and value after update and resort: " << vp_to_value_pair.front().first << ", " << vp_to_value_pair.front().second);
// Add the selected viewpoint and its meshes to the true maps
true_vp_to_mesh_map[top_vp_index] = vp_to_mesh_map[top_vp_index];
for (const auto& mesh : vp_to_mesh_map[top_vp_index]) {
true_mesh_to_vp_map[mesh].push_back(top_vp_index);
}
// Add top entry to list of kept vps, update the mesh to vp map, and remove from pair list
int top_vp_index = vp_to_value_pair.front().first;
vp_keep[top_vp_index] = true;
for (size_t i = 0; i < vp_to_mesh_map[top_vp_index].size(); i++)
{
mesh_to_vp_map[vp_to_mesh_map[top_vp_index].at(i)].push_back(top_vp_index);
}
vp_to_value_pair.erase(vp_to_value_pair.begin());
count_added++;
}
// Remove it from vp_to_value_pair
vp_to_value_pair.erase(max_it);
// Update vp_to_mesh_map_copy and remove meshes seen by that vp if they meet min_view_redundancy_
std::vector<int> meshes_to_remove = vp_to_mesh_map_copy[top_vp_index];
for (int mesh : meshes_to_remove) {
if (true_mesh_to_vp_map[mesh].size() >= min_view_redundancy_) {
for (auto& vp_meshes : vp_to_mesh_map_copy) {
vp_meshes.erase(std::remove(vp_meshes.begin(), vp_meshes.end(), mesh), vp_meshes.end());
}
}
}
// Reset vp_to_value_pair
vp_to_value_pair.clear();
for (size_t i = 0; i < vp_to_mesh_map_copy.size(); i++) {
if (!vp_keep[i]) {
vp_to_value_pair.push_back(std::make_pair(i, vp_to_mesh_map_copy[i].size()));
}
}
count_added++;
}
// Replace the original maps with the true maps
vp_to_mesh_map = std::move(true_vp_to_mesh_map);
mesh_to_vp_map = std::move(true_mesh_to_vp_map);
RCLCPP_INFO(this->get_logger(), "Finished vp selection");
RCLCPP_INFO_STREAM(this->get_logger(), "Number VPs that can't see anything (and kept): " << count_vp_sees_nothing);
RCLCPP_INFO_STREAM(this->get_logger(), "Number of VPs before and after: "
@@ -623,6 +728,18 @@ bool InspectionPlanner::mesh_to_greedy_vps()
vps_after_trim_->publish(marker_array);
}
RCLCPP_INFO_STREAM(this->get_logger(), "Total time to find vps: " << this->now().seconds() - start_time.seconds());
// check how many meshes can't be seen
mesh_not_seen_count = 0;
for (size_t j = 0; j < mesh->size(); ++j)
{
int vp_count = mesh_to_vp_map[j].size();
if (vp_count == 0)
{
mesh_not_seen_count++;
}
}
RCLCPP_WARN(this->get_logger(), "%d Meshes not seen by view points after reduction", mesh_not_seen_count);
return true;
}
@@ -764,55 +881,7 @@ bool InspectionPlanner::solve_tsp()
// Solve TSP
RCLCPP_INFO_STREAM(this->get_logger(), "TSP solver method: " << tsp_method_);
std::vector<int> tsp_index_order;
if (tsp_method_ == "greedy")
{
// tsp_index_order = greedy_tsp_algo(distance, this->get_logger());
RCLCPP_ERROR_STREAM(this->get_logger(), tsp_method_ << " has been removed for public release. Please try ortools for the TSP solver");
return false;
}
else if (tsp_method_ == "bnb")
{
// int source = 0;
// std::unique_ptr<TspNode> solution = BranchAndBound(
// distance, source, tsp_solver_allowed_time_, this->get_logger());
// if (solution->time < tsp_solver_allowed_time_)
// {
// RCLCPP_INFO_STREAM(this->get_logger(), "Optimal solution found:");
// RCLCPP_INFO_STREAM(this->get_logger(), "Total cost:" << solution->value);
// RCLCPP_DEBUG_STREAM(this->get_logger(), "Optimal cycle:");
// for(std::vector<int>::const_iterator it=solution->path->begin();it!=solution->path->end();it++)
// {
// tsp_index_order.push_back(*it);
// }
// // tsp_index_order.push_back(source);
// RCLCPP_INFO_STREAM(this->get_logger(), "Elapsed time:"<< solution->time << "s");
// }
// else
// {
// RCLCPP_INFO_STREAM(this->get_logger(), "Optimal solution NOT found in specified time:");
// RCLCPP_INFO_STREAM(this->get_logger(), "Total cost: " << solution->value);
// RCLCPP_DEBUG_STREAM(this->get_logger(), "Solution cycle:");
// for (std::vector<int>::const_iterator it = solution->path->begin(); it != solution->path->end(); it++)
// {
// tsp_index_order.push_back(*it);
// }
// // tsp_index_order.push_back(source);
// RCLCPP_INFO_STREAM(this->get_logger(), "Elapsed time:"<< solution->time << "s");
// }
// delete solution->path;
RCLCPP_ERROR_STREAM(this->get_logger(), tsp_method_ << " has been removed for public release. Please try ortools for the TSP solver");
return false;
}
else if (tsp_method_ == "hc")
{
// unsigned seed = 1;
// tsp_index_order = greedy_tsp_algo(distance, this->get_logger());
// tsp_index_order = HC(distance, tsp_index_order, tsp_solver_allowed_time_, seed, this->get_logger());
RCLCPP_ERROR_STREAM(this->get_logger(), tsp_method_ << " has been removed for public release. Please try ortools for the TSP solver");
return false;
}
else if (tsp_method_ == "ortools")
if (tsp_method_ == "ortools")
{
std::vector<std::vector<int>> distance_int;
double double_to_int_scale = 1000;
@@ -828,19 +897,6 @@ bool InspectionPlanner::solve_tsp()
}
tsp_index_order = operations_research::Tsp(distance_int, tsp_solver_allowed_time_, this->get_logger());
}
else if (tsp_method_ == "sa")
{
// unsigned seed = 1;
// tsp_index_order = greedy_tsp_algo(distance, this->get_logger());
// // double T = distance[initial[9]][initial[10]]*2*distance.size();
// // double T = .8;
// double T = 100;
// double T_min = 0.1 * distance.size();
// double r = 0.99;
// tsp_index_order = SA(distance, tsp_index_order, T, T_min, r, tsp_solver_allowed_time_, seed, this->get_logger());
RCLCPP_ERROR_STREAM(this->get_logger(), tsp_method_ << " has been removed for public release. Please try ortools for the TSP solver");
return false;
}
else
{
RCLCPP_ERROR(this->get_logger(), "VP sampling failed");
@@ -957,6 +1013,7 @@ void InspectionPlanner::plan()
return;
}
// Process VPs to graph or directly optimize
// Find path between vps
RCLCPP_INFO(this->get_logger(), "**Solving for flight path**");
@@ -976,6 +1033,10 @@ void InspectionPlanner::plan()
prev_vp_pos = cur_vp_pos;
}
RCLCPP_INFO_STREAM(this->get_logger(), "Final path length: " << path_length);
if (path_length > 999999)
{
RCLCPP_WARN(this->get_logger(), "!Path has collision with object! A possible solution is to increase time to solve.");
}
RCLCPP_WARN(this->get_logger(), "Finished making plan!");
RCLCPP_INFO_STREAM(this->get_logger(), "Total Time: " << this->now().seconds() - start_time.seconds());
+142 -130
View File
@@ -3,6 +3,108 @@
namespace mesh_utils
{
// from ChatGPT
bool ray_intersects_triangle(const Eigen::Vector3f& ray_origin, const Eigen::Vector3f& ray_direction, const Triangle& triangle) {
//Möller–Trumbore intersection algorithm
const float EPSILON = 1e-8f;
Eigen::Vector3f V0 = triangle.vertices[0];
Eigen::Vector3f V1 = triangle.vertices[1];
Eigen::Vector3f V2 = triangle.vertices[2];
Eigen::Vector3f E1 = V1 - V0;
Eigen::Vector3f E2 = V2 - V0;
Eigen::Vector3f P = ray_direction.cross(E2);
float det = E1.dot(P);
if (fabs(det) < EPSILON) {
return false; // Ray is parallel to the triangle plane
}
float invDet = 1.0f / det;
Eigen::Vector3f T = ray_origin - V0;
float u = T.dot(P) * invDet;
if ((u < 0 && abs(u) > EPSILON) || (u > 1 && abs(u-1) > EPSILON))
{
return false;
}
Eigen::Vector3f Q = T.cross(E1);
float v = ray_direction.dot(Q) * invDet;
if ((v < 0 && abs(v) > EPSILON) || (u + v > 1 && abs(u + v - 1) > EPSILON))
{
return false;
}
float t = E2.dot(Q) * invDet;
return t > EPSILON; // Intersection is valid if t > 0
}
// from ChatGPT
bool is_point_inside_mesh(const Eigen::Vector3f& rayOrigin, const Eigen::Vector3f& rayDirection, const std::vector<Triangle>& mesh) {
int intersectionCount = 0;
for (const auto& triangle : mesh) {
if (ray_intersects_triangle(rayOrigin, rayDirection, triangle)) {
intersectionCount++;
}
}
return intersectionCount % 2 == 1;
}
bool segment_intersects_triangle(const Eigen::Vector3f& P0, const Eigen::Vector3f& P1, const Triangle& triangle) {
const float EPSILON = 1e-8f;
Eigen::Vector3f V0 = triangle.vertices[0];
Eigen::Vector3f V1 = triangle.vertices[1];
Eigen::Vector3f V2 = triangle.vertices[2];
Eigen::Vector3f E1 = V1 - V0;
Eigen::Vector3f E2 = V2 - V0;
Eigen::Vector3f D = P1 - P0;
Eigen::Vector3f P = D.cross(E2);
float det = E1.dot(P);
if (fabs(det) < EPSILON) {
return false; // Segment is parallel to the triangle plane
}
float invDet = 1.0f / det;
Eigen::Vector3f T = P0 - V0;
float u = T.dot(P) * invDet;
if ((u < 0 && abs(u) > EPSILON) || (u > 1 && abs(u-1) > EPSILON))
{
return false;
}
Eigen::Vector3f Q = T.cross(E1);
float v = D.dot(Q) * invDet;
if ((v < 0 && abs(v) > EPSILON) || (u + v > 1 && abs(u + v - 1) > EPSILON))
{
return false;
}
float t = E2.dot(Q) * invDet;
if (t < 0 || t > 0.9999) {
return false; // Intersection point is not within the segment
}
// Calculate the intersection point
Eigen::Vector3f intersectionPoint = P0 + t * D;
// Check if the intersection point is at any of the triangle's vertices
if ((intersectionPoint - V0).norm() < EPSILON ||
(intersectionPoint - V1).norm() < EPSILON ||
(intersectionPoint - V2).norm() < EPSILON) {
return false; // Intersection point is at a vertex
}
// RCLCPP_INFO(logger_, "segment ray is blocked with: %f length %f u %f v %f", t, u, v);
return true;
}
// From ATL inspection stack with modifications
std::unique_ptr<std::vector<Triangle>> readSTLfile(std::string name, rclcpp::Logger logger_)
{
@@ -216,13 +318,20 @@ bool ray_views_point(const Eigen::Vector3f& vp_pose,
}
// point within incendent angle from surface normal
if (angle_between_vectors(surface_normal, -ray_direction) > max_incidence_angle)
{
return false;
Eigen::Vector3f neg_ray_direction = ray_direction*-1;
if (angle_between_vectors(surface_normal, neg_ray_direction) > max_incidence_angle)
{
// point within incendent angle from surface normal
Eigen::Vector3f neg_surface_normal = surface_normal*-1;
if (angle_between_vectors(neg_surface_normal, neg_ray_direction) > max_incidence_angle)
{
return false;
}
}
// Checks Max distance
if ((vp_pose-point).norm() > max_view_distance)
Eigen::Vector3f dist = vp_pose-point;
if ((dist).norm() > max_view_distance)
{
return false;
}
@@ -285,147 +394,50 @@ float point_to_triangle_distance(const Eigen::Vector3f& point, const mesh_utils:
return (closest_point - point).norm();
}
// Overloaded function with default arguments
// taken from ChatGPT
float closest_distance_between_segments(
const Eigen::Vector3f& a0, const Eigen::Vector3f& a1,
const Eigen::Vector3f& b0, const Eigen::Vector3f& b1) {
return closest_distance_between_segments(a0, a1, b0, b1, true, false, false, false, false);
}
// From https://stackoverflow.com/questions/2824478/shortest-distance-between-two-line-segments
float closest_distance_between_segments(
const Eigen::Vector3f& a0, const Eigen::Vector3f& a1,
const Eigen::Vector3f& b0, const Eigen::Vector3f& b1,
bool clampAll = true, bool clampA0 = false, bool clampA1 = false,
bool clampB0 = false, bool clampB1 = false)
const Eigen::Vector3f& b0, const Eigen::Vector3f& b1)
{
// If clampAll is true, set all clamps to true
if (clampAll)
{
clampA0 = true;
clampA1 = true;
clampB0 = true;
clampB1 = true;
}
// Calculate vectors and their magnitudes
Eigen::Vector3f A = a1 - a0;
Eigen::Vector3f B = b1 - b0;
float magA = A.norm();
float magB = B.norm();
Eigen::Vector3f r = a0 - b0;
float a = A.dot(A); // Squared length of segment A
float e = B.dot(B); // Squared length of segment B
float f = B.dot(r);
Eigen::Vector3f _A = A / magA;
Eigen::Vector3f _B = B / magB;
float s, t;
float c = A.dot(r);
float b = A.dot(B);
float denom = a * e - b * b;
Eigen::Vector3f cross = _A.cross(_B);
float denom = cross.squaredNorm();
// If segments are not parallel, compute the closest points
if (denom != 0.0f) {
s = (b * f - c * e) / denom;
t = (a * f - b * c) / denom;
// Define a small epsilon for floating-point comparison
const float epsilon = 1e-6;
// If lines are parallel (denom is close to 0), test if lines overlap
if (denom < epsilon)
{
float d0 = _A.dot(b0 - a0);
// Overlap only possible with clamping
if (clampA0 || clampA1 || clampB0 || clampB1)
{
float d1 = _A.dot(b1 - a0);
// Is segment B before A?
if (d0 <= 0 && d1 <= 0)
{
if (clampA0 && clampB1)
{
if (std::abs(d0) < std::abs(d1))
{
return (a0 - b0).norm();
}
return (a0 - b1).norm();
}
}
// Is segment B after A?
else if (d0 >= magA && d1 >= magA)
{
if (clampA1 && clampB0)
{
if (std::abs(d0) < std::abs(d1))
{
return (a1 - b0).norm();
}
return (a1 - b1).norm();
}
}
}
// Segments overlap, return distance between parallel segments
return ((d0 * _A) + a0 - b0).norm();
// Clamp s to [0, 1]
s = std::max(0.0f, std::min(1.0f, s));
} else {
// If segments are parallel, choose arbitrary s
s = 0.0f;
}
// Lines criss-cross: Calculate the projected closest points
Eigen::Vector3f t = b0 - a0;
float detA = t.dot(_B.cross(cross));
float detB = t.dot(_A.cross(cross));
// Compute the point on A closest to B
Eigen::Vector3f c1 = a0 + s * A;
float t0 = detA / denom;
float t1 = detB / denom;
// Compute the point on B closest to A
t = (b * s + f) / e;
Eigen::Vector3f pA = a0 + (_A * t0); // Projected closest point on segment A
Eigen::Vector3f pB = b0 + (_B * t1); // Projected closest point on segment B
// Clamp t to [0, 1]
t = std::max(0.0f, std::min(1.0f, t));
// Clamp projections
if (clampA0 || clampA1 || clampB0 || clampB1)
{
if (clampA0 && t0 < 0)
{
pA = a0;
}
else if (clampA1 && t0 > magA)
{
pA = a1;
}
// Compute the point on B closest to A
Eigen::Vector3f c2 = b0 + t * B;
if (clampB0 && t1 < 0)
{
pB = b0;
}
else if (clampB1 && t1 > magB)
{
pB = b1;
}
// Clamp projection A
if ((clampA0 && t0 < 0) || (clampA1 && t0 > magA))
{
float dot = _B.dot(pA - b0);
if (clampB0 && dot < 0)
{
dot = 0;
}
else if (clampB1 && dot > magB)
{
dot = magB;
}
pB = b0 + (_B * dot);
}
// Clamp projection B
if ((clampB0 && t1 < 0) || (clampB1 && t1 > magB))
{
float dot = _A.dot(pB - a0);
if (clampA0 && dot < 0)
{
dot = 0;
}
else if (clampA1 && dot > magA)
{
dot = magA;
}
pA = a0 + (_A * dot);
}
}
return (pA - pB).norm();
// Return the distance between the closest points
return (c1 - c2).norm();
}
}