1 | #ifndef __GWLODTOOLS_H__
|
---|
2 | #define __GWLODTOOLS_H__
|
---|
3 |
|
---|
4 | #include <mars_grids.h>
|
---|
5 | #include "geoworld.h"
|
---|
6 |
|
---|
7 | namespace geoworld
|
---|
8 | {
|
---|
9 | template< typename T >
|
---|
10 | class LODMagnifier
|
---|
11 | {
|
---|
12 | private:
|
---|
13 | typedef mars::MidpointDisplacementGrid< T > SeedingMPDG;
|
---|
14 | using NoiseFn = typename SeedingMPDG::BrownianFn;
|
---|
15 |
|
---|
16 | SeedingMPDG
|
---|
17 | ** _ppMPDG0,
|
---|
18 | * _pmpdg;
|
---|
19 | TileBlock< T > _block;
|
---|
20 | const mars::ScalarField< T > & _hmLowLOD;
|
---|
21 | unsigned short
|
---|
22 | _nDeltaLOD,
|
---|
23 | _nTileDim;
|
---|
24 | NoiseFn _functor;
|
---|
25 | unsigned long
|
---|
26 | _nWWidth, _nWHeight;
|
---|
27 |
|
---|
28 | unsigned int
|
---|
29 | _nSectionTilesAcross, _nSectionTilesDown;
|
---|
30 |
|
---|
31 | public:
|
---|
32 | LODMagnifier (const mars::ScalarField< T > & hmLowLOD, const unsigned short nDeltaLOD, const unsigned short nTileDim, const float fCoarseness, const unsigned long nWWidth, const unsigned long nWHeight)
|
---|
33 | :
|
---|
34 | _ppMPDG0 (new SeedingMPDG *[nWWidth / hmLowLOD.width]),
|
---|
35 | _pmpdg (SeedingMPDG::createInstance(hmLowLOD.width, hmLowLOD.height, mars::Normal)),
|
---|
36 | _hmLowLOD(hmLowLOD), _nDeltaLOD(nDeltaLOD), _functor(NoiseFn(std::min(hmLowLOD.width, hmLowLOD.height), fCoarseness)),
|
---|
37 | _nTileDim(nTileDim), _nWWidth(nWWidth), _nWHeight(nWHeight),
|
---|
38 | _nSectionTilesAcross(hmLowLOD.width / nTileDim), _nSectionTilesDown(hmLowLOD.height / nTileDim),
|
---|
39 | _block (nTileDim)
|
---|
40 | {
|
---|
41 | assert(((nTileDim - 1) & nTileDim) == 0); // Must be a power of 2
|
---|
42 | assert(_nSectionTilesAcross * nTileDim == hmLowLOD.width && _nSectionTilesDown * nTileDim == hmLowLOD.height);
|
---|
43 | assert(_nWWidth >> nDeltaLOD == hmLowLOD.width && _nWHeight >> nDeltaLOD == hmLowLOD.height);
|
---|
44 |
|
---|
45 | const unsigned int nSectionsWide = nWWidth / hmLowLOD.width;
|
---|
46 | for (unsigned int i = 0; i < nSectionsWide; ++i)
|
---|
47 | _ppMPDG0[i] = SeedingMPDG::createInstance(hmLowLOD.width, hmLowLOD.height, mars::Normal);
|
---|
48 | }
|
---|
49 | ~LODMagnifier()
|
---|
50 | {
|
---|
51 | const unsigned int nSectionsWide = _nWWidth / _hmLowLOD.width;
|
---|
52 | for (unsigned int i = 0; i < nSectionsWide; ++i)
|
---|
53 | delete _ppMPDG0[i];
|
---|
54 |
|
---|
55 | delete [] _ppMPDG0;
|
---|
56 | delete _pmpdg;
|
---|
57 | }
|
---|
58 |
|
---|
59 | void write (FileSource::MainDEM & tmap)
|
---|
60 | {
|
---|
61 | using namespace mars;
|
---|
62 |
|
---|
63 | tmap.seekp(0, 0, std::ios_base::beg);
|
---|
64 |
|
---|
65 | for (unsigned int jj = 0; jj < _nWHeight; jj += _hmLowLOD.height)
|
---|
66 | for (unsigned int ii = 0, c = 0; ii < _nWWidth; ii += _hmLowLOD.width, ++c)
|
---|
67 | {
|
---|
68 | const unsigned int nStitch = (jj > 0 ? StitchTop : 0) | (ii > 0 ? StitchLeft : 0);
|
---|
69 | SeedingMPDG * pMPDG = _ppMPDG0[c];
|
---|
70 |
|
---|
71 | if (jj > 0)
|
---|
72 | pMPDG->stitch(StitchTop);
|
---|
73 | if (c > 0)
|
---|
74 | pMPDG->stitch(StitchLeft, _ppMPDG0[c - 1]);
|
---|
75 |
|
---|
76 | pMPDG->seedFrom (_hmLowLOD, ii >> _nDeltaLOD, jj >> _nDeltaLOD, _nDeltaLOD);
|
---|
77 |
|
---|
78 | pMPDG->reset();
|
---|
79 | pMPDG->step(pMPDG->itercount - _nDeltaLOD);
|
---|
80 | pMPDG->iterations (_nDeltaLOD, _functor, (jj > 0 ? StitchTop : 0) | (ii > 0 ? StitchLeft : 0));
|
---|
81 |
|
---|
82 | tmap.seekp(ii / _nTileDim, jj / _nTileDim, std::ios::beg);
|
---|
83 |
|
---|
84 | for (unsigned int j = 0; j < _nSectionTilesDown; )
|
---|
85 | {
|
---|
86 | for (unsigned int i = 0; i < _nSectionTilesAcross; ++i)
|
---|
87 | {
|
---|
88 | _block.copyFrom(*pMPDG, i, j);
|
---|
89 | _block >> tmap;
|
---|
90 | }
|
---|
91 | if (++j < _nSectionTilesDown)
|
---|
92 | tmap.seekp(-static_cast< signed int > (_nSectionTilesAcross), 1, std::ios_base::cur);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | };
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endif
|
---|