Vectors

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

Vectors

Nick.Trou
Hi I'm attempting to add a 3D vector class to Lua and I wondered if anyone
had any thoughts on the matter. What would be the most efficient manner?
(ie. fastest). Can I use tags to overload the arithmetic operators. eg.
vec3= vec1 + vec2 ?

Be something like: struct _vector3 { float x,y,z; };

Cheers,
Nick


Reply | Threaded
Open this post in threaded view
|

Re: Vectors

David Jeske-2
You certainly can use tags to overload the arithmetic operators. 

I think you should ask yourself the question: Is it more important for
vector accesses to be fast, or transform/operations on the vectors to be
fast. If the latter is true then I would look back a few emails (or I can
dig it up) for an email of mine which explained how to make a C data
structure look like a table to Lua. If the former is true, then just make
a Lua table, change it's tag, and setup some tag handlers to do the
math operations.


On Mon, Jun 22, 1998 at 02:26:29PM -0300, [hidden email] wrote:
> Hi I'm attempting to add a 3D vector class to Lua and I wondered if anyone
> had any thoughts on the matter. What would be the most efficient manner?
> (ie. fastest). Can I use tags to overload the arithmetic operators. eg.
> vec3= vec1 + vec2 ?
> 
> Be something like: struct _vector3 { float x,y,z; };

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Vectors

Waldemar Celes-3
In reply to this post by Nick.Trou
> Hi I'm attempting to add a 3D vector class to Lua and I wondered if anyone
> had any thoughts on the matter. What would be the most efficient manner?
> (ie. fastest). Can I use tags to overload the arithmetic operators. eg.
> vec3= vec1 + vec2 ?
> 
> Be something like: struct _vector3 { float x,y,z; };

I would sugest you to write in C and then 
bind the class and its methods to Lua.

