Skip to content

Commit

Permalink
[SymForce] Move ComputeKeysToOptimize to factor.h
Browse files Browse the repository at this point in the history
I want to reuse this function for the dense linearizer, so I need it in
some neutral ground outside of `linearizer.h`.

As an aside, I think it makes more sense for this function to live in
`factor.h` regardless, so I'm moving it there.

Topic: factor_out_common_linearizer_code
GitOrigin-RevId: 7af7b60c5f4db2bc71160b5c2e5b09dc6f3b346c
  • Loading branch information
bradley-solliday-skydio authored and aaron-skydio committed Mar 31, 2023
1 parent f03e353 commit 4f70119
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
33 changes: 33 additions & 0 deletions symforce/opt/factor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include <ostream>
#include <unordered_set>

#include <Eigen/Sparse>

Expand Down Expand Up @@ -325,6 +326,38 @@ std::ostream& operator<<(std::ostream& os, const sym::linearized_dense_factorf_t
std::ostream& operator<<(std::ostream& os, const sym::linearized_sparse_factor_t& factor);
std::ostream& operator<<(std::ostream& os, const sym::linearized_sparse_factorf_t& factor);

/**
* Compute the combined set of keys to optimize from the given factors. Order using the given
* comparison function.
*/
template <typename Scalar, typename Compare>
std::vector<Key> ComputeKeysToOptimize(const std::vector<Factor<Scalar>>& factors,
Compare key_compare) {
// Some thoughts on efficiency at
// https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector

// Aggregate uniques
std::unordered_set<Key> key_set;
for (const Factor<Scalar>& factor : factors) {
key_set.insert(factor.OptimizedKeys().begin(), factor.OptimizedKeys().end());
}

// Copy to vector
std::vector<Key> keys;
keys.insert(keys.end(), key_set.begin(), key_set.end());

// Order
std::sort(keys.begin(), keys.end(), key_compare);

return keys;
}

// If no comparator is specified, use sym::Key::LexicalLessThan.
template <typename Scalar>
std::vector<Key> ComputeKeysToOptimize(const std::vector<Factor<Scalar>>& factors) {
return ComputeKeysToOptimize(factors, &sym::Key::LexicalLessThan);
}

} // namespace sym

// Template method implementations
Expand Down
34 changes: 0 additions & 34 deletions symforce/opt/linearizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#pragma once

#include <unordered_set>

#include <Eigen/Sparse>

#include <lcmtypes/sym/linearization_dense_factor_helper_t.hpp>
Expand Down Expand Up @@ -175,38 +173,6 @@ Linearization<Scalar> Linearize(const std::vector<Factor<Scalar>>& factors,
return linearization;
}

/**
* Compute the combined set of keys to optimize from the given factors. Order using the given
* comparison function.
*/
template <typename Scalar, typename Compare>
std::vector<Key> ComputeKeysToOptimize(const std::vector<Factor<Scalar>>& factors,
Compare key_compare) {
// Some thoughts on efficiency at
// https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector

// Aggregate uniques
std::unordered_set<Key> key_set;
for (const Factor<Scalar>& factor : factors) {
key_set.insert(factor.OptimizedKeys().begin(), factor.OptimizedKeys().end());
}

// Copy to vector
std::vector<Key> keys;
keys.insert(keys.end(), key_set.begin(), key_set.end());

// Order
std::sort(keys.begin(), keys.end(), key_compare);

return keys;
}

// If no comparator is specified, use sym::Key::LexicalLessThan.
template <typename Scalar>
std::vector<Key> ComputeKeysToOptimize(const std::vector<Factor<Scalar>>& factors) {
return ComputeKeysToOptimize(factors, &sym::Key::LexicalLessThan);
}

} // namespace sym

#include "./linearizer.tcc"
Expand Down
1 change: 1 addition & 0 deletions symforce/opt/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <sym/util/epsilon.h>

#include "./factor.h"
#include "./levenberg_marquardt_solver.h"
#include "./linearizer.h"
#include "./optimization_stats.h"
Expand Down

0 comments on commit 4f70119

Please sign in to comment.