Page 1 of 1

Optimalization for Payload

Posted: Sun Dec 07, 2025 1:56 am
by miso
Optimizations to store angles in a single byte (with small loss +-1.4 degree precision)
Can reduce network payload, as 3 angles xyz fits in 3 bytes instead of 12 byte (floats).

Code: Select all

;Wraps around angles to keep low range (0-360) Works with integers only
;Angle 370 will be 10, angle -10 will be 350
Procedure.i wrapangle(iangle.i)
  ProcedureReturn ((iangle%360)+360)%360
EndProcedure


;Converts a float angle into a single byte to reduce network payload 
;this will be 4 times smaller. x,y,z angles will be 3 bytes instead of 12
Procedure.a AngleToByte(angle.f)
  Protected iangle.i , aangle.a
  iangle.i = Round(angle.f,#PB_Round_Nearest)
  iangle.i = wrapangle(iangle.i)
  aangle = Round(((iangle/360)*255),#PB_Round_Nearest)
  ProcedureReturn aangle
EndProcedure

;converts byte angle back to float. +- 1.4 degree loss predicted, but can save a lot of bandwidth.
Procedure.f ByteToAngle(angle.a)
  Protected fangle.f
  fangle.f = ((angle/255)*360)
  ProcedureReturn fangle
EndProcedure

Debug "Angle 370 will be"
Debug wrapangle(370)
Debug "Angle 370 in byte format will be"
Debug angletobyte(370)
Debug "Angle 370 converted back from byte format to float will be"
Debug ByteToAngle(AngleToByte(370))

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 4:42 am
by idle
nice but fangles might have something to say about that! :lol:

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 5:38 am
by miso
I don't want to be sued, should I rename the variable? ;) No goats or pixels had been harmed in the creation process ;)

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 8:09 am
by Fangbeast
idle wrote: Sun Dec 07, 2025 4:42 am nice but fangles might have something to say about that! :lol:
OMG..That's so corny.

Re: Optimalization for Payload

Posted: Sun Dec 07, 2025 4:06 pm
by minimy
Genial!!! This save a lot of memory.
Thanks for share miso!
miso wrote: Sun Dec 07, 2025 5:38 am I don't want to be sued, should I rename the variable? ;) No goats or pixels had been harmed in the creation process ;)
Are you sure? :lol:
idle wrote: Sun Dec 07, 2025 4:42 am nice but fangles might have something to say about that! :lol:
:lol:

Re: Optimalization for Payload

Posted: Tue Dec 30, 2025 3:01 pm
by Thorium
Nice.
Maybe add a rounding to nearest integer on reading the angle back.
This would assure correct angles for 45, 90, 180, etc. Which in many projects are important. If you have a axis aligned angle you would want to keep it axis aligned even when you compress it.

Re: Optimalization for Payload

Posted: Tue Dec 30, 2025 6:33 pm
by miso
Thorium wrote: Tue Dec 30, 2025 3:01 pm Nice.
Maybe add a rounding to nearest integer on reading the angle back.
This would assure correct angles for 45, 90, 180, etc. Which in many projects are important. If you have a axis aligned angle you would want to keep it axis aligned even when you compress it.
Thanks for your observation. You are probably right.
Still I will leave this as is. If someone uses this in the future, will know how to adjust to his needs. I belive the idea is the interesting thing, and not the actual code.

Re: Optimalization for Payload

Posted: Wed Dec 31, 2025 1:41 am
by Fangbeast
Oi!! You forgot the 'f' on the angle!! Or do you want me to forget my name again???