|
| 1 | +function round(n) return math.floor(n + 0.5) end |
| 2 | + |
| 3 | +function angleRound(angle) |
| 4 | + local remainder = math.abs(angle % 90) |
| 5 | + if remainder > 45 then |
| 6 | + return 90 - remainder |
| 7 | + else |
| 8 | + return remainder |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +-- Returns an integer that we need to subtract from width/height |
| 13 | +-- due to camera rotation issues. |
| 14 | +function cropAmount(width, height, angle) |
| 15 | + local absAngle = angleRound(angle or 0) |
| 16 | + if (absAngle > 0) then |
| 17 | + local x = (5.61 - 0.095 * math.pow(absAngle, 2) + 9.06 * absAngle) |
| 18 | + local factor = x / 640 |
| 19 | + local longEdge = math.max(width, height) |
| 20 | + local result = round(longEdge * factor) |
| 21 | + return result |
| 22 | + end |
| 23 | + return 0 |
| 24 | +end |
| 25 | + |
| 26 | +function fwe(key) |
| 27 | + local e = env("CAMERA_CALIBRATION_" .. key) |
| 28 | + if e then |
| 29 | + return tonumber(e) |
| 30 | + else |
| 31 | + send_message("error", "You must first run camera calibration", "toast") |
| 32 | + os.exit() |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +local cam_rotation = fwe("total_rotation_angle") |
| 37 | +local scale = fwe("coord_scale") |
| 38 | +local z = fwe("camera_z") |
| 39 | +local raw_img_size_x_px = fwe("center_pixel_location_x") * 2 |
| 40 | +local raw_img_size_y_px = fwe("center_pixel_location_y") * 2 |
| 41 | +local raw_img_size_x_mm = raw_img_size_x_px * scale |
| 42 | +local raw_img_size_y_mm = raw_img_size_y_px * scale |
| 43 | +local margin_mm = cropAmount(raw_img_size_x_px, raw_img_size_y_px, cam_rotation) |
| 44 | +local cropped_img_size_x_mm = raw_img_size_x_mm - margin_mm |
| 45 | +local cropped_img_size_y_mm = raw_img_size_y_mm - margin_mm |
| 46 | +if math.abs(cam_rotation) < 45 then |
| 47 | + x_spacing_mm = cropped_img_size_x_mm |
| 48 | + y_spacing_mm = cropped_img_size_y_mm |
| 49 | +else |
| 50 | + x_spacing_mm = cropped_img_size_y_mm |
| 51 | + y_spacing_mm = cropped_img_size_x_mm |
| 52 | +end |
| 53 | +local grid_size_x_mm = garden_size().x - x_spacing_mm |
| 54 | +local y_grid_size_mm = garden_size().y - y_spacing_mm |
| 55 | +local x_grid_points = math.ceil(grid_size_x_mm / x_spacing_mm) |
| 56 | +local y_grid_points = math.ceil(y_grid_size_mm / y_spacing_mm) |
| 57 | +local total = (x_grid_points + 1) * (y_grid_points + 1) |
| 58 | +local x_grid_start_mm = (x_spacing_mm / 2) |
| 59 | +local y_grid_start_mm = (y_spacing_mm / 2) |
| 60 | +local x_offset_mm = fwe("camera_offset_x") |
| 61 | +local y_offset_mm = fwe("camera_offset_y") |
| 62 | + |
| 63 | +local each = function(callback) |
| 64 | + local y = 0 |
| 65 | + local count = 0 |
| 66 | + for x_grid_index = 0, x_grid_points do |
| 67 | + for y_grid_points = 0, y_grid_points do |
| 68 | + count = count + 1 |
| 69 | + local y_temp1 = (y_spacing_mm * y_grid_points) |
| 70 | + if (x_grid_index % 2) == 0 then |
| 71 | + y = (y_grid_size_mm + (y_temp1 - y_offset_mm)) |
| 72 | + else |
| 73 | + y = (y_grid_size_mm - (y_temp1 - y_offset_mm)) |
| 74 | + end |
| 75 | + callback({ |
| 76 | + x = (x_grid_start_mm + (x_spacing_mm * x_grid_index) - |
| 77 | + x_offset_mm), |
| 78 | + y = y, |
| 79 | + z = z, |
| 80 | + count = count |
| 81 | + }) |
| 82 | + end |
| 83 | + end |
| 84 | +end |
| 85 | + |
| 86 | +return { |
| 87 | + y_spacing_mm = y_spacing_mm, |
| 88 | + y_offset_mm = y_offset_mm, |
| 89 | + y_grid_start_mm = y_grid_start_mm, |
| 90 | + y_grid_size_mm = y_grid_size_mm, |
| 91 | + y_grid_points = y_grid_points, |
| 92 | + x_spacing_mm = x_spacing_mm, |
| 93 | + x_offset_mm = x_offset_mm, |
| 94 | + x_grid_start_mm = x_grid_start_mm, |
| 95 | + x_grid_points = x_grid_points, |
| 96 | + z = z, |
| 97 | + total = total, |
| 98 | + each = each |
| 99 | +} |
0 commit comments