/****************************************************************************
 *   gcxx.h : header file included in every GFC file.
 *   
 *   Change Log :
 *		Spring 1996 Initial version,  Liujian Qian
 *
 *   $Id: gcxx.h,v 1.14 1999/02/23 04:23:06 qian Exp $
 ****************************************************************************/
#ifndef _GCXX_H_
#define _GCXX_H_

#include <stdlib.h>    // for abort()
#include <iostream.h>


#ifndef	NULL
#define NULL	((void *) 0)
#endif	

#ifndef TRUE
#define TRUE    1
#endif

#ifndef FALSE
#define FALSE   0 
#endif

#ifndef ON
#define ON	1
#endif
       
#ifndef OFF
#define OFF	0
#endif

#ifndef NOP
#define NOP	
#endif

// some internal definition of GFC

#ifndef BYTE	 
#define BYTE	uint8
#endif

// whether to generate change record for each ADT. not supported!
#undef  USE_CHANGEREC

typedef unsigned char   uchar;

// signed integers
typedef signed char	int8;		/* 8 bits */
typedef signed short	int16;		/* 16 bits */
typedef signed int	int32;		/* 32 bits */

// Unsigned integers
typedef unsigned char	uint8;		/*  8 bits */
typedef unsigned short	uint16;		/*  16 bits */
typedef unsigned int	uint32;		/*  32 bits */



#ifndef bool
#define bool int
#define false 0
#define true  1
#endif

// 64 bit integers -- used by our timestamp ADT. broken for WIN32!!
#if defined(_WIN32) || defined(__WIN32__)
typedef long                    int64;  /* fake 64-bit int */
typedef unsigned long           uint64; /* fake u 64-bit int */
#else 
typedef long long int		int64;  /* 64 bits */
typedef unsigned long long int 	uint64; /* 64 bits */
#endif


typedef unsigned long    MAD;	//Morton Address.

typedef double    Coord;  //system-wide definition of coordinate type.


/*
 *-----------------------------------------------------------------
 *
 * 	 GFC general structs/enums/unions
 *
 *-----------------------------------------------------------------
 */

struct GFCConfig
{
    static uint32 	major_version;
    static uint32	minor_version;
    static uint32	micro_version;
    static uint32	grid_slab_size;
    static int32	shm_id;
    static int8		use_mmap;
    static int8		big_endian;
    static int8		debug;
    static uint32	version();
};


/**
 * the type enumeration for all GFC ADTs
 */
enum GType 
{
    _Base =0,  	 _Int,  	_Char,      _Varchar,    _Real,
    _Timestamp,  
    _Point,	 _Segment, 	_Polygon, 
    _Arc,   	 _Rect,	 	_Circle,     _Triangle,
    _Raster,     _Grid,		_Graph,	     _DEM
};


/**
 * GFC return codes
 */
enum Result {
    GOk         = 0,
    GFail       = 1,
    GNotFound	= 2,
    GNoMem      = 3,
    GItem       = 4,
    GWarn	= 5,
    GBound	= 6,
    GNoMore	= 7
};


/**
 * various geospatial units
 */
enum GeoUnit {
    //linear units
    Meter = 1,                  // 1.0
    Foot_International,         // 0.3048
    Foot_US,                    // 12/39.37
    Foot_US_Modified,           // 12.0004584/39.37
    Foot_Clark,                 // 12/39.370432
    Foot_Indian,                // 12/39.370141
    Link,                       // 7.92/39.370432
    Link_Benoit,                // 7.92/39.370113
    Link_Sears,                 // 7.92/39.370147 
    Chain_Benoit,               // 792/39.370113
    Chain_Sears,                // 792/39.370147
    Yard_Indian,                // 36/39.370141
    Yard_Sears,                 // 36/39.370147
    Fathom,                     // 1.8288
    Mile_Nautical,              // 1852.0
    Mile,			// 1.609 km

    //angular units
    Radian  = 100,              // 1.0
    Degree,                     // PI/180
    Minute,                     // (PI/180)/60
    Second,                     // (PI/180)/3600
    Gon,                        // PI/200
    Grad                        // PI/200
};

/**
 *  classification of polygon shapes.
 */
enum PolygonShape {
    GCONVEX 	= 0,	
    GNOT_CONVEX,
    GNOT_CONVEX_DEGENERATE,
    GCONVEX_DEGENERATE,
    GCONVEX_CCW,
    GCONVEX_CW
};



/*
 *-----------------------------------------------------------------
 *
 * 	 GFC macros
 *
 *-----------------------------------------------------------------
 */

#define GMAX(x, y)	((x) > (y) ? (x) : (y))

#define GMIN(x, y)	((x) < (y) ? (x) : (y))

#define GABS(x)		((x) >= 0 ? (x) : -(x))

#define GSWAP(a, b)	{ a^=b; b^=a; a^=b; }

#define CONPTR(p)	((const char*)(p))

//get the block distance between two points.
#ifndef GBLOCKDIST
#define GBLOCKDIST(sx,sy,ex,ey)  ( GABS((ex)-(sx)) + GABS((ey)-(sy)) )
#endif

// round a to nearest int 
#ifndef GROUND
#define GROUND(a)	((a)>0 ? (int)((a)+0.5) : -(int)(0.5-(a)))
#endif

// take sign of a, either -1, 0, or 1 
#ifndef GZSGN
#define GZSGN(a)		(((a)<0) ? -1 : (a)>0 ? 1 : 0)	
#endif

