eLynx SDK
v3.3.0 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 00053 ExportedByMath const char * elxChannelMaskToString(uint32 iChannelMask); 00054 00058 ExportedByMath uint32 elxToChannelMask(const char* iprChannelMask); 00059 00060 //---------------------------------------------------------------------------- 00063 00068 template<typename T> 00069 const T& elxMin(const T& iA, const T& iB); 00070 00076 template<typename T> 00077 const T& elxMin(const T& iA, const T& iB, const T& iC); 00078 00085 template<typename T> 00086 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD); 00087 00095 template<typename T> 00096 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE); 00097 00106 template<typename T> 00107 const T& elxMin(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE, const T& iF); 00108 00109 template<typename T> 00110 bool elxMin(const T * iprSrc, uint32 iSize, T& oMin); 00111 00112 template<typename T> 00113 bool elxMin(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMin); 00114 00115 template<typename T> 00116 T elxMin(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00117 00118 00123 template<typename T> 00124 const T& elxMax(const T& iA, const T& iB); 00125 00131 template<typename T> 00132 const T& elxMax(const T& iA, const T& iB, const T& iC); 00133 00140 template<typename T> 00141 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD); 00142 00150 template<typename T> 00151 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE); 00152 00161 template<typename T> 00162 const T& elxMax(const T& iA, const T& iB, const T& iC, const T& iD, const T& iE, const T& iF); 00163 00164 template<typename T> 00165 bool elxMax(const T * iprSrc, uint32 iSize, T& oMin); 00166 00167 template<typename T> 00168 bool elxMax(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMax); 00169 00170 template<typename T> 00171 T elxMax(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00172 00173 00174 template<typename T> 00175 bool elxMinMax(const T * iprSrc, uint32 iSize, T& oMin, T& oMax); 00176 00177 template<typename T> 00178 bool elxMinMax(const T * iprSrc, uint32 iSize, uint32 iStep, T * oprMin, T * oprMax); 00179 00180 00182 template<typename T> 00183 T elxNeg(const T iValue); 00184 00185 00187 template<typename T> 00188 T elxMedian(T iV0, T iV1, T iV2); 00189 00191 template<typename T> 00192 T elxMedian(T iV0, T iV1, T iV2, T iV3); 00193 00195 template<typename T> 00196 T elxMedian(T iV0, T iV1, T iV2, T iV3, T iV4, T iV5, T iV6, T iV7, T iV8); 00197 00199 00200 00201 //---------------------------------------------------------------------------- 00204 00205 template<typename T, typename U> 00206 void elxClamp(T iIn, U& oOut); 00207 00208 template<typename T> 00209 void elxClamp(T iInMin, T iInMax, T& oOut); 00210 00212 enum EValueAction 00213 { 00214 R_NoAction, // Clamp values to keep them in range 00215 R_Clamp, // Leave as is 00216 R_Cycle // Cycle values to keep them in range 00217 }; 00218 00219 // direct operator 00220 template<typename T> 00221 bool elxAdd(T * ioprSrc, uint32 iSize, T iValue); 00222 00223 template<typename T> 00224 bool elxSub(T * ioprSrc, uint32 iSize, T iValue); 00225 00226 template<typename T> 00227 bool elxMul(T * ioprSrc, uint32 iSize, T iValue); 00228 00229 template<typename T> 00230 bool elxDiv(T * ioprSrc, uint32 iSize, T iValue); 00231 00232 template<typename T> 00233 bool elxDif(T * ioprSrc, uint32 iSize, T iValue); 00234 00235 // operator with clamped resulting value 00236 template<typename T> 00237 bool elxAddClamp(T * ioprSrc, uint32 iSize, T iValue); 00238 00239 template<typename T> 00240 bool elxSubClamp(T * ioprSrc, uint32 iSize, T iValue); 00241 00242 template<typename T> 00243 bool elxMulClamp(T * ioprSrc, uint32 iSize, double iValue); 00244 00245 template<typename T> 00246 bool elxDivClamp(T * ioprSrc, uint32 iSize, double iValue); 00247 00248 // operator with cycling resulting value 00249 template<typename T> 00250 bool elxAddCycle(T * ioprSrc, uint32 iSize, T iValue); 00251 00252 template<typename T> 00253 bool elxSubCycle(T * ioprSrc, uint32 iSize, T iValue); 00254 00255 template<typename T> 00256 bool elxMulCycle(T * ioprSrc, uint32 iSize, double iValue); 00257 00258 template<typename T> 00259 bool elxDivCycle(T * ioprSrc, uint32 iSize, double iValue); 00260 00261 // operator with action option 00262 template<typename T> 00263 bool elxAdd(T * ioprSrc, uint32 iSize, T iValue, EValueAction iAction); 00264 00265 template<typename T> 00266 bool elxSub(T * ioprSrc, uint32 iSize, T iValue, EValueAction iAction); 00267 00268 template<typename T> 00269 bool elxMul(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00270 00271 template<typename T> 00272 bool elxDiv(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00273 00274 // operator value in double resolution 00275 template<typename T> 00276 bool elxAdd(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00277 00278 template<typename T> 00279 bool elxSub(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00280 00281 template<typename T> 00282 bool elxMul(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00283 00284 template<typename T> 00285 bool elxDiv(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00286 00287 // operator value in normalized double resolution 00288 template<typename T> 00289 bool elxAddNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00290 00291 template<typename T> 00292 bool elxSubNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00293 00294 template<typename T> 00295 bool elxMulNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00296 00297 template<typename T> 00298 bool elxDivNorm(T * ioprSrc, uint32 iSize, double iValue, EValueAction iAction); 00299 00300 00301 // operator with buffer 00302 /* 00303 template<typename T, typename U> 00304 bool elxAdd(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00305 00306 template<typename T, typename U> 00307 bool elxSub(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00308 00309 template<typename T, typename U> 00310 bool elxMul(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00311 00312 template<typename T, typename U> 00313 bool elxDiv(T * ioprDst, const U * iprSrc, uint32 iSize, EValueAction iAction); 00314 */ 00316 00317 00320 template<typename T> 00321 bool elxNormalize(T * ioprSrc, uint32 iSize); 00322 00323 template<typename T> 00324 bool elxNormalize(T * ioprSrc, uint32 iSize, T iMin, T iMax); 00325 00326 template<typename T> 00327 bool elxNormalize(T * ioprSrc, uint32 iSize, uint32 iChannelMask, uint32 iChannelCount); 00328 00329 template<typename T> 00330 bool elxNormalize(T * ioprSrc, uint32 iSize, 00331 const T * oprMinRange, const T * oprMaxRange, 00332 uint32 iChannelMask, uint32 iChannelCount); 00333 00335 00336 00337 //---------------------------------------------------------------------------- 00340 00345 template<typename T> 00346 T elxSign(T iValue); 00347 00352 template<typename T> 00353 T elxAbs(T iX); 00354 00359 template<typename T> 00360 T elxFloor(T iValue); 00361 00366 template<typename T> 00367 T elxCeil(T iValue); 00368 00374 template<typename T> 00375 T elxRound(T iValue); 00376 00381 template<typename T> 00382 int32 elxRint(T iValue); 00383 00391 template<typename T> 00392 T elxMod(T iA, T iB); 00393 00398 template<typename T> 00399 T elxMean(T iV1, T iV2); 00400 00405 template<typename T> 00406 T elxAbsDiff(T iV1, T iV2); 00407 00412 template<typename T> 00413 bool elxIsOdd(T iValue); 00414 00419 template<typename T> 00420 bool elxIsEven(T iValue); 00421 00422 ExportedByMath int32 elxHighBit(uint32 iVal); 00423 //ExportedByMath int32 elxHighBit(uint64 iVal); 00424 //ExportedByMath int32 elxLowBit(uint32 iVal); 00425 //ExportedByMath int32 elxLowBit(uint64 iVal); 00426 00428 00429 //---------------------------------------------------------------------------- 00432 00433 // square root of 2 constant value 00434 #ifndef M_SQRT2 00435 #define M_SQRT2 (1.4142135623730950488016887242097) 00436 #endif 00437 00439 #ifndef M_E 00440 #define M_E (2.7182818284590452353602874713527) 00441 #endif 00442 00446 template<typename T> 00447 T elxSqr(T iX); 00448 00452 template<typename T> 00453 T elxSqrt(T iX); 00454 00459 template<typename T> 00460 T elxPow(T iX, T iY); 00461 00465 template<typename T> 00466 T elxLog(T iX); 00467 00471 template<typename T> 00472 T elxLog10(T iX); 00473 00477 template<typename T> 00478 T elxExp(T iX); 00479 00483 template<typename T> 00484 bool elxIsPow2(T iValue); 00485 00489 template<typename T> 00490 T elxNextPow2(T iValue); 00491 00493 00494 00495 //---------------------------------------------------------------------------- 00499 #ifndef M_PI 00500 #define M_PI (3.1415926535897932384626433832795) 00501 #endif 00502 00504 #ifndef M_2PI 00505 #define M_2PI (6.283185307179586476925286766559) 00506 #endif 00507 00509 #ifndef M_PIo2 00510 #define M_PIo2 (1.5707963267948966192313216916398) 00511 #endif 00512 00514 #ifndef M_PIo4 00515 #define M_PIo4 (0.78539816339744830961566084581988) 00516 #endif 00517 00519 #ifndef M_3PIo2 00520 #define M_3PIo2 (4.7123889803846898576939650749193) 00521 #endif 00522 00526 template<typename T> 00527 T elxDeg2Rad(T iDegrees); 00528 00532 template<typename T> 00533 T elxRad2Deg(T iRadian); 00534 00538 template<typename T> 00539 T elxCos(T iRadian); 00540 00544 template<typename T> 00545 T elxSin(T iRadian); 00546 00550 template<typename T> 00551 T elxTan(T iRadian); 00552 00556 template<typename T> 00557 T elxArccos(T iX); 00558 00562 template<typename T> 00563 T elxArcsin(T iX); 00564 00568 template<typename T> 00569 T elxArctan(T iYoX); 00570 00571 /* 00575 ExportedByMath double HypCosd(double iRad); 00576 00580 ExportedByMath double HypSind(double iRad); 00581 00585 ExportedByMath double HypTand(double iRad); 00586 */ 00588 00589 //---------------------------------------------------------------------------- 00592 00593 // 1/sqrt(2*PI) 00594 #ifndef M_GAUSS1D 00595 #define M_GAUSS1D (0.39894228040143267793994605993438) 00596 #endif 00597 00598 // 1/(2*PI) 00599 #ifndef M_GAUSS2D 00600 #define M_GAUSS2D (0.15915494309189533576888376337251) 00601 #endif 00602 00604 template<typename T> 00605 T elxGetGaussianVariance(T iRadius); 00606 00608 template<typename T> 00609 T elxGaussian(T iX, T iSigma); 00610 00612 template<typename T> 00613 T elxGaussian(T iX, T iY, T iSigma); 00614 00615 template<typename T> 00616 T elxMeanGaussian (T iX, T iSigma); 00617 00619 template<typename T> 00620 T elxDoG(T iX, T iSigma); 00621 00623 template<typename T> 00624 T elxLoG(T iX, T iSigma); 00625 00627 template<typename T> 00628 T elxLoG(T iX, T iY, T iSigma); 00629 00631 00632 //---------------------------------------------------------------------------- 00635 00642 template<typename T> 00643 T elxSmooth(T iMin, T iMax, T iValue); 00644 00645 ExportedByMath double elxMidtone(double iX, double iMidtone); 00646 00647 //---------------------------------------------------------------------------- 00650 00653 ExportedByMath void elxRandomReset(); 00654 00655 ExportedByMath void elxRandomReset(int32 iSeed); 00656 00660 ExportedByMath int32 elxRandomSign(); 00661 00662 // Return a pseudorandom-number in range [0.0, 1.0] 00663 ExportedByMath double elxRandom(); 00664 00665 // Return a pseudorandom-number in range [0, iMax] 00666 template<typename T> 00667 T elxRandom(T iMax); 00668 00669 // Return a pseudorandom-number in range [iMin, iMax] 00670 template<typename T> 00671 T elxRandom(T iMin, T iMax); 00672 00674 00675 } // namespace Math 00676 } // namespace eLynx 00677 00678 #include "inl/Core.inl" 00679 #include "inl/MinMax.inl" 00680 #include "inl/Clamp.inl" 00681 #include "inl/Operator.inl" 00682 #include "inl/Normalize.inl" 00683 00684 #endif // __MathCore_h__ 00685 00686 /* 00687 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 00688 // Phase1: 1st sdk to be refactoring 00689 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 00690 00694 ExportedByMath const float& InfPosf(); 00695 00699 ExportedByMath const double& InfPosd(); 00700 00704 ExportedByMath const float& InfNegf(); 00705 00709 ExportedByMath const double& InfNegd(); 00710 00715 ExportedByMath bool IsNaN(float iVal); 00716 00721 ExportedByMath bool IsNaN(double iVal); 00722 00728 ExportedByMath bool IsFinite(float iVal); 00729 00735 ExportedByMath bool IsFinite(double iVal); 00737 00738 00739 //---------------------------------------------------------------------------- 00745 ExportedByMath const float& NaNf(); 00746 00750 ExportedByMath const double& NaNd(); 00751 00755 ExportedByMath const float& InfPosf(); 00756 00760 ExportedByMath const double& InfPosd(); 00761 00765 ExportedByMath const float& InfNegf(); 00766 00770 ExportedByMath const double& InfNegd(); 00771 00776 ExportedByMath bool IsNaN(float iVal); 00777 00782 ExportedByMath bool IsNaN(double iVal); 00783 00789 ExportedByMath bool IsFinite(float iVal); 00790 00796 ExportedByMath bool IsFinite(double iVal); 00798 00804 ExportedByMath float Lerpf(float iA, float iB, float iScalar); 00805 // linear interpolation 00806 inline F32 Lerpf(F32 iA, F32 iB, F32 iScalar) { return iA + iScalar*(iB-iA); } 00807 inline F64 Lerpd(F64 iA, F64 iB, F64 iScalar) { return iA + iScalar*(iB-iA); } 00808 */