+// default constructor
+SGBucket::SGBucket() {
+}
+
+
+// constructor for specified location
+SGBucket::SGBucket(const double dlon, const double dlat) {
+ set_bucket(dlon, dlat);
+}
+
+SGBucket::SGBucket(const SGGeod& geod) {
+ set_bucket(geod);
+}
+
+// create an impossible bucket if false
+SGBucket::SGBucket(const bool is_good) {
+ set_bucket(0.0, 0.0);
+ if ( !is_good ) {
+ lon = -1000;
+ }
+}
+
+
+// Parse a unique scenery tile index and find the lon, lat, x, and y
+SGBucket::SGBucket(const long int bindex) {
+ long int index = bindex;
+
+ lon = index >> 14;
+ index -= lon << 14;
+ lon -= 180;
+
+ lat = index >> 6;
+ index -= lat << 6;
+ lat -= 90;
+
+ y = index >> 3;
+ index -= y << 3;
+
+ x = index;
+}
+
+
+// Set the bucket params for the specified lat and lon
+void SGBucket::set_bucket( double *lonlat ) {
+ set_bucket( lonlat[0], lonlat[1] );
+}
+
+
+// Set the bucket params for the specified lat and lon
+void SGBucket::set_bucket( double dlon, double dlat ) {
+ //
+ // latitude first
+ //
+ double span = sg_bucket_span( dlat );
+ double diff = dlon - (double)(int)dlon;
+
+ // cout << "diff = " << diff << " span = " << span << endl;
+
+ if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
+ lon = (int)dlon;
+ } else {
+ lon = (int)dlon - 1;
+ }
+
+ // find subdivision or super lon if needed
+ if ( span < SG_EPSILON ) {
+ // polar cap
+ lon = 0;
+ x = 0;
+ } else if ( span <= 1.0 ) {
+ x = (int)((dlon - lon) / span);
+ } else {
+ if ( dlon >= 0 ) {
+ lon = (int)( (int)(lon / span) * span);
+ } else {
+ // cout << " lon = " << lon
+ // << " tmp = " << (int)((lon-1) / span) << endl;
+ lon = (int)( (int)((lon + 1) / span) * span - span);
+ if ( lon < -180 ) {
+ lon = -180;
+ }
+ }
+ x = 0;
+ }
+
+ //
+ // then latitude
+ //
+ diff = dlat - (double)(int)dlat;
+
+ if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
+ lat = (int)dlat;
+ } else {
+ lat = (int)dlat - 1;
+ }
+ y = (int)((dlat - lat) * 8);
+}
+
+
+void SGBucket::set_bucket(const SGGeod& geod)
+{
+ set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
+}
+