macros.h

Go to the documentation of this file.
00001 // $Id: macros.h 751 2006-03-31 15:43:49Z alex $
00002 // macros.h
00003 //
00004 // Author  : Mike
00005 // Purpose : Defines some useful macros (taken from Aurora)
00006 // Version : 1.0
00007 // Started : 18/08/94
00008 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00009 ================================XARAHEADERSTART===========================
00010  
00011                Xara LX, a vector drawing and manipulation program.
00012                     Copyright (C) 1993-2006 Xara Group Ltd.
00013        Copyright on certain contributions may be held in joint with their
00014               respective authors. See AUTHORS file for details.
00015 
00016 LICENSE TO USE AND MODIFY SOFTWARE
00017 ----------------------------------
00018 
00019 This file is part of Xara LX.
00020 
00021 Xara LX is free software; you can redistribute it and/or modify it
00022 under the terms of the GNU General Public License version 2 as published
00023 by the Free Software Foundation.
00024 
00025 Xara LX and its component source files are distributed in the hope
00026 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00027 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00028 See the GNU General Public License for more details.
00029 
00030 You should have received a copy of the GNU General Public License along
00031 with Xara LX (see the file GPL in the root directory of the
00032 distribution); if not, write to the Free Software Foundation, Inc., 51
00033 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00034 
00035 
00036 ADDITIONAL RIGHTS
00037 -----------------
00038 
00039 Conditional upon your continuing compliance with the GNU General Public
00040 License described above, Xara Group Ltd grants to you certain additional
00041 rights. 
00042 
00043 The additional rights are to use, modify, and distribute the software
00044 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00045 library and any other such library that any version of Xara LX relased
00046 by Xara Group Ltd requires in order to compile and execute, including
00047 the static linking of that library to XaraLX. In the case of the
00048 "CDraw" library, you may satisfy obligation under the GNU General Public
00049 License to provide source code by providing a binary copy of the library
00050 concerned and a copy of the license accompanying it.
00051 
00052 Nothing in this section restricts any of the rights you have under
00053 the GNU General Public License.
00054 
00055 
00056 SCOPE OF LICENSE
00057 ----------------
00058 
00059 This license applies to this program (XaraLX) and its constituent source
00060 files only, and does not necessarily apply to other Xara products which may
00061 in part share the same code base, and are subject to their own licensing
00062 terms.
00063 
00064 This license does not apply to files in the wxXtra directory, which
00065 are built into a separate library, and are subject to the wxWindows
00066 license contained within that directory in the file "WXXTRA-LICENSE".
00067 
00068 This license does not apply to the binary libraries (if any) within
00069 the "libs" directory, which are subject to a separate license contained
00070 within that directory in the file "LIBS-LICENSE".
00071 
00072 
00073 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00074 ----------------------------------------------
00075 
00076 Subject to the terms of the GNU Public License (see above), you are
00077 free to do whatever you like with your modifications. However, you may
00078 (at your option) wish contribute them to Xara's source tree. You can
00079 find details of how to do this at:
00080   http://www.xaraxtreme.org/developers/
00081 
00082 Prior to contributing your modifications, you will need to complete our
00083 contributor agreement. This can be found at:
00084   http://www.xaraxtreme.org/developers/contribute/
00085 
00086 Please note that Xara will not accept modifications which modify any of
00087 the text between the start and end of this header (marked
00088 XARAHEADERSTART and XARAHEADEREND).
00089 
00090 
00091 MARKS
00092 -----
00093 
00094 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00095 designs are registered or unregistered trademarks, design-marks, and/or
00096 service marks of Xara Group Ltd. All rights in these marks are reserved.
00097 
00098 
00099       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00100                         http://www.xara.com/
00101 
00102 =================================XARAHEADEREND============================
00103  */
00104 
00105 #ifndef INC_MACROS
00106 #define INC_MACROS
00107 
00108 #include <math.h>
00109 
00110 #define _Long(x)  ((INT32)(x))
00111 #define _Int(x)   ((INT32)(x))
00112 #define _Float(x) ((FLOAT)(x))
00113 #define _Dble(x)  ((double)(x))
00114 
00115 double const XS_2PI         =  6.28318530717958623200;
00116 double const XS_DEGTORAD    =  0.01745329251994329547;
00117 double const XS_E           =  2.71828182845904553488;
00118 double const XS_EEXPPI      = 23.14069263277927390732;
00119 double const XS_GOLDEN      =  1.61803398874989490253;
00120 double const XS_INVPI       =  0.31830988618379069122;
00121 double const XS_LN10        =  2.30258509299404590109;
00122 double const XS_LN2         =  0.69314718055994528623;
00123 double const XS_LOG10E      =  0.43429448190325187218;
00124 double const XS_LOG2E       =  1.44269504088896338700;
00125 double const XS_LOG05       = -0.693147180559945;
00126 double const XS_PI          =  3.14159265358979323846;
00127 double const XS_PIDIV2      =  1.57079632679489655800;
00128 double const XS_PIDIV4      =  0.78539816339744827900;
00129 double const XS_RADTODEG    = 57.29577951308232286465;
00130 double const XS_SQRT2       =  1.41421356237309514547;
00131 double const XS_SQRT2PI     =  2.50662827463100024161;
00132 double const XS_SQRT3       =  1.73205080756887719318;
00133 double const XS_SQRT10      =  3.16227766016837952279;
00134 double const XS_SQRTE       =  1.64872127070012819416;
00135 double const XS_SQRTHALF    =  0.70710678118654757274;
00136 double const XS_SQRTLN2     =  0.83255461115769768821;
00137 double const XS_SQRTPI      =  1.77245385090551588192;
00138 double const XS_EPSILON     =  1.0e-10;
00139 double const XS_GOOGOL      =  1.0e50;
00140 
00141 
00142 //***************************************************************************************
00143 // ONE ARGUMENT UTILITY FUNCTIONS - INT32 VERSIONS
00144 //***************************************************************************************
00145 
00146 inline INT32 LClamp(INT32 x, INT32 min, INT32 max)
00147 {
00148     return (x < min ? min : (x > max ? max : x));
00149 }
00150     
00151 inline INT32 LAbs(INT32 x)
00152 {
00153     return (x >= 0) ? x : -x;
00154 }
00155 
00156 inline INT32 LSign(INT32 x)
00157 {
00158     return (x < 0) ? -1 : 1;
00159 }
00160 
00161 inline INT32 LSmooth(INT32 x)
00162 {
00163     return (3 - 2 * x) * x * x;
00164 }
00165 
00166 
00167 //***************************************************************************************
00168 // TWO-ARGUMENT UTILITY FUNCTIONS - INT32 VERSIONS
00169 //***************************************************************************************
00170 
00171 inline INT32 LMin(INT32 x, INT32 y)
00172 {
00173     return (x < y) ? x : y;
00174 }
00175 
00176 inline INT32 LMax(INT32 x, INT32 y)
00177 {
00178     return (x > y) ? x : y;
00179 }
00180 
00181 
00182 //***************************************************************************************
00183 // THREE-ARGUMENT UTILITY FUNCTIONS - INT32 VERSIONS
00184 //***************************************************************************************
00185 
00186 inline INT32 LMin(INT32 x, INT32 y, INT32 z)
00187 {
00188     return (x < y) ? LMin(x, z) : LMin(y, z);
00189 }
00190 
00191 inline INT32 LMax(INT32 x, INT32 y, INT32 z)
00192 {
00193     return (x > y) ? LMax(x, z) : LMax(y, z);
00194 }
00195 
00196 
00197 
00198 //***************************************************************************************
00199 // ONE ARGUMENT UTILITY FUNCTIONS - FLOAT VERSIONS
00200 //***************************************************************************************
00201 
00202 inline float FClamp(float x)
00203 {
00204     return (x < 0.0f ? 0.0f : (x > 1.0f ? 1.0f : x));
00205 }
00206     
00207 inline float FAbs(float x)
00208 {
00209     return (x >= 0.0f) ? x : -x;
00210 }
00211 
00212 inline float FFrac(float x)
00213 {
00214     return (x-((INT32)(x)));
00215 }
00216 
00217 inline float FCeil(float x)
00218 {
00219     return (x == _Int(x)) ? x : (x > 0.0f) ? _Float(_Int(x) + 1.0f) : _Float(_Int(x));
00220 }
00221 
00222 inline float FFloor(float x)
00223 {
00224     return (x == _Int(x)) ? x : (x > 0.0f) ? _Float(_Int(x)) : _Float(_Int(x) - 1.0f);
00225 }
00226 
00227 inline float FRound(float x)
00228 {
00229     return (x >= 0.0f) ? _Float(_Int(x + 0.5f)) : _Float(- _Int(0.5f - x));
00230 }
00231 
00232 inline float FSign(float x)
00233 {
00234     return (x < 0.0f) ? -1.0f : 1.0f;
00235 }
00236 
00237 inline float FSmooth(float x)
00238 {
00239     return (3.0f - 2.0f * x) * x * x;
00240 }
00241 
00242 inline float FTrunc(float x)
00243 {
00244     return _Float(_Int(x));
00245 }
00246 
00247 
00248 //***************************************************************************************
00249 // TWO-ARGUMENT UTILITY FUNCTIONS - FLOAT VERSIONS
00250 //***************************************************************************************
00251 
00252 inline float FMin(float x, float y)
00253 {
00254     return (x < y) ? x : y;
00255 }
00256 
00257 inline float FMax(float x, float y)
00258 {
00259     return (x > y) ? x : y;
00260 }
00261 
00262 
00263 //***************************************************************************************
00264 // THREE-ARGUMENT UTILITY FUNCTIONS - FLOAT VERSIONS
00265 //***************************************************************************************
00266 
00267 inline float FMax(float x, float y, float z)
00268 {
00269     return (x > y) ? FMax(x, z) : FMax(y, z);
00270 }
00271 
00272 inline float FMin(float x, float y, float z)
00273 {
00274     return (x < y) ? FMin(x, z) : FMin(y, z);
00275 }
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 //***************************************************************************************
00285 // ONE ARGUMENT UTILITY FUNCTIONS - DOUBLE VERSIONS
00286 //***************************************************************************************
00287 
00288 inline double DAbs(double f)
00289 {
00290     return (f >= 0) ? f : -f;
00291 }
00292 
00293 inline double DFrac(double f)
00294 {
00295     return (f-_Long(f));
00296 }
00297 
00298 inline double DCeil(double f)
00299 {
00300     return (f == _Int(f)) ? f : (f > 0) ? _Dble(_Int(f) + 1) : _Dble(_Int(f));
00301 }
00302 
00303 inline double DCube(double f)
00304 {
00305     return f * f * f;
00306 }
00307 
00308 inline double DDegrees(double f)
00309 {
00310     return f * XS_RADTODEG;
00311 }
00312 
00313 inline double DFloor(double f)
00314 {
00315     return (f == _Int(f)) ? f : (f > 0) ? _Dble(_Int(f)) : _Dble(_Int(f)-1);
00316 }
00317 
00318 inline double DInv(double f)
00319 {
00320     return 1.0 / f;
00321 }
00322 
00323 inline BOOL DIsZero(double f)
00324 {
00325     return ((f<=0.0) ? (f >= -XS_EPSILON) : (f <= XS_EPSILON));
00326 }
00327 
00328 inline double DRadians(double f)
00329 {
00330     return f * XS_DEGTORAD;
00331 }
00332 
00333 inline double DRound(double f)
00334 {
00335     return (f >= 0) ? _Dble(_Int(f + 0.5)) : _Dble(- _Int(0.5 - f));
00336 }
00337 
00338 inline double DSign(double f)
00339 {
00340     return (f < 0) ? -1.0 : 1.0;
00341 }
00342 
00343 inline double DSmooth(double f)
00344 {
00345     return (3.0 - 2.0 * f) * f * f;
00346 }
00347 
00348 inline double DTrunc(double f)
00349 {
00350     return _Dble(_Int(f));
00351 }
00352 
00353 inline double DZSign(double f)
00354 {
00355     return (f > 0) ? 1.0 : (f < 0) ? -1.0 : 0.0;
00356 }
00357 
00358 inline double DCubeRoot(double x)
00359 {
00360     return (x>0.0) ? pow(x, 1.0/3.0) : (x<0.0) ? -pow(-x, 1.0/3.0) : 0.0;
00361 }
00362 
00363 
00364 
00365 //***************************************************************************************
00366 // TWO-ARGUMENT UTILITY FUNCTIONS - DOUBLE VERSIONS
00367 //***************************************************************************************
00368 
00369 inline BOOL DFuzEQ(double f, double g)
00370 {
00371     return (f <= g) ? (f >= g - XS_EPSILON) : (f <= g + XS_EPSILON);
00372 }
00373 
00374 inline BOOL DFuzGEQ(double f, double g)
00375 {
00376     return (f >= g - XS_EPSILON);
00377 }
00378 
00379 inline BOOL DFuzLEQ(double f, double g)
00380 {
00381     return (f <= g + XS_EPSILON);
00382 }
00383 
00384 inline double DMax(double f, double g)
00385 {
00386     return (f > g) ? f : g;
00387 }
00388 
00389 inline double DMin(double f, double g)
00390 {
00391     return (f < g) ? f : g;
00392 }
00393 
00394 inline void DSwap(double& f, double& g)
00395 {
00396     double gmTmp = f; f = g; g = gmTmp;
00397 }
00398 
00399 inline void DSwap(INT32& i, INT32& j)
00400 {
00401     INT32 gmTmp = i; i = j; j = gmTmp;
00402 }
00403 
00404 inline void DSort(double& lo, double& hi)
00405 {
00406     if (lo>hi) DSwap(lo,hi);
00407 }
00408 
00409 inline double DStep(double a, double f)
00410 {
00411     return _Dble(f>=a);
00412 }
00413 
00414 inline double DFuzStep(double a, double f, double fzz)
00415 {
00416     return _Dble(f>=(a+fzz));
00417 }
00418 
00419 inline double DGamma(double gamma, double f)
00420 {
00421     return (gamma==0.0) ? XS_GOOGOL : pow(f, 1/gamma);
00422 }
00423 
00424 inline double DBias(double bias, double f)
00425 {
00426     return pow(f,log(bias)/log(0.5));
00427 }
00428 
00429 inline double DGain(double gain, double f)
00430 {
00431     return (f<0.5) ? (DBias(1.0-gain,2.0*f)/2.0) : (1-DBias(1.0-gain, 2.0-2.0*f)/2.0);
00432 }
00433 
00434 inline double DExculsiveOr(double a, double b)
00435 {
00436     return (a + b - a * b);
00437 }
00438 
00439 inline double DIntersection(double a, double b)
00440 {
00441     return (a * b);
00442 }
00443 
00444 inline double DDifference(double a, double b)
00445 {
00446     return (a - a * b);
00447 }
00448 
00449 inline double DRepeat(double u, double freq)
00450 {
00451     // return distance into tile
00452     return DFrac(u*freq);
00453 }
00454 
00455 inline double DTile(double u, double freq)
00456 {
00457     // return the tile number
00458     return _Long(u*freq);
00459 }
00460 
00461 
00462 
00463 //***************************************************************************************
00464 // MULTI-ARGUMENT UTILITY FUNCTIONS - DOUBLE VERSIONS
00465 //***************************************************************************************
00466 
00467 inline double DClamp(const double x, const double a, const double b)
00468 {
00469     return (x < a ? a : (x > b ? b : x));
00470 }
00471 
00472 inline double DLerp(double f, double l, double h)
00473 {
00474     return l + ((h - l) * f );
00475 }
00476 
00477 inline double DNorm(double f, double a, double b)
00478 {
00479     return (f-a)/(b-a);
00480 }
00481 
00482 inline double DMax(double f, double g, double h)
00483 {
00484     return (f > g) ? DMax(f, h) : DMax(g, h);
00485 }
00486 
00487 inline double DMin(double f, double g, double h)
00488 {
00489     return (f < g) ? DMin(f, h) : DMin(g, h);
00490 }
00491 
00492 inline double DSlide(double f, double l, double h)
00493 {
00494     return (f < 0) ? l : (f > 1) ? h : DLerp(DSmooth(f), l, h);
00495 }
00496 
00497 inline double DSmoothStep(double a, double b, double f)
00498 {
00499     return (f < a) ? 0.0 : (f > b) ? 1.0 : DSmooth(DNorm(f,a,b));
00500 }
00501 
00502 inline double DBoxStep(double a, double b, double x)
00503 {
00504     return (DClamp((x-a)/(b-a),0,1));
00505 }
00506 
00507 inline double DPulse(double a, double b, double x)
00508 {
00509     return (DStep(a,x) - DStep(b,x));
00510 }
00511 
00512 inline double DFuzPulse(double a, double b, double x, double fzz)
00513 {
00514     return (DFuzStep(a,x,-fzz) - DFuzStep(b,x,+fzz));
00515 }
00516 
00517 inline double DRange(double t,
00518                        double min, double max, 
00519                        double lo, double mid, double hi)
00520 {
00521     return (t<min) ? lo : ((t>max) ? hi : mid);
00522 }
00523 
00524 
00525 #endif

Generated on Sat Nov 10 03:45:38 2007 for Camelot by  doxygen 1.4.4