Skip to content

Commit

Permalink
✨ bs.vector:fast_normalize now also return normalization factor + ✅ a…
Browse files Browse the repository at this point in the history
…dded dedicated unit test
  • Loading branch information
VForiel committed Aug 3, 2023
1 parent 84a2991 commit 318da12
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
#__________________________________________________
# INFO Copyright © 2021 Altearn.
# INFO ------------------------------------------------------------------------
# Copyright © 2023 Gunivers Community.

# Authors: Leirof
# Contributors:
# MC Version: 1.13
# Last check:
# Authors : Leirof
# Contributors :

# Original path: bs.vector:classic/normalize
# Documentation: https://bs-core.readthedocs.io//entity#vector
# Parallelizable: <true/false/global>
# Note:
# Version: 1.0
# Created: 19/02/2023 (1.19.2)
# Last verification: 21/05/2023 (1.19.4)
# Last modification: 21/05/2023 (1.19.4)

#__________________________________________________
# PARAMETERS
# Original path : bs.vecotr:fast_normalize
# Documentation : https://bookshelf.docs.gunivers.net/en/latest/vector#normalize
# Note :

# Input: bs.vector.x (score dummy)
# Input: bs.vector.y (score dummy)
# Input: bs.vector.z (score dummy)
# CODE ------------------------------------------------------------------------

# Output: bs.vector.x (score dummy)
# Output: bs.vector.y (score dummy)
# Output: bs.vector.z (score dummy)

#__________________________________________________
# INIT

scoreboard objectives add bs.vector.x dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector X","color":"aqua"}]
scoreboard objectives add bs.vector.y dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector Y","color":"aqua"}]
scoreboard objectives add bs.vector.z dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector Z","color":"aqua"}]

#__________________________________________________
# CONFIG

# The final vector will have (almost) this length
execute store success score #vector.fast_normalize.length bs.data run scoreboard players get @s bs.opt.0
execute if score #vector.fast_normalize.length bs.data matches 1 run scoreboard players operation #vector.fast_normalize.length bs.data = @s bs.opt.0
execute if score #vector.fast_normalize.length bs.data matches 0 run scoreboard players set #vector.fast_normalize.length bs.data 1000
scoreboard players reset @s bs.opt.0

#__________________________________________________
# CODE

### DEBUG
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":"> Bookshelf | ","color":"dark_aqua"},{"text":"Record from bs.vector:fast_normalize","color":"green","clickEvent":{"action":"run_command","value":"/tag @s remove bs.debug.vector.fast_normalize"},"hoverEvent":{"action":"show_text","contents":"Hide this debug"}}]
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":" Input: Vx=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3 Vy=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3 Vz=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3","color":"gray"}]
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":" Normalization lenght=","color":"gray"},{"score":{"name":"#vector.fast_normalize.length","objective":"bs.data"},"color":"aqua"},{"text":"e-3","color":"gray"}]
### END DEBUG
# Lenght after normalization
execute unless score @s bs.opt.0 matches 1.. run scoreboard players set @s bs.opt.0 1000

# Getting absolute value of each component
scoreboard players operation #vector.fast_normalize.x bs.data = @s bs.vector.x
scoreboard players operation #vector.fast_normalize.y bs.data = @s bs.vector.y
scoreboard players operation #vector.fast_normalize.z bs.data = @s bs.vector.z
Expand All @@ -55,24 +27,26 @@ execute if score #vector.fast_normalize.x bs.data matches ..-1 run scoreboard pl
execute if score #vector.fast_normalize.y bs.data matches ..-1 run scoreboard players operation #vector.fast_normalize.y bs.data *= -1 bs.const
execute if score #vector.fast_normalize.z bs.data matches ..-1 run scoreboard players operation #vector.fast_normalize.z bs.data *= -1 bs.const

# Getting the maximum component of the vector
scoreboard players operation #vector.fast_normalize.max bs.data = #vector.fast_normalize.x bs.data
execute if score #vector.fast_normalize.y bs.data > #vector.fast_normalize.max bs.data run scoreboard players operation #vector.fast_normalize.max bs.data = #vector.fast_normalize.y bs.data
execute if score #vector.fast_normalize.z bs.data > #vector.fast_normalize.max bs.data run scoreboard players operation #vector.fast_normalize.max bs.data = #vector.fast_normalize.z bs.data

