Program Listing for File chain3d_to_mesh_error.hpp
↰ Return to documentation for file (/tmp/ws/src/robot_calibration/robot_calibration/include/robot_calibration/cost_functions/chain3d_to_mesh_error.hpp
)
/*
* Copyright (C) 2018-2022 Michael Ferguson
* Copyright (C) 2015 Fetch Robotics Inc.
* Copyright (C) 2013-2014 Unbounded Robotics Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ROBOT_CALIBRATION_COST_FUNCTIONS_CHAIN3D_TO_MESH_ERROR_HPP
#define ROBOT_CALIBRATION_COST_FUNCTIONS_CHAIN3D_TO_MESH_ERROR_HPP
#include <limits>
#include <string>
#include <math.h>
#include <ceres/ceres.h>
#include <robot_calibration/models/camera3d.hpp>
#include <robot_calibration/models/chain3d.hpp>
#include <robot_calibration/optimization/offsets.hpp>
#include <robot_calibration/util/calibration_data.hpp>
#include <robot_calibration/util/mesh_loader.hpp>
#include <robot_calibration_msgs/msg/calibration_data.hpp>
namespace robot_calibration
{
double distToLine(Eigen::Vector3d& a, Eigen::Vector3d& b, Eigen::Vector3d c)
{
Eigen::Vector3d ab = b - a;
Eigen::Vector3d ac = c - a;
Eigen::Vector3d bc = c - b;
double e = ac.dot(ab);
if (e <= 0.0)
{
// Point A is closest to C
return ac.dot(ac);
}
double f = ab.dot(ab);
if (e >= f)
{
// Point B is closest to C
return bc.dot(bc);
}
// C actually projects between
return ac.dot(ac) - e * e / f;
}
struct Chain3dToMesh
{
Chain3dToMesh(Chain3dModel* chain_model,
OptimizationOffsets* offsets,
robot_calibration_msgs::msg::CalibrationData& data,
MeshPtr& mesh)
{
chain_model_ = chain_model;
offsets_ = offsets;
data_ = data;
mesh_ = mesh;
}
virtual ~Chain3dToMesh() {}
bool operator()(double const * const * free_params,
double* residuals) const
{
// Update calibration offsets based on free params
offsets_->update(free_params[0]);
// Project the camera observations
std::vector<geometry_msgs::msg::PointStamped> chain_pts =
chain_model_->project(data_, *offsets_);
// Compute residuals
for (size_t pt = 0; pt < chain_pts.size() ; ++pt)
{
Eigen::Vector3d p(chain_pts[pt].point.x, chain_pts[pt].point.y, chain_pts[pt].point.z);
// Find shortest distance to any line segment forming a triangle
double dist = std::numeric_limits<double>::max();
for (size_t t = 0; t < mesh_->triangle_count; ++t)
{
// Get the index of each vertex of the triangle
int A_idx = mesh_->triangles[(3 * t) + 0];
int B_idx = mesh_->triangles[(3 * t) + 1];
int C_idx = mesh_->triangles[(3 * t) + 2];
// Get the vertices
Eigen::Vector3d A(mesh_->vertices[(3 * A_idx) + 0], mesh_->vertices[(3 * A_idx) + 1], mesh_->vertices[(3 * A_idx) + 2]);
Eigen::Vector3d B(mesh_->vertices[(3 * B_idx) + 0], mesh_->vertices[(3 * B_idx) + 1], mesh_->vertices[(3 * B_idx) + 2]);
Eigen::Vector3d C(mesh_->vertices[(3 * C_idx) + 0], mesh_->vertices[(3 * C_idx) + 1], mesh_->vertices[(3 * C_idx) + 2]);
// Compare each line segment
double d = distToLine(A, B, p);
d = std::min(d, distToLine(B, C, p));
d = std::min(d, distToLine(C, A, p));
dist = std::min(d, dist);
}
residuals[pt] = std::sqrt(dist);
}
return true;
}
static ceres::CostFunction* Create(Chain3dModel* a_model,
OptimizationOffsets* offsets,
robot_calibration_msgs::msg::CalibrationData& data,
MeshPtr mesh)
{
int index = getSensorIndex(data, a_model->getName());
if (index == -1)
{
// In theory, we should never get here, because the optimizer does a check
std::cerr << "Sensor name doesn't match any of the existing finders" << std::endl;
return 0;
}
ceres::DynamicNumericDiffCostFunction<Chain3dToMesh> * func;
func = new ceres::DynamicNumericDiffCostFunction<Chain3dToMesh>(
new Chain3dToMesh(a_model, offsets, data, mesh));
func->AddParameterBlock(offsets->size());
func->SetNumResiduals(data.observations[index].features.size());
return static_cast<ceres::CostFunction*>(func);
}
Chain3dModel * chain_model_;
OptimizationOffsets * offsets_;
robot_calibration_msgs::msg::CalibrationData data_;
MeshPtr mesh_;
};
} // namespace robot_calibration
#endif // ROBOT_CALIBRATION_COST_FUNCTIONS_CHAIN3D_TO_MESH_ERROR_HPP