-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdataGenerate.cpp
372 lines (314 loc) · 10 KB
/
dataGenerate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "stdafx.h"
#include "dataGenerate.h"
#include "gdal_alg.h"
#include <ogr_spatialref.h>
#include <gdalwarper.h>
#include "gdal.h"
#include "cpl_conv.h"
#include "gdal_priv.h"
#include<osg/Node>
#include<osg/Geode>
#include<osg/Group>
#include<osgDB/ReadFile>
#include<osgDB/WriteFile>
#include<osg/PolygonMode>
#include<osg/MatrixTransform>
#include<osg/ComputeBoundsVisitor>
#include<osg/shapeDrawable>
#include<osg/Texture2D>
#include<osg/TexGen>
#include<osg/TexEnv>
#include <osgUtil/DelaunayTriangulator>
#include<osgViewer/Viewer>
#include<osg/LineWidth>
#include<osg/CullFace>
#include <regex>
#include <algorithm>
#include <osgDB/WriteFile>
using namespace std;
using namespace osg;
using namespace osgDB;
void CDataConvertor::startProduce()
{
startProcess();
}
void CDataConvertor::startProcess()
{
_beginthread(&produce, 0, this);
}
void CDataConvertor::produce(void* ptr)
{
CDataConvertor* convertor = (CDataConvertor*)ptr;
convertor->produceDsmDomOsgb();
}
int CDataConvertor::produceDsmDomOsgb(/*string inputFilePath, string strDomTitle, string strDsmTitle, int xPart, int yPart*/)
{
std::string inputFilePath = produceDlg->filepath;
std::string strDomTitle = produceDlg->domfile;
std::string strDsmTitle = produceDlg->demfile;
int xPart = produceDlg->xPart;
int yPart = produceDlg->yPart;
produceDlg->m_progress.SetRange(0, 1000 * xPart * yPart + 1);
produceDlg->m_progress.SetPos(1);
produceDlg->m_progress.ShowWindow(SW_SHOW);
////获取影像
GDALAllRegister();
if (strDomTitle.size() == 0)
{
AfxMessageBox("Tiff影像路径错误!");
produceDlg->m_progress.ShowWindow(SW_HIDE);
return 0;
}
std::string imageFileName = Tiff2Jpg(inputFilePath, strDomTitle);
std::string domFileName = inputFilePath + strDomTitle;
GDALDataset* poDataSet = (GDALDataset *)GDALOpen(domFileName.c_str(), GA_ReadOnly);
if (poDataSet == nullptr)
{
AfxMessageBox("Tiff影像打开失败!");
produceDlg->m_progress.ShowWindow(SW_HIDE);
return 0;
}
int wDom = poDataSet->GetRasterXSize();
int hDom = poDataSet->GetRasterYSize();
int cntDom = poDataSet->GetRasterCount();
int totalSize = wDom * hDom * cntDom;
unsigned char* pData = new unsigned char[totalSize];
memset(pData, 0, sizeof(unsigned char)* totalSize);
poDataSet->RasterIO(GF_Read, 0, 0, wDom, hDom, pData, wDom, hDom, GDT_Byte, cntDom, NULL, 0, 0, 0);
osg::ref_ptr<osg::Image> image = readImageFile(imageFileName);
if (!image)
{
AfxMessageBox("JPG影像打开失败!");
produceDlg->m_progress.ShowWindow(SW_HIDE);
return 0;
}
osgDB::writeImageFile(*image, imageFileName);
string dsmFileName = inputFilePath + strDsmTitle;
GDALDataset* dsmDataSet = (GDALDataset *)GDALOpen(dsmFileName.c_str(), GA_ReadOnly);
if (dsmDataSet == NULL)
{
AfxMessageBox("Tiff影像打开失败!");
produceDlg->m_progress.ShowWindow(SW_HIDE);
return 0;
}
int wDsm = dsmDataSet->GetRasterXSize();
int hDsm = dsmDataSet->GetRasterYSize();
int cntDsm = dsmDataSet->GetRasterCount();
double xRes = 0; double xRot = 0; double yRot = 0; double yRes = 0; double leftFull = 0; double topFull = 0;
int pos = strDsmTitle.rfind('.');
string strDsmCoord = strDsmTitle.substr(0, pos + 1) + "tfw";
string dsmCoordName = inputFilePath + strDsmCoord;
FILE* fpDsm = fopen(dsmCoordName.c_str(), "r");
if (!fpDsm)
{
AfxMessageBox(_T("未发现dem的tfw文件,生成失败"));
produceDlg->m_progress.ShowWindow(SW_HIDE);
GDALClose(dsmDataSet);
return 0;
}
fscanf(fpDsm, "%lf\n%lf\n%lf\n%lf\n%lf\n%lf\n", &xRes, &xRot, &yRot, &yRes, &leftFull, &topFull);
fclose(fpDsm);
//纹理坐标
double xDRes = 0; double xDRot = 0; double yDRot = 0; double yDRes = 0; double dLeftFull = 0; double dTopFull = 0;
pos = strDomTitle.rfind('.');
string strDomCoord = strDomTitle.substr(0, pos + 1) + "tfw";
string domCoordName = inputFilePath + strDomCoord;
FILE* fpDom = fopen(domCoordName.c_str(), "r");
if (!fpDom)
{
AfxMessageBox("未发现dom的tfw文件,生成失败");
produceDlg->m_progress.ShowWindow(SW_HIDE);
GDALClose(dsmDataSet);
return 0;
}
fscanf(fpDom, "%lf\n%lf\n%lf\n%lf\n%lf\n%lf\n", &xDRes, &xDRot, &yDRot, &yDRes, &dLeftFull, &dTopFull);
fclose(fpDom);
produceDlg->GetDlgItem(IDOK)->EnableWindow(FALSE);
int wsPart = wDsm / xPart + 1;
int hsPart = hDsm / yPart + 1;
int percent = 1;
for (int nY = 0; nY < yPart; nY++)
{
for (int nX = 0; nX < xPart; nX++)
{
percent = (nY * yPart + nX) * 1000 + 1;
produceDlg->m_progress.SetPos(percent);
wsPart = wDsm / xPart + 1;
hsPart = hDsm / yPart + 1;
if (nY == (yPart - 1))
{
hsPart = hsPart - 1 + (hDsm % (hsPart - 1));
}
if (nX == (xPart - 1))
{
wsPart = wsPart - 1 + (wDsm % (wsPart - 1));
}
ref_ptr<Geode> geode = new Geode;
int dsmSize = wsPart * hsPart * cntDsm;
double* dsmData = new double[dsmSize];
memset(dsmData, 0, dsmSize * sizeof(double));
dsmDataSet->RasterIO(GF_Read, nX * int(wDsm / xPart), nY * int(hDsm / yPart), wsPart, hsPart, dsmData, wsPart, hsPart, GDT_Float64, cntDsm, NULL, 0, 0, 0);
pos = strDomTitle.rfind('.');
stringstream ss;
ss << (nY * xPart + nX);
double left = leftFull + nX * int(wDsm / xPart) * xRes;
double top = topFull + nY * int(hDsm / yPart) * yRes;
double dLeft = dLeftFull;
double dTop = dTopFull;
double xTrans = left;
double yTrans = top;
dLeft = dLeft - left;
dTop = dTop - top;
double left1 = left;
double top1 = top;
left = 0;
top = 0;
ref_ptr<Vec3Array> coord = new Vec3Array;
for (size_t i = 0; i < hsPart; i++)
{
for (size_t j = 0; j < wsPart; j++)
{
double h = dsmData[i * wsPart + j];
if (h < -100) h = 0;
if (h != 0)
{
//计算三维坐标
double x = left + j * xRes;
double y = top + i * yRes;
coord->push_back(Vec3d(x, y, h));
}
}
}
ref_ptr<Geometry> geom = new Geometry;
geom->setVertexArray(coord);
geode->addDrawable(geom);
//创建Delaunay三角网对象
ref_ptr<osgUtil::DelaunayTriangulator> dt = new osgUtil::DelaunayTriangulator(coord.get());
dt->setInputPointArray(coord);
//10%
produceDlg->m_progress.SetPos(percent+100);
//生成三角网
dt->triangulate();
//90%
produceDlg->m_progress.SetPos(percent+900);
geom->addPrimitiveSet(dt->getTriangles());
ref_ptr<Vec2Array> textCoord = new Vec2Array;
ref_ptr<Vec3Array> verts = dynamic_cast<Vec3Array*> (geom->getVertexArray());
for (int i = 0; i < verts->size(); i++)
{
double x = verts->at(i).x();
double y = verts->at(i).y();
//计算二维坐标
double xCoord = (x - dLeft) / xDRes;
double yCoord = hDom - (y - dTop) / yDRes;
double xRatio = xCoord / wDom;
double yRatio = yCoord / hDom;
textCoord->push_back(Vec2d(xRatio, yRatio));
}
image->setFileName(imageFileName);
ref_ptr<Texture2D> texture = new Texture2D();
int w = image->s();
int h = image->t();
texture->setTextureSize(w, h);
texture->setInternalFormat(GL_RGBA);
texture->setImage(image.get());
geom->setTexCoordArray(0, textCoord.get());
//样式设置
ref_ptr<StateSet> state = new StateSet;
state->setTextureAttributeAndModes(0, texture.get(), StateAttribute::ON);
geom->setStateSet(state.get());
ref_ptr<Options> spOptions = new Options;
spOptions->setPluginStringData("WriteImageHint", "IncludeData");
osg::Vec3d p = geode->getBound().center();
ref_ptr<MatrixTransform> trans = new MatrixTransform;
osg::Matrix mat;
mat.setTrans(osg::Vec3d(xTrans, yTrans, 0));
trans->setMatrix(mat);
trans->addChild(geode);
;
std::string modelNameEx = inputFilePath + "domDemModel" + ss.str() + ".osg";
osgDB::writeNodeFile(*trans, modelNameEx.c_str(), spOptions.get());
delete[]dsmData;
//100%
produceDlg->m_progress.SetPos(percent+999);
}
}
delete[]pData;
char msg[255];
sprintf_s(msg,"生成完毕,输出在%s下!",produceDlg->filepath);
AfxMessageBox(CString(msg));
produceDlg->GetDlgItem(IDOK)->EnableWindow(TRUE);
produceDlg->m_progress.ShowWindow(SW_HIDE);
return 1;
}
std::string CDataConvertor::Tiff2Jpg(std::string inputFile, std::string tifName)
{
string pDstImgFileName;
unsigned char* pImageData;
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
string dsmFileName = inputFile + tifName;
GDALDataset* dsmDataSet = (GDALDataset *)GDALOpen(dsmFileName.c_str(), GA_ReadOnly);
if (dsmDataSet == nullptr)
{
return "error";
}
GDALDriver *pMemDriver = NULL;
pMemDriver = GetGDALDriverManager()->GetDriverByName("MEM");
if (pMemDriver == NULL) { return "error"; }
int width = dsmDataSet->GetRasterXSize();
int height = dsmDataSet->GetRasterYSize();
int nChannels = dsmDataSet->GetRasterCount();
GDALDataset * pMemDataSet = pMemDriver->Create("", width, height, nChannels, GDT_Byte, NULL);
GDALRasterBand *pBand = NULL;
int nLineCount = width * nChannels;
int totalSize = height * nLineCount;
pImageData = new unsigned char[totalSize];
memset(pImageData, 0, sizeof(unsigned char)* totalSize);
if (nChannels == 1)
{
pBand = dsmDataSet->GetRasterBand(1);
pBand->RasterIO(GF_Read, 0, 0, width, height, pImageData, width, height, GDT_Byte, 0, 0);
}
else if (nChannels == 3)
{
for (int i = 1; i <= nChannels; ++i)
{
//GDAL内band存储顺序为RGB,需要转换为我们一般的BGR存储,即低地址->高地址为:B G R
unsigned char *pImageOffset = (pImageData + i - 1);
GDALRasterBand* pBand = dsmDataSet->GetRasterBand(nChannels - i + 1);
pBand->RasterIO(
GF_Read,
0, 0,
width, height,
pImageOffset,
width, height,
GDT_Byte,
3,
0);
}
}
unsigned char *ptr1 = (unsigned char *)pImageData;
for (int i = 1; i <= nChannels; i++)
{
pBand = pMemDataSet->GetRasterBand(nChannels - i + 1);
pBand->RasterIO(GF_Write,
0,
0,
width,
height,
ptr1 + i - 1,
width,
height,
GDT_Byte,
nChannels,
nLineCount);
}
GDALDriver *pDstDriver = NULL;
pDstDriver = (GDALDriver *)GDALGetDriverByName("JPEG");
if (pDstDriver == NULL) { return false; }
pDstImgFileName = inputFile + tifName.substr(0, tifName.find_last_of('.')) + string(".jpg");
pDstDriver->CreateCopy(pDstImgFileName.c_str(), pMemDataSet, FALSE, NULL, NULL, NULL);
GDALClose(pMemDataSet);
return pDstImgFileName;
}