dad_utils.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "squirrel.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. struct lat_long_st
  5. {
  6. double latitude, longitude;
  7. };
  8. inline double toDeg(double lat)
  9. {
  10. return lat * 180.0 / M_PI;
  11. }
  12. inline double toRad(double x)
  13. {
  14. return x * M_PI / 180.0;
  15. }
  16. //
  17. // Convert Ordnance Survey grid reference easting/northing coordinate to (OSGB36) latitude/longitude
  18. //
  19. // @param {OsGridRef} easting/northing to be converted to latitude/longitude
  20. // @return {LatLon} latitude/longitude (in OSGB36) of supplied grid reference
  21. //
  22. static lat_long_st osGridToLatLong(double easting, double northing)
  23. {
  24. double E = easting;
  25. double N = northing;
  26. double a = 6377563.396;
  27. double b = 6356256.910; // Airy 1830 major & minor semi-axes
  28. double F0 = 0.9996012717; // NatGrid scale factor on central meridian
  29. double lat0 = 49.0*M_PI/180.0;
  30. double lon0 = -2.0*M_PI/180.0; // NatGrid true origin
  31. double N0 = -100000.0;
  32. double E0 = 400000.0; // northing & easting of true origin, metres
  33. double e2 = 1.0 - (b*b)/(a*a); // eccentricity squared
  34. double n = (a-b)/(a+b);
  35. double n2 = n*n;
  36. double n3 = n*n*n;
  37. double lat=lat0;
  38. double M=0.0;
  39. double ma_p1 = (1.0 + n + (5.0/4.0)*n2 + (5.0/4.0)*n3);
  40. double mb_p1 = (3.0*n + 3.0*n2 + (21.0/8.0)*n3);
  41. double mc_p1 = ((15.0/8.0)*n2 + (15.0/8.0)*n3);
  42. double md_p1 = (35.0/24.0)*n3;
  43. double n_less_n0 = N-N0;
  44. double bf0 = b * F0;
  45. double af0 = a * F0;
  46. do
  47. {
  48. lat = (n_less_n0-M)/af0 + lat;
  49. double lat_less_lat0 = lat-lat0;
  50. double lat_plus_lat0 = lat+lat0;
  51. double Ma = ma_p1 * lat_less_lat0;
  52. double Mb = mb_p1 * sin(lat_less_lat0) * cos(lat_plus_lat0);
  53. double Mc = mc_p1 * sin(2*lat_less_lat0) * cos(2*lat_plus_lat0);
  54. double Md = md_p1 * sin(3.0*lat_less_lat0) * cos(3*lat_plus_lat0);
  55. M = bf0 * (Ma - Mb + Mc - Md); // meridional arc
  56. }
  57. while (n_less_n0-M >= 0.00001); // ie until < 0.01mm
  58. double cosLat = cos(lat);
  59. double sinLat = sin(lat);
  60. double e2_sinLat2 = 1.0-e2*sinLat*sinLat;
  61. double nu = a*F0/sqrt(e2_sinLat2); // transverse radius of curvature
  62. double rho = a*F0*(1.0-e2)/pow(e2_sinLat2, 1.5); // meridional radius of curvature
  63. double eta2 = nu/rho-1.0;
  64. double tanLat = tan(lat);
  65. double tan2lat = tanLat*tanLat;
  66. double tan4lat = tan2lat*tan2lat;
  67. double tan6lat = tan4lat*tan2lat;
  68. double secLat = 1.0/cosLat;
  69. double nu3 = nu*nu*nu;
  70. double nu5 = nu3*nu*nu;
  71. double nu7 = nu5*nu*nu;
  72. double VII = tanLat/(2.0*rho*nu);
  73. double VIII = tanLat/(24.0*rho*nu3)*(5+3.0*tan2lat+eta2-9.0*tan2lat*eta2);
  74. double IX = tanLat/(720.0*rho*nu5)*(61+90.0*tan2lat+45.0*tan4lat);
  75. double X = secLat/nu;
  76. double XI = secLat/(6.0*nu3)*(nu/rho+2.0*tan2lat);
  77. double XII = secLat/(120.0*nu5)*(5+28.0*tan2lat+24.0*tan4lat);
  78. double XIIA = secLat/(5040.0*nu7)*(61+662.0*tan2lat+1320.0*tan4lat+720.0*tan6lat);
  79. double dE = (E-E0);
  80. double dE2 = dE*dE;
  81. double dE3 = dE2*dE;
  82. double dE4 = dE2*dE2;
  83. double dE5 = dE3*dE2;
  84. double dE6 = dE4*dE2;
  85. double dE7 = dE5*dE2;
  86. lat = lat - VII*dE2 + VIII*dE4 - IX*dE6;
  87. double lon = lon0 + X*dE - XI*dE3 + XII*dE5 - XIIA*dE7;
  88. return {toDeg(lat), toDeg(lon)};
  89. }
  90. static SQRESULT sq_osGridToLatLong(HSQUIRRELVM v)
  91. {
  92. SQ_FUNC_VARS_NO_TOP();
  93. SQ_GET_FLOAT(v, 2, easting);
  94. SQ_GET_FLOAT(v, 3, northing);
  95. lat_long_st ll = osGridToLatLong(easting, northing);
  96. sq_newarray(v, 2);
  97. sq_pushfloat(v, ll.latitude);
  98. sq_arrayappend(v, -1);
  99. sq_pushfloat(v, ll.longitude);
  100. sq_arrayappend(v, -1);
  101. return 1;
  102. }
  103. #ifdef SQ_WITH_DELAYED_RELEASE_HOOKS
  104. typedef struct {
  105. HSQOBJECT func_to_call;
  106. HSQOBJECT param;
  107. } gc_scope_alert_st;
  108. static const SQChar *gc_scope_alert_TAG = _SC("gc_scope_alert");
  109. static SQRESULT gc_scope_alert_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  110. {
  111. gc_scope_alert_st *self = ((gc_scope_alert_st *)p);
  112. //printf("%p %p\n", p, v);
  113. if(self){
  114. if(v){
  115. SQInteger top = sq_gettop(v);
  116. sq_reservestack(v, 20);
  117. sq_pushobject(v, self->func_to_call);
  118. sq_pushroottable(v);
  119. sq_pushobject(v, self->param);
  120. sq_call(v, 2, SQFalse, SQTrue);
  121. sq_release(v, &self->func_to_call);
  122. sq_release(v, &self->param);
  123. sq_settop(v, top);
  124. }
  125. //else
  126. sq_free(self, sizeof(gc_scope_alert_st));
  127. }
  128. return 0;
  129. }
  130. static SQRESULT gc_scope_alert_constructor(HSQUIRRELVM v)
  131. {
  132. SQInteger rc;
  133. gc_scope_alert_st proto;
  134. sq_resetobject(&proto.func_to_call);
  135. sq_resetobject(&proto.param);
  136. if((rc = sq_getstackobj(v, 2, &proto.func_to_call)) < 0) return rc;
  137. if(sq_gettop(v) > 2){
  138. if((rc = sq_getstackobj(v, 3, &proto.param)) < 0) return rc;
  139. }
  140. gc_scope_alert_st *self = (gc_scope_alert_st*)sq_malloc(sizeof(gc_scope_alert_st));
  141. if(!self) return sq_throwerror(v, "Failed to create %s", gc_scope_alert_TAG);
  142. sq_addref(v, &proto.func_to_call);
  143. sq_addref(v, &proto.param);
  144. *self = proto;
  145. sq_setinstanceup(v, 1, self);
  146. sq_setreleasehook(v,1, gc_scope_alert_releasehook);
  147. return 1;
  148. }
  149. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), gc_scope_alert_##name,nparams,tycheck}
  150. static SQRegFunction gc_scope_alert_methods[] =
  151. {
  152. _DECL_FUNC(constructor, -2, _SC("xc.")),
  153. {0,0}
  154. };
  155. #endif // SQ_WITH_DELAYED_RELEASE_HOOKS
  156. static SQRESULT spectralnorm_A(HSQUIRRELVM v)
  157. {
  158. SQ_FUNC_VARS_NO_TOP();
  159. SQ_GET_INTEGER(v, 2, i);
  160. SQ_GET_INTEGER(v, 3, j);
  161. SQInteger ij = j + i++;
  162. sq_pushfloat(v, 1.0/(ij * (ij+1)/2.0+i));
  163. return 1;
  164. }
  165. #ifdef __cplusplus
  166. extern "C" {
  167. #endif
  168. SQRESULT sqext_register_dad_utils(HSQUIRRELVM v)
  169. {
  170. sq_pushstring(v,_SC("dad_utils"),-1);
  171. sq_newtable(v);
  172. sq_insertfunc(v, _SC("osGridToLatLong"), sq_osGridToLatLong, 3, _SC(".ff"), true);
  173. sq_insertfunc(v, _SC("spectralnorm_A"), spectralnorm_A, 3, _SC(".ii"), true);
  174. #ifdef SQ_WITH_DELAYED_RELEASE_HOOKS
  175. sq_pushstring(v,gc_scope_alert_TAG,-1);
  176. sq_newclass(v,SQFalse);
  177. sq_settypetag(v,-1,(void*)gc_scope_alert_TAG);
  178. sq_insert_reg_funcs(v, gc_scope_alert_methods);
  179. sq_rawset(v,-3);
  180. #endif // SQ_WITH_DELAYED_RELEASE_HOOKS
  181. sq_rawset(v,-3);//insert dad_utils
  182. return 1;
  183. }
  184. #ifdef __cplusplus
  185. }
  186. #endif