Skip to content

Commit

Permalink
Merge pull request #59 from kc-ml2/DEV/main
Browse files Browse the repository at this point in the history
Dev/main
  • Loading branch information
yonghakim authored Aug 10, 2023
2 parents 8387562 + 8719f23 commit 98fd479
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
113 changes: 70 additions & 43 deletions meent/on_torch/modeler/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,53 @@ def __init__(self, period=None, *args, **kwargs):
@staticmethod
def rectangle(cx, cy, lx, ly, base):

a = torch.hstack([cy - ly / 2, cx - lx / 2]) # row, col
b = torch.hstack([cy + ly / 2, cx + lx / 2]) # row, col
# a = torch.hstack([cy - ly / 2, cx - lx / 2]) # row, col
# b = torch.hstack([cy + ly / 2, cx + lx / 2]) # row, col
#
# res = [[[a, b, base]]] # top_left, bottom_right

a = [cy - ly / 2, cx - lx / 2] # row, col
b = [cy + ly / 2, cx + lx / 2] # row, col

res = [[a, b, base]] # top_left, bottom_right


return res

def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margin=1E-5):
ddx, ddy = dx + 2, dy + 2
def rectangle_rotate(self, cx, cy, lx, ly, n_split_triangle, n_split_parallelogram, n_index, angle=None, angle_margin=1E-5):
# if type(lx) in (int, float):
# lx = torch.tensor(lx)
# if type(ly) in (int, float):
# ly = torch.tensor(ly)
# if type(angle) in (int, float):
# angle = torch.tensor(angle)
if type(lx) in (int, float):
lx = torch.tensor([lx])
if type(ly) in (int, float):
ly = torch.tensor([ly])
if type(angle) in (int, float):
angle = torch.tensor([angle])

if lx.type not in (torch.complex64, torch.complex128):
lx = lx.type(self.type_complex) # TODO
if ly.type not in (torch.complex64, torch.complex128):
ly = ly.type(self.type_complex)

n_split_triangle, n_split_parallelogram = n_split_triangle + 2, n_split_parallelogram + 2

if angle is None:
angle = torch.tensor(0 * torch.pi / 180)

if 0 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 0 * torch.pi / 2 + angle_margin:
return self.rectangle(cx, cy, lx, ly, base)
elif 1 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 1 * torch.pi / 2 + angle_margin:
return self.rectangle(cx, cy, ly, lx, base)
elif 2 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 2 * torch.pi / 2 + angle_margin:
return self.rectangle(cx, cy, lx, ly, base)
elif 3 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 3 * torch.pi / 2 + angle_margin:
return self.rectangle(cx, cy, ly, lx, base)
else:
pass
# if 0 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 0 * torch.pi / 2 + angle_margin:
# return self.rectangle(cx, cy, lx, ly, n_index)
# elif 1 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 1 * torch.pi / 2 + angle_margin:
# return self.rectangle(cx, cy, ly, lx, n_index)
# elif 2 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 2 * torch.pi / 2 + angle_margin:
# return self.rectangle(cx, cy, lx, ly, n_index)
# elif 3 * torch.pi / 2 - angle_margin <= abs(angle) % (2 * torch.pi) <= 3 * torch.pi / 2 + angle_margin:
# return self.rectangle(cx, cy, ly, lx, n_index)
# else:
# pass

angle = angle % (2 * torch.pi)

Expand All @@ -83,7 +108,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
DL += torch.tensor([[cx], [cy]])
LU += torch.tensor([[cx], [cy]])

if 0 < angle < torch.pi / 2:
if 0 <= angle < torch.pi / 2:
angle_inside = (torch.pi / 2) - angle

# trail = L + U
Expand All @@ -98,7 +123,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
length_top12, length_top24 = ly, lx
top2_left = False

elif torch.pi / 2 < angle < torch.pi:
elif torch.pi / 2 <= angle < torch.pi:

angle_inside = torch.pi - angle
# trail = U + R
Expand All @@ -113,7 +138,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
length_top12, length_top24 = lx, ly
top2_left = False

elif torch.pi < angle < torch.pi / 2 * 3:
elif torch.pi <= angle < torch.pi / 2 * 3:
angle_inside = (torch.pi * 3 / 2) - angle

# trail = R + D
Expand All @@ -128,7 +153,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
length_top12, length_top24 = ly, lx
top2_left = False

elif torch.pi / 2 * 3 < angle < torch.pi * 2:
elif torch.pi / 2 * 3 <= angle < torch.pi * 2:
angle_inside = (torch.pi * 2) - angle
# trail = D + L
top1, top4 = LU, RD
Expand All @@ -153,32 +178,32 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
length = length_top12 / torch.sin(angle_inside)
top3_cp = [top3[0] - length, top3[1]]

