Add parameter dims for variadic sized cost function

The class parameter dims is a helper class that holds the parameter
dimensions. The parameter dimensions are either dynamic or the
sizes are known at compile time. It is used to pass parameter block
dimensions around (e.g. between functions or classes).

Also the dimensions of the parameter blocks are checked at compile
time and must be greater than zero. This means trailing zero parameter
blocks will result in a compile error.

Change-Id: I4decf2f09e63fdb0fd652022b2be2a1cfd9c4478
diff --git a/include/ceres/internal/parameter_dims.h b/include/ceres/internal/parameter_dims.h
new file mode 100644
index 0000000..79d0680
--- /dev/null
+++ b/include/ceres/internal/parameter_dims.h
@@ -0,0 +1,124 @@
+// 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)
+
+#ifndef CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
+#define CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
+
+#include <array>
+
+#include "ceres/internal/integer_sequence.h"
+#include "ceres/internal/integer_sequence_algorithm.h"
+
+namespace ceres {
+namespace internal {
+
+// Checks, whether the given parameter block sizes are valid. Valid means every
+// dimension is bigger than zero.
+template <int N, int... Ts>
+constexpr bool IsValidParameterDimensionSequence(
+    integer_sequence<int, N, Ts...>) {
+  return (N <= 0) ? false
+                  : IsValidParameterDimensionSequence(
+                        integer_sequence<int, Ts...>());
+}
+
+constexpr bool IsValidParameterDimensionSequence(integer_sequence<int>) {
+  return true;
+}
+
+// Helper class that represents the parameter dimensions. The parameter
+// dimensions are either dynamic or the sizes are known at compile time. It is
+// used to pass parameter block dimensions around (e.g. between functions or
+// classes).
+//
+// As an example if one have three parameter blocks with dimensions (2, 4, 1),
+// one would use 'StaticParameterDims<2, 4, 1>' which is a synonym for
+// 'ParameterDims<false, 2, 4, 1>'.
+// For dynamic parameter dims, one would just use 'DynamicParameterDims', which
+// is a synonym for 'ParameterDims<true>'.
+template <bool IsDynamic, int... Ns>
+class ParameterDims {
+ public:
+  using Parameters = integer_sequence<int, Ns...>;
+
+  // The parameter dimensions are only valid if all parameter block dimensions
+  // are greater than zero.
+  static constexpr bool kIsValid =
+      IsValidParameterDimensionSequence(Parameters());
+  static_assert(kIsValid,
+                "Invalid parameter block dimension detected. Each parameter "
+                "block dimension must be bigger than zero.");
+
+  static constexpr bool kIsDynamic = IsDynamic;
+  static constexpr int kNumParameterBlocks = sizeof...(Ns);
+  static_assert(kIsDynamic || kNumParameterBlocks > 0,
+                "At least one parameter block must be specified.");
+
+  static constexpr int kNumParameters =
+      Sum<integer_sequence<int, Ns...>>::Value;
+
+  static constexpr int GetDim(int dim) { return params_[dim]; }
+
+  // If one has all parameters packed into a single array this function unpacks
+  // the parameters.
+  template <typename T>
+  static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
+      T* ptr) {
+    using Offsets = ExclusiveScan<Parameters>;
+    return GetUnpackedParameters(ptr, Offsets());
+  }
+
+ private:
+  template <typename T, int... Indices>
+  static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
+      T* ptr, integer_sequence<int, Indices...>) {
+    return std::array<T*, kNumParameterBlocks>{{ptr + Indices...}};
+  }
+
+  static constexpr std::array<int, kNumParameterBlocks> params_{Ns...};
+};
+
+// Even static constexpr member variables needs to be defined (not only
+// declared). As the ParameterDims class is tempalted this definition must
+// be in the header file.
+template <bool IsDynamic, int... Ns>
+constexpr std::array<int, ParameterDims<IsDynamic, Ns...>::kNumParameterBlocks>
+    ParameterDims<IsDynamic, Ns...>::params_;
+
+// Using declarations for static and dynamic parameter dims. This makes client
+// code easier to read.
+template <int... Ns>
+using StaticParameterDims = ParameterDims<false, Ns...>;
+using DynamicParameterDims = ParameterDims<true>;
+
+}  // namespace internal
+}  // namespace ceres
+
+#endif  // CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt
index fbce418..5bbe2bd 100644
--- a/internal/ceres/CMakeLists.txt
+++ b/internal/ceres/CMakeLists.txt
@@ -450,6 +450,7 @@
   ceres_test(parallel_utils)
   ceres_test(parameter_block)
   ceres_test(parameter_block_ordering)
