Remove unused ExpressionTypes

- Remove TERNARY (replaced by function call)
- Combine PARAMETER and RUNTIME_CONSTANT into INPUT_ASSIGNMENT

Change-Id: Id35fb45da08a9f80c1ac0dab7008deb0b3922835
diff --git a/include/ceres/internal/expression.h b/include/ceres/internal/expression.h
index 5ab2d62..b9e2e39 100644
--- a/include/ceres/internal/expression.h
+++ b/include/ceres/internal/expression.h
@@ -183,15 +183,15 @@
   // v_0 = 3.1415;
   COMPILE_TIME_CONSTANT,
 
-  // For example a local member of the cost-functor.
+  // Assignment from a user-variable to a generated variable that can be used by
+  // other expressions. This is used for local variables of cost functors and
+  // parameters of a functions.
   // v_0 = _observed_point_x;
-  RUNTIME_CONSTANT,
+  // v_0 = parameters[0][0];
+  INPUT_ASSIGNMENT,
 
-  // Input parameter
-  // v_0 = parameters[1][5];
-  PARAMETER,
-
-  // Output Variable Assignemnt
+  // Assignment from a generated variable to a user-variable. Used to store the
+  // output of a generated cost functor.
   // residual[0] = v_51;
   OUTPUT_ASSIGNMENT,
 
@@ -223,11 +223,6 @@
   // v_5 = f(v_0,v_1,...)
   FUNCTION_CALL,
 
-  // The ternary ?-operator. Separated from the general function call for easier
-  // access.
-  // v_3 = ternary(v_0,v_1,v_2);
-  TERNARY,
-
   // Conditional control expressions if/else/endif.
   // These are special expressions, because they don't define a new variable.
   IF,
@@ -257,8 +252,7 @@
   // These functions create the corresponding expression, add them to an
   // internal vector and return a reference to them.
   static ExpressionId CreateCompileTimeConstant(double v);
-  static ExpressionId CreateRuntimeConstant(const std::string& name);
-  static ExpressionId CreateParameter(const std::string& name);
+  static ExpressionId CreateInputAssignment(const std::string& name);
   static ExpressionId CreateOutputAssignment(ExpressionId v,
                                              const std::string& name);
   static ExpressionId CreateAssignment(ExpressionId dst, ExpressionId src);
@@ -273,9 +267,6 @@
   static ExpressionId CreateLogicalNegation(ExpressionId v);
   static ExpressionId CreateFunctionCall(
       const std::string& name, const std::vector<ExpressionId>& params);
-  static ExpressionId CreateTernary(ExpressionId condition,
-                                    ExpressionId if_true,
-                                    ExpressionId if_false);
 
   // Conditional control expressions are inserted into the graph but can't be
   // referenced by other expressions. Therefore they don't return an
diff --git a/include/ceres/internal/expression_ref.h b/include/ceres/internal/expression_ref.h
index e0386f0..3d376ee 100644
--- a/include/ceres/internal/expression_ref.h
+++ b/include/ceres/internal/expression_ref.h
@@ -56,6 +56,11 @@
   // must work for T = Jet<ExpressionRef>.
   ExpressionRef(double compile_time_constant);
 
+  // By adding this constructor (which always throws an error) we can detect
+  // invalid usage of ExpressionRef. ExpressionRef can only be created from
+  // constexpr doubles.
+  ExpressionRef(double& test);
+
   // Create an ASSIGNMENT expression from other to this.
   //
   // For example:
@@ -182,21 +187,21 @@
 // This struct is used to mark numbers which are constant over
 // multiple invocations but can differ between instances.
 template <typename T>
