2D vectors

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

2D vectors

Lars Ruoff
Hi,

is there any library or facility in Lua for working with 2D euclidean vectors?
I think of functions for getting (euclidean) distance, sum of two vectors, scalar multiplication, scalar product, transformation to and from polar coordinates and such.
I know these are not particularly difficult to implement, but i'd rather not reinvent the wheel.

Also, how would a 2D vector be defined most efficiently?
As a table with two entries, "x" and "y" ?

In my C code i have this kind of data structures:
struct VECTOR {
FLOAT x;
FLOAT y;
};
and i need to pass them to Lua and back.

These are part of larger data structures.
Am i right that i would need to pass the vectors as tables (inside other tables, possibly inside other tables, ...)?

regards,
Lars R.

Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

steve donovan
On Wed, Jun 11, 2014 at 9:32 AM, Lars Ruoff <[hidden email]> wrote:
> is there any library or facility in Lua for working with 2D euclidean
> vectors?

There's a C extension module for 3D vectors, called lv3

http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/

It's specifically intended as a 'learning aid', so this could be a
useful starting point.

Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

Luiz Henrique de Figueiredo
> On Wed, Jun 11, 2014 at 9:32 AM, Lars Ruoff <[hidden email]> wrote:
> > is there any library or facility in Lua for working with 2D euclidean
> > vectors?
>
> There's a C extension module for 3D vectors, called lv3
>
> http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
>
> It's specifically intended as a 'learning aid', so this could be a
> useful starting point.

To use it under 5.2, you need to remove v3.lua and change lv3.c:

     diff orig/lv3.c ./lv3.c
     18,19c18
     <  if (luaL_checkudata(L,i,MYTYPE)==NULL) luaL_typerror(L,i,MYTYPE);
     <  return lua_touserdata(L,i);
     ---
     >  return luaL_checkudata(L,i,MYTYPE);
     76c75
     < static const luaL_reg R[] =
     ---
     > static const luaL_Reg R[] =
     87c86
     <  luaL_openlib(L,NULL,R,0);
     ---
     >  luaL_setfuncs(L,R,0);


Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

steve donovan
On Wed, Jun 11, 2014 at 1:53 PM, Luiz Henrique de Figueiredo <[hidden email]-
> To use it under 5.2, you need to remove v3.lua and change lv3.c:

Thanks, Luiz.  Although I note that it does not provide more than the
basic indexing operations.

Ideally, this kind of thing should be in C for speed, but a little
Point class is described here:

http://lua-users.org/wiki/PointAndComplex

Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

Roland
In reply to this post by Lars Ruoff
Hi Lars,

I don't know if you are looking for a solution on the C-side, but I remember I have some implementations of vector classes in pure Lua. They might not be the best, but just quick modules I have written as part of some personal projects.
See vec3.lua (used in Lua port of Smallpt raytracer) and vector.lua (2d vectors used in a simulation of steering behaviors).
There is also this quite complete and clean module for 2d vectors, which is part of a larger set of utilities, named HUMP. See hump.vector.
And last, lua-vec. I haven't tested this out, though,  but the API looks quite complete.
Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

Lars Ruoff
Thanks Roland and all,

i have already a solution on the C(++) side. As i said, it's basically
struct VECTOR {
FLOAT x;
FLOAT y;
};
with some methods around it for utility functions.

Would it be better to expose those functions to Lua or use a Lua-side library?
The Lua scripts would need to get some vectors in tables, then do some calculation with it and return a bunch of other vectors.

And concerning my other initial question, would it feel "right" for a Lua coder to handle a 2D vector as a Lua table with elements "x" and "y"?




On Wed, Jun 11, 2014 at 8:18 PM, Roland <[hidden email]> wrote:
Hi Lars,

I don't know if you are looking for a solution on the C-side, but I remember
I have some implementations of vector classes in pure Lua. They might not be
the best, but just quick modules I have written as part of some personal
projects.
See  vec3.lua
<https://github.com/Yonaba/smallpt-lua/blob/e1e6831394229a6780d5a7c0d428e102e97fcf2f/core/vec3.lua>
(used in Lua port of Smallpt raytracer) and  vector.lua
<https://github.com/Yonaba/character-physics-demo/blob/5689eabec224e572c686a0fc0e72360e11ba9a32/vector.lua>
(2d vectors used in a simulation of steering behaviors).
There is also this quite complete and clean module for 2d vectors, which is
part of a larger set of utilities, named HUMP. See  hump.vector
<https://github.com/vrld/hump/blob/master/vector.lua>  .
And last,  lua-vec <https://code.google.com/p/lua-vec/>  . I haven't tested
this out, though,  but the API looks quite complete.