+  ceres_test(parameter_dims)
   ceres_test(partitioned_matrix_view)
   ceres_test(polynomial)
   ceres_test(problem)
diff --git a/internal/ceres/parameter_dims_test.cc b/internal/ceres/parameter_dims_test.cc
new file mode 100644
index 0000000..9a95cf5
--- /dev/null
+++ b/internal/ceres/parameter_dims_test.cc
@@ -0,0 +1,104 @@
+//
+// * 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/parameter_dims.h"
+
+#include <gtest/gtest.h>
+#include <type_traits>
+
+namespace ceres {
+namespace internal {
+
+// Is valid parameter dims unit test
+static_assert(IsValidParameterDimensionSequence(integer_sequence<int>()) ==
+                  true,
+              "Unit test of is valid parameter dimension sequence failed.");
+static_assert(
+    IsValidParameterDimensionSequence(integer_sequence<int, 2, 1>()) == true,
+    "Unit test of is valid parameter dimension sequence failed.");
+static_assert(
+    IsValidParameterDimensionSequence(integer_sequence<int, 0, 1>()) == false,
+    "Unit test of is valid parameter dimension sequence failed.");
+static_assert(
+    IsValidParameterDimensionSequence(integer_sequence<int, 3, 0>()) == false,
+    "Unit test of is valid parameter dimension sequence failed.");
+
+// Static parameter dims unit test
+static_assert(
+    std::is_same<StaticParameterDims<4, 2, 1>::Parameters,
+                 integer_sequence<int, 4, 2, 1>>::value == true,
+    "Unit test of type 'parameters' for static parameter dims failed.");
+
+static_assert(StaticParameterDims<4, 2, 1>::kIsValid == true,
+              "Unit test of is valid for static parameter dims failed.");
+static_assert(StaticParameterDims<4, 2, 1>::kIsDynamic == false,
+              "Unit test of is dynamic for static parameter dims failed.");
+static_assert(StaticParameterDims<4, 2, 1>::kNumParameterBlocks == 3,
+              "Unit test of number of parameter blocks for static parameter "
+              "dims failed.");
+static_assert(
+    StaticParameterDims<4, 2, 1>::kNumParameters == 7,
+    "Unit test of number of parameters for static parameter dims failed.");
+
+// Dynamic parameter dims unit test
+static_assert(DynamicParameterDims::kIsValid == true,
+              "Unit test of is valid for dynamic parameter dims failed.");
+static_assert(DynamicParameterDims::kIsDynamic == true,
+              "Unit test of is dynamic for dynamic parameter dims failed.");
+static_assert(DynamicParameterDims::kNumParameterBlocks == 0,
+              "Unit test of number if parameter blocks for dynamic parameter "
+              "dims failed.");
+static_assert(
+    DynamicParameterDims::kNumParameters == 0,
+    "Unit test of number of parameters for dynamic parameter dims failed.");
+
+TEST(ParameterDims, GetDims) {
+  constexpr int N0 = 3;
+  constexpr int N1 = 4;
+  constexpr int N2 = 2;
+
+  StaticParameterDims<N0, N1, N2> params;
+  EXPECT_EQ(N0, params.GetDim(0));
+  EXPECT_EQ(N1, params.GetDim(1));
+  EXPECT_EQ(N2, params.GetDim(2));
+}
+
+TEST(ParameterDims, GetUnpackedParameters) {
+  constexpr int N0 = 3;
+  constexpr int N1 = 4;
+  constexpr int N2 = 2;
+
+  using Params = StaticParameterDims<N0, N1, N2>;
+
+  std::array<double, Params::kNumParameters> packed_parameters{};
+  std::array<double*, 3> unpacked_parameters =
+      Params::GetUnpackedParameters(packed_parameters.data());
+
+  EXPECT_EQ(packed_parameters.data(), unpacked_parameters[0]);
+  EXPECT_EQ(packed_parameters.data() + N0, unpacked_parameters[1]);
+  EXPECT_EQ(packed_parameters.data() + N0 + N1, unpacked_parameters[2]);
+}
+
+}  // namespace internal
+}  // namespace ceres