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[] = { 2, 5, 3, 3, 2, 2, /* sub degree */ 2, 2, 2 };
37 static const unsigned _latFactors[] = { 3, 5, 1, 3, 2, 2, /* sub degree */ 2, 2, 2 };
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 [-180,180[.
54 /// The latitude maps to the interval [-90,90].
55 /// This works now that the tiles do no longer cut
56 /// neither the 180deg nor the 0deg boundary.
57 /// * This is not meant to be an API class for simgear. This is
58 /// just an internal tool that I would like to keep in the SPT loader.
59 /// But I want to have coverage somehow tested with the usual unit
60 /// tests, which is the reason to have this file split out.
65 { _offset[0] = 0; _offset[1] = 0; _size[0] = 0; _size[1] = 0; }
66 BucketBox(double lon, double lat, double width, double height)
68 _offset[0] = _longitudeDegToOffset(lon);
69 _offset[1] = _latitudeDegToOffset(lat);
70 _size[0] = _degToSize(width);
71 _size[1] = _degToSize(height);
73 BucketBox(const BucketBox& bucketBox)
75 _offset[0] = bucketBox._offset[0];
76 _offset[1] = bucketBox._offset[1];
77 _size[0] = bucketBox._size[0];
78 _size[1] = bucketBox._size[1];
81 BucketBox& operator=(const BucketBox& bucketBox)
83 _offset[0] = bucketBox._offset[0];
84 _offset[1] = bucketBox._offset[1];
85 _size[0] = bucketBox._size[0];
86 _size[1] = bucketBox._size[1];
91 { return _size[0] == 0 || _size[1] == 0; }
93 unsigned getOffset(unsigned i) const
94 { return _offset[i]; }
95 void setOffset(unsigned i, unsigned offset)
96 { _offset[i] = offset; }
98 unsigned getSize(unsigned i) const
100 void setSize(unsigned i, unsigned size)
103 double getLongitudeDeg() const
104 { return _offsetToLongitudeDeg(_offset[0]); }
105 void setLongitudeDeg(double lon)
106 { _offset[0] = _longitudeDegToOffset(lon); }
108 double getLatitudeDeg() const
109 { return _offsetToLatitudeDeg(_offset[1]); }
110 void setLatitudeDeg(double lat)
111 { _offset[1] = _latitudeDegToOffset(lat); }
113 double getWidthDeg() const
114 { return _sizeToDeg(_size[0]); }
115 void setWidthDeg(double width)
116 { _size[0] = _degToSize(width); }
118 double getHeightDeg() const
119 { return _sizeToDeg(_size[1]); }
120 void setHeightDeg(double height)
121 { _size[1] = _degToSize(height); }
123 bool getWidthIsBucketSize() const
125 if (_size[0] <= _bucketSpanAtOffset(_offset[1]))
127 return _size[0] <= _bucketSpanAtOffset(_offset[1] + _size[1] - 1);
130 bool getHeightIsBucketSize() const
131 { return _size[1] == 1; }
132 bool getIsBucketSize() const
133 { return getHeightIsBucketSize() && getWidthIsBucketSize(); }
135 SGBucket getBucket() const
137 // left align longitude offsets
138 unsigned offset = _offset[0] - _offset[0] % _bucketSpanAtOffset(_offset[1]);
139 return SGBucket(_offsetToLongitudeDeg(offset), _offsetToLatitudeDeg(_offset[1]));
142 BucketBox getParentBox(unsigned level) const
145 unsigned plon = product(_lonFactors + level, Elements(_lonFactors) - level);
146 unsigned plat = product(_latFactors + level, Elements(_latFactors) - level);
147 box._offset[0] = _offset[0] - _offset[0] % plon;
148 box._offset[0] = _normalizeLongitude(box._offset[0]);
149 box._offset[1] = _offset[1] - _offset[1] % plat;
156 BucketBox getSubBoxHeight(unsigned j, unsigned level) const
160 unsigned plat = product(_latFactors + level, Elements(_latFactors) - level);
161 unsigned plat1 = plat*_latFactors[level - 1];
162 box._offset[0] = _offset[0];
163 box._offset[1] = _offset[1] - _offset[1] % plat1 + j*plat;
164 box._size[0] = _size[0];
170 BucketBox getSubBoxWidth(unsigned i, unsigned level) const
174 unsigned plon = product(_lonFactors + level, Elements(_lonFactors) - level);
175 unsigned plon1 = plon*_lonFactors[level - 1];
176 box._offset[0] = _offset[0] - _offset[0] % plon1 + i*plon;
177 box._offset[1] = _offset[1];
179 box._size[1] = _size[1];
181 box._offset[0] = _normalizeLongitude(box._offset[0]);
186 unsigned getWidthLevel() const
187 { return _getLevel(_lonFactors, Elements(_lonFactors), _offset[0], _offset[0] + _size[0]); }
188 unsigned getHeightLevel() const
189 { return _getLevel(_latFactors, Elements(_latFactors), _offset[1], _offset[1] + _size[1]); }
191 unsigned getWidthIncrement(unsigned level) const
193 level = SGMisc<unsigned>::clip(level, 5, Elements(_lonFactors));
194 return product(_lonFactors + level, Elements(_lonFactors) - level);
196 unsigned getHeightIncrement(unsigned level) const
198 level = SGMisc<unsigned>::clip(level, 5, Elements(_latFactors));
199 return product(_latFactors + level, Elements(_latFactors) - level);
202 unsigned getStartLevel() const
204 if (getWidthIsBucketSize())
205 return getHeightLevel();
206 return std::min(getWidthLevel(), getHeightLevel());
209 SGSpheref getBoundingSphere() const
212 unsigned incx = 10*8;
213 for (unsigned i = 0; incx != 0; i += incx) {
214 unsigned incy = 10*8;
215 for (unsigned j = 0; incy != 0; j += incy) {
216 sphere.expandBy(SGVec3f::fromGeod(_offsetToGeod(_offset[0] + i, _offset[1] + j, -1000)));
217 sphere.expandBy(SGVec3f::fromGeod(_offsetToGeod(_offset[0] + i, _offset[1] + j, 10000)));
218 incy = std::min(incy, _size[1] - j);
220 incx = std::min(incx, _size[0] - i);
222 return SGSpheref(sphere.getCenter(), sphere.getRadius() + 5000);
225 // Split the current box into up to two boxes that do not cross the 360 deg border.
226 unsigned periodicSplit(BucketBox bucketBoxList[2]) const
231 bucketBoxList[0] = *this;
232 bucketBoxList[0]._offset[0] = _normalizeLongitude(bucketBoxList[0]._offset[0]);
233 if (bucketBoxList[0]._offset[0] + bucketBoxList[0]._size[0] <= 360*8)
236 bucketBoxList[1] = bucketBoxList[0];
237 bucketBoxList[0]._size[0] = 360*8 - bucketBoxList[0]._offset[0];
239 bucketBoxList[1]._offset[0] = 0;
240 bucketBoxList[1]._size[0] = _size[0] - bucketBoxList[0]._size[0];
245 unsigned getSubDivision(BucketBox bucketBoxList[], unsigned bucketBoxListSize) const
247 unsigned numTiles = 0;
249 // Quad tree like structure in x and y direction
250 unsigned widthLevel = getWidthLevel();
251 unsigned heightLevel = getHeightLevel();
254 if (getWidthIsBucketSize()) {
257 level = std::min(widthLevel, heightLevel);
259 for (unsigned j = 0; j < _latFactors[level]; ++j) {
260 BucketBox heightSplitBox = getSubBoxHeight(j, level + 1);
262 heightSplitBox = _intersection(*this, heightSplitBox);
263 if (heightSplitBox.empty())
266 if (heightSplitBox.getWidthIsBucketSize()) {
267 bucketBoxList[numTiles++] = heightSplitBox;
268 assert(numTiles <= bucketBoxListSize);
270 for (unsigned i = 0; i < _lonFactors[widthLevel]; ++i) {
271 BucketBox childBox = _intersection(heightSplitBox, heightSplitBox.getSubBoxWidth(i, widthLevel + 1));
272 if (childBox.empty())
275 bucketBoxList[numTiles++] = childBox;
276 assert(numTiles <= bucketBoxListSize);
283 unsigned getTileTriangles(unsigned i, unsigned j, unsigned width, unsigned height,
284 SGVec3f points[6], SGVec3f normals[6], SGVec2f texCoords[6]) const
286 unsigned numPoints = 0;
288 unsigned x0 = _offset[0] + i;
289 unsigned x1 = x0 + width;
291 unsigned y0 = _offset[1] + j;
292 unsigned y1 = y0 + height;
294 SGGeod p00 = _offsetToGeod(x0, y0, 0);
295 SGVec3f v00 = SGVec3f::fromGeod(p00);
296 SGVec3f n00 = SGQuatf::fromLonLat(p00).backTransform(SGVec3f(0, 0, -1));
297 SGVec2f t00(x0*1.0/(360*8), y0*1.0/(180*8));
299 SGGeod p10 = _offsetToGeod(x1, y0, 0);
300 SGVec3f v10 = SGVec3f::fromGeod(p10);
301 SGVec3f n10 = SGQuatf::fromLonLat(p10).backTransform(SGVec3f(0, 0, -1));
302 SGVec2f t10(x1*1.0/(360*8), y0*1.0/(180*8));
304 SGGeod p11 = _offsetToGeod(x1, y1, 0);
305 SGVec3f v11 = SGVec3f::fromGeod(p11);
306 SGVec3f n11 = SGQuatf::fromLonLat(p11).backTransform(SGVec3f(0, 0, -1));
307 SGVec2f t11(x1*1.0/(360*8), y1*1.0/(180*8));
309 SGGeod p01 = _offsetToGeod(x0, y1, 0);
310 SGVec3f v01 = SGVec3f::fromGeod(p01);
311 SGVec3f n01 = SGQuatf::fromLonLat(p01).backTransform(SGVec3f(0, 0, -1));
312 SGVec2f t01(x0*1.0/(360*8), y1*1.0/(180*8));
315 points[numPoints] = v00;
316 normals[numPoints] = n00;
317 texCoords[numPoints] = t00;
320 points[numPoints] = v10;
321 normals[numPoints] = n10;
322 texCoords[numPoints] = t10;
325 points[numPoints] = v01;
326 normals[numPoints] = n01;
327 texCoords[numPoints] = t01;
331 points[numPoints] = v11;
332 normals[numPoints] = n11;
333 texCoords[numPoints] = t11;
336 points[numPoints] = v01;
337 normals[numPoints] = n01;
338 texCoords[numPoints] = t01;
341 points[numPoints] = v10;
342 normals[numPoints] = n10;
343 texCoords[numPoints] = t10;
350 static unsigned _normalizeLongitude(unsigned offset)
351 { return offset - (360*8)*(offset/(360*8)); }
353 static unsigned _longitudeDegToOffset(double lon)
355 unsigned offset = (unsigned)(8*(lon + 180) + 0.5);
356 return _normalizeLongitude(offset);
358 static double _offsetToLongitudeDeg(unsigned offset)
359 { return offset*0.125 - 180; }
361 static unsigned _latitudeDegToOffset(double lat)
365 unsigned offset = (unsigned)(8*(lat + 90) + 0.5);
370 static double _offsetToLatitudeDeg(unsigned offset)
371 { return offset*0.125 - 90; }
373 static unsigned _degToSize(double deg)
377 return (unsigned)(8*deg + 0.5);
379 static double _sizeToDeg(unsigned size)
380 { return size*0.125; }
382 static unsigned _bucketSpanAtOffset(unsigned offset)
383 { return (unsigned)(8*sg_bucket_span(_offsetToLatitudeDeg(offset) + 0.0625) + 0.5); }
385 static SGGeod _offsetToGeod(unsigned offset0, unsigned offset1, double elev)
386 { return SGGeod::fromDegM(_offsetToLongitudeDeg(offset0), _offsetToLatitudeDeg(offset1), elev); }
388 static unsigned _getLevel(const unsigned factors[], unsigned nFactors, unsigned begin, unsigned end)
390 unsigned rbegin = end - 1;
391 for (; 0 < nFactors;) {
395 begin /= factors[nFactors];
396 rbegin /= factors[nFactors];
402 static BucketBox _intersection(const BucketBox& box0, const BucketBox& box1)
405 for (unsigned i = 0; i < 2; ++i) {
406 box._offset[i] = std::max(box0._offset[i], box1._offset[i]);
407 unsigned m = std::min(box0._offset[i] + box0._size[i], box1._offset[i] + box1._size[i]);
408 if (m <= box._offset[i])
411 box._size[i] = m - box._offset[i];
414 box._offset[0] = _normalizeLongitude(box._offset[0]);
424 operator==(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
426 if (bucketBox0.getOffset(0) != bucketBox1.getOffset(0))
428 if (bucketBox0.getOffset(1) != bucketBox1.getOffset(1))
430 if (bucketBox0.getSize(0) != bucketBox1.getSize(0))
432 if (bucketBox0.getSize(1) != bucketBox1.getSize(1))
438 operator!=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
439 { return !operator==(bucketBox0, bucketBox1); }
442 operator<(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
444 if (bucketBox0.getOffset(0) < bucketBox1.getOffset(0)) return true;
445 else if (bucketBox1.getOffset(0) < bucketBox0.getOffset(0)) return false;
446 else if (bucketBox0.getOffset(1) < bucketBox1.getOffset(1)) return true;
447 else if (bucketBox1.getOffset(1) < bucketBox0.getOffset(1)) return false;
448 else if (bucketBox0.getSize(0) < bucketBox1.getSize(0)) return true;
449 else if (bucketBox1.getSize(0) < bucketBox0.getSize(0)) return false;
450 else return bucketBox0.getSize(1) < bucketBox1.getSize(1);
454 operator>(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
455 { return operator<(bucketBox1, bucketBox0); }
458 operator<=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
459 { return !operator>(bucketBox0, bucketBox1); }
462 operator>=(const BucketBox& bucketBox0, const BucketBox& bucketBox1)
463 { return !operator<(bucketBox0, bucketBox1); }
465 /// Stream output operator.
466 /// Note that this is not only used for pretty printing but also for
467 /// generating the meta file names for on demand paging.
468 /// So, don't modify unless you know where else this is used.
469 template<typename char_type, typename traits_type>
470 std::basic_ostream<char_type, traits_type>&
471 operator<<(std::basic_ostream<char_type, traits_type>& os, const BucketBox& bucketBox)
473 double lon = bucketBox.getLongitudeDeg();
479 double lat = bucketBox.getLatitudeDeg();
489 os << "-" << bucketBox.getWidthDeg() << "x" << bucketBox.getHeightDeg();
493 /// Stream inout operator.
494 /// Note that this is used for reading the meta file names for on demand paging.
495 /// So, don't modify unless you know where else this is used.
496 template<typename char_type, typename traits_type>
497 std::basic_istream<char_type, traits_type>&
498 operator>>(std::basic_istream<char_type, traits_type>& is, BucketBox& bucketBox)
511 is.setstate(std::ios::failbit | is.rdstate());
518 is.setstate(std::ios::failbit | is.rdstate());
521 bucketBox.setLongitudeDeg(sign*num);
533 is.setstate(std::ios::failbit | is.rdstate());
540 bucketBox.setLatitudeDeg(sign*num);
546 is.setstate(std::ios::failbit | is.rdstate());
553 bucketBox.setWidthDeg(SGMiscd::min(num, 360));
559 is.setstate(std::ios::failbit | is.rdstate());
566 bucketBox.setHeightDeg(SGMiscd::min(num, 90 - bucketBox.getLatitudeDeg()));
571 } // namespace simgear