satgeometry.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:6k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (c) 1999 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *      This product includes software developed by the MASH Research
  17.  *      Group at the University of California Berkeley.
  18.  * 4. Neither the name of the University nor of the Research Group may be
  19.  *    used to endorse or promote products derived from this software without
  20.  *    specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  *
  34.  * Contributed by Tom Henderson, UCB Daedalus Research Group, June 1999
  35.  */
  36. #ifndef lint
  37. static const char rcsid[] =
  38.     "@(#) $Header: /cvsroot/nsnam/ns-2/satellite/satgeometry.cc,v 1.6 2001/05/21 19:27:31 haldar Exp $";
  39. #endif
  40. #include "satgeometry.h"
  41. #include "satposition.h"
  42. static class SatGeometryClass : public TclClass {
  43. public:
  44.         SatGeometryClass() : TclClass("SatGeometry") {}
  45.         TclObject* create(int, const char*const*) {
  46.                 return (new SatGeometry());
  47.         }
  48. } class_sat_geometry;
  49. // Returns the distance in km between points a and b
  50. double SatGeometry::distance(coordinate a, coordinate b)
  51. {
  52.         double a_x, a_y, a_z, b_x, b_y, b_z;     // cartesian
  53. spherical_to_cartesian(a.r, a.theta, a.phi, a_x, a_y, a_z);
  54. spherical_to_cartesian(b.r, b.theta, b.phi, b_x, b_y, b_z);
  55.         return (BaseTrace::round(DISTANCE(a_x, a_y, a_z, b_x, b_y, b_z), 1.0E+8));
  56. }
  57. void SatGeometry::spherical_to_cartesian(double R, double Theta,
  58.     double Phi, double &X, double &Y, double &Z)
  59. {      
  60. X = R * sin(Theta) * cos (Phi);
  61. Y = R * sin(Theta) * sin (Phi);
  62. Z = R * cos(Theta);
  63. }
  64. // Propagation delay is the distance divided by the speed of light
  65. double SatGeometry::propdelay(coordinate a, coordinate b)
  66. {
  67. double delay = distance(a, b)/LIGHT;
  68. return (BaseTrace::round(delay, 1.0E+8));
  69. }
  70. double SatGeometry::get_altitude(coordinate a)
  71. {
  72.         return (a.r - EARTH_RADIUS);
  73. }
  74. // Returns latitude in radians, in the range from -PI/2 to PI/2
  75. double SatGeometry::get_latitude(coordinate a)
  76. {
  77.         return (PI/2 - a.theta);
  78. }
  79. // Returns (earth-centric) longitude corresponding to the position of the node 
  80. // (the input coordinate corresponds to fixed coordinate system, through
  81. // which the Earth rotates, so we have to scale back the effects of rotation).
  82. // The return value ranges from -PI to PI.
  83. double SatGeometry::get_longitude(coordinate coord_)
  84. {
  85.         double period = EARTH_PERIOD; // period of earth in seconds
  86.         // adjust longitude so that it is earth-centric (i.e., account
  87.         // for earth rotating beneath).   
  88.         double earth_longitude = fmod((coord_.phi -
  89.            (fmod(NOW + SatPosition::time_advance_,period)/period) * 2*PI), 
  90.     2*PI);
  91. // Bring earth_longitude to be within (-PI, PI)
  92.         if (earth_longitude < (-1*PI))
  93. earth_longitude = 2*PI + earth_longitude;
  94.         if (earth_longitude > PI)
  95. earth_longitude = (-(2*PI - earth_longitude));
  96. if (fabs(earth_longitude) < 0.0001)
  97. return 0;   // To avoid trace output of "-0.00"
  98. else
  99. return (earth_longitude);
  100. }       
  101. // If the satellite is above the elevation mask of the terminal, returns 
  102. // the elevation mask in radians; otherwise, returns 0.
  103. double SatGeometry::check_elevation(coordinate satellite,
  104.     coordinate terminal, double elev_mask_)
  105. {
  106. double S = satellite.r;  // satellite radius
  107. double S_2 = satellite.r * satellite.r;  // satellite radius^2
  108. double E = EARTH_RADIUS;
  109. double E_2 = E * E;
  110. double d, theta, alpha;
  111. d = distance(satellite, terminal);
  112. if (d < sqrt(S_2 - E_2)) {
  113. // elevation angle > 0
  114. theta = acos((E_2+S_2-(d*d))/(2*E*S));
  115. alpha = acos(sin(theta) * S/d);
  116. return ( (alpha > elev_mask_) ? alpha : 0);
  117. } else
  118. return 0;
  119. }
  120. // This function determines whether two satellites are too far apart
  121. // to establish an ISL between them, due to Earth atmospheric grazing
  122. // (or shadowing by the Earth itself).  Assumes that both satellites nodes
  123. // are at the same altitude.  The line between the two satellites can be
  124. // bisected, and a perpendicular from that point to the Earth's center will
  125. // form a right triangle.  If the length of this perpendicular is less than
  126. // EARTH_RADIUS + ATMOS_MARGIN, the link cannot be established.
  127. //
  128. int SatGeometry::are_satellites_mutually_visible(coordinate first, coordinate second)
  129. {
  130. // if we drop a perpendicular from the ISL to the Earth's surface,
  131. // we have a right triangle.  The atmospheric margin is the minimum
  132. // ISL grazing altitude.
  133. double c, d, min_radius, grazing_radius;
  134. double radius = get_radius(first); // could just use first.r here.
  135. double distance_ = distance(first, second);
  136. c = radius * radius;
  137. d = (distance_/2) * (distance_/2);
  138. grazing_radius = (EARTH_RADIUS + ATMOS_MARGIN);
  139. min_radius = sqrt(c - d);
  140. if (min_radius >= grazing_radius) {
  141. return TRUE;
  142. } else {
  143. return FALSE;
  144. }
  145. }