Skip to content

添加常用向量结构和一些特定形状结构 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 69 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
5d8e690
添加 ffi 版本的 Vector2/Vector3/Vector4
BAKAOLC Apr 28, 2025
5c26343
重命名类型标识名
BAKAOLC Apr 28, 2025
d123dc3
添加部分常用类型结构
BAKAOLC Apr 28, 2025
9a40e95
为布尔数组、双精度数组和整型数组添加偏移量支持,更新创建函数和索引方法以处理偏移量
BAKAOLC Apr 29, 2025
16ae5df
add rotation methods to Vector2
BAKAOLC Apr 29, 2025
4271d13
refactor Vector2 rotation methods to use radians and improve document…
BAKAOLC Apr 29, 2025
6ba4458
add cross product method to Vector2
BAKAOLC Apr 29, 2025
27cbf2b
add Triangle class with geometric operations and methods
BAKAOLC Apr 29, 2025
ab034da
add methods to create Vector2 from radians and angles
BAKAOLC Apr 29, 2025
f81de14
add move and moved methods to Triangle for translation operations
BAKAOLC Apr 29, 2025
f7fd944
add Line class for geometric operations
BAKAOLC Apr 29, 2025
868ed00
rename Line class to Segment and update methods accordingly
BAKAOLC Apr 29, 2025
a6ec037
refactor: update struct names to include foundation prefix for consis…
BAKAOLC Apr 29, 2025
4163969
feat: add intersection methods for Segment and Triangle classes
BAKAOLC Apr 29, 2025
4294d26
feat: add Circle, Line, Ray classes and intersection methods for geom…
BAKAOLC Apr 29, 2025
a60abcf
feat: update intersection methods to return multiple points for geome…
BAKAOLC Apr 29, 2025
08c097d
feat: add intersection methods for Circle, Line, Ray, Segment, and Tr…
BAKAOLC Apr 29, 2025
7427682
feat: add intersection methods for Rectangle in Circle, Line, Ray, Se…
BAKAOLC Apr 29, 2025
4e026cc
feat: add Rectangle class with geometric operations and intersection …
BAKAOLC Apr 29, 2025
885444b
feat: 添加获取三角形顶点和边的功能,优化相交检测方法
BAKAOLC Apr 29, 2025
7ccbc4a
feat: add conversion methods for Vector2, Vector3, and Vector4, and e…
BAKAOLC Apr 29, 2025
83295d5
feat: add Sector class with intersection methods for Circle, Line, Ra…
BAKAOLC Apr 29, 2025
5e44fde
refactor: remove unused local variables from Line, Ray, and Segment m…
BAKAOLC Apr 29, 2025
7f8fed5
fix comment
BAKAOLC Apr 29, 2025
7003c4a
refactor: simplify intersection checks by utilizing ShapeIntersector …
BAKAOLC Apr 29, 2025
b31c635
feat: add angle and rotation methods for Line, Ray, Rectangle, and Se…
BAKAOLC Apr 29, 2025
731b0ee
refactor: reorganize Triangle intersection methods in ShapeIntersector
BAKAOLC Apr 29, 2025
13f0071
refactor: update sector direction calculations to use angle method
BAKAOLC Apr 29, 2025
d2a031c
refactor: consolidate sector direction and range properties in Sector…
BAKAOLC Apr 30, 2025
dae0a1c
refactor: optimize point containment checks in Sector and ShapeInters…
BAKAOLC Apr 30, 2025
892523d
refactor: update equality checks and distance comparisons to use incl…
BAKAOLC Apr 30, 2025
a12e824
refactor: add methods to calculate closest points on Line and Ray
BAKAOLC Apr 30, 2025
2c53ce4
refactor: rename nearestPoint method to closestPoint in Ray class
BAKAOLC Apr 30, 2025
18ce64e
refactor: simplify direction calculations and streamline closestPoint…
BAKAOLC Apr 30, 2025
d8882e8
refactor: simplify point containment and closest point calculations i…
BAKAOLC Apr 30, 2025
a2791e2
refactor: add perimeter calculation methods for Circle, Rectangle, Se…
BAKAOLC Apr 30, 2025
c5ffcec
feat: implement Polygon class with geometric operations and intersect…
BAKAOLC Apr 30, 2025
42d68a1
refactor: change size type from size_t to int in Polygon struct
BAKAOLC Apr 30, 2025
4265b4a
feat: add containsPoint method to Polygon class for point containment…
BAKAOLC Apr 30, 2025
f8af2e4
feat: add createFromTable methods for Vector2, Vector3, and Vector4 c…
BAKAOLC Apr 30, 2025
0d508c9
feat: add garbage collection for Polygon creation to manage memory
BAKAOLC Apr 30, 2025
656ca90
refactor: remove debug print statement from garbage collection in Pol…
BAKAOLC Apr 30, 2025
4168029
feat: implement custom __index and __newindex methods for Polygon class
BAKAOLC May 1, 2025
05434b0
feat: enhance convexity check in Polygon class for improved validity …
BAKAOLC May 1, 2025
71187be
feat: add rotation and incenter calculations for Circle and Rectangle…
BAKAOLC May 1, 2025
20a5044
feat: add laboratory for geometry
BAKAOLC May 1, 2025
21132b2
feat: add rendering for projected points in object visualization
BAKAOLC May 1, 2025
b9e14e8
feat: update closestPoint method in Circle class to return input poin…
BAKAOLC May 1, 2025
815decf
feat: add boundary parameter to closestPoint methods in geometric shapes
BAKAOLC May 1, 2025
a2be53f
feat: add centroid and getCenter methods for Circle, Polygon, Rectang…
BAKAOLC May 1, 2025
4d2d4d9
feat: add getCenter and getBoundingBoxSize methods for Circle, Line, …
BAKAOLC May 1, 2025
e52bd60
add MIT LICENSE about this
BAKAOLC May 1, 2025
ad0a104
feat: implement __index and __newindex metamethods for Circle, Line, …
BAKAOLC May 1, 2025
a0e9cb1
feat: add rendering toggle options for various geometric features in …
BAKAOLC May 1, 2025
76b65d4
fix: handle NaN values in player position calculation
BAKAOLC May 1, 2025
a2d7d08
fix: rename data reference for points in Polygon class
BAKAOLC May 1, 2025
55fdcba
feat: add clone method for geometric shape classes
BAKAOLC May 1, 2025
03ac1c4
重新整理代码结构
BAKAOLC May 1, 2025
cd0ccbc
feat: add Ellipse and BezierCurve classes with intersection methods
BAKAOLC May 1, 2025
ef13702
feat: render BezierCurve control points in the main drawing function
BAKAOLC May 1, 2025
4548651
refactor: simplify point transformation logic in Ellipse class
BAKAOLC May 1, 2025
18cd0ef
feat: add segment parameter to containsPoint method for BezierCurve
BAKAOLC May 1, 2025
ffbc84d
feat: add methods for equal arc length points and segments in BezierC…
BAKAOLC May 2, 2025
eb7d039
Merge pull request #1 from BAKAOLC/ffi_struct
BAKAOLC May 2, 2025
db468f0
fix: address parameter type mismatch warnings in ellipse intersection…
BAKAOLC May 2, 2025
049a6c5
feat: add matrix and transformation classes for 2D and 3D operations
BAKAOLC May 2, 2025
cb8fc60
feat: add AABB bounding box methods for geometric shapes
BAKAOLC May 10, 2025
b2c52ac
feat: enhance geometric shapes with rotation and scaling methods supp…
BAKAOLC May 10, 2025
dec237c
feat: 添加线段取点方法
BAKAOLC May 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions game/packages/thlib-scripts-v2/foundation/math/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 OLC

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.
296 changes: 296 additions & 0 deletions game/packages/thlib-scripts-v2/foundation/math/Vector2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
local ffi = require("ffi")

