cd2a9c26ce3c0c3e003ccbe61b9974b792565fea
1 #ifndef LIB2GEOM_UTILS_HEADER
2 #define LIB2GEOM_UTILS_HEADER
4 /** Various utility functions.
5 *
6 * Copyright 2007 Johan Engelen <goejendaagh@zonnet.nl>
7 * Copyright 2006 Michael G. Sloan <mgsloan@gmail.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 *
32 */
34 #include <cmath>
35 #include <stdexcept>
37 namespace Geom {
40 //#######################################################################
41 // Base exception class, all 2geom exceptions should be derrived from this one.
42 class Exception : public std::exception {
43 public:
44 Exception(const char * message, const char *file, const int line) {
45 msgstr = "Exception thrown: ";
46 msgstr += message;
47 msgstr += " (";
48 msgstr += file;
49 msgstr += ":";
50 msgstr += line;
51 msgstr += ")";
52 };
53 virtual ~Exception() throw() {}; // necessary to destroy the string object!!!
54 virtual const char * what() const throw () { return msgstr.c_str(); }
55 protected:
56 std::string msgstr;
57 };
59 //-----------------------------------------------------------------------
60 // Two main exception classes: LogicalError and RangeError.
61 // Logical errors are 2geom faults/bugs, RangeErrors are 'user' faults.
62 // This way, the 'user' can distinguish between groups of exceptions
63 // ('user' is the coder that uses lib2geom)
64 class LogicalError : public Exception {
65 public:
66 LogicalError(const char * message, const char *file, const int line)
67 : Exception(message, file, line) {};
68 };
69 #define throwLogicalError(message) throw(LogicalError(message, __FILE__, __LINE__))
71 class RangeError : public Exception {
72 public:
73 RangeError(const char * message, const char *file, const int line)
74 : Exception(message, file, line) {};
75 };
76 #define throwRangeError(message) throw(RangeError(message, __FILE__, __LINE__))
78 //-----------------------------------------------------------------------
79 // Special case exceptions. Best used with the defines :)
81 class NotImplemented : public LogicalError {
82 public:
83 NotImplemented(const char *file, const int line)
84 : LogicalError("Method not implemented", file, line) {};
85 };
86 #define throwNotImplemented(i) throw(NotImplemented(__FILE__, __LINE__))
88 class InvariantsViolation : public LogicalError {
89 public:
90 InvariantsViolation(const char *file, const int line)
91 : LogicalError("Invariants violation", file, line) {};
92 };
93 #define throwInvariantsViolation(i) throw(InvariantsViolation(__FILE__, __LINE__))
94 #define assert_invariants(e) ((e) ? (void)0 : throwInvariantsViolation())
96 class NotInvertible : public RangeError {
97 public:
98 NotInvertible(const char *file, const int line)
99 : RangeError("Function does not have a unique inverse", file, line) {};
100 };
101 #define throwNotInvertible(i) throw(NotInvertible(__FILE__, __LINE__))
103 //#######################################################################
105 // proper logical xor
106 inline bool logical_xor (bool a, bool b) { return (a || b) && !(a && b); }
108 /** Sign function - indicates the sign of a numeric type. -1 indicates negative, 1 indicates
109 * positive, and 0 indicates, well, 0. Mathsy people will know this is basically the derivative
110 * of abs, except for the fact that it is defined on 0.
111 */
112 template <class T> inline int sgn(const T& x) {return (x < 0 ? -1 : (x > 0 ? 1 : 0) );}
114 template <class T> inline T sqr(const T& x) {return x * x;}
115 template <class T> inline T cube(const T& x) {return x * x * x;}
117 /** Between function - returns true if a number x is within a range. The values delimiting the
118 * range and the number must have the same type.
119 */
120 template <class T> inline const T& between (const T& min, const T& max, const T& x)
121 { return min < x && max > x; }
123 /** Returns x rounded to the nearest integer. It is unspecified what happens
124 * if x is half way between two integers: we may in future use rint/round
125 * on platforms that have them.
126 */
127 inline double round(double const x) { return std::floor(x + .5); }
129 /** Returns x rounded to the nearest \a places decimal places.
131 Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
132 half way between two rounded numbers.
134 Note: places is the number of decimal places without using scientific (e) notation, not the
135 number of significant figures. This function may not be suitable for values of x whose
136 magnitude is so far from 1 that one would want to use scientific (e) notation.
138 places may be negative: e.g. places = -2 means rounding to a multiple of .01
139 **/
140 inline double decimal_round(double const x, int const places) {
141 //TODO: possibly implement with modulus instead?
142 double const multiplier = std::pow(10.0, places);
143 return round( x * multiplier ) / multiplier;
144 }
146 }
148 #endif