celx_frame.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // celx_frame.cpp
  2. //
  3. // Copyright (C) 2003-2008, the Celestia Development Team
  4. //
  5. // Lua script extensions for Celestia: frame object
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. #include "celx.h"
  12. #include "celx_internal.h"
  13. #include "celx_frame.h"
  14. #include "celestiacore.h"
  15. #include <celengine/observer.h>
  16. int frame_new(lua_State* l, const ObserverFrame& f)
  17. {
  18.     CelxLua celx(l);
  19.     
  20. // Use placement new to put the new frame in the userdata block.
  21. void* block = lua_newuserdata(l, sizeof(ObserverFrame));
  22. new (block) ObserverFrame(f);
  23.     
  24.     celx.setClass(Celx_Frame);
  25.     
  26.     return 1;
  27. }
  28. ObserverFrame* to_frame(lua_State* l, int index)
  29. {
  30.     CelxLua celx(l);
  31.     
  32.     return static_cast<ObserverFrame*>(celx.checkUserData(index, Celx_Frame));
  33. }
  34. static ObserverFrame* this_frame(lua_State* l)
  35. {
  36.     CelxLua celx(l);
  37.     
  38.     ObserverFrame* f = to_frame(l, 1);
  39.     if (f == NULL)
  40.     {
  41.         celx.doError("Bad frame object!");
  42.     }
  43.     
  44.     return f;
  45. }
  46. // Convert from frame coordinates to universal.
  47. static int frame_from(lua_State* l)
  48. {
  49.     CelxLua celx(l);
  50.     
  51.     celx.checkArgs(2, 3, "Two or three arguments required for frame:from");
  52.     
  53.     ObserverFrame* frame = this_frame(l);
  54.     CelestiaCore* appCore = celx.appCore(AllErrors);
  55.     
  56.     UniversalCoord* uc = NULL;
  57.     Quatd* q = NULL;
  58.     double jd = 0.0;
  59.     
  60.     if (celx.isType(2, Celx_Position))
  61.     {
  62.         uc = celx.toPosition(2);
  63.     }
  64.     else if (celx.isType(2, Celx_Rotation))
  65.     {
  66.         q = celx.toRotation(2);
  67.     }
  68.     if (uc == NULL && q == NULL)
  69.     {
  70.         celx.doError("Position or rotation expected as second argument to frame:from()");
  71.     }
  72.     
  73.     jd = celx.safeGetNumber(3, WrongType, "Second arg to frame:from must be a number", appCore->getSimulation()->getTime());
  74.     
  75.     if (uc != NULL)
  76.     {
  77.         UniversalCoord uc1 = frame->convertToUniversal(*uc, jd);
  78.         celx.newPosition(uc1);
  79.     }
  80.     else
  81.     {
  82.         Quatd q1 = frame->convertToUniversal(*q, jd);
  83.         celx.newRotation(q1);
  84.     }
  85.     
  86.     return 1;
  87. }
  88. // Convert from universal to frame coordinates.
  89. static int frame_to(lua_State* l)
  90. {
  91.     CelxLua celx(l);
  92.     celx.checkArgs(2, 3, "Two or three arguments required for frame:to");
  93.     
  94.     ObserverFrame* frame = this_frame(l);
  95.     CelestiaCore* appCore = celx.appCore(AllErrors);
  96.     
  97.     UniversalCoord* uc = NULL;
  98.     Quatd* q = NULL;
  99.     double jd = 0.0;
  100.     
  101.     if (celx.isType(2, Celx_Position))
  102.     {
  103.         uc = celx.toPosition(2);
  104.     }
  105.     else if (celx.isType(2, Celx_Rotation))
  106.     {
  107.         q = celx.toRotation(2);
  108.     }
  109.     
  110.     if (uc == NULL && q == NULL)
  111.     {
  112.         celx.doError("Position or rotation expected as second argument to frame:to()");
  113.     }
  114.     
  115.     jd = celx.safeGetNumber(3, WrongType, "Second arg to frame:to must be a number", appCore->getSimulation()->getTime());
  116.     
  117.     if (uc != NULL)
  118.     {
  119.         UniversalCoord uc1 = frame->convertFromUniversal(*uc, jd);
  120.         celx.newPosition(uc1);
  121.     }
  122.     else
  123.     {
  124.         Quatd q1 = frame->convertFromUniversal(*q, jd);
  125.         celx.newRotation(q1);
  126.     }
  127.     
  128.     return 1;
  129. }
  130. static int frame_getrefobject(lua_State* l)
  131. {
  132.     CelxLua celx(l);
  133.     celx.checkArgs(1, 1, "No arguments expected for frame:getrefobject()");
  134.     
  135.     ObserverFrame* frame = this_frame(l);
  136.     if (frame->getRefObject().getType() == Selection::Type_Nil)
  137.     {
  138.         celx.push(CelxValue());
  139.     }
  140.     else
  141.     {
  142.         celx.newObject(frame->getRefObject());
  143.     }
  144.     
  145.     return 1;
  146. }
  147. static int frame_gettargetobject(lua_State* l)
  148. {
  149.     CelxLua celx(l);
  150.     celx.checkArgs(1, 1, "No arguments expected for frame:gettarget()");
  151.     
  152.     ObserverFrame* frame = this_frame(l);
  153.     if (frame->getTargetObject().getType() == Selection::Type_Nil)
  154.     {
  155.         lua_pushnil(l);
  156.     }
  157.     else
  158.     {
  159.         celx.newObject(frame->getTargetObject());
  160.     }
  161.     return 1;
  162. }
  163. static int frame_getcoordinatesystem(lua_State* l)
  164. {
  165.     CelxLua celx(l);
  166.     celx.checkArgs(1, 1, "No arguments expected for frame:getcoordinatesystem()");
  167.     
  168.     ObserverFrame* frame = this_frame(l);
  169.     string coordsys;
  170.     switch (frame->getCoordinateSystem())
  171.     {
  172.         case ObserverFrame::Universal:
  173.             coordsys = "universal"; break;
  174.         case ObserverFrame::Ecliptical:
  175.             coordsys = "ecliptic"; break;
  176.         case ObserverFrame::Equatorial:
  177.             coordsys = "equatorial"; break;
  178.         case ObserverFrame::BodyFixed:
  179.             coordsys = "bodyfixed"; break;
  180.         case ObserverFrame::ObserverLocal:
  181.             coordsys = "observer"; break;
  182.         case ObserverFrame::PhaseLock:
  183.             coordsys = "lock"; break;
  184.         case ObserverFrame::Chase:
  185.             coordsys = "chase"; break;
  186.         default:
  187.             coordsys = "invalid";
  188.     }
  189.     
  190.     celx.push(coordsys.c_str());
  191.     
  192.     return 1;
  193. }
  194. static int frame_tostring(lua_State* l)
  195. {
  196.     CelxLua celx(l);
  197.     
  198.     // TODO: print out the actual information about the frame
  199.     celx.push("[Frame]");
  200.     
  201.     return 1;
  202. }
  203. /*! Garbage collection metamethod frame objects.
  204. */
  205. static int frame_gc(lua_State* l)
  206. {
  207. ObserverFrame* frame = this_frame(l);
  208.     
  209. // Explicitly call the destructor since the object was created with placement new
  210. frame->~ObserverFrame();
  211.     
  212. return 0;
  213. }
  214. void CreateFrameMetaTable(lua_State* l)
  215. {
  216.     CelxLua celx(l);
  217.     
  218.     celx.createClassMetatable(Celx_Frame);
  219.     
  220.     celx.registerMethod("__tostring", frame_tostring);
  221. celx.registerMethod("__gc", frame_gc);
  222.     celx.registerMethod("to", frame_to);
  223.     celx.registerMethod("from", frame_from);
  224.     celx.registerMethod("getcoordinatesystem", frame_getcoordinatesystem);
  225.     celx.registerMethod("getrefobject", frame_getrefobject);
  226.     celx.registerMethod("gettargetobject", frame_gettargetobject);
  227.     
  228.     lua_pop(l, 1); // remove metatable from stack
  229. }