diff --git a/engine/src/flutter/impeller/host/BUILD.gn b/engine/src/flutter/impeller/host/BUILD.gn index 67f42d79e4..ae7b48e403 100644 --- a/engine/src/flutter/impeller/host/BUILD.gn +++ b/engine/src/flutter/impeller/host/BUILD.gn @@ -25,4 +25,6 @@ executable("host") { "MetalKit.framework", "ModelIO.framework", ] + + deps = [ "//flutter/impeller/impeller" ] } diff --git a/engine/src/flutter/impeller/impeller/BUILD.gn b/engine/src/flutter/impeller/impeller/BUILD.gn index 8c9c369144..f7c06059ec 100644 --- a/engine/src/flutter/impeller/impeller/BUILD.gn +++ b/engine/src/flutter/impeller/impeller/BUILD.gn @@ -7,4 +7,34 @@ import("//flutter/common/config.gni") source_set("impeller") { cflags_objc = flutter_cflags_objc_arc cflags_objcc = flutter_cflags_objcc_arc + + geometry_sources = [ + "geometry/Matrix.cc", + "geometry/Matrix.h", + "geometry/Path.cc", + "geometry/Path.h", + "geometry/PathBuilder.cc", + "geometry/PathBuilder.h", + "geometry/PathComponent.cc", + "geometry/PathComponent.h", + "geometry/Point.cc", + "geometry/Point.h", + "geometry/Quaternion.cc", + "geometry/Quaternion.h", + "geometry/Rect.cc", + "geometry/Rect.h", + "geometry/Shear.cc", + "geometry/Shear.h", + "geometry/Size.cc", + "geometry/Size.h", + "geometry/Vector.cc", + "geometry/Vector.h", + ] + + sources = geometry_sources + + deps = [ + "//flutter/fml", + "//flutter/runtime:libdart", # For tracing, seems to be pulled in by FML. + ] } diff --git a/engine/src/flutter/impeller/impeller/geometry/Matrix.cc b/engine/src/flutter/impeller/impeller/geometry/Matrix.cc new file mode 100644 index 0000000000..8909df29bd --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Matrix.cc @@ -0,0 +1,484 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Matrix.h" +#include +#include + +namespace rl { +namespace geom { + +Matrix::Matrix(const Decomposition& d) : Matrix() { + /* + * Apply perspective. + */ + for (int i = 0; i < 4; i++) { + e[i][3] = d.perspective.e[i]; + } + + /* + * Apply translation. + */ + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + e[3][i] += d.translation.e[j] * e[j][i]; + } + } + + /* + * Apply rotation. + */ + + Matrix rotation; + + const auto x = -d.rotation.x; + const auto y = -d.rotation.y; + const auto z = -d.rotation.z; + const auto w = d.rotation.w; + + /* + * Construct a composite rotation matrix from the quaternion values. + */ + + rotation.e[0][0] = 1.0 - 2.0 * (y * y + z * z); + rotation.e[0][1] = 2.0 * (x * y - z * w); + rotation.e[0][2] = 2.0 * (x * z + y * w); + rotation.e[1][0] = 2.0 * (x * y + z * w); + rotation.e[1][1] = 1.0 - 2.0 * (x * x + z * z); + rotation.e[1][2] = 2.0 * (y * z - x * w); + rotation.e[2][0] = 2.0 * (x * z - y * w); + rotation.e[2][1] = 2.0 * (y * z + x * w); + rotation.e[2][2] = 1.0 - 2.0 * (x * x + y * y); + + *this = *this * rotation; + + /* + * Apply shear. + */ + Matrix shear; + + if (d.shear.e[2] != 0) { + shear.e[2][1] = d.shear.e[2]; + *this = *this * shear; + } + + if (d.shear.e[1] != 0) { + shear.e[2][1] = 0.0; + shear.e[2][0] = d.shear.e[1]; + *this = *this * shear; + } + + if (d.shear.e[0] != 0) { + shear.e[2][0] = 0.0; + shear.e[1][0] = d.shear.e[0]; + *this = *this * shear; + } + + /* + * Apply scale. + */ + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + e[i][j] *= d.scale.e[i]; + } + } +} + +Matrix Matrix::Orthographic(double left, + double right, + double bottom, + double top, + double nearZ, + double farZ) { + double ral = right + left; + double rsl = right - left; + double tab = top + bottom; + double tsb = top - bottom; + double fan = farZ + nearZ; + double fsn = farZ - nearZ; + + // clang-format off + return Matrix(2.0 / rsl, 0.0, 0.0, 0.0, + 0.0, 2.0 / tsb, 0.0, 0.0, + 0.0, 0.0, -2.0 / fsn, 0.0, + -ral / rsl, -tab / tsb, -fan / fsn, 1.0); + // clang-format on +} + +Matrix Matrix::Orthographic(const Size& size) { + return Matrix::Orthographic(0, size.width, size.height, 0, -INT_MAX, INT_MAX); +} + +Matrix Matrix::Perspective(double fov, + double aspect, + double nearZ, + double farZ) { + double cotan = 1.0 / tan(fov / 2.0); + + return Matrix(cotan / aspect, 0.0, 0.0, 0.0, // + 0.0, cotan, 0.0, 0.0, // + 0.0, 0.0, (farZ + nearZ) / (nearZ - farZ), -1.0, // + 0.0, 0.0, (2.0 * farZ * nearZ) / (nearZ - farZ), 0.0 // + ); +} + +Matrix Matrix::LookAt(const Vector3& eye, + const Vector3& center, + const Vector3& up) { + auto n = (eye - center).normalize(); + auto u = (up.cross(n)).normalize(); + auto v = n.cross(u); + return {u.x, v.x, n.x, 0.0, // + u.y, v.y, n.y, 0.0, // + u.z, v.z, n.z, 0.0, // + (-u).dot(eye), (-v).dot(eye), (-n).dot(eye), 1.0}; +} + +Matrix Matrix::operator+(const Matrix& o) const { + return Matrix( + m[0] + o.m[0], m[1] + o.m[1], m[2] + o.m[2], m[3] + o.m[3], // + m[4] + o.m[4], m[5] + o.m[5], m[6] + o.m[6], m[7] + o.m[7], // + m[8] + o.m[8], m[9] + o.m[9], m[10] + o.m[10], m[11] + o.m[11], // + m[12] + o.m[12], m[13] + o.m[13], m[14] + o.m[14], m[15] + o.m[15] // + ); +} + +std::string Matrix::toString() const { + std::stringstream stream; + for (int i = 0, limit = 16; i < limit; i++) { + stream << m[i]; + if (i != limit - 1) { + stream << ","; + } + } + return stream.str(); +} + +void Matrix::fromString(const std::string& str) { + std::stringstream stream(str); + for (int i = 0; i < 16; i++) { + stream >> m[i]; + stream.ignore(); + } +} + +Matrix Matrix::invert() const { + Matrix tmp{ + m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10], + + -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - + m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10], + + m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6], + + -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - + m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6], + + -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - + m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10], + + m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10], + + -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - + m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6], + + m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6], + + m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9], + + -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - + m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9], + + m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5], + + -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - + m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5], + + -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - + m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9], + + m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9], + + -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - + m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5], + + m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5]}; + + double det = + m[0] * tmp.m[0] + m[1] * tmp.m[4] + m[2] * tmp.m[8] + m[3] * tmp.m[12]; + + if (det == 0) { + return {}; + } + + det = 1.0 / det; + + return {tmp.m[0] * det, tmp.m[1] * det, tmp.m[2] * det, tmp.m[3] * det, + tmp.m[4] * det, tmp.m[5] * det, tmp.m[6] * det, tmp.m[7] * det, + tmp.m[8] * det, tmp.m[9] * det, tmp.m[10] * det, tmp.m[11] * det, + tmp.m[12] * det, tmp.m[13] * det, tmp.m[14] * det, tmp.m[15] * det}; +} + +Matrix Matrix::transpose() const { + return { + 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], // + }; +} + +double Matrix::determinant() const { + auto a00 = e[0][0]; + auto a01 = e[0][1]; + auto a02 = e[0][2]; + auto a03 = e[0][3]; + auto a10 = e[1][0]; + auto a11 = e[1][1]; + auto a12 = e[1][2]; + auto a13 = e[1][3]; + auto a20 = e[2][0]; + auto a21 = e[2][1]; + auto a22 = e[2][2]; + auto a23 = e[2][3]; + auto a30 = e[3][0]; + auto a31 = e[3][1]; + auto a32 = e[3][2]; + auto a33 = e[3][3]; + + auto b00 = a00 * a11 - a01 * a10; + auto b01 = a00 * a12 - a02 * a10; + auto b02 = a00 * a13 - a03 * a10; + auto b03 = a01 * a12 - a02 * a11; + auto b04 = a01 * a13 - a03 * a11; + auto b05 = a02 * a13 - a03 * a12; + auto b06 = a20 * a31 - a21 * a30; + auto b07 = a20 * a32 - a22 * a30; + auto b08 = a20 * a33 - a23 * a30; + auto b09 = a21 * a32 - a22 * a31; + auto b10 = a21 * a33 - a23 * a31; + auto b11 = a22 * a33 - a23 * a32; + + return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; +} + +/* + * Adapted for Radar from Graphics Gems: + * http://www.realtimerendering.com/resources/GraphicsGems/gemsii/unmatrix.c + */ +Matrix::DecompositionResult Matrix::decompose() const { + /* + * Normalize the matrix. + */ + Matrix self = *this; + + if (self.e[3][3] == 0) { + return {false, {}}; + } + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + self.e[i][j] /= self.e[3][3]; + } + } + + /* + * `perspectiveMatrix` is used to solve for perspective, but it also provides + * an easy way to test for singularity of the upper 3x3 component. + */ + Matrix perpectiveMatrix = self; + for (int i = 0; i < 3; i++) { + perpectiveMatrix.e[i][3] = 0; + } + + perpectiveMatrix.e[3][3] = 1; + + if (perpectiveMatrix.determinant() == 0.0) { + return {false, {}}; + } + + Decomposition result; + + /* + * ========================================================================== + * First, isolate perspective. + * ========================================================================== + */ + if (self.e[0][3] != 0.0 || self.e[1][3] != 0.0 || self.e[2][3] != 0.0) { + /* + * prhs is the right hand side of the equation. + */ + const Vector4 rightHandSide(self.e[0][3], // + self.e[1][3], // + self.e[2][3], // + self.e[3][3]); + + /* + * Solve the equation by inverting `perspectiveMatrix` and multiplying + * prhs by the inverse. + */ + + result.perspective = rightHandSide * perpectiveMatrix.invert().transpose(); + + /* + * Clear the perspective partition. + */ + self.e[0][3] = self.e[1][3] = self.e[2][3] = 0; + self.e[3][3] = 1; + } + + /* + * ========================================================================== + * Next, the translation. + * ========================================================================== + */ + result.translation = {self.e[3][0], self.e[3][1], self.e[3][2]}; + self.e[3][0] = self.e[3][1] = self.e[3][2] = 0.0; + + /* + * ========================================================================== + * Next, the scale and shear. + * ========================================================================== + */ + Vector3 row[3]; + for (int i = 0; i < 3; i++) { + row[i].x = self.e[i][0]; + row[i].y = self.e[i][1]; + row[i].z = self.e[i][2]; + } + + /* + * Compute X scale factor and normalize first row. + */ + result.scale.x = row[0].length(); + row[0] = row[0].normalize(); + + /* + * Compute XY shear factor and make 2nd row orthogonal to 1st. + */ + result.shear.xy = row[0].dot(row[1]); + row[1] = Vector3::Combine(row[1], 1.0, row[0], -result.shear.xy); + + /* + * Compute Y scale and normalize 2nd row. + */ + result.scale.y = row[1].length(); + row[1] = row[1].normalize(); + result.shear.xy /= result.scale.y; + + /* + * Compute XZ and YZ shears, orthogonalize 3rd row. + */ + result.shear.xz = row[0].dot(row[2]); + row[2] = Vector3::Combine(row[2], 1.0, row[0], -result.shear.xz); + result.shear.yz = row[1].dot(row[2]); + row[2] = Vector3::Combine(row[2], 1.0, row[1], -result.shear.yz); + + /* + * Next, get Z scale and normalize 3rd row. + */ + result.scale.z = row[2].length(); + row[2] = row[2].normalize(); + + result.shear.xz /= result.scale.z; + result.shear.yz /= result.scale.z; + + /* + * At this point, the matrix (in rows[]) is orthonormal. + * Check for a coordinate system flip. If the determinant + * is -1, then negate the matrix and the scaling factors. + */ + if (row[0].dot(row[1].cross(row[2])) < 0) { + result.scale.x *= -1; + result.scale.y *= -1; + result.scale.z *= -1; + + for (int i = 0; i < 3; i++) { + row[i].x *= -1; + row[i].y *= -1; + row[i].z *= -1; + } + } + + /* + * ========================================================================== + * Finally, get the rotations out. + * ========================================================================== + */ + result.rotation.x = + 0.5 * sqrt(fmax(1.0 + row[0].x - row[1].y - row[2].z, 0.0)); + result.rotation.y = + 0.5 * sqrt(fmax(1.0 - row[0].x + row[1].y - row[2].z, 0.0)); + result.rotation.z = + 0.5 * sqrt(fmax(1.0 - row[0].x - row[1].y + row[2].z, 0.0)); + result.rotation.w = + 0.5 * sqrt(fmax(1.0 + row[0].x + row[1].y + row[2].z, 0.0)); + + if (row[2].y > row[1].z) { + result.rotation.x = -result.rotation.x; + } + if (row[0].z > row[2].x) { + result.rotation.y = -result.rotation.y; + } + if (row[1].x > row[0].y) { + result.rotation.z = -result.rotation.z; + } + + return DecompositionResult(true, result); +} + +uint64_t Matrix::Decomposition::componentsMask() const { + uint64_t mask = 0; + + Quaternion noRotation(0.0, 0.0, 0.0, 1.0); + if (rotation != noRotation) { + mask = mask | static_cast(Component::Rotation); + } + + Vector4 defaultPerspective(0.0, 0.0, 0.0, 1.0); + if (perspective != defaultPerspective) { + mask = mask | static_cast(Component::Perspective); + } + + Shear noShear(0.0, 0.0, 0.0); + if (shear != noShear) { + mask = mask | static_cast(Component::Shear); + } + + Vector3 defaultScale(1.0, 1.0, 1.0); + if (scale != defaultScale) { + mask = mask | static_cast(Component::Scale); + } + + Vector3 defaultTranslation(0.0, 0.0, 0.0); + if (translation != defaultTranslation) { + mask = mask | static_cast(Component::Translation); + } + + return mask; +} + +std::string Matrix::Decomposition::toString() const { + std::stringstream stream; + + stream << "Translation: " << translation.toString() << std::endl; + stream << "Scale: " << scale.toString() << std::endl; + stream << "Shear: " << shear.toString() << std::endl; + stream << "Perspective: " << perspective.toString() << std::endl; + stream << "Rotation: " << rotation.toString() << std::endl; + + return stream.str(); +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Matrix.h b/engine/src/flutter/impeller/impeller/geometry/Matrix.h new file mode 100644 index 0000000000..add1b681b5 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Matrix.h @@ -0,0 +1,299 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "Point.h" +#include "Quaternion.h" +#include "Shear.h" +#include "Size.h" +#include "Vector.h" + +namespace rl { +namespace geom { + +struct Matrix { + union { + double m[16]; + double e[4][4]; + Vector4 vec[4]; + }; + + struct Decomposition { + Vector3 translation; + Vector3 scale; + Shear shear; + Vector4 perspective; + Quaternion rotation; + + enum class Component { + Translation = 1 << 0, + Scale = 1 << 1, + Shear = 1 << 2, + Perspective = 1 << 3, + Rotation = 1 << 4, + }; + + uint64_t componentsMask() const; + + std::string toString() const; + }; + + using DecompositionResult = + std::pair; + + Matrix() + // clang-format off + : vec{ Vector4(1.0, 0.0, 0.0, 0.0), + Vector4(0.0, 1.0, 0.0, 0.0), + Vector4(0.0, 0.0, 1.0, 0.0), + Vector4(0.0, 0.0, 0.0, 1.0)} {} + // clang-format on + + // clang-format off + Matrix(double m0, double m1, double m2, double m3, + double m4, double m5, double m6, double m7, + double m8, double m9, double m10, double m11, + double m12, double m13, double m14, double m15) + : vec{Vector4(m0, m1, m2, m3), + Vector4(m4, m5, m6, m7), + Vector4(m8, m9, m10, m11), + Vector4(m12, m13, m14, m15)} {} + // clang-format on + + Matrix(const Decomposition& decomposition); + + static Matrix Translation(const Vector3& t) { + // clang-format off + return Matrix(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + t.x, t.y, t.z, 1.0); + // clang-format on + } + + static Matrix Scale(const Vector3& s) { + // clang-format off + return Matrix(s.x, 0.0, 0.0, 0.0, + 0.0, s.y, 0.0, 0.0, + 0.0, 0.0, s.z, 0.0, + 0.0, 0.0, 0.0, 1.0); + // clang-format on + } + + static Matrix Rotation(double radians, const Vector4& r) { + const Vector4 v = r.normalize(); + + const double cosine = cos(radians); + const double cosp = 1.0f - cosine; + const double sine = sin(radians); + + // clang-format off + return Matrix( + cosine + cosp * v.x * v.x, + cosp * v.x * v.y + v.z * sine, + cosp * v.x * v.z - v.y * sine, + 0.0, + + cosp * v.x * v.y - v.z * sine, + cosine + cosp * v.y * v.y, + cosp * v.y * v.z + v.x * sine, + 0.0, + + cosp * v.x * v.z + v.y * sine, + cosp * v.y * v.z - v.x * sine, + cosine + cosp * v.z * v.z, + 0.0, + + 0.0, + 0.0, + 0.0, + 1.0); + // clang-format on + } + + static Matrix RotationX(double radians) { + double cosine = cos(radians); + double sine = sin(radians); + // clang-format off + return Matrix( + 1.0, 0.0, 0.0, 0.0, + 0.0, cosine, sine, 0.0, + 0.0, -sine, cosine, 0.0, + 0.0, 0.0, 0.0, 1.0 + ); + // clang-format on + } + + static Matrix RotationY(double radians) { + double cosine = cos(radians); + double sine = sin(radians); + + // clang-format off + return Matrix( + cosine, 0.0, -sine, 0.0, + 0.0, 1.0, 0.0, 0.0, + sine, 0.0, cosine, 0.0, + 0.0, 0.0, 0.0, 1.0 + ); + // clang-format on + } + + static Matrix RotationZ(double radians) { + double cosine = cos(radians); + double sine = sin(radians); + + // clang-format off + return Matrix ( + cosine, sine, 0.0, 0.0, + -sine, cosine, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0 + ); + // clang-format on + } + + Matrix translate(const Vector3& t) const { + // clang-format off + return Matrix(m[0], m[1], m[2], m[3], + m[4], m[5], m[6], m[7], + m[8], m[9], m[10], m[11], + m[0] * t.x + m[4] * t.y + m[8] * t.z + m[12], + m[1] * t.x + m[5] * t.y + m[9] * t.z + m[13], + m[2] * t.x + m[6] * t.y + m[10] * t.z + m[14], + m[15]); + // clang-format on + } + + Matrix scale(const Vector3& s) const { + // clang-format off + return Matrix(m[0] * s.x, m[1] * s.x, m[2] * s.x , m[3] * s.x, + m[4] * s.y, m[5] * s.y, m[6] * s.y , m[7] * s.y, + m[8] * s.z, m[9] * s.z, m[10] * s.z, m[11] * s.z, + m[12] , m[13] , m[14] , m[15]); + // clang-format on + } + + Matrix multiply(const Matrix& o) const { + // clang-format off + return Matrix( + m[0] * o.m[0] + m[4] * o.m[1] + m[8] * o.m[2] + m[12] * o.m[3], + m[1] * o.m[0] + m[5] * o.m[1] + m[9] * o.m[2] + m[13] * o.m[3], + m[2] * o.m[0] + m[6] * o.m[1] + m[10] * o.m[2] + m[14] * o.m[3], + m[3] * o.m[0] + m[7] * o.m[1] + m[11] * o.m[2] + m[15] * o.m[3], + m[0] * o.m[4] + m[4] * o.m[5] + m[8] * o.m[6] + m[12] * o.m[7], + m[1] * o.m[4] + m[5] * o.m[5] + m[9] * o.m[6] + m[13] * o.m[7], + m[2] * o.m[4] + m[6] * o.m[5] + m[10] * o.m[6] + m[14] * o.m[7], + m[3] * o.m[4] + m[7] * o.m[5] + m[11] * o.m[6] + m[15] * o.m[7], + m[0] * o.m[8] + m[4] * o.m[9] + m[8] * o.m[10] + m[12] * o.m[11], + m[1] * o.m[8] + m[5] * o.m[9] + m[9] * o.m[10] + m[13] * o.m[11], + m[2] * o.m[8] + m[6] * o.m[9] + m[10] * o.m[10] + m[14] * o.m[11], + m[3] * o.m[8] + m[7] * o.m[9] + m[11] * o.m[10] + m[15] * o.m[11], + m[0] * o.m[12] + m[4] * o.m[13] + m[8] * o.m[14] + m[12] * o.m[15], + m[1] * o.m[12] + m[5] * o.m[13] + m[9] * o.m[14] + m[13] * o.m[15], + m[2] * o.m[12] + m[6] * o.m[13] + m[10] * o.m[14] + m[14] * o.m[15], + m[3] * o.m[12] + m[7] * o.m[13] + m[11] * o.m[14] + m[15] * o.m[15]); + // clang-format on + } + + Matrix transpose() const; + + Matrix invert() const; + + double determinant() const; + + bool isAffine() const { + return (m[2] == 0 && m[3] == 0 && m[6] == 0 && m[7] == 0 && m[8] == 0 && + m[9] == 0 && m[10] == 1 && m[11] == 0 && m[14] == 0 && m[15] == 1); + } + + bool isIdentity() const { + return ( + // clang-format off + m[0] == 1.0 && m[1] == 0.0 && m[2] == 0.0 && m[3] == 0.0 && + m[4] == 0.0 && m[5] == 1.0 && m[6] == 0.0 && m[7] == 0.0 && + m[8] == 0.0 && m[9] == 0.0 && m[10] == 1.0 && m[11] == 0.0 && + m[12] == 0.0 && m[13] == 0.0 && m[14] == 0.0 && m[15] == 1.0 + // clang-format on + ); + } + + DecompositionResult decompose() const; + + bool operator==(const Matrix& m) const { + // clang-format off + return vec[0] == m.vec[0] + && vec[1] == m.vec[1] + && vec[2] == m.vec[2] + && vec[3] == m.vec[3]; + // clang-format on + } + + bool operator!=(const Matrix& m) const { + // clang-format off + return vec[0] != m.vec[0] + || vec[1] != m.vec[1] + || vec[2] != m.vec[2] + || vec[3] != m.vec[3]; + // clang-format on + } + + Matrix operator+(const Vector3& t) const { return translate(t); } + + Matrix operator-(const Vector3& t) const { return translate(-t); } + + Matrix operator*(const Vector3& s) const { return scale(s); } + + Matrix operator*(const Matrix& m) const { return multiply(m); } + + Matrix operator+(const Matrix& m) const; + + static Matrix Orthographic(double left, + double right, + double bottom, + double top, + double nearZ, + double farZ); + + static Matrix Orthographic(const Size& size); + + /** + * Specify a viewing frustum in the worlds coordinate system. + * + * @param fov angle of the field of view (in radians). + * @param aspect aspect ratio. + * @param nearZ near clipping plane. + * @param farZ far clipping plane. + * + * @return the perspective projection matrix. + */ + static Matrix Perspective(double fov, + double aspect, + double nearZ, + double farZ); + + static Matrix LookAt(const Vector3& eye, + const Vector3& center, + const Vector3& up); + + std::string toString() const; + + void fromString(const std::string& str); +}; + +static_assert(sizeof(struct Matrix) == sizeof(double) * 16, + "The matrix must be of consistent size."); + +static inline Vector4 operator*(const Vector4& v, const Matrix& m) { + return Vector4(v.x * m.m[0] + v.y * m.m[4] + v.z * m.m[8] + v.w * m.m[12], + v.x * m.m[1] + v.y * m.m[5] + v.z * m.m[9] + v.w * m.m[13], + v.x * m.m[2] + v.y * m.m[6] + v.z * m.m[10] + v.w * m.m[14], + v.x * m.m[3] + v.y * m.m[7] + v.z * m.m[11] + v.w * m.m[15]); +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Path.cc b/engine/src/flutter/impeller/impeller/geometry/Path.cc new file mode 100644 index 0000000000..8651108f9c --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Path.cc @@ -0,0 +1,194 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Path.h" + +namespace rl { +namespace geom { + +Path::Path() = default; + +Path::~Path() = default; + +size_t Path::componentCount() const { + return _components.size(); +} + +Path& Path::addLinearComponent(Point p1, Point p2) { + _linears.emplace_back(p1, p2); + _components.emplace_back(ComponentType::Linear, _linears.size() - 1); + return *this; +} + +Path& Path::addQuadraticComponent(Point p1, Point cp, Point p2) { + _quads.emplace_back(p1, cp, p2); + _components.emplace_back(ComponentType::Quadratic, _quads.size() - 1); + return *this; +} + +Path& Path::addCubicComponent(Point p1, Point cp1, Point cp2, Point p2) { + _cubics.emplace_back(p1, cp1, cp2, p2); + _components.emplace_back(ComponentType::Cubic, _cubics.size() - 1); + return *this; +} + +void Path::enumerateComponents(Applier linearApplier, + Applier quadApplier, + Applier cubicApplier) const { + size_t currentIndex = 0; + for (const auto& component : _components) { + switch (component.type) { + case ComponentType::Linear: + if (linearApplier) { + linearApplier(currentIndex, _linears[component.index]); + } + break; + case ComponentType::Quadratic: + if (quadApplier) { + quadApplier(currentIndex, _quads[component.index]); + } + break; + case ComponentType::Cubic: + if (cubicApplier) { + cubicApplier(currentIndex, _cubics[component.index]); + } + break; + } + currentIndex++; + } +} + +bool Path::linearComponentAtIndex(size_t index, + LinearPathComponent& linear) const { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Linear) { + return false; + } + + linear = _linears[_components[index].index]; + return true; +} + +bool Path::quadraticComponentAtIndex(size_t index, + QuadraticPathComponent& quadratic) const { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Quadratic) { + return false; + } + + quadratic = _quads[_components[index].index]; + return true; +} + +bool Path::cubicComponentAtIndex(size_t index, + CubicPathComponent& cubic) const { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Cubic) { + return false; + } + + cubic = _cubics[_components[index].index]; + return true; +} + +bool Path::updateLinearComponentAtIndex(size_t index, + const LinearPathComponent& linear) { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Linear) { + return false; + } + + _linears[_components[index].index] = linear; + return true; +} + +bool Path::updateQuadraticComponentAtIndex( + size_t index, + const QuadraticPathComponent& quadratic) { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Quadratic) { + return false; + } + + _quads[_components[index].index] = quadratic; + return true; +} + +bool Path::updateCubicComponentAtIndex(size_t index, + CubicPathComponent& cubic) { + if (index >= _components.size()) { + return false; + } + + if (_components[index].type != ComponentType::Cubic) { + return false; + } + + _cubics[_components[index].index] = cubic; + return true; +} + +void Path::smoothPoints(SmoothPointsEnumerator enumerator, + const SmoothingApproximation& approximation) const { + if (enumerator == nullptr) { + return; + } + + for (const auto& component : _components) { + switch (component.type) { + case ComponentType::Linear: { + if (!enumerator(_linears[component.index].smoothPoints())) { + return; + } + } break; + case ComponentType::Quadratic: { + if (!enumerator(_quads[component.index].smoothPoints(approximation))) { + return; + } + } break; + case ComponentType::Cubic: { + if (!enumerator(_cubics[component.index].smoothPoints(approximation))) { + return; + } + } break; + } + } +} + +Rect Path::boundingBox() const { + Rect box; + + for (const auto& linear : _linears) { + box = box.withPoints(linear.extrema()); + } + + for (const auto& quad : _quads) { + box = box.withPoints(quad.extrema()); + } + + for (const auto& cubic : _cubics) { + box = box.withPoints(cubic.extrema()); + } + + return box; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Path.h b/engine/src/flutter/impeller/impeller/geometry/Path.h new file mode 100644 index 0000000000..d36b730074 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Path.h @@ -0,0 +1,80 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "PathComponent.h" + +namespace rl { +namespace geom { + +class Path { + public: + enum class ComponentType : uint8_t { + Linear, + Quadratic, + Cubic, + }; + + Path(); + + ~Path(); + + size_t componentCount() const; + + Path& addLinearComponent(Point p1, Point p2); + + Path& addQuadraticComponent(Point p1, Point cp, Point p2); + + Path& addCubicComponent(Point p1, Point cp1, Point cp2, Point p2); + + template + using Applier = std::function; + void enumerateComponents(Applier linearApplier, + Applier quadApplier, + Applier cubicApplier) const; + + bool linearComponentAtIndex(size_t index, LinearPathComponent& linear) const; + + bool quadraticComponentAtIndex(size_t index, + QuadraticPathComponent& quadratic) const; + + bool cubicComponentAtIndex(size_t index, CubicPathComponent& cubic) const; + + bool updateLinearComponentAtIndex(size_t index, + const LinearPathComponent& linear); + + bool updateQuadraticComponentAtIndex(size_t index, + const QuadraticPathComponent& quadratic); + + bool updateCubicComponentAtIndex(size_t index, CubicPathComponent& cubic); + + using SmoothPointsEnumerator = std::function points)>; + void smoothPoints(SmoothPointsEnumerator enumerator, + const SmoothingApproximation& approximation) const; + + Rect boundingBox() const; + + private: + struct ComponentIndexPair { + ComponentType type; + size_t index; + + ComponentIndexPair() : type(ComponentType::Linear), index(0) {} + + ComponentIndexPair(ComponentType aType, size_t aIndex) + : type(aType), index(aIndex) {} + }; + + std::vector _components; + std::vector _linears; + std::vector _quads; + std::vector _cubics; +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/PathBuilder.cc b/engine/src/flutter/impeller/impeller/geometry/PathBuilder.cc new file mode 100644 index 0000000000..35a8b718ed --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/PathBuilder.cc @@ -0,0 +1,328 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "PathBuilder.h" + +namespace rl { +namespace geom { + +static const double kArcApproximationMagic = 0.551915024494; + +PathBuilder::PathBuilder() = default; + +PathBuilder::~PathBuilder() = default; + +Path PathBuilder::path() const { + return _prototype; +} + +PathBuilder& PathBuilder::moveTo(Point point, bool relative) { + _current = relative ? _current + point : point; + _subpathStart = _current; + return *this; +} + +PathBuilder& PathBuilder::close() { + lineTo(_subpathStart); + return *this; +} + +PathBuilder& PathBuilder::lineTo(Point point, bool relative) { + point = relative ? _current + point : point; + _prototype.addLinearComponent(_current, point); + _current = point; + return *this; +} + +PathBuilder& PathBuilder::horizontalLineTo(double x, bool relative) { + Point endpoint = + relative ? Point{_current.x + x, _current.y} : Point{x, _current.y}; + _prototype.addLinearComponent(_current, endpoint); + _current = endpoint; + return *this; +} + +PathBuilder& PathBuilder::verticalLineTo(double y, bool relative) { + Point endpoint = + relative ? Point{_current.x, _current.y + y} : Point{_current.x, y}; + _prototype.addLinearComponent(_current, endpoint); + _current = endpoint; + return *this; +} + +PathBuilder& PathBuilder::quadraticCurveTo(Point point, + Point controlPoint, + bool relative) { + point = relative ? _current + point : point; + controlPoint = relative ? _current + controlPoint : controlPoint; + _prototype.addQuadraticComponent(_current, controlPoint, point); + _current = point; + return *this; +} + +Point PathBuilder::reflectedQuadraticControlPoint1() const { + /* + * If there is no previous command or if the previous command was not a + * quadratic, assume the control point is coincident with the current point. + */ + if (_prototype.componentCount() == 0) { + return _current; + } + + QuadraticPathComponent quad; + if (!_prototype.quadraticComponentAtIndex(_prototype.componentCount() - 1, + quad)) { + return _current; + } + + /* + * The control point is assumed to be the reflection of the control point on + * the previous command relative to the current point. + */ + return (_current * 2.0) - quad.cp; +} + +PathBuilder& PathBuilder::smoothQuadraticCurveTo(Point point, bool relative) { + point = relative ? _current + point : point; + /* + * The reflected control point is absolute and we made the endpoint absolute + * too. So there the last argument is always false (i.e, not relative). + */ + quadraticCurveTo(point, reflectedQuadraticControlPoint1(), false); + return *this; +} + +PathBuilder& PathBuilder::cubicCurveTo(Point point, + Point controlPoint1, + Point controlPoint2, + bool relative) { + controlPoint1 = relative ? _current + controlPoint1 : controlPoint1; + controlPoint2 = relative ? _current + controlPoint2 : controlPoint2; + point = relative ? _current + point : point; + _prototype.addCubicComponent(_current, controlPoint1, controlPoint2, point); + _current = point; + return *this; +} + +Point PathBuilder::reflectedCubicControlPoint1() const { + /* + * If there is no previous command or if the previous command was not a + * cubic, assume the first control point is coincident with the current + * point. + */ + if (_prototype.componentCount() == 0) { + return _current; + } + + CubicPathComponent cubic; + if (!_prototype.cubicComponentAtIndex(_prototype.componentCount() - 1, + cubic)) { + return _current; + } + + /* + * The first control point is assumed to be the reflection of the second + * control point on the previous command relative to the current point. + */ + return (_current * 2.0) - cubic.cp2; +} + +PathBuilder& PathBuilder::smoothCubicCurveTo(Point point, + Point controlPoint2, + bool relative) { + auto controlPoint1 = reflectedCubicControlPoint1(); + controlPoint2 = relative ? _current + controlPoint2 : controlPoint2; + auto endpoint = relative ? _current + point : point; + + cubicCurveTo(endpoint, // endpoint + controlPoint1, // control point 1 + controlPoint2, // control point 2 + false // relative since all points are already absolute + ); + return *this; +} + +PathBuilder& PathBuilder::addRect(Rect rect) { + _current = rect.origin; + + auto topLeft = rect.origin; + auto bottomLeft = rect.origin + Point{0.0, rect.size.height}; + auto bottomRight = rect.origin + Point{rect.size.width, rect.size.height}; + auto topRight = rect.origin + Point{rect.size.width, 0.0}; + + _prototype.addLinearComponent(topLeft, bottomLeft) + .addLinearComponent(bottomLeft, bottomRight) + .addLinearComponent(bottomRight, topRight) + .addLinearComponent(topRight, topLeft); + + return *this; +} + +PathBuilder& PathBuilder::addCircle(const Point& center, double radius) { + _current = center + Point{0.0, radius}; + + const double diameter = radius * 2.0; + const double magic = kArcApproximationMagic * radius; + + _prototype.addCubicComponent( + {center.x + radius, center.y}, // + {center.x + radius + magic, center.y}, // + {center.x + diameter, center.y + radius - magic}, // + {center.x + diameter, center.y + radius} // + ); + + _prototype.addCubicComponent( + {center.x + diameter, center.y + radius}, // + {center.x + diameter, center.y + radius + magic}, // + {center.x + radius + magic, center.y + diameter}, // + {center.x + radius, center.y + diameter} // + ); + + _prototype.addCubicComponent( + {center.x + radius, center.y + diameter}, // + {center.x + radius - magic, center.y + diameter}, // + {center.x, center.y + radius + magic}, // + {center.x, center.y + radius} // + ); + + _prototype.addCubicComponent({center.x, center.y + radius}, // + {center.x, center.y + radius - magic}, // + {center.x + radius - magic, center.y}, // + {center.x + radius, center.y} // + ); + + return *this; +} + +PathBuilder& PathBuilder::addRoundedRect(Rect rect, double radius) { + return radius == 0.0 ? addRect(rect) + : addRoundedRect(rect, {radius, radius, radius, radius}); +} + +PathBuilder& PathBuilder::addRoundedRect(Rect rect, RoundingRadii radii) { + _current = rect.origin + Point{radii.topLeft, 0.0}; + + const double magicTopRight = kArcApproximationMagic * radii.topRight; + const double magicBottomRight = kArcApproximationMagic * radii.bottomRight; + const double magicBottomLeft = kArcApproximationMagic * radii.bottomLeft; + const double magicTopLeft = kArcApproximationMagic * radii.topLeft; + + /* + * Top line. + */ + _prototype.addLinearComponent( + {rect.origin.x + radii.topLeft, rect.origin.y}, + {rect.origin.x + rect.size.width - radii.topRight, rect.origin.y}); + + /* + * Top right arc. + */ + _prototype.addCubicComponent( + {rect.origin.x + rect.size.width - radii.topRight, rect.origin.y}, + {rect.origin.x + rect.size.width - radii.topRight + magicTopRight, + rect.origin.y}, + {rect.origin.x + rect.size.width, + rect.origin.y + radii.topRight - magicTopRight}, + {rect.origin.x + rect.size.width, rect.origin.y + radii.topRight}); + + /* + * Right line. + */ + _prototype.addLinearComponent( + {rect.origin.x + rect.size.width, rect.origin.y + radii.topRight}, + {rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height - radii.bottomRight}); + + /* + * Bottom right arc. + */ + _prototype.addCubicComponent( + {rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height - radii.bottomRight}, + {rect.origin.x + rect.size.width, + rect.origin.y + rect.size.height - radii.bottomRight + magicBottomRight}, + {rect.origin.x + rect.size.width - radii.bottomRight + magicBottomRight, + rect.origin.y + rect.size.height}, + {rect.origin.x + rect.size.width - radii.bottomRight, + rect.origin.y + rect.size.height}); + + /* + * Bottom line. + */ + _prototype.addLinearComponent( + {rect.origin.x + rect.size.width - radii.bottomRight, + rect.origin.y + rect.size.height}, + {rect.origin.x + radii.bottomLeft, rect.origin.y + rect.size.height}); + + /* + * Bottom left arc. + */ + _prototype.addCubicComponent( + {rect.origin.x + radii.bottomLeft, rect.origin.y + rect.size.height}, + {rect.origin.x + radii.bottomLeft - magicBottomLeft, + rect.origin.y + rect.size.height}, + {rect.origin.x, + rect.origin.y + rect.size.height - radii.bottomLeft + magicBottomLeft}, + {rect.origin.x, rect.origin.y + rect.size.height - radii.bottomLeft}); + + /* + * Left line. + */ + _prototype.addLinearComponent( + {rect.origin.x, rect.origin.y + rect.size.height - radii.bottomLeft}, + {rect.origin.x, rect.origin.y + radii.topLeft}); + + /* + * Top left arc. + */ + _prototype.addCubicComponent( + {rect.origin.x, rect.origin.y + radii.topLeft}, + {rect.origin.x, rect.origin.y + radii.topLeft - magicTopLeft}, + {rect.origin.x + radii.topLeft - magicTopLeft, rect.origin.y}, + {rect.origin.x + radii.topLeft, rect.origin.y}); + + return *this; +} + +PathBuilder& PathBuilder::addEllipse(const Point& center, const Size& radius) { + _current = center + Point{0.0, radius.height}; + + const Size diameter = {radius.width * 2.0, radius.height * 2.0}; + const Size magic = {kArcApproximationMagic * radius.width, + kArcApproximationMagic * radius.height}; + + _prototype.addCubicComponent( + {center.x + radius.width, center.y}, // + {center.x + radius.width + magic.width, center.y}, // + {center.x + diameter.width, center.y + radius.height - magic.height}, // + {center.x + diameter.width, center.y + radius.height} // + ); + + _prototype.addCubicComponent( + {center.x + diameter.width, center.y + radius.height}, // + {center.x + diameter.width, center.y + radius.height + magic.height}, // + {center.x + radius.width + magic.width, center.y + diameter.height}, // + {center.x + radius.width, center.y + diameter.height} // + ); + + _prototype.addCubicComponent( + {center.x + radius.width, center.y + diameter.height}, // + {center.x + radius.width - magic.width, center.y + diameter.height}, // + {center.x, center.y + radius.height + magic.height}, // + {center.x, center.y + radius.height} // + ); + + _prototype.addCubicComponent( + {center.x, center.y + radius.height}, // + {center.x, center.y + radius.height - magic.height}, // + {center.x + radius.width - magic.width, center.y}, // + {center.x + radius.width, center.y} // + ); + + return *this; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/PathBuilder.h b/engine/src/flutter/impeller/impeller/geometry/PathBuilder.h new file mode 100644 index 0000000000..ba023508b9 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/PathBuilder.h @@ -0,0 +1,90 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include "Path.h" +#include "Rect.h" +#include "flutter/fml/macros.h" + +namespace rl { +namespace geom { + +class PathBuilder { + public: + PathBuilder(); + + ~PathBuilder(); + + Path path() const; + + PathBuilder& moveTo(Point point, bool relative = false); + + PathBuilder& close(); + + PathBuilder& lineTo(Point point, bool relative = false); + + PathBuilder& horizontalLineTo(double x, bool relative = false); + + PathBuilder& verticalLineTo(double y, bool relative = false); + + PathBuilder& quadraticCurveTo(Point point, + Point controlPoint, + bool relative = false); + + PathBuilder& smoothQuadraticCurveTo(Point point, bool relative = false); + + PathBuilder& cubicCurveTo(Point point, + Point controlPoint1, + Point controlPoint2, + bool relative = false); + + PathBuilder& smoothCubicCurveTo(Point point, + Point controlPoint2, + bool relative = false); + + PathBuilder& addRect(Rect rect); + + PathBuilder& addRoundedRect(Rect rect, double radius); + + PathBuilder& addCircle(const Point& center, double radius); + + PathBuilder& addEllipse(const Point& center, const Size& size); + + struct RoundingRadii { + double topLeft; + double bottomLeft; + double topRight; + double bottomRight; + + RoundingRadii() + : topLeft(0.0), bottomLeft(0.0), topRight(0.0), bottomRight(0.0) {} + + RoundingRadii(double pTopLeft, + double pBottomLeft, + double pTopRight, + double pBottomRight) + : topLeft(pTopLeft), + bottomLeft(pBottomLeft), + topRight(pTopRight), + bottomRight(pBottomRight) {} + }; + + PathBuilder& addRoundedRect(Rect rect, RoundingRadii radii); + + private: + Point _subpathStart; + Point _current; + Path _prototype; + + Point reflectedQuadraticControlPoint1() const; + + Point reflectedCubicControlPoint1() const; + + FML_DISALLOW_COPY_AND_ASSIGN(PathBuilder); +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/PathComponent.cc b/engine/src/flutter/impeller/impeller/geometry/PathComponent.cc new file mode 100644 index 0000000000..f1ddeac764 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/PathComponent.cc @@ -0,0 +1,421 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "PathComponent.h" +#include + +namespace rl { +namespace geom { + +static const size_t kRecursionLimit = 32; +static const double kCurveCollinearityEpsilon = 1e-30; +static const double kCurveAngleToleranceEpsilon = 0.01; + +/* + * Based on: https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases + */ + +static inline double LinearSolve(double t, double p0, double p1) { + return p0 + t * (p1 - p0); +} + +static inline double QuadraticSolve(double t, double p0, double p1, double p2) { + return (1 - t) * (1 - t) * p0 + // + 2 * (1 - t) * t * p1 + // + t * t * p2; +} + +static inline double QuadraticSolveDerivative(double t, + double p0, + double p1, + double p2) { + return 2 * (1 - t) * (p1 - p0) + // + 2 * t * (p2 - p1); +} + +static inline double CubicSolve(double t, + double p0, + double p1, + double p2, + double p3) { + return (1 - t) * (1 - t) * (1 - t) * p0 + // + 3 * (1 - t) * (1 - t) * t * p1 + // + 3 * (1 - t) * t * t * p2 + // + t * t * t * p3; +} + +static inline double CubicSolveDerivative(double t, + double p0, + double p1, + double p2, + double p3) { + return -3 * p0 * (1 - t) * (1 - t) + // + p1 * (3 * (1 - t) * (1 - t) - 6 * (1 - t) * t) + + p2 * (6 * (1 - t) * t - 3 * t * t) + // + 3 * p3 * t * t; +} + +Point LinearPathComponent::solve(double time) const { + return { + LinearSolve(time, p1.x, p2.x), // x + LinearSolve(time, p1.y, p2.y), // y + }; +} + +std::vector LinearPathComponent::smoothPoints() const { + return {p1, p2}; +} + +std::vector LinearPathComponent::extrema() const { + return {p1, p2}; +} + +Point QuadraticPathComponent::solve(double time) const { + return { + QuadraticSolve(time, p1.x, cp.x, p2.x), // x + QuadraticSolve(time, p1.y, cp.y, p2.y), // y + }; +} + +Point QuadraticPathComponent::solveDerivative(double time) const { + return { + QuadraticSolveDerivative(time, p1.x, cp.x, p2.x), // x + QuadraticSolveDerivative(time, p1.y, cp.y, p2.y), // y + }; +} + +std::vector QuadraticPathComponent::smoothPoints( + const SmoothingApproximation& approximation) const { + CubicPathComponent elevated(*this); + return elevated.smoothPoints(approximation); +} + +std::vector QuadraticPathComponent::extrema() const { + CubicPathComponent elevated(*this); + return elevated.extrema(); +} + +Point CubicPathComponent::solve(double time) const { + return { + CubicSolve(time, p1.x, cp1.x, cp2.x, p2.x), // x + CubicSolve(time, p1.y, cp1.y, cp2.y, p2.y), // y + }; +} + +Point CubicPathComponent::solveDerivative(double time) const { + return { + CubicSolveDerivative(time, p1.x, cp1.x, cp2.x, p2.x), // x + CubicSolveDerivative(time, p1.y, cp1.y, cp2.y, p2.y), // y + }; +} + +/* + * Paul de Casteljau's subdivision with modifications as described in + * http://www.antigrain.com/research/adaptive_bezier/index.html. + * Refer to the diagram on that page for a description of the points. + */ +static void CubicPathSmoothenRecursive(const SmoothingApproximation& approx, + std::vector& points, + Point p1, + Point p2, + Point p3, + Point p4, + size_t level) { + if (level >= kRecursionLimit) { + return; + } + + /* + * Find all midpoints. + */ + auto p12 = (p1 + p2) / 2.0; + auto p23 = (p2 + p3) / 2.0; + auto p34 = (p3 + p4) / 2.0; + + auto p123 = (p12 + p23) / 2.0; + auto p234 = (p23 + p34) / 2.0; + + auto p1234 = (p123 + p234) / 2.0; + + /* + * Attempt approximation using single straight line. + */ + auto d = p4 - p1; + double d2 = fabs(((p2.x - p4.x) * d.y - (p2.y - p4.y) * d.x)); + double d3 = fabs(((p3.x - p4.x) * d.y - (p3.y - p4.y) * d.x)); + + double da1 = 0; + double da2 = 0; + double k = 0; + + switch ((static_cast(d2 > kCurveCollinearityEpsilon) << 1) + + static_cast(d3 > kCurveCollinearityEpsilon)) { + case 0: + /* + * All collinear OR p1 == p4. + */ + k = d.x * d.x + d.y * d.y; + if (k == 0) { + d2 = p1.distanceSquared(p2); + d3 = p4.distanceSquared(p3); + } else { + k = 1.0 / k; + da1 = p2.x - p1.x; + da2 = p2.y - p1.y; + d2 = k * (da1 * d.x + da2 * d.y); + da1 = p3.x - p1.x; + da2 = p3.y - p1.y; + d3 = k * (da1 * d.x + da2 * d.y); + + if (d2 > 0 && d2 < 1 && d3 > 0 && d3 < 1) { + /* + * Simple collinear case, 1---2---3---4. Leave just two endpoints. + */ + return; + } + + if (d2 <= 0) { + d2 = p2.distanceSquared(p1); + } else if (d2 >= 1) { + d2 = p2.distanceSquared(p4); + } else { + d2 = p2.distanceSquared({p1.x + d2 * d.x, p1.y + d2 * d.y}); + } + + if (d3 <= 0) { + d3 = p3.distanceSquared(p1); + } else if (d3 >= 1) { + d3 = p3.distanceSquared(p4); + } else { + d3 = p3.distanceSquared({p1.x + d3 * d.x, p1.y + d3 * d.y}); + } + } + + if (d2 > d3) { + if (d2 < approx.distanceToleranceSquare) { + points.emplace_back(p2); + return; + } + } else { + if (d3 < approx.distanceToleranceSquare) { + points.emplace_back(p3); + return; + } + } + break; + case 1: + /* + * p1, p2, p4 are collinear, p3 is significant. + */ + if (d3 * d3 <= approx.distanceToleranceSquare * (d.x * d.x + d.y * d.y)) { + if (approx.angleTolerance < kCurveAngleToleranceEpsilon) { + points.emplace_back(p23); + return; + } + + /* + * Angle Condition. + */ + da1 = ::fabs(::atan2(p4.y - p3.y, p4.x - p3.x) - + ::atan2(p3.y - p2.y, p3.x - p2.x)); + + if (da1 >= M_PI) { + da1 = 2.0 * M_PI - da1; + } + + if (da1 < approx.angleTolerance) { + points.emplace_back(p2); + points.emplace_back(p3); + return; + } + + if (approx.cuspLimit != 0.0) { + if (da1 > approx.cuspLimit) { + points.emplace_back(p3); + return; + } + } + } + break; + + case 2: + /* + * p1,p3,p4 are collinear, p2 is significant. + */ + if (d2 * d2 <= approx.distanceToleranceSquare * (d.x * d.x + d.y * d.y)) { + if (approx.angleTolerance < kCurveAngleToleranceEpsilon) { + points.emplace_back(p23); + return; + } + + /* + * Angle Condition. + */ + da1 = ::fabs(::atan2(p3.y - p2.y, p3.x - p2.x) - + ::atan2(p2.y - p1.y, p2.x - p1.x)); + + if (da1 >= M_PI) { + da1 = 2.0 * M_PI - da1; + } + + if (da1 < approx.angleTolerance) { + points.emplace_back(p2); + points.emplace_back(p3); + return; + } + + if (approx.cuspLimit != 0.0) { + if (da1 > approx.cuspLimit) { + points.emplace_back(p2); + return; + } + } + } + break; + + case 3: + /* + * Regular case. + */ + if ((d2 + d3) * (d2 + d3) <= + approx.distanceToleranceSquare * (d.x * d.x + d.y * d.y)) { + /* + * If the curvature doesn't exceed the distance_tolerance value + * we tend to finish subdivisions. + */ + if (approx.angleTolerance < kCurveAngleToleranceEpsilon) { + points.emplace_back(p23); + return; + } + + /* + * Angle & Cusp Condition. + */ + k = ::atan2(p3.y - p2.y, p3.x - p2.x); + da1 = ::fabs(k - ::atan2(p2.y - p1.y, p2.x - p1.x)); + da2 = ::fabs(::atan2(p4.y - p3.y, p4.x - p3.x) - k); + + if (da1 >= M_PI) { + da1 = 2.0 * M_PI - da1; + } + + if (da2 >= M_PI) { + da2 = 2.0 * M_PI - da2; + } + + if (da1 + da2 < approx.angleTolerance) { + /* + * Finally we can stop the recursion. + */ + points.emplace_back(p23); + return; + } + + if (approx.cuspLimit != 0.0) { + if (da1 > approx.cuspLimit) { + points.emplace_back(p2); + return; + } + + if (da2 > approx.cuspLimit) { + points.emplace_back(p3); + return; + } + } + } + break; + } + + /* + * Continue subdivision. + */ + CubicPathSmoothenRecursive(approx, points, p1, p12, p123, p1234, level + 1); + CubicPathSmoothenRecursive(approx, points, p1234, p234, p34, p4, level + 1); +} + +std::vector CubicPathComponent::smoothPoints( + const SmoothingApproximation& approximation) const { + std::vector points; + points.emplace_back(p1); + CubicPathSmoothenRecursive(approximation, points, p1, cp1, cp2, p2, 0); + points.emplace_back(p2); + return points; +} + +static inline bool NearEqual(double a, double b, double epsilon) { + return (a > (b - epsilon)) && (a < (b + epsilon)); +} + +static inline bool NearZero(double a) { + return NearEqual(a, 0.0, 1e-12); +} + +static void CubicPathBoundingPopulateValues(std::vector& values, + double p1, + double p2, + double p3, + double p4) { + const double a = 3.0 * (-p1 + 3.0 * p2 - 3.0 * p3 + p4); + const double b = 6.0 * (p1 - 2.0 * p2 + p3); + const double c = 3.0 * (p2 - p1); + + /* + * Boundary conditions. + */ + if (NearZero(a)) { + if (NearZero(b)) { + return; + } + + double t = -c / b; + if (t >= 0.0 && t <= 1.0) { + values.emplace_back(t); + } + return; + } + + double b2Minus4AC = (b * b) - (4.0 * a * c); + + if (b2Minus4AC < 0.0) { + return; + } + + double rootB2Minus4AC = ::sqrt(b2Minus4AC); + + { + double t = (-b + rootB2Minus4AC) / (2.0 * a); + if (t >= 0.0 && t <= 1.0) { + values.emplace_back(t); + } + } + + { + double t = (-b - rootB2Minus4AC) / (2.0 * a); + if (t >= 0.0 && t <= 1.0) { + values.emplace_back(t); + } + } +} + +std::vector CubicPathComponent::extrema() const { + /* + * As described in: https://pomax.github.io/bezierinfo/#extremities + */ + std::vector values; + + CubicPathBoundingPopulateValues(values, p1.x, cp1.x, cp2.x, p2.x); + CubicPathBoundingPopulateValues(values, p1.y, cp1.y, cp2.y, p2.y); + + std::vector points; + + for (const auto& value : values) { + points.emplace_back(solve(value)); + } + + return points; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/PathComponent.h b/engine/src/flutter/impeller/impeller/geometry/PathComponent.h new file mode 100644 index 0000000000..0acf40266d --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/PathComponent.h @@ -0,0 +1,111 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include + +#include "Rect.h" + +namespace rl { +namespace geom { + +struct SmoothingApproximation { + const double scale; + const double angleTolerance; + const double cuspLimit; + const double distanceToleranceSquare; + + SmoothingApproximation(/* default */) + : SmoothingApproximation(1.0 /* scale */, + 0.0 /* angle tolerance */, + 0.0 /* cusp limit */) {} + + SmoothingApproximation(double pScale, + double pAngleTolerance, + double pCuspLimit) + : scale(pScale), + angleTolerance(pAngleTolerance), + cuspLimit(pCuspLimit), + distanceToleranceSquare(0.5 * pScale * 0.5 * pScale) {} +}; + +struct LinearPathComponent { + Point p1; + Point p2; + + LinearPathComponent() {} + + LinearPathComponent(Point ap1, Point ap2) : p1(ap1), p2(ap2) {} + + Point solve(double time) const; + + std::vector smoothPoints() const; + + std::vector extrema() const; + + bool operator==(const LinearPathComponent& other) const { + return p1 == other.p1 && p2 == other.p2; + } +}; + +struct QuadraticPathComponent { + Point p1; + Point cp; + Point p2; + + QuadraticPathComponent() {} + + QuadraticPathComponent(Point ap1, Point acp, Point ap2) + : p1(ap1), cp(acp), p2(ap2) {} + + Point solve(double time) const; + + Point solveDerivative(double time) const; + + std::vector smoothPoints( + const SmoothingApproximation& approximation) const; + + std::vector extrema() const; + + bool operator==(const QuadraticPathComponent& other) const { + return p1 == other.p1 && cp == other.cp && p2 == other.p2; + } +}; + +struct CubicPathComponent { + Point p1; + Point cp1; + Point cp2; + Point p2; + + CubicPathComponent() {} + + CubicPathComponent(const QuadraticPathComponent& q) + : p1(q.p1), + cp1(q.p1 + (q.cp - q.p1) * (2.0 / 3.0)), + cp2(q.p2 + (q.cp - q.p2) * (2.0 / 3.0)), + p2(q.p2) {} + + CubicPathComponent(Point ap1, Point acp1, Point acp2, Point ap2) + : p1(ap1), cp1(acp1), cp2(acp2), p2(ap2) {} + + Point solve(double time) const; + + Point solveDerivative(double time) const; + + std::vector smoothPoints( + const SmoothingApproximation& approximation) const; + + std::vector extrema() const; + + bool operator==(const CubicPathComponent& other) const { + return p1 == other.p1 && cp1 == other.cp1 && cp2 == other.cp2 && + p2 == other.p2; + } +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Point.cc b/engine/src/flutter/impeller/impeller/geometry/Point.cc new file mode 100644 index 0000000000..b7cfd3805f --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Point.cc @@ -0,0 +1,26 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Point.h" +#include + +namespace rl { +namespace geom { + +std::string Point::toString() const { + std::stringstream stream; + stream << x << "," << y; + return stream.str(); +} + +void Point::fromString(const std::string& str) { + std::stringstream stream(str); + stream >> x; + stream.ignore(); + stream >> y; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Point.h b/engine/src/flutter/impeller/impeller/geometry/Point.h new file mode 100644 index 0000000000..77f3cc37e4 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Point.h @@ -0,0 +1,58 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "Size.h" + +namespace rl { +namespace geom { + +struct Point { + double x; + double y; + + Point() : x(0.0), y(0.0) {} + Point(double x, double y) : x(x), y(y) {} + + /* + * Operator overloads + */ + bool operator==(const Point& p) const { return p.x == x && p.y == y; } + bool operator!=(const Point& p) const { return p.x != x || p.y != y; } + + Point operator-() const { return {-x, -y}; } + + Point operator+(const Point& p) const { return {x + p.x, y + p.y}; } + Point operator+(const Size& s) const { return {x + s.width, y + s.height}; } + + Point operator-(const Point& p) const { return {x - p.x, y - p.y}; } + Point operator-(const Size& s) const { return {x - s.width, y - s.height}; } + + Point operator*(double scale) const { return {x * scale, y * scale}; } + Point operator*(const Point& p) const { return {x * p.x, y * p.y}; } + Point operator*(const Size& s) const { return {x * s.width, y * s.height}; } + + Point operator/(double d) const { return {x / d, y / d}; } + Point operator/(const Point& p) const { return {x / p.x, y / p.y}; } + Point operator/(const Size& s) const { return {x / s.width, y / s.height}; } + + double distanceSquared(const Point& p) const { + double dx = p.x - x; + double dy = p.y - y; + return dx * dx + dy * dy; + } + + double distance(const Point& p) const { return sqrt(distanceSquared(p)); } + + std::string toString() const; + + void fromString(const std::string& str); +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Quaternion.cc b/engine/src/flutter/impeller/impeller/geometry/Quaternion.cc new file mode 100644 index 0000000000..bae8cdd541 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Quaternion.cc @@ -0,0 +1,40 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Quaternion.h" +#include + +namespace rl { +namespace geom { + +Quaternion Quaternion::slerp(const Quaternion& to, double time) const { + double cosine = dot(to); + if (fabs(cosine) < 1.0 - 1e-3 /* epsilon */) { + /* + * Spherical Interpolation. + */ + auto sine = sqrt(1.0 - cosine * cosine); + auto angle = atan2(sine, cosine); + auto sineInverse = 1.0 / sine; + auto c0 = sin((1.0 - time) * angle) * sineInverse; + auto c1 = sin(time * angle) * sineInverse; + return *this * c0 + to * c1; + } else { + /* + * Linear Interpolation. + */ + return (*this * (1.0 - time) + to * time).normalize(); + } +} + +std::string Quaternion::toString() const { + std::stringstream stream; + stream << "{" << x << ", " + << ", " << y << ", " << z << ", " << w << "}"; + return stream.str(); +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Quaternion.h b/engine/src/flutter/impeller/impeller/geometry/Quaternion.h new file mode 100644 index 0000000000..2b97e4da81 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Quaternion.h @@ -0,0 +1,83 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include "Vector.h" + +namespace rl { +namespace geom { + +struct Quaternion { + union { + struct { + double x; + double y; + double z; + double w; + }; + double e[4]; + }; + + Quaternion() : x(0.0), y(0.0), z(0.0), w(1.0) {} + + Quaternion(double px, double py, double pz, double pw) + : x(px), y(py), z(pz), w(pw) {} + + Quaternion(const Vector3& axis, double angle) { + const auto sine = sin(angle * 0.5); + x = sine * axis.x; + y = sine * axis.y; + z = sine * axis.z; + w = cos(angle * 0.5); + } + + double dot(const Quaternion& q) const { + return x * q.x + y * q.y + z * q.z + w * q.w; + } + + double length() const { return sqrt(x * x + y * y + z * z + w * w); } + + Quaternion normalize() const { + auto m = 1.0 / length(); + return {x * m, y * m, z * m, w * m}; + } + + Quaternion slerp(const Quaternion& to, double time) const; + + Quaternion operator*(const Quaternion& o) const { + return { + w * o.x + x * o.w + y * o.z - z * o.y, + w * o.y + y * o.w + z * o.x - x * o.z, + w * o.z + z * o.w + x * o.y - y * o.x, + w * o.w - x * o.x - y * o.y - z * o.z, + }; + } + + Quaternion operator*(double scale) const { + return {scale * x, scale * y, scale * z, scale * w}; + } + + Quaternion operator+(const Quaternion& o) const { + return {x + o.x, y + o.y, z + o.z, w + o.w}; + } + + Quaternion operator-(const Quaternion& o) const { + return {x - o.x, y - o.y, z - o.z, w - o.w}; + } + + bool operator==(const Quaternion& o) const { + return x == o.x && y == o.y && z == o.z && w == o.w; + } + + bool operator!=(const Quaternion& o) const { + return x != o.x || y != o.y || z != o.z || w != o.w; + } + + std::string toString() const; +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Rect.cc b/engine/src/flutter/impeller/impeller/geometry/Rect.cc new file mode 100644 index 0000000000..878270f17c --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Rect.cc @@ -0,0 +1,62 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Rect.h" +#include + +namespace rl { +namespace geom { + +Rect Rect::withPoint(const Point& p) const { + Rect copy = *this; + if (p.x < origin.x) { + copy.origin.x = p.x; + copy.size.width += (origin.x - p.x); + } + + if (p.y < origin.y) { + copy.origin.y = p.y; + copy.size.height += (origin.y - p.y); + } + + if (p.x > (size.width + origin.x)) { + copy.size.width += p.x - (size.width + origin.x); + } + + if (p.y > (size.height + origin.y)) { + copy.size.height += p.y - (size.height + origin.y); + } + + return copy; +} + +Rect Rect::withPoints(const std::vector& points) const { + Rect box = *this; + for (const auto& point : points) { + box = box.withPoint(point); + } + return box; +} + +std::string Rect::toString() const { + std::stringstream stream; + stream << origin.x << "," << origin.y << "," << size.width << "," + << size.height; + return stream.str(); +} + +void Rect::fromString(const std::string& str) { + std::stringstream stream(str); + stream >> origin.x; + stream.ignore(); + stream >> origin.y; + stream.ignore(); + stream >> size.width; + stream.ignore(); + stream >> size.height; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Rect.h b/engine/src/flutter/impeller/impeller/geometry/Rect.h new file mode 100644 index 0000000000..21abc492a0 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Rect.h @@ -0,0 +1,72 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include "Point.h" +#include "Size.h" + +namespace rl { +namespace geom { + +struct Rect { + Point origin; + Size size; + + Rect() : origin({0.0, 0.0}), size({0.0, 0.0}) {} + Rect(Size size) : origin({0.0, 0.0}), size(size) {} + Rect(Point origin, Size size) : origin(origin), size(size) {} + Rect(const double components[4]) + : origin(components[0], components[1]), + size(components[2], components[3]) {} + Rect(double x, double y, double width, double height) + : origin(x, y), size(width, height) {} + + /* + * Operator overloads + */ + Rect operator+(const Rect& r) const { + return Rect({origin.x + r.origin.x, origin.y + r.origin.y}, + {size.width + r.size.width, size.height + r.size.height}); + } + + Rect operator-(const Rect& r) const { + return Rect({origin.x - r.origin.x, origin.y - r.origin.y}, + {size.width - r.size.width, size.height - r.size.height}); + } + + Rect operator*(double scale) const { + return Rect({origin.x * scale, origin.y * scale}, + {size.width * scale, size.height * scale}); + } + + Rect operator*(const Rect& r) const { + return Rect({origin.x * r.origin.x, origin.y * r.origin.y}, + {size.width * r.size.width, size.height * r.size.height}); + } + + bool operator==(const Rect& r) const { + return origin == r.origin && size == r.size; + } + + bool contains(const Point& p) const { + return p.x >= origin.x && p.x <= size.width && p.y >= origin.y && + p.y <= size.height; + } + + bool isZero() const { return size.isZero(); } + + Rect withPoint(const Point& p) const; + + Rect withPoints(const std::vector& points) const; + + std::string toString() const; + + void fromString(const std::string& str); +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Shear.cc b/engine/src/flutter/impeller/impeller/geometry/Shear.cc new file mode 100644 index 0000000000..dd880a9761 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Shear.cc @@ -0,0 +1,19 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Shear.h" +#include + +namespace rl { +namespace geom { + +std::string Shear::toString() const { + std::stringstream stream; + stream << "{" << xy << ", " << xz << ", " << yz << "}"; + return stream.str(); +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Shear.h b/engine/src/flutter/impeller/impeller/geometry/Shear.h new file mode 100644 index 0000000000..f229d08699 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Shear.h @@ -0,0 +1,37 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include + +namespace rl { +namespace geom { + +struct Shear { + union { + struct { + double xy; + double xz; + double yz; + }; + double e[3]; + }; + + Shear() : xy(0.0), xz(0.0), yz(0.0) {} + + Shear(double xy, double xz, double yz) : xy(xy), xz(xz), yz(yz) {} + + bool operator==(const Shear& o) const { + return xy == o.xy && xz == o.xz && yz == o.yz; + } + + bool operator!=(const Shear& o) const { return !(*this == o); } + + std::string toString() const; +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Size.cc b/engine/src/flutter/impeller/impeller/geometry/Size.cc new file mode 100644 index 0000000000..6bc5febf74 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Size.cc @@ -0,0 +1,26 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Size.h" +#include + +namespace rl { +namespace geom { + +std::string Size::toString() const { + std::stringstream stream; + stream << width << "," << height; + return stream.str(); +} + +void Size::fromString(const std::string& str) { + std::stringstream stream(str); + stream >> width; + stream.ignore(); + stream >> height; +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Size.h b/engine/src/flutter/impeller/impeller/geometry/Size.h new file mode 100644 index 0000000000..b2f2ab8229 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Size.h @@ -0,0 +1,59 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include + +namespace rl { +namespace geom { + +struct Size { + double width; + double height; + + Size() : width(0.0), height(0.0) {} + + Size(double width, double height) : width(width), height(height) {} + + /* + * Operator overloads + */ + Size operator*(double scale) const { return {width * scale, height * scale}; } + + bool operator==(const Size& s) const { + return s.width == width && s.height == height; + } + + bool operator!=(const Size& s) const { + return s.width != width || s.height != height; + } + + Size operator+(const Size& s) const { + return {width + s.width, height + s.height}; + } + + Size operator-(const Size& s) const { + return {width - s.width, height - s.height}; + } + + Size unionWith(const Size& o) const { + return { + std::max(width, o.width), + std::max(height, o.height), + }; + } + + bool isZero() const { return width * height == 0.0; } + + bool isPositive() const { return width > 0.0 && height > 0.0; } + + std::string toString() const; + + void fromString(const std::string& str); +}; + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Vector.cc b/engine/src/flutter/impeller/impeller/geometry/Vector.cc new file mode 100644 index 0000000000..ad20c487e6 --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Vector.cc @@ -0,0 +1,25 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#include "Vector.h" +#include + +namespace rl { +namespace geom { + +std::string Vector3::toString() const { + std::stringstream stream; + stream << "{" << x << ", " << y << ", " << z << "}"; + return stream.str(); +} + +std::string Vector4::toString() const { + std::stringstream stream; + stream << "{" << x << ", " << y << ", " << z << ", " << w << "}"; + return stream.str(); +} + +} // namespace geom +} // namespace rl diff --git a/engine/src/flutter/impeller/impeller/geometry/Vector.h b/engine/src/flutter/impeller/impeller/geometry/Vector.h new file mode 100644 index 0000000000..0457d1911a --- /dev/null +++ b/engine/src/flutter/impeller/impeller/geometry/Vector.h @@ -0,0 +1,146 @@ +/* + * This source file is part of the Radar project. + * Licensed under the MIT License. See LICENSE file for details. + */ + +#pragma once + +#include +#include +#include "Point.h" +#include "Size.h" + +namespace rl { +namespace geom { + +struct Vector3 { + union { + struct { + double x; + double y; + double z; + }; + double e[3]; + }; + + Vector3() : x(0.0), y(0.0), z(0.0) {} + + Vector3(const Point& p) : x(p.x), y(p.y), z(0.0) {} + + Vector3(const Size& s) : x(s.width), y(s.height), z(0.0) {} + + Vector3(double x, double y) : x(x), y(y), z(0.0) {} + + Vector3(double x, double y, double z) : x(x), y(y), z(z) {} + + /** + * The length (or magnitude of the vector). + * + * @return the calculated length. + */ + double length() const { return sqrt(x * x + y * y + z * z); } + + Vector3 normalize() const { + const auto len = length(); + return {x / len, y / len, z / len}; + } + + double dot(const Vector3& other) const { + return ((x * other.x) + (y * other.y) + (z * other.z)); + } + + Vector3 cross(const Vector3& other) const { + return { + (y * other.z) - (z * other.y), // + (z * other.x) - (x * other.z), // + (x * other.y) - (y * other.x) // + }; + } + + bool operator==(const Vector3& v) const { + return v.x == x && v.y == y && v.z == z; + } + + bool operator!=(const Vector3& v) const { + return v.x != x || v.y != y || v.z != z; + } + + Vector3 operator-() const { return Vector3(-x, -y, -z); } + + Vector3 operator+(const Vector3& v) const { + return Vector3(x + v.x, y + v.y, z + v.z); + } + + Vector3 operator-(const Vector3& v) const { + return Vector3(x - v.x, y - v.y, z - v.z); + } + + /** + * Make a linear combination of two vectors and return the result. + * + * @param a the first vector. + * @param aScale the scale to use for the first vector. + * @param b the second vector. + * @param bScale the scale to use for the second vector. + * + * @return the combined vector. + */ + static inline Vector3 Combine(const Vector3& a, + double aScale, + const Vector3& b, + double bScale) { + return { + aScale * a.x + bScale * b.x, // + aScale * a.y + bScale * b.y, // + aScale * a.z + bScale * b.z, // + }; + } + + std::string toString() const; +}; + +struct Vector4 { + union { + struct { + double x; + double y; + double z; + double w; + }; + double e[4]; + }; + + Vector4() : x(0.0), y(0.0), z(0.0), w(1.0) {} + + Vector4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {} + + Vector4(const Vector3& v) : x(v.x), y(v.y), z(v.z), w(1.0) {} + + Vector4(const Point& p) : x(p.x), y(p.y), z(0.0), w(1.0) {} + + Vector4 normalize() const { + const double inverse = 1.0 / sqrt(x * x + y * y + z * z + w * w); + return Vector4(x * inverse, y * inverse, z * inverse, w * inverse); + } + + bool operator==(const Vector4& v) const { + return (x == v.x) && (y == v.y) && (z == v.z) && (w == v.w); + } + + bool operator!=(const Vector4& v) const { + return (x != v.x) || (y != v.y) || (z != v.z) || (w != v.w); + } + + Vector4 operator+(const Vector4& v) const { + return Vector4(x + v.x, y + v.y, z + v.z, w + v.w); + } + + Vector4 operator-(const Vector4& v) const { + return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); + } + + std::string toString() const; +}; + +} // namespace geom +} // namespace rl