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


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