sphere.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 // Sphere for tracking error bound. Spheres are defined only by their radius.
40 //
42 
43 #ifndef FASTRACK_BOUND_SPHERE_H
44 #define FASTRACK_BOUND_SPHERE_H
45 
47 #include <fastrack/utils/types.h>
48 
49 #include <fastrack_srvs/TrackingBoundSphere.h>
50 #include <fastrack_srvs/TrackingBoundSphereResponse.h>
51 
52 namespace fastrack {
53 namespace bound {
54 
55 struct Sphere
56  : public TrackingBoundRos<fastrack_srvs::TrackingBoundSphere::Response> {
57  // Radius.
58  double r;
59 
60  // Initialize from vector.
61  bool Initialize(const std::vector<double>& params) {
62  if (params.size() != 1) {
63  ROS_ERROR("Sphere: params were incorrect size.");
64  return false;
65  }
66 
67  r = params[0];
68  return true;
69  }
70 
71  // Convert from service response type SR.
72  void FromRos(const fastrack_srvs::TrackingBoundSphere::Response& res) {
73  r = res.r;
74  }
75 
76  // Convert to service response.
77  fastrack_srvs::TrackingBoundSphere::Response ToRos() const {
78  fastrack_srvs::TrackingBoundSphere::Response res;
79  res.r = r;
80 
81  return res;
82  }
83 
84  // Returns true if this tracking error bound (at the given position) overlaps
85  // with different shapes.
86  bool OverlapsSphere(const Vector3d& p, const Vector3d& center,
87  double radius) const {
88  return (p - center).squaredNorm() <= (radius + r) * (radius + r);
89  }
90 
91  bool OverlapsBox(const Vector3d& p, const Vector3d& lower,
92  const Vector3d& upper) const {
93  const Vector3d closest_point(std::min(upper(0), std::max(lower(0), p(0))),
94  std::min(upper(1), std::max(lower(1), p(1))),
95  std::min(upper(2), std::max(lower(2), p(2))));
96  return (closest_point - p).squaredNorm() <= r * r;
97  }
98 
99  // Returns true if this tracking error bound (at the given position) is
100  // contained within a box.
101  bool ContainedWithinBox(const Vector3d& p, const Vector3d& lower,
102  const Vector3d& upper) const {
103  return p(0) >= lower(0) + r && p(0) <= upper(0) - r &&
104  p(1) >= lower(1) + r && p(1) <= upper(1) - r &&
105  p(2) >= lower(2) + r && p(2) <= upper(2) - r;
106  }
107 
108  // Visualize.
109  void Visualize(const ros::Publisher& pub, const std::string& frame) const {
110  visualization_msgs::Marker m;
111  m.ns = "bound";
112  m.header.frame_id = frame;
113  m.header.stamp = ros::Time::now();
114  m.id = 0;
115  m.type = visualization_msgs::Marker::SPHERE;
116  m.action = visualization_msgs::Marker::ADD;
117  m.color.a = 0.3;
118  m.color.r = 0.5;
119  m.color.g = 0.1;
120  m.color.b = 0.5;
121  m.scale.x = 2.0 * r;
122  m.scale.y = 2.0 * r;
123  m.scale.z = 2.0 * r;
124 
125  pub.publish(m);
126  }
127 
128 }; //\struct Sphere
129 
130 } //\namespace bound
131 } //\namespace fastrack
132 
133 #endif
bool ContainedWithinBox(const Vector3d &p, const Vector3d &lower, const Vector3d &upper) const
Definition: sphere.h:101
void FromRos(const fastrack_srvs::TrackingBoundSphere::Response &res)
Definition: sphere.h:72
bool OverlapsBox(const Vector3d &p, const Vector3d &lower, const Vector3d &upper) const
Definition: sphere.h:91
void Visualize(const ros::Publisher &pub, const std::string &frame) const
Definition: sphere.h:109
Definition: box.h:53
bool Initialize(const std::vector< double > &params)
Definition: sphere.h:61
fastrack_srvs::TrackingBoundSphere::Response ToRos() const
Definition: sphere.h:77
bool OverlapsSphere(const Vector3d &p, const Vector3d &center, double radius) const
Definition: sphere.h:86


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