Subversion Repositories public

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
158 andreas 1
/***************************************************************************
2
 *   Copyright (C) 2007, 2008 by Andreas Theofilu                          *
3
 *   andreas@theosys.at                                                    *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation version 3 of the License.                *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
19
 
20
#include <iostream>
21
#include "transform.h"
22
 
23
using std::cout;
24
using std::cerr;
25
using std::clog;
26
using std::endl;
27
 
28
transform::transform(double _llat, double _llon, double _rlat, double _rlon)
29
{
30
	llat = _llat;
31
	llon = _llon;
32
	rlat = _rlat;
33
	rlon = _rlon;
34
	MapLatCtr = (rlat - llat) / 2.0 + llat;
35
	MapLonCtr = (llon - rlon) / 2.0 + rlon;
36
	MapXCtr = MapYCtr = 0;
37
	xscale = yscale = 0.0;
38
	width = height = 0;
39
}
40
 
41
transform::~transform()
42
{
43
}
44
 
45
/*
46
 * Set the dimensions in pixels of the image and calculate the scaling.
47
 */
48
void transform::setDimensions(int _width, int _height)
49
{
50
	width = _width;
51
	height = _height;
52
	MapXCtr = width / 2;
53
	MapYCtr = height / 2;
54
	xscale = 1.0 / ((double)width - 1.0);
55
	yscale = -1.0 / ((double)height - 1.0);
56
	PixPerLatDeg = width / (llat - rlat);
57
	PixPerLonDeg = height / (rlon - llon);
58
}
59
 
60
/*
61
 * Set the scale of the image.
62
 * This function makes only a rudimentary check of the given values
63
 * and overwrites the previous values if no errors were found.
64
 */
65
void transform::setScale(double scx, double scy)
66
{
67
	if (scx <= 0.0 || scx > 1.0 || scy >= 0.0 || scy < -1.0)
68
	   return;
69
 
70
	xscale = scx;
71
	yscale = scy;
72
}
73
 
74
/*
75
 * Return the current scale of X andd Y dimensions
76
 */
77
void transform::getScale(double *scx, double *scy)
78
{
79
	*scx = xscale;
80
	*scy = yscale;
81
}
82
 
83
/*
84
 * Calculate the pixel position on a raster map out of the
85
 * geo coordinates given in radian.
86
 */
87
bool transform::latLonToPix(double plat, double plon, int *px, int *py)
88
{
89
int x,y;
90
double lat, lon, xdist, ydist, dist, az, pixdist;
91
 
92
	lat = plat;
93
	lon = plon;
94
	ydist = PixPerLatDeg * (lat - MapLatCtr);
95
	xdist = PixPerLonDeg * (lon - MapLonCtr);
96
	az = atan(ydist / xdist);
97
 
98
	if (xdist < 0.0)
99
	   az += M_PI;
100
 
101
	dist = (xdist * xdist) + (ydist * ydist);
102
	pixdist = sqrt(dist);
103
	x = (int)(MapXCtr + pixdist * cos(az));
104
	y = (int)(MapYCtr - pixdist * sin(az));
105
	*px = x;
106
	*py = y;
107
	return true;
108
}
109
 
110
/*
111
 * Calculate the offset inside an image. This value can be used in a
112
 * buffer as an index, for example.
113
 */
114
long transform::getOffset(double plat, double plon)
115
{
116
int x, y;
117
 
118
	latLonToPix(plat, plon, &x, &y);
119
	return ((long)width * (long)y + (long)x);
120
}