! Tests LayerT5 Ellipsoids
!
! @bug      NXM-3934 LayerT5 Ellipsoid Support
! @since    4.1.2
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  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)
  switch INDEPENDENT  independent GET DEF=FALSE SDEF=TRUE  ! have XYZ and GEO plot support independent gc values

  call setDatDir
  invoke doubleMax java.lang.Double.MAX_VALUE

  ! *************** IMPORTANT LayerT5.setPoint USAGE INFORMATION *********************
  !
  ! 'Creating' a T5 layer via LayerT5.setPoint(Table) requires the layer be intialized
  ! with a T5000 file. This sets LayerT5 up to be able to handle point updates and the
  ! appropriate range updates (critical to 3DGRAPH and XY views). Here are the steps:
  ! 1) Create a initial T5000 file with a temporary point that has checked/critical 
  !    fields be the same. Note: If ellipsoids end up being handled like ellipses,
  !    the file created below will need an ELLIPSOID (or whatever name we go with) entry.
  ! 2) Next, clear the layerT5 with the clear() method
  ! 3) Add points via LayerT5's setPoint(Table) method
  !**********************************************************************************
  ! creation of starter T5000 file
  set t:ellipsoid5000_data {}
  set t:ellipsoid5000_data.0 {POS=(0.0,0,0),NAME="TEMP"}
  header/create ram:t5_starter.tmp TYPE=5000 RF=ECR COMPONENTS=(POS/VD/GEODETIC/DISTANCE_M,NAME/1A/UNDEFINED/NONE)
  file open/d/a out ram:t5_starter.tmp
    invoke ,, out.setData(0,ellipsoid5000_data.0)
  file close out

  if interAct then
    set instructions "Click 'Manually Add Ellipsoid'"

    ! define each layers' settings (including data type, autoselect, and select messages being enabled)
    set layerXYZ "LT=T5,NAME=XYZDATA,DATATYPE="XYZ",AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE"
    set layerGEO "LT=T5,NAME=GEODATA,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE"

    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 4 /title

      ! 3DGRAPH/XYZ plot with custom axis labels
      !   1) regular readout and axis numbers are disabled, as they display the -1.2 to 1.2 values like XYZ view and are useless
      !   2) extended readout point counts could be enabled if this were part of a LayerDrawable
      plot/id=xyzplot/msgid=main t5_starter.tmp{Layer={^layerXYZ}} view=3DGRAPH &
             AXIS=-RNUMBERS|-BNUMBERS xlabel=EAST ylabel=NORTH zlabel=UP
      
      ! GEODETIC plot
      !   Note: Real usage would be best if all the layerT5 layers were part of a LayerDrawable
      !   That would allow point selection to be coordinated among layers and the point count to show on the extended readout
      !   Additional information: You have a layer or layers of symbols too (currently only available via setPoint(Table) API
      plot/id=geoplot/msgid=main world|t5_starter.tmp{Layer={^layerGEO}} view=GEODETIC readout=+OnAxis

      gcontrol label macro "Global Controls"
      gcontrol/nc=1 button ADDELLIPSOID "" "Manually Add Ellispoid"

      ! XYZ plot controls (can be independent or heavily linked to GEO)
      gcontrol label macro "XYZ Ellipsoid Settings"
        gcontrol DVAL posX "POS X" P1=0 P2=-1 P3=-1
        gcontrol DVAL posy "POS Y" P1=0 P2=-1 P3=-1
        gcontrol DVAL posZ "POS Z" P1=0 P2=0 P3=-1
        IF INDEPENDENT ISTRUE
          gcontrol DVAL semiX "SEMI X" P1=1 P2=0 P3=-1
          gcontrol DVAL semiY "SEMI Y" P1=2 P2=0 P3=-1
          gcontrol DVAL semiZ "SEMI Z" P1=4 P2=0 P3=-1
          gcontrol DVAL rotX "ROTATION X" P1=45 P2=0 P3=180
          gcontrol DVAL rotY "ROTATION Y" P1=10 P2=0 P3=180
          gcontrol DVAL rotZ "ROTATION Z" P1=20 P2=0 P3=180
        ENDIF

      ! GEO plot controls
      gcontrol label macro "GEO Ellisoid Settings"
      gcontrol DVAL posLat "POS LAT" P1=0 P2=-1 P3=-1
      gcontrol DVAL posLon "POS LON" P1=0 P2=-1 P3=-1
      gcontrol DVAL posAlt "POS ALT" P1=1000000 P2=0 P3=-1
      gcontrol DVAL semiE "SEMI E" P1=100000 P2=0 P3=-1
      gcontrol DVAL semiN "SEMI N" P1=1000000 P2=0 P3=-1
      gcontrol DVAL semiU "SEMI U" P1=50000 P2=0 P3=-1
      gcontrol DVAL rotE "ROTATION E" P1=0 P2=0 P3=180
      gcontrol DVAL rotN "ROTATION N" P1=0 P2=0 P3=180
      gcontrol DVAL rotU "ROTATION U" P1=0 P2=0 P3=180

      ! SHELLGUI for message display
      shellgui/id=idshellgui/attach/rows=15

    pipe off

    %test_plot_layert5_ellipsoid_t5000
    %test_plot_layert5_ellipsoid_csv
  endif
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqss "MARK" or msg.name EQS "PANOBS"
  elseif msg.name eqss "ADDELLIPSOID"
    call addEllipsoid
  elseif msg.name EQS "SELECT" OR msg.name EQS "DESELECT" then
   info "^msg.name MESSAGE"
   say "^msg.name ^msg.data"
  else
    !say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg

  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  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

!*****************************************************************************
! add Ellipsoids
!*****************************************************************************
procedure addEllipsoid
  ! get layers
  res xyzLay reg.xyzplot.layers.xyzdata
  res geoLay reg.geoplot.layers.geodata
  ! delete "starter" point
  invoke ,, xyzLay.clear()
  invoke ,, geoLay.clear()
  ! Set labels for XYZ plot Ctrl Click POPUP (defaults to X,Y,Z)
  res t:labels {POS={X="East",Y="North",Z="Up"}}
  invoke ,, xyzLay.setPopupLabels(labels)

  ! ****************  Add Ellipsoids **********************
  ! XYZ Ellipsoid
  IF INDEPENDENT ISTRUE
    res t:symTableXYZ {POS={X=^{gc.PosX.value},Y=^{gc.PosY.value},Z=^{gc.PosZ.value}},NAME="Rich",&
      ELPSOID={SEMIMAJOR=^{gc.semiX.value},SEMIMINOR=^{gc.semiY.value},SEMIMEDIAN=^{gc.semiZ.value},&
      ROTATION_X=^{gc.rotX.value},ROTATION_Y=^{gc.rotY.value},ROTATION_Z=^{gc.rotZ.value}}}
  ELSE
    res t:symTableXYZ {POS={X=^{gc.PosX.value},Y=^{gc.PosY.value},Z=^{gc.PosZ.value}},NAME="Rich",&
      ELPSOID={SEMIMAJOR=^{gc.semiE.value},SEMIMINOR=^{gc.semiN.value},SEMIMEDIAN=^{gc.semiU.value},&
      ROTATION_X=^{gc.rotE.value},ROTATION_Y=^{gc.rotN.value},ROTATION_Z=^{gc.rotU.value}}}
  ENDIF
  ! GEO Ellipsoid
  res t:symTableGEO {POS={LON=^{gc.PosLon.value},LAT=^{gc.PosLat.value},ALT=^{gc.PosAlt.value}},NAME="JAY",&
      ELPSOID={AXIS1=^{gc.semiE.value},AXIS2=^{gc.semiN.value},AXIS3=^{gc.semiU.value},&
      ANGLE1=^{gc.rotE.value},ANGLE2=^{gc.rotN.value},ANGLE3=^{gc.rotU.value}}}
  invoke ,, xyzLay.setPoint(-1,symTableXYZ)
  invoke ,, geoLay.setPoint(-1,symTableGEO)

return
