cylinder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, The Regents of the University of California (Regents).
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
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
38 //
39 // Cylinder for tracking error bound. This can occur when dynamics decouple
40 // in z vs x/y. Cylinders are parameterized by radius and half vertical height.
41 //
43 
44 #ifndef FASTRACK_BOUND_CYLINDER_H
45 #define FASTRACK_BOUND_CYLINDER_H
46 
48 #include <fastrack/utils/types.h>
49 
50 #include <fastrack_srvs/TrackingBoundCylinder.h>
51 #include <fastrack_srvs/TrackingBoundCylinderResponse.h>
52 
53 namespace fastrack {
54 namespace bound {
55 
56 struct Cylinder
57  : public TrackingBoundRos<fastrack_srvs::TrackingBoundCylinder::Response> {
58  // Radius.
59  double r;
60 
61  // Half vertical extent.
62  double z;
63 
64  // Initialize from vector. Assume laid out as: [r, z].
65  bool Initialize(const std::vector<double>& params) {
66  if (params.size() != 2) {
67  ROS_ERROR("Cylinder: params were incorrect size.");
68  return false;
69  }
70 
71  r = params[0];
72  z = params[1];
73  return true;
74  }
75 
76  // Convert from service response type SR.
77  void FromRos(const fastrack_srvs::TrackingBoundCylinder::Response& res) {
78  r = res.r;
79  }
80 
81  // Convert to service response.
82  fastrack_srvs::TrackingBoundCylinder::Response ToRos() const {
83  fastrack_srvs::TrackingBoundCylinder::Response res;
84  res.r = r;
85 
86  return res;
87  }
88 
89  // Returns true if this tracking error bound (at the given position) overlaps
90  // with different shapes.
91  bool OverlapsSphere(const Vector3d& p, const Vector3d& center,
92  double radius) const {
93  // Catch no overlap in z.
94  if (p(2) + z < center(2) - radius || p(2) - z > center(2) + radius)
95  return false;
96 
97  // Find z coordinate on cylinder closest to the sphere.
98  const double closest_z = std::max(p(2) - z, std::min(p(2) + z, center(2)));
99 
100  // Compute radius of sphere at this z coordinate.
101  const double dz = center(2) - closest_z;
102  const double effective_r = std::sqrt(radius * radius - dz * dz);
103 
104  return (p.head<2>() - center.head<2>()).squaredNorm() <=
105  (effective_r + r) * (effective_r + r);
106  }
107 
108  bool OverlapsBox(const Vector3d& p, const Vector3d& lower,
109  const Vector3d& upper) const {
110  // Catch no overlap in z.
111  if (p(2) < lower(2) - z || p(2) > lower(2) + z) return false;
112 
113  // Overlapping in z, so check x/y.
114  const double closest_x = std::min(upper(0), std::max(p(0), lower(0)));
115  const double closest_y = std::min(upper(1), std::max(p(1), lower(1)));
116  const double dx = closest_x - p(0);
117  const double dy = closest_y - p(1);
118 
119  return dx * dx + dy * dy <= r;
120  }
121 
122  // Returns true if this tracking error bound (at the given position) is
123  // contained within a box.
124  bool ContainedWithinBox(const Vector3d& p, const Vector3d& lower,
125  const Vector3d& upper) const {
126  return p(0) >= lower(0) + r && p(0) <= upper(0) - r &&
127  p(1) >= lower(1) + r && p(1) <= upper(1) - r &&
128  p(2) >= lower(2) + z && p(2) <= upper(2) - z;
129  }
130 
131  // Visualize.
132  void Visualize(const ros::Publisher& pub, const std::string& frame) const {
133  visualization_msgs::Marker m;
134  m.ns = "bound";
135  m.header.frame_id = frame;
136  m.header.stamp = ros::Time::now();
137  m.id = 0;
138  m.type = visualization_msgs::Marker::CYLINDER;
139  m.action = visualization_msgs::Marker::ADD;
140  m.color.a = 0.3;
141  m.color.r = 0.5;
142  m.color.g = 0.1;
143  m.color.b = 0.5;
144  m.scale.x = 2.0 * r;
145  m.scale.y = 2.0 * r;
146  m.scale.z = 2.0 * z;
147 
148  pub.publish(m);
149  }
150 
151 }; //\struct Cylinder
152 
153 } //\namespace bound
154 } //\namespace fastrack
155 
156 #endif
bool Initialize(const std::vector< double > &params)
Definition: cylinder.h:65
bool OverlapsSphere(const Vector3d &p, const Vector3d &center, double radius) const
Definition: cylinder.h:91
fastrack_srvs::TrackingBoundCylinder::Response ToRos() const
Definition: cylinder.h:82
void FromRos(const fastrack_srvs::TrackingBoundCylinder::Response &res)
Definition: cylinder.h:77
bool OverlapsBox(const Vector3d &p, const Vector3d &lower, const Vector3d &upper) const
Definition: cylinder.h:108
Definition: box.h:53
bool ContainedWithinBox(const Vector3d &p, const Vector3d &lower, const Vector3d &upper) const
Definition: cylinder.h:124
void Visualize(const ros::Publisher &pub, const std::string &frame) const
Definition: cylinder.h:132


fastrack
Author(s): David Fridovich-Keil
autogenerated on Mon Aug 3 2020 21:28:37