2020"""
2121
2222import copy
23- import math
2423import logging
24+ import math
2525import sys
2626from dataclasses import dataclass , field
2727from enum import Enum
@@ -1921,6 +1921,7 @@ def _normalize_rotation(angle: float) -> float:
19211921 return normalized
19221922
19231923 rotation_deg = _normalize_rotation (element .rotation_deg or 0.0 )
1924+ rotation_from_columns = False
19241925 if abs (rotation_deg ) <= 1.0e-3 :
19251926 candidate_rotations = [
19261927 _normalize_rotation (col .rotation_deg or 0.0 )
@@ -1933,6 +1934,7 @@ def _normalize_rotation(angle: float) -> float:
19331934 if candidate_rotations :
19341935 candidate_rotations .sort ()
19351936 rotation_deg = candidate_rotations [len (candidate_rotations ) // 2 ]
1937+ rotation_from_columns = True
19361938
19371939 rotation_active = abs (rotation_deg ) > 1.0e-3
19381940 base_table_bbox = element .bbox_unrotated or tb
@@ -2148,6 +2150,94 @@ def _belongs_to_current_table(candidate: CVATElement) -> bool:
21482150 row_sections ,
21492151 fillable_cells ,
21502152 )
2153+ if rotation_active and rows and cols :
2154+
2155+ def _center_x (bbox : BoundingBox ) -> float :
2156+ return (bbox .l + bbox .r ) / 2.0
2157+
2158+ def _center_y (bbox : BoundingBox ) -> float :
2159+ return (bbox .t + bbox .b ) / 2.0
2160+
2161+ def _order_ids (elements : list [CVATElement ], axis : str ) -> list [int ]:
2162+ key = (
2163+ (lambda el : _center_x (el .bbox ))
2164+ if axis == "x"
2165+ else (lambda el : _center_y (el .bbox ))
2166+ )
2167+ return [el .id for el in sorted (elements , key = key )]
2168+
2169+ original_rows = [
2170+ self .doc_structure .get_element_by_id (el .id ) or el for el in rows
2171+ ]
2172+ original_cols = [
2173+ self .doc_structure .get_element_by_id (el .id ) or el for el in cols
2174+ ]
2175+
2176+ row_vertical = sum (
2177+ el .bbox .height > el .bbox .width for el in original_rows
2178+ ) >= (len (original_rows ) / 2 )
2179+ col_horizontal = sum (
2180+ el .bbox .width > el .bbox .height for el in original_cols
2181+ ) >= (len (original_cols ) / 2 )
2182+
2183+ desired_row_axis = "x" if row_vertical else "y"
2184+ desired_col_axis = "y" if col_horizontal else "x"
2185+ if 45.0 <= abs (rotation_deg ) <= 135.0 :
2186+ desired_col_axis = "y"
2187+
2188+ desired_row_order = _order_ids (original_rows , desired_row_axis )
2189+ desired_col_order = _order_ids (original_cols , desired_col_axis )
2190+ actual_row_order = _order_ids (rows , "y" )
2191+ actual_col_order = _order_ids (cols , "x" )
2192+
2193+ reverse_rows = (
2194+ actual_row_order == list (reversed (desired_row_order ))
2195+ and actual_row_order != desired_row_order
2196+ )
2197+ reverse_cols = (
2198+ actual_col_order == list (reversed (desired_col_order ))
2199+ and actual_col_order != desired_col_order
2200+ )
2201+
2202+ if (
2203+ rotation_from_columns
2204+ and 45.0 <= abs (rotation_deg ) <= 135.0
2205+ and reverse_rows
2206+ and not reverse_cols
2207+ ):
2208+ reverse_cols = True
2209+
2210+ if reverse_rows or reverse_cols :
2211+ flipped_cells : list [Cell ] = []
2212+ row_max = len (rows ) - 1
2213+ col_max = len (cols ) - 1
2214+ for cell in computed_table_cells :
2215+ start_row = cell .start_row
2216+ end_row = cell .end_row
2217+ start_col = cell .start_column
2218+ end_col = cell .end_column
2219+ if reverse_rows :
2220+ start_row = row_max - cell .end_row
2221+ end_row = row_max - cell .start_row
2222+ if reverse_cols :
2223+ start_col = col_max - cell .end_column
2224+ end_col = col_max - cell .start_column
2225+ flipped_cells .append (
2226+ Cell (
2227+ start_row = start_row ,
2228+ end_row = end_row ,
2229+ start_column = start_col ,
2230+ end_column = end_col ,
2231+ row_span_length = cell .row_span_length ,
2232+ column_span_length = cell .column_span_length ,
2233+ bbox = cell .bbox ,
2234+ column_header = cell .column_header ,
2235+ row_header = cell .row_header ,
2236+ row_section = cell .row_section ,
2237+ fillable_cell = cell .fillable_cell ,
2238+ )
2239+ )
2240+ computed_table_cells = flipped_cells
21512241 if rotation_active :
21522242 computed_table_cells = [
21532243 Cell (
0 commit comments