local type = type
local string = string
local math = math
local require = require

local Vector3, Vector4

ffi.cdef [[
typedef struct {
double x;
double y;
} foundation_math_Vector2;
]]

---@class foundation.math.Vector2
---@field x number X坐标分量
---@field y number Y坐标分量
local Vector2 = {}
Vector2.__index = Vector2
Vector2.__type = "foundation.math.Vector2"

---创建一个零向量
---@return foundation.math.Vector2 零向量
function Vector2.zero()
return Vector2.create(0, 0)
end

---创建一个新的二维向量
---@param x number|nil X坐标分量,默认为0
---@param y number|nil Y坐标分量,默认为0
---@return foundation.math.Vector2 新创建的向量
function Vector2.create(x, y)
---@diagnostic disable-next-line: return-type-mismatch
return ffi.new("foundation_math_Vector2", x or 0, y or 0)
end

---通过特定结构的对象创建一个新的二维向量
---@param tbl table|foundation.math.Vector2 表或向量
---@return foundation.math.Vector2 新创建的向量
function Vector2.createFromTable(tbl)
if tbl.x and tbl.y then
return Vector2.create(tbl.x, tbl.y)
end
if tbl[1] and tbl[2] then
return Vector2.create(tbl[1], tbl[2])
end
error("Invalid table format for Vector2 creation")
end

