1 // BucketBox.cxx -- Helper for on demand database paging.
3 // Copyright (C) 2010 - 2011 Mathias Froehlich
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #ifndef _BUCKETBOX_HXX
21 #define _BUCKETBOX_HXX
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/math/SGGeometry.hxx>
32 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
35 static const unsigned _lonFactors[] = { 3, 5, 3, 2, 2, 2, /* sub degree */ 2, 2, 2 };
37 static const unsigned _latFactors[] = { 5, 3, 3, 2, 2, /* sub degree */ 2, 2, 2, 1 };
39 static unsigned product(const unsigned* factors, unsigned count)
47 /// /Rectangular/ sub part of the earths surface.
48 /// Stored with offset point in lon/lat and width and height.
49 /// The values are stored in a fixed point format having 3 fractional
50 /// bits which matches the SGBuckets maximum tile resolution.
52 /// Notable /design/ decision:
53 /// * The longitude maps to the interval [0,360[ which appears to be
54 /// counter productive for the file/directory names and the
55 /// texture coordinates which map [-180,180[.
56 /// The reason is that the buckets at 89deg longitude are 8deg
57 /// latitude width. So there is a bunch of buckets that range from
58 /// [176, -184[ longitude. So the wrap happens at 0deg instead
59 /// of 180deg since we have a cut edge for all latitudes.
60 /// * This is not meant to be an API class for simgear. This is
61 /// just an internal tool that I would like to keep in the SPT loader.
62 /// But I want to have coverage somehow tested with the usual unit
63 /// tests, which is the reason to have this file split out.
68 { _offset[0] = 0; _offset[1] = 0; _size[0] = 0; _size[1] = 0; }
69 BucketBox(double lon, double lat, double width, double height)
71 _offset[0] = _longitudeDegToOffset(lon);
72 _offset[1] = _latitudeDegToOffset(lat);
73 _size[0] = _degToSize(width);
74 _size[1] = _degToSize(height);
76 BucketBox(const BucketBox& bucketBox)
78 _offset[0] = bucketBox._offset[0];
79 _offset[1] = bucketBox._offset[1];
80 _size[0] = bucketBox._size[0];
81 _size[1] = bucketBox._size[1];
84 BucketBox& operator=(const BucketBox& bucketBox)
86 _offset[0] = bucketBox._offset[0];
87 _offset[1] = bucketBox._offset[1];
88 _size[0] = bucketBox._size[0];
89 _size[1] = bucketBox._size[1];
94 { return _size[0] == 0 || _size[1] == 0; }
96 unsigned getOffset(unsigned i) const
97 { return _offset[i]; }
98 void setOffset(unsigned i, unsigned offset)
99 { _offset[i] = offset; }
101 unsigned getSize(unsigned i) const
103 void setSize(unsigned i, unsigned size)
106 double getLongitudeDeg() const
107 { return _offsetToLongitudeDeg(_offset[0]); }
108 void setLongitudeDeg(double lon)
109 { _offset[0] = _longitudeDegToOffset(lon); }
111 double getLatitudeDeg() const
112 { return _offsetToLatitudeDeg(_offset[1]); }
113 void setLatitudeDeg(double lat)
114 { _offset[1] = _latitudeDegToOffset(lat); }
116 double getWidthDeg() const
117 { return _sizeToDeg(_size[0]); }
118 void setWidthDeg(double width)
119 { _size[0] = _degToSize(width); }
121 double getHeightDeg() const
122 { return _sizeToDeg(_size[1]); }
123 void setHeightDeg(double height)
124 { _size[1] = _degToSize(height); }
126 bool getWidthIsBucketSize() const
128 if (_size[0] <= _bucketSpanAtOffset(_offset[1]))
130 return _size[0] <= _bucketSpanAtOffset(_offset[1] + _size[1] - 1);
133 bool getHeightIsBucketSize() const
134 { return _size[1] == 1; }
135 bool getIsBucketSize() const
136 { return getHeightIsBucketSize() && getWidthIsBucketSize(); }
138 SGBucket getBucket() const
140 // left align longitude offsets
141 unsigned offset = _offset[0] - _offset[0] % _bucketSpanAtOffset(_offset[1]);
142 return SGBucket(_offsetToLongitudeDeg(offset), _offsetToLatitudeDeg(_offset[1]));
145 BucketBox getSubBoxHeight(unsigned j, unsigned level) const
149 unsigned plat = product(_latFactors + level, Elements(_latFactors) - level);
150 unsigned plat1 = plat*_latFactors[level - 1];
151 box._offset[0] = _offset[0];
152 box._offset[1] = _offset[1] - _offset[1] % plat1 + j*plat;
153 box._size[0] = _size[0];
159 BucketBox getSubBoxWidth(unsigned i, unsigned level) const
163 unsigned plon = product(_lonFactors + level, Elements(_lonFactors) - level);
164 unsigned plon1 = plon*_lonFactors[level - 1];
165 box._offset[0] = _offset[0] - _offset[0] % plon1 + i*plon;
166 box._offset[1] = _offset[1];
168 box._size[1] = _size[1];
170 box._offset[0] = _normalizeLongitude(box._offset[0]);
175 unsigned getWidthLevel() const
176 { return _getLevel(_lonFactors, Elements(_lonFactors), _offset[0], _offset[0] + _size[0]); }
177 unsigned getHeightLevel() const
178 { return _getLevel(_latFactors, Elements(_latFactors), _offset[1], _offset[1] + _size[1]); }
180 unsigned getWidthIncrement(unsigned level) const
182 level = SGMisc<unsigned>::clip(level, 5, Elements(_lonFactors));
183 return product(_lonFactors + level, Elements(_lonFactors) - level);
185 unsigned getHeightIncrement(unsigned level) const
187 level = SGMisc<unsigned>::clip(level, 5, Elements(_latFactors));
188 return product(_latFactors + level, Elements(_latFactors) - level);
191 unsigned getStartLevel() const
193 if (getWidthIsBucketSize())
194 return getHeightLevel();
195 return std::min(getWidthLevel(), getHeightLevel());
198 SGSpheref getBoundingSphere() const
201 unsigned incx = 10*8;
202 for (unsigned i = 0; incx != 0; i += incx) {
203 unsigned incy = 10*8;
204 for (unsigned j = 0; incy != 0; j += incy) {
205 sphere.expandBy(SGVec3f::fromGeod(_offsetToGeod(_offset[0] + i, _offset[1] + j, -1000)));
206 sphere.expandBy(SGVec3f::fromGeod(_offsetToGeod(_offset[0] + i, _offset[1] + j, 10000)));
207 incy = std::min(incy, _size[1] - j);
209 incx = std::min(incx, _size[0] - i);
211 return SGSpheref(sphere.getCenter(), sphere.getRadius() + 5000);
214 // Split the current box into up to two boxes that do not cross the 360 deg border.
215 unsigned periodicSplit(BucketBox bucketBoxList[2]) const
220 bucketBoxList[0] = *this;
221 bucketBoxList[0]._offset[0] = _normalizeLongitude(bucketBoxList[0]._offset[0]);
222 if (bucketBoxList[0]._offset[0] + bucketBoxList[0]._size[0] <= 360*8)
225 bucketBoxList[1] = bucketBoxList[0];
226 bucketBoxList[0]._size[0] = 360*8 - bucketBoxList[0]._offset[0];
228 bucketBoxList[1]._offset[0] = 0;
229 bucketBoxList[1]._size[0] = _size[0] - bucketBoxList[0]._size[0];
234 unsigned getSubDivision(BucketBox bucketBoxList[], unsigned bucketBoxListSize) const
236 unsigned numTiles = 0;
238 // Quad tree like structure in x and y direction
239 unsigned widthLevel = getWidthLevel();
240 unsigned heightLevel = getHeightLevel();
243 if (getWidthIsBucketSize()) {
246 level = std::min(widthLevel, heightLevel);
248 for (unsigned j = 0; j < _latFactors[level]; ++j) {
249 BucketBox heightSplitBox = getSubBoxHeight(j, level + 1);
251 heightSplitBox = _intersection(*this, heightSplitBox);
252 if (heightSplitBox.empty())
255 if (heightSplitBox.getWidthIsBucketSize()) {
256 bucketBoxList[numTiles++] = heightSplitBox;
257 assert(numTiles <= bucketBoxListSize);
259 for (unsigned i = 0; i < _lonFactors[widthLevel]; ++i) {
260 BucketBox childBox = _intersection(heightSplitBox, heightSplitBox.getSubBoxWidth(i, widthLevel + 1));
261 if (childBox.empty())
264 bucketBoxList[numTiles++] = childBox;
265 assert(numTiles <= bucketBoxListSize);
272 unsigned getTileTriangles(unsigned i, unsigned j, unsigned width, unsigned height,
273 SGVec3f points[6], SGVec3f normals[6], SGVec2f texCoords[6]) const
275 unsigned numPoints = 0;
277 unsigned x0 = _offset[0] + i;
278 unsigned x1 = x0 + width;
280 unsigned y0 = _offset[1] + j;
281 unsigned y1 = y0 + height;
283 SGGeod p00 = _offsetToGeod(x0, y0, 0);
284 SGVec3f v00 = SGVec3f::fromGeod(p00);
285 SGVec3f n00 = SGQuatf::fromLonLat(p00).backTransform(SGVec3f(0, 0, -1));
286 SGVec2f t00(x0*1.0/(360*8) + 0.5, y0*1.0/(180*8));
288 SGGeod p10 = _offsetToGeod(x1, y0, 0);
289 SGVec3f v10 = SGVec3f::fromGeod(p10);
290 SGVec3f n10 = SGQuatf::fromLonLat(p10).backTransform(SGVec3f(0, 0, -1));
291 SGVec2f t10(x1*1.0/(360*8) + 0.5, y0*1.0/(180*8));
293 SGGeod p11 = _offsetToGeod(x1, y1, 0);
294 SGVec3f v11 = SGVec3f::fromGeod(p11);
295 SGVec3f n11 = SGQuatf::fromLonLat(p11).backTransform(SGVec3f(0, 0, -1));
296 SGVec2f t11(x1*1.0/(360*8) + 0.5, y1*1.0/(180*8));
298 SGGeod p01 = _offsetToGeod(x0, y1, 0);
299 SGVec3f v01 = SGVec3f::fromGeod(p01);
300 SGVec3f n01 = SGQuatf::fromLonLat(p01).backTransform(SGVec3f(0, 0, -1));
301 SGVec2f t01(x0*1.0/(360*8) + 0.5, y1*1.0/(180*8));
304 points[numPoints] = v00;
305 normals[numPoints] = n00;
306 texCoords[numPoints] = t00;
309 points[numPoints] = v10;
310 normals[numPoints] = n10;
311 texCoords[numPoints] = t10;
314 points[numPoints] = v01;
315 normals[numPoints] = n01;
316 texCoords[numPoints] = t01;
320 points[numPoints] = v11;
321 normals[numPoints] = n11;
322 texCoords[numPoints] = t11;
325 points[numPoints] = v01;
326 normals[numPoints] = n01;
327 texCoords[numPoints] = t01;
330 points[numPoints] = v10;
331 normals[numPoints] = n10;
332 texCoords[numPoints] = t10;
339 static unsigned _normalizeLongitude(unsigned offset)
340 { return offset - (360*8)*(offset/(360*8)); }
342 static unsigned _longitudeDegToOffset(double lon)
344 lon = SGMiscd::normalizePeriodic(0, 360, lon);
345 unsigned offset = (unsigned)(8*lon + 0.5);
346 return _normalizeLongitude(offset);
348 static double _offsetToLongitudeDeg(unsigned offset)
351 return offset*0.125 - 360;
356 static unsigned _latitudeDegToOffset(double lat)
360 unsigned offset = (unsigned)(8*(lat + 90) + 0.5);
365 static double _offsetToLatitudeDeg(unsigned offset)
366 { return offset*0.125 - 90; }
368 static unsigned _degToSize(double deg)
372 return (unsigned)(8*deg + 0.5);
374 static double _sizeToDeg(unsigned size)
375 { return size*0.125; }
377 static unsigned _bucketSpanAtOffset(unsigned offset)
378 { return (unsigned)(8*sg_bucket_span(_offsetToLatitudeDeg(offset) + 0.0625) + 0.5); }
380 static SGGeod _offsetToGeod(unsigned offset0, unsigned offset1, double elev)
381 { return SGGeod::fromDegM(_offsetToLongitudeDeg(offset0), _offsetToLatitudeDeg(offset1), elev); }
383 static unsigned _getLevel(const unsigned factors[], unsigned nFactors, unsigned begin, unsigned end)
385 unsigned rbegin = end - 1;
386 for (; 0 < nFactors;) {
390 begin /= factors[nFactors];
391 rbegin /= factors[nFactors];
397 static BucketBox _intersection(const BucketBox& box0, const BucketBox& box1)
400 for (unsigned i = 0; i < 2; ++i) {
401 box._offset[i] = std::max(box0._offset[i], box1._offset[i]);
402 unsigned m = std::min(box0._offset[i] + box0._size[i], box1._offset[i] + box1._size[i]);
403 if (m <= box._offset[i])
406 box._size[i] = m - box._offset[i];
409 box._offset[0] = _normalizeLongitude(box._offset[0]);
419 operator==(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
421 if (bucketBox0.getOffset(0) != bucketBox1.getOffset(0))
423 if (bucketBox0.getOffset(1) != bucketBox1.getOffset(1))
425 if (bucketBox0.getSize(0) != bucketBox1.getSize(0))
427 if (bucketBox0.getSize(1) != bucketBox1.getSize(1))
433 operator!=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
434 { return !operator==(bucketBox0, bucketBox1); }
437 operator<(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
439 if (bucketBox0.getOffset(0) < bucketBox1.getOffset(0)) return true;
440 else if (bucketBox1.getOffset(0) < bucketBox0.getOffset(0)) return false;
441 else if (bucketBox0.getOffset(1) < bucketBox1.getOffset(1)) return true;
442 else if (bucketBox1.getOffset(1) < bucketBox0.getOffset(1)) return false;
443 else if (bucketBox0.getSize(0) < bucketBox1.getSize(0)) return true;
444 else if (bucketBox1.getSize(0) < bucketBox0.getSize(0)) return false;
445 else return bucketBox0.getSize(1) < bucketBox1.getSize(1);
449 operator>(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
450 { return operator<(bucketBox1, bucketBox0); }
453 operator<=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
454 { return !operator>(bucketBox0, bucketBox1); }
457 operator>=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
458 { return !operator<(bucketBox0, bucketBox1); }
460 /// Stream output operator.
461 /// Note that this is not only used for pretty printing but also for
462 /// generating the meta file names for on demand paging.
463 /// So, don't modify unless you know where else this is used.
464 template<typename char_type, typename traits_type>
465 std::basic_ostream<char_type, traits_type>&
466 operator<<(std::basic_ostream<char_type, traits_type>& os, const BucketBox& bucketBox)
468 double lon = bucketBox.getLongitudeDeg();
474 double lat = bucketBox.getLatitudeDeg();
484 os << "-" << bucketBox.getWidthDeg() << "x" << bucketBox.getHeightDeg();
488 /// Stream inout operator.
489 /// Note that this is used for reading the meta file names for on demand paging.
490 /// So, don't modify unless you know where else this is used.
491 template<typename char_type, typename traits_type>
492 std::basic_istream<char_type, traits_type>&
493 operator>>(std::basic_istream<char_type, traits_type>& is, BucketBox& bucketBox)
506 is.setstate(std::ios::failbit | is.rdstate());
513 is.setstate(std::ios::failbit | is.rdstate());
516 bucketBox.setLongitudeDeg(sign*num);
528 is.setstate(std::ios::failbit | is.rdstate());
535 bucketBox.setLatitudeDeg(sign*num);
541 is.setstate(std::ios::failbit | is.rdstate());
548 bucketBox.setWidthDeg(SGMiscd::min(num, 360));
554 is.setstate(std::ios::failbit | is.rdstate());
561 bucketBox.setHeightDeg(SGMiscd::min(num, 90 - bucketBox.getLatitudeDeg()));
566 } // namespace simgear