summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/dms.cpp
blob: 3619a578bbfb6cfcd8a1d17addefc7112a22f611 (plain)
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
/***************************************************************************
                          dms.cpp  -  K Desktop Planetarium
                             -------------------
    begin                : Sun Feb 11 2001
    copyright            : (C) 2001 by Jason Harris
    email                : jharris@30doradus.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>

#include "dms.h"
#include <kglobal.h>
#include <klocale.h>
#include <tqregexp.h>

void dms::setD( const double &x ) {
	D = x;
	scDirty = true;
	rDirty = true;
}

void dms::setD(const int &d, const int &m, const int &s, const int &ms) {
	D = (double)abs(d) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.;
	if (d<0) {D = -1.0*D;}
	scDirty = true;
	rDirty = true;
}

void dms::setH( const double &x ) {
  setD( x*15.0 );
}

void dms::setH(const int &h, const int &m, const int &s, const int &ms) {
  D = 15.0*((double)abs(h) + ((double)m + ((double)s + (double)ms/1000.)/60.)/60.);
  if (h<0) {D = -1.0*D;}
	scDirty = true;
	rDirty = true;
}

void dms::setRadians( const double &Rad ) {
	setD( Rad/DegToRad );
	Radians = Rad;
}

bool dms::setFromString( const TQString &str, bool isDeg ) {
	int d(0), m(0);
	double s(0.0);
	bool checkValue( false ), badEntry( false ), negative( false );
	TQString entry = str.stripWhiteSpace();

	//remove any instances of unit characters.
	//h, d, m, s, ', ", or the degree symbol (ASCII 176)
	entry.replace( TQRegExp("h"), "" );
	entry.replace( TQRegExp("d"), "" );
	entry.replace( TQRegExp("m"), "" );
	entry.replace( TQRegExp("s"), "" );
	TQString sdeg;
	sdeg.sprintf("%c", 176);
	entry.replace( TQRegExp(sdeg), "" );
	entry.replace( TQRegExp("\'"), "" );
	entry.replace( TQRegExp("\""), "" );

	//Account for localized decimal-point settings
	//TQString::toDouble() requires that the decimal symbol is "."
	entry.replace( KGlobal::locale()->decimalSymbol(), "." );
 
	//empty entry returns false
	if ( entry.isEmpty() ) {
		setD( 0.0 );
		return false;
	}

	//try parsing a simple integer
	d = entry.toInt( &checkValue );
	if ( checkValue ) {
		if (isDeg) setD( d, 0, 0 );
		else setH( d, 0, 0 );
		return true;
	}

	//try parsing a simple double
	double x = entry.toDouble( &checkValue );
	if ( checkValue ) {
		if ( isDeg ) setD( x );
		else setH( x );
		return true;
	}

	//try parsing multiple fields.
	TQStringList fields;

	//check for colon-delimiters or space-delimiters
	if ( entry.contains(':') )
		fields = TQStringList::split( ':', entry );
	else fields = TQStringList::split( " ", entry );

	//anything with one field is invalid!
	if ( fields.count() == 1 ) {
		setD(0.0);
		return false;
	}

	//If two fields we will add a third one, and then parse with
	//the 3-field code block. If field[1] is an int, add a third field equal to "0".
	//If field[1] is a double, convert it to integer arcmin, and convert
	//the remainder to integer arcsec
	//If field[1] is neither int nor double, return false.
	if ( fields.count() == 2 ) {
		m = fields[1].toInt( &checkValue );
		if ( checkValue ) fields.append( TQString( "0" ) );
		else {
			double mx = fields[1].toDouble( &checkValue );
			if ( checkValue ) {
				fields[1] = TQString("%1").tqarg( int(mx) );
				fields.append( TQString("%1").tqarg( int( 60.0*(mx - int(mx)) ) ) );
			} else {
				setD( 0.0 );
				return false;
			}
		}
	}

	//Now have (at least) three fields ( h/d m s );
	//we can ignore anything after 3rd field
	if ( fields.count() >= 3 ) {
		//See if first two fields parse as integers, and third field as a double
		
		d = fields[0].toInt( &checkValue );
		if ( !checkValue ) badEntry = true;
		m = fields[1].toInt( &checkValue );
		if ( !checkValue ) badEntry = true;
		s = fields[2].toDouble( &checkValue );
		if ( !checkValue ) badEntry = true;
		
		//Special case: If first field is "-0", store the negative sign.  
		//(otherwise it gets dropped)
		if ( fields[0].at(0) == '-' && d == 0 ) negative = true;
	}

	if ( !badEntry ) {
		double D = (double)abs(d) + (double)abs(m)/60.
				+ (double)fabs(s)/3600.;

		if ( negative || d<0 || m < 0 || s<0 ) { D = -1.0*D;}

		if (isDeg) {
			setD( D );
		} else {
			setH( D );
		}
	} else {
		setD( 0.0 );
		return false;
	}

	return true;
}

const int dms::arcmin( void ) const {
	int am = int( float( 60.0*( fabs(D) - abs( degree() ) ) ) );
	if ( D<0.0 && D>-1.0 ) { //angle less than zero, but greater than -1.0
		am = -1*am; //make minute negative
	}
	return am;
}

const int dms::arcsec( void ) const {
	int as = int( float( 60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) ) );
	//If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
	if ( degree()==0 && arcmin()==0 && D<0.0 ) {
		as = -1*as;
	}
	return as;
}

const int dms::marcsec( void ) const {
	int as = int( float( 1000.0*(60.0*( 60.0*( fabs(D) - abs( degree() ) ) - abs( arcmin() ) ) - abs( arcsec() ) ) ) );
	//If the angle is slightly less than 0.0, give ArcSec a neg. sgn.
	if ( degree()==0 && arcmin()==0 && arcsec()== 0 && D<0.0 ) {
		as = -1*as;
	}
	return as;
}

const int dms::minute( void ) const {
	int hm = int( float( 60.0*( fabs( Hours() ) - abs( hour() ) ) ) );
	if ( Hours()<0.0 && Hours()>-1.0 ) { //angle less than zero, but greater than -1.0
		hm = -1*hm; //make minute negative
	}
	return hm;
}

const int dms::second( void ) const {
	int hs = int( float( 60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) ) );
	if ( hour()==0 && minute()==0 && Hours()<0.0 ) {
		hs = -1*hs;
	}
	return hs;
}

const int dms::msecond( void ) const {
	int hs = int( float( 1000.0*(60.0*( 60.0*( fabs( Hours() ) - abs( hour() ) ) - abs( minute() ) ) - abs( second() ) ) ) );
	if ( hour()==0 && minute()==0 && second()==0 && Hours()<0.0 ) {
		hs = -1*hs;
	}
	return hs;
}

const double& dms::sin( void ) const {
	if ( scDirty ) {
		double s,c;
		SinCos( s, c );
	}
	
	return Sin;
}

const double& dms::cos( void ) const {
	if ( scDirty ) {
		double s,c;
		SinCos( s, c );
	}
	
	return Cos;
}

void dms::SinCos( double &sina, double &cosa ) const {
	/**We have two versions of this function.  One is ANSI standard, but
		*slower.  The other is faster, but is GNU only.
		*Using the __GLIBC_ and __GLIBC_MINOR_ constants to determine if the
		* GNU extension sincos() is defined.
		*/

	if ( scDirty ) {
                #ifdef __GLIBC__ 
                #if ( __GLIBC__ >= 2 && __GLIBC_MINOR__ >=1 && !defined(__UCLIBC__))
		//GNU version
		sincos( radians(), &Sin, &Cos );
                #else
		//For older GLIBC versions
	        Sin = ::sin( radians() );
		Cos = ::cos( radians() );
		#endif
		#else
		//ANSI-compliant version
		Sin = ::sin( radians() );
		Cos = ::cos( radians() );
		#endif
		scDirty = false;
	}
	sina = Sin;
	cosa = Cos;
}

