summaryrefslogtreecommitdiffstats
path: root/kstars/kstars/ksplanetbase.cpp
blob: 87f5e004fe897856e2274fd103718eea2e204b68 (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
/***************************************************************************
                          ksplanetbase.cpp  -  Trinity Desktop Planetarium
                             -------------------
    begin                : Sun Jul 22 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 <math.h>

#include <tqfile.h>
#include <tqpoint.h>
#include <tqwmatrix.h>

#include "ksplanetbase.h"
#include "ksplanet.h"
#include "kstarsdata.h"
#include "ksutils.h"
#include "ksnumbers.h"
#include "kspopupmenu.h"


KSPlanetBase::KSPlanetBase( KStarsData *kd, TQString s, TQString image_file, double pSize )
 : SkyObject( 2, 0.0, 0.0, 0.0, s, "" ), Rearth(0.0), Image(0), data(kd) {

	 if (! image_file.isEmpty()) {
		TQFile imFile;

		if ( KSUtils::openDataFile( imFile, image_file ) ) {
			imFile.close();
			Image0.load( imFile.name() );
			Image = Image0.convertDepth( 32 );
			Image0 = Image;
		}
	}
	PositionAngle = 0.0;
	ImageAngle = 0.0;
	PhysicalSize = pSize;
	Trail.setAutoDelete( TRUE );
}

void KSPlanetBase::EquatorialToEcliptic( const dms *Obliquity ) {
	findEcliptic( Obliquity, ep.longitude, ep.latitude );
}

void KSPlanetBase::EclipticToEquatorial( const dms *Obliquity ) {
	setFromEcliptic( Obliquity, &ep.longitude, &ep.latitude );
}

void KSPlanetBase::updateCoords( KSNumbers *num, bool includePlanets, const dms *lat, const dms *LST ){
	if ( includePlanets ) {
		data->earth()->findPosition( num ); //since we don't pass lat & LST, localizeCoords will be skipped

		if ( lat && LST ) {
			findPosition( num, lat, LST, data->earth() );
			if ( hasTrail() ) Trail.removeLast();
		} else {
			findGeocentricPosition( num, data->earth() );
		}
	}
}

void KSPlanetBase::findPosition( const KSNumbers *num, const dms *lat, const dms *LST, const KSPlanetBase *Earth ) {
	findGeocentricPosition( num, Earth );  //private function, reimplemented in each subclass

	if ( Earth ) setRearth( Earth );

	if ( lat && LST )
		localizeCoords( num, lat, LST ); //correct for figure-of-the-Earth

	if ( hasTrail() ) {
		Trail.append( new SkyPoint( ra(), dec() ) );
		if ( Trail.count() > MAXTRAIL ) Trail.removeFirst();
	}

	if ( isMajorPlanet() )
		findMagnitude(num);
}

bool KSPlanetBase::isMajorPlanet() const {
	if ( name() == "Mercury" || name() == "Venus" || name() == "Mars" ||
				name() == "Jupiter" || name() == "Saturn" || name() == "Uranus" ||
				name() == "Neptune" || name() == "Pluto" )
		return true;
	return false;
}

void KSPlanetBase::localizeCoords( const KSNumbers *num, const dms *lat, const dms *LST ) {
	//convert geocentric coordinates to local apparent coordinates (topocentric coordinates)
	dms HA, HA2; //Hour Angle, before and after correction
	double rsinp, rcosp, u, sinHA, cosHA, sinDec, cosDec, D;
	double cosHA2;
	double r = Rearth * AU_KM; //distance from Earth, in km
	u = atan( 0.996647*tan( lat->radians() ) );
	rsinp = 0.996647*sin( u );
	rcosp = cos( u );
	HA.setD( LST->Degrees() - ra()->Degrees() );
	HA.SinCos( sinHA, cosHA );
	dec()->SinCos( sinDec, cosDec );

	D = atan( ( rcosp*sinHA )/( r*cosDec/6378.14 - rcosp*cosHA ) );
	dms temp;
	temp.setRadians( ra()->radians() - D );
	setRA( temp );

	HA2.setD( LST->Degrees() - ra()->Degrees() );
	cosHA2 = cos( HA2.radians() );
	temp.setRadians( atan( cosHA2*( r*sinDec/6378.14 - rsinp )/( r*cosDec*cosHA/6378.14 - rcosp ) ) );
	setDec( temp );

	EquatorialToEcliptic( num->obliquity() );
}

void KSPlanetBase::setRearth( const KSPlanetBase *Earth ) {
	double sinL, sinB, sinL0, sinB0;
	double cosL, cosB, cosL0, cosB0;
	double x,y,z;

	//The Moon's Rearth is set in its findGeocentricPosition()...
	if ( name() == "Moon" ) {
		return;
	}

	if ( name() == "Earth" ) {
		Rearth = 0.0;
		return;
	}

	if ( ! Earth && name() != "Moon" ) {
		kdDebug() << i18n( "KSPlanetBase::setRearth():  Error: Need an Earth pointer.  (" ) << name() << ")" << endl;
		Rearth = 1.0;
		return;
	}

	Earth->ecLong()->SinCos( sinL0, cosL0 );
	Earth->ecLat()->SinCos( sinB0, cosB0 );
	double eX = Earth->rsun()*cosB0*cosL0;
	double eY = Earth->rsun()*cosB0*sinL0;
	double eZ = Earth->rsun()*sinB0;

	helEcLong()->SinCos( sinL, cosL );
	helEcLat()->SinCos( sinB, cosB );
	x = rsun()*cosB*cosL - eX;
	y = rsun()*cosB*sinL - eY;
	z = rsun()*sinB - eZ;

	Rearth = sqrt(x*x + y*y + z*z);

	//Set angular size, in arcmin
	AngularSize = asin(PhysicalSize/Rearth/AU_KM)*60.*180./dms::PI;
}

void KSPlanetBase::updateTrail( dms *LST, const dms *lat ) {
	for ( SkyPoint *sp = Trail.first(); sp; sp = Trail.next() )
		sp->EquatorialToHorizontal( LST, lat );
}

void KSPlanetBase::findPA( const KSNumbers *num ) {
	//Determine position angle of planet (assuming that it is aligned with
	//the Ecliptic, which is only roughly correct).
	//Displace a point along +Ecliptic Latitude by 1 degree
	SkyPoint test;
	dms newELat( ecLat()->Degrees() + 1.0 );
	test.setFromEcliptic( num->obliquity(), ecLong(), &newELat );
	double dx = test.ra()->Degrees() - ra()->Degrees();
	double dy = dec()->Degrees() - test.dec()->Degrees();
	double pa;
	if ( dy ) {
		pa = atan( dx/dy )*180.0/dms::PI;
	} else {
		pa = 90.0;
		if ( dx > 0 ) pa = -90.0;
	}
	setPA( pa );
}

void KSPlanetBase::rotateImage( double imAngle ) {
	ImageAngle = imAngle;
	TQWMatrix m;
	m.rotate( ImageAngle );
	Image = Image0.xForm( m );
}

void KSPlanetBase::scaleRotateImage( int scale, double imAngle ) {
	ImageAngle = imAngle;
	TQWMatrix m;
	m.rotate( ImageAngle );
	Image = Image0.xForm( m ).smoothScale( scale, scale );
}

void KSPlanetBase::findMagnitude(const KSNumbers *num) {
	
	double cosDec, sinDec;
	dec()->SinCos(cosDec, sinDec);
	
	/* Phase of the planet in degrees */
	double earthSun = 1.;
	double cosPhase = (rsun()*rsun() + rearth()*rearth() - earthSun*earthSun) 
	  / (2 * rsun() * rearth() );   
	double phase = acos ( cosPhase ) * 180.0 / dms::PI;

	/* Computation of the visual magnitude (V band) of the planet.
	* Algorithm provided by Pere Planesas (Observatorio Astronomico Nacional)
	* It has some simmilarity to J. Meeus algorithm in Astronomical Algorithms, Chapter 40.
	* */

	// Initialized to the faintest magnitude observable with the HST
	float magnitude = 30;

	double param = 5 * log10(rsun() * rearth() );
	double f1 = phase/100.;

	if ( name() == "Mercury" ) {
		if ( phase > 150. ) f1 = 1.5; 
		magnitude = -0.36 + param + 3.8*f1 - 2.73*f1*f1 + 2*f1*f1*f1;
	}
	if ( name() =="Venus")
		magnitude = -4.29 + param + 0.09*f1 + 2.39*f1*f1 - 0.65*f1*f1*f1;
	if( name() == "Mars")
		magnitude = -1.52 + param + 0.016*phase; 
	if( name() == "Jupiter") 
		magnitude = -9.25 + param + 0.005*phase;  

	if( name() == "Saturn") {
		double T = num->julianCenturies();
		double a0 = (40.66-4.695*T)* dms::PI / 180.;
		double d0 = (83.52+0.403*T)* dms::PI / 180.;
		double sinx = -cos(d0)*cosDec*cos(a0 - ra()->radians());
		sinx = fabs(sinx-sin(d0)*sinDec);
		double rings = -2.6*sinx + 1.25*sinx*sinx;
		magnitude = -8.88 + param + 0.044*phase + rings;  
	}

	if( name() == "Uranus")
		magnitude = -7.19 + param + 0.0028*phase;  
	if( name() == "Neptune")
		magnitude = -6.87 + param;
	if( name() == "Pluto" )
		magnitude = -1.01 + param + 0.041*phase;

	setMag(magnitude);
}