diff --git a/include/ceres/internal/integer_sequence.h b/include/ceres/internal/integer_sequence.h new file mode 100644 index 000000000..3936b92b0 --- /dev/null +++ b/include/ceres/internal/integer_sequence.h @@ -0,0 +1,106 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2018 Google Inc. All rights reserved. +// http://ceres-solver.org/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: jodebo_beck@gmx.de (Johannes Beck) +// +// This class mimics std::integer_sequence. That is the reason to follow the +// naming convention of the stl and not the google one. Once Ceres switches +// to c++ 14 this class can be removed. + +#ifndef CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ +#define CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ + +#if __cplusplus >= 201402L +// We have at least c++ 14 support. Use integer_sequence from the standard. +// Sometimes the STL implementation uses a compiler intrinsic to generate +// the sequences which will speed up compilation. +#include + +namespace ceres { +namespace internal { +template +using integer_sequence = std::integer_sequence; + +template +using make_integer_sequence = std::make_integer_sequence; + +} // namespace internal +} // namespace ceres +#else + +namespace ceres { +namespace internal { + +template +struct integer_sequence { + using value_type = T; +}; + +// Implementation of make_integer_sequence. +// +// Recursively instantiate make_integer_sequence_impl until Ns +// contains the sequence 0, 1, ..., Total-1. +// +// Example for Total = 4: +// T CurIdx, Total, Ns... +// make_integer_sequence_impl +// make_integer_sequence_impl +// make_integer_sequence_impl +// make_integer_sequence_impl +// make_integer_sequence_impl +// ^^^^^^^^^^ +// resulting sequence. +// +// The implemented algorithm has linear complexity for simplicity. A O(log(N)) +// implementation can be found e.g. here: +// https://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence +template +struct make_integer_sequence_impl { + using type = typename make_integer_sequence_impl::type; +}; + +// End of 'recursion' when CurIdx reaches Total. All indices 0, 1, ..., N-1 are +// contained in Ns. The final integer_sequence is created here. +template +struct make_integer_sequence_impl { + using type = integer_sequence; +}; + +// A helper alias template to simplify creation of integer_sequence with 0, 1, +// ..., N-1 as Ns: +template +using make_integer_sequence = + typename make_integer_sequence_impl::type; + +} // namespace internal +} // namespace ceres + +#endif + +#endif // CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_H_ diff --git a/include/ceres/internal/integer_sequence_algorithm.h b/include/ceres/internal/integer_sequence_algorithm.h new file mode 100644 index 000000000..bdeab9314 --- /dev/null +++ b/include/ceres/internal/integer_sequence_algorithm.h @@ -0,0 +1,165 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2018 Google Inc. All rights reserved. +// http://ceres-solver.org/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: jodebo_beck@gmx.de (Johannes Beck) +// +// Algorithms to be used together with integer_sequence, like computing the sum +// or the exclusive scan (sometimes called exclusive prefix sum) at compile +// time. + +#ifndef CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_ +#define CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_ + +#include "integer_sequence.h" + +namespace ceres { +namespace internal { + +// Implementation of calculating the sum of an integer sequence. +// Recursively instantiate SumImpl and calculate the sum of the N first +// numbers. This reduces the number of instantiations and speeds up +// compilation. +// +// Examples: +// 1) integer_sequence: +// Value = 5 +// +// 2) integer_sequence: +// Value = 4 + 2 + SumImpl>::Value +// Value = 4 + 2 + 0 +// +// 3) integer_sequence: +// Value = 2 + 1 + SumImpl>::Value +// Value = 2 + 1 + 4 +template +struct SumImpl; + +// Strip of and sum the first number. +template +struct SumImpl> { + static constexpr T Value = N + SumImpl>::Value; +}; + +// Strip of and sum the first two numbers. +template +struct SumImpl> { + static constexpr T Value = + N1 + N2 + SumImpl>::Value; +}; + +// Strip of and sum the first four numbers. +template +struct SumImpl> { + static constexpr T Value = + N1 + N2 + N3 + N4 + SumImpl>::Value; +}; + +// Only one number is left. 'Value' is just that number ('recursion' ends). +template +struct SumImpl> { + static constexpr T Value = N; +}; + +// No number is left. 'Value' is the identity element (for sum this is zero). +template +struct SumImpl> { + static constexpr T Value = T(0); +}; + +// Calculate the sum of an integer sequence. The resulting sum will be stored in +// 'Value'. +template +class Sum { + using T = typename Seq::value_type; + + public: + static constexpr T Value = SumImpl::Value; +}; + +// Implementation of calculating an exclusive scan (exclusive prefix sum) of an +// integer sequence. Exclusive means that the i-th input element is not included +// in the i-th sum. Calculating the exclusive scan for an input array I results +// in the following output R: +// +// R[0] = 0 +// R[1] = I[0]; +// R[2] = I[0] + I[1]; +// R[3] = I[0] + I[1] + I[2]; +// ... +// +// In C++17 std::exclusive_scan does the same operation at runtime (but +// cannot be used to calculate the prefix sum at compile time). See +// https://en.cppreference.com/w/cpp/algorithm/exclusive_scan for a more +// detailed description. +// +// Example for integer_sequence (seq := integer_sequence): +// T , Sum, Ns... , Rs... +// ExclusiveScanImpl, seq> +// ExclusiveScanImpl, seq> +// ExclusiveScanImpl, seq> +// ExclusiveScanImpl, seq> +// ^^^^^^^^^^^^^^^^^ +// resulting sequence +template +struct ExclusiveScanImpl; + +template +struct ExclusiveScanImpl, + integer_sequence> { + using Type = + typename ExclusiveScanImpl, + integer_sequence>::Type; +}; + +// End of 'recursion'. The resulting type is SeqOut. +template +struct ExclusiveScanImpl, SeqOut> { + using Type = SeqOut; +}; + +// Calculates the exclusive scan of the specified integer sequence. The last +// element (the total) is not included in the resulting sequence so they have +// same length. This means the exclusive scan of integer_sequence +// will be integer_sequence. +template +class ExclusiveScanT { + using T = typename Seq::value_type; + + public: + using Type = + typename ExclusiveScanImpl>::Type; +}; + +// Helper to use exclusive scan without typename. +template +using ExclusiveScan = typename ExclusiveScanT::Type; + +} // namespace internal +} // namespace ceres + +#endif // CERES_PUBLIC_INTERNAL_INTEGER_SEQUENCE_ALGORITHM_H_ diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index e90562ac7..fbce4189c 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt @@ -431,6 +431,8 @@ if (BUILD_TESTING AND GFLAGS) ceres_test(implicit_schur_complement) ceres_test(inner_product_computer) ceres_test(invert_psd_matrix) + ceres_test(integer_sequence) + ceres_test(integer_sequence_algorithm) ceres_test(is_close) ceres_test(iterative_refiner) ceres_test(iterative_schur_complement_solver) diff --git a/internal/ceres/integer_sequence_algorithm_test.cc b/internal/ceres/integer_sequence_algorithm_test.cc new file mode 100644 index 000000000..a6c85d032 --- /dev/null +++ b/internal/ceres/integer_sequence_algorithm_test.cc @@ -0,0 +1,71 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2018 Google Inc. All rights reserved. +// http://ceres-solver.org/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: jodebo_beck@gmx.de (Johannes Beck) + +#include "ceres/internal/integer_sequence_algorithm.h" + +#include + +namespace ceres { +namespace internal { + +// Unit tests for summation of integer sequence. +static_assert(Sum>::Value == 0, + "Unit test of summing up an integer sequence failed."); +static_assert(Sum>::Value == 2, + "Unit test of summing up an integer sequence failed."); +static_assert(Sum>::Value == 5, + "Unit test of summing up an integer sequence failed."); +static_assert(Sum>::Value == 15, + "Unit test of summing up an integer sequence failed."); +static_assert(Sum>::Value == 19, + "Unit test of summing up an integer sequence failed."); +static_assert(Sum>::Value == 20, + "Unit test of summing up an integer sequence failed."); + +// Unit tests for exclusive scan of integer sequence. +static_assert(std::is_same>, + integer_sequence>::value, + "Unit test of calculating the exclusive scan of an integer " + "sequence failed."); +static_assert(std::is_same>, + integer_sequence>::value, + "Unit test of calculating the exclusive scan of an integer " + "sequence failed."); +static_assert(std::is_same>, + integer_sequence>::value, + "Unit test of calculating the exclusive scan of an integer " + "sequence failed."); +static_assert(std::is_same>, + integer_sequence>::value, + "Unit test of calculating the exclusive scan of an integer " + "sequence failed."); + +} // namespace internal +} // namespace ceres diff --git a/internal/ceres/integer_sequence_test.cc b/internal/ceres/integer_sequence_test.cc new file mode 100644 index 000000000..ab3559af7 --- /dev/null +++ b/internal/ceres/integer_sequence_test.cc @@ -0,0 +1,58 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2018 Google Inc. All rights reserved. +// http://ceres-solver.org/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: jodebo_beck@gmx.de (Johannes Beck) + +#include "ceres/internal/integer_sequence.h" + +#include + +namespace ceres { +namespace internal { + +// Unit test for integer_sequence<...>::value_type +static_assert(std::is_same::value_type, + unsigned int>::value, + "Unit test of integer sequence value type failed."); + +// Unit tests for make_integer_sequence +static_assert( + std::is_same, integer_sequence>::value, + "Unit test of make integer sequence failed."); +static_assert(std::is_same, + integer_sequence>::value, + "Unit test of make integer sequence failed."); +static_assert(std::is_same, + integer_sequence>::value, + "Unit test of make integer sequence failed."); +static_assert(std::is_same, + integer_sequence>::value, + "Unit test of make integer sequence failed."); + +} // namespace internal +} // namespace ceres