!******************************************************************************!
! Test the drawing of an ellipse  
!
! Arguments: Double values for the position lat, position lon, ellipse semi major,
!            ellipse semi minor, and ellipse tilt
!
! @bug      2471 GeodeticUtil: ComputeEllipseLL(..) returns wrong tilt angle
! @author:  JC4, J Bauer
!******************************************************************************!
startmacro D:posLat[20.01] D:posLon[10.001] D:elpsLat[20.02] D:elpsLon[10.002] D:elpsSemiMaj[2250.0] D:elpsSemiMin[200.0] D:elpsTilt[172.0]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)

  call setDatDir

  if interAct then
    set instructions "The ellipse should be red &
                      and should overlap the green numbers."

    call setupEllipses
    
    pipe on
    
      panel/msgid=MAIN/setup/controls=gc
        gcontrol label macro "Test MACRO"
        gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

        gcontrol text  idtext "Instructions" instructions 24 8 /title
    
      plot/msgid=main ^{datDir}.world.prm|expected_results{aux=RAM,layer={color="RED"}}|actual_results{aux=ram} &
       x1=9.99 x2=10.03 y1=19.99 y2=20.05 view=Latlon 
         
    pipe off
         
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
procedure processMessage m:msg
!say "got msg ^msg data= ^msg.data"
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Set up the ellipses 
procedure setupEllipses
     calc tilt elpsTilt       ! What it should be
    !calc tilt 90 elpsTilt - ! should not need to do this...

    invoke pos    nxm.sys.lib.Position.fromGeo(0.0,posLat,posLon)
    invoke elpsPos nxm.sys.lib.Position.fromGeo(0.0,elpsLat,elpsLon)
    invoke ellipse nxm.sys.lib.GeodeticUtil.computeMapEllipse(elpsLon,elpsLat,elpsSemiMaj,elpsSemiMin,tilt,-1)
    invoke inside  nxm.sys.lib.GeodeticUtil.insidePolygon(pos,2,ellipse)
    invoke course  nxm.sys.lib.GeodeticUtil.computeCourse(elpsPos,pos)
    
    !say "Pos      : ^pos"
    !say "elpsPos  : ^elpsPos"
    !say "ellipse  : ^ellipse"
    !say "inside   : ^inside"
    !say "course   : ^course"
    
    file open/d/n exp_out expected_results{aux=RAM,type=5000,sr=(NAME/4A,POS/VD,ELPS/VD) }
    file open/d/n act_out actual_results{aux=RAM,type=5000,sr=(NAME/4A,POS/VD) }
    
    res t:row {S:NAME="EXPECTED",D:POS=(0.0,^elpsLat,^elpsLon),D:ELPS=(^elpsSemiMaj,^elpsSemiMin,^elpsTilt) }
    invoke ,, exp_out.writeDataTable(row)
    
    set i 0
    set deg elpsTilt
    while i < 182
      calc j i 1 +
      set lon ellipse(^i)
      set lat ellipse(^j)
      
      invoke p       nxm.sys.lib.Position.fromGeo(0.0,lat,lon)
      invoke dist    nxm.sys.lib.GeodeticUtil.computeDistance(elpsPos,p)
      invoke ang     nxm.sys.lib.GeodeticUtil.computeCourse(elpsPos,p)
      
      !say " ^i : p=^p dist=^dist ang=^ang deg=^deg"
      
      res t:row {S:NAME="^i",D:POS=(0.0,^lat,^lon) }
      invoke ,, act_out.writeDataTable(row)
      
      calc L:i i 2 +
      calc deg deg 4 + 360 mod
    endwhile
    
    file close exp_out
    file close act_out

return
