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(from - to, from), from_(from), to_(to)
00044   {
00045     
00046   }
00047 
00048   double Segment::dividingRatio(const Eigen::Vector3f& point) const
00049   {
00050     if (to_[0] != from_[0]) {
00051       return (point[0] - from_[0]) / (to_[0] - from_[0]);
00052     }
00053     else if (to_[1] != from_[1]) {
00054       return (point[1] - from_[1]) / (to_[1] - from_[1]);
00055     }
00056     else {
00057       return (point[2] - from_[2]) / (to_[2] - from_[2]);
00058     }
00059   }
00060   
00061   void Segment::foot(const Eigen::Vector3f& from, Eigen::Vector3f& output) const
00062   {
00063     Eigen::Vector3f foot_point;
00064     Line::foot(from, foot_point);
00065     double r = dividingRatio(foot_point);
00066     if (r < 0.0) {
00067       output = from_;
00068     }
00069     else if (r > 1.0) {
00070       output = to_;
00071     }
00072     else {
00073       output = foot_point;
00074     }
00075   }
00076 
00077   double Segment::distance(const Eigen::Vector3f& point) const
00078   {
00079     Eigen::Vector3f foot_point;
00080     return distance(point, foot_point);
00081   }
00082 
00083   double Segment::distance(const Eigen::Vector3f& point,
00084                            Eigen::Vector3f& foot_point) const
00085   {
00086     foot(point, foot_point);
00087     return (foot_point - point).norm();
00088   }
00089 
00090   bool Segment::intersect(Plane& plane, Eigen::Vector3f& point) const
00091   {
00092     double x = - (plane.getNormal().dot(origin_) + plane.getD()) / (plane.getNormal().dot(direction_));
00093     point = direction_ * x + origin_;
00094     double r = dividingRatio(point);
00095     return 0 <= r && r <= 1.0;
00096   }
00097 
00098   std::ostream& operator<<(std::ostream& os, const Segment& seg)
00099   {
00100     os << "[" << seg.from_[0] << ", " << seg.from_[1] << ", " << seg.from_[2] << "] -- "
00101        << "[" << seg.to_[0] << ", " << seg.to_[1] << ", " << seg.to_[2] << "]";
00102   }
00103 
00104 }


jsk_recognition_utils
Author(s):
autogenerated on Wed Sep 16 2015 04:36:01