10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Various Google-specific macros. 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This code is compiled directly on many platforms, including client 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// platforms like Windows, Mac, and embedded systems. Before making 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// any changes here, make sure that you're not breaking any platforms. 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_PUBLIC_INTERNAL_MACROS_H_ 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_PUBLIC_INTERNAL_MACROS_H_ 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <cstddef> // For size_t. 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// A macro to disallow the copy constructor and operator= functions 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This should be used in the private: declarations for a class 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// For disallowing only assign or copy, write the code directly, but declare 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the intend in a comment, for example: 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// void operator=(const TypeName&); // _DISALLOW_ASSIGN 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Note, that most uses of CERES_DISALLOW_ASSIGN and CERES_DISALLOW_COPY 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// are broken semantically, one should either use disallow both or 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// neither. Try to avoid these in new code. 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_DISALLOW_COPY_AND_ASSIGN(TypeName) \ 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong TypeName(const TypeName&); \ 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong void operator=(const TypeName&) 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// A macro to disallow all the implicit constructors, namely the 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// default constructor, copy constructor and operator= functions. 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This should be used in the private: declarations for a class 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// that wants to prevent anyone from instantiating it. This is 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// especially useful for classes containing only static methods. 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong TypeName(); \ 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CERES_DISALLOW_COPY_AND_ASSIGN(TypeName) 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The arraysize(arr) macro returns the # of elements in an array arr. 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The expression is a compile-time constant, and therefore can be 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used in defining new arrays, for example. If you use arraysize on 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// a pointer by mistake, you will get a compile-time error. 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// One caveat is that arraysize() doesn't accept any array of an 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// anonymous type or a type defined inside a function. In these rare 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// cases, you have to use the unsafe ARRAYSIZE() macro below. This is 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// due to a limitation in C++'s template system. The limitation might 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// eventually be removed, but it hasn't happened yet. 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This template function declaration is used in defining arraysize. 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Note that the function doesn't need an implementation, as we only 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// use its type. 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <typename T, size_t N> 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongchar (&ArraySizeHelper(T (&array)[N]))[N]; 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// That gcc wants both of these prototypes seems mysterious. VC, for 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// its part, can't decide which to use (another mystery). Matching of 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// template overloads: the final frontier. 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef _WIN32 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtemplate <typename T, size_t N> 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongchar (&ArraySizeHelper(const T (&array)[N]))[N]; 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif 900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define arraysize(array) (sizeof(ArraySizeHelper(array))) 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARRAYSIZE performs essentially the same calculation as arraysize, 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// but can be used on anonymous types or types defined inside 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// functions. It's less safe than arraysize as it accepts some 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// (although not all) pointers. Therefore, you should use arraysize 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// whenever possible. 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The expression ARRAYSIZE(a) is a compile-time constant of type 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// size_t. 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARRAYSIZE catches a few type errors. If you see a compiler error 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// "warning: division by zero in ..." 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// when using ARRAYSIZE, you are (wrongfully) giving it a pointer. 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// You should only use ARRAYSIZE on statically allocated arrays. 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The following comments are on the implementation details, and can 1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// be ignored by the users. 1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in 1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the array) and sizeof(*(arr)) (the # of bytes in one array 1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// element). If the former is divisible by the latter, perhaps arr is 1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// indeed an array, in which case the division result is the # of 1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// elements in the array. Otherwise, arr cannot possibly be an array, 1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and we generate a compiler error to prevent the code from 1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// compiling. 1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Since the size of bool is implementation-defined, we need to cast 1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final 1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// result has type size_t. 1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This macro is not perfect as it wrongfully accepts certain 1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// pointers, namely where the pointer size is divisible by the pointee 1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// size. Since all our code has to go through a 32-bit compiler, 1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// where a pointer is 4 bytes, this means all pointers to a type whose 1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// size is 3 or greater than 4 will be (righteously) rejected. 1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Kudos to Jorg Brown for this simple and elegant implementation. 1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// - wan 2005-11-16 1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE. However, 1351d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// the definition comes from the over-broad windows.h header that 1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// introduces a macro, ERROR, that conflicts with the logging framework 1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// that Ceres uses. Instead, rename ARRAYSIZE to CERES_ARRAYSIZE. 1381d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#define CERES_ARRAYSIZE(a) \ 1391d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling ((sizeof(a) / sizeof(*(a))) / \ 1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) 1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1421d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// Tell the compiler to warn about unused return values for functions 1431d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// declared with this macro. The macro should be used on function 1441d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling// declarations following the argument list: 1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Sprocket* AllocateSprocket() MUST_USE_RESULT; 1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ 1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong && !defined(COMPILER_ICC) 15079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#define CERES_MUST_USE_RESULT __attribute__ ((warn_unused_result)) 1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#else 15279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#define CERES_MUST_USE_RESULT 1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif 1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Platform independent macros to get aligned memory allocations. 1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// For example 1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// MyFoo my_foo CERES_ALIGN_ATTRIBUTE(16); 1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Gives us an instance of MyFoo which is aligned at a 16 byte 1610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// boundary. 1620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#if defined(_MSC_VER) 1630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_ALIGN_ATTRIBUTE(n) __declspec(align(n)) 1640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_ALIGN_OF(T) __alignof(T) 1650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#elif defined(__GNUC__) 1660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_ALIGN_ATTRIBUTE(n) __attribute__((aligned(n))) 1670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_ALIGN_OF(T) __alignof(T) 1680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif 1690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_PUBLIC_INTERNAL_MACROS_H_ 171