for i in range(ddx + 1):
x = top1[0] - (top1[0] - top2[0]) / ddx * i
y = top1[1] - (top1[1] - top2[1]) / ddy * i
for i in range(n_split_triangle + 1):
x = top1[0] - (top1[0] - top2[0]) / n_split_triangle * i
y = top1[1] - (top1[1] - top2[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x + length / ddx * i)
xxx_cp.append(x + length / n_split_triangle * i)
yyy_cp.append(y)

for i in range(ddy + 1):
for i in range(n_split_parallelogram + 1):

x = top2[0] + (top3_cp[0] - top2[0]) / ddx * i
y = top2[1] - (top2[1] - top3_cp[1]) / ddy * i
x = top2[0] + (top3_cp[0] - top2[0]) / n_split_triangle * i
y = top2[1] - (top2[1] - top3_cp[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x + length)
yyy_cp.append(y)

for i in range(ddx + 1):
x = top3_cp[0] + (top4[0] - top3_cp[0]) / ddx * i
y = top3_cp[1] - (top3_cp[1] - top4[1]) / ddy * i
for i in range(n_split_triangle + 1):
x = top3_cp[0] + (top4[0] - top3_cp[0]) / n_split_triangle * i
y = top3_cp[1] - (top3_cp[1] - top4[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x + length / ddx * (ddx - i))
xxx_cp.append(x + length / n_split_triangle * (n_split_triangle - i))
yyy_cp.append(y)

obj_list1 = []
Expand All @@ -194,7 +219,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi

x_mean = (x + x_next) / 2
x_cp_mean = (x_cp + x_cp_next) / 2
obj_list1.append([[y_cp_next, x_mean], [y, x_cp_mean], base])
obj_list1.append([[y_cp_next, x_mean], [y, x_cp_mean], n_index])

return obj_list1

Expand All @@ -203,32 +228,32 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi
length = length_top12 / torch.cos(angle_inside)
top3_cp = [top3[0] + length, top3[1]]

for i in range(ddx + 1):
x = top1[0] + (top2[0] - top1[0]) / ddx * i
y = top1[1] - (top1[1] - top2[1]) / ddy * i
for i in range(n_split_triangle + 1):
x = top1[0] + (top2[0] - top1[0]) / n_split_triangle * i
y = top1[1] - (top1[1] - top2[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x - length / ddx * i)
xxx_cp.append(x - length / n_split_triangle * i)
yyy_cp.append(y)

for i in range(ddy + 1):
for i in range(n_split_parallelogram + 1):

x = top2[0] - (top2[0] - top3_cp[0]) / ddx * i
y = top2[1] - (top2[1] - top3_cp[1]) / ddy * i
x = top2[0] - (top2[0] - top3_cp[0]) / n_split_triangle * i
y = top2[1] - (top2[1] - top3_cp[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x - length)
yyy_cp.append(y)

for i in range(ddx + 1):
x = top3_cp[0] - (top3_cp[0] - top4[0]) / ddx * i
y = top3_cp[1] - (top3_cp[1] - top4[1]) / ddy * i
for i in range(n_split_triangle + 1):
x = top3_cp[0] - (top3_cp[0] - top4[0]) / n_split_triangle * i
y = top3_cp[1] - (top3_cp[1] - top4[1]) / n_split_parallelogram * i
xxx.append(x)
yyy.append(y)

xxx_cp.append(x - length / ddx * (ddx - i))
xxx_cp.append(x - length / n_split_triangle * (n_split_triangle - i))
yyy_cp.append(y)

obj_list1 = []
Expand All @@ -244,7 +269,7 @@ def rectangle_rotate(self, cx, cy, lx, ly, dx, dy, base, angle=None, angle_margi

x_mean = (x + x_next) / 2
x_cp_mean = (x_cp + x_cp_next) / 2
obj_list1.append([[y_cp_next, x_cp_mean], [y, x_mean], base])
obj_list1.append([[y_cp_next, x_cp_mean], [y, x_mean], n_index])

return obj_list1

Expand Down Expand Up @@ -363,7 +388,9 @@ def vector(self, layer_info, x64=True):
if col_list and col_list[0] == 0:
col_list = col_list[1:]

ucell_layer = torch.ones((len(row_list), len(col_list)), dtype=datatype, requires_grad=True) * pmtvy_base
# ucell_layer = torch.ones((len(row_list), len(col_list)), dtype=datatype, requires_grad=True) * pmtvy_base
ucell_layer = torch.ones((len(row_list), len(col_list)), dtype=datatype) * pmtvy_base
# TODO: requires_grad?

for obj in obj_list:
top_left, bottom_right, pmty = obj
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
setup(
name='meent',
version='0.9.5',
version='0.9.6',
url='https://github.com/kc-ml2/meent',
author='KC ML2',
author_email='[email protected]',
Expand Down

0 comments on commit 98fd479

Please sign in to comment.