!******************************************************************************!
! Test drawing of a polygon in Layert5 and GeodeticUtil.insidePolygon
! on dateline crossing cases
! Calls GeodeticUtil.insidePolygon with these polys
! color point feature green if inside and red if outside
!
! Arguments: None
!
! @issue    NXM-3779 GeodeticUtil.insidePolygon
! @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)

  call setDatDir

  if interAct then
    set instructions "DR #827438-3 GeodeticUtil.insidePolygon would show.\n\n&
                      TOP LEFT: North Pole as outside of the polygon\n&
                      TOP RIGHT: North Pole as inside of the polygon\n&
                      Note: The polygon on the right is just a slightly rotated version of the one on the left.\n\n&
                      BOTTOM LEFT: the point shown as outside of the polygon\n&
                      BOTTOM RIGHT: the point shown as inside of the polygon\n&
                      Note: The polygon on the right is just a slightly rotated version of the one on the left.\n\n&
                      CLICK 'Check With Fix' and the tests should pass and ALL Features be GREEN&
                      ."

    call createFiles
    res z:fix_enabled false
    invoke ,, nxm.sys.lib.GeodeticUtil.setinsidePolyFix(fix_enabled)

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

      plot/id="NP1"   ^{datDir}.world.prm|ram:north_pole_1 View=GEO
      plot/id="NP2"   ^{datDir}.world.prm|ram:north_pole_2 View=GEO
      plot/id="DATEL1" ^{datDir}.world.prm|ram:dateline_lat0_1 View=GEO
      plot/id="DATEL2" ^{datDir}.world.prm|ram:dateline_lat0_2 View=GEO
      gcontrol/toggle button check "CheckInside" "Check With Fix, Check Without Fix"
      call addFeatures
      call rotatePlots
    pipe off

    %test_plot_layert5_polygon_edit
  endif
  ! make sure fix is active!
  res z:fix_enabled true
  invoke ,, nxm.sys.lib.GeodeticUtil.setinsidePolyFix(fix_enabled)
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 eqs "CHECK" then
    if msg.data EQS "CHECK WITH FIX" THEN
      res z:fix_enabled true
    else 
      res z:fix_enabled false
    endif
    invoke ,, nxm.sys.lib.GeodeticUtil.setinsidePolyFix(fix_enabled)
    call checkPoints
  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