// take binary sign of a, either -1, or 1 if >= 0 
#define GSGN(a)		(((a)<0) ? -1 : 1)

// square a
#define GSQR(a)		((a)*(a))	

// clamp the input to the specified range 
#define GCLAMP(v,l,h)	((v)<(l) ? (l) : (v) > (h) ? (h) : v)

// linear interpolation from l (when a=0) to h (when a=1)
// *** equal to (a*h) +((1-a)*l) ***
#define GLERP(a,l,h)	((l)+(((h)-(l))*(a)))

//macro to ensure 8-byte alignment (for Solaris)
#ifndef GAlign
#define GALIGNON 0x8
#define GALIGNON1 (GALIGNON-1)
#define GAlign(sz) ((sz + GALIGNON1) & ~GALIGNON1)
#endif

//
//    NOTE:  The following macro to determine if two numbers  
//    have the same sign, is for 2's complement number        
//    representation.  It will need to be modified for other  
//    number systems.                                         
//                                                            
#define SAME_SIGNS( a, b )	\
		(((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 )


#define EARTH_RADIUS	6370997  	//shpere earth's radius (in meters)

#ifndef _PI_
#define _PI_		3.14159265358979323846
#endif

#ifndef PI
#define PI              _PI_
#endif
#ifndef TWO_PI
#define TWO_PI  	6.2831853071795864769	// 2 * pi 
#endif
#ifndef HALF_PI
#define HALF_PI		1.5707963267948966	// pi / 2 
#endif
#ifndef E
#define E		2.718282		// the venerable e 
#endif

#define SQRT2		1.414214		// sqrt(2) 
#define SQRT3		1.732051		// sqrt(3) 
#define GOLDEN		1.618034		// the golden ratio 

#ifndef RAD_TO_DEG
#define RAD_TO_DEG	57.29577951308232
#endif

#ifndef DEG_TO_RAD
#define DEG_TO_RAD	.0174532925199432958
#endif

//convert decimal degree to radian
#define GRADIAN(x)	((x)*DEG_TO_RAD)
//convert radian to decimal degree
#define GDEGREE(x)	((x)*RAD_TO_DEG)

#define UNDEFINED	-9999		// return this if operator has no meaning

#define PolyDim	   	2		// dimension of poly|lines|gons.


//byte size of (n) coordinates
#define  COORD_SZ(n)	(n)*PolyDim*sizeof(Coord)

#define  InvalidCoord 	1.175494351E-38F

//
//maximum size of a Varchar string
#define MAX_STR  1024

//
//maximum length of names (layer name, db name, attribute name, et al)
#define MAX_NAME  64

#ifndef VAR_LEN
#define VAR_LEN	-1	//for variable length ADTs
#endif


// 
// macros for swapping byte order. Originally from Linux Kernel.
//

#define swab16(x) \
        ((uint16)( \
                (((uint16)(x) & 0x00ffU) << 8) | \
                (((uint16)(x) & 0xff00U) >> 8) ))

#define swab32(x) \
        ((uint32)( \
                (((uint32)(x) & (uint32)0x000000ffUL) << 24) | \
                (((uint32)(x) & (uint32)0x0000ff00UL) <<  8) | \
                (((uint32)(x) & (uint32)0x00ff0000UL) >>  8) | \
                (((uint32)(x) & (uint32)0xff000000UL) >> 24) ))

#if defined(_WIN32) || defined(__WIN32__)
#define swab64(x)  (x)
#else
#define swab64(x) \
        ((uint64)( \
                (uint64)(((uint64)(x) & (uint64)0x00000000000000ffULL) << 56) | \
                (uint64)(((uint64)(x) & (uint64)0x000000000000ff00ULL) << 40) | \
                (uint64)(((uint64)(x) & (uint64)0x0000000000ff0000ULL) << 24) | \
                (uint64)(((uint64)(x) & (uint64)0x00000000ff000000ULL) <<  8) | \
                (uint64)(((uint64)(x) & (uint64)0x000000ff00000000ULL) >>  8) | \
                (uint64)(((uint64)(x) & (uint64)0x0000ff0000000000ULL) >> 24) | \
                (uint64)(((uint64)(x) & (uint64)0x00ff000000000000ULL) >> 40) | \
                (uint64)(((uint64)(x) & (uint64)0xff00000000000000ULL) >> 56) ))

#endif



/*
 *-------------------------------------------------------------------------
 * 
 * 	GFC error-related Macros
 * 
 *-------------------------------------------------------------------------
 */
#define GAssert(x) { \
		         if (!(x)) \
			 { \
			   cerr << "GFC assertion failed in \"" \
		                << __FILE__ << ": " \
				<< __LINE__ << "\"" << endl; \
			   abort(); \
			 } \
                       }

#define msgFail(msg) \
{ \
    cerr<<"Fatal: @"<< __FILE__<<":"<< __LINE__<<" "<<msg<<"\n";  \
    return GFail; \
}
    

#define msgWarn(msg) \
{ \
    cerr<<"Warning: @"<< __FILE__<<":"<< __LINE__<<" "<<(msg)<<"\n"; \
} 

extern void GDBG(char* fmt, ...);


#endif  /* ifndef _GCXX_H_ */



Documentation generated by lqian@lqian-sun on Wed Jul 14 09:36:10 EDT 1999