segment.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2015, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #define BOOST_PARAMETER_MAX_ARITY 7
00037 
00038 #include "jsk_recognition_utils/geo/segment.h"
00039 #include "jsk_recognition_utils/geo_util.h"
00040 namespace jsk_recognition_utils
00041 {
00042   Segment::Segment(const Eigen::Vector3f& from, const Eigen::Vector3f to):
00043     Line(to - from, from), to_(to), length_((to - from).norm())
00044   {
00045   }
00046 
00047   double Segment::dividingRatio(const Eigen::Vector3f& point) const
00048   {
00049     if (to_[0] != origin_[0]) {
00050       return (point[0] - origin_[0]) / (to_[0] - origin_[0]);
00051     }
00052     else if (to_[1] != origin_[1]) {
00053       return (point[1] - origin_[1]) / (to_[1] - origin_[1]);
00054     }
00055     else {
00056       return (point[2] - origin_[2]) / (to_[2] - origin_[2]);
00057     }
00058   }
00059 
00060   void Segment::foot(const Eigen::Vector3f& from, Eigen::Vector3f& output) const
00061   {
00062     Eigen::Vector3f foot_point;
00063     Line::foot(from, foot_point);
00064     double r = dividingRatio(foot_point);
00065     if (r < 0.0) {
00066       output = origin_;
00067     }
00068     else if (r > 1.0) {
00069       output = to_;
00070     }
00071     else {
00072       output = foot_point;
00073     }
00074   }
00075 
00076   double Segment::distance(const Eigen::Vector3f& point) const
00077   {
00078     Eigen::Vector3f foot_point;
00079     return distance(point, foot_point);
00080   }
00081 
00082   double Segment::distance(const Eigen::Vector3f& point,
00083                            Eigen::Vector3f& foot_point) const
00084   {
00085     foot(point, foot_point);
00086     return (foot_point - point).norm();
00087   }
00088 
00089   bool Segment::intersect(Plane& plane, Eigen::Vector3f& point) const
00090   {
00091     double x = - (plane.getNormal().dot(origin_) + plane.getD()) / (plane.getNormal().dot(direction_));
00092     point = direction_ * x + origin_;
00093     double r = dividingRatio(point);
00094     return 0 <= r && r <= 1.0;
00095   }
00096 
00097   void Segment::midpoint(Eigen::Vector3f& midpoint) const
00098   {
00099     midpoint = (origin_ + to_) * 0.5;
00100   }
00101 
00102   std::ostream& operator<<(std::ostream& os, const Segment& seg)
00103   {
00104     os << "[" << seg.origin_[0] << ", " << seg.origin_[1] << ", " << seg.origin_[2] << "] -- "
00105        << "[" << seg.to_[0] << ", " << seg.to_[1] << ", " << seg.to_[2] << "]";
00106   }
00107 
00108   void Segment::getEnd(Eigen::Vector3f& output) const
00109   {
00110     output = to_;
00111   }
00112 
00113   Eigen::Vector3f Segment::getEnd() const
00114   {
00115     return to_;
00116   }
00117 
00118   double Segment::distanceWithInfo(const Eigen::Vector3f& from,
00119                                    Eigen::Vector3f& foot_point,
00120                                    double &distance_to_goal) const
00121   {
00122     const double alpha = computeAlpha(from);
00123 
00124     if (alpha >= 0 && alpha <= length_) {
00125       // foot on the line
00126       foot_point = alpha * direction_ + origin_;
00127       distance_to_goal = length_ - alpha;
00128     } else if (alpha < 0) {
00129       // foot out of the line
00130       foot_point = origin_;
00131       distance_to_goal = length_;
00132     } else {
00133       foot_point = to_;
00134       distance_to_goal = 0;
00135     }
00136     return (from - foot_point).norm();
00137   }
00138 
00139   Segment::Ptr Segment::flipSegment() const
00140   {
00141     Segment::Ptr ret (new Segment(to_, origin_));
00142     return ret;
00143   }
00144 
00145   double Segment::length() const
00146   {
00147     return length_;
00148   }
00149 
00150   void Segment::toMarker(visualization_msgs::Marker& marker) const
00151   {
00152     marker.type = visualization_msgs::Marker::ARROW;//
00153 
00154     geometry_msgs::Point st;
00155     geometry_msgs::Point ed;
00156     st.x = origin_[0];
00157     st.y = origin_[1];
00158     st.z = origin_[2];
00159     ed.x = to_[0];
00160     ed.y = to_[1];
00161     ed.z = to_[2];
00162 
00163     marker.points.push_back(st);
00164     marker.points.push_back(ed);
00165 
00166     marker.scale.x = 0.012;
00167     marker.scale.y = 0.02;
00168     marker.color.a = 1;
00169     marker.color.r = 1;
00170     marker.color.g = 1;
00171     marker.color.b = 0;
00172   }
00173 
00174   bool Segment::isCross (const Line &ln, double distance_threshold) const
00175   {
00176     Eigen::Vector3f ln_origin = ln.getOrigin();
00177     Eigen::Vector3f ln_direction = ln.getDirection();
00178     Eigen::Vector3f v12 = (ln_origin - origin_);
00179     double n1n2 = ln_direction.dot(direction_);
00180     if (fabs(n1n2) < 1e-20) { // parallel
00181       return false;
00182     }
00183     double alp1 = (ln_direction.dot(v12) - (n1n2 * direction_.dot(v12))) / (1 - n1n2 * n1n2);
00184     double alp2 = ((n1n2 * ln_direction.dot(v12)) - direction_.dot(v12)) / (1 - n1n2 * n1n2);
00185 
00186     if (// alp1 >= 0 && alp1 <= ln.length() &&
00187         alp2 >= 0 && alp2 <= length_) {
00188       Eigen::Vector3f p1 = alp1 * ln_direction + ln_origin;
00189       Eigen::Vector3f p2 = alp2 * direction_ + origin_;
00190       if ((p1 - p2).norm() < distance_threshold) {
00191         return true;
00192       } else {
00193         return false;
00194       }
00195     }
00196 
00197     return false;
00198   }
00199 
00200   bool Segment::isCross (const Segment &ln, double distance_threshold) const
00201   {
00202     Eigen::Vector3f ln_origin = ln.getOrigin();
00203     Eigen::Vector3f ln_direction = ln.getDirection();
00204     Eigen::Vector3f v12 = (ln_origin - origin_);
00205     double n1n2 = ln_direction.dot(direction_);
00206     if (fabs(n1n2) < 1e-20) { // parallel
00207       return false;
00208     }
00209     double alp1 = (ln_direction.dot(v12) - (n1n2 * direction_.dot(v12))) / (1 - n1n2 * n1n2);
00210     double alp2 = ((n1n2 * ln_direction.dot(v12)) - direction_.dot(v12)) / (1 - n1n2 * n1n2);
00211 
00212     if (alp1 >= 0 && alp1 <= ln.length() &&
00213         alp2 >= 0 && alp2 <= length_) {
00214       Eigen::Vector3f p1 = alp1 * ln_direction + ln_origin;
00215       Eigen::Vector3f p2 = alp2 * direction_ + origin_;
00216       if ((p1 - p2).norm() < distance_threshold) {
00217         return true;
00218       } else {
00219         return false;
00220       }
00221     }
00222 
00223     return false;
00224   }
00225 }


jsk_recognition_utils
Author(s):
autogenerated on Tue Jul 2 2019 19:40:37