garmentiq.utils.compute_measurement_distances
Computing measurement distances between landmark pairs.
1"""Computing measurement distances between landmark pairs.""" 2import math 3 4 5def compute_measurement_distances(garment_dict): 6 """ 7 Computes Euclidean distances between specified landmark points for each garment 8 in a dictionary and updates the dictionary with these distances. 9 10 This function iterates through the `garment_dict`, accesses the 'landmarks' 11 and 'measurements' for each garment, calculates the Euclidean distance 12 between the 'start' and 'end' landmarks defined for each measurement, 13 and stores the rounded distance in both a new dictionary and within the 14 original `garment_dict`. 15 16 Args: 17 garment_dict (dict): A dictionary where keys are garment names and values 18 contain 'landmarks' (with 'x' and 'y' coordinates) 19 and 'measurements' (with 'start' and 'end' landmark IDs). 20 21 Returns: 22 tuple: 23 - distances (dict): A dictionary mapping measurement names to their computed distances. 24 - garment_dict (dict): The original `garment_dict` updated with a 'distance' field 25 for each measurement. 26 """ 27 28 def euclidean(p1, p2): 29 return math.sqrt((p1["x"] - p2["x"]) ** 2 + (p1["y"] - p2["y"]) ** 2) 30 31 distances = {} 32 for garment_name, garment_data in garment_dict.items(): 33 landmarks = garment_data["landmarks"] 34 for measurement_name, measurement_data in garment_data["measurements"].items(): 35 start_id = measurement_data["landmarks"]["start"] 36 end_id = measurement_data["landmarks"]["end"] 37 38 point1 = landmarks[start_id] 39 point2 = landmarks[end_id] 40 distance = euclidean(point1, point2) 41 42 # Store the distance in the return dict 43 distances[measurement_name] = round(distance, 16) 44 45 # Update the original dictionary 46 garment_dict[garment_name]["measurements"][measurement_name][ 47 "distance" 48 ] = round(distance, 16) 49 50 return distances, garment_dict
def
compute_measurement_distances(garment_dict):
6def compute_measurement_distances(garment_dict): 7 """ 8 Computes Euclidean distances between specified landmark points for each garment 9 in a dictionary and updates the dictionary with these distances. 10 11 This function iterates through the `garment_dict`, accesses the 'landmarks' 12 and 'measurements' for each garment, calculates the Euclidean distance 13 between the 'start' and 'end' landmarks defined for each measurement, 14 and stores the rounded distance in both a new dictionary and within the 15 original `garment_dict`. 16 17 Args: 18 garment_dict (dict): A dictionary where keys are garment names and values 19 contain 'landmarks' (with 'x' and 'y' coordinates) 20 and 'measurements' (with 'start' and 'end' landmark IDs). 21 22 Returns: 23 tuple: 24 - distances (dict): A dictionary mapping measurement names to their computed distances. 25 - garment_dict (dict): The original `garment_dict` updated with a 'distance' field 26 for each measurement. 27 """ 28 29 def euclidean(p1, p2): 30 return math.sqrt((p1["x"] - p2["x"]) ** 2 + (p1["y"] - p2["y"]) ** 2) 31 32 distances = {} 33 for garment_name, garment_data in garment_dict.items(): 34 landmarks = garment_data["landmarks"] 35 for measurement_name, measurement_data in garment_data["measurements"].items(): 36 start_id = measurement_data["landmarks"]["start"] 37 end_id = measurement_data["landmarks"]["end"] 38 39 point1 = landmarks[start_id] 40 point2 = landmarks[end_id] 41 distance = euclidean(point1, point2) 42 43 # Store the distance in the return dict 44 distances[measurement_name] = round(distance, 16) 45 46 # Update the original dictionary 47 garment_dict[garment_name]["measurements"][measurement_name][ 48 "distance" 49 ] = round(distance, 16) 50 51 return distances, garment_dict
Computes Euclidean distances between specified landmark points for each garment in a dictionary and updates the dictionary with these distances.
This function iterates through the garment_dict, accesses the 'landmarks'
and 'measurements' for each garment, calculates the Euclidean distance
between the 'start' and 'end' landmarks defined for each measurement,
and stores the rounded distance in both a new dictionary and within the
original garment_dict.
Arguments:
- garment_dict (dict): A dictionary where keys are garment names and values contain 'landmarks' (with 'x' and 'y' coordinates) and 'measurements' (with 'start' and 'end' landmark IDs).
Returns:
tuple: - distances (dict): A dictionary mapping measurement names to their computed distances. - garment_dict (dict): The original
garment_dictupdated with a 'distance' field for each measurement.