utils.h revision 63267518b4ce196caab66ef8cbdcfc0921206b3d
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen#ifndef LIB2GEOM_UTILS_HEADER
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen#define LIB2GEOM_UTILS_HEADER
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** Various utility functions.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
feb44ad276e996ca244e071f936c61b009bd73f4johanengelen * Copyright 2007 Johan Engelen <goejendaagh@zonnet.nl>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright 2006 Michael G. Sloan <mgsloan@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This library is free software; you can redistribute it and/or
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * modify it either under the terms of the GNU Lesser General Public
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * License version 2.1 as published by the Free Software Foundation
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * (the "LGPL") or, at your option, under the terms of the Mozilla
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Public License Version 1.1 (the "MPL"). If you do not alter this
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * notice, a recipient may use your version of this file under either
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the MPL or the LGPL.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the LGPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-LGPL-2.1; if not, write to the Free Software
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the MPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-MPL-1.1
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * The contents of this file are subject to the Mozilla Public License
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Version 1.1 (the "License"); you may not use this file except in
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * compliance with the License. You may obtain a copy of the License at
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * http://www.mozilla.org/MPL/
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * OF ANY KIND, either express or implied. See the LGPL or the MPL for
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the specific language governing rights and limitations.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <cmath>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
c3bc76d351bed6e320f6dbf2242012f026659287johanengelennamespace Geom {
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen// proper logical xor
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengeleninline bool logical_xor (bool a, bool b) { return (a || b) && !(a && b); }
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** Sign function - indicates the sign of a numeric type. -1 indicates negative, 1 indicates
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * positive, and 0 indicates, well, 0. Mathsy people will know this is basically the derivative
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * of abs, except for the fact that it is defined on 0.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T> inline int sgn(const T& x) {return (x < 0 ? -1 : (x > 0 ? 1 : 0) );}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T> inline T sqr(const T& x) {return x * x;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T> inline T cube(const T& x) {return x * x * x;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** Between function - returns true if a number x is within a range. The values delimiting the
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * range and the number must have the same type.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T> inline const T& between (const T& min, const T& max, const T& x)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen { return min < x && max > x; }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** Returns x rounded to the nearest integer. It is unspecified what happens
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * if x is half way between two integers: we may in future use rint/round
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * on platforms that have them.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline double round(double const x) { return std::floor(x + .5); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** Returns x rounded to the nearest \a places decimal places.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen half way between two rounded numbers.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Note: places is the number of decimal places without using scientific (e) notation, not the
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen number of significant figures. This function may not be suitable for values of x whose
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen magnitude is so far from 1 that one would want to use scientific (e) notation.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen places may be negative: e.g. places = -2 means rounding to a multiple of .01
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen**/
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline double decimal_round(double const x, int const places) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //TODO: possibly implement with modulus instead?
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double const multiplier = std::pow(10.0, places);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return round( x * multiplier ) / multiplier;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen}
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#endif
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen/*
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen Local Variables:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen mode:c++
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-style:"stroustrup"
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen indent-tabs-mode:nil
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen fill-column:99
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen End:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen*/
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :