2014-04-30 08:50:12 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-04-30 08:50:12 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-30 08:50:12 +08:00
|
|
|
Original file from GamePlay3D: http://gameplay3d.org
|
|
|
|
|
|
|
|
This file was modified to fit the cocos2d-x project
|
|
|
|
*/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "math/MathUtil.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <cpu-features.h>
|
2014-09-24 17:21:38 +08:00
|
|
|
#endif
|
|
|
|
|
2014-09-25 10:46:25 +08:00
|
|
|
//#define USE_NEON32 : neon 32 code will be used
|
|
|
|
//#define USE_NEON64 : neon 64 code will be used
|
|
|
|
//#define INCLUDE_NEON32 : neon 32 code included
|
|
|
|
//#define INCLUDE_NEON64 : neon 64 code included
|
|
|
|
//#define USE_SSE : SSE code used
|
|
|
|
//#define INCLUDE_SSE : SSE code included
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_IOS)
|
2021-12-25 10:04:45 +08:00
|
|
|
# if defined(__arm64__)
|
|
|
|
# define USE_NEON64
|
|
|
|
# define INCLUDE_NEON64
|
|
|
|
# elif defined(__ARM_NEON__)
|
|
|
|
# define USE_NEON32
|
|
|
|
# define INCLUDE_NEON32
|
|
|
|
# else
|
|
|
|
# endif
|
2022-07-15 19:17:01 +08:00
|
|
|
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2021-12-25 10:04:45 +08:00
|
|
|
# if defined(__arm64__) || defined(__aarch64__)
|
|
|
|
# define USE_NEON64
|
|
|
|
# define INCLUDE_NEON64
|
|
|
|
# elif defined(__ARM_NEON__)
|
|
|
|
# define INCLUDE_NEON32
|
|
|
|
# else
|
|
|
|
# endif
|
2014-09-25 10:46:25 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#if defined(__SSE__)
|
|
|
|
# define USE_SSE
|
|
|
|
# define INCLUDE_SSE
|
2014-09-25 10:46:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INCLUDE_NEON32
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "math/MathUtilNeon.inl"
|
2014-09-24 16:12:47 +08:00
|
|
|
#endif
|
|
|
|
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef INCLUDE_NEON64
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "math/MathUtilNeon64.inl"
|
2014-09-25 10:46:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INCLUDE_SSE
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "math/MathUtilSSE.inl"
|
2014-09-25 10:46:25 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "math/MathUtil.inl"
|
2014-09-24 16:12:47 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_MATH_BEGIN
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
void MathUtil::smooth(float* x, float target, float elapsedTime, float responseTime)
|
|
|
|
{
|
|
|
|
GP_ASSERT(x);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-02 10:33:07 +08:00
|
|
|
if (elapsedTime > 0)
|
|
|
|
{
|
|
|
|
*x += (target - *x) * elapsedTime / (elapsedTime + responseTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::smooth(float* x, float target, float elapsedTime, float riseTime, float fallTime)
|
|
|
|
{
|
|
|
|
GP_ASSERT(x);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-02 10:33:07 +08:00
|
|
|
if (elapsedTime > 0)
|
|
|
|
{
|
|
|
|
float delta = target - *x;
|
|
|
|
*x += delta * elapsedTime / (elapsedTime + (delta > 0 ? riseTime : fallTime));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:17:25 +08:00
|
|
|
float MathUtil::lerp(float from, float to, float alpha)
|
|
|
|
{
|
|
|
|
return from * (1.0f - alpha) + to * alpha;
|
|
|
|
}
|
|
|
|
|
2014-09-25 10:46:25 +08:00
|
|
|
bool MathUtil::isNeon32Enabled()
|
2014-09-24 17:21:38 +08:00
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 17:21:38 +08:00
|
|
|
return true;
|
2022-07-15 19:17:01 +08:00
|
|
|
#elif (defined(INCLUDE_NEON32) && (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID))
|
2014-09-25 16:09:28 +08:00
|
|
|
class AnrdoidNeonChecker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AnrdoidNeonChecker()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM &&
|
|
|
|
(android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0)
|
2014-09-25 16:09:28 +08:00
|
|
|
_isNeonEnabled = true;
|
|
|
|
else
|
|
|
|
_isNeonEnabled = false;
|
|
|
|
}
|
|
|
|
bool isNeonEnabled() const { return _isNeonEnabled; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-09-25 16:09:28 +08:00
|
|
|
private:
|
|
|
|
bool _isNeonEnabled;
|
|
|
|
};
|
|
|
|
static AnrdoidNeonChecker checker;
|
|
|
|
return checker.isNeonEnabled();
|
2014-09-24 17:21:38 +08:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-25 10:46:25 +08:00
|
|
|
bool MathUtil::isNeon64Enabled()
|
2014-09-24 17:21:38 +08:00
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON64
|
2014-09-24 17:21:38 +08:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-24 10:32:29 +08:00
|
|
|
void MathUtil::addMatrix(const float* m, float scalar, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::addMatrix(m, scalar, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::addMatrix(m, scalar, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::addMatrix(m, scalar, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::addMatrix(m, scalar, dst);
|
2014-09-24 10:32:29 +08:00
|
|
|
#else
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilC::addMatrix(m, scalar, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::addMatrix(const float* m1, const float* m2, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::addMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::addMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::addMatrix(m1, m2, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::addMatrix(m1, m2, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::addMatrix(m1, m2, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::subtractMatrix(const float* m1, const float* m2, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::subtractMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::subtractMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::subtractMatrix(m1, m2, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::subtractMatrix(m1, m2, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::subtractMatrix(m1, m2, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::multiplyMatrix(const float* m, float scalar, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::multiplyMatrix(m, scalar, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::multiplyMatrix(m, scalar, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::multiplyMatrix(m, scalar, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::multiplyMatrix(m, scalar, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::multiplyMatrix(m, scalar, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::multiplyMatrix(const float* m1, const float* m2, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::multiplyMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::multiplyMatrix(m1, m2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::multiplyMatrix(m1, m2, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::multiplyMatrix(m1, m2, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::multiplyMatrix(m1, m2, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::negateMatrix(const float* m, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::negateMatrix(m, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::negateMatrix(m, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::negateMatrix(m, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::negateMatrix(m, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::negateMatrix(m, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::transposeMatrix(const float* m, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::transposeMatrix(m, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::transposeMatrix(m, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::transposeMatrix(m, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::transposeMatrix(m, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::transposeMatrix(m, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::transformVec4(const float* m, float x, float y, float z, float w, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::transformVec4(m, x, y, z, w, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::transformVec4(m, x, y, z, w, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::transformVec4(m, x, y, z, w, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::transformVec4(m, x, y, z, w, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::transformVec4(m, x, y, z, w, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::transformVec4(const float* m, const float* v, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::transformVec4(m, v, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::transformVec4(m, v, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::transformVec4(m, v, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::transformVec4(m, v, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::transformVec4(m, v, dst);
|
|
|
|
#endif
|
2014-09-24 10:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtil::crossVec3(const float* v1, const float* v2, float* dst)
|
|
|
|
{
|
2014-09-25 10:46:25 +08:00
|
|
|
#ifdef USE_NEON32
|
2014-09-24 16:12:47 +08:00
|
|
|
MathUtilNeon::crossVec3(v1, v2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(USE_NEON64)
|
2014-09-25 10:53:44 +08:00
|
|
|
MathUtilNeon64::crossVec3(v1, v2, dst);
|
2021-12-25 10:04:45 +08:00
|
|
|
#elif defined(INCLUDE_NEON32)
|
|
|
|
if (isNeon32Enabled())
|
|
|
|
MathUtilNeon::crossVec3(v1, v2, dst);
|
|
|
|
else
|
|
|
|
MathUtilC::crossVec3(v1, v2, dst);
|
2014-09-24 16:12:47 +08:00
|
|
|
#else
|
|
|
|
MathUtilC::crossVec3(v1, v2, dst);
|
2014-09-24 10:32:29 +08:00
|
|
|
#endif
|
2014-09-24 11:06:07 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_MATH_END
|