<commit_before>"""
Convert the GUI's map into a grid.
"""

import utm
import numpy as np
import math
from shapely.geometry import Polygon, Point
import matplotlib.pyplot as plt
from matplotlib import colors
from sys import path
from tqdm import tqdm
import pickle
path.append("..")

import settings
import astar

def project_to_virtual(point):
    """
    Project a given point into a virtual space aligned with east.

    Keyword arguments:
    point: Point expressed by Latitude/Longitude values

    Output expressed by UTM values
    """

    point = utm.from_latlon(point[0], point[1])
    x = math.cos(settings.ANGLE) * (point[0] - settings.RP_UTM[0]) - math.sin(settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[0]
    y = math.sin(settings.ANGLE) * (point[0] - settings.RP_UTM[0]) + math.cos(settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[1]

    return x, y #Easting, Northing

def project_to_original(point):
    """
    Project a given point into the real space.

    Keyword arguments:
    point: Point expressed by UTM values

    Output expressed by Latitude/Longitude values
    """

    x = math.cos(-settings.ANGLE) * (point[0] - settings.RP_UTM[0]) - math.sin(-settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[0]
    y = math.sin(-settings.ANGLE) * (point[0] - settings.RP_UTM[0]) + math.cos(-settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[1]
    point = utm.to_latlon(x, y, 53, "S")

    return point[0], point[1] #Latitude, Longitude

class Mapper():
    """
    Represent the environment as grids.
    """

    def __init__(self, limits, starting_point, obstacles, default_targets):
        """
        Instantiate a Mapper object

        Keyword arguments:
        limits: environment boundaries
        starting_point: drones' patrol starting point
        obstacles: array of non admissible zones
        """

        #Environment boundaries
        self.limits = limits
        self.projected_limits = self.project_limits()

        #Coefficients for index calculations
        self.X = self.projected_limits[0][0]
        self.Y = self.projected_limits[2][0]
        self.Z = self.Y - self.X
        self.A = self.projected_limits[0][1]
        self.B = self.projected_limits[2][1]
        self.C = self.A - self.B

        #Environment elements
        self.starting_point = self.latlong_to_index(starting_point)
        self.obstacles = obstacles
        self.default_targets = [self.latlong_to_index(t) for t in default_targets]
        self.default_targets.append(self.starting_point)
        self.world = self.create_world()
        print("Computing shortest paths to default targets...")
        self.paths = {(d1, d2):astar.astar(self.world, tuple(reversed(d1)), tuple(reversed(d2))) for d1 in tqdm(self.default_targets) for d2 in self.default_targets}
        #self.paths = {d:astar.astar(self.world, tuple(reversed(self.starting_point)), tuple(reversed(d))) for d in tqdm(self.default_targets)}
        m = 0
        for p in self.paths:
            m += self.paths[p][1]
        print("Total length: " + str(m))
        print(str(len(self.paths)) + " paths")
        m /= len(self.paths.keys())
        print("average path length: " + str(m))
        print("Paths computed")
        for k in self.paths:
            if self.paths[k][0]:
                for c in self.paths[k][0]:
                    self.world[c[0]][c[1]] = 4
        for d in self.default_targets:
            self.world[d[1]][d[0]] = 3
        self.world[self.starting_point[1]][self.starting_point[0]] = 2

        #Environment uncertainty
        #self.uncertainty_grid = self.create_uncertainty_grid()

    def project_limits(self):
        """<commit_msg>remove starting point from target points list<commit_after>"""
Convert the GUI's map into a grid.
"""

import utm
import numpy as np
import math
from shapely.geometry import Polygon, Point
import matplotlib.pyplot as plt
from matplotlib import colors
from sys import path
from tqdm import tqdm
import pickle
path.append("..")

import settings
import astar

def project_to_virtual(point):
    """
    Project a given point into a virtual space aligned with east.

    Keyword arguments:
    point: Point expressed by Latitude/Longitude values

    Output expressed by UTM values
    """

    point = utm.from_latlon(point[0], point[1])
    x = math.cos(settings.ANGLE) * (point[0] - settings.RP_UTM[0]) - math.sin(settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[0]
    y = math.sin(settings.ANGLE) * (point[0] - settings.RP_UTM[0]) + math.cos(settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[1]

    return x, y #Easting, Northing

def project_to_original(point):
    """
    Project a given point into the real space.

    Keyword arguments:
    point: Point expressed by UTM values

    Output expressed by Latitude/Longitude values
    """

    x = math.cos(-settings.ANGLE) * (point[0] - settings.RP_UTM[0]) - math.sin(-settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[0]
    y = math.sin(-settings.ANGLE) * (point[0] - settings.RP_UTM[0]) + math.cos(-settings.ANGLE) * (point[1] - settings.RP_UTM[1]) + settings.RP_UTM[1]
    point = utm.to_latlon(x, y, 53, "S")

    return point[0], point[1] #Latitude, Longitude

class Mapper():
    """
    Represent the environment as grids.
    """

    def __init__(self, limits, starting_point, obstacles, default_targets):
        """
        Instantiate a Mapper object

        Keyword arguments:
        limits: environment boundaries
        starting_point: drones' patrol starting point
        obstacles: array of non admissible zones
        """

        #Environment boundaries
        self.limits = limits
        self.projected_limits = self.project_limits()

        #Coefficients for index calculations
        self.X = self.projected_limits[0][0]
        self.Y = self.projected_limits[2][0]
        self.Z = self.Y - self.X
        self.A = self.projected_limits[0][1]
        self.B = self.projected_limits[2][1]
        self.C = self.A - self.B

        #Environment elements
        self.starting_point = self.latlong_to_index(starting_point)
        self.obstacles = obstacles
        self.default_targets = [self.latlong_to_index(t) for t in default_targets]
        self.default_targets.append(self.starting_point)
        self.world = self.create_world()
        print("Computing shortest paths to default targets...")
        self.paths = {(d1, d2):astar.astar(self.world, tuple(reversed(d1)), tuple(reversed(d2))) for d1 in tqdm(self.default_targets) for d2 in self.default_targets}
        self.default_targets = self.default_targets[:-1] #Removing the starting point from target points list
        print("Paths computed")
        for k in self.paths:
            if self.paths[k][0]:
                for c in self.paths[k][0]:
                    self.world[c[0]][c[1]] = 4
        for d in self.default_targets:
            self.world[d[1]][d[0]] = 3
        self.world[self.starting_point[1]][self.starting_point[0]] = 2

        #Environment uncertainty
        #self.uncertainty_grid = self.create_uncertainty_grid()

    def project_limits(self):
        """
{'fraction_non_alphanumeric': 0.12242641978720464, 'fraction_numerical': 0.014508774354014094, 'mean_word_length': 2.885131508319914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4110426', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2310, 'n_words': 567}