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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
|
/// \file BasicMathPlugin.cpp
/// \author Roger James
/// \date 13th November 2013
#include "BasicMathPlugin.h"
#include "DriverCommon.h"
#include "libastro.h"
#include <libnova/julian_day.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_linalg.h>
#include "indicom.h"
#include <limits>
#include <iostream>
#include <map>
namespace INDI
{
namespace AlignmentSubsystem
{
BasicMathPlugin::BasicMathPlugin()
{
pActualToApparentTransform = gsl_matrix_alloc(3, 3);
pApparentToActualTransform = gsl_matrix_alloc(3, 3);
}
// Destructor
BasicMathPlugin::~BasicMathPlugin()
{
gsl_matrix_free(pActualToApparentTransform);
gsl_matrix_free(pApparentToActualTransform);
}
// Public methods
bool BasicMathPlugin::Initialise(InMemoryDatabase *pInMemoryDatabase)
{
MathPlugin::Initialise(pInMemoryDatabase);
InMemoryDatabase::AlignmentDatabaseType &SyncPoints = pInMemoryDatabase->GetAlignmentDatabase();
/// See how many entries there are in the in memory database.
/// - If just one use a hint to mounts approximate alignment, this can either be ZENITH,
/// NORTH_CELESTIAL_POLE or SOUTH_CELESTIAL_POLE. The hint is used to make a dummy second
/// entry. A dummy third entry is computed from the cross product of the first two. A transform
/// matrix is then computed.
/// - If two make the dummy third entry and compute a transform matrix.
/// - If three compute a transform matrix.
/// - If four or more compute a convex hull, then matrices for each
/// triangular facet of the hull.
switch (SyncPoints.size())
{
// JM 2021-07-04: No Transformation required.
case 0:
return true;
// JM 2021-07-04: For 1 point, it should be direct reciporical transformation.
case 1:
{
AlignmentDatabaseEntry &Entry1 = SyncPoints[0];
INDI::IEquatorialCoordinates RaDec;
INDI::IHorizontalCoordinates ActualSyncPoint1;
TelescopeDirectionVector ActualDirectionCosine1;
IGeographicCoordinates Position;
if (!pInMemoryDatabase->GetDatabaseReferencePosition(Position))
return false;
RaDec.declination = Entry1.Declination;
RaDec.rightascension = Entry1.RightAscension;
if (ApproximateMountAlignment == ZENITH)
{
EquatorialToHorizontal(&RaDec, &Position, Entry1.ObservationJulianDate, &ActualSyncPoint1);
// Now express this coordinate as a normalised direction vector (a.k.a direction cosines)
ActualDirectionCosine1 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint1);
}
else
{
ActualDirectionCosine1 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec);
}
TelescopeDirectionVector DummyActualDirectionCosine2;
TelescopeDirectionVector DummyApparentDirectionCosine2;
TelescopeDirectionVector DummyActualDirectionCosine3;
TelescopeDirectionVector DummyApparentDirectionCosine3;
switch (ApproximateMountAlignment)
{
case ZENITH:
DummyActualDirectionCosine2.x = 0.0;
DummyActualDirectionCosine2.y = 0.0;
DummyActualDirectionCosine2.z = 1.0;
DummyApparentDirectionCosine2 = DummyActualDirectionCosine2;
break;
case NORTH_CELESTIAL_POLE:
{
INDI::IEquatorialCoordinates DummyRaDec;
//INDI::IHorizontalCoordinates DummyAltAz;
DummyRaDec.rightascension = 0.0;
DummyRaDec.declination = 90.0;
//EquatorialToHorizontal(&DummyRaDec, &Position, ln_get_julian_from_sys(), &DummyAltAz);
DummyActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(DummyRaDec);
DummyApparentDirectionCosine2 = DummyActualDirectionCosine2;
break;
}
case SOUTH_CELESTIAL_POLE:
{
INDI::IEquatorialCoordinates DummyRaDec;
//INDI::IHorizontalCoordinates DummyAltAz;
DummyRaDec.rightascension = 0.0;
DummyRaDec.declination = -90.0;
//EquatorialToHorizontal(&DummyRaDec, &Position, ln_get_julian_from_sys(), &DummyAltAz);
DummyActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(DummyRaDec);
DummyApparentDirectionCosine2 = DummyActualDirectionCosine2;
break;
}
}
DummyActualDirectionCosine3 = ActualDirectionCosine1 * DummyActualDirectionCosine2;
DummyActualDirectionCosine3.Normalise();
DummyApparentDirectionCosine3 = Entry1.TelescopeDirection * DummyApparentDirectionCosine2;
DummyApparentDirectionCosine3.Normalise();
CalculateTransformMatrices(ActualDirectionCosine1, DummyActualDirectionCosine2, DummyActualDirectionCosine3,
Entry1.TelescopeDirection, DummyApparentDirectionCosine2,
DummyApparentDirectionCosine3, pActualToApparentTransform,
pApparentToActualTransform);
return true;
}
case 2:
{
// First compute local horizontal coordinates for the two sync points
AlignmentDatabaseEntry &Entry1 = SyncPoints[0];
AlignmentDatabaseEntry &Entry2 = SyncPoints[1];
INDI::IEquatorialCoordinates RaDec1;
INDI::IEquatorialCoordinates RaDec2;
TelescopeDirectionVector ActualDirectionCosine1;
TelescopeDirectionVector ActualDirectionCosine2;
RaDec1.declination = Entry1.Declination;
RaDec1.rightascension = Entry1.RightAscension;
RaDec2.declination = Entry2.Declination;
RaDec2.rightascension = Entry2.RightAscension;
IGeographicCoordinates Position { 0, 0, 0 };
if (!pInMemoryDatabase->GetDatabaseReferencePosition(Position))
return false;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualSyncPoint1;
INDI::IHorizontalCoordinates ActualSyncPoint2;
EquatorialToHorizontal(&RaDec1, &Position, Entry1.ObservationJulianDate, &ActualSyncPoint1);
EquatorialToHorizontal(&RaDec2, &Position, Entry2.ObservationJulianDate, &ActualSyncPoint2);
ActualDirectionCosine1 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint2);
}
else
{
ActualDirectionCosine1 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec2);
}
// Now express these coordinates as normalised direction vectors (a.k.a direction cosines)
TelescopeDirectionVector DummyActualDirectionCosine3;
TelescopeDirectionVector DummyApparentDirectionCosine3;
DummyActualDirectionCosine3 = ActualDirectionCosine1 * ActualDirectionCosine2;
DummyActualDirectionCosine3.Normalise();
DummyApparentDirectionCosine3 = Entry1.TelescopeDirection * Entry2.TelescopeDirection;
DummyApparentDirectionCosine3.Normalise();
// The third direction vectors is generated by taking the cross product of the first two
CalculateTransformMatrices(ActualDirectionCosine1, ActualDirectionCosine2, DummyActualDirectionCosine3,
Entry1.TelescopeDirection, Entry2.TelescopeDirection,
DummyApparentDirectionCosine3, pActualToApparentTransform,
pApparentToActualTransform);
return true;
}
case 3:
{
// First compute local horizontal coordinates for the three sync points
AlignmentDatabaseEntry &Entry1 = SyncPoints[0];
AlignmentDatabaseEntry &Entry2 = SyncPoints[1];
AlignmentDatabaseEntry &Entry3 = SyncPoints[2];
INDI::IEquatorialCoordinates RaDec1, RaDec2, RaDec3;
TelescopeDirectionVector ActualDirectionCosine1, ActualDirectionCosine2, ActualDirectionCosine3;
RaDec1.declination = Entry1.Declination;
RaDec1.rightascension = Entry1.RightAscension;
RaDec2.declination = Entry2.Declination;
RaDec2.rightascension = Entry2.RightAscension;
RaDec3.declination = Entry3.Declination;
RaDec3.rightascension = Entry3.RightAscension;
IGeographicCoordinates Position { 0, 0, 0 };
if (!pInMemoryDatabase->GetDatabaseReferencePosition(Position))
return false;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualSyncPoint1;
INDI::IHorizontalCoordinates ActualSyncPoint2;
INDI::IHorizontalCoordinates ActualSyncPoint3;
EquatorialToHorizontal(&RaDec1, &Position, Entry1.ObservationJulianDate, &ActualSyncPoint1);
EquatorialToHorizontal(&RaDec2, &Position, Entry2.ObservationJulianDate, &ActualSyncPoint2);
EquatorialToHorizontal(&RaDec3, &Position, Entry3.ObservationJulianDate, &ActualSyncPoint3);
// Now express these coordinates as normalised direction vectors (a.k.a direction cosines)
ActualDirectionCosine1 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint3);
}
else
{
ActualDirectionCosine1 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec3);
}
CalculateTransformMatrices(ActualDirectionCosine1, ActualDirectionCosine2, ActualDirectionCosine3,
Entry1.TelescopeDirection, Entry2.TelescopeDirection, Entry3.TelescopeDirection,
pActualToApparentTransform, pApparentToActualTransform);
return true;
}
default:
{
IGeographicCoordinates Position { 0, 0, 0 };
if (!pInMemoryDatabase->GetDatabaseReferencePosition(Position))
return false;
// Compute Hulls etc.
ActualConvexHull.Reset();
ApparentConvexHull.Reset();
ActualDirectionCosines.clear();
// Add a dummy point at the nadir
ActualConvexHull.MakeNewVertex(0.0, 0.0, -1.0, 0);
ApparentConvexHull.MakeNewVertex(0.0, 0.0, -1.0, 0);
int VertexNumber = 1;
// Add the rest of the vertices
for (InMemoryDatabase::AlignmentDatabaseType::const_iterator Itr = SyncPoints.begin();
Itr != SyncPoints.end(); Itr++)
{
INDI::IEquatorialCoordinates RaDec;
TelescopeDirectionVector ActualDirectionCosine;
RaDec.declination = (*Itr).Declination;
RaDec.rightascension = (*Itr).RightAscension;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualSyncPoint;
EquatorialToHorizontal(&RaDec, &Position, (*Itr).ObservationJulianDate, &ActualSyncPoint);
// Now express this coordinate as normalised direction vectors (a.k.a direction cosines)
ActualDirectionCosine = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint);
}
else
{
ActualDirectionCosine = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec);
}
ActualDirectionCosines.push_back(ActualDirectionCosine);
ActualConvexHull.MakeNewVertex(ActualDirectionCosine.x, ActualDirectionCosine.y,
ActualDirectionCosine.z, VertexNumber);
ApparentConvexHull.MakeNewVertex((*Itr).TelescopeDirection.x, (*Itr).TelescopeDirection.y,
(*Itr).TelescopeDirection.z, VertexNumber);
VertexNumber++;
}
// I should only need to do this once but it is easier to do it twice
if (!ActualConvexHull.DoubleTriangle())
return false;
ActualConvexHull.ConstructHull();
ActualConvexHull.EdgeOrderOnFaces();
if (!ApparentConvexHull.DoubleTriangle())
return false;
ApparentConvexHull.ConstructHull();
ApparentConvexHull.EdgeOrderOnFaces();
// Make the matrices
ConvexHull::tFace CurrentFace = ActualConvexHull.faces;
#ifdef CONVEX_HULL_DEBUGGING
int ActualFaces = 0;
#endif
if (nullptr != CurrentFace)
{
do
{
#ifdef CONVEX_HULL_DEBUGGING
ActualFaces++;
#endif
if ((0 == CurrentFace->vertex[0]->vnum) || (0 == CurrentFace->vertex[1]->vnum) ||
(0 == CurrentFace->vertex[2]->vnum))
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Initialise - Ignoring actual face %d", ActualFaces);
#endif
}
else
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Initialise - Processing actual face %d v1 %d v2 %d v3 %d", ActualFaces,
CurrentFace->vertex[0]->vnum, CurrentFace->vertex[1]->vnum,
CurrentFace->vertex[2]->vnum);
#endif
CalculateTransformMatrices(ActualDirectionCosines[CurrentFace->vertex[0]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[1]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[2]->vnum - 1],
SyncPoints[CurrentFace->vertex[0]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[1]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[2]->vnum - 1].TelescopeDirection,
CurrentFace->pMatrix, nullptr);
}
CurrentFace = CurrentFace->next;
}
while (CurrentFace != ActualConvexHull.faces);
}
// One of these days I will optimise this
CurrentFace = ApparentConvexHull.faces;
#ifdef CONVEX_HULL_DEBUGGING
int ApparentFaces = 0;
#endif
if (nullptr != CurrentFace)
{
do
{
#ifdef CONVEX_HULL_DEBUGGING
ApparentFaces++;
#endif
if ((0 == CurrentFace->vertex[0]->vnum) || (0 == CurrentFace->vertex[1]->vnum) ||
(0 == CurrentFace->vertex[2]->vnum))
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Initialise - Ignoring apparent face %d", ApparentFaces);
#endif
}
else
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Initialise - Processing apparent face %d v1 %d v2 %d v3 %d", ApparentFaces,
CurrentFace->vertex[0]->vnum, CurrentFace->vertex[1]->vnum,
CurrentFace->vertex[2]->vnum);
#endif
CalculateTransformMatrices(SyncPoints[CurrentFace->vertex[0]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[1]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[2]->vnum - 1].TelescopeDirection,
ActualDirectionCosines[CurrentFace->vertex[0]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[1]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[2]->vnum - 1],
CurrentFace->pMatrix, nullptr);
}
CurrentFace = CurrentFace->next;
}
while (CurrentFace != ApparentConvexHull.faces);
}
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Initialise - ActualFaces %d ApparentFaces %d", ActualFaces, ApparentFaces);
ActualConvexHull.PrintObj("ActualHull.obj");
ActualConvexHull.PrintOut("ActualHull.log", ActualConvexHull.vertices);
ApparentConvexHull.PrintObj("ApparentHull.obj");
ActualConvexHull.PrintOut("ApparentHull.log", ApparentConvexHull.vertices);
#endif
return true;
}
}
}
bool BasicMathPlugin::TransformCelestialToTelescope(const double RightAscension, const double Declination,
double JulianOffset,
TelescopeDirectionVector &ApparentTelescopeDirectionVector)
{
INDI::IEquatorialCoordinates ActualRaDec;
ActualRaDec.rightascension = RightAscension;
ActualRaDec.declination = Declination;
IGeographicCoordinates Position { 0, 0, 0 };
// Should check that this the same as the current observing position
if ((nullptr == pInMemoryDatabase) || !pInMemoryDatabase->GetDatabaseReferencePosition(Position))
return false;
InMemoryDatabase::AlignmentDatabaseType &SyncPoints = pInMemoryDatabase->GetAlignmentDatabase();
switch (SyncPoints.size())
{
case 0:
{
// 0 sync points
switch (ApproximateMountAlignment)
{
case ZENITH:
INDI::IHorizontalCoordinates ActualAltAz;
EquatorialToHorizontal(&ActualRaDec, &Position, ln_get_julian_from_sys() + JulianOffset, &ActualAltAz);
ApparentTelescopeDirectionVector = TelescopeDirectionVectorFromAltitudeAzimuth(ActualAltAz);
ASSDEBUGF("Celestial to telescope - Actual Az %lf Alt %lf", ActualAltAz.azimuth, ActualAltAz.altitude);
break;
case NORTH_CELESTIAL_POLE:
// Rotate the TDV coordinate system clockwise (negative) around the y axis by 90 minus
// the (positive)observatory latitude. The vector itself is rotated anticlockwise
//ApparentTelescopeDirectionVector.RotateAroundY(Position.latitude - 90.0);
case SOUTH_CELESTIAL_POLE:
// Rotate the TDV coordinate system anticlockwise (positive) around the y axis by 90 plus
// the (negative)observatory latitude. The vector itself is rotated clockwise
//ApparentTelescopeDirectionVector.RotateAroundY(Position.latitude + 90.0);
ApparentTelescopeDirectionVector = TelescopeDirectionVectorFromEquatorialCoordinates(ActualRaDec);
break;
}
break;
}
case 1:
case 2:
case 3:
{
TelescopeDirectionVector ActualVector;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualAltAz;
EquatorialToHorizontal(&ActualRaDec, &Position, ln_get_julian_from_sys() + JulianOffset, &ActualAltAz);
ActualVector = TelescopeDirectionVectorFromAltitudeAzimuth(ActualAltAz);
}
else
{
ActualVector = TelescopeDirectionVectorFromEquatorialCoordinates(ActualRaDec);
}
gsl_vector *pGSLActualVector = gsl_vector_alloc(3);
gsl_vector_set(pGSLActualVector, 0, ActualVector.x);
gsl_vector_set(pGSLActualVector, 1, ActualVector.y);
gsl_vector_set(pGSLActualVector, 2, ActualVector.z);
gsl_vector *pGSLApparentVector = gsl_vector_alloc(3);
MatrixVectorMultiply(pActualToApparentTransform, pGSLActualVector, pGSLApparentVector);
ApparentTelescopeDirectionVector.x = gsl_vector_get(pGSLApparentVector, 0);
ApparentTelescopeDirectionVector.y = gsl_vector_get(pGSLApparentVector, 1);
ApparentTelescopeDirectionVector.z = gsl_vector_get(pGSLApparentVector, 2);
ApparentTelescopeDirectionVector.Normalise();
gsl_vector_free(pGSLActualVector);
gsl_vector_free(pGSLApparentVector);
break;
}
default:
{
TelescopeDirectionVector ActualVector;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualAltAz;
EquatorialToHorizontal(&ActualRaDec, &Position, ln_get_julian_from_sys() + JulianOffset, &ActualAltAz);
ActualVector = TelescopeDirectionVectorFromAltitudeAzimuth(ActualAltAz);
}
else
{
ActualVector = TelescopeDirectionVectorFromEquatorialCoordinates(ActualRaDec);
}
gsl_matrix *pTransform;
gsl_matrix *pComputedTransform = nullptr;
// Scale the actual telescope direction vector to make sure it traverses the unit sphere.
TelescopeDirectionVector ScaledActualVector = ActualVector * 2.0;
// Shoot the scaled vector in the into the list of actual facets
// and use the conversuion matrix from the one it intersects
ConvexHull::tFace CurrentFace = ActualConvexHull.faces;
#ifdef CONVEX_HULL_DEBUGGING
int ActualFaces = 0;
#endif
if (nullptr != CurrentFace)
{
do
{
#ifdef CONVEX_HULL_DEBUGGING
ActualFaces++;
#endif
// Ignore faces containing vertex 0 (nadir).
if ((0 == CurrentFace->vertex[0]->vnum) || (0 == CurrentFace->vertex[1]->vnum) ||
(0 == CurrentFace->vertex[2]->vnum))
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Celestial to telescope - Ignoring actual face %d", ActualFaces);
#endif
}
else
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Celestial to telescope - Processing actual face %d v1 %d v2 %d v3 %d", ActualFaces,
CurrentFace->vertex[0]->vnum, CurrentFace->vertex[1]->vnum,
CurrentFace->vertex[2]->vnum);
#endif
if (RayTriangleIntersection(ScaledActualVector,
ActualDirectionCosines[CurrentFace->vertex[0]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[1]->vnum - 1],
ActualDirectionCosines[CurrentFace->vertex[2]->vnum - 1]))
break;
}
CurrentFace = CurrentFace->next;
}
while (CurrentFace != ActualConvexHull.faces);
if (CurrentFace == ActualConvexHull.faces)
{
// Find the three nearest points and build a transform
std::map<double, const AlignmentDatabaseEntry *> NearestMap;
for (InMemoryDatabase::AlignmentDatabaseType::const_iterator Itr = SyncPoints.begin();
Itr != SyncPoints.end(); Itr++)
{
INDI::IEquatorialCoordinates RaDec;
TelescopeDirectionVector ActualDirectionCosine;
RaDec.rightascension = (*Itr).RightAscension;
RaDec.declination = (*Itr).Declination;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualPoint;
EquatorialToHorizontal(&RaDec, &Position, (*Itr).ObservationJulianDate, &ActualPoint);
ActualDirectionCosine = TelescopeDirectionVectorFromAltitudeAzimuth(ActualPoint);
}
else
{
ActualDirectionCosine = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec);
}
NearestMap[(ActualDirectionCosine - ActualVector).Length()] = &(*Itr);
}
// First compute local horizontal coordinates for the three sync points
std::map<double, const AlignmentDatabaseEntry *>::const_iterator Nearest = NearestMap.begin();
const AlignmentDatabaseEntry *pEntry1 = (*Nearest).second;
Nearest++;
const AlignmentDatabaseEntry *pEntry2 = (*Nearest).second;
Nearest++;
const AlignmentDatabaseEntry *pEntry3 = (*Nearest).second;
INDI::IEquatorialCoordinates RaDec1;
INDI::IEquatorialCoordinates RaDec2;
INDI::IEquatorialCoordinates RaDec3;
TelescopeDirectionVector ActualDirectionCosine1;
TelescopeDirectionVector ActualDirectionCosine2;
TelescopeDirectionVector ActualDirectionCosine3;
RaDec1.declination = pEntry1->Declination;
RaDec1.rightascension = pEntry1->RightAscension;
RaDec2.declination = pEntry2->Declination;
RaDec2.rightascension = pEntry2->RightAscension;
RaDec3.declination = pEntry3->Declination;
RaDec3.rightascension = pEntry3->RightAscension;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualSyncPoint1;
INDI::IHorizontalCoordinates ActualSyncPoint2;
INDI::IHorizontalCoordinates ActualSyncPoint3;
EquatorialToHorizontal(&RaDec1, &Position, pEntry1->ObservationJulianDate, &ActualSyncPoint1);
EquatorialToHorizontal(&RaDec2, &Position, pEntry2->ObservationJulianDate, &ActualSyncPoint2);
EquatorialToHorizontal(&RaDec3, &Position, pEntry3->ObservationJulianDate, &ActualSyncPoint3);
// Now express these coordinates as normalised direction vectors (a.k.a direction cosines)
ActualDirectionCosine1 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint3);
}
else
{
ActualDirectionCosine1 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec3);
}
pComputedTransform = gsl_matrix_alloc(3, 3);
CalculateTransformMatrices(ActualDirectionCosine1, ActualDirectionCosine2, ActualDirectionCosine3,
pEntry1->TelescopeDirection, pEntry2->TelescopeDirection,
pEntry3->TelescopeDirection, pComputedTransform, nullptr);
pTransform = pComputedTransform;
}
else
pTransform = CurrentFace->pMatrix;
}
else
return false;
// OK - got an intersection - CurrentFace is pointing at the face
gsl_vector *pGSLActualVector = gsl_vector_alloc(3);
gsl_vector_set(pGSLActualVector, 0, ActualVector.x);
gsl_vector_set(pGSLActualVector, 1, ActualVector.y);
gsl_vector_set(pGSLActualVector, 2, ActualVector.z);
gsl_vector *pGSLApparentVector = gsl_vector_alloc(3);
MatrixVectorMultiply(pTransform, pGSLActualVector, pGSLApparentVector);
ApparentTelescopeDirectionVector.x = gsl_vector_get(pGSLApparentVector, 0);
ApparentTelescopeDirectionVector.y = gsl_vector_get(pGSLApparentVector, 1);
ApparentTelescopeDirectionVector.z = gsl_vector_get(pGSLApparentVector, 2);
ApparentTelescopeDirectionVector.Normalise();
gsl_vector_free(pGSLActualVector);
gsl_vector_free(pGSLApparentVector);
if (nullptr != pComputedTransform)
gsl_matrix_free(pComputedTransform);
break;
}
}
// INDI::IHorizontalCoordinates ApparentAltAz;
// AltitudeAzimuthFromTelescopeDirectionVector(ApparentTelescopeDirectionVector, ApparentAltAz);
// ASSDEBUGF("Celestial to telescope - Apparent Az %lf Alt %lf", ApparentAltAz.azimuth, ApparentAltAz.altitude);
return true;
}
bool BasicMathPlugin::TransformTelescopeToCelestial(const TelescopeDirectionVector &ApparentTelescopeDirectionVector,
double &RightAscension, double &Declination)
{
IGeographicCoordinates Position;
//INDI::IHorizontalCoordinates ApparentAltAz;
INDI::IHorizontalCoordinates ActualAltAz;
INDI::IEquatorialCoordinates ActualRaDec;
// AltitudeAzimuthFromTelescopeDirectionVector(ApparentTelescopeDirectionVector, ApparentAltAz);
// ASSDEBUGF("Telescope to celestial - Apparent Az %lf Alt %lf", ApparentAltAz.azimuth, ApparentAltAz.altitude);
if ((nullptr == pInMemoryDatabase) || !pInMemoryDatabase->GetDatabaseReferencePosition(Position))
{
// Should check that this the same as the current observing position
ASSDEBUG("No database or no position in database");
return false;
}
InMemoryDatabase::AlignmentDatabaseType &SyncPoints = pInMemoryDatabase->GetAlignmentDatabase();
switch (SyncPoints.size())
{
case 0:
{
// 0 sync points
switch (ApproximateMountAlignment)
{
// For Alt-Az mounts, get Alt-Az from the telescope direction vector first
// Then transform to actual RA/DE
case ZENITH:
{
ASSDEBUGF("ApparentVector x %lf y %lf z %lf", ApparentTelescopeDirectionVector.x,
ApparentTelescopeDirectionVector.y, ApparentTelescopeDirectionVector.z);
//ASSDEBUGF("ActualVector x %lf y %lf z %lf", RotatedTDV.x, RotatedTDV.y, RotatedTDV.z);
AltitudeAzimuthFromTelescopeDirectionVector(ApparentTelescopeDirectionVector, ActualAltAz);
HorizontalToEquatorial(&ActualAltAz, &Position, ln_get_julian_from_sys(), &ActualRaDec);
}
break;
// For equatorial mount with zero sync points, just convert back from telescope
// direction vector to equatorial coordinates.
case NORTH_CELESTIAL_POLE:
case SOUTH_CELESTIAL_POLE:
EquatorialCoordinatesFromTelescopeDirectionVector(ApparentTelescopeDirectionVector, ActualRaDec);
break;
}
RightAscension = ActualRaDec.rightascension;
Declination = ActualRaDec.declination;
break;
}
case 1:
case 2:
case 3:
{
gsl_vector *pGSLApparentVector = gsl_vector_alloc(3);
gsl_vector_set(pGSLApparentVector, 0, ApparentTelescopeDirectionVector.x);
gsl_vector_set(pGSLApparentVector, 1, ApparentTelescopeDirectionVector.y);
gsl_vector_set(pGSLApparentVector, 2, ApparentTelescopeDirectionVector.z);
gsl_vector *pGSLActualVector = gsl_vector_alloc(3);
MatrixVectorMultiply(pApparentToActualTransform, pGSLApparentVector, pGSLActualVector);
Dump3("ApparentVector", pGSLApparentVector);
Dump3("ActualVector", pGSLActualVector);
TelescopeDirectionVector ActualTelescopeDirectionVector;
ActualTelescopeDirectionVector.x = gsl_vector_get(pGSLActualVector, 0);
ActualTelescopeDirectionVector.y = gsl_vector_get(pGSLActualVector, 1);
ActualTelescopeDirectionVector.z = gsl_vector_get(pGSLActualVector, 2);
ActualTelescopeDirectionVector.Normalise();
if (ApproximateMountAlignment == ZENITH)
{
AltitudeAzimuthFromTelescopeDirectionVector(ActualTelescopeDirectionVector, ActualAltAz);
HorizontalToEquatorial(&ActualAltAz, &Position, ln_get_julian_from_sys(), &ActualRaDec);
}
else
{
EquatorialCoordinatesFromTelescopeDirectionVector(ActualTelescopeDirectionVector, ActualRaDec);
}
RightAscension = ActualRaDec.rightascension;
Declination = ActualRaDec.declination;
gsl_vector_free(pGSLActualVector);
gsl_vector_free(pGSLApparentVector);
break;
}
default:
{
gsl_matrix *pTransform;
gsl_matrix *pComputedTransform = nullptr;
// Scale the apparent telescope direction vector to make sure it traverses the unit sphere.
TelescopeDirectionVector ScaledApparentVector = ApparentTelescopeDirectionVector * 2.0;
// Shoot the scaled vector in the into the list of apparent facets
// and use the conversuion matrix from the one it intersects
ConvexHull::tFace CurrentFace = ApparentConvexHull.faces;
#ifdef CONVEX_HULL_DEBUGGING
int ApparentFaces = 0;
#endif
if (nullptr != CurrentFace)
{
do
{
#ifdef CONVEX_HULL_DEBUGGING
ApparentFaces++;
#endif
// Ignore faces containing vertex 0 (nadir).
if ((0 == CurrentFace->vertex[0]->vnum) || (0 == CurrentFace->vertex[1]->vnum) ||
(0 == CurrentFace->vertex[2]->vnum))
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("Celestial to telescope - Ignoring apparent face %d", ApparentFaces);
#endif
}
else
{
#ifdef CONVEX_HULL_DEBUGGING
ASSDEBUGF("TelescopeToCelestial - Processing apparent face %d v1 %d v2 %d v3 %d", ApparentFaces,
CurrentFace->vertex[0]->vnum, CurrentFace->vertex[1]->vnum,
CurrentFace->vertex[2]->vnum);
#endif
if (RayTriangleIntersection(ScaledApparentVector,
SyncPoints[CurrentFace->vertex[0]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[1]->vnum - 1].TelescopeDirection,
SyncPoints[CurrentFace->vertex[2]->vnum - 1].TelescopeDirection))
break;
}
CurrentFace = CurrentFace->next;
}
while (CurrentFace != ApparentConvexHull.faces);
if (CurrentFace == ApparentConvexHull.faces)
{
// Find the three nearest points and build a transform
std::map<double, const AlignmentDatabaseEntry *> NearestMap;
for (InMemoryDatabase::AlignmentDatabaseType::const_iterator Itr = SyncPoints.begin();
Itr != SyncPoints.end(); Itr++)
{
NearestMap[((*Itr).TelescopeDirection - ApparentTelescopeDirectionVector).Length()] = &(*Itr);
}
// First compute local horizontal coordinates for the three sync points
std::map<double, const AlignmentDatabaseEntry *>::const_iterator Nearest = NearestMap.begin();
const AlignmentDatabaseEntry *pEntry1 = (*Nearest).second;
Nearest++;
const AlignmentDatabaseEntry *pEntry2 = (*Nearest).second;
Nearest++;
const AlignmentDatabaseEntry *pEntry3 = (*Nearest).second;
INDI::IEquatorialCoordinates RaDec1;
INDI::IEquatorialCoordinates RaDec2;
INDI::IEquatorialCoordinates RaDec3;
TelescopeDirectionVector ActualDirectionCosine1;
TelescopeDirectionVector ActualDirectionCosine2;
TelescopeDirectionVector ActualDirectionCosine3;
RaDec1.declination = pEntry1->Declination;
RaDec1.rightascension = pEntry1->RightAscension;
RaDec2.declination = pEntry2->Declination;
RaDec2.rightascension = pEntry2->RightAscension;
RaDec3.declination = pEntry3->Declination;
RaDec3.rightascension = pEntry3->RightAscension;
if (ApproximateMountAlignment == ZENITH)
{
INDI::IHorizontalCoordinates ActualSyncPoint1;
INDI::IHorizontalCoordinates ActualSyncPoint2;
INDI::IHorizontalCoordinates ActualSyncPoint3;
EquatorialToHorizontal(&RaDec1, &Position, pEntry1->ObservationJulianDate, &ActualSyncPoint1);
EquatorialToHorizontal(&RaDec2, &Position, pEntry2->ObservationJulianDate, &ActualSyncPoint2);
EquatorialToHorizontal(&RaDec3, &Position, pEntry3->ObservationJulianDate, &ActualSyncPoint3);
// Now express these coordinates as normalised direction vectors (a.k.a direction cosines)
ActualDirectionCosine1 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromAltitudeAzimuth(ActualSyncPoint3);
}
else
{
ActualDirectionCosine1 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec1);
ActualDirectionCosine2 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec2);
ActualDirectionCosine3 = TelescopeDirectionVectorFromEquatorialCoordinates(RaDec3);
}
pComputedTransform = gsl_matrix_alloc(3, 3);
CalculateTransformMatrices(pEntry1->TelescopeDirection, pEntry2->TelescopeDirection,
pEntry3->TelescopeDirection, ActualDirectionCosine1,
ActualDirectionCosine2, ActualDirectionCosine3, pComputedTransform,
nullptr);
pTransform = pComputedTransform;
}
else
pTransform = CurrentFace->pMatrix;
}
else
return false;
// OK - got an intersection - CurrentFace is pointing at the face
gsl_vector *pGSLApparentVector = gsl_vector_alloc(3);
gsl_vector_set(pGSLApparentVector, 0, ApparentTelescopeDirectionVector.x);
gsl_vector_set(pGSLApparentVector, 1, ApparentTelescopeDirectionVector.y);
gsl_vector_set(pGSLApparentVector, 2, ApparentTelescopeDirectionVector.z);
gsl_vector *pGSLActualVector = gsl_vector_alloc(3);
MatrixVectorMultiply(pTransform, pGSLApparentVector, pGSLActualVector);
TelescopeDirectionVector ActualTelescopeDirectionVector;
ActualTelescopeDirectionVector.x = gsl_vector_get(pGSLActualVector, 0);
ActualTelescopeDirectionVector.y = gsl_vector_get(pGSLActualVector, 1);
ActualTelescopeDirectionVector.z = gsl_vector_get(pGSLActualVector, 2);
ActualTelescopeDirectionVector.Normalise();
if (ApproximateMountAlignment == ZENITH)
{
AltitudeAzimuthFromTelescopeDirectionVector(ActualTelescopeDirectionVector, ActualAltAz);
HorizontalToEquatorial(&ActualAltAz, &Position, ln_get_julian_from_sys(), &ActualRaDec);
}
else
{
EquatorialCoordinatesFromTelescopeDirectionVector(ActualTelescopeDirectionVector, ActualRaDec);
}
// libnova works in decimal degrees so conversion is needed here
RightAscension = ActualRaDec.rightascension;
Declination = ActualRaDec.declination;
gsl_vector_free(pGSLActualVector);
gsl_vector_free(pGSLApparentVector);
if (nullptr != pComputedTransform)
gsl_matrix_free(pComputedTransform);
break;
}
}
//ASSDEBUGF("Telescope to Celestial - Actual Az %lf Alt %lf", ActualAltAz.azimuth, ActualAltAz.altitude);
return true;
}
// Private methods
void BasicMathPlugin::Dump3(const char *Label, gsl_vector *pVector)
{
ASSDEBUGF("Vector dump - %s", Label);
ASSDEBUGF("%lf %lf %lf", gsl_vector_get(pVector, 0), gsl_vector_get(pVector, 1), gsl_vector_get(pVector, 2));
}
void BasicMathPlugin::Dump3x3(const char *Label, gsl_matrix *pMatrix)
{
ASSDEBUGF("Matrix dump - %s", Label);
ASSDEBUGF("Row 0 %lf %lf %lf", gsl_matrix_get(pMatrix, 0, 0), gsl_matrix_get(pMatrix, 0, 1),
gsl_matrix_get(pMatrix, 0, 2));
ASSDEBUGF("Row 1 %lf %lf %lf", gsl_matrix_get(pMatrix, 1, 0), gsl_matrix_get(pMatrix, 1, 1),
gsl_matrix_get(pMatrix, 1, 2));
ASSDEBUGF("Row 2 %lf %lf %lf", gsl_matrix_get(pMatrix, 2, 0), gsl_matrix_get(pMatrix, 2, 1),
gsl_matrix_get(pMatrix, 2, 2));
}
/// Use gsl to compute the determinant of a 3x3 matrix
double BasicMathPlugin::Matrix3x3Determinant(gsl_matrix *pMatrix)
{
gsl_permutation *pPermutation = gsl_permutation_alloc(3);
gsl_matrix *pDecomp = gsl_matrix_alloc(3, 3);
int Signum;
double Determinant;
gsl_matrix_memcpy(pDecomp, pMatrix);
gsl_linalg_LU_decomp(pDecomp, pPermutation, &Signum);
Determinant = gsl_linalg_LU_det(pDecomp, Signum);
gsl_matrix_free(pDecomp);
gsl_permutation_free(pPermutation);
return Determinant;
}
/// Use gsl to compute the inverse of a 3x3 matrix
bool BasicMathPlugin::MatrixInvert3x3(gsl_matrix *pInput, gsl_matrix *pInversion)
{
bool Retcode = true;
gsl_permutation *pPermutation = gsl_permutation_alloc(3);
gsl_matrix *pDecomp = gsl_matrix_alloc(3, 3);
int Signum;
gsl_matrix_memcpy(pDecomp, pInput);
gsl_linalg_LU_decomp(pDecomp, pPermutation, &Signum);
// Test for singularity
if (0 == gsl_linalg_LU_det(pDecomp, Signum))
{
Retcode = false;
}
else
gsl_linalg_LU_invert(pDecomp, pPermutation, pInversion);
gsl_matrix_free(pDecomp);
gsl_permutation_free(pPermutation);
return Retcode;
}
/// Use gsl blas support to multiply two matrices together and put the result in a third.
/// For our purposes all the matrices should be 3 by 3.
void BasicMathPlugin::MatrixMatrixMultiply(gsl_matrix *pA, gsl_matrix *pB, gsl_matrix *pC)
{
// Zeroise the output matrix
gsl_matrix_set_zero(pC);
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, pA, pB, 0.0, pC);
}
/// Use gsl blas support to multiply a matrix by a vector and put the result in another vector
/// For our purposes the the matrix should be 3x3 and vector 3.
void BasicMathPlugin::MatrixVectorMultiply(gsl_matrix *pA, gsl_vector *pB, gsl_vector *pC)
{
// Zeroise the output vector
gsl_vector_set_zero(pC);
gsl_blas_dgemv(CblasNoTrans, 1.0, pA, pB, 0.0, pC);
}
bool BasicMathPlugin::RayTriangleIntersection(TelescopeDirectionVector &Ray, TelescopeDirectionVector &TriangleVertex1,
TelescopeDirectionVector &TriangleVertex2,
TelescopeDirectionVector &TriangleVertex3)
{
// Use Möller-Trumbore
//Find vectors for two edges sharing V1
TelescopeDirectionVector Edge1 = TriangleVertex2 - TriangleVertex1;
TelescopeDirectionVector Edge2 = TriangleVertex3 - TriangleVertex1;
TelescopeDirectionVector P = Ray * Edge2; // cross product
double Determinant = Edge1 ^ P; // dot product
double InverseDeterminant = 1.0 / Determinant;
// If the determinant is negative the triangle is backfacing
// If the determinant is close to 0, the ray misses the triangle
if ((Determinant > -std::numeric_limits<double>::epsilon()) &&
(Determinant < std::numeric_limits<double>::epsilon()))
return false;
// I use zero as ray origin so
TelescopeDirectionVector T(-TriangleVertex1.x, -TriangleVertex1.y, -TriangleVertex1.z);
// Calculate the u parameter
double u = (T ^ P) * InverseDeterminant;
if (u < 0.0 || u > 1.0)
//The intersection lies outside of the triangle
return false;
//Prepare to test v parameter
TelescopeDirectionVector Q = T * Edge1;
//Calculate v parameter and test bound
double v = (Ray ^ Q) * InverseDeterminant;
if (v < 0.0 || u + v > 1.0)
//The intersection lies outside of the triangle
return false;
double t = (Edge2 ^ Q) * InverseDeterminant;
if (t > std::numeric_limits<double>::epsilon())
{
//ray intersection
return true;
}
// No hit, no win
return false;
}
} // namespace AlignmentSubsystem
} // namespace INDI
|