File: createTIN.cpp

package info (click to toggle)
libterralib 4.3.0%2Bdfsg.2-14
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 61,564 kB
  • sloc: cpp: 225,052; ansic: 31,562; makefile: 807; sh: 80; xml: 37
file content (173 lines) | stat: -rw-r--r-- 4,667 bytes parent folder | download | duplicates (8)
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
// TestAkima.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>

#include "TeTin.h"
#include "TeAsciiFile.h"
#include "TeGeometryAlgorithms.h"
#include "TeRaster.h"
#include "TeDefines.h"

//Read samples file in ASCII Spring format
void readSamplesFromSPRFile(TeSampleSet& sampleSet, const std::string& fileName)
{
	TeAsciiFile	pFile (fileName);

	while (pFile.isNotAtEOF())
	{
		std::string geoId;
		std::string repType = pFile.readString ();
		pFile.findNewLine();
		if (repType == "POINT3D")
		{
			geoId = pFile.readString ();
			while ( pFile.isNotAtEOF() && geoId != "END" )
			{
				double x = atof ( geoId.c_str() );
				geoId = pFile.readString ();
				if ( pFile.isNotAtEOF() && geoId != "END" )
				{
					double y = atof ( geoId.c_str() );
					geoId = pFile.readString ();
					if ( pFile.isNotAtEOF() && geoId != "END" )
					{
						double z = atof ( geoId.c_str() );
						sampleSet.add ( TeSample(TeCoord2D(x, y), z) );
						geoId = pFile.readString ();
					}
				}
			}
		}
	}
}

//Read contour lines file in ASCII Spring format
void readContoursFromSPRFile (TeContourLineSet& contourSet, const std::string& fileName)
{
	TeAsciiFile	pFile (fileName);
	//cout.precision(6);
	while (pFile.isNotAtEOF())
	{
		std::string geoId;
		std::string repType = pFile.readString ();
		pFile.findNewLine();
		if (repType == "LINE3D")
		{
			geoId = pFile.readString ();
			if (geoId == "HEIGHT")
			{
				geoId = pFile.readString ();
                double z = atof ( geoId.c_str() );
				//cout << z << endl;
				TeLine2D line;
				geoId = pFile.readString ();
				while ( pFile.isNotAtEOF() && geoId != "END" )
				{
					double x = atof ( geoId.c_str() );
					geoId = pFile.readString ();
					if ( pFile.isNotAtEOF() && geoId != "END" )
					{
						double y = atof ( geoId.c_str() );
						line.add ( TeCoord2D(x, y) );
						//cout << x << " " << y << endl;
						geoId = pFile.readString ();
					}
				}
				//TeLineSimplify(TeLine2D& ptlist, double snap, double maxdist)
//				TeLineSimplify(line, 10, 50);
				TeContourLine cLine (line, z);
				contourSet.add(cLine);
			}
		}
	}
}

void writeTriangleEdgesSPRFile (const std::string& name, TeTin& tin)
{
	fstream sprcfile;
	sprcfile.open ( name.c_str(), ios::out );
	sprcfile << "LINES" << "\n";
	sprcfile << "INFO" << "\n";
	sprcfile << "UNITS Metros" << "\n";
	sprcfile << "INFO_END" << "\n";
	sprcfile.precision(10);

	TeTin::TeTinTriangleIterator ti;
	for (ti = tin.triangleBegin(); ti != tin.triangleEnd(); ti++)
	{
		TeCoord2D pt[3];
		tin.trianglePoints(*ti, pt[0], pt[1], pt[2]);
		sprcfile << pt[0].x() << " " << pt[0].y() << " " << "\n";
		sprcfile << pt[1].x() << " " << pt[1].y() << " " << "\n";
		sprcfile << pt[2].x() << " " << pt[2].y() << " " << "\n";
		sprcfile << pt[0].x() << " " << pt[0].y() << " " << "\n";
		sprcfile << "END" << "\n";
	}
	sprcfile << "END" << "\n";
	sprcfile.close ();
}

int main(int argc, char* argv[])
{
//	1. Read samples
//	1.1 Read XYZ samples from SPRING file
	TeSampleSet samples;
	readSamplesFromSPRFile( samples, "..\\data\\samples.spr" );

//	1.2 Read cntour lines from SPRING file
	TeContourLineSet contours;
	readContoursFromSPRFile( contours, "..\\data\\samples.spr"  );

//	2. Create Original TIN
	TeBox tinBox( samples.box() );
	updateBox(tinBox, contours.box() );

	TeTin tt;
	tt.createInitialTriangles (tinBox);

//	2.1. Set precision used for building TIN
	double tol = TePrecision::instance().precision(); // Save old precision
	double tinPrecision = tinBox.width() / 1.e6; // 1.e6th of deltaX
	TePrecision::instance().setPrecision(tinPrecision);

//	2.2. Insert samples in TIN
	TeSampleSet::iterator si;
	for (si = samples.begin(); si != samples.end(); si++)
	{
		double x = (*si).location().x();
		double y = (*si).location().y();
		double z = (*si).value();
		tt.insertPoint (x, y, z);
	}

	TeContourLineSet::iterator ci;
	for (ci = contours.begin(); ci != contours.end(); ci++)
	{
		TeContourLine line ( *ci );

		//TeLineSimplify(TeLine2D& ptlist, double snap, double maxdist)
		TeLineSimplify(line, tinPrecision*400., tinPrecision*2000);

		double z = (*ci).value();
		TeContourLine::iterator li;
		for (li = line.begin(); li != line.end(); li++)
		{
			double x = (*li).x();
			double y = (*li).y();
			tt.insertPoint (x, y, z);
		}
	}


//	2.3. Fix edge triangles TIN
	tt.convexize();

	writeTriangleEdgesSPRFile (string("..\\data\\tin.spr" ), tt);

	TePrecision::instance().setPrecision(tol);

	return 0;
}