to select-3d-model
  let selected-file user-file
  if selected-file != false [
    set model-path selected-file
  ]

end


to open-3d-model [current-model-file should-create]

  if current-model-file = "" [
   user-message "Please select a 3D model"
   stop
  ]

  ; clear world
   clear-all
   reset-ticks
   reset-timer
    set total-points 0

  ; create X, Y and Z axis
  if show-axis = true [
  create-turtles 1 [ set shape "line"
          set heading 90
          set color red
          set size world-width
          stamp
          die ]
  create-turtles 1 [ set shape "line"
          set color yellow
          set heading 0
          set size world-height
          stamp
          die ]
  create-turtles 1 [ set shape "line"
          set pitch 90
          set color blue
          set size world-depth
          stamp
          die ]

  ; Draw X, Y and Z axis line
  ask patch max-pxcor 0 0 [ set plabel "x-axis"  ]
  ask patch 0 max-pycor 0 [ set plabel "y-axis" ]
  ask patch 0 0 max-pzcor [ set plabel "z-axis" ]

  ]

  ; init temp variables
  set max_x -10000
  set min_x 10000
  set max_y -10000
  set min_y 10000
  set max_z -10000
  set min_z 10000
  set total-points 0
  let count1 0

  ; set current model-file variable to current-model-file
  set model-path current-model-file

  ; open the csv file
  file-open current-model-file
  let result csv:from-row file-read-line

  print (word "Starting importing..." current-model-file)
  while [ not file-at-end? ] [

    set count1 count1 + 1

    let row csv:from-row file-read-line
    ifelse count1 mod sample-rate != 0 [ ; use a point per sample-rate
      ; print("pass")
      ; pass
    ]
    [
    ; print(row)
    ; read points (x,y,z)
    let x item 1 row
    let y item 2 row
    let z item 3 row
    ; identify world size
    if x > max_x [ set max_x x]
    if x < min_x [ set min_x x]
    if y > max_y [ set max_y y]
    if y < min_y [ set min_y y]
    if z > max_z [ set max_z z]
    if z < min_z [ set min_z z]

    ; create an agent for each point
    if should-create = true [
    create-turtles 1 [
      set shape "dot"
      set xcor x * scale
      set ycor y * scale
      set zcor z * scale
      set size point-size
      set color blue
      set object-type "human"
    ]
      ]

      ; counter the total number of points
       set total-points total-points + 1
    ]


  ]
  file-close

  ; show world size [x, y, z]
  print (word "point counts: " total-points)
  print (word "x range: ["   min_x "," max_x "]")
  print (word "y range: ["   min_y "," max_y "]")
  print (word "z range: ["   min_z "," max_z "]")

  ; print (word total-points "\t" sample-rate "\t" timer "\t" scale  "\t["   precision min_x 2 "," precision max_x 2 "]" "\t["  precision min_y 2 "," precision max_y 2 "]" "\t["   precision min_z 2 "," precision max_z 2 "]")
  print (word total-points "\t" sample-rate "\t" timer "\t" scale  "\t"  precision (max_x - min_x) 2 "\t" precision (max_y - min_y) 2  "\t"  precision (max_z - min_z) 2 )
  ;
  print("Finished Importing!")

      set world-min-x int (min_x * scale * world-scale)
    set world-max-x int  (max_x * scale *  world-scale)
    set world-min-y int (min_y * scale * world-scale )
    set world-max-y int (max_y * scale * world-scale )
    set world-min-z int (min_z * scale * world-scale )
    set world-max-z int (max_z * scale * world-scale )

      ifelse world-max-x > 70 or world-max-y > 70 or world-max-z > 70 or world-min-x < -70 or world-min-y < -70 or world-min-z < -70  [
      user-message "world size may be too large? We have stopped resizing world size. you can manually set the world size later!"
    ][
       resize-world world-min-x world-max-x  world-min-y  world-max-y    world-min-z   world-max-z
    ]

end

to infer-world-size
   open-3d-model model-path false
   resize-world world-min-x  world-max-x world-min-y  world-max-y  world-min-z  world-max-z

end

to define-world-size
   resize-world world-min-x  world-max-x world-min-y  world-max-y  world-min-z  world-max-z
end