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