Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Library: Passing a vector as an integer

Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
06-15-2008 12:27
[edit]Forgot to include my exception to handle 1.000 being passed[/edit]

CODE

//Encodes a color vector as an integer for easy passing through the on_rez event
integer encode(vector x)
{
if(x.x>=1.0)
{
x.x=.999;
}
if(x.y>=1.0)
{
x.y=.999;
}
if(x.z>=1.0)
{
x.z=.999;
}
integer y=1000000000;
y += llFloor(x.x*1000)*1000000;
y += llFloor(x.y*1000)*1000;
y += llFloor(x.z*1000);
return y;
}

vector decode (integer x)
{
string temp=(string)x;
float tempx= (float)llGetSubString(temp,1,3);
float tempy= (float)llGetSubString(temp,4,6);
float tempz= (float)llGetSubString(temp,7,9);
vector y=<tempx,tempy,tempz>/1000;
return y;
}




default
{


touch_start(integer total_number)
{
vector temp=<llFrand(1),llFrand(1),llFrand(1)>;//Lets get a random colour vector :)
llSetColor(temp,1);//Lets show the color of what we generated
llSetColor(decode(encode(temp)),3);//Lets show the color of what we generated after being encoded and then decoded
llSetText("Original: " + (string)temp + "\n Encoded: " + (string)encode(temp) + "\n Decoded: " + (string)decode(encode(temp)),<1,1,1>,1);//shows the numerical data :)

}
}
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Library bump
06-18-2008 12:20
:)
_____________________
i've got nothing. ;)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
06-18-2008 15:42
Just to clarify, this will only works for color vectors, not position vectors.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
06-18-2008 18:34
From: Jesse Barnett
Just to clarify, this will only works for color vectors, not position vectors.


whoops, thanks for pointing out that little detail :) edited accordingly.


although, it can be easily enough converted to work with any vector so long as you only need 3 digits of accuracy per entry.
_____________________
My SLExchange shop

Typos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not.


The function is working perfectly fine. It's just not working the way you wanted it to work.
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
06-18-2008 18:40
realistically thats all most would need
Pruoki Snook
Registered User
Join date: 29 May 2008
Posts: 3
06-22-2008 12:30
How would I use this to pass a color on via on_rez()
see when I set the color before encoding ex <1,0,0> Red, it doesn't decode properly, but when I do it like this <.9,0,0> it does.

So I guess what i'm asking is how would I make it work with full color vectors?
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
06-22-2008 14:17
From: Pruoki Snook
How would I use this to pass a color on via on_rez()
see when I set the color before encoding ex <1,0,0> Red, it doesn't decode properly, but when I do it like this <.9,0,0> it does.

So I guess what i'm asking is how would I make it work with full color vectors?


This script works by manipulating just the first three numbers of the float for each and discarding any integers. Just try .999

If you need more then that then just do what we normally do. Pass a channel integer in the on_rez event and have it listen on that channel for the vector.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
06-22-2008 15:01
From: Senuka Harbinger
...it can be easily enough converted to work with any vector so long as you only need 3 digits of accuracy per entry.

Hmm. I don't know. You might want to reconsider that "easily" bit. In order to handle large or small components, you're going to need to encode some kind of exponent. That's probably going to mean using a very low-resolution floating point number encoding for each component, or using part of the integer for a magnitude using such a format, and the rest for the components of a normalized version. Something like that.

In all I'd say a string encoding is a lot more worth the effort, especially given that it is built into the library/language. ;)
Pruoki Snook
Registered User
Join date: 29 May 2008
Posts: 3
06-23-2008 11:47
Heh i'm trying to avoid the listens lol I'm wanting to pass user inputted color vectors for customization of what the system rezes. like the user inputs <1,0,0> for red or something more complicated with 3 digits each.

If that makes sense :]
Vi Shenley
Still Rezzing
Join date: 24 Oct 2006
Posts: 103
06-24-2008 03:40
I wonder why you cannot pass all vectors as integers by simply defining vectors as three digits followed by three decimals (using leading and trailing zeroes as necessary). Concatenating them will then produce an eighteen digit integer, which can then be easily converted back to a vector.

