/*
  X d e s y
  ---------

  Modul:    mein_testdll01.cpp
  Version:  1.000
  Datum:    21.04.2008
  Autor:    F. Kern

  Beschreibung:

  Beispielanwendung für die Xdesy-Ansteuerungs-Bilbliothek Xdesy_Interface.dll

  Richtungsnetz: 
    Unbekannte:        P
    Beobachtungstypen: H

  f.kern@xdesy.de
  www.xdesy.de
*/

#pragma argsused

#ifdef __BORLANDC__
#include <vcl.h>   // add for Borland Compiler, Turbo C++, etc.
#endif

#include <stdio.h>
#include <math.h>

#include "xdesy_interface.h"


/*
; goesch.mkr
; Kahmen, H.: Vermessungskunde II, Sammlung Goeschen, 14. Aufl.;
;       W. de Gruyter, Berlin, New York 1986, S.212-215
;
;
; Kapitel: 5.4.6 Mehrfaches Rueckwaertseinschneiden durch Ausgleichung
;
s H 0.001 0.0

P  1  0 6531.28  48177.62
P  2  0 7185.19  49600.15
P  3  0 5670.69  49830.93
P  4  0 5077.24  47863.91
P  N -3 6059.0   48565.2

S N -1 0.0
M 1 H 356.2465
M 2 H  47.3114
M 3 H 118.9497
M 4 H 239.4920

*/


void callbackfunc_report_warnings(char *str)
{
  printf("REPORT-W: %s\n",str);
}

void callbackfunc_report_errors(char *str)
{
  printf("REPORT-E: %s\n",str);
}

int main(int argc, char* argv[])
{
  char str[512];
  int  P[5];
  int  S[1];
  int  CLSS[1];
  int  M[4];
  int  ret;

  printf("%s",xdesyAbout(str));

//  xdesyOpen("DLL-Test","F.Kern","no comment",callbackfunc_report_warnings,callbackfunc_report_errors);
  xdesyOpen("DLL-Test","F.Kern","no comment",NULL,callbackfunc_report_errors);

  P[0] = xdesyGKCoord2D("1","00",48177.62,6531.28);
  P[1] = xdesyGKCoord2D("2","00",49600.15,7185.19);
  P[2] = xdesyGKCoord2D("3","00",49830.93,5670.69);
  P[3] = xdesyGKCoord2D("4","00",47863.91,5077.24);
  P[4] = xdesyGKCoord2D("N","11",    0.0 ,    0.0);

  CLSS[0] = xdesyMeasurementClass("H",0.001,0.0);

  S[0] = xdesyStation(P[4],"1",0.0);

  M[0] = xdesyRelMeasurement(S[0],P[0],CLSS[0],356.2465,0.0);
  M[1] = xdesyRelMeasurement(S[0],P[1],CLSS[0], 47.3114,0.0);
  M[2] = xdesyRelMeasurement(S[0],P[2],CLSS[0],118.9497,0.0);
  M[3] = xdesyRelMeasurement(S[0],P[3],CLSS[0],239.4920,0.0);

  ret = xdesySolve("-n -a -i");
  if (ret)
  {
    double  xyz[2];
    double sxyz[2];

    printf("Koordinaten\n");
    for (int i=0;i<5;i++)
    {
      xdesyGetResultGKCoord(P[i],2,xyz,sxyz);
      printf("%d %lf %lf %lf %lf\n",i,xyz[0],xyz[1],sxyz[0],sxyz[1]);
    }

    double qxx[3*3];
     double A[3],B[3],C[3];
    for (int i=4;i<5;i++) // Neupunkte
    {
      xdesyGetKovarianzGKCoord(P[i],2,qxx);
      printf("%d %12.6lf %12.6lf\n",i,sqrt(qxx[0*3+0]),qxx[0*3+1]/sqrt(qxx[0*3+0])/sqrt(qxx[1*3+1]));
      printf("%d %12s %12.6lf\n"   ,i,""              ,sqrt(qxx[1*3+1]));


      xdesyKonfidenzEllipsoid(qxx,A,B,C);

      printf("%d Halbachse A Laenge=%12.6lf Hz=%12.6lf V=%12.6lf\n",i,A[0],A[1],A[2]);
      printf("%d Halbachse B Laenge=%12.6lf Hz=%12.6lf V=%12.6lf\n",i,B[0],B[1],B[2]);
      printf("%d Halbachse C Laenge=%12.6lf Hz=%12.6lf V=%12.6lf\n",i,C[0],C[1],C[2]);
    }

    double ori;
    double sori;

    printf("Standpunkte\n");
    for (int i=0;i<1;i++)
    {
      xdesyGetResultStation(S[i],&ori,&sori);
      printf("%d %lf %lf\n",i,ori,sori);
    }


    double v;
    double spost;
    double r;
    double nv;
    int    ret;

    printf("Messwerte\n");
    for (int i=0;i<4;i++)
    {
      ret = xdesyGetResultMeasurement(M[i],&v,&spost,&r,&nv);
      if (ret==0)
      {
        printf("%2s %d %lf %lf %lf %lf\n","",i,v,spost,r,nv);
      }
      else
      {
        printf("%2s %d %lf %lf %lf\n","*",i,v,spost,r,nv);
      }
    }

    int    cnt;
    double s;

    printf("Standardabweichung der Gewichtseinheit\n");
    xdesyGetResultMeasurementClass(0,&cnt,&s);
    printf("Gewichtseinheit %d %lf\n",cnt,s);

    printf("Varianzkomponenten\n");
    for (int i=0;i<1;i++)
    {
      xdesyGetResultMeasurementClass(CLSS[i],&cnt,&s);
      printf("%d %d %lf\n",i,cnt,s);
    }
  }
  xdesyClose();
  return 0;
}
//---------------------------------------------------------------------------
