mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
@@ -672,6 +672,13 @@ export class Mat3 extends ValueType {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix layout
|
||||
* |m00 m03 m06|
|
||||
* |m01 m04 m07|
|
||||
* |m02 m05 m08|
|
||||
*/
|
||||
|
||||
/**
|
||||
* @en Value at column 0 row 0 of the matrix.
|
||||
* @zh 矩阵第 0 列第 0 行的元素。
|
||||
|
||||
@@ -1656,6 +1656,14 @@ export class Mat4 extends ValueType {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix layout
|
||||
* |m00 m04 m08 m12|
|
||||
* |m01 m05 m09 m13|
|
||||
* |m02 m06 m10 m14|
|
||||
* |m03 m07 m11 m15|
|
||||
*/
|
||||
|
||||
/**
|
||||
* @en Value at column 0 row 0 of the matrix.
|
||||
* @zh 矩阵第 0 列第 0 行的元素。
|
||||
|
||||
@@ -342,6 +342,13 @@ export class Quat extends ValueType {
|
||||
/**
|
||||
* @en Spherical quaternion interpolation with two control points
|
||||
* @zh 带两个控制点的四元数球面插值
|
||||
* @param out the receiving quaternion
|
||||
* @param a the first operand
|
||||
* @param b the second operand
|
||||
* @param c the third operand
|
||||
* @param d the fourth operand
|
||||
* @param t interpolation amount, in the range [0-1], between the two inputs
|
||||
* @returns out
|
||||
*/
|
||||
public static sqlerp<Out extends IQuatLike> (out: Out, a: Out, b: Out, c: Out, d: Out, t: number) {
|
||||
Quat.slerp(qt_1, a, d, t);
|
||||
|
||||
@@ -862,8 +862,6 @@ cocos_source_files(MODULE ccmath
|
||||
cocos/math/Math.h
|
||||
cocos/math/Math.cpp
|
||||
cocos/math/MathBase.h
|
||||
cocos/math/Vertex.cpp
|
||||
cocos/math/Vertex.h
|
||||
cocos/math/Mat3.cpp
|
||||
cocos/math/Mat3.h
|
||||
cocos/math/Mat4.cpp
|
||||
|
||||
@@ -80,10 +80,10 @@ Plane::Plane(float nx, float ny, float nz, float d) : Plane() {
|
||||
void Plane::transform(const Mat4 &mat) {
|
||||
Mat4 tempMat = mat.getInversed();
|
||||
tempMat.transpose();
|
||||
Vec4 tempVec4 = {n.x, n.y, n.z, d};
|
||||
Vec4 tempVec4 = {n.x, n.y, n.z, -d};
|
||||
tempMat.transformVector(&tempVec4);
|
||||
n.set(tempVec4.x, tempVec4.y, tempVec4.z);
|
||||
d = tempVec4.w;
|
||||
d = -tempVec4.w;
|
||||
}
|
||||
|
||||
// Define from 3 vertices.
|
||||
@@ -92,7 +92,7 @@ void Plane::define(const Vec3 &v0, const Vec3 &v1, const Vec3 &v2) {
|
||||
const Vec3 dist2 = v2 - v0;
|
||||
|
||||
Vec3 dist;
|
||||
Vec3::crossProduct(dist1, dist2, &dist);
|
||||
Vec3::cross(dist1, dist2, &dist);
|
||||
define(dist, v0);
|
||||
}
|
||||
// Define from a normal vector and a point on the plane.
|
||||
|
||||
@@ -116,27 +116,27 @@ void Sphere::transform(const Mat4 &m,
|
||||
out->_radius = _radius * mathutils::maxComponent(scale);
|
||||
}
|
||||
|
||||
int Sphere::interset(const Plane &plane) const {
|
||||
int Sphere::intersect(const Plane &plane) const {
|
||||
const float dot = plane.n.dot(_center);
|
||||
const float r = _radius * plane.n.length();
|
||||
if (dot + r < plane.d) {
|
||||
return -1;
|
||||
return -1; // Sphere is on the back of the plane
|
||||
}
|
||||
|
||||
if (dot - r > plane.d) {
|
||||
return 0;
|
||||
return 0; // Sphere is on the front of the plane
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 1; // intersect
|
||||
}
|
||||
|
||||
bool Sphere::interset(const Frustum &frustum) const {
|
||||
bool Sphere::intersect(const Frustum &frustum) const {
|
||||
const auto &planes = frustum.planes;
|
||||
const auto *self = this;
|
||||
return std::all_of(planes.begin(),
|
||||
planes.end(),
|
||||
// frustum plane normal points to the inside
|
||||
[self](const Plane *plane) { return self->interset(*plane) != -1; });
|
||||
[self](const Plane *plane) { return self->intersect(*plane) != -1; });
|
||||
}
|
||||
|
||||
void Sphere::mergePoint(const Vec3 &point) {
|
||||
@@ -184,24 +184,11 @@ void Sphere::mergeAABB(const AABB *aabb) {
|
||||
}
|
||||
|
||||
int Sphere::spherePlane(const Plane &plane) const {
|
||||
const auto dot = cc::Vec3::dot(plane.n, _center);
|
||||
const auto r = _radius * plane.n.length();
|
||||
if (dot + r < plane.d) {
|
||||
return -1;
|
||||
}
|
||||
if (dot - r > plane.d) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
return intersect(plane);
|
||||
}
|
||||
|
||||
bool Sphere::sphereFrustum(const Frustum &frustum) const {
|
||||
const auto &planes = frustum.planes;
|
||||
const auto *self = this;
|
||||
return std::all_of(planes.begin(),
|
||||
planes.end(),
|
||||
// frustum plane normal points to the inside
|
||||
[self](const Plane *plane) { return self->interset(*plane) != -1; });
|
||||
return intersect(frustum);
|
||||
}
|
||||
|
||||
void Sphere::mergeFrustum(const Frustum &frustum) {
|
||||
|
||||
@@ -135,8 +135,8 @@ public:
|
||||
inline void merge(const Vec3 &point) { mergePoint(point); }
|
||||
void merge(const ccstd::vector<Vec3> &points);
|
||||
inline void merge(const Frustum &frustum) { mergeFrustum(frustum); }
|
||||
bool interset(const Frustum &frustum) const;
|
||||
int interset(const Plane &plane) const;
|
||||
bool intersect(const Frustum &frustum) const;
|
||||
int intersect(const Plane &plane) const;
|
||||
int spherePlane(const Plane &plane) const;
|
||||
bool sphereFrustum(const Frustum &frustum) const;
|
||||
|
||||
|
||||
@@ -33,22 +33,15 @@
|
||||
#include "tetgen.h"
|
||||
#endif
|
||||
|
||||
#define FIX_TS_NATIVE_INCOMPATIBLE 0
|
||||
|
||||
namespace cc {
|
||||
namespace gi {
|
||||
|
||||
void CircumSphere::init(const Vec3 &p0, const Vec3 &p1, const Vec3 &p2, const Vec3 &p3) {
|
||||
// calculate circumsphere of 4 points in R^3 space.
|
||||
#if FIX_TS_NATIVE_INCOMPATIBLE
|
||||
Mat3 mat(p1.x - p0.x, p1.y - p0.y, p1.z - p0.z,
|
||||
p2.x - p0.x, p2.y - p0.y, p2.z - p0.z,
|
||||
p3.x - p0.x, p3.y - p0.y, p3.z - p0.z);
|
||||
#else
|
||||
Mat3 mat(p1.x - p0.x, p2.x - p0.x, p3.x - p0.x,
|
||||
p1.y - p0.y, p2.y - p0.y, p3.y - p0.y,
|
||||
p1.z - p0.z, p2.z - p0.z, p3.z - p0.z);
|
||||
#endif
|
||||
|
||||
mat.inverse();
|
||||
mat.transpose();
|
||||
|
||||
@@ -380,17 +373,11 @@ void Delaunay::computeTetrahedronMatrix(Tetrahedron &tetrahedron) {
|
||||
const auto &p2 = _probes[tetrahedron.vertex2].position;
|
||||
const auto &p3 = _probes[tetrahedron.vertex3].position;
|
||||
|
||||
#if FIX_TS_NATIVE_INCOMPATIBLE
|
||||
tetrahedron.matrix.set(
|
||||
p0.x - p3.x, p1.x - p3.x, p2.x - p3.x,
|
||||
p0.y - p3.y, p1.y - p3.y, p2.y - p3.y,
|
||||
p0.z - p3.z, p1.z - p3.z, p2.z - p3.z);
|
||||
#else
|
||||
tetrahedron.matrix.set(
|
||||
p0.x - p3.x, p0.y - p3.y, p0.z - p3.z,
|
||||
p1.x - p3.x, p1.y - p3.y, p1.z - p3.z,
|
||||
p2.x - p3.x, p2.y - p3.y, p2.z - p3.z);
|
||||
#endif
|
||||
|
||||
tetrahedron.matrix.inverse();
|
||||
tetrahedron.matrix.transpose();
|
||||
}
|
||||
@@ -449,11 +436,7 @@ void Delaunay::computeOuterCellMatrix(Tetrahedron &tetrahedron) {
|
||||
}
|
||||
|
||||
// transpose the matrix
|
||||
#if FIX_TS_NATIVE_INCOMPATIBLE
|
||||
tetrahedron.matrix.set(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
|
||||
#else
|
||||
tetrahedron.matrix.set(m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8]);
|
||||
#endif
|
||||
|
||||
// last column of mat3x4
|
||||
tetrahedron.offset.set(m[9], m[10], m[11]);
|
||||
|
||||
@@ -71,7 +71,8 @@ Size Size::operator*(float a) const {
|
||||
|
||||
Size Size::operator/(float a) const {
|
||||
CC_ASSERT_NE(a, 0);
|
||||
return Size(this->width / a, this->height / a);
|
||||
float inv = 1.0F / a;
|
||||
return Size(this->width * inv, this->height * inv);
|
||||
}
|
||||
|
||||
void Size::setSize(float w, float h) {
|
||||
|
||||
+26
-14
@@ -38,9 +38,9 @@ Mat3::Mat3() {
|
||||
}
|
||||
|
||||
Mat3::Mat3(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22) {
|
||||
set(m00, m01, m02, m10, m11, m12, m20, m21, m22);
|
||||
float m03, float m04, float m05,
|
||||
float m06, float m07, float m08) {
|
||||
set(m00, m01, m02, m03, m04, m05, m06, m07, m08);
|
||||
}
|
||||
|
||||
Mat3::Mat3(const float *mat) {
|
||||
@@ -52,17 +52,17 @@ Mat3::Mat3(const Mat3 ©) {
|
||||
}
|
||||
|
||||
void Mat3::set(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22) {
|
||||
float m03, float m04, float m05,
|
||||
float m06, float m07, float m08) {
|
||||
m[0] = m00;
|
||||
m[1] = m10;
|
||||
m[2] = m20;
|
||||
m[3] = m01;
|
||||
m[4] = m11;
|
||||
m[5] = m21;
|
||||
m[6] = m02;
|
||||
m[7] = m12;
|
||||
m[8] = m22;
|
||||
m[1] = m01;
|
||||
m[2] = m02;
|
||||
m[3] = m03;
|
||||
m[4] = m04;
|
||||
m[5] = m05;
|
||||
m[6] = m06;
|
||||
m[7] = m07;
|
||||
m[8] = m08;
|
||||
}
|
||||
|
||||
void Mat3::set(const float *mat) {
|
||||
@@ -153,6 +153,10 @@ void Mat3::inverse() {
|
||||
|
||||
// Calculate the determinant
|
||||
float det = a00 * b01 + a01 * b11 + a02 * b21;
|
||||
if (det == 0.F) {
|
||||
set(0.F, 0.F, 0.F, 0.F, 0.F, 0.F, 0.F, 0.F, 0.F);
|
||||
return;
|
||||
}
|
||||
|
||||
det = 1.0F / det;
|
||||
m[0] = b01 * det;
|
||||
@@ -420,7 +424,15 @@ void Mat3::subtract(const Mat3 &a, const Mat3 &b, Mat3 *out) {
|
||||
}
|
||||
|
||||
bool Mat3::approxEquals(const Mat3 &v, float precision /* = CC_FLOAT_CMP_PRECISION */) const {
|
||||
return math::isEqualF(m[0], v.m[0], precision) && math::isEqualF(m[1], v.m[1], precision) && math::isEqualF(m[2], v.m[2], precision) && math::isEqualF(m[3], v.m[3], precision) && math::isEqualF(m[4], v.m[4], precision) && math::isEqualF(m[5], v.m[5], precision) && math::isEqualF(m[6], v.m[6], precision) && math::isEqualF(m[7], v.m[7], precision) && math::isEqualF(m[8], v.m[8], precision);
|
||||
return math::isEqualF(m[0], v.m[0], precision) &&
|
||||
math::isEqualF(m[1], v.m[1], precision) &&
|
||||
math::isEqualF(m[2], v.m[2], precision) &&
|
||||
math::isEqualF(m[3], v.m[3], precision) &&
|
||||
math::isEqualF(m[4], v.m[4], precision) &&
|
||||
math::isEqualF(m[5], v.m[5], precision) &&
|
||||
math::isEqualF(m[6], v.m[6], precision) &&
|
||||
math::isEqualF(m[7], v.m[7], precision) &&
|
||||
math::isEqualF(m[8], v.m[8], precision);
|
||||
}
|
||||
|
||||
const Mat3 Mat3::IDENTITY = Mat3(
|
||||
|
||||
+12
-28
@@ -44,7 +44,11 @@ class CC_DLL Mat3 {
|
||||
public:
|
||||
/**
|
||||
* Stores the columns of this 3x3 matrix.
|
||||
* */
|
||||
* matrix layout
|
||||
* |m[0] m[3] m[6]|
|
||||
* |m[1] m[4] m[7]|
|
||||
* |m[2] m[5] m[8]|
|
||||
*/
|
||||
float m[9];
|
||||
|
||||
/**
|
||||
@@ -58,21 +62,11 @@ public:
|
||||
Mat3();
|
||||
|
||||
/**
|
||||
* Constructs a matrix initialized to the specified value.
|
||||
*
|
||||
* @param m00 The first element of the first row.
|
||||
* @param m01 The second element of the first row.
|
||||
* @param m02 The third element of the first row.
|
||||
* @param m10 The first element of the second row.
|
||||
* @param m11 The second element of the second row.
|
||||
* @param m12 The third element of the second row.
|
||||
* @param m20 The first element of the third row.
|
||||
* @param m21 The second element of the third row.
|
||||
* @param m22 The third element of the third row.
|
||||
* Constructs a matrix initialized to the specified values which are column-major order.
|
||||
*/
|
||||
Mat3(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22);
|
||||
float m03, float m04, float m05,
|
||||
float m06, float m07, float m08);
|
||||
|
||||
/**
|
||||
* Creates a matrix initialized to the specified column-major array.
|
||||
@@ -119,21 +113,11 @@ public:
|
||||
~Mat3() = default;
|
||||
|
||||
/**
|
||||
* Sets the values of this matrix.
|
||||
*
|
||||
* @param m00 The first element of the first row.
|
||||
* @param m01 The second element of the first row.
|
||||
* @param m02 The third element of the first row.
|
||||
* @param m10 The first element of the second row.
|
||||
* @param m11 The second element of the second row.
|
||||
* @param m12 The third element of the second row.
|
||||
* @param m20 The first element of the third row.
|
||||
* @param m21 The second element of the third row.
|
||||
* @param m22 The third element of the third row.
|
||||
* Sets the values of this matrix which are column-major order.
|
||||
*/
|
||||
void set(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22);
|
||||
float m03, float m04, float m05,
|
||||
float m06, float m07, float m08);
|
||||
|
||||
/**
|
||||
* Sets the values of this matrix to those in the specified column-major array.
|
||||
@@ -170,7 +154,7 @@ public:
|
||||
void inverse();
|
||||
|
||||
/**
|
||||
* Calculates the adjugate of a matrix.
|
||||
* Calculates the adjoint matrix.
|
||||
*/
|
||||
static void adjoint(const Mat3 &mat, Mat3 *out);
|
||||
|
||||
|
||||
@@ -88,7 +88,12 @@ public:
|
||||
// }
|
||||
/**
|
||||
* Stores the columns of this 4x4 matrix.
|
||||
* */
|
||||
* matrix layout
|
||||
* |m[0] m[4] m[8] m[12]|
|
||||
* |m[1] m[5] m[9] m[13]|
|
||||
* |m[2] m[6] m[10] m[14]|
|
||||
* |m[3] m[7] m[11] m[15]|
|
||||
*/
|
||||
#ifdef __SSE__
|
||||
union {
|
||||
__m128 col[4];
|
||||
|
||||
@@ -66,7 +66,7 @@ inline bool isPowerOfTwo(T n) {
|
||||
}
|
||||
|
||||
inline bool isEqualF(float lhs, float rhs, float precision = 0.000001F) {
|
||||
return (std::fabs(lhs - rhs) < precision);
|
||||
return (std::fabs(lhs - rhs) <= precision);
|
||||
}
|
||||
|
||||
inline bool isNotEqualF(float lhs, float rhs, float precision = 0.000001F) {
|
||||
|
||||
@@ -101,7 +101,6 @@ void Quaternion::conjugate() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
//w = w;
|
||||
}
|
||||
|
||||
Quaternion Quaternion::getConjugated() const {
|
||||
@@ -110,29 +109,14 @@ Quaternion Quaternion::getConjugated() const {
|
||||
return q;
|
||||
}
|
||||
|
||||
bool Quaternion::inverse() {
|
||||
float n = x * x + y * y + z * z + w * w;
|
||||
if (n == 1.F) {
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
//w = w;
|
||||
void Quaternion::inverse() {
|
||||
float dot = x * x + y * y + z * z + w * w;
|
||||
float invDot = dot > 0.F ? 1.F / dot : 0.F;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Too close to zero.
|
||||
if (n < 0.000001F) {
|
||||
return false;
|
||||
}
|
||||
|
||||
n = 1.F / n;
|
||||
x = -x * n;
|
||||
y = -y * n;
|
||||
z = -z * n;
|
||||
w = w * n;
|
||||
|
||||
return true;
|
||||
x = -x * invDot;
|
||||
y = -y * invDot;
|
||||
z = -z * invDot;
|
||||
w = w * invDot;
|
||||
}
|
||||
|
||||
Quaternion Quaternion::getInversed() const {
|
||||
@@ -160,24 +144,19 @@ void Quaternion::multiply(const Quaternion &q1, const Quaternion &q2, Quaternion
|
||||
}
|
||||
|
||||
void Quaternion::normalize() {
|
||||
float n = x * x + y * y + z * z + w * w;
|
||||
|
||||
// Already normalized.
|
||||
if (n == 1.F) {
|
||||
return;
|
||||
float len = x * x + y * y + z * z + w * w;
|
||||
if (len > 0.F) {
|
||||
len = 1.F / sqrt(len);
|
||||
x *= len;
|
||||
y *= len;
|
||||
z *= len;
|
||||
w *= len;
|
||||
} else {
|
||||
x = 0.F;
|
||||
y = 0.F;
|
||||
z = 0.F;
|
||||
w = 0.F;
|
||||
}
|
||||
|
||||
n = sqrt(n);
|
||||
// Too close to zero.
|
||||
if (n < 0.000001F) {
|
||||
return;
|
||||
}
|
||||
|
||||
n = 1.F / n;
|
||||
x *= n;
|
||||
y *= n;
|
||||
z *= n;
|
||||
w *= n;
|
||||
}
|
||||
|
||||
Quaternion Quaternion::getNormalized() const {
|
||||
@@ -240,26 +219,60 @@ void Quaternion::lerp(const Quaternion &q1, const Quaternion &q2, float t, Quate
|
||||
CC_ASSERT(dst);
|
||||
CC_ASSERT(!(t < 0.F || t > 1.F));
|
||||
|
||||
if (t == 0.F) {
|
||||
memcpy(dst, &q1, sizeof(float) * 4);
|
||||
return;
|
||||
}
|
||||
if (t == 1.F) {
|
||||
memcpy(dst, &q2, sizeof(float) * 4);
|
||||
return;
|
||||
}
|
||||
|
||||
float t1 = 1.F - t;
|
||||
|
||||
dst->x = t1 * q1.x + t * q2.x;
|
||||
dst->y = t1 * q1.y + t * q2.y;
|
||||
dst->z = t1 * q1.z + t * q2.z;
|
||||
dst->w = t1 * q1.w + t * q2.w;
|
||||
}
|
||||
|
||||
void Quaternion::slerp(const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst) {
|
||||
CC_ASSERT(dst);
|
||||
slerp(q1.x, q1.y, q1.z, q1.w, q2.x, q2.y, q2.z, q2.w, t, &dst->x, &dst->y, &dst->z, &dst->w);
|
||||
void Quaternion::slerp(const Quaternion &a, const Quaternion &b, float t, Quaternion *dst) {
|
||||
// benchmarks: http://jsperf.com/quaternion-slerp-implementations
|
||||
|
||||
float scale0 = 0;
|
||||
float scale1 = 0;
|
||||
float bx = b.x;
|
||||
float by = b.y;
|
||||
float bz = b.z;
|
||||
float bw = b.w;
|
||||
|
||||
// calc cosine
|
||||
float cosom = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
// adjust signs (if necessary)
|
||||
if (cosom < 0.F) {
|
||||
cosom = -cosom;
|
||||
bx = -bx;
|
||||
by = -by;
|
||||
bz = -bz;
|
||||
bw = -bw;
|
||||
}
|
||||
// calculate coefficients
|
||||
if ((1.F - cosom) > 0.000001F) {
|
||||
// standard case (slerp)
|
||||
const float omega = acos(cosom);
|
||||
const float sinom = sinf(omega);
|
||||
scale0 = sinf((1.0F - t) * omega) / sinom;
|
||||
scale1 = sinf(t * omega) / sinom;
|
||||
} else {
|
||||
// "from" and "to" quaternions are very close
|
||||
// ... so we can do a linear interpolation
|
||||
scale0 = 1.0F - t;
|
||||
scale1 = t;
|
||||
}
|
||||
// calculate final values
|
||||
dst->x = scale0 * a.x + scale1 * bx;
|
||||
dst->y = scale0 * a.y + scale1 * by;
|
||||
dst->z = scale0 * a.z + scale1 * bz;
|
||||
dst->w = scale0 * a.w + scale1 * bw;
|
||||
}
|
||||
|
||||
void Quaternion::sqlerp(const Quaternion &a, const Quaternion &b, const Quaternion &c, const Quaternion &d, float t, Quaternion *dst) {
|
||||
Quaternion q1(0.F, 0.F, 0.F, 1.F);
|
||||
Quaternion q2(0.F, 0.F, 0.F, 1.F);
|
||||
|
||||
slerp(a, d, t, &q1);
|
||||
slerp(b, c, t, &q2);
|
||||
slerp(q1, q2, 2.F * t * (1.F - t), dst);
|
||||
}
|
||||
|
||||
void Quaternion::squad(const Quaternion &q1, const Quaternion &q2, const Quaternion &s1, const Quaternion &s2, float t, Quaternion *dst) {
|
||||
@@ -317,7 +330,7 @@ void Quaternion::toEuler(const Quaternion &q, bool outerZ, Vec3 *out) {
|
||||
float r2d = 180.F / math::PI;
|
||||
if (test > 0.499999) {
|
||||
bank = 0;
|
||||
heading = -2 * atan2(x, w) * r2d;
|
||||
heading = 2 * atan2(x, w) * r2d;
|
||||
attitude = 90;
|
||||
} else if (test < -0.499999) {
|
||||
bank = 0;
|
||||
@@ -344,43 +357,43 @@ void Quaternion::toEuler(const Quaternion &q, bool outerZ, Vec3 *out) {
|
||||
void Quaternion::fromMat3(const Mat3 &m, Quaternion *out) {
|
||||
CC_ASSERT(out);
|
||||
float m00 = m.m[0];
|
||||
float m03 = m.m[1];
|
||||
float m06 = m.m[2];
|
||||
float m10 = m.m[1];
|
||||
float m20 = m.m[2];
|
||||
float m01 = m.m[3];
|
||||
float m04 = m.m[4];
|
||||
float m07 = m.m[5];
|
||||
float m11 = m.m[4];
|
||||
float m21 = m.m[5];
|
||||
float m02 = m.m[6];
|
||||
float m05 = m.m[7];
|
||||
float m08 = m.m[8];
|
||||
float trace = m00 + m04 + m08;
|
||||
if (trace > 0) {
|
||||
float m12 = m.m[7];
|
||||
float m22 = m.m[8];
|
||||
float trace = m00 + m11 + m22;
|
||||
|
||||
if (trace > 0.F) {
|
||||
const float s = 0.5F / sqrtf(trace + 1.F);
|
||||
out->w = 0.25F / s;
|
||||
out->x = (m05 - m07) * s;
|
||||
out->y = (m06 - m02) * s;
|
||||
out->z = (m01 - m03) * s;
|
||||
} else if ((m00 > m04) && (m00 > m08)) {
|
||||
//m00 - m04 - m08 consistent with ts engine, otherwise y-axis rotation greater than 90 degrees will not get the correct result
|
||||
const float s = 2.F * sqrtf(1.F + m00 - m04 - m08);
|
||||
out->x = (m21 - m12) * s;
|
||||
out->y = (m02 - m20) * s;
|
||||
out->z = (m10 - m01) * s;
|
||||
} else if ((m00 > m11) && (m00 > m22)) {
|
||||
const float s = 0.5F / sqrtf(1.F + m00 - m11 - m22);
|
||||
|
||||
out->w = (m05 - m07) / s;
|
||||
out->x = 0.25F * s;
|
||||
out->y = (m03 + m01) / s;
|
||||
out->z = (m06 + m02) / s;
|
||||
} else if (m04 > m08) {
|
||||
const float s = 2.F * sqrtf(1.F + m04 - m00 - m08);
|
||||
out->w = (m21 - m12) * s;
|
||||
out->x = 0.25F / s;
|
||||
out->y = (m01 + m10) * s;
|
||||
out->z = (m02 + m20) * s;
|
||||
} else if (m11 > m22) {
|
||||
const float s = 0.5F / sqrtf(1.F + m11 - m00 - m22);
|
||||
|
||||
out->w = (m06 - m02) / s;
|
||||
out->x = (m03 + m01) / s;
|
||||
out->y = 0.25F * s;
|
||||
out->z = (m07 + m05) / s;
|
||||
out->w = (m02 - m20) * s;
|
||||
out->x = (m02 + m10) * s;
|
||||
out->y = 0.25F / s;
|
||||
out->z = (m12 + m21) * s;
|
||||
} else {
|
||||
const float s = 2.F * sqrtf(1.F + m08 - m00 - m04);
|
||||
const float s = 0.5F / sqrtf(1.F + m22 - m00 - m11);
|
||||
|
||||
out->w = (m01 - m03) / s;
|
||||
out->x = (m06 + m02) / s;
|
||||
out->y = (m07 + m05) / s;
|
||||
out->z = 0.25F * s;
|
||||
out->w = (m10 - m01) * s;
|
||||
out->x = (m02 + m20) * s;
|
||||
out->y = (m12 + m21) * s;
|
||||
out->z = 0.25F / s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class Mat3;
|
||||
/**
|
||||
* Defines a 4-element quaternion that represents the orientation of an object in space.
|
||||
*
|
||||
* Quaternions are typically used as a replacement for euler angles and rotation matrices as a way to achieve smooth interpolation and avoid gimbal lock.
|
||||
* Quaternions are typically used as a replacement for Euler angles and rotation matrices as a way to achieve smooth interpolation and avoid gimbal lock.
|
||||
*
|
||||
* Note that this quaternion class does not automatically keep the quaternion normalized. Therefore, care must be taken to normalize the quaternion when necessary, by calling the normalize method.
|
||||
* This class provides three methods for doing quaternion interpolation: lerp, slerp, and squad.
|
||||
@@ -64,9 +64,6 @@ class Mat3;
|
||||
* For the point p = (1.0, 1.0, 1.0), the following figures show the trajectories of p using lerp, slerp, and squad.
|
||||
*/
|
||||
class CC_DLL Quaternion {
|
||||
friend class Curve;
|
||||
friend class Transform;
|
||||
|
||||
public:
|
||||
/**
|
||||
* The x-value of the quaternion's vector component.
|
||||
@@ -206,10 +203,8 @@ public:
|
||||
* when the quaternion is unit-length. For this reason, it is more
|
||||
* efficient to use the conjugate method directly when you know your
|
||||
* quaternion is already unit-length.
|
||||
*
|
||||
* @return true if the inverse can be computed, false otherwise.
|
||||
*/
|
||||
bool inverse();
|
||||
void inverse();
|
||||
|
||||
/**
|
||||
* Gets the inverse of this quaternion.
|
||||
@@ -346,7 +341,20 @@ public:
|
||||
* @param t The interpolation coefficient.
|
||||
* @param dst A quaternion to store the result in.
|
||||
*/
|
||||
static void slerp(const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst);
|
||||
static void slerp(const Quaternion &a, const Quaternion &b, float t, Quaternion *dst);
|
||||
|
||||
/**
|
||||
* @en Spherical quaternion interpolation with two control points
|
||||
*
|
||||
* @param out the receiving quaternion
|
||||
* @param a the first operand
|
||||
* @param b the second operand
|
||||
* @param c the third operand
|
||||
* @param d the fourth operand
|
||||
* @param t interpolation amount, in the range [0-1], between the two inputs
|
||||
* @returns out
|
||||
*/
|
||||
static void sqlerp(const Quaternion &a, const Quaternion &b, const Quaternion &c, const Quaternion &d, float t, Quaternion *dst);
|
||||
|
||||
/**
|
||||
* Interpolates over a series of quaternions using spherical spline interpolation.
|
||||
@@ -365,6 +373,9 @@ public:
|
||||
* @param t The interpolation coefficient.
|
||||
* @param dst A quaternion to store the result in.
|
||||
*/
|
||||
/**
|
||||
* @deprecated since v3.8.0 please use [[sqlerp]] instead
|
||||
*/
|
||||
static void squad(const Quaternion &q1, const Quaternion &q2, const Quaternion &s1, const Quaternion &s2, float t, Quaternion *dst);
|
||||
|
||||
/**
|
||||
@@ -377,13 +388,6 @@ public:
|
||||
*/
|
||||
inline const Quaternion operator*(const Quaternion &q) const;
|
||||
|
||||
/**
|
||||
* Calculates the quaternion product of this quaternion with the given vec3.
|
||||
* @param v The vec3 to multiply.
|
||||
* @return The vec3 product.
|
||||
*/
|
||||
inline Vec3 operator*(const Vec3 &v) const;
|
||||
|
||||
/**
|
||||
* Multiplies this quaternion with the given quaternion.
|
||||
*
|
||||
@@ -427,8 +431,14 @@ private:
|
||||
* @param dstz A pointer to store the z component of the slerp in.
|
||||
* @param dstw A pointer to store the w component of the slerp in.
|
||||
*/
|
||||
/**
|
||||
* @deprecated since v3.8.0
|
||||
*/
|
||||
static void slerp(float q1x, float q1y, float q1z, float q1w, float q2x, float q2y, float q2z, float q2w, float t, float *dstx, float *dsty, float *dstz, float *dstw);
|
||||
|
||||
/**
|
||||
* @deprecated since v3.8.0
|
||||
*/
|
||||
static void slerpForSquad(const Quaternion &q1, const Quaternion &q2, float t, Quaternion *dst);
|
||||
};
|
||||
|
||||
|
||||
@@ -24,30 +24,15 @@
|
||||
|
||||
NS_CC_MATH_BEGIN
|
||||
|
||||
inline const Quaternion Quaternion::operator*(const Quaternion& q) const
|
||||
{
|
||||
inline const Quaternion Quaternion::operator*(const Quaternion& q) const {
|
||||
Quaternion result(*this);
|
||||
result.multiply(q);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Quaternion& Quaternion::operator*=(const Quaternion& q)
|
||||
{
|
||||
inline Quaternion& Quaternion::operator*=(const Quaternion& q) {
|
||||
multiply(q);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Vec3 Quaternion::operator*(const Vec3& v) const
|
||||
{
|
||||
Vec3 uv, uuv;
|
||||
Vec3 qvec(x, y, z);
|
||||
Vec3::cross(qvec, v, &uv);
|
||||
Vec3::cross(qvec, uv, &uuv);
|
||||
|
||||
uv *= (2.0f * w);
|
||||
uuv *= 2.0f;
|
||||
|
||||
return v + uv + uuv;
|
||||
}
|
||||
|
||||
NS_CC_MATH_END
|
||||
|
||||
@@ -42,14 +42,14 @@ float random() {
|
||||
return uniformReal(rd);
|
||||
}
|
||||
|
||||
Vec3ElementType absMaxComponent(const Vec3 &v) {
|
||||
float absMaxComponent(const Vec3 &v) {
|
||||
return absMax(absMax(v.x, v.y), v.z);
|
||||
}
|
||||
|
||||
Vec3ElementType maxComponent(const Vec3 &v) {
|
||||
float maxComponent(const Vec3 &v) {
|
||||
return std::max(std::max(v.x, v.y), v.z);
|
||||
}
|
||||
|
||||
} // namespace mathutils
|
||||
|
||||
} // namespace cc
|
||||
} // namespace cc
|
||||
|
||||
@@ -264,15 +264,14 @@ auto inverseLerp(T from, T to, T value) {
|
||||
return (value - from) / (to - from);
|
||||
}
|
||||
|
||||
using Vec3ElementType = decltype(static_cast<Vec3 *>(nullptr)->x);
|
||||
/**
|
||||
* @zh 对所有分量的绝对值进行比较大小,返回绝对值最大的分量。
|
||||
* @param v 类 Vec3 结构
|
||||
* @returns 绝对值最大的分量
|
||||
*/
|
||||
Vec3ElementType absMaxComponent(const Vec3 &v);
|
||||
float absMaxComponent(const Vec3 &v);
|
||||
|
||||
Vec3ElementType maxComponent(const Vec3 &v);
|
||||
float maxComponent(const Vec3 &v);
|
||||
|
||||
/**
|
||||
* @zh 对 a b 的绝对值进行比较大小,返回绝对值最大的值。
|
||||
|
||||
+39
-57
@@ -36,22 +36,22 @@ bool isOneDimensionSegmentOverlap(float a, float b, float c, float d, float *s,
|
||||
if (abmax < cdmin || cdmax < abmin) {
|
||||
// abmin->abmax->cdmin->cdmax or cdmin->cdmax->abmin->abmax
|
||||
return false;
|
||||
} else {
|
||||
if (abmin >= cdmin && abmin <= cdmax) {
|
||||
// cdmin->abmin->cdmax->abmax or cdmin->abmin->abmax->cdmax
|
||||
if (s != nullptr) *s = abmin;
|
||||
if (e != nullptr) *e = cdmax < abmax ? cdmax : abmax;
|
||||
} else if (abmax >= cdmin && abmax <= cdmax) {
|
||||
// ABmin->CDmin->ABmax->CDmax
|
||||
if (s != nullptr) *s = cdmin;
|
||||
if (e != nullptr) *e = abmax;
|
||||
} else {
|
||||
// ABmin->CDmin->CDmax->ABmax
|
||||
if (s != nullptr) *s = cdmin;
|
||||
if (e != nullptr) *e = cdmax;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (abmin >= cdmin && abmin <= cdmax) {
|
||||
// cdmin->abmin->cdmax->abmax or cdmin->abmin->abmax->cdmax
|
||||
if (s != nullptr) *s = abmin;
|
||||
if (e != nullptr) *e = cdmax < abmax ? cdmax : abmax;
|
||||
} else if (abmax >= cdmin && abmax <= cdmax) {
|
||||
// ABmin->CDmin->ABmax->CDmax
|
||||
if (s != nullptr) *s = cdmin;
|
||||
if (e != nullptr) *e = abmax;
|
||||
} else {
|
||||
// ABmin->CDmin->CDmax->ABmax
|
||||
if (s != nullptr) *s = cdmin;
|
||||
if (e != nullptr) *e = cdmax;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// cross product of 2 vector. a->b X c->d
|
||||
@@ -60,8 +60,17 @@ float crossProduct2Vector(const Vec2 &a, const Vec2 &b, const Vec2 &c, const Vec
|
||||
}
|
||||
|
||||
float Vec2::angle(const Vec2 &v1, const Vec2 &v2) {
|
||||
float dz = v1.x * v2.y - v1.y * v2.x;
|
||||
return atan2f(fabsf(dz) + MATH_FLOAT_SMALL, dot(v1, v2));
|
||||
const auto magSqr1 = v1.x * v1.x + v1.y * v1.y;
|
||||
const auto magSqr2 = v2.x * v2.x + v2.y * v2.y;
|
||||
|
||||
if (magSqr1 == 0.0F || magSqr2 == 0.0F) {
|
||||
return 0.0F;
|
||||
}
|
||||
|
||||
const auto dot = v1.x * v2.x + v1.y * v2.y;
|
||||
auto cosine = dot / (sqrt(magSqr1 * magSqr2));
|
||||
cosine = clampf(cosine, -1.0, 1.0);
|
||||
return acos(cosine);
|
||||
}
|
||||
|
||||
void Vec2::add(const Vec2 &v1, const Vec2 &v2, Vec2 *dst) {
|
||||
@@ -130,21 +139,12 @@ float Vec2::length() const {
|
||||
}
|
||||
|
||||
void Vec2::normalize() {
|
||||
float n = x * x + y * y;
|
||||
// Already normalized.
|
||||
if (n == 1.0F) {
|
||||
return;
|
||||
float len = x * x + y * y;
|
||||
if (len > 0.0F) {
|
||||
len = 1.0F / std::sqrt(len);
|
||||
x *= len;
|
||||
y *= len;
|
||||
}
|
||||
|
||||
n = std::sqrt(n);
|
||||
// Too close to zero.
|
||||
if (n < MATH_TOLERANCE) {
|
||||
return;
|
||||
}
|
||||
|
||||
n = 1.0F / n;
|
||||
x *= n;
|
||||
y *= n;
|
||||
}
|
||||
|
||||
Vec2 Vec2::getNormalized() const {
|
||||
@@ -199,13 +199,7 @@ bool Vec2::fuzzyEquals(const Vec2 &b, float var) const {
|
||||
}
|
||||
|
||||
float Vec2::getAngle(const Vec2 &other) const {
|
||||
Vec2 a2 = getNormalized();
|
||||
Vec2 b2 = other.getNormalized();
|
||||
float angle = atan2f(a2.cross(b2), a2.dot(b2));
|
||||
if (std::abs(angle) < FLT_EPSILON) {
|
||||
return 0.F;
|
||||
}
|
||||
return angle;
|
||||
return Vec2::angle(*this, other);
|
||||
}
|
||||
|
||||
Vec2 Vec2::rotateByAngle(const Vec2 &pivot, float angle) const {
|
||||
@@ -242,11 +236,7 @@ bool Vec2::isLineParallel(const Vec2 &a, const Vec2 &b,
|
||||
|
||||
if (crossProduct2Vector(a, b, c, d) == 0) {
|
||||
// line overlap
|
||||
if (crossProduct2Vector(c, d, c, a) == 0 || crossProduct2Vector(a, b, c, a) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !(crossProduct2Vector(c, d, c, a) == 0 || crossProduct2Vector(a, b, c, a) == 0);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -259,12 +249,7 @@ bool Vec2::isLineOverlap(const Vec2 &a, const Vec2 &b,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (crossProduct2Vector(a, b, c, d) == 0 &&
|
||||
(crossProduct2Vector(c, d, c, a) == 0 || crossProduct2Vector(a, b, c, a) == 0)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return (crossProduct2Vector(a, b, c, d) == 0 && (crossProduct2Vector(c, d, c, a) == 0 || crossProduct2Vector(a, b, c, a) == 0));
|
||||
}
|
||||
|
||||
bool Vec2::isSegmentOverlap(const Vec2 &a, const Vec2 &b, const Vec2 &c, const Vec2 &d, Vec2 *s, Vec2 *e) {
|
||||
@@ -277,18 +262,15 @@ bool Vec2::isSegmentOverlap(const Vec2 &a, const Vec2 &b, const Vec2 &c, const V
|
||||
}
|
||||
|
||||
bool Vec2::isSegmentIntersect(const Vec2 &a, const Vec2 &b, const Vec2 &c, const Vec2 &d) {
|
||||
float s, t;
|
||||
float s;
|
||||
float t;
|
||||
|
||||
if (isLineIntersect(a, b, c, d, &s, &t) &&
|
||||
(s >= 0.0F && s <= 1.0F && t >= 0.0F && t <= 1.0F)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return (isLineIntersect(a, b, c, d, &s, &t) && (s >= 0.0F && s <= 1.0F && t >= 0.0F && t <= 1.0F));
|
||||
}
|
||||
|
||||
Vec2 Vec2::getIntersectPoint(const Vec2 &a, const Vec2 &b, const Vec2 &c, const Vec2 &d) {
|
||||
float s, t;
|
||||
float s;
|
||||
float t;
|
||||
|
||||
if (isLineIntersect(a, b, c, d, &s, &t)) {
|
||||
// Vec2 of intersection
|
||||
|
||||
@@ -461,7 +461,7 @@ public:
|
||||
/** @returns if points have fuzzy equality which means equal with some degree of variance.
|
||||
@since v2.1.4
|
||||
*/
|
||||
bool fuzzyEquals(const Vec2 &target, float variance) const;
|
||||
bool fuzzyEquals(const Vec2 &b, float var) const;
|
||||
|
||||
/** Calculates distance between point an origin
|
||||
@return float
|
||||
@@ -510,6 +510,7 @@ public:
|
||||
/** Calculates cross product of two points.
|
||||
@return float
|
||||
@since v2.1.4
|
||||
@deprecated since v3.8.0, There is no physical meaning.
|
||||
*/
|
||||
inline float cross(const Vec2 &other) const {
|
||||
return x * other.y - y * other.x;
|
||||
@@ -607,10 +608,10 @@ public:
|
||||
}
|
||||
|
||||
/** A general line-line intersection test
|
||||
@param a the startpoint for the first line L1 = (a - b)
|
||||
@param b the endpoint for the first line L1 = (a - b)
|
||||
@param c the startpoint for the second line L2 = (c - d)
|
||||
@param d the endpoint for the second line L2 = (c - d)
|
||||
@param a the start point for the first line L1 = (a - b)
|
||||
@param b the end point for the first line L1 = (a - b)
|
||||
@param c the start point for the second line L2 = (c - d)
|
||||
@param d the end point for the second line L2 = (c - d)
|
||||
@param s the range for a hitpoint in L1 (p = a + s*(b - a))
|
||||
@param t the range for a hitpoint in L2 (p = c + t*(d - c))
|
||||
@return whether these two lines intersects.
|
||||
@@ -618,7 +619,7 @@ public:
|
||||
Note that to truly test intersection for segments we have to make
|
||||
sure that s & t lie within [0..1] and for rays, make sure s & t > 0
|
||||
the hit point is c + t * (d - c);
|
||||
the hit point also is a + b * (b - a);
|
||||
the hit point also is a + s * (b - a);
|
||||
@since 3.0
|
||||
*/
|
||||
static bool isLineIntersect(const Vec2 &a, const Vec2 &b,
|
||||
|
||||
+42
-79
@@ -25,210 +25,173 @@
|
||||
NS_CC_MATH_BEGIN
|
||||
|
||||
inline Vec2::Vec2()
|
||||
: x(0.0f), y(0.0f)
|
||||
{
|
||||
: x(0.0f), y(0.0f) {
|
||||
}
|
||||
|
||||
inline Vec2::Vec2(float xx, float yy)
|
||||
: x(xx), y(yy)
|
||||
{
|
||||
: x(xx), y(yy) {
|
||||
}
|
||||
|
||||
inline Vec2::Vec2(const float* array)
|
||||
{
|
||||
inline Vec2::Vec2(const float* array) {
|
||||
set(array);
|
||||
}
|
||||
|
||||
inline Vec2::Vec2(const Vec2& p1, const Vec2& p2)
|
||||
{
|
||||
inline Vec2::Vec2(const Vec2& p1, const Vec2& p2) {
|
||||
set(p1, p2);
|
||||
}
|
||||
|
||||
inline Vec2::Vec2(const Vec2& copy)
|
||||
{
|
||||
inline Vec2::Vec2(const Vec2& copy) {
|
||||
set(copy);
|
||||
}
|
||||
|
||||
inline Vec2::~Vec2()
|
||||
{
|
||||
inline Vec2::~Vec2() {
|
||||
}
|
||||
|
||||
inline bool Vec2::isZero() const
|
||||
{
|
||||
inline bool Vec2::isZero() const {
|
||||
return x == 0.0f && y == 0.0f;
|
||||
}
|
||||
|
||||
bool Vec2::isOne() const
|
||||
{
|
||||
bool Vec2::isOne() const {
|
||||
return x == 1.0f && y == 1.0f;
|
||||
}
|
||||
|
||||
inline void Vec2::add(const Vec2& v)
|
||||
{
|
||||
inline void Vec2::add(const Vec2& v) {
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
}
|
||||
|
||||
inline float Vec2::distanceSquared(const Vec2& v) const
|
||||
{
|
||||
inline float Vec2::distanceSquared(const Vec2& v) const {
|
||||
float dx = v.x - x;
|
||||
float dy = v.y - y;
|
||||
return (dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
inline float Vec2::dot(const Vec2& v) const
|
||||
{
|
||||
inline float Vec2::dot(const Vec2& v) const {
|
||||
return (x * v.x + y * v.y);
|
||||
}
|
||||
|
||||
inline float Vec2::lengthSquared() const
|
||||
{
|
||||
inline float Vec2::lengthSquared() const {
|
||||
return (x * x + y * y);
|
||||
}
|
||||
|
||||
inline void Vec2::negate()
|
||||
{
|
||||
inline void Vec2::negate() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
}
|
||||
|
||||
inline void Vec2::scale(float scalar)
|
||||
{
|
||||
inline void Vec2::scale(float scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
}
|
||||
|
||||
inline void Vec2::scale(const Vec2& scale)
|
||||
{
|
||||
inline void Vec2::scale(const Vec2& scale) {
|
||||
x *= scale.x;
|
||||
y *= scale.y;
|
||||
}
|
||||
|
||||
inline void Vec2::set(float xx, float yy)
|
||||
{
|
||||
inline void Vec2::set(float xx, float yy) {
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
}
|
||||
|
||||
inline void Vec2::set(const Vec2& v)
|
||||
{
|
||||
inline void Vec2::set(const Vec2& v) {
|
||||
this->x = v.x;
|
||||
this->y = v.y;
|
||||
}
|
||||
|
||||
inline void Vec2::set(const Vec2& p1, const Vec2& p2)
|
||||
{
|
||||
inline void Vec2::set(const Vec2& p1, const Vec2& p2) {
|
||||
x = p2.x - p1.x;
|
||||
y = p2.y - p1.y;
|
||||
}
|
||||
|
||||
void Vec2::setZero()
|
||||
{
|
||||
void Vec2::setZero() {
|
||||
x = y = 0.0f;
|
||||
}
|
||||
|
||||
inline void Vec2::subtract(const Vec2& v)
|
||||
{
|
||||
inline void Vec2::subtract(const Vec2& v) {
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
}
|
||||
|
||||
inline void Vec2::smooth(const Vec2& target, float elapsedTime, float responseTime)
|
||||
{
|
||||
if (elapsedTime > 0)
|
||||
{
|
||||
inline void Vec2::smooth(const Vec2& target, float elapsedTime, float responseTime) {
|
||||
if (elapsedTime > 0) {
|
||||
*this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
|
||||
}
|
||||
}
|
||||
|
||||
inline const Vec2 Vec2::operator+(const Vec2& v) const
|
||||
{
|
||||
inline const Vec2 Vec2::operator+(const Vec2& v) const {
|
||||
Vec2 result(*this);
|
||||
result.add(v);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec2& Vec2::operator+=(const Vec2& v)
|
||||
{
|
||||
inline Vec2& Vec2::operator+=(const Vec2& v) {
|
||||
add(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec2 Vec2::operator-(const Vec2& v) const
|
||||
{
|
||||
inline const Vec2 Vec2::operator-(const Vec2& v) const {
|
||||
Vec2 result(*this);
|
||||
result.subtract(v);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec2& Vec2::operator-=(const Vec2& v)
|
||||
{
|
||||
inline Vec2& Vec2::operator-=(const Vec2& v) {
|
||||
subtract(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec2 Vec2::operator-() const
|
||||
{
|
||||
inline const Vec2 Vec2::operator-() const {
|
||||
Vec2 result(*this);
|
||||
result.negate();
|
||||
return result;
|
||||
}
|
||||
|
||||
inline const Vec2 Vec2::operator*(float s) const
|
||||
{
|
||||
inline const Vec2 Vec2::operator*(float s) const {
|
||||
Vec2 result(*this);
|
||||
result.scale(s);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec2& Vec2::operator*=(float s)
|
||||
{
|
||||
inline Vec2& Vec2::operator*=(float s) {
|
||||
scale(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec2 Vec2::operator/(const float s) const
|
||||
{
|
||||
return Vec2(this->x / s, this->y / s);
|
||||
inline const Vec2 Vec2::operator/(const float s) const {
|
||||
const float inv = 1.0f / s;
|
||||
return Vec2(this->x * inv, this->y * inv);
|
||||
}
|
||||
|
||||
inline bool Vec2::operator<(const Vec2& v) const
|
||||
{
|
||||
if (x == v.x)
|
||||
{
|
||||
inline bool Vec2::operator<(const Vec2& v) const {
|
||||
if (x == v.x) {
|
||||
return y < v.y;
|
||||
}
|
||||
return x < v.x;
|
||||
}
|
||||
|
||||
inline bool Vec2::operator>(const Vec2& v) const
|
||||
{
|
||||
if (x == v.x)
|
||||
{
|
||||
inline bool Vec2::operator>(const Vec2& v) const {
|
||||
if (x == v.x) {
|
||||
return y > v.y;
|
||||
}
|
||||
return x > v.x;
|
||||
}
|
||||
|
||||
inline bool Vec2::operator==(const Vec2& v) const
|
||||
{
|
||||
return x==v.x && y==v.y;
|
||||
inline bool Vec2::operator==(const Vec2& v) const {
|
||||
return x == v.x && y == v.y;
|
||||
}
|
||||
|
||||
inline bool Vec2::operator!=(const Vec2& v) const
|
||||
{
|
||||
return x!=v.x || y!=v.y;
|
||||
inline bool Vec2::operator!=(const Vec2& v) const {
|
||||
return x != v.x || y != v.y;
|
||||
}
|
||||
|
||||
inline const Vec2 operator*(float x, const Vec2& v)
|
||||
{
|
||||
inline const Vec2 operator*(float x, const Vec2& v) {
|
||||
Vec2 result(v);
|
||||
result.scale(x);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Vec2::setPoint(float xx, float yy)
|
||||
{
|
||||
void Vec2::setPoint(float xx, float yy) {
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
}
|
||||
|
||||
+18
-28
@@ -102,11 +102,17 @@ void Vec3::transformInverseRTS(const Vec3 &v, const Quaternion &r, const Vec3 &t
|
||||
}
|
||||
|
||||
float Vec3::angle(const Vec3 &v1, const Vec3 &v2) {
|
||||
const float dx = v1.y * v2.z - v1.z * v2.y;
|
||||
const float dy = v1.z * v2.x - v1.x * v2.z;
|
||||
const float dz = v1.x * v2.y - v1.y * v2.x;
|
||||
const auto magSqr1 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z;
|
||||
const auto magSqr2 = v2.x * v2.x + v2.y * v2.y + v2.z * v2.z;
|
||||
|
||||
return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
|
||||
if (magSqr1 == 0.0F || magSqr2 == 0.0F) {
|
||||
return 0.0F;
|
||||
}
|
||||
|
||||
const auto dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
auto cosine = dot / (sqrt(magSqr1 * magSqr2));
|
||||
cosine = clampf(cosine, -1.0, 1.0);
|
||||
return acos(cosine);
|
||||
}
|
||||
|
||||
void Vec3::add(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
|
||||
@@ -182,15 +188,6 @@ void Vec3::cross(const Vec3 &v) {
|
||||
}
|
||||
|
||||
void Vec3::cross(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
|
||||
CC_ASSERT(dst);
|
||||
|
||||
// NOTE: This code assumes Vec3 struct members are contiguous floats in memory.
|
||||
// We might want to revisit this (and other areas of code that make this assumption)
|
||||
// later to guarantee 100% safety/compatibility.
|
||||
MathUtil::crossVec3(&v1.x, &v2.x, &dst->x);
|
||||
}
|
||||
|
||||
void Vec3::crossProduct(const Vec3 &v1, const Vec3 &v2, Vec3 *dst) {
|
||||
dst->set(
|
||||
v1.y * v2.z - v1.z * v2.y,
|
||||
v1.z * v2.x - v1.x * v2.z,
|
||||
@@ -283,6 +280,8 @@ void Vec3::transformMat4Normal(const Vec3 &v, const Mat4 &m, Vec3 *dst) {
|
||||
float y = v.y;
|
||||
float z = v.z;
|
||||
float rhw = m.m[3] * x + m.m[7] * y + m.m[11] * z;
|
||||
// Important note by stanley:
|
||||
// abs needs to be removed later, because the operation will generate a wrong homogeneous coordinate
|
||||
rhw = rhw != 0.0F ? std::abs(1.0F / rhw) : 1.0F;
|
||||
dst->x = (m.m[0] * x + m.m[4] * y + m.m[8] * z) * rhw;
|
||||
dst->y = (m.m[1] * x + m.m[5] * y + m.m[9] * z) * rhw;
|
||||
@@ -332,22 +331,13 @@ float Vec3::dot(const Vec3 &v1, const Vec3 &v2) {
|
||||
}
|
||||
|
||||
void Vec3::normalize() {
|
||||
float n = x * x + y * y + z * z;
|
||||
// Already normalized.
|
||||
if (n == 1.0F) {
|
||||
return;
|
||||
float len = x * x + y * y + z * z;
|
||||
if (len > 0.0F) {
|
||||
len = 1.0F / std::sqrt(len);
|
||||
x *= len;
|
||||
y *= len;
|
||||
z *= len;
|
||||
}
|
||||
|
||||
n = std::sqrt(n);
|
||||
// Too close to zero.
|
||||
if (n < MATH_TOLERANCE) {
|
||||
return;
|
||||
}
|
||||
|
||||
n = 1.0F / n;
|
||||
x *= n;
|
||||
y *= n;
|
||||
z *= n;
|
||||
}
|
||||
|
||||
Vec3 Vec3::getNormalized() const {
|
||||
|
||||
@@ -193,15 +193,6 @@ public:
|
||||
*/
|
||||
void cross(const Vec3 &v);
|
||||
|
||||
/**
|
||||
* Calculate cross product.
|
||||
*
|
||||
* @param v1 is a vector.
|
||||
* @param v2 is a vector.
|
||||
* @param dst is a vector used to calculate the cross product of v1 and v2.
|
||||
*/
|
||||
static void crossProduct(const Vec3 &v1, const Vec3 &v2, Vec3 *dst);
|
||||
|
||||
/**
|
||||
* Computes the cross product of the specified vectors and stores the result in dst.
|
||||
*
|
||||
|
||||
+33
-61
@@ -22,74 +22,62 @@
|
||||
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
#include "math/Vec3.h"
|
||||
#include "math/Mat4.h"
|
||||
#include "math/Vec3.h"
|
||||
|
||||
NS_CC_MATH_BEGIN
|
||||
|
||||
|
||||
inline bool Vec3::isZero() const
|
||||
{
|
||||
inline bool Vec3::isZero() const {
|
||||
return x == 0.0f && y == 0.0f && z == 0.0f;
|
||||
}
|
||||
|
||||
inline bool Vec3::isOne() const
|
||||
{
|
||||
inline bool Vec3::isOne() const {
|
||||
return x == 1.0f && y == 1.0f && z == 1.0f;
|
||||
}
|
||||
|
||||
inline void Vec3::add(const Vec3& v)
|
||||
{
|
||||
inline void Vec3::add(const Vec3& v) {
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
z += v.z;
|
||||
}
|
||||
|
||||
inline void Vec3::add(float xx, float yy, float zz)
|
||||
{
|
||||
inline void Vec3::add(float xx, float yy, float zz) {
|
||||
x += xx;
|
||||
y += yy;
|
||||
z += zz;
|
||||
}
|
||||
|
||||
inline float Vec3::length() const
|
||||
{
|
||||
inline float Vec3::length() const {
|
||||
return std::sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
inline float Vec3::lengthSquared() const
|
||||
{
|
||||
inline float Vec3::lengthSquared() const {
|
||||
return (x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
inline void Vec3::negate()
|
||||
{
|
||||
inline void Vec3::negate() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
}
|
||||
|
||||
inline void Vec3::scale(float scalar)
|
||||
{
|
||||
inline void Vec3::scale(float scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
}
|
||||
|
||||
inline Vec3 Vec3::lerp(const Vec3 &target, float alpha) const
|
||||
{
|
||||
inline Vec3 Vec3::lerp(const Vec3& target, float alpha) const {
|
||||
return *this * (1.f - alpha) + target * alpha;
|
||||
}
|
||||
|
||||
inline void Vec3::set(float xx, float yy, float zz)
|
||||
{
|
||||
inline void Vec3::set(float xx, float yy, float zz) {
|
||||
this->x = xx;
|
||||
this->y = yy;
|
||||
this->z = zz;
|
||||
}
|
||||
|
||||
inline void Vec3::set(const float* array)
|
||||
{
|
||||
inline void Vec3::set(const float* array) {
|
||||
CC_ASSERT(array);
|
||||
|
||||
x = array[0];
|
||||
@@ -97,105 +85,89 @@ inline void Vec3::set(const float* array)
|
||||
z = array[2];
|
||||
}
|
||||
|
||||
inline void Vec3::set(const Vec3& v)
|
||||
{
|
||||
inline void Vec3::set(const Vec3& v) {
|
||||
this->x = v.x;
|
||||
this->y = v.y;
|
||||
this->z = v.z;
|
||||
}
|
||||
|
||||
inline void Vec3::set(const Vec3& p1, const Vec3& p2)
|
||||
{
|
||||
inline void Vec3::set(const Vec3& p1, const Vec3& p2) {
|
||||
x = p2.x - p1.x;
|
||||
y = p2.y - p1.y;
|
||||
z = p2.z - p1.z;
|
||||
}
|
||||
|
||||
inline void Vec3::setZero()
|
||||
{
|
||||
inline void Vec3::setZero() {
|
||||
x = y = z = 0.0f;
|
||||
}
|
||||
|
||||
inline void Vec3::subtract(const Vec3& v)
|
||||
{
|
||||
inline void Vec3::subtract(const Vec3& v) {
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
z -= v.z;
|
||||
}
|
||||
|
||||
inline const Vec3 Vec3::operator+(const Vec3& v) const
|
||||
{
|
||||
inline const Vec3 Vec3::operator+(const Vec3& v) const {
|
||||
Vec3 result(*this);
|
||||
result.add(v);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec3& Vec3::operator+=(const Vec3& v)
|
||||
{
|
||||
inline Vec3& Vec3::operator+=(const Vec3& v) {
|
||||
add(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec3 Vec3::operator-(const Vec3& v) const
|
||||
{
|
||||
inline const Vec3 Vec3::operator-(const Vec3& v) const {
|
||||
Vec3 result(*this);
|
||||
result.subtract(v);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec3& Vec3::operator-=(const Vec3& v)
|
||||
{
|
||||
inline Vec3& Vec3::operator-=(const Vec3& v) {
|
||||
subtract(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec3 Vec3::operator-() const
|
||||
{
|
||||
inline const Vec3 Vec3::operator-() const {
|
||||
Vec3 result(*this);
|
||||
result.negate();
|
||||
return result;
|
||||
}
|
||||
|
||||
inline const Vec3 Vec3::operator*(float s) const
|
||||
{
|
||||
inline const Vec3 Vec3::operator*(float s) const {
|
||||
Vec3 result(*this);
|
||||
result.scale(s);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Vec3 Vec3::operator*(const Vec3 &rhs) const
|
||||
{
|
||||
inline Vec3 Vec3::operator*(const Vec3& rhs) const {
|
||||
return Vec3(x * rhs.x, y * rhs.y, z * rhs.z);
|
||||
}
|
||||
|
||||
inline Vec3& Vec3::operator*=(float s)
|
||||
{
|
||||
inline Vec3& Vec3::operator*=(float s) {
|
||||
scale(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Vec3 Vec3::operator/(const float s) const
|
||||
{
|
||||
return Vec3(this->x / s, this->y / s, this->z / s);
|
||||
inline const Vec3 Vec3::operator/(const float s) const {
|
||||
const float inv = 1.0F / s;
|
||||
return Vec3(this->x * inv, this->y * inv, this->z * inv);
|
||||
}
|
||||
|
||||
inline Vec3 Vec3::operator/(const Vec3 &rhs) const
|
||||
{
|
||||
inline Vec3 Vec3::operator/(const Vec3& rhs) const {
|
||||
return Vec3(x / rhs.x, y / rhs.y, z / rhs.z);
|
||||
}
|
||||
|
||||
inline bool Vec3::operator==(const Vec3& v) const
|
||||
{
|
||||
return x==v.x && y==v.y && z==v.z;
|
||||
inline bool Vec3::operator==(const Vec3& v) const {
|
||||
return x == v.x && y == v.y && z == v.z;
|
||||
}
|
||||
|
||||
inline bool Vec3::operator!=(const Vec3& v) const
|
||||
{
|
||||
return x!=v.x || y!=v.y || z!=v.z;
|
||||
inline bool Vec3::operator!=(const Vec3& v) const {
|
||||
return x != v.x || y != v.y || z != v.z;
|
||||
}
|
||||
|
||||
inline const Vec3 operator*(float x, const Vec3& v)
|
||||
{
|
||||
inline const Vec3 operator*(float x, const Vec3& v) {
|
||||
Vec3 result(v);
|
||||
result.scale(x);
|
||||
return result;
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2011 ForzeField Studios S.L
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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 "math/Vertex.h"
|
||||
#include "base/Macros.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void ccVertexLineToPolygon(const ccstd::vector<Vec2> &points, float stroke, unsigned int offset, unsigned int nuPoints, ccstd::vector<Vec2> *vertices) {
|
||||
CC_ASSERT(vertices);
|
||||
nuPoints += offset;
|
||||
if (nuPoints <= 1) return;
|
||||
|
||||
stroke *= 0.5F;
|
||||
|
||||
unsigned int idx;
|
||||
unsigned int nuPointsMinus = nuPoints - 1;
|
||||
|
||||
for (unsigned int i = offset; i < nuPoints; i++) {
|
||||
idx = i * 2;
|
||||
Vec2 p1 = points[i];
|
||||
Vec2 perpVector;
|
||||
|
||||
if (i == 0) {
|
||||
perpVector = (p1 - points[i + 1]).getNormalized().getPerp();
|
||||
} else if (i == nuPointsMinus) {
|
||||
perpVector = (points[i - 1] - p1).getNormalized().getPerp();
|
||||
} else {
|
||||
Vec2 p2 = points[i + 1];
|
||||
Vec2 p0 = points[i - 1];
|
||||
|
||||
Vec2 p2p1 = (p2 - p1).getNormalized();
|
||||
Vec2 p0p1 = (p0 - p1).getNormalized();
|
||||
|
||||
// Calculate angle between vectors
|
||||
float angle = acosf(p2p1.dot(p0p1));
|
||||
|
||||
if (angle < CC_DEGREES_TO_RADIANS(70)) {
|
||||
perpVector = p2p1.getMidpoint(p0p1).getNormalized().getPerp();
|
||||
} else if (angle < CC_DEGREES_TO_RADIANS(170)) {
|
||||
perpVector = p2p1.getMidpoint(p0p1).getNormalized();
|
||||
} else {
|
||||
perpVector = (p2 - p0).getNormalized().getPerp();
|
||||
}
|
||||
}
|
||||
perpVector = perpVector * stroke;
|
||||
|
||||
(*vertices)[idx].set(p1.x + perpVector.x, p1.y + perpVector.y);
|
||||
(*vertices)[idx + 1].set(p1.x - perpVector.x, p1.y - perpVector.y);
|
||||
}
|
||||
|
||||
// Validate vertexes
|
||||
offset = (offset == 0) ? 0 : offset - 1;
|
||||
for (unsigned int i = offset; i < nuPointsMinus; i++) {
|
||||
idx = i * 2;
|
||||
const unsigned int idx1 = idx + 2;
|
||||
|
||||
Vec2 p1 = (*vertices)[idx];
|
||||
Vec2 p2 = (*vertices)[idx + 1];
|
||||
Vec2 p3 = (*vertices)[idx1];
|
||||
Vec2 p4 = (*vertices)[idx1 + 1];
|
||||
|
||||
float s;
|
||||
//BOOL fixVertex = !ccpLineIntersect(Vec2(p1.x, p1.y), Vec2(p4.x, p4.y), Vec2(p2.x, p2.y), Vec2(p3.x, p3.y), &s, &t);
|
||||
bool fixVertex = !ccVertexLineIntersect(p1.x, p1.y, p4.x, p4.y, p2.x, p2.y, p3.x, p3.y, &s);
|
||||
if (!fixVertex) {
|
||||
if (s < 0.F || s > 1.F) {
|
||||
fixVertex = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (fixVertex) {
|
||||
(*vertices)[idx1] = p4;
|
||||
(*vertices)[idx1 + 1] = p3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ccVertexLineIntersect(float ax, float ay,
|
||||
float bx, float by,
|
||||
float cx, float cy,
|
||||
float dx, float dy, float *t) {
|
||||
float distAB;
|
||||
float theCos;
|
||||
float theSin;
|
||||
float newX;
|
||||
|
||||
// FAIL: Line undefined
|
||||
if ((ax == bx && ay == by) || (cx == dx && cy == dy)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Translate system to make A the origin
|
||||
bx -= ax;
|
||||
by -= ay;
|
||||
cx -= ax;
|
||||
cy -= ay;
|
||||
dx -= ax;
|
||||
dy -= ay;
|
||||
|
||||
// Length of segment AB
|
||||
distAB = sqrtf(bx * bx + by * by);
|
||||
|
||||
// Rotate the system so that point B is on the positive X axis.
|
||||
theCos = bx / distAB;
|
||||
theSin = by / distAB;
|
||||
newX = cx * theCos + cy * theSin;
|
||||
cy = cy * theCos - cx * theSin;
|
||||
cx = newX;
|
||||
newX = dx * theCos + dy * theSin;
|
||||
dy = dy * theCos - dx * theSin;
|
||||
dx = newX;
|
||||
|
||||
// FAIL: Lines are parallel.
|
||||
if (cy == dy) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Discover the relative position of the intersection in the line AB
|
||||
*t = (dx + (cx - dx) * dy / (dy - cy)) / distAB;
|
||||
|
||||
// Success.
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
@@ -1,57 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2011 ForzeField Studios S.L
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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.
|
||||
****************************************************************************/
|
||||
#ifndef __CCVERTEX_H__
|
||||
#define __CCVERTEX_H__
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "base/std/container/vector.h"
|
||||
#include "math/Vec2.h"
|
||||
|
||||
/**
|
||||
* @addtogroup base
|
||||
* @{
|
||||
*/
|
||||
|
||||
namespace cc {
|
||||
|
||||
/** @file CCVertex.h */
|
||||
|
||||
/** converts a line to a polygon */
|
||||
void CC_DLL ccVertexLineToPolygon(const ccstd::vector<Vec2> &points, float stroke, unsigned int offset, unsigned int nuPoints, ccstd::vector<Vec2> *vertices);
|
||||
|
||||
/** returns whether or not the line intersects */
|
||||
bool CC_DLL ccVertexLineIntersect(float ax, float ay,
|
||||
float bx, float by,
|
||||
float cx, float cy,
|
||||
float dx, float dy, float *t);
|
||||
|
||||
} // namespace cc
|
||||
|
||||
// end of base group
|
||||
/// @}
|
||||
|
||||
#endif /* __CCVERTEX_H__ */
|
||||
@@ -64,7 +64,6 @@ inline uint32_t getFontIndex(bool bold, bool italic) {
|
||||
}
|
||||
|
||||
inline ccstd::string getFontPath(uint32_t index) {
|
||||
// stanley todo: use readable names later.
|
||||
static const ccstd::string UUIDS[DEBUG_FONT_COUNT] = {
|
||||
"OpenSans-Regular", //"OpenSans-Regular",
|
||||
"OpenSans-Bold", //"OpenSans-Bold",
|
||||
|
||||
@@ -322,7 +322,7 @@ void GeometryRenderer::addTriangle(const Vec3 &v0, const Vec3 &v1, const Vec3 &v
|
||||
const Vec3 dist1 = v1 - v0;
|
||||
const Vec3 dist2 = v2 - v0;
|
||||
Vec3 norm;
|
||||
Vec3::crossProduct(dist1, dist2, &norm);
|
||||
Vec3::cross(dist1, dist2, &norm);
|
||||
norm.normalize();
|
||||
normal.set(norm.x, norm.y, norm.z, 1.0F);
|
||||
}
|
||||
|
||||
@@ -38,31 +38,31 @@ TEST(mathMat3Test, test4) {
|
||||
ExpectEq(mat3.m[0] == 1 && mat3.m[4] == 1 && mat3.m[8] == 1, true);
|
||||
// transpose
|
||||
logLabel = "test the mat3 transpose function";
|
||||
mat3.set(11, 21, 31, 12, 22, 32, 13, 23, 33);
|
||||
mat3.set(11, 12, 13, 21, 22, 23, 31, 32, 33);
|
||||
mat3.transpose();
|
||||
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
|
||||
cc::Mat3 matTemp(11, 21, 31, 12, 22, 32, 13, 23, 33);
|
||||
cc::Mat3 matTemp(11, 12, 13, 21, 22, 23, 31, 32, 33);
|
||||
cc::Mat3::transpose(matTemp, &mat3);
|
||||
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
|
||||
// inverse
|
||||
logLabel = "test the mat3 inverse function";
|
||||
mat3.set(0, 2, 0, 3, 0, 0, 2, 0, 1);
|
||||
mat3.set(0, 3, 2, 2, 0, 0, 0, 0, 1);
|
||||
mat3.inverse();
|
||||
ExpectEq(mat3.m[1] == 0.5f && mat3.m[8] == 1, true);
|
||||
// adjoint
|
||||
logLabel = "test the mat3 adjoint function";
|
||||
cc::Mat3 matAdj(0, 2, 0, 3, 0, 0, 2, 0, 1);
|
||||
cc::Mat3 matAdj(0, 3, 2, 2, 0, 0, 0, 0, 1);
|
||||
cc::Mat3::adjoint(matAdj, &mat3);
|
||||
ExpectEq(mat3.m[1] == -3 && mat3.m[3] == -2 && mat3.m[8] == -6, true);
|
||||
// determinant
|
||||
logLabel = "test the mat3 determinant function";
|
||||
mat3.set(1, 0, 3, 2, 1, 0, 4, 0, 2);
|
||||
mat3.set(1, 2, 4, 0, 1, 0, 3, 0, 2);
|
||||
float det = mat3.determinant();
|
||||
ExpectEq(det == -10, true);
|
||||
// multiply
|
||||
logLabel = "test the mat3 multiply function";
|
||||
cc::Mat3 lm(2, 3, 1, 4, 7, 3, 8, 2, 0);
|
||||
cc::Mat3 rm(5, 2, 6, 8, 1, 0, 6, 5, 4);
|
||||
cc::Mat3 lm(2, 4, 8, 3, 7, 2, 1, 3, 0);
|
||||
cc::Mat3 rm(5, 8, 6, 2, 1, 5, 6, 0, 4);
|
||||
cc::Mat3::multiply(lm, rm, &mat3);
|
||||
ExpectEq(mat3.m[0] == 40 && mat3.m[4] == 30 && mat3.m[8] == 48, true);
|
||||
// translate
|
||||
@@ -105,13 +105,16 @@ TEST(mathMat3Test, test4) {
|
||||
// fromQuat
|
||||
logLabel = "test the mat3 fromQuat function";
|
||||
cc::Mat3 fromQuat;
|
||||
cc::Quaternion quat(0, 0, 3, 1);
|
||||
cc::Quaternion quat;
|
||||
cc::Quaternion::createFromAxisAngle(cc::Vec3(0, 0, 1), M_PI / 2, &quat);
|
||||
cc::Mat3::fromQuat(quat, &fromQuat);
|
||||
ExpectEq(copyMat.m[0] == 3 && copyMat.m[3] == 6 && copyMat.m[4] == 7, true);
|
||||
cc::Vec3 vec3;
|
||||
vec3.transformMat3(cc::Vec3(1, 1, 0), fromQuat);
|
||||
ExpectEq(vec3.approxEquals(cc::Vec3(-1, 1, 0)), true);
|
||||
// add
|
||||
logLabel = "test the mat3 add function";
|
||||
cc::Mat3 lAdd(9, 0, 0, 3, 8, 0, 9, 0, 0);
|
||||
cc::Mat3 rAdd(1, 0, 0, 5, 2, 0, 1, 0, 0);
|
||||
cc::Mat3 lAdd(9, 3, 9, 0, 8, 0, 0, 0, 0);
|
||||
cc::Mat3 rAdd(1, 5, 1, 0, 2, 0, 0, 0, 0);
|
||||
cc::Mat3 addOut;
|
||||
cc::Mat3::add(lAdd, rAdd, &addOut);
|
||||
ExpectEq(addOut.m[0] == 10 && addOut.m[2] == 10 && addOut.m[4] == 10, true);
|
||||
|
||||
@@ -101,12 +101,31 @@ TEST(mathQuaternionTest, test6) {
|
||||
ExpectEq(IsEqualF(7.5, quat.x) && IsEqualF(0.5, quat.z), true);
|
||||
// slerp
|
||||
logLabel = "test the quaternion slerp function";
|
||||
q1.set(1, 2, 10, 1);
|
||||
q2.set(30, 10, 20, 1);
|
||||
cc::Quaternion::slerp(q1, q2, 0, &quat);
|
||||
ExpectEq(IsEqualF(1, quat.x) && IsEqualF(10, quat.z), true);
|
||||
q1.setIdentity();
|
||||
q2.set(sin(M_PI / 4), 0, 0, cos(M_PI / 4));
|
||||
cc::Quaternion::slerp(q1, q2, 0.5f, &quat);
|
||||
ExpectEq(quat.approxEquals(cc::Quaternion(sin(M_PI / 8), 0, 0, cos(M_PI / 8))), true);
|
||||
logLabel = "test the quaternion approx equal function";
|
||||
cc::Quaternion a{0.123456F, 1.234567F, 2.345678F, 3.345679F};
|
||||
cc::Quaternion b{0.123455F, 1.234568F, 2.345679F, 3.345678F};
|
||||
ExpectEq(a.approxEquals(b), true);
|
||||
// fromEuler
|
||||
logLabel = "test the quaternion fromEuler function";
|
||||
cc::Quaternion::fromEuler(0, 0, 60, &q1);
|
||||
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
|
||||
// toEuler
|
||||
logLabel = "test the quaternion toEuler function";
|
||||
cc::Vec3 v;
|
||||
cc::Quaternion::toEuler(q1, false, &v);
|
||||
ExpectEq(v == cc::Vec3(0, 0, 60), true);
|
||||
// fromMat3
|
||||
logLabel = "test the quaternion fromMat3 function";
|
||||
cc::Mat3 m;
|
||||
cc::Mat3::fromRotation(M_PI / 3, &m);
|
||||
cc::Quaternion::fromMat3(m, &q1);
|
||||
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
|
||||
// createFromAngleZ
|
||||
logLabel = "test the quaternion createFromAngleZ function";
|
||||
cc::Quaternion::createFromAngleZ(60, &q1);
|
||||
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ TEST(mathVec3Test, test2) {
|
||||
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(-2, 0, 0)), M_PI), true);
|
||||
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, 2, 0)), M_PI * 0.5f), true);
|
||||
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, -2, 0)), M_PI * 0.5f), true);
|
||||
// transformInverseRTS
|
||||
logLabel = "test the vec3 transformInverseRTS function";
|
||||
cc::Vec3::transformInverseRTS(cc::Vec3(-2, 4, 7), cc::Quaternion(0, 0, sin(M_PI / 4), cos(M_PI / 4)), cc::Vec3(1, 2, 3), cc::Vec3(2, 3, 4), &vec3);
|
||||
ExpectEq(vec3.approxEquals(cc::Vec3(1, 1, 1)), true);
|
||||
// add
|
||||
logLabel = "test the vec3 add function";
|
||||
vec3.set(3.9, 1.3, 0);
|
||||
@@ -95,11 +99,23 @@ TEST(mathVec3Test, test2) {
|
||||
matTranslate.m[14] = 2;
|
||||
vec3.transformMat4(cc::Vec3(1, 1, 2), matTranslate);
|
||||
ExpectEq(vec3 == cc::Vec3(3, 2, 4), true);
|
||||
// transformMat4Normal
|
||||
logLabel = "test the vec3 transformMat4Normal function";
|
||||
cc::Mat4 matTransfromNormal;
|
||||
matTransfromNormal.m[0] = 2;
|
||||
matTransfromNormal.m[5] = 4;
|
||||
matTransfromNormal.m[10] = 8;
|
||||
matTransfromNormal.m[12] = 2;
|
||||
matTransfromNormal.m[13] = 1;
|
||||
matTransfromNormal.m[14] = 2;
|
||||
cc::Vec3::transformMat4Normal(cc::Vec3(1, 1, 2), matTransfromNormal, &vec3);
|
||||
ExpectEq(vec3 == cc::Vec3(2, 4, 16), true);
|
||||
// transformQuat
|
||||
logLabel = "test the vec3 transformQuat function";
|
||||
cc::Quaternion quaternion(10, 0, 0, 0);
|
||||
vec3.set(3, 2, 4);
|
||||
cc::Quaternion quaternion(sin(M_PI / 4), 0, 0, cos(M_PI / 4));
|
||||
vec3.transformQuat(quaternion);
|
||||
ExpectEq(vec3 == cc::Vec3(300, -200, -400), true);
|
||||
ExpectEq(vec3.approxEquals(cc::Vec3(3, -4, 2)), true);
|
||||
// distance
|
||||
logLabel = "test the vec3 distance function";
|
||||
vec3.set(7, 2, 0);
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
|
||||
http://www.cocos2d-x.org
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
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 <math.h>
|
||||
#include <vector>
|
||||
#include "cocos/math/Math.h"
|
||||
#include "cocos/math/Vec2.h"
|
||||
#include "cocos/math/Vertex.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "utils.h"
|
||||
|
||||
TEST(mathVertexTest, test8) {
|
||||
// ccVertexLineToPolygon
|
||||
logLabel = "test the vertex ccVertexLineToPolygon function";
|
||||
std::vector<cc::Vec2> points{4};
|
||||
points[0] = {1, 1};
|
||||
points[1] = {10, 10};
|
||||
points[2] = {0, -10};
|
||||
points[3] = {20, 70};
|
||||
std::vector<cc::Vec2> outP{8};
|
||||
cc::ccVertexLineToPolygon(points, 1, 0, 4, &outP);
|
||||
ExpectEq(IsEqualF(outP[5].x, -0.468942523) && IsEqualF(outP[5].y, -9.82652664), true);
|
||||
// ccVertexLineIntersect
|
||||
logLabel = "test the vertex ccVertexLineIntersect function";
|
||||
float res;
|
||||
bool isFind = cc::ccVertexLineIntersect(5, 5, 1, 10, 12, 20, 12, 40, &res);
|
||||
ExpectEq(isFind, true);
|
||||
}
|
||||
Reference in New Issue
Block a user