--
View this message in context: http://lua.2524044.n2.nabble.com/2D-vectors-tp7660586p7660623.html
Sent from the Lua-l mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

Andrew Starks



On Wed, Jun 11, 2014 at 1:52 PM, Lars Ruoff <[hidden email]> wrote:
Thanks Roland and all,

i have already a solution on the C(++) side. As i said, it's basically
struct VECTOR {
FLOAT x;
FLOAT y;
};
with some methods around it for utility functions.

Would it be better to expose those functions to Lua or use a Lua-side library?
The Lua scripts would need to get some vectors in tables, then do some calculation with it and return a bunch of other vectors.

And concerning my other initial question, would it feel "right" for a Lua coder to handle a 2D vector as a Lua table with elements "x" and "y"?




On Wed, Jun 11, 2014 at 8:18 PM, Roland <[hidden email]> wrote:
Hi Lars,

I don't know if you are looking for a solution on the C-side, but I remember
I have some implementations of vector classes in pure Lua. They might not be
the best, but just quick modules I have written as part of some personal
projects.
See  vec3.lua
<https://github.com/Yonaba/smallpt-lua/blob/e1e6831394229a6780d5a7c0d428e102e97fcf2f/core/vec3.lua>
(used in Lua port of Smallpt raytracer) and  vector.lua
<https://github.com/Yonaba/character-physics-demo/blob/5689eabec224e572c686a0fc0e72360e11ba9a32/vector.lua>
(2d vectors used in a simulation of steering behaviors).
There is also this quite complete and clean module for 2d vectors, which is
part of a larger set of utilities, named HUMP. See  hump.vector
<https://github.com/vrld/hump/blob/master/vector.lua>  .
And last,  lua-vec <https://code.google.com/p/lua-vec/>  . I haven't tested
this out, though,  but the API looks quite complete.




--
View this message in context: http://lua.2524044.n2.nabble.com/2D-vectors-tp7660586p7660623.html
Sent from the Lua-l mailing list archive at Nabble.com.


As a consumer of this kind of stuff (as opposed to a domain expert):

I'd say it'd feel more natural to have it be { x, y } as opposed to {x = val, y = val}

you can also make a metatable for this so that x and y work, as well but I wouldn't do that. It's fine to represent a tuple as indexed values, as opposed to named pairs.

-Andrew 

Reply | Threaded
Open this post in threaded view
|

Re: 2D vectors

Tim Hill
In reply to this post by Lars Ruoff

On Jun 11, 2014, at 11:52 AM, Lars Ruoff <[hidden email]> wrote:

Thanks Roland and all,

i have already a solution on the C(++) side. As i said, it's basically
struct VECTOR {
FLOAT x;
FLOAT y;
};
with some methods around it for utility functions.

Would it be better to expose those functions to Lua or use a Lua-side library?
The Lua scripts would need to get some vectors in tables, then do some calculation with it and return a bunch of other vectors.

And concerning my other initial question, would it feel "right" for a Lua coder to handle a 2D vector as a Lua table with elements "x" and "y"?


This comes down to how much efficiency you need, and if you want the vectors to be opaque or transparent to Lua code

If you want Lua to act as a custodian of opaque vectors that are operated on via a C library, then userdata is your friend. You can, of course, expose the C API through a metatable and then have a nice OO style vector interface to your C library. This is pretty fast and compact, but perhaps cumbersome if you want to make the vectors more transparent to Lua.

If you go the table route, be aware that Lua tables do have more overhead than a userdata. Also, it’s typically somewhat faster (and more compact) to store the X,Y parts in the array part of the table (so, use v = {1, 2} instead of v = { x = 1, y = 2 } ), effectively treating them more like a tuple.

If you have a LOT of vectors to work with in arrays, storing them in column form is much more efficient (that is, two arrays, one of X values, one of Y values). For a million vectors, this approach uses two tables instead of one million, and is MUCH more efficient in memory usage.

—Tim