mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Math tune cpp (#14526)
* fix problem, add missing functions for vec4 * add unit test for math lib, review geom lib, fix comment, clean code * revert changes on native math lib * fix typo, cancel deprecates * fix csm layer frustum usage * tune document * restore native changes * stage math * correct frustum * add more unit test * fix mistakes introduced in the last commit * add missing changes * update * update deprecated information, rename interfaces * fix operator priority issue * fix math library related issues
This commit is contained in:
@@ -21,13 +21,14 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#include "gtest/gtest.h"
|
||||
#include "cocos/math/Vec2.h"
|
||||
#include "cocos/math/Math.h"
|
||||
#include "cocos/math/MathUtil.h"
|
||||
#include "utils.h"
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include "cocos/math/Math.h"
|
||||
#include "cocos/math/MathUtil.h"
|
||||
#include "cocos/math/Utils.h"
|
||||
#include "cocos/math/Vec2.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "utils.h"
|
||||
|
||||
TEST(mathUtilsTest, test9) {
|
||||
// smooth
|
||||
@@ -41,4 +42,70 @@ TEST(mathUtilsTest, test9) {
|
||||
logLabel = "test the MathUtil lerp function";
|
||||
float res = cc::MathUtil::lerp(2, 15, 0.8);
|
||||
ExpectEq(IsEqualF(res, 12.3999996), true);
|
||||
|
||||
// equals
|
||||
logLabel = "test the MathUtil equals function";
|
||||
bool res1 = cc::mathutils::equals(2.0F, 2.0000001F);
|
||||
ExpectEq(res1, true);
|
||||
// approx
|
||||
logLabel = "test the MathUtil approx function";
|
||||
bool res2 = cc::mathutils::approx(2.0F, 2.0000001F);
|
||||
ExpectEq(res2, true);
|
||||
bool res3 = cc::mathutils::approx(2.0F, 2.0000001F, 0.000001F);
|
||||
ExpectEq(res3, true);
|
||||
// clamp
|
||||
logLabel = "test the MathUtil clamp function";
|
||||
float res4 = cc::mathutils::clamp(2.0F, 1.0F, 3.0F);
|
||||
ExpectEq(IsEqualF(res4, 2.0F), true);
|
||||
float res5 = cc::mathutils::clamp01(2.0F);
|
||||
ExpectEq(IsEqualF(res5, 1.0F), true);
|
||||
// lerp
|
||||
logLabel = "test the MathUtil lerp function";
|
||||
float res6 = cc::mathutils::lerp(2.0F, 3.0F, 0.5F);
|
||||
ExpectEq(IsEqualF(res6, 2.5F), true);
|
||||
// toRadian
|
||||
logLabel = "test the MathUtil toRadian function";
|
||||
float res7 = cc::mathutils::toRadian(180.0F);
|
||||
ExpectEq(cc::mathutils::equals(res7, 3.14159274F), true);
|
||||
// toDegree
|
||||
logLabel = "test the MathUtil toDegree function";
|
||||
float res8 = cc::mathutils::toDegree(3.14159274F);
|
||||
ExpectEq(cc::mathutils::equals(res8, 180.0F), true);
|
||||
// nextPow2
|
||||
logLabel = "test the MathUtil nextPow2 function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(0), 0), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(1), 1), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(2), 2), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(3), 4), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(4), 4), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(5), 8), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::nextPow2(31), 32), true);
|
||||
// repeat
|
||||
logLabel = "test the MathUtil repeat function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::repeat(0.0F, 1.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::repeat(1.0F, 1.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::repeat(2.0F, 1.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::repeat(3.0F, 1.0F), 0.0F), true);
|
||||
// pingPong
|
||||
logLabel = "test the MathUtil pingPong function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::pingPong(0.0F, 1.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::pingPong(1.0F, 1.0F), 1.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::pingPong(2.0F, 1.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::pingPong(0.5F, 1.0F), 0.5F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::pingPong(.5F, .5F), 0.5F), true);
|
||||
// inverseLerp
|
||||
logLabel = "test the MathUtil inverseLerp function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 0.0F), 0.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 1.0F), 1.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 0.5F), 0.5F), true);
|
||||
// absMaxComponent
|
||||
logLabel = "test the MathUtil absMaxComponent function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, 2, 3)), 3), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, 2, -3)), -3), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, -2, 3)), 3), true);
|
||||
// absMax
|
||||
logLabel = "test the MathUtil absMax function";
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMax(1.0F, 3.0F), 3.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMax(-1.0F, 3.0F), 3.0F), true);
|
||||
ExpectEq(IsEqualF(cc::mathutils::absMax(1.0F, -3.0F), -3.0F), true);
|
||||
}
|
||||
Reference in New Issue
Block a user