---通过给定的弧度和长度创建一个新的二维向量
---@param rad number 弧度
---@param length number 向量长度
---@return foundation.math.Vector2 新创建的向量
---@overload fun(rad: number): foundation.math.Vector2
function Vector2.createFromRad(rad, length)
length = length or 1
local x = length * math.cos(rad)
local y = length * math.sin(rad)
return Vector2.create(x, y)
end

---通过给定的角度和长度创建一个新的二维向量
---@param angle number 角度
---@param length number 向量长度
---@return foundation.math.Vector2 新创建的向量
---@overload fun(angle: number): foundation.math.Vector2
function Vector2.createFromAngle(angle, length)
local rad = math.rad(angle)
return Vector2.createFromRad(rad, length)
end

---向量加法运算符重载
---@param a foundation.math.Vector2|number 第一个操作数
---@param b foundation.math.Vector2|number 第二个操作数
---@return foundation.math.Vector2 相加后的结果
function Vector2.__add(a, b)
if type(a) == "number" then
return Vector2.create(a + b.x, a + b.y)
elseif type(b) == "number" then
return Vector2.create(a.x + b, a.y + b)
else
return Vector2.create(a.x + b.x, a.y + b.y)
end
end

---向量减法运算符重载
---@param a foundation.math.Vector2|number 第一个操作数
---@param b foundation.math.Vector2|number 第二个操作数
---@return foundation.math.Vector2 相减后的结果
function Vector2.__sub(a, b)
if type(a) == "number" then
return Vector2.create(a - b.x, a - b.y)
elseif type(b) == "number" then
return Vector2.create(a.x - b, a.y - b)
else
return Vector2.create(a.x - b.x, a.y - b.y)
end
end

---向量乘法运算符重载
---@param a foundation.math.Vector2|number 第一个操作数
---@param b foundation.math.Vector2|number 第二个操作数
---@return foundation.math.Vector2 相乘后的结果
function Vector2.__mul(a, b)
if type(a) == "number" then
return Vector2.create(a * b.x, a * b.y)
elseif type(b) == "number" then
return Vector2.create(a.x * b, a.y * b)
else
return Vector2.create(a.x * b.x, a.y * b.y)
end
end

---向量除法运算符重载
---@param a foundation.math.Vector2|number 第一个操作数
---@param b foundation.math.Vector2|number 第二个操作数
---@return foundation.math.Vector2 相除后的结果
function Vector2.__div(a, b)
if type(a) == "number" then
return Vector2.create(a / b.x, a / b.y)
elseif type(b) == "number" then
return Vector2.create(a.x / b, a.y / b)
else
return Vector2.create(a.x / b.x, a.y / b.y)
end
end

---向量取负运算符重载
---@param v foundation.math.Vector2 操作数
---@return foundation.math.Vector2 取反后的向量
function Vector2.__unm(v)
return Vector2.create(-v.x, -v.y)
end

---向量相等性比较运算符重载
---@param a foundation.math.Vector2 第一个操作数
---@param b foundation.math.Vector2 第二个操作数
---@return boolean 两个向量是否相等
function Vector2.__eq(a, b)
return math.abs(a.x - b.x) <= 1e-10 and math.abs(a.y - b.y) <= 1e-10
end

---向量字符串表示
---@param v foundation.math.Vector2 操作数
---@return string 向量的字符串表示
function Vector2.__tostring(v)
return string.format("Vector2(%f, %f)", v.x, v.y)
end

