Port Ceres to Windows

This is a preliminary, but full, port of Ceres to Windows.
Currently all tests compile and run, with only system_test
failing to work correctly due to a path issue.

Change-Id: I4152c1588bf51ffd7f4d9401ef9759f5d28c299c
diff --git a/include/ceres/fpclassify.h b/include/ceres/fpclassify.h
index 59424bf..550d4d4 100644
--- a/include/ceres/fpclassify.h
+++ b/include/ceres/fpclassify.h
@@ -37,12 +37,28 @@
 #ifndef CERES_PUBLIC_FPCLASSIFY_H_
 #define CERES_PUBLIC_FPCLASSIFY_H_
 
+#if defined(_MSC_VER)
+#include <float.h>
+#endif
+
 namespace ceres {
 
+#if defined(_MSC_VER)
+inline bool IsFinite  (double x) { return _finite(x);                }
+inline bool IsInfinite(double x) { return !_finite(x) && !_isnan(x); }
+inline bool IsNaN     (double x) { return _isnan(x);                 }
+inline bool IsNormal  (double x) {
+  int classification = _fpclass(x);
+  return classification == _FPCLASS_NN ||
+         classification == _FPCLASS_PN;
+}
+#else
+// TODO(keir): Test the "else" with more platforms.
 inline bool IsFinite  (double x) { return std::isfinite(x); }
 inline bool IsInfinite(double x) { return std::isinf(x);    }
 inline bool IsNaN     (double x) { return std::isnan(x);    }
 inline bool IsNormal  (double x) { return std::isnormal(x); }
+#endif
 
 }  // namespace ceres
 
diff --git a/include/ceres/internal/fixed_array.h b/include/ceres/internal/fixed_array.h
index aa1722c..e11b71b 100644
--- a/include/ceres/internal/fixed_array.h
+++ b/include/ceres/internal/fixed_array.h
@@ -69,6 +69,12 @@
 // Non-POD types will be default-initialized just like regular vectors or
 // arrays.
 
+#if defined(_WIN64)
+   typedef __int64      ssize_t;
+#elif defined(_WIN32)
+   typedef __int32      ssize_t;
+#endif
+
 template <typename T, ssize_t inline_elements = -1>
 class FixedArray {
  public:
@@ -152,13 +158,13 @@
 
   // Allocate some space, not an array of elements of type T, so that we can
   // skip calling the T constructors and destructors for space we never use.
-  ManualConstructor<InnerContainer> inline_space_[kInlineElements] CERES_ALIGN_ATTRIBUTE(16);
+  ManualConstructor<InnerContainer> CERES_ALIGN_ATTRIBUTE(16) inline_space_[kInlineElements];
 };
 
 // Implementation details follow
 
 template <class T, ssize_t S>
-inline FixedArray<T, S>::FixedArray(FixedArray<T, S>::size_type n)
+inline FixedArray<T, S>::FixedArray(typename FixedArray<T, S>::size_type n)
     : size_(n),
       array_((n <= kInlineElements
               ? reinterpret_cast<InnerContainer*>(inline_space_)
diff --git a/include/ceres/internal/macros.h b/include/ceres/internal/macros.h
index 0e84e9c..83ec311 100644
--- a/include/ceres/internal/macros.h
+++ b/include/ceres/internal/macros.h
@@ -83,7 +83,7 @@
 // That gcc wants both of these prototypes seems mysterious. VC, for
 // its part, can't decide which to use (another mystery). Matching of
 // template overloads: the final frontier.
-#ifndef COMPILER_MSVC
+#ifndef _WIN32
 template <typename T, size_t N>
 char (&ArraySizeHelper(const T (&array)[N]))[N];
 #endif
@@ -131,12 +131,13 @@
 //
 // - wan 2005-11-16
 //
-// Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE.
-#if !defined(COMPILER_MSVC) || (defined(_MSC_VER) && _MSC_VER < 1400)
-#define ARRAYSIZE(a) \
+// Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE. However,
+// the definition comes from the over-broad windows.h header that 
+// introduces a macro, ERROR, that conflicts with the logging framework
+// that Ceres uses. Instead, rename ARRAYSIZE to CERES_ARRAYSIZE.
+#define CERES_ARRAYSIZE(a) \
   ((sizeof(a) / sizeof(*(a))) / \
    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
-#endif
 
 // Tell the compiler to warn about unused return values for functions declared
 // with this macro.  The macro should be used on function declarations
diff --git a/include/ceres/jet.h b/include/ceres/jet.h
index 552df65..e94df5d 100644
--- a/include/ceres/jet.h
+++ b/include/ceres/jet.h
@@ -650,6 +650,10 @@
   typedef ceres::Jet<T, N> NonInteger;
   typedef ceres::Jet<T, N> Nested;
 
+  static typename ceres::Jet<T, N> dummy_precision() {
+    return ceres::Jet<T, N>(1e-12);
+  }
+
   enum {
     IsComplex = 0,
     IsInteger = 0,
diff --git a/include/ceres/rotation.h b/include/ceres/rotation.h
index 834ca45..6c0d58e 100644
--- a/include/ceres/rotation.h
+++ b/include/ceres/rotation.h
@@ -327,7 +327,8 @@
 inline void EulerAnglesToRotationMatrix(const T* euler,
                                         const int row_stride,
                                         T* R) {
-  const T degrees_to_radians(M_PI / 180.0);
+  const double kPi = 3.14159265358979323846;
+  const T degrees_to_radians(kPi / 180.0);
 
   const T pitch(euler[0] * degrees_to_radians);
   const T roll(euler[1] * degrees_to_radians);
@@ -509,4 +510,5 @@
 }
 
 }  // namespace ceres
+
 #endif  // CERES_PUBLIC_ROTATION_H_