Exemplo n.º 1
0
static int luaB_inv (lua_State *L) {
  float w, x, y, z;
  if (lua_gettop(L) != 1) luaL_error(L, "Invalid params, try inv(q)");
  lua_checkquat(L, 1, &w, &x, &y, &z);
  lua_pushquat(L,w,x,y,z);
  return 1;
}
Exemplo n.º 2
0
static int luaB_unpack (lua_State *L) {
  if (lua_gettop(L)==1 && lua_isvector3(L,1)) {
    float x, y, z; lua_checkvector3(L, 1, &x, &y, &z);
    lua_pushnumber(L,x);
    lua_pushnumber(L,y);
    lua_pushnumber(L,z);
    return 3;
  } else if (lua_gettop(L)==1 && lua_isquat(L,1)) {
    float w, x, y, z; lua_checkquat(L, 1, &w, &x, &y, &z);
    lua_pushnumber(L,w);
    lua_pushnumber(L,x);
    lua_pushnumber(L,y);
    lua_pushnumber(L,z);
    return 4;
  } else {
    int i, e, n;
    luaL_checktype(L, 1, LUA_TTABLE);
    i = luaL_optint(L, 2, 1);
    e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
    if (i > e) return 0;  /* empty range */
    n = e - i + 1;  /* number of elements */
    if (n <= 0 || !lua_checkstack(L, n))  /* n <= 0 means arith. overflow */
      return luaL_error(L, "too many results to unpack");
    lua_rawgeti(L, 1, i);  /* push arg[i] (avoiding overflow problems) */
    while (i++ < e)  /* push arg[i + 1...e] */
      lua_rawgeti(L, 1, i);
    return n;
  }
}
Exemplo n.º 3
0
static int math_pow (lua_State *L) {
  if (lua_gettop(L) != 2) {
    luaL_error(L, "Wrong number of arguments: use math.pow(number, number) or math.pow(quat, number)");
  }
  if (lua_type(L, 1) != LUA_TNUMBER && lua_type(L, 1) != LUA_TQUAT) {
    luaL_error(L, "Invalid type for 1st parameter, use math.pow(number, number) or math.pow(quat, number)");
  }
  if (lua_type(L, 2) != LUA_TNUMBER) {
    luaL_error(L, "math.pow second argument must be a number");
  }

  if (lua_type(L, 1) == LUA_TNUMBER) {
      lua_pushnumber(L, pow(lua_tonumber(L, 1), lua_tonumber(L, 2)));
  } else {
    float w, x, y, z,  l;
    float index;
    lua_checkquat(L, 1, &w, &x, &y, &z);
    index = (float)lua_tonumber(L, 2);
    l = sqrtf(x*x + y*y + z*z);
    if (l==0) {
      lua_pushquat(L, 1, 0, 0, 0);
    } else {
      float angle, sangle;
      float w2, x2, y2, z2;
      angle = index * acosf(w); /* without the factor of 2 */
      sangle = sinf(angle);
      w2 = cosf(angle);
      x2 = sangle * x/l;
      y2 = sangle * y/l;
      z2 = sangle * z/l;
      lua_pushquat(L, w2, x2, y2, z2);
    }
  }
  return 1;
}
Exemplo n.º 4
0
static int luaB_inv (lua_State *L) {
  float w, x, y, z;
  if (lua_gettop(L) != 1) luaL_error(L, "Invalid params, try inv(q)");
  lua_checkquat(L, 1, &w, &x, &y, &z);
  /* don't invert w, as that would mean inv(Q_ID) would flip the polarity of w */
  lua_pushquat(L,w,-x,-y,-z);
  return 1;
}
Exemplo n.º 5
0
static int luaB_slerp (lua_State *L) {
  float w1, x1, y1, z1;
  float w2, x2, y2, z2;
  float t, theta, dot;
  if (lua_gettop(L) != 3) luaL_error(L, "Invalid params, try slerp(q1,q2,a)");
  lua_checkquat(L, 1, &w1, &x1, &y1, &z1);
  lua_checkquat(L, 2, &w2, &x2, &y2, &z2);
  t = (float)lua_tonumber(L, 3);

  dot = w1*w2 + x1*x2 + y1*y2 + z1*z2;
  if (dot < 0) {
    /* flip one of them */
    w2 *= -1;
    x2 *= -1;
    y2 *= -1;
    z2 *= -1;
    /* update dot to match */
    dot *= -1;
  }

  /* dot > 0 now */
    
  /* Due to rounding errors, even when vectors are normalised in Lua, dot can be > 1.
   * We treat this case as if dot == 1 as it can only happen when the quats are very
   * similar.
   */

  if (dot < 1) {
    theta = acosf(dot);
    float d = 1.0f / sinf(theta);
    float s0 = sinf((1.0f - t) * theta);
    float s1 = sinf(t * theta);
    lua_pushquat(L,
        d * (w1*s0 + w2*s1),
        d * (x1*s0 + x2*s1),
        d * (y1*s0 + y2*s1),
        d * (z1*s0 + z2*s1)
    );
  } else {
    lua_pushquat(L,w1,x1,y1,z1);
  }

  return 1;
}
Exemplo n.º 6
0
static int luaB_norm (lua_State *L) {
  if (lua_gettop(L)==1 && lua_isvector3(L,1)) {
    float x, y, z;
    float len;
    lua_checkvector3(L, 1, &x, &y, &z);
    len = sqrtf(x*x + y*y + z*z);
    lua_pushvector3(L,x/len,y/len,z/len);
    return 1;
  } else if (lua_gettop(L)==1 && lua_isquat(L,1)) {
    float w, x, y, z;
    float qlen;
    lua_checkquat(L, 1, &w, &x, &y, &z);
    qlen = sqrtf(w*w + x*x + y*y + z*z);
    lua_pushquat(L,w/qlen,x/qlen,y/qlen,z/qlen);
    return 1;
  } else {
    luaL_error(L, "Invalid params, try norm(v) norm(q)");
    return 0;
  }
}
Exemplo n.º 7
0
static int luaB_norm (lua_State *L) {
  if (lua_gettop(L)==1 && lua_isvector2(L,1)) {
    float x, y;
    float len;
    lua_checkvector2(L, 1, &x, &y);
    len = sqrtf(x*x + y*y);
    if (len == 0)
        luaL_error(L, "Cannot normalise vector2(0,0)");
    lua_pushvector2(L,x/len,y/len);
    return 1;
  } else if (lua_gettop(L)==1 && lua_isvector3(L,1)) {
    float x, y, z;
    float len;
    lua_checkvector3(L, 1, &x, &y, &z);
    len = sqrtf(x*x + y*y + z*z);
    if (len == 0)
        luaL_error(L, "Cannot normalise vector3(0,0,0)");
    lua_pushvector3(L,x/len,y/len,z/len);
    return 1;
  } else if (lua_gettop(L)==1 && lua_isvector4(L,1)) {
    float x, y, z, w;
    float len;
    lua_checkvector4(L, 1, &x, &y, &z, &w);
    len = sqrtf(x*x + y*y + z*z + w*w);
    if (len == 0)
        luaL_error(L, "Cannot normalise vector4(0,0,0,0)");
    lua_pushvector4(L,x/len,y/len,z/len,w/len);
    return 1;
  } else if (lua_gettop(L)==1 && lua_isquat(L,1)) {
    float w, x, y, z;
    float qlen;
    lua_checkquat(L, 1, &w, &x, &y, &z);
    qlen = sqrtf(w*w + x*x + y*y + z*z);
    if (qlen == 0)
        luaL_error(L, "Cannot normalise quat(0,0,0,0)");
    lua_pushquat(L,w/qlen,x/qlen,y/qlen,z/qlen);
    return 1;
  } else {
    return luaL_error(L, "Invalid arguments, try norm(v) or norm(q).");
  }
}