Jump to content

Pack Vector in Integer and Unpack


Mollymews
 Share

You are about to reply to a thread that has been inactive for 1246 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

a conversation here

https://community.secondlife.com/forums/topic/453503-can-llsensor-be-restricted-to-avatars-on-one-parcel-only/

led into a discussion about a weapon passing a target vector to a bullet in the on_rez integer parameter. Which in turn raised a question about packing negative and positive XYZ values of a vector into a integer and unpacking them back to a vector

this builds on Rolig's wrapper functions for positive vector values, detailed in the conversation
 

// pack vector into a 9:9:13 bitmap integer with accuracy of 1 meter
// X range: -255..+255
// y range: -255..+255
// Z range: -4095..+4095

integer FromVector(vector v)
{
   // we might want to do bounds capping here. For example
   // if (v.x > 255.0) v.x = 255.0;
   // else if (v.x < -255.0) v.x = -255.0;
   // same for v.y and v.z
   
   return
      ((v.x < 0.0) * 0x40000000 + ((llAbs((integer)v.x) & 0xFF) << 22)) |
      ((v.y < 0.0) * 0x200000 + ((llAbs((integer)v.y) & 0xFF) << 13)) |
      ((v.z < 0.0) * 0x1000 + (llAbs((integer)v.z) & 0xFFF));
}

vector FromInteger(integer i)
{
   return <
      (float)(llList2Integer([1,-1], (i & 0x40000000) > 0) * ((i >> 22) & 0xFF)),
      (float)(llList2Integer([1,-1], (i & 0x200000) > 0) * ((i >> 13) & 0xFF)),
      (float)(llList2Integer([1,-1], (i & 0x1000) > 0) * (i & 0xFFF))
   >;    
}


default
{
    state_entry()
    {
        vector v = <-255.0, 255.00, -4095.0>;
        
        integer i = FromVector(v);
        vector w = FromInteger(i);
        
        llOwnerSay(llDumpList2String([v, w], " "));
    }
}

 

  • Like 2
  • Thanks 3
Link to comment
Share on other sites

in the code above I mention this in a comment

if (v.x > 255.0) v.x = 255.0;
else if (v.x < -255.0) v,x = -255.0;

for the typical programmer (which is most of us) this code is easier to read and understand what is happening. This coding approach is found in a lot of tutorials, which from a tutorial/explanation pov is a good thing to do

as we get more experience and subsequent exposure to code written by more advanced programmers we start to see a lot more inline logic constructs. A LSL equivalent of a inline logic construct that does the above

float f = 256.0;

f = llList2Float([f, -255.0, 255.0], (f < -255.0) + (2 * (f > 255.0)));

breaking this down:

integer i = (f < -255.0) + (2 * (f > 255.0));

when f >= -255.0 and f <= 255.0 then i = 0

(f < -255.0) = FALSE = 0. (f > 255.0) = FALSE = 0. (0) + (2 * 0) = 0

when f < -255.0 then i = 1

(f < -255.0) = TRUE = 1. (f > 255.0) = FALSE = 0. (1) + (2 * 0) = 1

when f > 255.0 then i = 2

(f < -255.0) = FALSE = 0. (f > 255.0) = TRUE = 1. (0) + (2 * 1) = 2


is this a good idea ?  It depends

my reason for posting this (given that I did use this approach in the above code to map FALSE and TRUE respectively to [1,-1]) is about exploring ways we can use lists as a programming tool, in addition to using lists for storing data. It is not always the best approach. But I think it can help us to understand how logic works. And when we do have a better understanding of logic there will be times when we recognise that inline logic constructs can be a better approach

  • Like 2
Link to comment
Share on other sites

  • 6 months later...

looking thru some LSL code from 2005 I came across this by Void Singer

-n | 1;

pretty cool

n = 0 = (-n | 1) = 1;
n = 1 = (-n | 1) = -1;

so I rewrote FromInteger using this method as an alternative to using a list lookup function

vector FromInteger(integer i)
{
   return <
      (float)(-(i >> 30 & 0x1) | 0x1) * (i >> 22 & 0xFF),
      (float)(-(i >> 21 & 0x1) | 0x1) * (i >> 13 & 0xFF),
      (float)(-(i >> 12 & 0x1) | 0x1) * (i & 0xFFF)
   >;     
}

 

 

 

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1246 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...