ProlateHyperspheroid.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, University of Toronto
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the University of Toronto 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
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Jonathan Gammell*/
36 
37 // The class's header
38 #include "ompl/util/ProlateHyperspheroid.h"
39 // For OMPL exceptions
40 #include "ompl/util/Exception.h"
41 // For OMPL information
42 #include "ompl/util/Console.h"
43 // For geometric equations like prolateHyperspheroidMeasure
44 #include "ompl/util/GeometricEquations.h"
45 
46 // For std::make_shared
47 #include <memory>
48 
49 // Eigen core:
50 #include <Eigen/Core>
51 // Inversion and determinants
52 #include <Eigen/LU>
53 // SVD decomposition
54 #include <Eigen/SVD>
55 
56 
57 
58 
59 struct ompl::ProlateHyperspheroid::PhsData
60 {
62  unsigned int dim_;
64  bool isTransformUpToDate_;
66  double minTransverseDiameter_;
68  double transverseDiameter_;
70  double phsMeasure_;
72  Eigen::VectorXd xFocus1_;
74  Eigen::VectorXd xFocus2_;
76  Eigen::VectorXd xCentre_;
78  Eigen::MatrixXd rotationWorldFromEllipse_;
80  Eigen::MatrixXd transformationWorldFromEllipse_;
81 };
82 
83 
84 ompl::ProlateHyperspheroid::ProlateHyperspheroid(unsigned int n, const double focus1[], const double focus2[])
85  : dataPtr_ (std::make_shared<PhsData>())
86 {
87  //Initialize the data:
88  dataPtr_->dim_ = n;
89  dataPtr_->transverseDiameter_ = 0.0; // Initialize to something.
90  dataPtr_->isTransformUpToDate_ = false;
91 
92  // Copy the arrays into their Eigen containers via the Eigen::Map "view"
93  dataPtr_->xFocus1_ = Eigen::Map<const Eigen::VectorXd>(focus1, dataPtr_->dim_);
94  dataPtr_->xFocus2_ = Eigen::Map<const Eigen::VectorXd>(focus2, dataPtr_->dim_);
95 
96  // Calculate the minimum transverse diameter
97  dataPtr_->minTransverseDiameter_ = (dataPtr_->xFocus1_ - dataPtr_->xFocus2_).norm();
98 
99  // Calculate the centre:
100  dataPtr_->xCentre_ = 0.5 * (dataPtr_->xFocus1_ + dataPtr_->xFocus2_);
101 
102  // Calculate the rotation
103  updateRotation();
104 }
105 
107 {
108  if (transverseDiameter < dataPtr_->minTransverseDiameter_)
109  {
110  OMPL_ERROR("%g < %g", transverseDiameter, dataPtr_->minTransverseDiameter_);
111  throw Exception("Transverse diameter cannot be less than the distance between the foci.");
112  }
113 
114  // Store and update if changed
115  if (dataPtr_->transverseDiameter_ != transverseDiameter)
116  {
117  // Mark as out of date
118  dataPtr_->isTransformUpToDate_ = false;
119 
120  // Store
121  dataPtr_->transverseDiameter_ = transverseDiameter;
122 
123  // Update the transform
124  updateTransformation();
125  }
126  // No else, the diameter didn't change
127 }
128 
129 void ompl::ProlateHyperspheroid::transform(const double sphere[], double phs[]) const
130 {
131  if (dataPtr_->isTransformUpToDate_ == false)
132  {
133  throw Exception("The transformation is not up to date in the PHS class. Has the transverse diameter been set?");
134  }
135 
136  // Calculate the tranformation and offset, using Eigen::Map views of the data
137  Eigen::Map<Eigen::VectorXd>(phs, dataPtr_->dim_) = dataPtr_->transformationWorldFromEllipse_*Eigen::Map<const Eigen::VectorXd>(sphere, dataPtr_->dim_);
138  Eigen::Map<Eigen::VectorXd>(phs, dataPtr_->dim_) += dataPtr_->xCentre_;
139 }
140 
141 bool ompl::ProlateHyperspheroid::isInPhs(const double point[]) const
142 {
143  if (dataPtr_->isTransformUpToDate_ == false)
144  {
145  // The transform is not up to date until the transverse diameter has been set
146  throw Exception ("The transverse diameter has not been set");
147  }
148 
149  return (getPathLength(point) < dataPtr_->transverseDiameter_);
150 }
151 
152 bool ompl::ProlateHyperspheroid::isOnPhs(const double point[]) const
153 {
154  if (dataPtr_->isTransformUpToDate_ == false)
155  {
156  // The transform is not up to date until the transverse diameter has been set
157  throw Exception ("The transverse diameter has not been set");
158  }
159 
160  return (getPathLength(point) == dataPtr_->transverseDiameter_);
161 }
162 
164 {
165  return dataPtr_->dim_;
166 }
167 
168 
170 {
171  if (dataPtr_->isTransformUpToDate_ == false)
172  {
173  // The transform is not up to date until the transverse diameter has been set, therefore we have no transverse diameter and we have infinite measure
174  return std::numeric_limits<double>::infinity();
175  }
176  else
177  {
178  // Calculate and return:
179  return dataPtr_->phsMeasure_;
180  }
181 }
182 
183 double ompl::ProlateHyperspheroid::getPhsMeasure(double tranDiam) const
184 {
185  return prolateHyperspheroidMeasure(dataPtr_->dim_, dataPtr_->minTransverseDiameter_, tranDiam);
186 }
187 
189 {
190  return dataPtr_->minTransverseDiameter_;
191 }
192 
193 double ompl::ProlateHyperspheroid::getPathLength(const double point[]) const
194 {
195  return (dataPtr_->xFocus1_ - Eigen::Map<const Eigen::VectorXd>(point, dataPtr_->dim_)).norm() + (Eigen::Map<const Eigen::VectorXd>(point, dataPtr_->dim_) - dataPtr_->xFocus2_).norm();
196 }
197 
199 {
200  return dataPtr_->dim_;
201 }
202 
203 void ompl::ProlateHyperspheroid::updateRotation(void)
204 {
205  // Mark the transform as out of date
206  dataPtr_->isTransformUpToDate_ = false;
207 
208  // If the minTransverseDiameter_ is too close to 0, we treat this as a circle.
209  double circleTol = 1E-9;
210  if (dataPtr_->minTransverseDiameter_ < circleTol)
211  {
212  dataPtr_->rotationWorldFromEllipse_.setIdentity(dataPtr_->dim_, dataPtr_->dim_);
213  }
214  else
215  {
216  // Variables
217  // The transverse axis of the PHS expressed in the world frame.
218  Eigen::VectorXd transverseAxis(dataPtr_->dim_);
219  // The matrix representation of the Wahba problem
220  Eigen::MatrixXd wahbaProb(dataPtr_->dim_, dataPtr_->dim_);
221  // The middle diagonal matrix in the SVD solution to the Wahba problem
222  Eigen::VectorXd middleM(dataPtr_->dim_);
223 
224  // Calculate the major axis, storing as the first eigenvector
225  transverseAxis = (dataPtr_->xFocus2_ - dataPtr_->xFocus1_ )/dataPtr_->minTransverseDiameter_;
226 
227  // Calculate the rotation that will allow us to generate the remaining eigenvectors
228  // Formulate as a Wahba problem, first forming the matrix a_j*a_i' where a_j is the transverse axis if the ellipse in the world frame, and a_i is the first basis vector of the world frame (i.e., [1 0 .... 0])
229  wahbaProb = transverseAxis * Eigen::MatrixXd::Identity(dataPtr_->dim_, dataPtr_->dim_).col(0).transpose();
230 
231  // Then run it through the SVD solver
232  Eigen::JacobiSVD<Eigen::MatrixXd, Eigen::NoQRPreconditioner> svd(wahbaProb, Eigen::ComputeFullV | Eigen::ComputeFullU);
233 
234  // Then calculate the rotation matrix from the U and V components of SVD
235  // Calculate the middle diagonal matrix
236  middleM = Eigen::VectorXd::Ones(dataPtr_->dim_);
237  // Make the last value equal to det(U)*det(V) (zero-based indexing remember)
238  middleM(dataPtr_->dim_ - 1) = svd.matrixU().determinant() * svd.matrixV().determinant();
239 
240  // Calculate the rotation
241  dataPtr_->rotationWorldFromEllipse_ = svd.matrixU() * middleM.asDiagonal() * svd.matrixV().transpose();
242  }
243 }
244 
245 void ompl::ProlateHyperspheroid::updateTransformation(void)
246 {
247  // Variables
248  // The radii of the ellipse
249  Eigen::VectorXd diagAsVector(dataPtr_->dim_);
250  // The conjugate diameters:
251  double conjugateDiamater;
252 
253  // Calculate the conjugate radius
254  conjugateDiamater = std::sqrt(dataPtr_->transverseDiameter_*dataPtr_->transverseDiameter_ - dataPtr_->minTransverseDiameter_*dataPtr_->minTransverseDiameter_);
255 
256  // Store into the diagonal matrix
257  // All the elements but one are the conjugate radius
258  diagAsVector.fill(conjugateDiamater/2.0);
259 
260  // The first element in diagonal is the transverse radius
261  diagAsVector(0) = 0.5 * dataPtr_->transverseDiameter_;
262 
263  // Calculate the transformation matrix
264  dataPtr_->transformationWorldFromEllipse_ = dataPtr_->rotationWorldFromEllipse_ * diagAsVector.asDiagonal();
265 
266  // Calculate the measure:
267  dataPtr_->phsMeasure_ = prolateHyperspheroidMeasure(dataPtr_->dim_, dataPtr_->minTransverseDiameter_, dataPtr_->transverseDiameter_);
268 
269  // Mark as up to date
270  dataPtr_->isTransformUpToDate_ = true;
271 }
STL namespace.
void setTransverseDiameter(double transverseDiameter)
Set the transverse diameter of the PHS.
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
double getPhsMeasure(void) const
The measure of the PHS.
unsigned int getPhsDimension(void) const
The dimension of the PHS.
double prolateHyperspheroidMeasure(unsigned int N, double dFoci, double dTransverse)
The Lebesgue measure (i.e., "volume") of an n-dimensional prolate hyperspheroid (a symmetric hyperell...
The exception type for ompl.
Definition: Exception.h:47
void transform(const double sphere[], double phs[]) const
Transform a point from a sphere to PHS. The return variable phs is expected to already exist...
bool isInPhs(const double point[]) const
Check if the given point lies in the PHS.
ProlateHyperspheroid(unsigned int n, const double focus1[], const double focus2[])
The description of an n-dimensional prolate hyperspheroid.
double getMinTransverseDiameter(void) const
The minimum transverse diameter of the PHS, i.e., the distance between the foci.
bool isOnPhs(const double point[]) const
Check if the given point lies on the PHS.
double getPathLength(const double point[]) const
Calculate length of a line that originates from one focus, passes through the given point...
unsigned int getDimension() const
The state dimension of the PHS.