const double& dms::radians( void ) const {
	if ( rDirty ) {
		Radians = D*DegToRad;
		rDirty = false;
	}

	return Radians;
}

const dms dms::reduce( void ) const {
	double a = D;
	while (a<0.0) {a += 360.0;}
	while (a>=360.0) {a -= 360.0;}
	return dms( a );
}

const TQString dms::toDMSString(bool forceSign) const {
	TQString dummy;
	char pm(' ');
	int dd = abs(degree());
	int dm = abs(arcmin());
	int ds = abs(arcsec());

	if ( Degrees() < 0.0 ) pm = '-';
	else if (forceSign && Degrees() > 0.0 ) pm = '+';

	TQString format( "%c%3d%c %02d\' %02d\"" );
	if ( dd < 100 ) format = "%c%2d%c %02d\' %02d\"";
	if ( dd < 10  ) format = "%c%1d%c %02d\' %02d\"";

	return dummy.sprintf(format.local8Bit(), pm, dd, 176, dm, ds);
}

const TQString dms::toHMSString() const {
	TQString dummy;
	return dummy.sprintf("%02dh %02dm %02ds", hour(), minute(), second());
}

dms dms::fromString(TQString & st, bool deg) {
	dms result;
	bool ok( false );

	ok = result.setFromString( st, deg );

//	if ( ok )
		return result;
//	else {
//		kdDebug() << i18n( "Could Not Set Angle from string: " ) << st << endl;
//		return result;
//	}
}

// M_PI is defined in math.h
const double dms::PI = M_PI;
const double dms::DegToRad = PI/180.0;