Minetest-WorldEditAdditions/worldeditadditions/utils/vector.lua
Starbeamrainbowlabs fd5804dd9c
//erode: Finish the initial round of bugfixing, but I'm on the fence about it.
Specifically, I'm unsure about whether I'm happy with the effects of the 
algorithm.
Also, we convolve with a 3x3 gaussian kernel after erosion is complete - 
and we have verified that the erosion is having an positive effect at 
"roughening up" a terrain surface.
It seems like the initial blog post was correct: the algorithm does tend 
to make steep surfaces steeper.
It also appears that it's more effective on larger areas, and 'gentler' 
curves. THis might be because the surface normals are more conducive to 
making the snowballs roll.
Finally, we need to decide whether we want to keep the precomputed 
normals as we have now, or whether we want to dynamically compute them 
at the some of request.
2020-08-21 20:59:50 +01:00

42 lines
1.2 KiB
Lua

worldeditadditions.vector = {}
function worldeditadditions.vector.tostring(v)
return "(" .. v.x ..", " .. v.y ..", " .. v.z ..")"
end
-- Calculates the length squared of the given vector.
-- @param v Vector The vector to operate on
-- @return number The length of the given vector squared
function worldeditadditions.vector.lengthsquared(v)
if not v.y then return v.x*v.x + v.z*v.z end
return v.x*v.x + v.y*v.y + v.z*v.z
end
--- Normalises the given vector such that its length is 1.
-- Also known as calculating the unit vector.
-- This method does *not* mutate.
-- @param v Vector The vector to calculate from.
-- @return Vector A new normalised vector.
function worldeditadditions.vector.normalize(v)
local length = math.sqrt(worldeditadditions.vector.lengthsquared(v))
if not v.y then return {
x = v.x / length,
z = v.z / length
} end
return {
x = v.x / length,
y = v.y / length,
z = v.z / length
}
end
--- Rounds the values in a vector down.
-- Warning: This MUTATES the given vector!
-- @param v Vector The vector to operate on
function worldeditadditions.vector.floor(v)
v.x = math.floor(v.x)
-- Some vectors are 2d, but on the x / z axes
if v.y then v.y = math.floor(v.y) end
-- Some vectors are 2d
if v.z then v.z = math.floor(v.z) end
end