height_map.cc
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00030 #include <towr/height_map.h>
00031 
00032 #include <cmath>
00033 
00034 namespace towr {
00035 
00036 double
00037 HeightMap::GetDerivativeOfHeightWrt (Dim2D dim, double x, double y) const
00038 {
00039   switch (dim) {
00040     case X: return GetHeightDerivWrtX(x,y);
00041     case Y: return GetHeightDerivWrtY(x,y);
00042     default: assert(false); // derivative dimension not implemented
00043   }
00044 }
00045 
00046 HeightMap::Vector3d
00047 HeightMap::GetNormalizedBasis (Direction basis, double x, double y) const
00048 {
00049   return GetBasis(basis, x, y).normalized();
00050 }
00051 
00052 HeightMap::Vector3d
00053 HeightMap::GetBasis (Direction basis, double x, double y,
00054                                   const DimDerivs& deriv) const
00055 {
00056   switch (basis) {
00057     case Normal:   return GetNormal(x,y, deriv);
00058     case Tangent1: return GetTangent1(x,y, deriv);
00059     case Tangent2: return GetTangent2(x,y, deriv);
00060     default: assert(false); // basis does not exist
00061   }
00062 }
00063 
00064 HeightMap::Vector3d
00065 HeightMap::GetDerivativeOfNormalizedBasisWrt (Direction basis, Dim2D dim,
00066                                               double x, double y) const
00067 {
00068   // inner derivative
00069   Vector3d dv_wrt_dim = GetBasis(basis, x, y, {dim});
00070 
00071   // outer derivative
00072   Vector3d v = GetBasis(basis, x,y, {});
00073   Vector3d dn_norm_wrt_n = GetDerivativeOfNormalizedVectorWrtNonNormalizedIndex(v, dim);
00074   return dn_norm_wrt_n.cwiseProduct(dv_wrt_dim);
00075 }
00076 
00077 HeightMap::Vector3d
00078 HeightMap::GetNormal(double x, double y, const DimDerivs& deriv) const
00079 {
00080   Vector3d n;
00081 
00082   bool basis_requested = deriv.empty();
00083 
00084   for (auto dim : {X_,Y_}) {
00085     if (basis_requested)
00086       n(dim) = -GetDerivativeOfHeightWrt(dim, x, y);
00087     else
00088       n(dim) = -GetSecondDerivativeOfHeightWrt(dim, deriv.front(), x, y);
00089   }
00090 
00091   n(Z) = basis_requested? 1.0 : 0.0;
00092 
00093   return n;
00094 }
00095 
00096 HeightMap::Vector3d
00097 HeightMap::GetTangent1 (double x, double y, const DimDerivs& deriv) const
00098 {
00099   Vector3d tx;
00100 
00101   bool basis_requested = deriv.empty();
00102 
00103   tx(X) = basis_requested? 1.0 : 0.0;
00104   tx(Y) = 0.0;
00105   tx(Z) = basis_requested? GetDerivativeOfHeightWrt(X_, x, y)
00106                          : GetSecondDerivativeOfHeightWrt(X_, deriv.front(), x, y);
00107 
00108   return tx;
00109 }
00110 
00111 HeightMap::Vector3d
00112 HeightMap::GetTangent2 (double x, double y, const DimDerivs& deriv) const
00113 {
00114   Vector3d ty;
00115 
00116   bool basis_requested = deriv.empty();
00117 
00118   ty(X) = 0.0;
00119   ty(Y) = basis_requested? 1.0 : 0.0;
00120   ty(Z) = basis_requested? GetDerivativeOfHeightWrt(Y_, x,y)
00121                          : GetSecondDerivativeOfHeightWrt(Y_, deriv.front(), x, y);
00122   return ty;
00123 }
00124 
00125 HeightMap::Vector3d
00126 HeightMap::GetDerivativeOfNormalizedVectorWrtNonNormalizedIndex (
00127     const Vector3d& v, int idx) const
00128 {
00129   // see notebook or
00130   // http://blog.mmacklin.com/2012/05/
00131   return 1/v.squaredNorm()*(v.norm() * Vector3d::Unit(idx) - v(idx)*v.normalized());
00132 }
00133 
00134 double
00135 HeightMap::GetSecondDerivativeOfHeightWrt (Dim2D dim1, Dim2D dim2,
00136                                            double x, double y) const
00137 {
00138   if (dim1 == X_) {
00139     if (dim2 == X_) return GetHeightDerivWrtXX(x,y);
00140     if (dim2 == Y_) return GetHeightDerivWrtXY(x,y);
00141   } else {
00142     if (dim2 == X_) return GetHeightDerivWrtYX(x,y);
00143     if (dim2 == Y_) return GetHeightDerivWrtYY(x,y);
00144   }
00145 
00146   assert(false); // second derivative not specified.
00147 }
00148 
00149 } /* namespace towr */


towr_core
Author(s): Alexander W. Winkler
autogenerated on Mon Apr 9 2018 03:12:44