height_map.cc
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 
7 * Redistributions of source code must retain the above copyright notice, this
8  list of conditions and the following disclaimer.
9 
10 * Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 * Neither the name of the copyright holder nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ******************************************************************************/
29 
30 #include <towr/height_map.h>
31 
32 #include <cmath>
33 
34 namespace towr {
35 
36 double
37 HeightMap::GetDerivativeOfHeightWrt (Dim2D dim, double x, double y) const
38 {
39  switch (dim) {
40  case X: return GetHeightDerivWrtX(x,y);
41  case Y: return GetHeightDerivWrtY(x,y);
42  default: assert(false); // derivative dimension not implemented
43  }
44 }
45 
47 HeightMap::GetNormalizedBasis (Direction basis, double x, double y) const
48 {
49  return GetBasis(basis, x, y).normalized();
50 }
51 
53 HeightMap::GetBasis (Direction basis, double x, double y,
54  const DimDerivs& deriv) const
55 {
56  switch (basis) {
57  case Normal: return GetNormal(x,y, deriv);
58  case Tangent1: return GetTangent1(x,y, deriv);
59  case Tangent2: return GetTangent2(x,y, deriv);
60  default: assert(false); // basis does not exist
61  }
62 }
63 
66  double x, double y) const
67 {
68  // inner derivative
69  Vector3d dv_wrt_dim = GetBasis(basis, x, y, {dim});
70 
71  // outer derivative
72  Vector3d v = GetBasis(basis, x,y, {});
74  return dn_norm_wrt_n.cwiseProduct(dv_wrt_dim);
75 }
76 
78 HeightMap::GetNormal(double x, double y, const DimDerivs& deriv) const
79 {
80  Vector3d n;
81 
82  bool basis_requested = deriv.empty();
83 
84  for (auto dim : {X_,Y_}) {
85  if (basis_requested)
86  n(dim) = -GetDerivativeOfHeightWrt(dim, x, y);
87  else
88  n(dim) = -GetSecondDerivativeOfHeightWrt(dim, deriv.front(), x, y);
89  }
90 
91  n(Z) = basis_requested? 1.0 : 0.0;
92 
93  return n;
94 }
95 
97 HeightMap::GetTangent1 (double x, double y, const DimDerivs& deriv) const
98 {
99  Vector3d tx;
100 
101  bool basis_requested = deriv.empty();
102 
103  tx(X) = basis_requested? 1.0 : 0.0;
104  tx(Y) = 0.0;
105  tx(Z) = basis_requested? GetDerivativeOfHeightWrt(X_, x, y)
106  : GetSecondDerivativeOfHeightWrt(X_, deriv.front(), x, y);
107 
108  return tx;
109 }
110 
112 HeightMap::GetTangent2 (double x, double y, const DimDerivs& deriv) const
113 {
114  Vector3d ty;
115 
116  bool basis_requested = deriv.empty();
117 
118  ty(X) = 0.0;
119  ty(Y) = basis_requested? 1.0 : 0.0;
120  ty(Z) = basis_requested? GetDerivativeOfHeightWrt(Y_, x,y)
121  : GetSecondDerivativeOfHeightWrt(Y_, deriv.front(), x, y);
122  return ty;
123 }
124 
127  const Vector3d& v, int idx) const
128 {
129  // see notebook or
130  // http://blog.mmacklin.com/2012/05/
131  return 1/v.squaredNorm()*(v.norm() * Vector3d::Unit(idx) - v(idx)*v.normalized());
132 }
133 
134 double
136  double x, double y) const
137 {
138  if (dim1 == X_) {
139  if (dim2 == X_) return GetHeightDerivWrtXX(x,y);
140  if (dim2 == Y_) return GetHeightDerivWrtXY(x,y);
141  } else {
142  if (dim2 == X_) return GetHeightDerivWrtYX(x,y);
143  if (dim2 == Y_) return GetHeightDerivWrtYY(x,y);
144  }
145 
146  assert(false); // second derivative not specified.
147 }
148 
149 } /* namespace towr */
Vector3d GetBasis(Direction direction, double x, double y, const DimDerivs &dim_deriv={}) const
returns either the terrain normal/tangent or its derivative.
Definition: height_map.cc:53
virtual double GetHeightDerivWrtY(double x, double y) const
Definition: height_map.h:132
virtual double GetHeightDerivWrtYY(double x, double y) const
Definition: height_map.h:138
Vector3d GetDerivativeOfNormalizedVectorWrtNonNormalizedIndex(const Vector3d &non_normalized, int index) const
Definition: height_map.cc:126
Vector3d GetTangent1(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:97
virtual double GetHeightDerivWrtYX(double x, double y) const
Definition: height_map.h:137
Vector3d GetNormalizedBasis(Direction direction, double x, double y) const
Returns either the vector normal or tangent to the terrain patch.
Definition: height_map.cc:47
double GetSecondDerivativeOfHeightWrt(Dim2D dim1, Dim2D dim2, double x, double y) const
Definition: height_map.cc:135
Eigen::Vector3d Vector3d
Definition: height_map.h:56
Vector3d GetDerivativeOfNormalizedBasisWrt(Direction direction, Dim2D dim, double x, double y) const
How the terrain normal/tangent vectors change when moving in x or y.
Definition: height_map.cc:65
virtual double GetHeightDerivWrtX(double x, double y) const
Definition: height_map.h:131
virtual double GetHeightDerivWrtXX(double x, double y) const
Definition: height_map.h:135
virtual double GetHeightDerivWrtXY(double x, double y) const
Definition: height_map.h:136
std::vector< Dim2D > DimDerivs
dimensional derivatives
Definition: height_map.h:107
double GetDerivativeOfHeightWrt(Dim2D dim, double x, double y) const
How the height value changes at a 2D position in direction dim.
Definition: height_map.cc:37
Vector3d GetTangent2(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:112
Vector3d GetNormal(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:78


towr_core
Author(s): Alexander W. Winkler
autogenerated on Sat Apr 7 2018 02:15:57