-struct RuntimeConstant {
+struct InputAssignment {
   using ReturnType = T;
   static inline ReturnType Get(double v, const char* /* unused */) { return v; }
 };
 
 template <>
-struct RuntimeConstant<ExpressionRef> {
+struct InputAssignment<ExpressionRef> {
   using ReturnType = ExpressionRef;
   static inline ReturnType Get(double /* unused */, const char* name) {
-    return ExpressionRef::Create(Expression::CreateRuntimeConstant(name));
+    return ExpressionRef::Create(Expression::CreateInputAssignment(name));
   }
 };
 
 template <typename G, int N>
-struct RuntimeConstant<Jet<G, N>> {
+struct InputAssignment<Jet<G, N>> {
   using ReturnType = Jet<G, N>;
   static inline Jet<G, N> Get(double v, const char* /* unused */) {
     return Jet<G, N>(v);
@@ -204,27 +209,31 @@
 };
 
 template <int N>
-struct RuntimeConstant<Jet<ExpressionRef, N>> {
+struct InputAssignment<Jet<ExpressionRef, N>> {
   using ReturnType = Jet<ExpressionRef, N>;
   static inline ReturnType Get(double /* unused */, const char* name) {
     // Note: The scalar value of v will be thrown away, because we don't need it
     // during code generation.
     return Jet<ExpressionRef, N>(
-        ExpressionRef::Create(Expression::CreateRuntimeConstant(name)));
+        ExpressionRef::Create(Expression::CreateInputAssignment(name)));
   }
 };
 
 template <typename T>
-inline typename RuntimeConstant<T>::ReturnType MakeRuntimeConstant(
+inline typename InputAssignment<T>::ReturnType MakeInputAssignment(
     double v, const char* name) {
-  return RuntimeConstant<T>::Get(v, name);
+  return InputAssignment<T>::Get(v, name);
 }
 
-#define CERES_EXPRESSION_RUNTIME_CONSTANT(_v) \
-  ceres::internal::MakeRuntimeConstant<T>(_v, #_v)
+// This macro should be used for local variables in cost functors. Using local
+// variables directly, will compile their current value into the code.
+// Example:
+//  T x = CERES_LOCAL_VARIABLE(observed_x_);
+#define CERES_LOCAL_VARIABLE(_v) \
+  ceres::internal::MakeInputAssignment<T>(_v, #_v)
 
 inline ExpressionRef MakeParameter(const std::string& name) {
-  return ExpressionRef::Create(Expression::CreateParameter(name));
+  return ExpressionRef::Create(Expression::CreateInputAssignment(name));
 }
 inline ExpressionRef MakeOutput(ExpressionRef v, const std::string& name) {
   return ExpressionRef::Create(Expression::CreateOutputAssignment(v.id, name));
diff --git a/internal/ceres/expression.cc b/internal/ceres/expression.cc
index 4c8dd5c..b3f9106 100644
--- a/internal/ceres/expression.cc
+++ b/internal/ceres/expression.cc
@@ -63,33 +63,12 @@
   return expr.lhs_id_;
 }
 
-ExpressionId Expression::CreateRuntimeConstant(const std::string& name) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::RUNTIME_CONSTANT);
+ExpressionId Expression::CreateInputAssignment(const std::string& name) {
+  auto& expr = MakeArithmeticExpression(ExpressionType::INPUT_ASSIGNMENT);
   expr.name_ = name;
   return expr.lhs_id_;
 }
 
-ExpressionId Expression::CreateParameter(const std::string& name) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::PARAMETER);
-  expr.name_ = name;
-  return expr.lhs_id_;
-}
-
-ExpressionId Expression::CreateAssignment(ExpressionId dst, ExpressionId src) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::ASSIGNMENT, dst);
-
-  expr.arguments_.push_back(src);
-  return expr.lhs_id_;
-}
-
-ExpressionId Expression::CreateUnaryArithmetic(const std::string& op,
-                                               ExpressionId v) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::UNARY_ARITHMETIC);
-  expr.name_ = op;
-  expr.arguments_.push_back(v);
-  return expr.lhs_id_;
-}
-
 ExpressionId Expression::CreateOutputAssignment(ExpressionId v,
                                                 const std::string& name) {
   auto& expr = MakeArithmeticExpression(ExpressionType::OUTPUT_ASSIGNMENT);
@@ -98,21 +77,28 @@
   return expr.lhs_id_;
 }
 
-ExpressionId Expression::CreateFunctionCall(
-    const std::string& name, const std::vector<ExpressionId>& params) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::FUNCTION_CALL);
-  expr.arguments_ = params;
-  expr.name_ = name;
+ExpressionId Expression::CreateAssignment(ExpressionId dst, ExpressionId src) {
+  auto& expr = MakeArithmeticExpression(ExpressionType::ASSIGNMENT, dst);
+
+  expr.arguments_.push_back(src);
   return expr.lhs_id_;
 }
 
-ExpressionId Expression::CreateTernary(ExpressionId condition,
-                                       ExpressionId if_true,
-                                       ExpressionId if_false) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::TERNARY);
-  expr.arguments_.push_back(condition);
-  expr.arguments_.push_back(if_true);
-  expr.arguments_.push_back(if_false);
+ExpressionId Expression::CreateBinaryArithmetic(const std::string& op,
+                                                ExpressionId l,
+                                                ExpressionId r) {
+  auto& expr = MakeArithmeticExpression(ExpressionType::BINARY_ARITHMETIC);
+  expr.name_ = op;
+  expr.arguments_.push_back(l);
+  expr.arguments_.push_back(r);
+  return expr.lhs_id_;
+}
+
+ExpressionId Expression::CreateUnaryArithmetic(const std::string& op,
+                                               ExpressionId v) {
+  auto& expr = MakeArithmeticExpression(ExpressionType::UNARY_ARITHMETIC);
+  expr.name_ = op;
+  expr.arguments_.push_back(v);
   return expr.lhs_id_;
 }
 
@@ -132,13 +118,11 @@
   return expr.lhs_id_;
 }
 
-ExpressionId Expression::CreateBinaryArithmetic(const std::string& op,
-                                                ExpressionId l,
-                                                ExpressionId r) {
-  auto& expr = MakeArithmeticExpression(ExpressionType::BINARY_ARITHMETIC);
-  expr.name_ = op;
-  expr.arguments_.push_back(l);
-  expr.arguments_.push_back(r);
+ExpressionId Expression::CreateFunctionCall(
+    const std::string& name, const std::vector<ExpressionId>& params) {
+  auto& expr = MakeArithmeticExpression(ExpressionType::FUNCTION_CALL);
+  expr.arguments_ = params;
+  expr.name_ = name;
   return expr.lhs_id_;
 }
 
diff --git a/internal/ceres/expression_ref.cc b/internal/ceres/expression_ref.cc
index 7c2bc5d..9aa25d4 100644
--- a/internal/ceres/expression_ref.cc
+++ b/internal/ceres/expression_ref.cc
@@ -44,6 +44,11 @@
   id = Expression::CreateCompileTimeConstant(compile_time_constant);
 }
 
+ExpressionRef::ExpressionRef(double& test) {
+  CHECK(false)
+      << "ExpressionRefs can only be created from compile-time constants.";
+}
+
 ExpressionRef::ExpressionRef(const ExpressionRef& other) { *this = other; }
 
 ExpressionRef& ExpressionRef::operator=(const ExpressionRef& other) {
@@ -125,7 +130,7 @@
 ExpressionRef Ternary(ComparisonExpressionRef c,
                       ExpressionRef a,
                       ExpressionRef b) {
-  return ExpressionRef::Create(Expression::CreateTernary(c.id, a.id, b.id));
+  return MakeFunctionCall("ternary", {c.id, a.id, b.id});
 }
 
 #define CERES_DEFINE_EXPRESSION_COMPARISON_OPERATOR(op)                   \