+// default constructor
+SGBucket::SGBucket() :
+ lon(-1000),
+ lat(-1000),
+ x(0),
+ y(0)
+{
+}
+
+bool SGBucket::isValid() const
+{
+ // The most northerly valid latitude is 89, not 90. There is no tile
+ // whose *bottom* latitude is 90. Similar there is no tile whose left egde
+ // is 180 longitude.
+ return (lon >= -180) &&
+ (lon < 180) &&
+ (lat >= -90) &&
+ (lat < 90) &&
+ (x < 8) && (y < 8);
+}
+
+void SGBucket::make_bad()
+{
+ lon = -1000;
+ lat = -1000;
+}
+
+#ifndef NO_DEPRECATED_API
+
+// constructor for specified location
+SGBucket::SGBucket(const double dlon, const double dlat) {
+ set_bucket(dlon, dlat);
+}
+#endif
+
+SGBucket::SGBucket(const SGGeod& geod) {
+ innerSet(geod.getLongitudeDeg(),
+ geod.getLatitudeDeg());
+}
+
+// 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;
+}
+
+/* Calculate the greatest integral value less than
+ * or equal to the given value (floor(x)),
+ * but attribute coordinates close to the boundary to the next
+ * (increasing) integral
+ */
+static int floorWithEpsilon(double x)
+{
+ return static_cast<int>(floor(x + SG_EPSILON));
+}
+
+#ifndef NO_DEPRECATED_API
+
+void SGBucket::set_bucket(double dlon, double dlat)
+{
+ innerSet(dlon, dlat);
+}
+
+
+void SGBucket::set_bucket(const SGGeod& geod)
+{
+ innerSet(geod.getLongitudeDeg(), geod.getLatitudeDeg());
+}
+
+#endif
+
+// Set the bucket params for the specified lat and lon
+void SGBucket::innerSet( double dlon, double dlat )
+{
+ if ((dlon < -180.0) || (dlon >= 180.0)) {
+ SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed longitude:" << dlon);
+ dlon = SGMiscd::normalizePeriodic(-180.0, 180.0, dlon);
+ }
+
+ if ((dlat < -90.0) || (dlat > 90.0)) {
+ SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed latitude" << dlat);
+ dlat = SGMiscd::clip(dlat, -90.0, 90.0);
+ }
+
+ //
+ // longitude first
+ //
+ double span = sg_bucket_span( dlat );
+ // we do NOT need to special case lon=180 here, since
+ // normalizePeriodic will never return 180; it will
+ // return -180, which is what we want.
+ lon = floorWithEpsilon(dlon);
+
+ // find subdivision or super lon if needed
+ if ( span <= 1.0 ) {
+ /* We have more than one tile per degree of
+ * longitude, so we need an x offset.
+ */
+ x = floorWithEpsilon((dlon - lon) / span);
+ } else {
+ /* We have one or more degrees per tile,
+ * so we need to find the base longitude
+ * of that tile.
+ *
+ * First we calculate the integral base longitude
+ * (e.g. -85.5 => -86) and then find the greatest
+ * multiple of span that is less than or equal to
+ * that longitude.
+ *
+ * That way, the Greenwich Meridian is always
+ * a tile border.
+ */
+ lon=static_cast<int>(floor(lon / span) * span);
+ x = 0;
+ }
+
+ //
+ // then latitude
+ //
+ lat = floorWithEpsilon(dlat);
+
+ // special case when passing in the north pole point (possibly due to
+ // clipping latitude above). Ensures we generate a valid bucket in this
+ // scenario
+ if (lat == 90) {
+ lat = 89;
+ y = 7;
+ } else {
+ /* Latitude base and offset are easier, as
+ * tiles always are 1/8 degree of latitude wide.
+ */
+ y = floorWithEpsilon((dlat - lat) * 8);
+ }
+}
+