###########################################
# Adaptive Bed Meshing Macros
###########################################

[gcode_macro BED_MESH_CALIBRATE]
rename_existing: _BED_MESH_CALIBRATE
gcode:
    {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
    {% set bed_mesh_min = printer.configfile.settings.bed_mesh.mesh_min %}
    {% set bed_mesh_max = printer.configfile.settings.bed_mesh.mesh_max %}
    {% set probe_count = printer.configfile.settings.bed_mesh.probe_count %}
    {% set kamp_settings = printer["gcode_macro _KAMP_Settings"] %}
    {% set verbose_enable = kamp_settings.verbose_enable | abs %}
    {% set mesh_margin = kamp_settings.mesh_margin | float %}
    {% set fuzz_amount = kamp_settings.fuzz_amount | float %}
    {% set probe_count = probe_count if probe_count|length > 1 else probe_count * 2  %}
    {% set max_probe_point_distance_x = ( bed_mesh_max[0] - bed_mesh_min[0] ) / (probe_count[0] - 1)  %}
    {% set max_probe_point_distance_y = ( bed_mesh_max[1] - bed_mesh_min[1] ) / (probe_count[1] - 1)  %}
    {% set x_min = all_points | map(attribute=0) | min | default(bed_mesh_min[0]) %}
    {% set y_min = all_points | map(attribute=1) | min | default(bed_mesh_min[1]) %}
    {% set x_max = all_points | map(attribute=0) | max | default(bed_mesh_max[0]) %}
    {% set y_max = all_points | map(attribute=1) | max | default(bed_mesh_max[1]) %}
    {% set fuzz_range = range((0) | int, (fuzz_amount * 100) | int + 1) %}
    {% set adapted_x_min = x_min - mesh_margin - (fuzz_range | random / 100.0) %}
    {% set adapted_y_min = y_min - mesh_margin - (fuzz_range | random / 100.0) %}
    {% set adapted_x_max = x_max + mesh_margin + (fuzz_range | random / 100.0) %}
    {% set adapted_y_max = y_max + mesh_margin + (fuzz_range | random / 100.0) %}
    {% set adapted_x_min = [adapted_x_min , bed_mesh_min[0]] | max %}
    {% set adapted_y_min = [adapted_y_min , bed_mesh_min[1]] | max %}
    {% set adapted_x_max = [adapted_x_max , bed_mesh_max[0]] | min %}
    {% set adapted_y_max = [adapted_y_max , bed_mesh_max[1]] | min %}
    {% set points_x = (((adapted_x_max - adapted_x_min) / max_probe_point_distance_x) | round(method='ceil') | int) + 1 %}
    {% set points_y = (((adapted_y_max - adapted_y_min) / max_probe_point_distance_y) | round(method='ceil') | int) + 1 %}
    {% if (points_x > points_y) %}
        {% set points_y = points_x %}
    {% endif %}
    {% if (points_x < points_y) %}
        {% set points_x = points_y %}
    {% endif %}
    {% if (([points_x, points_y]|max) > 6) %}
        {% set algorithm = "bicubic" %}
        {% set min_points = 4 %}
    {% else %}
        {% set algorithm = "lagrange" %}
        {% set min_points = 3 %}
    {% endif %} 
    {% set points_x = [points_x , min_points]|max %}
    {% set points_y = [points_y , min_points]|max %}
    {% set points_x = [points_x , probe_count[0]]|min %}
    {% set points_y = [points_y , probe_count[1]]|min %}

    {% if verbose_enable == True %}
        {% if printer.exclude_object.objects != [] %}
            RESPOND TYPE=command MSG="Algorithm: {algorithm}"
            RESPOND TYPE=command MSG="Default probe count: {probe_count[0]},{probe_count[1]}"
            RESPOND TYPE=command MSG="Adapted probe count: {points_x},{points_y}"
            RESPOND TYPE=command MSG="Default mesh bounds: {bed_mesh_min[0]},{bed_mesh_min[1]}, {bed_mesh_max[0]},{bed_mesh_max[1]}"
            {% if mesh_margin > 0 %}
                RESPOND TYPE=command MSG="Mesh margin is {mesh_margin}, mesh bounds extended by {mesh_margin}mm."
            {% else %}
                RESPOND TYPE=command MSG="Mesh margin is 0, margin not increased."
            {% endif %}
            {% if fuzz_amount > 0 %}
                RESPOND TYPE=command MSG="Mesh point fuzzing enabled, points fuzzed up to {fuzz_amount}mm"
            {% else %}
                RESPOND TYPE=command MSG="Fuzz amount is 0, mesh points not fuzzed."
            {% endif %}
            RESPOND TYPE=command MSG="Adapted mesh bounds: {adapted_x_min},{adapted_y_min}, {adapted_x_max},{adapted_y_max}"
            RESPOND TYPE=command MSG="KAMP adjustments successful. Happy KAMPing!"
        {% else %}
            RESPOND TYPE=command MSG="No object detected! Make sure you have enabled Exclude Objets setting in your slicer. Using Full Bed Mesh."
            G4 P5000
        {% endif %}
    {% endif %}
    _BED_MESH_CALIBRATE mesh_min={adapted_x_min},{adapted_y_min} mesh_max={adapted_x_max},{adapted_y_max} ALGORITHM={algorithm} PROBE_COUNT={points_x},{points_y}
