Newer
Older
debug_print(clip_uvs)
debug_print("===== Subject UV List =====")
debug_print(subject_uvs)
# check if clip and subject is overlapped completely
if __is_polygon_same(clip_uvs, subject_uvs, same_polygon_threshold):
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
polygons = [subject_uvs.as_list()]
debug_print("===== Polygons Overlapped Completely =====")
debug_print(polygons)
return True, polygons
# check if subject is in clip
if __is_points_in_polygon(subject_uvs, clip_uvs):
polygons = [subject_uvs.as_list()]
return True, polygons
# check if clip is in subject
if __is_points_in_polygon(clip_uvs, subject_uvs):
polygons = [subject_uvs.as_list()]
return True, polygons
# check if clip and subject is overlapped partially
intersections = []
while True:
subject_uvs.reset()
while True:
uv_start1 = clip_uvs.get()
uv_end1 = clip_uvs.get(1)
uv_start2 = subject_uvs.get()
uv_end2 = subject_uvs.get(1)
intersected, point = __is_segment_intersect(uv_start1, uv_end1,
uv_start2, uv_end2)
if intersected:
clip_uvs.insert(point, 1)
subject_uvs.insert(point, 1)
intersections.append([point,
[clip_uvs.get(), clip_uvs.get(1)]])
subject_uvs.next()
if subject_uvs.get() == subject_uvs.head():
break
clip_uvs.next()
if clip_uvs.get() == clip_uvs.head():
break
debug_print("===== Intersection List =====")
debug_print(intersections)
# no intersection, so subject and clip is not overlapped
if not intersections:
return False, None
def get_intersection_pair(intersects, key):
for sect in intersects:
if sect[0] == key:
return sect[1]
return None
# make enter/exit pair
subject_uvs.reset()
subject_entering = []
subject_exiting = []
clip_entering = []
clip_exiting = []
intersect_uv_list = []
while True:
pair = get_intersection_pair(intersections, subject_uvs.get())
if pair:
sub = subject_uvs.get(1) - subject_uvs.get(-1)
inter = pair[1] - pair[0]
cross = sub.x * inter.y - inter.x * sub.y
if cross < 0:
subject_entering.append(subject_uvs.get())
clip_exiting.append(subject_uvs.get())
else:
subject_exiting.append(subject_uvs.get())
clip_entering.append(subject_uvs.get())
intersect_uv_list.append(subject_uvs.get())
subject_uvs.next()
if subject_uvs.get() == subject_uvs.head():
break
debug_print("===== Enter List =====")
debug_print(clip_entering)
debug_print(subject_entering)
debug_print("===== Exit List =====")
debug_print(clip_exiting)
debug_print(subject_exiting)
# for now, can't handle the situation when fulfill all below conditions
# * two faces have common edge
# * each face is intersected
# * Show Mode is "Part"
# so for now, ignore this situation
if len(subject_entering) != len(subject_exiting):
if mode == 'FACE':
polygons = [subject_uvs.as_list()]
return True, polygons
return False, None
def traverse(current_list, entering, exiting, p, current, other_list):
result = current_list.find(current)
if not result:
return None
if result != current:
print("Internal Error")
return None
if not exiting:
print("Internal Error: No exiting UV")
return None
# enter
if entering.count(current) >= 1:
entering.remove(current)
current_list.find_and_next(current)
current = current_list.get()
while exiting.count(current) == 0:
p.append(current.copy())
current_list.find_and_next(current)
current = current_list.get()
if prev == current:
error = True
break
prev = current
if error:
print("Internal Error: Infinite loop")
return None
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# exit
p.append(current.copy())
exiting.remove(current)
other_list.find_and_set(current)
return other_list.get()
# Traverse
polygons = []
current_uv_list = subject_uvs
other_uv_list = clip_uvs
current_entering = subject_entering
current_exiting = subject_exiting
poly = []
current_uv = current_entering[0]
while True:
current_uv = traverse(current_uv_list, current_entering,
current_exiting, poly, current_uv, other_uv_list)
if current_uv is None:
break
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
if current_uv_list == subject_uvs:
current_uv_list = clip_uvs
other_uv_list = subject_uvs
current_entering = clip_entering
current_exiting = clip_exiting
debug_print("-- Next: Clip --")
else:
current_uv_list = subject_uvs
other_uv_list = clip_uvs
current_entering = subject_entering
current_exiting = subject_exiting
debug_print("-- Next: Subject --")
debug_print(clip_entering)
debug_print(clip_exiting)
debug_print(subject_entering)
debug_print(subject_exiting)
if not clip_entering and not clip_exiting \
and not subject_entering and not subject_exiting:
break
polygons.append(poly)
debug_print("===== Polygons Overlapped Partially =====")
debug_print(polygons)
return True, polygons
def __is_polygon_flipped(points):
area = 0.0
for i in range(len(points)):
uv1 = points.get(i)
uv2 = points.get(i + 1)
a = uv1.x * uv2.y - uv1.y * uv2.x
area = area + a
if area < 0:
# clock-wise
return True
return False
def __is_point_in_polygon(point, subject_points):
count = 0
for i in range(len(subject_points)):
uv_start1 = subject_points.get(i)
uv_end1 = subject_points.get(i + 1)
uv_start2 = point
uv_end2 = Vector((1000000.0, point.y))
intersected, _ = __is_segment_intersect(uv_start1, uv_end1,
uv_start2, uv_end2)
if intersected:
count = count + 1
return count % 2
def __is_points_in_polygon(points, subject_points):
for i in range(len(points)):
internal = __is_point_in_polygon(points.get(i), subject_points)
if not internal:
return False
return True
def get_uv_editable_objects(context):
if compat.check_version(2, 80, 0) < 0:
objs = [context.active_object]
else:
objs = [o for o in bpy.data.objects
if compat.get_object_select(o) and o.type == 'MESH']
objs.append(context.active_object)
objs = list(set(objs))
return objs
def get_overlapped_uv_info(bm_list, faces_list, uv_layer_list,
mode, same_polygon_threshold=0.0000001):
isl = []
for bm, uv_layer, faces in zip(bm_list, uv_layer_list, faces_list):
info = get_island_info_from_faces(bm, faces, uv_layer)
overlapped_bm_paris = []
for i, (i1, uv_layer_1, bm_1) in enumerate(isl):
for i2, uv_layer_2, bm_2 in isl[i + 1:]:
if (i1["max"].x < i2["min"].x) or (i2["max"].x < i1["min"].x) or \
(i1["max"].y < i2["min"].y) or (i2["max"].y < i1["min"].y):
continue
overlapped_isl_pairs.append([i1, i2])
overlapped_uv_layer_pairs.append([uv_layer_1, uv_layer_2])
# next, check polygon overlapped
overlapped_uvs = []
for oip, uvlp, bmp in zip(overlapped_isl_pairs,
overlapped_uv_layer_pairs,
overlapped_bm_paris):
for subject in oip[1]["faces"]:
f_subject = subject["face"]
# fast operation, apply bounding box algorithm
if (clip["max_uv"].x < subject["min_uv"].x) or \
(subject["max_uv"].x < clip["min_uv"].x) or \
(clip["max_uv"].y < subject["min_uv"].y) or \
(subject["max_uv"].y < clip["min_uv"].y):
continue
subject_uvs = [l[uvlp[1]].uv.copy() for l in f_subject.loops]
# slow operation, apply Weiler-Atherton cliping algorithm
result, polygons = \
__do_weiler_atherton_cliping(clip_uvs, subject_uvs,
mode, same_polygon_threshold)
overlapped_uvs.append({"clip_bmesh": bmp[0],
"subject_bmesh": bmp[1],
"clip_face": f_clip,
"clip_uv_layer": uvlp[0],
"subject_uv_layer": uvlp[1],
"subject_uvs": subject_uvs,
"polygons": polygons})
return overlapped_uvs
def get_flipped_uv_info(bm_list, faces_list, uv_layer_list):
for bm, faces, uv_layer in zip(bm_list, faces_list, uv_layer_list):
for f in faces:
polygon = RingBuffer([l[uv_layer].uv.copy() for l in f.loops])
if __is_polygon_flipped(polygon):
uvs = [l[uv_layer].uv.copy() for l in f.loops]
"uv_layer": uv_layer,
"uvs": uvs,
"polygons": [polygon.as_list()]})
if len(points1) != len(points2):
return False
pts1 = points1.as_list()
pts2 = points2.as_list()
for p1 in pts1:
for p2 in pts2:
diff = p2 - p1