-- $LastChangedDate: 2010-05-25 21:26:05 -0400 (Tue, 25 May 2010) $
-- LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG

print( "Running vec.lua" )

XX = 0
YY = 1
ZZ = 2

-- 2D vector class (index starts as zero).
Vec2_ = {}
function Vec2( x, y )  -- this is a ctor
    t = { [XX] = x, [YY] = y }
    setmetatable( t, Vec2_ )
    Vec2_.__index = Vec2_
    return t
end
function Vec2_:__tostring()
    return tostring(self[XX]) .. "," .. tostring(self[YY])
end

-- 3D vector class (index starts as zero).
Vec3_ = {}
function Vec3( x, y, z )  -- this is a ctor
    t = { [XX] = x, [YY] = y, [ZZ] = z }
    setmetatable( t, Vec3_ )
    Vec3_.__index = Vec3_
    return t
end
function Vec3_:__tostring()
    return tostring(self[XX]) .. "," .. tostring(self[YY]) .. "," .. tostring(self[ZZ])
end

-- Test.
assert( test )  -- ensure C++ exported these
assert( test.SetVec3 )
assert( test.GetVec3 )

-- By SetVec3() C++ will specifically test for the values 123,456,789.
-- And by GetVec3(), Lua checks that C++ returns 123,456,789
-- as a table indexed from 0 (zero rather than one).
print( "Testing passing vec3 between Lua and C++" )
v = Vec3( 123.0, 456.0, 789.0 )
test.SetVec3( v )
v2 = test.GetVec3()
assert( type(v2) == "table" )
if ( v2[0] ~= 123.0 or v2[1] ~= 456.0 or v2[2] ~= 789.0 ) then
    error( "GetVec3() failed" )
end
