Skip to content

Instantly share code, notes, and snippets.

@jwnimmer-tri
Created February 10, 2018 16:00
Show Gist options
  • Select an option

  • Save jwnimmer-tri/37b27098f2a28ca34a099bdb68426eda to your computer and use it in GitHub Desktop.

Select an option

Save jwnimmer-tri/37b27098f2a28ca34a099bdb68426eda to your computer and use it in GitHub Desktop.
diff --git a/math/barycentric.cc b/math/barycentric.cc
index d4ba892..92f4fd6 100644
--- a/math/barycentric.cc
+++ b/math/barycentric.cc
@@ -14,19 +14,19 @@ namespace drake {
namespace math {
template <typename T>
-BarycentricMesh<T>::BarycentricMesh(std::unique_ptr<MeshGrid> input_grid)
+BarycentricMesh<T>::BarycentricMesh(MeshGrid input_grid)
: input_grid_(std::move(input_grid)),
- stride_(input_grid_->size()),
+ stride_(input_grid_.size()),
num_interpolants_{1} {
- DRAKE_DEMAND(input_grid_->size() > 0);
+ DRAKE_DEMAND(input_grid_.size() > 0);
for (int i = 0; i < get_num_inputs(); i++) {
// Must define at least one mesh point per dimension.
- DRAKE_DEMAND(!input_grid_->at(i).empty());
+ DRAKE_DEMAND(!input_grid_.at(i).empty());
// Gain one interpolant for every non-singleton dimension
- if (input_grid_->at(i).size() > 1) num_interpolants_++;
+ if (input_grid_.at(i).size() > 1) num_interpolants_++;
- stride_[i] = (i == 0) ? 1 : input_grid_->at(i - 1).size() * stride_[i - 1];
+ stride_[i] = (i == 0) ? 1 : input_grid_.at(i - 1).size() * stride_[i - 1];
}
}
@@ -38,9 +38,9 @@ void BarycentricMesh<T>::get_mesh_point(int index,
// Iterate through the input dimensions, assigning the value and reducing
// the index to be relevant only to the remaining dimensions.
for (int i = 0; i < get_num_inputs(); i++) {
- const auto& coords = input_grid_->at(i);
+ const auto& coords = input_grid_.at(i);
int dim_index = index % coords.size();
- (*point)[i] = *(std::next(input_grid_->at(i).begin(), dim_index));
+ (*point)[i] = *(std::next(input_grid_.at(i).begin(), dim_index));
index /= coords.size(); // intentionally truncate to int.
}
DRAKE_DEMAND(index == 0); // otherwise the index was out of range.
@@ -63,7 +63,7 @@ void BarycentricMesh<T>::EvalBarycentricWeights(
// indices. Set current_index to the "top right" corner index.
int count = 0;
for (int i = 0; i < get_num_inputs(); i++) {
- const auto& coords = input_grid_->at(i);
+ const auto& coords = input_grid_.at(i);
// Skip over singleton dimensions.
if (coords.size() == 1) continue;
diff --git a/math/barycentric.h b/math/barycentric.h
index 5136ace..469f119 100644
--- a/math/barycentric.h
+++ b/math/barycentric.h
@@ -54,13 +54,13 @@ class BarycentricMesh {
typedef std::vector<Coordinates> MeshGrid;
/// Constructs the mesh.
- explicit BarycentricMesh(std::unique_ptr<MeshGrid> input_grid);
+ explicit BarycentricMesh(MeshGrid input_grid);
/// Accessor methods.
- int get_num_inputs() const { return input_grid_->size(); }
+ int get_num_inputs() const { return input_grid_.size(); }
int get_num_mesh_points() const {
int num_mesh_points = 1;
- for (const auto& coords : *input_grid_) {
+ for (const auto& coords : input_grid_) {
num_mesh_points *= coords.size();
}
return num_mesh_points;
@@ -105,9 +105,8 @@ class BarycentricMesh {
EigenPtr<VectorX<T>> output) const;
private:
- const std::unique_ptr<MeshGrid> input_grid_; // Specifies the location of the
- // mesh points in the input
- // space.
+ const MeshGrid input_grid_; // Specifies the location of the mesh points in
+ // the input space.
std::vector<int> stride_; // The number of elements to skip to arrive at the
// next value (per input dimension)
int num_interpolants_{1}; // The number of points used in any interpolation.
diff --git a/math/test/barycentric_test.cc b/math/test/barycentric_test.cc
index b92aaa8..99cbf04 100644
--- a/math/test/barycentric_test.cc
+++ b/math/test/barycentric_test.cc
@@ -24,14 +24,12 @@ GTEST_TEST(BarycentricTest, GetMeshPoints) {
// Create a mesh in 3 (input) dimensions, with the second dimension being a
// singleton.
const int kNumInputs = 3;
- auto grid = std::make_unique<BarycentricMesh<double>::MeshGrid>(kNumInputs);
-
- // Setup the input mesh by specifying the grid one dimension at at time.
- grid->at(0) = {0.0, 1.0};
- grid->at(1) = {2.0};
- grid->at(2) = {3.0, 4.0};
+ BarycentricMesh<double> bary{{
+ {0.0, 1.0}, // BR
+ {2.0}, // BR
+ {3.0, 4.0}, // BR
+ }};
- BarycentricMesh<double> bary(std::move(grid));
EXPECT_EQ(bary.get_num_inputs(), kNumInputs);
EXPECT_EQ(bary.get_num_mesh_points(), 4);
@@ -51,15 +49,11 @@ GTEST_TEST(BarycentricTest, GetMeshPoints) {
GTEST_TEST(BarycentricTest, EvalWeights) {
// Create a mesh in 3 (input) dimensions, with the second dimension being a
// singleton.
- const int kNumInputs = 3;
- auto grid = std::make_unique<BarycentricMesh<double>::MeshGrid>(kNumInputs);
-
- // Setup the input mesh by specifying the grid one dimension at at time.
- grid->at(0) = {0.0, 1.0};
- grid->at(1) = {2.0};
- grid->at(2) = {3.0, 4.0};
-
- BarycentricMesh<double> bary(std::move(grid));
+ BarycentricMesh<double> bary{{
+ {0.0, 1.0}, // BR
+ {2.0}, // BR
+ {3.0, 4.0}, // BR
+ }};
VectorXi indices(3);
VectorXd weights(3);
@@ -101,14 +95,10 @@ GTEST_TEST(BarycentricTest, EvalWeights) {
}
GTEST_TEST(BarycentricTest, EvalTest) {
- const int kNumInputs = 2;
- auto grid = std::make_unique<BarycentricMesh<double>::MeshGrid>(kNumInputs);
-
- // Setup the input mesh by specifying the grid one dimension at at time.
- grid->at(0) = {0.0, 1.0};
- grid->at(1) = {0.0, 1.0};
-
- BarycentricMesh<double> bary(std::move(grid));
+ BarycentricMesh<double> bary{{
+ {0.0, 1.0}, // BR
+ {0.0, 1.0}, // BR
+ }};
MatrixXd mesh = Eigen::RowVector4d{1., 2., 3., 4.};
@@ -144,11 +134,10 @@ GTEST_TEST(BarycentricTest, MultidimensionalOutput) {
const int kNumInputs = 2;
auto grid = std::make_unique<BarycentricMesh<double>::MeshGrid>(kNumInputs);
- // Setup the input mesh by specifying the grid one dimension at at time.
- grid->at(0) = {0.0, 1.0};
- grid->at(1) = {0.0, 1.0};
-
- BarycentricMesh<double> bary(std::move(grid));
+ BarycentricMesh<double> bary{{
+ {0.0, 1.0}, // BR
+ {0.0, 1.0}, // BR
+ }};
const int kNumOutputs = 2;
MatrixXd mesh(kNumOutputs, bary.get_num_mesh_points());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment