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 
32 
33 #include <cmath>
34 
35 namespace towr {
36 
39 {
40  switch (type) {
41  case FlatID: return std::make_shared<FlatGround>(); break;
42  case BlockID: return std::make_shared<Block>(); break;
43  case StairsID: return std::make_shared<Stairs>(); break;
44  case GapID: return std::make_shared<Gap>(); break;
45  case SlopeID: return std::make_shared<Slope>(); break;
46  case ChimneyID: return std::make_shared<Chimney>(); break;
47  case ChimneyLRID: return std::make_shared<ChimneyLR>(); break;
48  default: assert(false); break;
49  }
50 }
51 
52 double
53 HeightMap::GetDerivativeOfHeightWrt (Dim2D dim, double x, double y) const
54 {
55  switch (dim) {
56  case X: return GetHeightDerivWrtX(x,y);
57  case Y: return GetHeightDerivWrtY(x,y);
58  default: assert(false); // derivative dimension not implemented
59  }
60 }
61 
63 HeightMap::GetNormalizedBasis (Direction basis, double x, double y) const
64 {
65  return GetBasis(basis, x, y).normalized();
66 }
67 
69 HeightMap::GetBasis (Direction basis, double x, double y,
70  const DimDerivs& deriv) const
71 {
72  switch (basis) {
73  case Normal: return GetNormal(x,y, deriv);
74  case Tangent1: return GetTangent1(x,y, deriv);
75  case Tangent2: return GetTangent2(x,y, deriv);
76  default: assert(false); // basis does not exist
77  }
78 }
79 
82  double x, double y) const
83 {
84  // inner derivative
85  Vector3d dv_wrt_dim = GetBasis(basis, x, y, {dim});
86 
87  // outer derivative
88  Vector3d v = GetBasis(basis, x,y, {});
90  return dn_norm_wrt_n.cwiseProduct(dv_wrt_dim);
91 }
92 
94 HeightMap::GetNormal(double x, double y, const DimDerivs& deriv) const
95 {
96  Vector3d n;
97 
98  bool basis_requested = deriv.empty();
99 
100  for (auto dim : {X_,Y_}) {
101  if (basis_requested)
102  n(dim) = -GetDerivativeOfHeightWrt(dim, x, y);
103  else
104  n(dim) = -GetSecondDerivativeOfHeightWrt(dim, deriv.front(), x, y);
105  }
106 
107  n(Z) = basis_requested? 1.0 : 0.0;
108 
109  return n;
110 }
111 
113 HeightMap::GetTangent1 (double x, double y, const DimDerivs& deriv) const
114 {
115  Vector3d tx;
116 
117  bool basis_requested = deriv.empty();
118 
119  tx(X) = basis_requested? 1.0 : 0.0;
120  tx(Y) = 0.0;
121  tx(Z) = basis_requested? GetDerivativeOfHeightWrt(X_, x, y)
122  : GetSecondDerivativeOfHeightWrt(X_, deriv.front(), x, y);
123 
124  return tx;
125 }
126 
128 HeightMap::GetTangent2 (double x, double y, const DimDerivs& deriv) const
129 {
130  Vector3d ty;
131 
132  bool basis_requested = deriv.empty();
133 
134  ty(X) = 0.0;
135  ty(Y) = basis_requested? 1.0 : 0.0;
136  ty(Z) = basis_requested? GetDerivativeOfHeightWrt(Y_, x,y)
137  : GetSecondDerivativeOfHeightWrt(Y_, deriv.front(), x, y);
138  return ty;
139 }
140 
143  const Vector3d& v, int idx) const
144 {
145  // see notebook or
146  // http://blog.mmacklin.com/2012/05/
147  return 1/v.squaredNorm()*(v.norm() * Vector3d::Unit(idx) - v(idx)*v.normalized());
148 }
149 
150 double
152  double x, double y) const
153 {
154  if (dim1 == X_) {
155  if (dim2 == X_) return GetHeightDerivWrtXX(x,y);
156  if (dim2 == Y_) return GetHeightDerivWrtXY(x,y);
157  } else {
158  if (dim2 == X_) return GetHeightDerivWrtYX(x,y);
159  if (dim2 == Y_) return GetHeightDerivWrtYY(x,y);
160  }
161 
162  assert(false); // second derivative not specified.
163 }
164 
165 } /* 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:69
virtual double GetHeightDerivWrtY(double x, double y) const
Definition: height_map.h:164
virtual double GetHeightDerivWrtYY(double x, double y) const
Definition: height_map.h:170
TerrainID
Terrains IDs corresponding for factory method.
Definition: height_map.h:79
Vector3d GetDerivativeOfNormalizedVectorWrtNonNormalizedIndex(const Vector3d &non_normalized, int index) const
Definition: height_map.cc:142
Vector3d GetTangent1(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:113
virtual double GetHeightDerivWrtYX(double x, double y) const
Definition: height_map.h:169
Vector3d GetNormalizedBasis(Direction direction, double x, double y) const
Returns either the vector normal or tangent to the terrain patch.
Definition: height_map.cc:63
static HeightMap::Ptr MakeTerrain(TerrainID type)
Definition: height_map.cc:38
double GetSecondDerivativeOfHeightWrt(Dim2D dim1, Dim2D dim2, double x, double y) const
Definition: height_map.cc:151
Eigen::Vector3d Vector3d
Definition: height_map.h:74
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:81
virtual double GetHeightDerivWrtX(double x, double y) const
Definition: height_map.h:163
virtual double GetHeightDerivWrtXX(double x, double y) const
Definition: height_map.h:167
virtual double GetHeightDerivWrtXY(double x, double y) const
Definition: height_map.h:168
std::vector< Dim2D > DimDerivs
dimensional derivatives
Definition: height_map.h:139
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:53
Vector3d GetTangent2(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:128
std::shared_ptr< HeightMap > Ptr
Definition: height_map.h:73
Vector3d GetNormal(double x, double y, const DimDerivs &={}) const
Definition: height_map.cc:94


towr
Author(s): Alexander W. Winkler
autogenerated on Fri Apr 2 2021 02:14:16