viewpoint_sampler.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
37 #include <angles/angles.h>
38 #include <opencv2/opencv.hpp>
39 
40 namespace jsk_pcl_ros
41 {
43  double angle_step, double angle_min, double angle_max,
44  double radius_step, double radius_min, double radius_max, int n_points):
45  angle_step_(angle_step), angle_min_(angle_min), angle_max_(angle_max),
46  radius_step_(radius_step), radius_min_(radius_min), radius_max_(radius_max),
47  index_(0),
48  angle_(angle_min),
49  radius_(radius_min),
50  n_points_(n_points)
51  {
52 
53  }
54 
56  {
57  index_ = 0;
60  }
61 
63  {
65  if (angle_ > angle_max_ * 1.001) { // 1.001 for prevent rounding error
68  if (radius_ > radius_max_ * 1.001) { // 1.001 for prevent rounding error
70  ++index_;
71  }
72  }
73  }
74 
76  {
78  }
79 
81  double& x, double& y, double& z)
82  {
83  double norm = sqrt(x*x + y*y + z*z);
84  x /= norm;
85  y /= norm;
86  z /= norm;
87  }
88 
89  void ViewpointSampler::get(Eigen::Affine3f& transform)
90  {
91  // compute the Point(x, y ,z) on the sphere based on index_ and radius_ using Golden Spiral technique
92  double angle_rad = angles::from_degrees(angle_);
93  const double inc = CV_PI * (3 - sqrt(5));
94  const double off = 2.0f / double(n_points_);
95  double y = index_ * off - 1.0f + (off / 2.0f);
96  double r = sqrt(1.0f - y * y);
97  double phi = index_ * inc;
98  double x = std::cos(phi) * r;
99  double z = std::sin(phi) * r;
100  double lat = std::acos(z), lon;
101  if ((fabs(std::sin(lat)) < 1e-5) || (fabs(y / std::sin(lat)) > 1))
102  lon = 0;
103  else
104  lon = std::asin(y / std::sin(lat));
105  x *= radius_; // * cos(lon) * sin(lat);
106  y *= radius_; //double y = radius * sin(lon) * sin(lat);
107  z *= radius_; //double z = radius * cos(lat);
108 
109  cv::Vec3d T(x, y, z);
110 
111  // Figure out the up vector
112  double x_up = radius_ * std::cos(lon) * std::sin(lat - 1e-5) - x;
113  double y_up = radius_ * std::sin(lon) * std::sin(lat - 1e-5) - y;
114  double z_up = radius_ * std::cos(lat - 1e-5) - z;
115  normalizeVector(x_up, y_up, z_up);
116 
117  // Figure out the third vector of the basis
118  double x_right = -y_up * z + z_up * y;
119  double y_right = x_up * z - z_up * x;
120  double z_right = -x_up * y + y_up * x;
121  normalizeVector(x_right, y_right, z_right);
122 
123  // Rotate the up vector in that basis
124  double x_new_up = x_up * std::cos(angle_rad) + x_right * std::sin(angle_rad);
125  double y_new_up = y_up * std::cos(angle_rad) + y_right * std::sin(angle_rad);
126  double z_new_up = z_up * std::cos(angle_rad) + z_right * std::sin(angle_rad);
127  cv::Vec3d up(x_new_up, y_new_up, z_new_up);
128 
129  // compute the left vector
130  cv::Vec3d l;
131  l = up.cross(T); // cross product
132  normalizeVector(l(0),l(1),l(2));
133 
134  up = T.cross(l); // cross product
135  normalizeVector(up(0), up(1), up(2));
136 
137  // compute transformation
138  Eigen::Vector3f ez = Eigen::Vector3f(-T[0], -T[1], -T[2]).normalized();
139  Eigen::Vector3f ey = Eigen::Vector3f(up[0], up[1], up[2]).normalized();
140  Eigen::Vector3f ex = ey.cross(ez).normalized();
141  Eigen::Vector3f translation = Eigen::Vector3f(T[0], T[1], T[2]);
142  Eigen::Matrix3f mat;
143  mat.col(0) = ex;
144  mat.col(1) = ey;
145  mat.col(2) = ez;
146 
147  Eigen::Quaternionf q(mat);
148  transform = Eigen::Translation3f(translation) * Eigen::AngleAxisf(q);
149  }
150 }
pointer T
ViewpointSampler(double angle_step, double angle_min, double angle_max, double radius_step, double radius_min, double radius_max, int n_points)
q
long l
double sqrt()
static double from_degrees(double degrees)
virtual void normalizeVector(double &x, double &y, double &z)
virtual void get(Eigen::Affine3f &transform)


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:47