distance_to_line_2d.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021, University of Oxford
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #include <exotica_core/server.h>
32 
34 
35 namespace exotica
36 {
38 {
39  scene_ = scene;
40  Initialize();
41 }
42 
44 {
45  if (frames_.size() != 3)
46  {
47  ThrowPretty("This task map requires three frames to initialise: The first two define the line, the third is the query frame.");
48  }
49 
50  if (Server::IsRos())
51  {
52  pub_debug_ = Server::Advertise<visualization_msgs::MarkerArray>(object_name_ + "/debug", 1, true);
53 
54  debug_marker_array_msg_.markers.resize(4);
55 
56  // First = P1
57  // Second = P2
58  // Third = P3
59  for (std::size_t i = 0; i < 4; ++i)
60  {
61  debug_marker_array_msg_.markers[i].type = visualization_msgs::Marker::SPHERE;
62  debug_marker_array_msg_.markers[i].action = visualization_msgs::Marker::ADD;
63  debug_marker_array_msg_.markers[i].id = i;
64  debug_marker_array_msg_.markers[i].color.a = .7f;
65  debug_marker_array_msg_.markers[i].color.r = 0.5f;
66  debug_marker_array_msg_.markers[i].color.g = 0.f;
67  debug_marker_array_msg_.markers[i].color.b = 0.f;
68  debug_marker_array_msg_.markers[i].scale.x = debug_marker_array_msg_.markers[i].scale.y = debug_marker_array_msg_.markers[i].scale.z = .02f;
69  debug_marker_array_msg_.markers[i].pose.orientation.w = 1.0;
70 
71  debug_marker_array_msg_.markers[i].header.frame_id = "exotica/" + scene_->GetRootFrameName();
72  }
73  // Query point in blue:
74  debug_marker_array_msg_.markers[2].color.r = 0.f;
75  debug_marker_array_msg_.markers[2].color.b = 0.5f;
76 
77  // Fourth = Line
78  debug_marker_array_msg_.markers[3].type = visualization_msgs::Marker::LINE_LIST;
79  }
80 }
81 
83 {
84  if (phi.rows() != 1) ThrowNamed("Wrong size of Phi!");
85 
86  const Eigen::Vector2d P1(kinematics[0].Phi(0).p.data[0], kinematics[0].Phi(0).p.data[1]);
87  const Eigen::Vector2d P2(kinematics[0].Phi(1).p.data[0], kinematics[0].Phi(1).p.data[1]);
88  const Eigen::Vector2d P3(kinematics[0].Phi(2).p.data[0], kinematics[0].Phi(2).p.data[1]);
89  PointToLineDistance(P1, P2, P3, phi(0));
90 
91  if (Server::IsRos() && debug_)
92  {
93  for (std::size_t i = 0; i < 3; ++i)
94  {
95  debug_marker_array_msg_.markers[i].pose.position.x = kinematics[0].Phi(i).p.data[0];
96  debug_marker_array_msg_.markers[i].pose.position.y = kinematics[0].Phi(i).p.data[1];
97  debug_marker_array_msg_.markers[i].pose.position.z = 0.0;
98  }
99 
100  // Line strip
101  debug_marker_array_msg_.markers[3].points.resize(2);
102  for (std::size_t i = 0; i < 2; ++i)
103  {
104  debug_marker_array_msg_.markers[3].points[i].x = kinematics[0].Phi(i).p.data[0];
105  debug_marker_array_msg_.markers[3].points[i].y = kinematics[0].Phi(i).p.data[1];
106  debug_marker_array_msg_.markers[3].points[i].z = 0.0;
107  }
108 
110  }
111 }
112 
114 {
115  if (phi.rows() != 1) ThrowNamed("Wrong size of Phi!");
116  if (jacobian.rows() != 1 || jacobian.cols() != kinematics[0].jacobian(0).data.cols()) ThrowNamed("Wrong size of jacobian! " << kinematics[0].jacobian(0).data.cols());
117 
118  const Eigen::Vector2d P1(kinematics[0].Phi(0).p.data[0], kinematics[0].Phi(0).p.data[1]);
119  const Eigen::Vector2d P2(kinematics[0].Phi(1).p.data[0], kinematics[0].Phi(1).p.data[1]);
120  const Eigen::Vector2d P3(kinematics[0].Phi(2).p.data[0], kinematics[0].Phi(2).p.data[1]);
121  PointToLineDistance(P1, P2, P3, phi(0));
122  PointToLineDistanceDerivative(P1, P2, P3, kinematics[0].jacobian[0].data, kinematics[0].jacobian[1].data, kinematics[0].jacobian[2].data, jacobian);
123 }
124 
126 {
127  return 1;
128 }
129 
130 } // namespace exotica
void publish(const boost::shared_ptr< M > &message) const
std::vector< KinematicFrameRequest > frames_
void PointToLineDistance(const Eigen::Vector2d &P1, const Eigen::Vector2d &P2, const Eigen::Vector2d &P3, double &d)
Computes the signed distance between a point and a line defined by two points in 2D.
Eigen::Ref< Eigen::VectorXd > VectorXdRef
#define ThrowPretty(m)
std::shared_ptr< Scene > ScenePtr
void PointToLineDistanceDerivative(const Eigen::Vector2d &P1, const Eigen::Vector2d &P2, const Eigen::Vector2d &P3, const Eigen::MatrixXd &dP1_dq, const Eigen::MatrixXd &dP2_dq, const Eigen::MatrixXd &dP3_dq, Eigen::Ref< Eigen::MatrixXd > &derivative)
Derivative of signed distance between a point and a line defined by two points in 2D...
std::string object_name_
#define ThrowNamed(m)
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
void Update(Eigen::VectorXdRefConst q, Eigen::VectorXdRef phi) final
visualization_msgs::MarkerArray debug_marker_array_msg_
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
REGISTER_TASKMAP_TYPE("DistanceToLine2D", exotica::DistanceToLine2D)
static bool IsRos()
void AssignScene(ScenePtr scene) final


exotica_core_task_maps
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:36:09