dad_utils.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. typedef struct {
  104. HSQOBJECT func_to_call;
  105. HSQOBJECT param;
  106. } gc_scope_alert_st;
  107. static const SQChar *gc_scope_alert_TAG = _SC("gc_scope_alert");
  108. static SQRESULT gc_scope_alert_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  109. {
  110. gc_scope_alert_st *self = ((gc_scope_alert_st *)p);
  111. //printf("%p %p\n", p, v);
  112. if(self){
  113. if(v){
  114. SQInteger top = sq_gettop(v);
  115. sq_reservestack(v, 20);
  116. sq_pushobject(v, self->func_to_call);
  117. sq_pushroottable(v);
  118. sq_pushobject(v, self->param);
  119. sq_call(v, 2, SQFalse, SQTrue);
  120. sq_release(v, &self->func_to_call);
  121. sq_release(v, &self->param);
  122. sq_settop(v, top);
  123. }
  124. //else
  125. sq_free(self, sizeof(gc_scope_alert_st));
  126. }
  127. return 0;
  128. }
  129. static SQRESULT gc_scope_alert_constructor(HSQUIRRELVM v)
  130. {
  131. SQInteger rc;
  132. gc_scope_alert_st proto;
  133. sq_resetobject(&proto.func_to_call);
  134. sq_resetobject(&proto.param);
  135. if((rc = sq_getstackobj(v, 2, &proto.func_to_call)) < 0) return rc;
  136. if(sq_gettop(v) > 2){
  137. if((rc = sq_getstackobj(v, 3, &proto.param)) < 0) return rc;
  138. }
  139. gc_scope_alert_st *self = (gc_scope_alert_st*)sq_malloc(sizeof(gc_scope_alert_st));
  140. if(!self) return sq_throwerror(v, "Failed to create %s", gc_scope_alert_TAG);
  141. sq_addref(v, &proto.func_to_call);
  142. sq_addref(v, &proto.param);
  143. *self = proto;
  144. sq_setinstanceup(v, 1, self);
  145. sq_setreleasehook(v,1, gc_scope_alert_releasehook);
  146. return 1;
  147. }
  148. static SQRESULT spectralnorm_A(HSQUIRRELVM v)
  149. {
  150. SQ_FUNC_VARS_NO_TOP();
  151. SQ_GET_INTEGER(v, 2, i);
  152. SQ_GET_INTEGER(v, 3, j);
  153. SQInteger ij = j + i++;
  154. sq_pushfloat(v, 1.0/(ij * (ij+1)/2.0+i));
  155. return 1;
  156. }
  157. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), gc_scope_alert_##name,nparams,tycheck}
  158. static SQRegFunction gc_scope_alert_methods[] =
  159. {
  160. _DECL_FUNC(constructor, -2, _SC("xc.")),
  161. {0,0}
  162. };
  163. #ifdef __cplusplus
  164. extern "C" {
  165. #endif
  166. SQRESULT sqext_register_dad_utils(HSQUIRRELVM v)
  167. {
  168. sq_pushstring(v,_SC("dad_utils"),-1);
  169. sq_newtable(v);
  170. sq_insertfunc(v, _SC("osGridToLatLong"), sq_osGridToLatLong, 3, _SC(".ff"), true);
  171. sq_insertfunc(v, _SC("spectralnorm_A"), spectralnorm_A, 3, _SC(".ii"), true);
  172. sq_pushstring(v,gc_scope_alert_TAG,-1);
  173. sq_newclass(v,SQFalse);
  174. sq_settypetag(v,-1,(void*)gc_scope_alert_TAG);
  175. sq_insert_reg_funcs(v, gc_scope_alert_methods);
  176. sq_rawset(v,-3);
  177. sq_rawset(v,-3);//insert dad_utils
  178. return 1;
  179. }
  180. #ifdef __cplusplus
  181. }
  182. #endif