def test_midpoint(self): box = Box(-1.3, -5.4, 2.4, -3.2) expected_midpoint = np.array( [-1.3 + (2.4 - (-1.3)) / 2.0, -5.4 + (-3.2 - (-5.4)) / 2.0]) assert_array_equal(box.midpoint(), expected_midpoint)
def test_distance_to_point(self): box = Box(-1.3, -5.4, 2.4, -3.2) point = np.array([-10, 13]) closest_point = np.array([box.x_min, box.y_max]) expected_distance = np.linalg.norm(closest_point - point) self.assertEqual(expected_distance, box.distance_to_point(point))
def test_move_midpoint(self): box = Box(-1.3, -5.4, 2.4, -3.2) new_midpoint = np.array([-3.1, 17.0]) new_box = box.move_midpoint_to(new_midpoint) assert_array_equal(new_midpoint, new_box.midpoint()) self.assertAlmostEqual(box.width, new_box.width) self.assertAlmostEqual(box.height, new_box.height)
def collect_text_bboxes(text_objects, renderer, padding): bboxes = [t.get_window_extent(renderer) for t in text_objects] boxes = [] # print(bboxes) ax = plt.gca() for bbox in bboxes: expanded = bbox.padded(p=1) b = Box(*expanded.get_points().ravel()) # print(b) # boxes.append(Box(b.x_min, b.y_min+2, b.midpoint()[0]-1, b.y_max-1)) boxes.append(Box(b.x_min, b.y_min+4, b.x_max-2, b.y_max)) # print(Box(b.x_min, b.y_min+4, b.x_max-2, b.y_max)) return boxes
def point_bounding_box(point,x,padding, ax): if(x == 0.0): r = 0 else: r = math.log(math.pow(10,x),2) point0 = point-7-r point1 = point+7+r return Box(point0[0], point0[1], point1[0], point1[1])
def test_anchoring_position(self): step = np.pi / 16 positions = list(range(17)) + list(range(-15, 0)) point = np.array([1, -2]) box = Box(0, 0, 1, 1) for position in positions: angle = step * position new_box = box.move_midpoint_to_angle(point, angle, new_distance=3) combinations = itertools.product( [ (new_box.x_min, 'left'), (new_box.x_max, 'right'), (new_box.midpoint()[0], 'center'), ], [ (new_box.y_min, 'bottom'), (new_box.y_max, 'top'), (new_box.midpoint()[1], 'middle'), ], ) best_combination = None best_distance = np.inf for (x, x_align), (y, y_align) in combinations: distance = np.linalg.norm(np.array([x, y]) - point) if distance < best_distance: best_combination = (x, y), x_align, y_align best_distance = distance actual_anchor = new_box.anchoring_position(point) self.assertEqual( best_combination[1:], actual_anchor[1:], 'Wrong alignment for pos {}: {!r} != {!r}'.format( position, best_combination, actual_anchor)) assert_array_equal( best_combination[0], actual_anchor[0], 'Wrong ans for pos {}: {!r} != {!r}'.format( position, best_combination, actual_anchor))
def test_fast_overlap_calculation(self): box = Box(1, 1, 3, 3) random = np.random.RandomState(42) points = random.randn(50, 2) + 2 point_bboxes = [] for i in range(len(points)): point_bboxes.append(point_bounding_box(points[i], padding=0.05)) expected_overlap = 0 for point_bbox in point_bboxes: expected_overlap += box.overlap(point_bbox) qtree = make_qtree(point_bboxes) overlap = fast_overlap(box, qtree) self.assertAlmostEqual(expected_overlap, overlap)
def _initialise(ax, anchor_x, anchor_y, labels, time, padding): renderer = ax.get_figure().canvas.get_renderer() # force a draw to get accurate axis bound ax.draw(renderer) axes_bbox = Box(*ax.get_window_extent(renderer).get_points().ravel()) text_objects = [] text_strings = [] anchors = [] t_o = [] other_points = [] for x, y, label in zip(anchor_x, anchor_y, labels): point = np.array([x, y]) if label: anchor = np.array([x, y]) anchors.append(anchor) text_objects.append(ax.text(x, y, label, size=12)) text_strings.append(label) else: other_points.append(point) # Trigger draw event so text boxes get correct locations ax.draw(renderer) trans_data = ax.transData anchors_transformed = [] for anchor in anchors: anchors_transformed.append(trans_data.transform(anchor)) label_bboxes = collect_text_bboxes(text_objects, renderer, padding=padding) # Delete all the text objects, we don't need them no more for now #删除textobjects for text_obj in text_objects: text_obj.remove() # Collect other bboxes to avoid other_bboxes = collect_point_bboxes(ax, anchors + other_points, padding=padding) r = collect_point_r(ax, anchors + other_points, padding=padding) return anchors, anchors_transformed, axes_bbox, label_bboxes, other_bboxes, text_strings, text_objects,r
def _draw_annotations(date, SiW, ZhiY, label, v, vp, box, newj, r, hidepro, hideprofe, ax, anchors, offsets, text_strings, label_bboxes, other_bboxes, padding, draw_bboxes=True, **kwargs): # print(r) # print(label) newbbox = [] nanchors = [] distance1 = 0 distance2 = 0 distance3 = 0 distance4 = 0 distance5 = 0 t = 0.1 deltatime = 1 trans = ax.transData.inverted() + ax.transData numpro = [] for k in range(len(label)): dk = [i for i, x in enumerate(text_strings) if( x == label[k])] numpro.append(dk[0]) text_strings = ['AnHui', 'AoMen', 'BeiJing', 'FuJian', 'GanSu', 'GuangDong', 'GuangXi', 'GuiZhou', 'HaiNan', 'HeBei', 'HeNan', 'HeiLongJiang', 'HuBei', 'HuNan', 'JiLin', 'JiangSu', 'JiangXi', 'LiaoNing', 'NeiMengGu', 'NingXia', 'QingHai', 'ShanDong', 'ShanXi', 'ShanXi', 'ShangHai', 'SiChuan', 'TaiWan', 'TianJin', 'XiZang', 'XiangGang', 'XinJiang', 'YunNan', 'ZheJiang', 'ChongQing'] if(v == 0): for text, anchor, offset, bbox, radius, siw, zhiy,otherbox in zip(text_strings, anchors, offsets, label_bboxes, r, SiW, ZhiY, other_bboxes): bbox = bbox.translate(*offset) nanchors.append(ax.transData.transform(anchor)) ax.annotate(text, xy=anchor, xytext=(bbox.x_min+2, bbox.midpoint()[1]-2), xycoords= 'data', textcoords=trans, arrowprops=dict(arrowstyle='-', connectionstyle="arc3", color='black', alpha=.3, ), fontsize=12, ) newbbox.append(bbox) if draw_bboxes: patch = bbox.rectangle_patch(fill=True, color='r', transform=trans, alpha=0) ax.add_patch(patch) # patch = otherbox.rectangle_patch(fill=True, color='r', # transform=trans, alpha=0) # ax.add_patch(patch) if(siw == 0.0 and zhiy == 0.0): a3 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, 360, (6+radius)/2, color='blue', transform=trans) ax.add_patch(a3) else: thelta1 = 360*(siw/(siw+zhiy)) thelta2 = 360*(zhiy/(siw+zhiy)) # print(thelta1,thelta2) a1 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, thelta1, (6+radius)/2, color='crimson', transform=trans) a2 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, thelta1, 360,(6+radius)/2, color='lightgreen', transform=trans) ax.add_patch(a1) ax.add_patch(a2) else: bboxes = [] for i in range(len(box)): bboxes.append(Box(float(box[i][0]), float(box[i][1]), float(box[i][2]), float(box[i][3]))) for text, anchor, offset, bbox, radius, siw, zhiy, otherbox in zip(text_strings, anchors, v, bboxes, r, SiW, ZhiY,other_bboxes): bbox = bbox.translate(float(offset[0])*t, float(offset[1])*t) nanchors.append(ax.transData.transform(anchor)) newbbox.append(bbox) #显示不隐藏的与隐藏的点 showtext_strings = [] showanchors = [] showv = [] showbboxes = [] showr = [] showSiW = [] showZhiY = [] showother_bboxes = [] hideshowtext_strings = [] hideshowanchors = [] hideshowv = [] hideshowbboxes = [] hideshowr = [] hideshowSiW = [] hideshowZhiY = [] hideshowother_bboxes = [] if(hidepro != []): for i in range(len(text_strings)): if (i in hidepro) and (i not in hideprofe): hideshowtext_strings.append(text_strings[i]) hideshowanchors.append(anchors[i]) hideshowv.append(v[i]) hideshowbboxes.append(bboxes[i]) hideshowr.append(r[i]) hideshowSiW.append(SiW[i]) hideshowZhiY.append(ZhiY[i]) hideshowother_bboxes.append(other_bboxes[i]) if i not in hidepro: showtext_strings.append(text_strings[i]) showanchors.append(anchors[i]) showv.append(v[i]) showbboxes.append(bboxes[i]) showr.append(r[i]) showSiW.append(SiW[i]) showZhiY.append(ZhiY[i]) showother_bboxes.append(other_bboxes[i]) #显示feature for text, anchor, offset, bbox, radius, siw, zhiy, otherbox in zip(text_strings, anchors, v, bboxes, r, SiW, ZhiY, other_bboxes): bbox = bbox.translate(float(offset[0])*t, float(offset[1])*t) if draw_bboxes: patch = bbox.rectangle_patch(fill=True, color='r', transform=trans, alpha=0) ax.add_patch(patch) # patch = otherbox.rectangle_patch(fill=True, color='b', # transform=trans, alpha=0) # ax.add_patch(patch) if(siw == 0.0 and zhiy == 0.0): a3 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, 360, (6+radius)/2, color='blue', transform=trans) ax.add_patch(a3) else: thelta1 = 360*(siw/(siw+zhiy)) thelta2 = 360*(zhiy/(siw+zhiy)) a1 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, thelta1, (6+radius)/2, color='crimson', transform=trans) a2 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, thelta1, 360, (6+radius)/2, color='lightgreen', transform=trans) ax.add_patch(a1) ax.add_patch(a2) #显示不隐藏的 for text, anchor, offset, bbox, radius, siw, zhiy, otherbox in zip(showtext_strings, showanchors, showv, showbboxes, showr, showSiW, showZhiY, showother_bboxes): bbox = bbox.translate(float(offset[0])*t, float(offset[1])*t) ax.annotate(text, xy=ax.transData.transform(anchor), xytext=(bbox.x_min+2, bbox.midpoint()[1]-2), xycoords=trans, textcoords=trans, arrowprops=dict(arrowstyle='-', connectionstyle="arc3", color='black', alpha=.3, ), fontsize=12, ) if draw_bboxes: patch = bbox.rectangle_patch(fill=True, color='r', transform=trans, alpha=0) ax.add_patch(patch) patch = otherbox.rectangle_patch(fill=True, color='b', transform=trans, alpha=0) ax.add_patch(patch) if(siw == 0.0 and zhiy == 0.0): a3 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, 360, (6+radius)/2, color='blue', transform=trans) ax.add_patch(a3) else: thelta1 = 360*(siw/(siw+zhiy)) thelta2 = 360*(zhiy/(siw+zhiy)) a1 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, thelta1, (6+radius)/2, color='crimson', transform=trans) a2 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, thelta1, 360, (6+radius)/2, color='lightgreen', transform=trans) ax.add_patch(a1) ax.add_patch(a2) else: for text, anchor, offset, bbox, radius, siw, zhiy, otherbox in zip(text_strings, anchors, v, bboxes, r, SiW, ZhiY, other_bboxes): bbox = bbox.translate(float(offset[0])*t, float(offset[1])*t) ax.annotate(text, xy=ax.transData.transform(anchor), xytext=(bbox.x_min+2, bbox.midpoint()[1]-2), xycoords=trans, textcoords=trans, arrowprops=dict(arrowstyle='-', connectionstyle="arc3", color='black', alpha=.3, ), fontsize=12, ) if draw_bboxes: patch = bbox.rectangle_patch(fill=True, color='r', transform=trans, alpha=0) ax.add_patch(patch) # patch = otherbox.rectangle_patch(fill=True, color='b', # transform=trans, alpha=0) # ax.add_patch(patch) if(siw == 0.0 and zhiy == 0.0): a3 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, 360, (6+radius)/2, color='blue', transform=trans) ax.add_patch(a3) else: thelta1 = 360*(siw/(siw+zhiy)) thelta2 = 360*(zhiy/(siw+zhiy)) a1 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, 0, thelta1, (6+radius)/2, color='crimson', transform=trans) a2 = bbox.Wedge_patch( ax.transData.transform(anchor)[0], ax.transData.transform(anchor)[1], 6+radius, thelta1, 360, (6+radius)/2, color='lightgreen', transform=trans) ax.add_patch(a1) ax.add_patch(a2) overlap_area = 0 n_intersecting_lines = 0 for (bbox, anchor), (other_bbox, other_anchor) in itertools.combinations( zip(newbbox, nanchors), 2): overlap = bbox.overlap(other_bbox) overlap_area += overlap anchor_x, anchor_y = anchor closest_x, closest_y = bbox.x_min+2, bbox.midpoint()[1]-2 other_anchor_x, other_anchor_y = other_anchor other_closest_x, other_closest_y = other_bbox.x_min+2, other_bbox.midpoint()[1]-2 # penalty for intersecting lines if line_intersection(anchor_x, anchor_y, closest_x, closest_y, other_anchor_x, other_anchor_y, other_closest_x, other_closest_y): n_intersecting_lines += 1 # 引线相交的个数 distances_to_anchor = 0 non_label_overlap_area = 0 other_bboxes_qtree = make_qtree(other_bboxes) for bbox, anchor in zip(newbbox, nanchors): # compute penalty for distance away from anchor 标签到点的距离 distances_to_anchor += pow((bbox.midpoint()[0]-anchor[0]), 2)+pow( (bbox.midpoint()[1]-anchor[1]), 2) non_label_overlap_area += fast_overlap(bbox, other_bboxes_qtree) print(date) # with open('D:/Google Downloads/yiqing/evaluate/li.txt', 'a')as fi: # fi.write(str(overlap_area)+','+str(non_label_overlap_area)+','+str(distances_to_anchor)+','+str(n_intersecting_lines)+'\n') # mx = [] # my = [] # px = [] # py = [] # for i in range(len(newbbox)): # px.append(nanchors[i][0]) # py.append(nanchors[i][1]) # mx.append(newbbox[i].midpoint()[0]) # my.append(newbbox[i].midpoint()[1]) # for i in range(len(newbbox)): # with open('D:/Google Downloads/yiqing/evaluate/smooth li.txt', 'a')as fi: # fi.write(str(round(mx[i]-px[i], 2))+','+str(round(my[i]-py[i], 2))+',') # with open('D:/Google Downloads/yiqing/evaluate/smooth li.txt', 'a')as fi: # fi.write('\n') m1 = 30 #label-label m2 = 17 #feature-label m3 = 150 # feature future mdis = 18 #mpull clabel = 40 cpoint = 50 cpull = 20 cjiao = 0 cclosed = 15 ctime = 1 # clabel = 1 # cpoint = 1 # cpull = 20 # cjiao = 20 # cclosed = 15 # ctime = 1 cf = 6 k = 0 R = 6 #初始圆环半径 priority = [] #优先级 priorindex = [] #优先级索引 #priority for i in range(len(newbbox)): if(v != 0 ): deltaLeiJ = abs((nanchors[i][0]-float(vp[i][0]))/nanchors[i][0]) vSiW = SiW[i]/nanchors[i][0] nLeiJ = nanchors[i][0] priority.append({'index':i,'pro':text_strings[i],'sum':deltaLeiJ+vSiW+(1-(1/nLeiJ))}) sort = sorted(priority, key=lambda x: x['sum'],reverse=True) if(v != 0): for i in range(len(newbbox)): index = sorted(priority, key=lambda x: x['sum'], reverse=True)[i]['index'] priorindex.append(index) hide = [] hidefeature = [] labelscore = [] for i in range(len(newbbox)): label_score = 0 for j in range(len(newbbox)): if(j != i and v != 0): # 判断label_label overlap overlap1 = newbbox[i].overlap(newbbox[j]) if overlap1 > 50 and(priorindex.index(i) > priorindex.index(j)): # print(text_strings[newbbox.index(bbox)],text_strings[newbbox.index(other_bbox)]) label_score += 1 # 判断label_feature overlap overlap2 = newbbox[i].overlap(other_bboxes[j]) if overlap2 > 50: label_score += 1 labelscore.append(label_score) featurescore = [] for i in range(len(other_bboxes)): feature_score = 0 for j in range(len(other_bboxes)): if(j != i and v != 0): # 判断feature_label overlap overlap1 = other_bboxes[i].overlap(newbbox[j]) # if overlap1 > 1 and(priorindex.index(i) > priorindex.index(j)): if overlap1 > 50: # print(text_strings[newbbox.index(bbox)],text_strings[newbbox.index(other_bbox)]) feature_score += 1 # print(date,text_strings[i],text_strings[j]) featurescore.append(feature_score) for i in range(len(labelscore)): if(labelscore[i] > 0 or featurescore[i] > 0): hide.append(i) if(featurescore[i] > 0): hidefeature.append(i) newhide = [] newhidefe = [] for i in hide: if i not in newhide: newhide.append(i) for i in hidefeature: if i not in newhidefe: newhidefe.append(i) if(hide!= []): with open('C:/Users/lenovo/Desktop/Test province/score.txt', 'w')as fi: fi.write(str(newhide)) else: with open('C:/Users/lenovo/Desktop/Test province/score.txt', "r+") as f: read_data = f.read() f.seek(0) f.truncate() if(hidefeature != []): with open('C:/Users/lenovo/Desktop/Test province/score-f.txt', 'w')as fi: fi.write(str(newhidefe)) else: with open('C:/Users/lenovo/Desktop/Test province/score-f.txt', "r+") as f: read_data = f.read() f.seek(0) f.truncate() for i in range(len(newbbox)): # collision F1 = [0, 0] #label collision F2 = [0, 0] #feature collision F3 = [0, 0] #pull F4 = [0, 0] #jiaodian pull F5 = [0, 0] #friction F6 = [0, 0] #ftime F7 = [0, 0] #point closed collision vector1 = [0, 0] vector2 = [0, 0] vector3 = [0, 0] vector4 = [0, 0] vector5 = [0, 0] vector6 = [0, 0] hide = [] for j in range(len(newbbox)): if(j != i): if(v == 0): distance1 = np.linalg.norm(newbbox[j].midpoint()-newbbox[i].midpoint())-( newbbox[i].height+newbbox[j].height)/2 vector1 = (newbbox[i].midpoint() - newbbox[j].midpoint())/np.linalg.norm(newbbox[i].midpoint() - newbbox[j].midpoint()) F1 += min((distance1/m1)-1,0)*(-vector1) else: if(priorindex.index(i) < priorindex.index(j)): distance1 = np.linalg.norm(newbbox[j].midpoint()-newbbox[i].midpoint())-( newbbox[i].height+newbbox[j].height)/2 vector1 = (newbbox[i].midpoint() - newbbox[j].midpoint())/np.linalg.norm(newbbox[i].midpoint() - newbbox[j].midpoint()) # print(min((distance1/m1)-1, 0)*(-vector1)) F1 += min((distance1/m1)-1,0)*(-vector1) else: F1 += numpy.array([0.0, 0.0]) distance2 = np.linalg.norm(newbbox[i].midpoint()-nanchors[j])-(newbbox[i].height)/2 vector2 = (newbbox[i].midpoint()-nanchors[j]) / \ np.linalg.norm(newbbox[i].midpoint()-nanchors[j]) F2 += min((distance2/(m2+R+r[i]))-1,0)*(-vector2) # pull distance3 = np.linalg.norm(newbbox[i].midpoint()-nanchors[i])-(newbbox[i].height)/2 vector3 = (newbbox[i].midpoint()-nanchors[i]) / \ np.linalg.norm(newbbox[i].midpoint()-nanchors[i]) if(distance3 - mdis <= 0): F3 = 0 else: F3 += - \ (math.log( np.linalg.norm(newbbox[i].midpoint()-nanchors[i])-(newbbox[i].height)/2-mdis+1))*vector3 # point closed collision distance6 = (newbbox[i].width)/2+(R+r[i])-np.linalg.norm(newbbox[i].midpoint()-nanchors[i]) # print(distance6) if(distance6 > 0): F7 += (math.log( np.linalg.norm(abs(newbbox[i].midpoint()-nanchors[i])-((newbbox[i].width)/2+(R+r[i])))+1))*vector3 # F7 = 0 else: F7 = 0 if(i not in numpro): # jiao dian pull F4 = 0 else: midx = nanchors[numpro[k]][0]+float(newj[k][0]) midy = nanchors[numpro[k]][1]+float(newj[k][1]) distance4 = max( abs(newbbox[numpro[k]].midpoint()-[midx, midy]))-(newbbox[numpro[k]].height)/2 vector4 = (newbbox[numpro[k]].midpoint()-[midx, midy]) / \ np.linalg.norm(newbbox[numpro[k]].midpoint() - [midx, midy]) # F4 = 0 F4 += - \ (math.log( np.linalg.norm(abs(newbbox[numpro[k]].midpoint()-[midx, midy])-(newbbox[numpro[k]].height)/2)+1))*vector4 k += 1 if(v == 0): F5 = 0 F = clabel*F1+cpoint*F2+cpull*F3+cjiao*F4+cf*F5 V = 0+F*t with open('C:/Users/lenovo/Desktop/Test province/v.txt', 'a')as fi: fi.write(str(newbbox[i])+'\n'+str(np.round(V,3))+'\n') with open('C:/Users/lenovo/Desktop/Test province/vp.txt', 'a')as fi: fi.write(str(nanchors[i])+'\n') else: # f deltapi = np.array(nanchors[i])-[float(vp[i][0]), float(vp[i][1])] f5 = -([float(v[i][0]), float(v[i][1])]-(deltapi/t)) F5 += f5 # ftime for j in range(len(newbbox)): if(j != i): deltapj = np.array( nanchors[j])-[float(vp[j][0]), float(vp[j][1])] vi = np.linalg.norm(deltapi/t) vj = np.linalg.norm(deltapj/t) newanchors = nanchors[j] + (deltapj/t)*deltatime distance5 = np.linalg.norm( newbbox[i].midpoint()-newanchors)-(newbbox[i].height)/2 vector5 = (newbbox[i].midpoint() - newanchors) / \ np.linalg.norm(newbbox[i].midpoint() - newanchors) cosangle = cosVector(deltapi/t, deltapj/t) # print('angle',cosangle) F6 += vector5 * math.log(max(abs(vj/vi), abs(vi/vj))+1) * deltatime*max(1-(distance5/m3), 0)*(math.pow(math.e,cosangle)) # F6 = 0 with open('C:/Users/lenovo/Desktop/Test province/deltat.txt', 'a')as fi: fi.write( str(date)+' '+str(text_strings[i])+str(text_strings[j])+str(F6)+'\n') F = clabel*F1+cpoint*F2+cpull*F3+cjiao*F4+cf*F5+ctime*F6+cclosed*F7 V = [float(v[i][0]), float(v[i][1])]+F*t with open('C:/Users/lenovo/Desktop/Test province/v.txt', 'a')as fi: fi.write(str(newbbox[i])+'\n'+str(np.round(V,3))+'\n') with open('C:/Users/lenovo/Desktop/Test province/vp.txt', 'a')as fi: fi.write(str(nanchors[i])+'\n')
def test_angle_to_point(self): point = [0, 0] ur = Box(1, 1, 2, 2) angle_ur = np.pi / 4 ul = Box(-2, 1, -1, 2) angle_ul = 3 / 4 * np.pi ll = Box(-2, -2, -1, -1) angle_ll = -3 / 4 * np.pi lr = Box(1, -2, 2, -1) angle_lr = -np.pi / 4 self.assertAlmostEqual(angle_ur, ur.angle_to_point(point)) self.assertAlmostEqual(angle_ul, ul.angle_to_point(point)) self.assertAlmostEqual(angle_ll, ll.angle_to_point(point)) self.assertAlmostEqual(angle_lr, lr.angle_to_point(point))
def test_distance_to_point_inside_box(self): box = Box(0, 0, 1, 1) point = np.array([0.1, 0.3]) expected_distance = 0 self.assertEqual(expected_distance, box.distance_to_point(point))
def test_area(self): box = Box(-1.3, -5.4, 2.4, -3.2) area = box.width * box.height self.assertEqual(area, box.area())
def test_height(self): box = Box(-1.3, -5.4, 2.4, -3.2) expected_height = -3.2 - (-5.4) self.assertEqual(expected_height, box.height)
def test_overlap(self): box1 = Box(-5, -3, 6, 1) box2 = Box(-2, 0, 1, 3) box3 = Box(-7, -5, -3, -2) intersection12 = Box(-2, 0, 1, 1).area() intersection13 = Box(-5, -3, -3, -2).area() intersection23 = 0.0 self.assertEqual(box1.overlap(box2), intersection12) self.assertEqual(box2.overlap(box1), intersection12) self.assertEqual(box1.overlap(box3), intersection13) self.assertEqual(box3.overlap(box1), intersection13) self.assertEqual(box2.overlap(box3), intersection23) self.assertEqual(box3.overlap(box2), intersection23) self.assertEqual(box1.overlap(box1), box1.area())
def test_width(self): box = Box(-1.3, -5.4, 2.4, -3.2) expected_width = 2.4 - (-1.3) self.assertEqual(expected_width, box.width)