scoreboard players operation @s bs.vector.x *= #vector.fast_normalize.length bs.data
scoreboard players operation @s bs.vector.y *= #vector.fast_normalize.length bs.data
scoreboard players operation @s bs.vector.z *= #vector.fast_normalize.length bs.data
# Multiplying the vector by the final length
scoreboard players operation @s bs.vector.x *= @s bs.opt.0
scoreboard players operation @s bs.vector.y *= @s bs.opt.0
scoreboard players operation @s bs.vector.z *= @s bs.opt.0

# Dividing the vector by the maximum component
scoreboard players operation @s bs.vector.x /= #vector.fast_normalize.max bs.data
scoreboard players operation @s bs.vector.y /= #vector.fast_normalize.max bs.data
scoreboard players operation @s bs.vector.z /= #vector.fast_normalize.max bs.data

scoreboard players set @s bs.out.0 1000
scoreboard players operation @s bs.out.0 *= #vector.fast_normalize.length bs.data
scoreboard players operation @s bs.out.0 /= #vector.fast_normalize.max bs.data
# Computing normalization factor (with 3 decimals)
# Vi = A * Vn ==> A = Vi / Vn
scoreboard players operation @s bs.out.0 = #vector.fast_normalize.max bs.data
scoreboard players operation @s bs.out.0 *= 1000 bs.const
scoreboard players operation @s bs.out.0 /= @s bs.opt.0

### DEBUG
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":" Output: Vx=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3 Vy=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3 Vz=","color":"gray"},{"score":{"name":"@s","objective":"bs.vector.x"},"color":"aqua"},{"text":"e-3","color":"gray"}]
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":" Normalization factor=","color":"gray"},{"score":{"name":"@s","objective":"bs.out.0"},"color":"aqua"},{"text":"e-3","color":"gray"}]
tellraw @a[tag=bs.debug.vector.fast_normalize] [{"text":"<","color":"dark_aqua"}]
### END DEBUG
# Reset option score
scoreboard players reset @s bs.opt.0
29 changes: 11 additions & 18 deletions datapacks/Bookshelf/data/bs.vector/functions/normalize.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,30 @@

# Version: 1.0
# Created: 19/02/2023 (1.19.2)
# Last verification: 19/02/2023 (1.19.2)
# Last modification: 19/02/2023 (1.19.2)
# Last verification: 21/05/2023 (1.19.4)
# Last modification: 21/05/2023 (1.19.4)

# Original path : bs.vecotr:fast_normalize
# Documentation : https://bookshelf.docs.gunivers.net/en/latest/vector#normalize
# Note :

# INIT ------------------------------------------------------------------------

scoreboard objectives add bs.vector.x dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector X","color":"aqua"}]
scoreboard objectives add bs.vector.y dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector Y","color":"aqua"}]
scoreboard objectives add bs.vector.z dummy [{"text":"Bookshelf ","color":"dark_gray"},{"text":"Vector Z","color":"aqua"}]

# CONFIG ----------------------------------------------------------------------

# The final vector will have (almost) this length
execute unless entity @s[tag=bs.config.override] run scoreboard players set vector.normalize.length bs.config 1000
tag @s[tag=bs.config.override] remove bs.config.override

# CODE ------------------------------------------------------------------------

# Lenght after normalization
execute unless score @s bs.opt.0 matches 0.. run scoreboard players set @s bs.opt.0 1000

scoreboard players operation #vector.backup.out.0 bs.data = @s bs.out.0

function bs.vector:length

scoreboard players operation @s bs.vector.x *= vector.normalize.length bs.config
scoreboard players operation @s bs.vector.y *= vector.normalize.length bs.config
scoreboard players operation @s bs.vector.z *= vector.normalize.length bs.config
scoreboard players operation @s bs.vector.x *= @s bs.opt.0
scoreboard players operation @s bs.vector.y *= @s bs.opt.0
scoreboard players operation @s bs.vector.z *= @s bs.opt.0

scoreboard players operation @s bs.vector.x /= @s bs.out.0
scoreboard players operation @s bs.vector.y /= @s bs.out.0
scoreboard players operation @s bs.vector.z /= @s bs.out.0

scoreboard players operation @s bs.out.0 = #vector.backup.out.0 bs.data
scoreboard players operation @s bs.out.0 = #vector.backup.out.0 bs.data

scoreboard players reset @s bs.opt.0

0 comments on commit 318da12

Please sign in to comment.