Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Utility.h

Go to the documentation of this file.
00001 /*
00002  * Utility.h
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00028 #ifndef UTILITY_H
00029 #define UTILITY_H
00030 
00031 #include "FlyLegacy.h"
00032 
00033 
00034 //
00035 // Various constants
00036 //
00037 #define MILES_PER_NM      (1.151)
00038 #define KM_PER_NM         (1.852)
00039 #define METRES_PER_FOOT   (0.3048)
00040 #define FEET_PER_NM       (6076.0)
00041 #define PI                (3.1415926)
00042 #define DEG2RAD           (PI / 180.0)
00043 #define RAD2DEG           (180.0 / PI)
00044 #define FEET_PER_MILE     (5280.0)
00045 #define FEET_PER_METRE    (3.2808399)
00046 
00047 // Assume polar circumference of 24,818 statue miles
00048 #define POLAR_CIRCUMFERENCE       (double)(1.3104211E+08)
00049 #define FEET_PER_LAT_ARCSEC       (double)(1.0111274E+02)
00050 
00051 // Assume equator circumference of 24,900 statue miles
00052 #define EQUATORIAL_CIRCUMFERENCE    (double)(1.3147339E+08)
00053 
00054 // Artificial "mean" circumference of 21,600 nautical miles
00055 #define MEAN_CIRCUMFERENCE          (FEET_PER_NM * 21600)
00056 
00057 
00058 
00059 /*
00060  * Utility\Geodesy.cpp functions
00061  */
00062 
00063 //
00064 // Convert an SPosition from geocentric to (WGS84) geodetic systems
00065 //
00066 SPosition GeocToGeod (SPosition);
00067 
00068 //
00069 // Convert an SPosition from WGS84 geodetic to geocentric systems
00070 //
00071 SPosition GeodToGeoc (SPosition);
00072 
00073 //
00074 // Convert a geocentric SPosition (lat,lon,alt) to cartesian coordinates
00075 //   in an SVector struct
00076 //
00077 SVector GeocToCartesian (SPosition);
00078 
00079 //
00080 // Convert cartesian coordinates to a geocentric SPosition
00081 //
00082 SPosition CartesianToGeoc (SVector);
00083 
00084 //
00085 // Convert a geodetic SPosition (lat,lon,alt) to cartesian coordinates
00086 //   in an SVector struct
00087 //
00088 SVector GeodToCartesian (SPosition);
00089 
00090 //
00091 // Convert cartesian coordinates to a geodetic SPosition
00092 //
00093 SPosition CartesianToGeod (SVector);
00094 
00095 
00096 
00097 void test_geodesy (void);
00098 
00099 
00100 
00101 //
00102 // Calculate great circle distance between two positions based on spherical
00103 //   geocentric coordinates
00104 //
00105 
00106 //
00107 // Calculate great circle distance between two positions based on WGS84
00108 //   geodetic coordinates
00109 //
00110 float GreatCircleDistance(SPosition *from, SPosition *to);
00111 
00112 
00113 //
00114 // Calculate a WGS84 geodetic position based on a starting position (also
00115 //   in WGS84 geodetic coordinates, and an azimuth/range specified in
00116 //   the SVector argument.  The 'h' heading (radians, 0 = true north)
00117 //   and 'r' range (in feet) are the only fields used
00118 //
00119 SPosition GreatCirclePosition(SPosition *from, SVector *polar);
00120 
00121 
00122 //
00123 // Calculate the azimuth bearing (in radians, 0 = true north) and
00124 //   range (in feet) between two positions in WGS84 geodetic coordinates
00125 SVector GreatCirclePolar(SPosition *from, SPosition *to);
00126 
00127 
00128 //
00129 // Simple vector addition
00130 //
00131 SVector VectorSum (SVector v1, SVector v2);
00132 
00133 //
00134 // Simple vector subtraction
00135 //
00136 SVector VectorDifference (SVector v1, SVector v2);
00137 
00138 //
00139 // Utility\Math.cpp functions
00140 //
00141 double  WrapTwoPi (double rad);
00142 double  WrapPi (double rad);
00143 double  Wrap360 (double deg);
00144 int     Wrap360 (int deg);
00145 double  Wrap180 (double deg);
00146 float Wrap24 (float hrs);
00147 float Wrap60 (float min);
00148 double FeetPerLonArcsec (double lat);
00149 
00150 //
00151 // AddVector computes a new global position based on a starting position (lat/lon/alt)
00152 //   and an offset vector.  This function uses a fast, simple approximation and
00153 //   should only be used for relatively short distances (less than a couple of miles).
00154 //   For long distances, use the GreatCircle... functions.
00155 //
00156 //    x = East/West offset in feet, East is positive
00157 //    y = Altitude offset in feet, Up is positive
00158 //    z = North/South offset in feet, North is positive
00159 //
00160 
00161 SPosition AddVector(SPosition &from, SVector &v);
00162 
00163 //
00164 // SubtractVector computes a new global position based on a starting position
00165 //   and an offset vector.  This is essentially the same as AddVector except
00166 //   the distances are inverted (i.e. an argument to AddVector of +20 feet
00167 //   is equivalent to the same argument to SubtractVector of -20 feet).
00168 //
00169 SPosition SubtractVector(SPosition &from, SVector &v);
00170 
00171 //
00172 // SubtractPosition computes the vector offset between two globe positions.
00173 //   See "AddVector" for details on the result.
00174 //
00175 SVector SubtractPosition(SPosition &from, SPosition &to);
00176 
00177 
00178 float NmToMi (float nm);
00179 float MiToNm (float mi);
00180 float FeetToNm (float feet);
00181 float NmToFeet (float nm);
00182 float FpsToKt (float fps);
00183 
00184 double  FeetToMetres (double ft);
00185 double  MetresToFeet (double m);
00186 float KmToNm (float km);
00187 float NmToKm (float nm);
00188 
00189 float RadToDeg (float rad);
00190 double  RadToDeg (double rad);
00191 float DegToRad (float deg);
00192 double  DegToRad (double deg);
00193 
00194 char *TagString (char* s, Tag tag);
00195 Tag   StringToTag (const char* s);
00196 void  TagToString (char* s, Tag tag);
00197 
00198 void FormatPosition (SPosition pos, char* s);
00199 void FormatRADec (double ra, double dec, char* s);
00200 void FormatSiderealTime (double st, char *s);
00201 
00202 void TrimTrailingWhitespace (char* s);
00203 
00204 
00205 //
00206 // Utility\Messaging.cpp
00207 //
00208 EMessageResult SendMessage (SMessage *msg);
00209 
00210 
00211 //
00212 // Utility\Scenery.cpp functions
00213 //
00214 
00215 //
00216 // The following struct is used to represent CIE chrominance/luminance data
00217 //   triplets, either Yxy or XYZ forms
00218 //
00219 typedef struct {
00220   union {
00221     struct {
00222       float Y;
00223       float x;
00224       float y;
00225     } Yxy;
00226     struct {
00227       float X;
00228       float Y;
00229       float Z;
00230     } XYZ;
00231   };
00232 } SCIE;
00233 
00234 
00235 //
00236 // Convert CIE Yxy triplet to CIE XYZ
00237 //
00238 // Input parameters:
00239 //  Y CIE Yxy luminance Y value   [0..100]
00240 //  x CIE Yxy chrominance x value   [0..1]
00241 //  y CIE Yxy chrominance y value   [0..1]
00242 //
00243 void CIE_Yxy_to_XYZ (SCIE in, SCIE &out);
00244 
00245 //
00246 // Convert CIE XYZ triplet to RGB colour using the D65 white point.
00247 //
00248 void CIE_XYZ_to_RGB_D65 (SCIE XYZ, sgdVec3 &RGB);
00249 
00250 
00251 //
00252 // Convert lat/lon into globe tile
00253 //
00254 // Arguments:
00255 //  latArg
00256 //    Latitude is specified as decimal value in the range [-90.0, +90.0]
00257 //      as degrees North (+) or South (-) of the Equator.  The Fly! scenery
00258 //      system does not model extreme latitudes north or south of approx.
00259 //      85 degrees.
00260 //
00261 //  lonArg
00262 //    Longitude is specified as decimal value in the range [-180.0, 180.0]
00263 //      as degrees East (+) or West (-) of the Prime Meridian.  Some sample
00264 //      (approximate) longitude values:
00265 //
00266 //  iLatGlobe, iLonGlobe
00267 //    Globe tile indexes
00268 //
00269 // Return code:
00270 //  bool
00271 //    Indicates success or failure of the conversion.  If the function
00272 //    returns false then the contents of the tile argument are undefined.
00273 //
00274 void init_globe_tile_table ();
00275 void lat_lon_to_qgt (double latArg, double lonArg, int &x, int &z);
00276 void lat_lon_to_globe_tile (double latArg, double lonArg, int &x, int &z);
00277 
00278 void qgt_lon_bounds (int x, double &w, double &e);
00279 void qgt_lat_bounds (int z, double &s, double &n);
00280 void globe_tile_lon_bounds (int x, double &w, double &e);
00281 void globe_tile_lat_bounds (int z, double &s, double &n);
00282 
00283 void qgt_position (SPosition pos, int &qx, int &qz, double &x, double &z);
00284 void delta_qgt (SPosition pos, int rx, int rz, double &x, double &z);
00285 
00286 float TerrainScale (SPosition pos);
00287 float GlobeTileDistance (void);
00288 SVector PosToFlatCartesian (SPosition pos, int gx, int gz);
00289 SVector PosToFlatCartesian (SPosition pos);
00290 SVector PosToScaledFlatCartesian (SPosition pos);
00291 SVector PosToFlatCartesianQgt (SPosition pos, int x, int z);
00292 SVector PosToFlatCartesianQgt (SPosition pos);
00293 SVector PosToScaledFlatCartesianQgt (SPosition pos);
00294 
00295 //
00296 // CImageTIFF
00297 //
00298 // Implemented in Utility/ImageTIFF.cpp
00299 //
00300 class CImageTIFF {
00301 public:
00302   // Constructor/Destructor
00303   CImageTIFF (const char* tifFilename);
00304   ~CImageTIFF (void);
00305 
00306   // CImageTIFF methods
00307   int         GetWidth (void);
00308   int         GetHeight (void);
00309   GLuint      CreateTexture (bool mipmap);
00310 
00311 protected:
00312   char      name[64];
00313   int       width, height, depth;
00314   GLubyte   *image;
00315   GLuint    texid;
00316 };
00317 
00318 
00319 //
00320 // CMaskImage
00321 //
00322 // Implemented in Utility\RawImage.cpp
00323 //
00324 class CMaskImage {
00325 public:
00326   // Constructor/Destructor
00327   CMaskImage (int width, int height);
00328   CMaskImage (int width, int height, const GLubyte *data);
00329   CMaskImage (int width, int height, const char* rawFilename);
00330   CMaskImage (const CMaskImage &src);
00331   ~CMaskImage (void);
00332 
00333   // CMaskImage methods
00334   int     GetWidth (void);
00335   int     GetHeight (void);
00336 
00337   // void   MaskPoint (int x, int y);
00338   // void   MaskLine (int x1, int y1, int x2, int y2);
00339   // void   MaskPolygon (int nPoints, int* data);
00340 
00341 public:
00342   char      name[64];
00343   int       width, height;
00344   GLubyte   *rawdata;
00345 };
00346 
00347 
00348 //
00349 // CRawImage
00350 //
00351 // Implemented in RawImage.cpp
00352 //
00353 class CRawImage {
00354 public:
00355   // Constructor/Destructor
00356   CRawImage (const char* rawFilename,
00357              const char* actFilename = NULL,
00358              const char* opaFilename = NULL);
00359   CRawImage (int width,
00360              int height,
00361              const char* rawFilename,
00362              const char* actFilename = NULL,
00363              const char* opaFilename = NULL);
00364   CRawImage (const CRawImage &src);
00365   ~CRawImage (void);
00366 
00367   // CRawImage methods
00368   void      Init (void);
00369   void      Load (const char* rawFilename,
00370                   const char* actFilename,
00371                   const char* opaFilename = NULL);
00372   int       GetWidth (void);
00373   int       GetHeight (void);
00374   void      SetName (const char* name);
00375   void      CreateTransferMaps (void);
00376   GLubyte*  GetRGBImageData (void);
00377   GLubyte*  GetRGBAImageData (void);
00378 
00379   void      Copy (const CRawImage &src);
00380   void      CopyMasked (const CRawImage *src, const CMaskImage *mask);
00381   void      Merge (const CRawImage *raw, const CMaskImage *mask, int actOffset);
00382   void      MergeTransitions (const CRawImage *bRaw, const CMaskImage *bMask,
00383                   const CRawImage *xRaw, const CMaskImage *xMask,
00384                   const CRawImage *yRaw, const CMaskImage *yMask);
00385   GLuint    GetTexture (bool mipmap);
00386   void      FreeTexture (void);
00387 
00388 protected:
00389   char    name[64];
00390   int     width, height;
00391   GLubyte   *actdata;
00392   GLubyte   *rawdata;
00393   GLubyte   *opadata;
00394   GLuint    texid;
00395 
00396   GLfloat   *xferRed;
00397   GLfloat   *xferGreen;
00398   GLfloat   *xferBlue;
00399   GLfloat   *xferAlpha;
00400 };
00401 
00402 
00403 class CSunRawImage : public CRawImage {
00404 public:
00405   CSunRawImage (int width,
00406               int height,
00407           const char* rawFilename,
00408           const char* actFilename);
00409 };
00410 
00411 
00412 //
00413 // CModelBIN
00414 //
00415 // Implemented in Utility/ModelBIN.cpp
00416 //
00417 class CModelBIN {
00418 public:
00419   // Constructors / Destructor
00420   CModelBIN (const char* binFilename);
00421   ~CModelBIN ();
00422 
00423   // CModelACM methods
00424   ssgEntity *GetSSGEntity (void);
00425 
00426 protected:
00427   ssgBranch       *top;           // Top-level branch for entire model
00428   CRawImage       *rawtexture;    // Texture image (RAW/ACT file)
00429 };
00430 
00431 
00432 //
00433 // CModelSMF
00434 //
00435 // Implemented in Utility/ModelSMF.cpp
00436 //
00437 class CModelSMF {
00438 public:
00439   // Constructors / Destructor
00440   CModelSMF (const char* smfFilename);
00441   ~CModelSMF ();
00442 
00443   // CModelACM methods
00444   ssgEntity *GetSSGEntity (void);
00445 
00446 protected:
00447   ssgTransform    *top;           // Top-level transform branch for entire model
00448 };
00449 
00450 
00451 //
00452 // CModelACM
00453 //
00454 // Implemented in Utility/ModelACM.cpp
00455 //
00456 class CModelACM {
00457 public:
00458   // Constructors / Destructor
00459   CModelACM (const char* acmFilename);
00460   ~CModelACM ();
00461 
00462   // CModelACM methods
00463   ssgEntity *GetSSGEntity (void);
00464 
00465 protected:
00466   ssgTransform    *top;           // Top-level transform branch for entire model
00467   CImageTIFF      *tifftexture;   // TIFF texture image
00468 };
00469 
00470 
00471 //
00472 // Surface and Bitmap utilities
00473 //
00474 // Implemented in Utility/Bitmaps.cpp
00475 //
00476 SSurface*     CreateSurface(int width, int height);
00477 void          FreeSurface(SSurface *surface);
00478 void          EraseSurface(SSurface *surface);
00479 void          EraseSurfaceRGB(SSurface *surface, unsigned int rgb);
00480 void          EraseSurfaceRGBA(SSurface *surface, unsigned int rgba);
00481 void          DrawDot(SSurface *surface, int x, int y, unsigned int rgbColor);
00482 void          DrawLine(SSurface *surface, int x1, int y1, int x2, int y2, unsigned int rgbColor);
00483 void          DrawRect(SSurface *surface, int x1, int y1, int x2, int y2, unsigned int rgbColor);
00484 void          FillRect(SSurface *surface, int x1, int y1, int x2, int y2, unsigned int rgbColor);
00485 void          DrawCircle(SSurface *surface, int xCenter, int yCenter, int radius, unsigned int rgbColor);
00486 unsigned int  MakeRGB(unsigned int r, unsigned int g, unsigned int b);
00487 unsigned int  MakeRGBA(unsigned int r, unsigned int g, unsigned int b, unsigned int a);
00488 void          UnmakeRGB (unsigned int rgb, unsigned int *r, unsigned int *g, unsigned int *b);
00489 void          Blit (SSurface *surface);
00490 void          BlitTransparent (SSurface *surface, unsigned int rgbTransparentColor);
00491 GLuint        TextureFromSurface (SSurface *s, bool mipmap);
00492 
00493 int           LoadBitmap(SBitmap *bm);
00494 void          DrawBitmap(SSurface *surface, SBitmap *bm, int x, int y, int frame);
00495 void          DrawBitmapPartial (SSurface *surface, SBitmap *bm, int dx, int dy, int sx1, int sy1, int sx2, int sy2, int frame);
00496 void          GetBitmapSize(SBitmap *bm, int *xSize, int *ySize);
00497 int           NumBitmapFrames(SBitmap *bm);
00498 void          FreeBitmap(SBitmap *bm);
00499 
00500 
00501 //
00502 // Font utilities
00503 //
00504 // Implemented in Utility/Fonts.cpp
00505 //
00506 int LoadFont (SFont *font);
00507 void FreeFont (SFont *font);
00508 void DrawChar (SSurface *surface, SFont *font,
00509                int x, int y, unsigned int rgbColour,
00510                char ascii);
00511 void DrawText (SSurface *surface, SFont *font,
00512                int x, int y, unsigned int rgbColour,
00513                const char* text);
00514 void DrawTextC (SSurface *surface, SFont *font,
00515                 int x, int y, unsigned int rgbColour,
00516                 const char* text);
00517 void DrawTextR (SSurface *surface, SFont *font,
00518                 int x, int y, unsigned int rgbColour,
00519                 const char* text);
00520 int TextHeight(SFont *font, const char *text);
00521 int TextWidth(SFont *font, const char *text);
00522 int CharHeight(SFont *font, char c);
00523 int CharWidth(SFont *font, char c);
00524 int MaxCharHeight(SFont *font);
00525 int MaxCharWidth(SFont *font);
00526 
00527 
00528 //
00529 // CDataMap
00530 //
00531 // Data mappings are common objects in stream files.  They use one of a number
00532 //   of techniques to map one float value (x) to another (y).
00533 //
00534 // Implemented in BaseObjects.cpp
00535 
00537 
00538 class CDataLookup {
00539 public:
00540   virtual float Lookup (float x) = 0;
00541 };
00542 
00543 class CDataMap : public CDataLookup, public CStreamObject {
00544 public:
00545   // Constructors / destructor
00546   CDataMap (void);
00547 
00548   // CStreamObject methods
00549   int Read (SStream *stream, Tag tag);
00550 
00551   // CDataLookup methods
00552   float Lookup (float x);
00553 
00554 protected:
00555   CDataLookup     *dataSource;
00556 };
00557 
00558 
00559 #endif // UTILITY_H
00560 
00561 
SourceForge.net Logo Documentation generated by doxygen