Example:

<114.23, 76.567, 21.02> becomes 114230 076567 021020 (without the spaces)

Is there anything wrong with this approach?

Vi
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
06-24-2008 04:43
That would rule if we had 64 bit ints, but they're only 32 in LSL :(
_____________________
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
06-24-2008 04:53
Like Victoria said..................

"All integer data types are signed 32 bit values between −2,147,483,648 and +2,147,483,647"

only gives you 10 digits to work with.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Nexii Malthus
[Cubitar]Mothership
Join date: 24 Apr 2006
Posts: 400
06-27-2008 11:28
Perhaps there is a way to turn the signed integer into unsigned through some functions to fully utilize the 32nd bit.
_____________________

Geometric Library, for all your 3D maths needs.
https://wiki.secondlife.com/wiki/Geometric

Creator of the Vertical Life Client
Jack Abraham
Lantern By Day
Join date: 11 Apr 2008
Posts: 113
01-17-2009 09:46
I use the following for passing a vector and a 7-bit integer to some of my stuff. Handles values integer values 0-256 for the vector, giving me 1m resolution within the region.

CODE

// Bitmasks and constants
integer X_MASK = 2139095040;
integer X_MULT = 8388608;
integer Y_MASK = 8355840;
integer Y_MULT = 32768;
integer Z_MASK = 32640;
integer Z_MULT = 128;
integer POWER_MASK = 127;

integer encode( vector pos, integer val )
{
integer encoded;
encoded = llRound(pos.x) * X_MULT;
encoded += llRound(pos.y) * Y_MULT;
encoded += llRound(pos.z) * Z_MULT;
encoded += val;
return encoded;
}

vector decodeVec( integer encoded )
{
integer x;
integer y;
integer z;
x = ( encoded & X_MASK ) / X_MULT;
y = ( encoded & Y_MASK ) / Y_MULT;
z = ( encoded & Z_MASK ) / Z_MULT;
return < (float)x, (float)y, (float)z >;
}

integer decodeInt( integer encoded )
{
return encoded & POWER_MASK;
}
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-17-2009 13:35
10 bits per field, 8bits for integer, 2 bits for binary fraction, resolution increased to .25m with a 256.75m cap and two bits to pad with.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Viktoria Dovgal
Join date: 29 Jul 2007
Posts: 3,593
12-07-2009 05:08
Ha, throwing another onto the pile Just Because.
CODE

integer gTenBitsI = 0x3FF;
float gTenBitsF = gTenBitsI;

// assumes a well-formed color value (xyz are all 0.0 to 1.0)
integer Color2Int(vector color) {
return (integer)(color.x * gTenBitsI)
+ ((integer)(color.y * gTenBitsI) << 10)
+ ((integer)(color.z * gTenBitsI) << 20);
}

vector Int2Color(integer int) {
return <
(int & gTenBitsI) / gTenBitsF,
(int >> 10 & gTenBitsI) / gTenBitsF,
(int >> 20 & gTenBitsI) / gTenBitsF
>;
}


& a variation that might be useful for region coordintes.
Slop is 1m on Z, 1/4m on XY.
The flipflops push the worst slop to the upper end.

CODE

// rollovers tend to go offworld
float gMaxXY = 255.999;
float gMaxZ = 4095.999;

// assumes that coords are in range <0-gMaxXY, 0-gMaxXY, 0-gMaxZ>
integer Region2Integer(vector coord) {
return (integer)((gMaxXY - coord.x) / gMaxXY * 1023.)
+ ((integer)((gMaxXY - coord.y) / gMaxXY * 1023.) << 10)
+ ((integer)((gMaxZ - coord.z) / gMaxZ * 4095.) << 20);
}

// unpacker
vector Integer2Region(integer i) {
return <
gMaxXY - ((i & 1023) / 1023. * gMaxXY),
gMaxXY - ((i >> 10 & 1023) / 1023. * gMaxXY),
gMaxZ - ((i >> 20 & 4095) / 4095. * gMaxZ)
>;
}