summaryrefslogtreecommitdiff
path: root/src/Calibrate.cpp
blob: 5e3aedce8b714f9a76d3b2ac0939b210f8bd6654 (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
/*
The MIT License (MIT)

Copyright (c) 2012 Juan Ramón

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "Calibrate.hpp"
#include <QMediaPlayer>
#include <QCompleter>
#include <QFileDialog>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include <QObject>
#include <QDebug>
#include <QString>
#include <QTime>
#include <QDateTime>
#include <QMessageBox>
#include <QInputDialog>
#include <QException>
#include <QRegExp>
#include <QRegExpValidator>
#include <QList>
#include <QMessageBox>
#include <QWidget>
#include <QByteArray>
#include <QDateTime>
#include <QCoreApplication>
#include <stdio.h>
Calibrate::Calibrate(){
	_t0 = 0;
	_k =  1.002737908; // Constant.. Relationship between the solar time (M) and the sidereal time (S): (S = M * 1.002737908)
	_isSetR1 = false;
	_isSetR2 = false;
	_isSetR3 = false;
}

/*
 * Calculates the inverse of the m[3x3] matrix and returns it in the second parameter.
 */
void Calibrate::_inv(double m[3][3], double res[3][3]){
	double idet;

	//Inverse of the determinant
	idet = 1/(
				(m[0][0]*m[1][1]*m[2][2]) + (m[0][1]*m[1][2]*m[2][0]) + (m[0][2]*m[1][0]*m[2][1])
			  -	(m[0][2]*m[1][1]*m[2][0]) - (m[0][1]*m[1][0]*m[2][2]) - (m[0][0]*m[1][2]*m[2][1]) 
			 );

	res[0][0] = ((m[1][1]*m[2][2]) - (m[2][1]*m[1][2]))*idet;
	res[0][1] = ((m[2][1]*m[0][2]) - (m[0][1]*m[2][2]))*idet;
	res[0][2] = ((m[0][1]*m[1][2]) - (m[1][1]*m[0][2]))*idet;

	res[1][0] = ((m[1][2]*m[2][0]) - (m[2][2]*m[1][0]))*idet;
	res[1][1] = ((m[2][2]*m[0][0]) - (m[0][2]*m[2][0]))*idet;
	res[1][2] = ((m[0][2]*m[1][0]) - (m[1][2]*m[0][0]))*idet;

	res[2][0] = ((m[1][0]*m[2][1]) - (m[2][0]*m[1][1]))*idet;
	res[2][1] = ((m[2][0]*m[0][1]) - (m[0][0]*m[2][1]))*idet;
	res[2][2] = ((m[0][0]*m[1][1]) - (m[1][0]*m[0][1]))*idet;
}

/*
 * Multiplies two matrices, m1[3x3] and m2[3x3], and returns the result in 
 * the third parameter.
 */
void Calibrate::_m_prod(double m1[3][3], double m2[3][3], double res[3][3]){
	for(int i=0; i<3; i++)
		for(int j=0; j<3; j++){
			res[i][j] = 0.0;
			for(int k=0; k<3; k++) //multiplying row by column
				res[i][j] += m1[i][k] * m2[k][j];
		}
}

/*
 * Calculates the Vector cosines (EVC) from the equatorial coordinates (ar, dec, t).
 */
void Calibrate::_setEVC(double ar, double dec, double t, double* EVC){
	EVC[0] = cos(dec)*cos(ar - _k*(t-_t0));
	EVC[1] = cos(dec)*sin(ar - _k*(t-_t0));
	EVC[2] = sin(dec);
	qDebug() << "ardec = ["<<ar <<"," <<dec<<"]";	
	qDebug() << "LMN = ["<<EVC[0] <<"," <<EVC[1] <<"," <<EVC[2]<<"]";
}

/*
 * Calculates the Vector cosines (HVC) from the horizontal coordinates (ac, alt).
 */
void Calibrate::_setHVC(double ac, double alt, double* HVC){
	HVC[0] = cos(alt)*cos(ac);
	HVC[1] = cos(alt)*sin(ac);
	HVC[2] = sin(alt);
	qDebug() << "altac = ["<<alt <<"," <<ac<<"]";
	qDebug() << "clmn=["<<HVC[0] <<"," <<HVC[1] <<"," <<HVC[2]<<"]";
}

/*
 * Sets the initial observation time.
 */
void Calibrate::setTime(double t0){
	_t0 = t0;
}

/*
 * Sets the first reference object.
 * If all the reference objects have been established, calls the function that calculates T and iT.
 */
void Calibrate::setRef_1(double ar, double dec, double t, double ac, double alt){
	_setEVC(ar, dec, t, _LMN1);
	_setHVC(ac, alt, _lmn1);
	_isSetR1 = true;
	_isSetR3 = false;

	if(_isSetR1 && _isSetR2 && _isSetR3)
		_setT();
}

/*
 * Sets the second reference object.
 * If all the reference objects have been established, calls the function that calculates T and iT.
 */
void Calibrate::setRef_2(double ar, double dec, double t, double ac, double alt){
	_setEVC(ar, dec, t, _LMN2);
	_setHVC(ac, alt, _lmn2);
	_isSetR2 = true;
	_isSetR3 = false;

	if(_isSetR1 && _isSetR2 && _isSetR3)
		_setT();
}

/*
 * Sets the third reference object.
 * If all the reference objects have been established, calls the function that calculates T and iT.
 */
void Calibrate::setRef_3(double ar, double dec, double t, double ac, double alt){
	_setEVC(ar, dec, t, _LMN3);
	_setHVC(ac, alt, _lmn3);
	_isSetR3 = true;

	if(_isSetR1 && _isSetR2 && _isSetR3)
		_setT();
}

/*
 * Indicates if the three reference objects have been established.
 */
bool Calibrate::isConfigured(){
	return (_isSetR1 && _isSetR2 && _isSetR3);
}

/*
 * Third reference object calculated from the cross product of the two first ones.
 * Then calls the function that calculates T and iT.
 */
void Calibrate::autoRef_3(){
	float sqrt1, sqrt2;
	qDebug() << "inside autoref";
	_isSetR3 = true;	
	if(_isSetR1 && _isSetR2){
		sqrt1 = (1/(  sqrt( pow(( (_lmn1[1]*_lmn2[2]) - (_lmn1[2]*_lmn2[1])),2) +
							pow(( (_lmn1[2]*_lmn2[0]) - (_lmn1[0]*_lmn2[2])),2) +
							pow(( (_lmn1[0]*_lmn2[1]) - (_lmn1[1]*_lmn2[0])),2))
				));
		_lmn3[0] = sqrt1 * ( (_lmn1[1]*_lmn2[2]) - (_lmn1[2]*_lmn2[1]) );
		_lmn3[1] = sqrt1 * ( (_lmn1[2]*_lmn2[0]) - (_lmn1[0]*_lmn2[2]) );
		_lmn3[2] = sqrt1 * ( (_lmn1[0]*_lmn2[1]) - (_lmn1[1]*_lmn2[0]) );

		sqrt2 = (1/(  sqrt( pow(( (_LMN1[1]*_LMN2[2]) - (_LMN1[2]*_LMN2[1])),2) +
							pow(( (_LMN1[2]*_LMN2[0]) - (_LMN1[0]*_LMN2[2])),2) +
							pow(( (_LMN1[0]*_LMN2[1]) - (_LMN1[1]*_LMN2[0])),2))
				));
		_LMN3[0] = sqrt2 * ( (_LMN1[1]*_LMN2[2]) - (_LMN1[2]*_LMN2[1]) );
		_LMN3[1] = sqrt2 * ( (_LMN1[2]*_LMN2[0]) - (_LMN1[0]*_LMN2[2]) );
		_LMN3[2] = sqrt2 * ( (_LMN1[0]*_LMN2[1]) - (_LMN1[1]*_LMN2[0]) );

		if(_isSetR1 && _isSetR2 && _isSetR3)
			_setT();
	}
}

/*
 *	Sets the transformation matrix and its inverse (T and iT, respectively).
 */
void Calibrate::_setT(){
	double subT1[3][3], subT2[3][3], aux[3][3];

	subT1[0][0] = _lmn1[0]; subT1[0][1] = _lmn2[0]; subT1[0][2] = _lmn3[0];
	subT1[1][0] = _lmn1[1]; subT1[1][1] = _lmn2[1]; subT1[1][2] = _lmn3[1];
	subT1[2][0] = _lmn1[2]; subT1[2][1] = _lmn2[2]; subT1[2][2] = _lmn3[2];

	subT2[0][0] = _LMN1[0]; subT2[0][1] = _LMN2[0]; subT2[0][2] = _LMN3[0];
	subT2[1][0] = _LMN1[1]; subT2[1][1] = _LMN2[1]; subT2[1][2] = _LMN3[1];
	subT2[2][0] = _LMN1[2]; subT2[2][1] = _LMN2[2]; subT2[2][2] = _LMN3[2];

	_inv(subT2, aux);
	_m_prod(subT1, aux, _T);
	_inv(_T, _iT);
	qDebug() << "LMN3 = ["<<_LMN3[0] <<"," <<_LMN3[1] <<"," <<_LMN3[2]<<"]" ;
	qDebug() << "lmn3 = ["<<_lmn3[0] <<"," <<_lmn3[1] <<"," <<_lmn3[2]<<"]" ;
	qDebug() << "T1 = ["<<_T[0][0] <<"," <<_T[0][1] <<"," <<_T[0][2] <<"," <<_T[1][0] <<"," <<_T[1][1] <<"," <<_T[1][2] <<"," <<_T[2][0] <<"," <<_T[2][1] <<"," <<_T[2][2] <<"]" ;
	//qDebug() << "iT = ["<<_iT[0][0] <<"," <<_iT[0][1] <<"," <<_iT[0][2] <<"," <<_iT[1][0] <<"," <<_iT[1][1] <<"," <<_iT[1][2] <<"," <<_iT[2][0] <<"," <<_iT[2][1] <<"," <<_iT[2][2] <<"]";
	
}

/*
 * Horizontal coordinates (ac, alt) obtained from equatorial ones and time (ar, dec, t).
 *
 * If the third reference object is not established, it calculates it by calling the 
 * proper function.
 */
void Calibrate::getHCoords(double ar, double dec, double t, double *ac, double *alt){
	double HVC[3];
	double EVC[3];
	_setEVC(ar, dec, t, EVC);

	if(!_isSetR3){
		autoRef_3();
	}

	for(int i=0; i<3; i++){
		HVC[i] = 0.0; }
	for(int i=0; i<3; i++){	
		for(int j=0; j<3; j++){
			HVC[i] += _T[i][j] * EVC[j];
		}
	}	

	qDebug() << "clmnforgoto=["<<HVC[0] <<"," <<HVC[1] <<"," <<HVC[2]<<"]";

	(*ac) = atan2(HVC[1], HVC[0]);
        //(*alt) = atan2(HVC[2]*sin(*ac),HVC[1]);
        (*alt) = asin(HVC[2]);
}

/*
 * Equatorial coordinates (ar, dec) obtained from horizontal ones and time (ac, alt, t).
 * 
 * If the third reference object is not established, it calculates it by calling the 
 * proper function.
 */
void Calibrate::getECoords(double ac, double alt, double t, double *ar, double *dec){
	double HVC[3];
	double EVC[3];
	_setHVC(ac, alt, HVC);

	if(!_isSetR3){
		autoRef_3();
	}

	for(int i=0; i<3; i++)
		EVC[i] = 0.0;
	for(int i=0; i<3; i++)
		for(int j=0; j<3; j++)
				EVC[i] += _iT[i][j] * HVC[j];

	(*ar) = atan2(EVC[1], EVC[0]) + (_k*(t-_t0));
	(*dec) = asin(EVC[2]);
}