"toLua" may help you on it
(http://www.tecgraf.puc-rio.br/~celes/tolua/tolua.html).

-- waldemar

Reply | Threaded
Open this post in threaded view
|

Re: Vectors

Steve Dekorte-2
In reply to this post by David Jeske-2
>You certainly can use tags to overload the arithmetic operators.
>
>I think you should ask yourself the question: Is it more important for
>vector accesses to be fast, or transform/operations on the vectors to be
>fast. If the latter is true then I would look back a few emails (or I can
>dig it up) for an email of mine which explained how to make a C data
>structure look like a table to Lua. If the former is true, then just make
>a Lua table, change it's tag, and setup some tag handlers to do the
>math operations.

The problem with the operator overloading approach is that there are more
kinds of operations that you might want to perform on a vector or matrix than
there are ACSII arithmetic symbols to be overloaded.
(add, subtract, dot, cross, scale, invert, negate, size, etc)

There is also the ordering problem. (does the standard ordering of all
these operations apply properly to their vector and/or matrix usage?)

I'd recommend a real object, something like this:

Vector =
{
   array = {},
   size = 0,
   clone = clone
}

function Vector:size(aVector)
 return self.size
end

function Vector:empty(aVector)
    local index = 0
    while ( index < size ) do
        self.array[index] = nil
    end
end

function Vector:setSize(aSize)
    self:empty()
    while ( size < aSize ) do
        self.array[size] = 0
    end
end

function Vector:dot(aVector)
 -- add code here
end

function Vector:cross(aVector)
 -- add code here
end

-- etc ----


Using it would look like this:

vec1 = Vector:clone():setSize(3)
vec2 = Vector:clone():setSize(3)

dotProduct = vec1:dot(vec2)
crossProduct = vec1:cross(vec2)
-- etc --

As for moving it to C, this is an option, but you might be carefull
to make sure that the amount by which you'll be increasing your
application's speed is worth sacrificing the flexilibity and
development time savings of doing it in Lua.

Steve


Reply | Threaded
Open this post in threaded view
|

Re: Vectors

Luiz Henrique de Figueiredo
In reply to this post by Nick.Trou
>From [hidden email] Mon Jun 22 15:58:32 1998
>
>The problem with the operator overloading approach is that there are more
>kinds of operations that you might want to perform on a vector or matrix than
>there are ACSII arithmetic symbols to be overloaded.
>(add, subtract, dot, cross, scale, invert, negate, size, etc)

not really. the tag method can be polymorphic and look at the types of
the arguments. for example, when handling "mult", you may check whether
one of the arguments is a number -- in this case, it's a "scale", not a full
matrix product. and so on.

>There is also the ordering problem. (does the standard ordering of all
>these operations apply properly to their vector and/or matrix usage?)

tag methods for the "arith" event occur in the standard ordering.
if you're not sure, look at the opcodes generated. use luac -l.

>I'd recommend a real object, something like this:
>
>Vector =
>{
>   array = {},
>   size = 0,
>   clone = clone
>}

>dotProduct = vec1:dot(vec2)
>crossProduct = vec1:cross(vec2)

this is perfectly good. operator overloading is something extra that can
be built on top of this.

>As for moving it to C, this is an option, but you might be carefull
>to make sure that the amount by which you'll be increasing your
>application's speed is worth sacrificing the flexilibity and
>development time savings of doing it in Lua.

my feelings exactly.

come on, folks, Lua is fast :-) (but remember that Lua is an interpreted
language -- it's not C).
--lhf

Reply | Threaded
Open this post in threaded view
|

Save / Restore Tables

Ashley Fryer
Greetings Lua People!

What is the best way to save/restore a table to/from disk in version 3.1?  I
would prefer to do this in C if possible.

Thanks in advance,
ashley


Reply | Threaded
Open this post in threaded view
|

Re: Save / Restore Tables

Luiz Henrique de Figueiredo
>From [hidden email] Mon Jun 22 17:41:18 1998
>
>What is the best way to save/restore a table to/from disk in version 3.1?  I
>would prefer to do this in C if possible.

The best way depends on the application.
But see test/save.lua in the distribution.
--lhf

Reply | Threaded
Open this post in threaded view
|

RE: Save / Restore Tables

Ashley Fryer
> From: lhf
>
> >From [hidden email] Mon Jun 22 17:41:18 1998
> >
> >What is the best way to save/restore a table to/from disk in
> >version 3.1?  I would prefer to do this in C if possible.
>
> The best way depends on the application.
> But see test/save.lua in the distribution.

Ok, I looked at the code.  That explains how to save state.  And then to
restore state, I'll lua_dostring the output from dump.lua back into Lua.
It's not a C solution, but it'll work.

Thanks, lhf.

Reply | Threaded
Open this post in threaded view
|

Re: Vectors

Norman Ramsey-3
In reply to this post by Steve Dekorte-2
 > As for moving it to C, this is an option, but you might be carefull
 > to make sure that the amount by which you'll be increasing your
 > application's speed is worth sacrificing the flexilibity and
 > development time savings of doing it in Lua.

Speaking of this problem, are there any profiling tools that help
identify hot spots in Lua code?

Norman

Reply | Threaded
Open this post in threaded view
|

Re: Vectors

David Jeske-2
On Mon, Jun 22, 1998 at 10:51:51PM -0300, Norman Ramsey wrote:
> Speaking of this problem, are there any profiling tools that help
> identify hot spots in Lua code?

This would be pretty valuable, particularly for how I deal with
partitioning my functionality. Normally I'll write whatever I need in Lua
if I can do it without exporting some new function. The plan is to go back
and optimize things which end up too slow, however, so far speed has not
been a problem.

It would be really easy to strap on something which would gather
statistics about how many times a particular address/function was run.
This wouldn't give you timing information, but it would be half way there.
In fact, it could be done with the debugging interface.

Likewise, it should be easy to make a function profiler which uses the
debugging interface to time the amount of time spent in a function. (can
you trap on a return?)

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]