eLynx SDK
v3.0.1 C++ image processing API reference |
00001 //============================================================================ 00002 // MathCore.h Math.Component package 00003 //============================================================================ 00004 // Usage : definitions of very basic math helpers 00005 //---------------------------------------------------------------------------- 00006 // Copyright (C) 2006 by eLynx project 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Library General Public 00010 // License as published by the Free Software Foundation; either 00011 // version 2 of the License, or (at your option) any later version. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 // See the GNU Library General Public License for more details. 00017 //---------------------------------------------------------------------------- 00018 #ifndef __MathCore_h__ 00019 #define __MathCore_h__ 00020 00021 #include <elx/core/CoreTypes.h> 00022 #include "MathLib.h" 00023 00024 namespace eLynx { 00025 namespace Math { 00026 00028 #define CM_Channel0 (1<<0) 00029 #define CM_Channel1 (1<<1) 00030 #define CM_Channel2 (1<<2) 00031 #define CM_Channel3 (1<<3) 00032 00033 #define CM_Channel01 (CM_Channel0 | CM_Channel1) 00034 #define CM_Channel02 (CM_Channel0 | CM_Channel2) 00035 #define CM_Channel03 (CM_Channel0 | CM_Channel3) 00036 #define CM_Channel12 (CM_Channel1 | CM_Channel2) 00037 #define CM_Channel13 (CM_Channel1 | CM_Channel3) 00038 #define CM_Channel23 (CM_Channel2 | CM_Channel3) 00039 00040 #define CM_Channel012 (CM_Channel0 | CM_Channel1 | CM_Channel2) 00041 #define CM_Channel013 (CM_Channel0 | CM_Channel1 | CM_Channel3) 00042 #define CM_Channel023 (CM_Channel0 | CM_Channel2 | CM_Channel3) 00043 #define CM_Channel123 (CM_Channel1 | CM_Channel2 | CM_Channel3) 00044 00045 #define CM_Channel0123 (CM_Channel0 | CM_Channel1 | CM_Channel2 | CM_Channel3) 00046 00047 #define CM_None (0) 00048 #define CM_All (CM_Channel0123) 00049 00050 //---------------------------------------------------------------------------- 00053 00058 template<typename T> 00059 const T& elxMin(const T& iA, const T& iB); 00060 00066 template<typename T> 00067 const T& elxMin(const T& iA, const T& iB, const T& iC); 00068 00075 template<typename T> 00076 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD); 00077 00085 template<typename T> 00086 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE); 00087 00096 template<typename T> 00097 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE, const T& iF); 00098 00099 template<typename T> 00100 bool elxMin(const T * iprSrc, uint32 iSize, T& oMin); 00101 00102 template<typename T> 00103 bool elxMin(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMin); 00104 00105 template<typename T> 00106 T elxMin(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00107 00108 00113 template<typename T> 00114 const T& elxMax(const T& iA, const T& iB); 00115 00121 template<typename T> 00122 const T& elxMax(const T& iA, const T& iB, const T& iC); 00123 00130 template<typename T> 00131 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD); 00132 00140 template<typename T> 00141 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE); 00142 00151 template<typename T> 00152 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE, const T& iF); 00153 00154 template<typename T> 00155 bool elxMax(const T * iprSrc, uint32 iSize, T& oMin); 00156 00157 template<typename T> 00158 bool elxMax(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMax); 00159 00160 template<typename T> 00161 T elxMax(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00162 00163 00164 template<typename T> 00165 bool elxMinMax(const T * iprSrc, uint32 iSize, T& oMin, T& oMax); 00166 00167 template<typename T> 00168 bool elxMinMax(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMin, T * oprMax); 00169 00170 00172 template<typename T> 00173 T elxNeg(const T iValue); 00174 00175 00177 template<typename T> 00178 T elxMedian(T iV0, T iV1, T iV2); 00179 00181 template<typename T> 00182 T elxMedian(T iV0, T iV1, T iV2, T iV3); 00183 00185 template<typename T> 00186 T elxMedian(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00187 00189 00190 00191 //---------------------------------------------------------------------------- 00194 00195 template<typename T, typename U> 00196 void elxClamp(T iIn, U& oOut); 00197 00198 template<typename T> 00199 void elxClamp(T iInMin, T iInMax, T& oOut); 00200 00202 enum EValueAction 00203 { 00204 R_NoAction, // Clamp values to keep them in range 00205 R_Clamp, // Leave as is 00206 R_Cycle // Cycle values to keep them in range 00207 }; 00208 00209 // direct operator 00210 template<typename T> 00211 bool elxAdd(T * ioprSrc, uint32 iSize, T iValue); 00212 00213 template<typename T> 00214 bool elxSub(T * ioprSrc, uint32 iSize, T iValue); 00215 00216 template<typename T> 00217 bool elxMul(T * ioprSrc, uint32 iSize, T iValue); 00218 00219 template<typename T> 00220 bool elxDiv(T * ioprSrc, uint32 iSize, T iValue); 00221 00222 template<typename T> 00223 bool elxDif(T * ioprSrc, uint32 iSize, T iValue); 00224 00225 // operator with clamped resulting value 00226 template<typename T> 00227 bool elxAddClamp(T * ioprSrc, uint32 iSize, T iValue); 00228 00229 template<typename T> 00230 bool elxSubClamp(T * ioprSrc, uint32 iSize, T iValue); 00231 00232 template<typename T> 00233 bool elxMulClamp(T * ioprSrc, uint32 iSize, double iValue); 00234 00235 template<typename T> 00236 bool elxDivClamp(T * ioprSrc, uint32 iSize, double iValue); 00237 00238 // operator with cycling resulting value 00239 template<typename T> 00240 bool elxAddCycle(T * ioprSrc, uint32 iSize, T iValue); 00241 00242 template<typename T> 00243 bool elxSubCycle(T * ioprSrc, uint32 iSize, T iValue); 00244 00245 template<typename T> 00246 bool elxMulCycle(T * ioprSrc, uint32 iSize, double iValue); 00247 00248 template<typename T> 00249 bool elxDivCycle(T * ioprSrc, uint32 iSize, double iValue); 00250 00251 // operator with action option 00252 template<typename T> 00253 bool elxAdd(T * ioprSrc, uint32 iSize, T iValue, EValueAction iAction); 00254 00255 template<typename T> 00256 bool elxSub(T * ioprSrc, uint32 iSize, T iValue, EValueAction iAction); 00257 00258 template<typename T> 00259 bool elxMul(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00260 00261 template<typename T> 00262 bool elxDiv(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00263 00264 // operator value in double resolution 00265 template<typename T> 00266 bool elxAdd(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00267 00268 template<typename T> 00269 bool elxSub(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00270 00271 template<typename T> 00272 bool elxMul(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00273 00274 template<typename T> 00275 bool elxDiv(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00276 00277 // operator value in normalized double resolution 00278 template<typename T> 00279 bool elxAddNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00280 00281 template<typename T> 00282 bool elxSubNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00283 00284 template<typename T> 00285 bool elxMulNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00286 00287 template<typename T> 00288 bool elxDivNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00289 00290 00291 // operator with buffer 00292 /* 00293 template<typename T, typename U> 00294 bool elxAdd(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00295 00296 template<typename T, typename U> 00297 bool elxSub(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00298 00299 template<typename T, typename U> 00300 bool elxMul(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00301 00302 template<typename T, typename U> 00303 bool elxDiv(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00304 */ 00306 00307 00310 template<typename T> 00311 bool elxNormalize(T * ioprSrc, uint32 iSize, T iMin, T iMax); 00312 00313 template<typename T> 00314 bool elxNormalize(T * ioprSrc, uint32 iSize, const T * oprMin, const T * oprMax, uint32 iChannelMask, uint32 iChannelCount); 00315 00316 template<typename T> 00317 bool elxNormalize(T * ioprSrc, uint32 iSize); 00318 00319 template<typename T> 00320 bool elxNormalize(T * ioprSrc, uint32 iStep, uint32 iSize, uint32 iChannelMask, uint32 iChannelCount); 00322 00323 00324 //---------------------------------------------------------------------------- 00327 00332 template<typename T> 00333 T elxSign(T iValue); 00334 00339 template<typename T> 00340 T elxAbs(T iX); 00341 00346 template<typename T> 00347 T elxFloor(T iValue); 00348 00353 template<typename T> 00354 T elxCeil(T iValue); 00355 00361 template<typename T> 00362 T elxRound(T iValue); 00363 00368 template<typename T> 00369 int32 elxRint(T iValue); 00370 00378 template<typename T> 00379 T elxMod(T iA, T iB); 00380 00385 template<typename T> 00386 T elxMean(T iV1, T iV2); 00387 00392 template<typename T> 00393 T elxAbsDiff(T iV1, T iV2); 00394 00399 template<typename T> 00400 bool elxIsOdd(T iValue); 00401 00406 template<typename T> 00407 bool elxIsEven(T iValue); 00408 00409 ExportedByMath int32 elxHighBit(uint32 iVal); 00410 //ExportedByMath int32 elxHighBit(uint64 iVal); 00411 //ExportedByMath int32 elxLowBit(uint32 iVal); 00412 //ExportedByMath int32 elxLowBit(uint64 iVal); 00413 00415 00416 //---------------------------------------------------------------------------- 00419 00420 // square root of 2 constant value 00421 #ifndef M_SQRT2 00422 #define M_SQRT2 (1.4142135623730950488016887242097) 00423 #endif 00424 00426 #ifndef M_E 00427 #define M_E (2.7182818284590452353602874713527) 00428 #endif 00429 00433 template<typename T> 00434 T elxSqr(T iX); 00435 00439 template<typename T> 00440 T elxSqrt(T iX); 00441 00446 template<typename T> 00447 T elxPow(T iX, T iY); 00448 00452 template<typename T> 00453 T elxLog(T iX); 00454 00458 template<typename T> 00459 T elxLog10(T iX); 00460 00464 template<typename T> 00465 T elxExp(T iX); 00466 00470 template<typename T> 00471 bool elxIsPow2(T iValue); 00472 00476 template<typename T> 00477 T elxNextPow2(T iValue); 00478 00480 00481 00482 //---------------------------------------------------------------------------- 00486 #ifndef M_PI 00487 #define M_PI (3.1415926535897932384626433832795) 00488 #endif 00489 00491 #ifndef M_2PI 00492 #define M_2PI (6.283185307179586476925286766559) 00493 #endif 00494 00496 #ifndef M_PIo2 00497 #define M_PIo2 (1.5707963267948966192313216916398) 00498 #endif 00499 00501 #ifndef M_PIo4 00502 #define M_PIo4 (0.78539816339744830961566084581988) 00503 #endif 00504 00506 #ifndef M_3PIo2 00507 #define M_3PIo2 (4.7123889803846898576939650749193) 00508 #endif 00509 00513 template<typename T> 00514 T elxDeg2Rad(T iDegrees); 00515 00519 template<typename T> 00520 T elxRad2Deg(T iRadian); 00521 00525 template<typename T> 00526 T elxCos(T iRadian); 00527 00531 template<typename T> 00532 T elxSin(T iRadian); 00533 00537 template<typename T> 00538 T elxTan(T iRadian); 00539 00543 template<typename T> 00544 T elxArccos(T iX); 00545 00549 template<typename T> 00550 T elxArcsin(T iX); 00551 00555 template<typename T> 00556 T elxArctan(T iYoX); 00557 00558 /* 00562 ExportedByMath double HypCosd(double iRad); 00563 00567 ExportedByMath double HypSind(double iRad); 00568 00572 ExportedByMath double HypTand(double iRad); 00573 */ 00575 00576 //---------------------------------------------------------------------------- 00579 00580 // 1/sqrt(2*PI) 00581 #ifndef M_GAUSS1D 00582 #define M_GAUSS1D (0.39894228040143267793994605993438) 00583 #endif 00584 00585 // 1/(2*PI) 00586 #ifndef M_GAUSS2D 00587 #define M_GAUSS2D (0.15915494309189533576888376337251) 00588 #endif 00589 00591 template<typename T> 00592 T elxGetGaussianVariance(T iRadius); 00593 00595 template<typename T> 00596 T elxGaussian(T iX, T iSigma); 00597 00599 template<typename T> 00600 T elxGaussian(T iX, T iY, T iSigma); 00601 00602 template<typename T> 00603 T elxMeanGaussian (T iX, T iSigma); 00604 00606 template<typename T> 00607 T elxDoG(T iX, T iSigma); 00608 00610 template<typename T> 00611 T elxLoG(T iX, T iSigma); 00612 00614 template<typename T> 00615 T elxLoG(T iX, T iY, T iSigma); 00616 00618 00619 //---------------------------------------------------------------------------- 00622 00629 template<typename T> 00630 T elxSmooth(T iMin, T iMax, T iValue); 00631 00632 ExportedByMath double elxMidtone(double iX, double iMidtone); 00633 00634 //---------------------------------------------------------------------------- 00637 00640 ExportedByMath void elxRandomReset(); 00641 00642 ExportedByMath void elxRandomReset(int32 iSeed); 00643 00647 ExportedByMath int32 elxRandomSign(); 00648 00649 // Return a pseudorandom-number in range [0.0, 1.0] 00650 ExportedByMath double elxRandom(); 00651 00652 // Return a pseudorandom-number in range [0, iMax] 00653 template<typename T> 00654 T elxRandom(T iMax); 00655 00656 // Return a pseudorandom-number in range [iMin, iMax] 00657 template<typename T> 00658 T elxRandom(T iMin, T iMax); 00659 00661 00662 } // namespace Math 00663 } // namespace eLynx 00664 00665 #include "inl/Core.inl" 00666 #include "inl/MinMax.inl" 00667 #include "inl/Clamp.inl" 00668 #include "inl/Operator.inl" 00669 #include "inl/Normalize.inl" 00670 00671 #endif // __MathCore_h__ 00672 00673 /* 00674 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 00675 // Phase1: 1st sdk to be refactoring 00676 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 00677 00681 ExportedByMath const float& InfPosf(); 00682 00686 ExportedByMath const double& InfPosd(); 00687 00691 ExportedByMath const float& InfNegf(); 00692 00696 ExportedByMath const double& InfNegd(); 00697 00702 ExportedByMath bool IsNaN(float iVal); 00703 00708 ExportedByMath bool IsNaN(double iVal); 00709 00715 ExportedByMath bool IsFinite(float iVal); 00716 00722 ExportedByMath bool IsFinite(double iVal); 00724 00725 00726 //---------------------------------------------------------------------------- 00732 ExportedByMath const float& NaNf(); 00733 00737 ExportedByMath const double& NaNd(); 00738 00742 ExportedByMath const float& InfPosf(); 00743 00747 ExportedByMath const double& InfPosd(); 00748 00752 ExportedByMath const float& InfNegf(); 00753 00757 ExportedByMath const double& InfNegd(); 00758 00763 ExportedByMath bool IsNaN(float iVal); 00764 00769 ExportedByMath bool IsNaN(double iVal); 00770 00776 ExportedByMath bool IsFinite(float iVal); 00777 00783 ExportedByMath bool IsFinite(double iVal); 00785 00791 ExportedByMath float Lerpf(float iA, float iB, float iScalar); 00792 // linear interpolation 00793 inline F32 Lerpf(F32 iA, F32 iB, F32 iScalar) { return iA + iScalar*(iB-iA); } 00794 inline F64 Lerpd(F64 iA, F64 iB, F64 iScalar) { return iA + iScalar*(iB-iA); } 00795 */