Skip to content

Commit

Permalink
import pair length when model imported
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Dec 18, 2023
1 parent f6d5f7c commit c748a4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
37 changes: 26 additions & 11 deletions server/server/mark/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,36 @@ def to_side_list(model_id: int, pairs: np.ndarray, pair_id: int):
return side_list


def point_to_line_distance(line_points, point):
assert len(line_points) == 2, "line_points must have two points"
A = line_points[0]
B = line_points[1]
P = point

# Calculate the vector AP and vector AB
AP = P - A
AB = B - A

# Calculate the cross product (AP x AB)
cross_product = np.cross(AP, AB)

# Calculate the distance
distance = np.linalg.norm(cross_product) / np.linalg.norm(AB)

return distance


def import_sides(model_id: int, pairs: np.ndarray, pair_type: str, mysql_config: dict):
cnx = mysql.connector.connect(**mysql_config, database="coord")
cursor = cnx.cursor()
for pair in pairs:
pair_id = import_pair(model_id, pair_type, mysql_config)
pair_id = import_pair(model_id, pair_type, pair, cnx, cursor)
side_list = to_side_list(model_id, pair, pair_id)
insert_query = (
"INSERT INTO side (x0, y0, z0, x1, y1, z1, model_id, pair_id)"
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
)
try:
cursor.executemany(insert_query, side_list)
except IntegrityError:
print("Error: unable to import lines")
cursor.executemany(insert_query, side_list)
cnx.commit()
cursor.close()
cnx.close()
Expand Down Expand Up @@ -200,20 +216,19 @@ def import_lines_from_paired_lines_on_facets(
import_edges_from_sides(sides, mysql_config)


def import_pair(model_id: int, pair_type: str, mysql_config: dict) -> int:
cnx = mysql.connector.connect(**mysql_config, database="coord")
cursor = cnx.cursor()
insert_query = "INSERT INTO pair (model_id, type) VALUES (%s, %s)"
def import_pair(model_id: int, pair_type: str, pair, cnx, cursor) -> int:
assert len(pair) == 2, "pair must have two lines"
length = point_to_line_distance(pair[0], pair[1][0])
insert_query = "INSERT INTO pair (model_id, type, length) VALUES (%s, %s, %s)"
cursor.execute(
insert_query,
(
model_id,
pair_type,
length,
),
)
cnx.commit()
cursor.close()
cnx.close()
return cursor.lastrowid


Expand Down
8 changes: 1 addition & 7 deletions server/server/mark/pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def add_line_length(model_id: int, mysql_config: dict, process_id: int):
sides = get_sides_by_pair_id(pair_id, mysql_config)
side1 = sides[0]
side2 = sides[1]
length = point_to_line_distance([side1[0:3], side1[3:6]], side2[0:3])
edge_results1 = get_edges_by_side_id(side1[6], mysql_config, process_id)
edge_results2 = get_edges_by_side_id(side2[6], mysql_config, process_id)
line_edge_list = to_line_edge_list(edge_results1, edge_results2)
Expand All @@ -107,7 +106,7 @@ def add_line_length(model_id: int, mysql_config: dict, process_id: int):
continue
measured_length = np.mean(distances)
measured_length = float(np.round(measured_length, 3))
add_measured_length(pair_id, length, measured_length, mysql_config, process_id)
add_measured_length(pair_id, measured_length, mysql_config, process_id)


def get_sides_by_pair_id(pair_id: int, mysql_config: dict):
Expand All @@ -123,17 +122,12 @@ def get_sides_by_pair_id(pair_id: int, mysql_config: dict):

def add_measured_length(
pair_id: int,
length: float,
measured_length: float,
mysql_config: dict,
process_id: int,
):
cnx = mysql.connector.connect(**mysql_config, database="coord")
cursor = cnx.cursor()
query = "UPDATE pair SET length = %s WHERE id = %s"
cursor.execute(query, (length, pair_id))
cnx.commit()

query = "INSERT INTO pair_result (pair_id, process_id, length) VALUES (%s, %s, %s)"
cursor.execute(query, (pair_id, process_id, measured_length))
cnx.commit()
Expand Down

0 comments on commit c748a4c

Please sign in to comment.