---获取向量长度
---@param v foundation.math.Vector2 操作数
---@return number 向量的长度
function Vector2.__len(v)
return math.sqrt(v.x * v.x + v.y * v.y)
end

Vector2.length = Vector2.__len

---获取向量的副本
---@return foundation.math.Vector2 向量的副本
function Vector2:clone()
return Vector2.create(self.x, self.y)
end

---获取向量的角度(弧度)
---@return number 向量的角度,单位为弧度
function Vector2:angle()
return math.atan2(self.y, self.x)
end

---获取向量的角度(度)
---@return number 向量的角度,单位为度
function Vector2:degreeAngle()
return math.deg(self:angle())
end

---计算两个向量的点积
---@param other foundation.math.Vector2 另一个向量
---@return number 两个向量的点积
function Vector2:dot(other)
return self.x * other.x + self.y * other.y
end

---计算两个向量的叉积
---@param other foundation.math.Vector2 另一个向量
---@return number 两个向量的叉积
function Vector2:cross(other)
return self.x * other.y - self.y * other.x
end

---将当前向量归一化(更改当前向量)
---@return foundation.math.Vector2 归一化后的向量(自身引用)
function Vector2:normalize()
local len = self:length()
if len > 1e-10 then
self.x = self.x / len
self.y = self.y / len
else
self.x = 0
self.y = 0
end
return self
end

---获取向量的归一化副本
---@return foundation.math.Vector2 归一化后的向量副本
function Vector2:normalized()
local len = self:length()
if len <= 1e-10 then
return Vector2.zero()
end
return Vector2.create(self.x / len, self.y / len)
end

---将当前向量旋转指定弧度(更改当前向量)
---@param rad number 旋转弧度
---@return foundation.math.Vector2 旋转后的向量(自身引用)
function Vector2:rotate(rad)
local cos = math.cos(rad)
local sin = math.sin(rad)
local x = self.x * cos - self.y * sin
local y = self.x * sin + self.y * cos
self.x = x
self.y = y
return self
end

---将向量旋转指定角度(更改当前向量)
---@param angle number 旋转角度
---@return foundation.math.Vector2 旋转后的向量(自身引用)
function Vector2:degreeRotate(angle)
angle = math.rad(angle)
return self:rotate(angle)
end

---获取向量的旋转指定弧度的副本
---@param rad number 旋转弧度
---@return foundation.math.Vector2 旋转后的向量副本
function Vector2:rotated(rad)
local cos = math.cos(rad)
local sin = math.sin(rad)
local x = self.x * cos - self.y * sin
local y = self.x * sin + self.y * cos
return Vector2.create(x, y)
end

---获取向量的旋转指定角度的副本
---@param angle number 旋转角度
---@return foundation.math.Vector2 旋转后的向量副本
function Vector2:degreeRotated(angle)
angle = math.rad(angle)
return self:rotated(angle)
end

---将Vector2转换为Vector3
---@param z number|nil Z坐标分量,默认为0
---@return foundation.math.Vector3 转换后的Vector3
function Vector2:toVector3(z)
Vector3 = Vector3 or require("foundation.math.Vector3")
return Vector3.create(self.x, self.y, z or 0)
end

---将Vector2转换为Vector4
---@param z number|nil Z坐标分量,默认为0
---@param w number|nil W坐标分量,默认为0
---@return foundation.math.Vector4 转换后的Vector4
function Vector2:toVector4(z, w)
Vector4 = Vector4 or require("foundation.math.Vector4")
return Vector4.create(self.x, self.y, z or 0, w or 0)
end

--region LuaSTG Evo API
do
Vector2.LuaSTG = Vector2.length
Vector2.Angle = Vector2.degreeAngle

---归一化向量(LuaSTG Evo 兼容)
---@return foundation.math.Vector2 归一化后的向量副本
function Vector2:Normalize()
self:normalize()
return Vector2.create(self.x, self.y)
end

Vector2.Normalized = Vector2.normalized
Vector2.Dot = Vector2.dot

local lstg = require("lstg")
lstg.Vector2 = Vector2.create
end
--endregion

ffi.metatype("foundation_math_Vector2", Vector2)

return Vector2
Loading