!******************************************************************************!
! create the test files
!******************************************************************************!
procedure createFiles

    ! create the Type 5000 file to display
    header/create ram:north_pole_1 TYPE=5000 SR=(NAME/4A,POS/VD,FLAG/8B,CONN/SB)
    headermod ram:north_pole_1 RF=ECR

    file open/d/w out ram:north_pole_1   ! open the file for editing

    ! define the polygon
    res t:poly {}
    res poly.row0 {name="POINT_0",POS=(0.0, 77.0, 95.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row1 {name="POINT_1",POS=(0.0, 75.0,160.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row2 {name="POINT_2",POS=(0.0, 80.0,-81.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row3 {name="POINT_3",POS=(0.0, 85.0,-22.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row4 {name="POINT_4",POS=(0.0, 80.0, 47.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row5 {name="POINT_0",POS=(0.0, 77.0, 95.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}

    ! update the file
    foreach row INTABLE/V poly
      invoke ,, out.writeDataTable(row)
    endfor

    file close out         ! cleanup resource

    ! create the Type 5000 file to display
    header/create ram:north_pole_2 TYPE=5000 SR=(NAME/4A,POS/VD,FLAG/8B,CONN/SB)
    headermod ram:north_pole_2 RF=ECR

    file open/d/w out ram:north_pole_2   ! open the file for editing

    ! define the polygon
    res t:poly {}
    res poly.row0 {name="POINT_0",POS=(0.0, 77.0,  75.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row1 {name="POINT_1",POS=(0.0, 75.0, 140.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row2 {name="POINT_2",POS=(0.0, 80.0,-101.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row3 {name="POINT_3",POS=(0.0, 85.0, -42.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row4 {name="POINT_4",POS=(0.0, 80.0,  27.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row5 {name="POINT_0",POS=(0.0, 77.0,  75.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}

    ! update the file
    foreach row INTABLE/V poly
      invoke ,, out.writeDataTable(row)
    endfor

    file close out         ! cleanup resource

    ! create the Type 5000 file to display
    header/create ram:dateline_lat0_1 TYPE=5000 SR=(NAME/4A,POS/VD,FLAG/8B,CONN/SB)
    headermod ram:dateline_lat0_1 RF=ECR

    file open/d/w out ram:dateline_lat0_1   ! open the file for editing

    ! define the polygon
    res t:poly {}
    res poly.row0 {name="POINT_0",POS=(0.0,  20.0, 170.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row1 {name="POINT_1",POS=(0.0,  20.0, -85.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row2 {name="POINT_2",POS=(0.0, -20.0, -85.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row3 {name="POINT_3",POS=(0.0, -20.0, 170.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row4 {name="POINT_4",POS=(0.0,  20.0, 170.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}

    ! update the file
    foreach row INTABLE/V poly
      invoke ,, out.writeDataTable(row)
    endfor

    file close out         ! cleanup resource

    ! create the Type 5000 file to display
    header/create ram:dateline_lat0_2 TYPE=5000 SR=(NAME/4A,POS/VD,FLAG/8B,CONN/SB)
    headermod ram:dateline_lat0_2 RF=ECR

    file open/d/w out ram:dateline_lat0_2   ! open the file for editing

    ! define the polygon
    res t:poly {}
    res poly.row0 {name="POINT_0",POS=(0.0, 20.0,  150.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row1 {name="POINT_1",POS=(0.0, 20.0, -105.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row2 {name="POINT_2",POS=(0.0,-20.0, -105.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row3 {name="POINT_3",POS=(0.0,-20.0,  150.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}
    res poly.row4 {name="POINT_4",POS=(0.0, 20.0,  150.0),FLAG=(0,0,2,0,-9,0,1,1),CONN=1}

    ! update the file
    foreach row INTABLE/V poly
      invoke ,, out.writeDataTable(row)
    endfor

    file close out         ! cleanup resource
return

!******************************************************************************!
! add the features
!******************************************************************************!
procedure addFeatures

  feature label=north_pole_1 table={NAME="NORTH_POLE_1",TYPE=SYMBOL,X=0,Y=90,&
                                  COLOR=RED,LINE={SYMBOL="UTRI"}} plot=np1
  feature label=north_pole_2 table={NAME="NORTH_POLE_2",TYPE=SYMBOL,X=0,Y=90,&
                                  COLOR=GREEN,LINE={SYMBOL="LTRI"}} plot=np2
  feature label=dateline_1 table={NAME="DATELINE1",TYPE=SYMBOL,X=-170,Y=0,&
                                  COLOR=RED,LINE={SYMBOL="DTRI"}} plot=datel1
  feature label=dateline_2 table={NAME="DATELINE2",TYPE=SYMBOL,X=-170,Y=0,&
                                  COLOR=GREEN,LINE={SYMBOL="RTRI"}} plot=datel2
return

!******************************************************************************!
! rotate the plots
!******************************************************************************!
procedure rotatePlots

  invoke rotateNP nxm.sys.test.TestUtil.createDoubleArray("0.0,90.0,6")
  message send NP1  name=PANOBS info=-7  DATA=rotateNP
  message send NP2  name=PANOBS info=-7  DATA=rotateNP
  invoke rotateDL nxm.sys.test.TestUtil.createDoubleArray("220,0.0,6")
  message send DATEL1  name=PANOBS info=-7  DATA=rotateDL
  message send DATEL2  name=PANOBS info=-7  DATA=rotateDL
return

!******************************************************************************!
! check the values
!******************************************************************************!
procedure checkPoints
  res dim 2
  new nxm.sys.lib.Position() northPole
  invoke ,, northPole.setGeo(0,90,0)

  invoke arraypts reg.np1.layers.north_pole_1.getArrayPoints(dim)
  invoke z:np1_inside nxm.sys.lib.GeodeticUtil.insidePolygon(northPole,dim,arraypts)
  if np1_inside then
    res reg.np1.features.north_pole_1.color GREEN
  else
    res reg.np1.features.north_pole_1.color RED
  endif
  if fix_enabled then
    assert/text="^reg.main.name: Top Left North Pole is inside" np1_inside ISTRUE
  endif

  invoke arraypts reg.np2.layers.north_pole_2.getArrayPoints(dim)
  invoke z:np2_inside nxm.sys.lib.GeodeticUtil.insidePolygon(northPole,dim,arraypts)
  if np2_inside then
    res reg.np2.features.north_pole_2.color GREEN
  else
    res reg.np2.features.north_pole_2.color RED
  endif
  if fix_enabled then
    assert/text="^reg.main.name: Top Right North Pole is inside" np2_inside ISTRUE
  endif

  new nxm.sys.lib.Position() dateline
  invoke ,, dateline.setGeo(0,0,-170)

  invoke arraypts reg.datel1.layers.dateline_lat0_1.getArrayPoints(dim)
  invoke z:dl1_inside nxm.sys.lib.GeodeticUtil.insidePolygon(dateline,dim,arraypts)
  if dl1_inside then
    res reg.datel1.features.dateline1.color GREEN
  else
    res reg.datel1.features.dateline1.color RED
  endif
  if fix_enabled then
    assert/text="^reg.main.name: Bottom Left DateLine is inside" dl1_inside ISTRUE
  endif

  invoke arraypts reg.datel2.layers.dateline_lat0_2.getArrayPoints(dim)
  invoke z:dl2_inside nxm.sys.lib.GeodeticUtil.insidePolygon(dateline,dim,arraypts)
  if dl2_inside then
    res reg.datel2.features.dateline2.color GREEN
  else
    res reg.datel2.features.dateline2.color RED
  endif
    if fix_enabled then
    assert/text="^reg.main.name: Bottom Right DateLine is inside" dl2_inside ISTRUE
  endif

return
