def __distance_prism_prism(self, prism1, prism2): # return minimal distance between facets # if one does not contain another ac = AffinityChecker() if ac.check(prism1, prism2) or ac.check(prism2, prism1): return 0 prism1_facets = [prism1.top_facet, prism1.bottom_facet] for i in range(len(prism1.top_facet.vertices)): side_facet1 = PolygonRegular([prism1.top_facet.vertices[i], prism1.top_facet.vertices[i - 1], prism1.bottom_facet.vertices[i - 1], prism1.bottom_facet.vertices[i]]) prism1_facets.append(side_facet1) prism2_facets = [prism2.top_facet, prism2.bottom_facet] for i in range(len(prism2.top_facet.vertices)): side_facet2 = PolygonRegular([prism2.top_facet.vertices[i], prism2.top_facet.vertices[i - 1], prism2.bottom_facet.vertices[i - 1], prism2.bottom_facet.vertices[i]]) prism2_facets.append(side_facet2) distance = self.__distance_polygon_polygon min_distance = distance(prism1_facets[0], prism2_facets[0]) # start value for i, facet1 in enumerate(prism1_facets): for j, facet2 in enumerate(prism2_facets): min_distance = min(min_distance, distance(facet1, facet2)) return min_distance
def test_inits(): # vec inits: from 3 floats/ints + vec = Vector(1, 1.0, 1) # point inits: from pt + # from vec + # from 3 floats/ints + pt1 = Point(0, 0, 0) pt2 = Point(1, 0, 0) pt3 = Point(0, 1, 0) pt4 = Point(1, 1, 0) pt5 = Point(0, 0, 1) pt6 = Point(1, 0, 1) pt7 = Point(0, 1, 1) pt8 = Point(1, 1, 1) pt9 = Point(vec) pt10 = Point(1, 1.0, 1.0) # line inits: from 2 pts + # from seg + # from pt, vec + line = Line(pt1, pt2) segment1 = Segment(pt1, pt2) line3 = Line(segment1) line1 = Line(pt1, vec) # segment inits: from 2 pts + # from vec + # from 3 floats/ints + segment = Segment(pt1, pt2) segment3 = Segment(vec) segment4 = Segment(1.0, 1, 1.0) # plane inits: from 3 pts + # from pt, vec + # from 4 floats/ints + # from 2 vecs + # from 2 segs + # from 2 lines - # from vec / seg / line and vec / seg / line seem to be + plane = Plane(pt1, pt2, pt3) plane1 = Plane(1, 1.0, 1, 1) plane2 = Plane(pt1, vec) vec1 = Vector(2, 2, 0) plane3 = Plane(vec, vec1) plane4 = Plane(segment, segment4) plane5 = Plane(line1, line3) plane6 = Plane(vec1, line1) plane7 = Plane(vec1, segment3) # PolygonRegular inits: from vertices + polygon = PolygonRegular([pt1, pt2, pt3, pt4]) polygon1 = PolygonRegular([pt5, pt6, pt7, pt8]) # PrismRegular intis prism = PrismRegular(polygon, polygon1)
def __distance_plane_prism(self, plane, prism): distance = self.__distance_plane_polygon result = min(distance(plane, prism.top_facet), distance(plane, prism.bottom_facet)) # start value for i in range(len(prism.top_facet.vertices)): side_facet = PolygonRegular([prism.top_facet.vertices[i], prism.top_facet.vertices[i - 1], prism.bottom_facet.vertices[i - 1], prism.bottom_facet.vertices[i]]) result = min(result, distance(plane, side_facet)) return result
def test_strs(debug_flag=True): pt = Point(1.5, 1.5, 1) pt8 = Point(1.5, 1.5, 1.5) pt0 = Point(1, 1, 1) pt1 = Point(1, 2, 1) pt2 = Point(2, 2, 1) pt3 = Point(2, 1, 1) pt4 = Point(1, 1, 2) pt5 = Point(1, 2, 2) pt6 = Point(2, 2, 2) pt7 = Point(2, 1, 2) line = Line(pt0, pt2) segment = Segment(pt0, pt2) segment1 = Segment(Point(0, 0, 1), Point(3, 3, 1)) plane = Plane(pt0, pt1, pt2) polygon = PolygonRegular([pt0, pt1, pt2, pt3]) polygon1 = PolygonRegular( [Point(0, 0, 1), Point(0, 3, 1), Point(3, 3, 1), Point(3, 0, 1)]) polygon2 = PolygonRegular([ Point(1.5, 1.5, 1.5), Point(1.5, 1.6, 1.5), Point(1.6, 1.6, 1.5), Point(1.6, 1.5, 1.5) ]) top_facet = PolygonRegular([pt4, pt5, pt6, pt7]) prism = PrismRegular(top_facet, polygon) if debug_flag: print(pt) print(line) print(segment) print(plane) print(polygon) print(prism) return 0
def reparse(): """ Reads geofile and outputs something that is easy to read with cpp code. """ taus = [1.5, 2.5, 5] Ns = [4, 8, 12, 16, 20, 24, 28, 30] for tau in taus: for N in Ns: for attempt in range(5): fname = 'Structures/tau' + str(tau) + 'N' + str(N) + '_' + str( attempt) + '.geo' fout = 'Reparsed/tau' + str(tau) + 'N' + str(N) + '_' + str( attempt) + '.geo' reader = GeoReader(fname) reader.read_polygonal_cylinders_raw() shells = reader.shells shell_prisms = [] f = open(fout, 'w') for shell in shells: shell_prism = RegularPrismFeomGeoPlanes( shell).prism_regular shell_prisms.append(shell_prism) top = shell_prism.top_facet bottom = shell_prism.bottom_facet for pt in top.vertices: s = 'top ' + str(pt.x) + ' ' + str(pt.y) + ' ' + str( pt.z) + '\n' f.write(s) for pt in bottom.vertices: s = 'bottom ' + str(pt.x) + ' ' + str( pt.y) + ' ' + str(pt.z) + '\n' f.write(s) top_poly = PolygonRegular(top.vertices) bot_poly = PolygonRegular(bottom.vertices) dc = DistanceCalculator()
def __distance_polygon_prism(self, polygon, prism): # result = min_facet_polygon_distance # if prism does not contain polygon distance = self.__distance_polygon_polygon result = min(distance(polygon, prism.top_facet), distance(polygon, prism.bottom_facet)) for i in range(len(prism.top_facet.vertices)): side_facet = PolygonRegular([prism.top_facet.vertices[i], prism.top_facet.vertices[i - 1], prism.bottom_facet.vertices[i - 1], prism.bottom_facet.vertices[i]]) result = min(result, distance(polygon, side_facet)) ac = AffinityChecker() if ac.check(polygon, prism): return 0 return result
def __distance_segment_prism(self, segment, prism): distance = self.__distance_segment_polygon result = min(distance(segment, prism.top_facet), distance(segment, prism.bottom_facet)) # side facets: for i in range(len(prism.top_facet.vertices)): if i == 0: j = len(prism.top_facet.vertices) - 1 else: j = i - 1 side_facet = PolygonRegular([prism.top_facet.vertices[i], prism.top_facet.vertices[j], prism.bottom_facet.vertices[j], prism.bottom_facet.vertices[i]]) result = min(result, distance(segment, side_facet)) return result
def __distance_line_prism(self, line, prism): distance = self.__distance_line_polygon min_facet_distance = min(distance(line, prism.top_facet), distance(line, prism.bottom_facet)) for i in range(len(prism.top_facet.vertices)): if i == 0: j = len(prism.top_facet.vertices) - 1 else: j = i - 1 side_facet = PolygonRegular([prism.top_facet.vertices[i], prism.bottom_facet.vertices[i], prism.bottom_facet.vertices[j], prism.top_facet.vertices[j]]) tmp_distance = distance(line, side_facet) min_facet_distance = min(min_facet_distance, tmp_distance) return min_facet_distance
def __distance_point_prism(self, point, prism): ac = AffinityChecker() if ac.check(point, prism): return 0 dist_poly = self.__distance_point_polygon min_distance_to_facet = min(dist_poly(point, prism.top_facet), dist_poly(point, prism.bottom_facet)) for i in range(len(prism.top_facet.vertices)): if i == 0: j = len(prism.top_facet.vertices) - 1 else: j = i - 1 side_facet = PolygonRegular([prism.top_facet.vertices[i], prism.top_facet.vertices[j], prism.bottom_facet.vertices[j], prism.bottom_facet.vertices[i]]) min_distance_to_facet = min(min_distance_to_facet, dist_poly(point, side_facet)) return min_distance_to_facet return -100
def __init__(self, planes): top = planes[0] bottom = planes[1] sides = planes[2:] top_poly_vertices = [] bottom_poly_vertices = [] for i in range(len(sides)): # find intersection of side[i], side[i-1] and top and # intersection of side[i], side[i-1] and bottom side_one = sides[i] side_two = sides[i - 1] # with top: det_system_matrix = Matrix3([ side_one.a, side_one.b, side_one.c, side_two.a, side_two.b, side_two.c, top.a, top.b, top.c ]).det() det_pt_x_matrix = Matrix3([ -side_one.d, side_one.b, side_one.c, -side_two.d, side_two.b, side_two.c, -top.d, top.b, top.c ]).det() det_pt_y_matrix = Matrix3([ side_one.a, -side_one.d, side_one.c, side_two.a, -side_two.d, side_two.c, top.a, -top.d, top.c ]).det() det_pt_z_matrix = Matrix3([ side_one.a, side_one.b, -side_one.d, side_two.a, side_two.b, -side_two.d, top.a, top.b, -top.d ]).det() if det_system_matrix == 0: print('Error in RegularPrismFromGeoPlanes:', 'det is null') return None pt_top = Point(det_pt_x_matrix / det_system_matrix, det_pt_y_matrix / det_system_matrix, det_pt_z_matrix / det_system_matrix) # with bottom: det_system_matrix = Matrix3([ side_one.a, side_one.b, side_one.c, side_two.a, side_two.b, side_two.c, bottom.a, bottom.b, bottom.c ]).det() det_pt_x_matrix = Matrix3([ -side_one.d, side_one.b, side_one.c, -side_two.d, side_two.b, side_two.c, -bottom.d, bottom.b, bottom.c ]).det() det_pt_y_matrix = Matrix3([ side_one.a, -side_one.d, side_one.c, side_two.a, -side_two.d, side_two.c, bottom.a, -bottom.d, bottom.c ]).det() det_pt_z_matrix = Matrix3([ side_one.a, side_one.b, -side_one.d, side_two.a, side_two.b, -side_two.d, bottom.a, bottom.b, -bottom.d ]).det() if det_system_matrix == 0: print('Error in RegularPrismFromGeoPlanes:', 'det is null') return None pt_bottom = Point(det_pt_x_matrix / det_system_matrix, det_pt_y_matrix / det_system_matrix, det_pt_z_matrix / det_system_matrix) top_poly_vertices.append(pt_top) bottom_poly_vertices.append(pt_bottom) top_facet = PolygonRegular(top_poly_vertices) bottom_facet = PolygonRegular(bottom_poly_vertices) self.prism_regular = PrismRegular(top_facet, bottom_facet)
def test_distances(debug_flag=True): dc = DistanceCalculator() pt = Point(0, 0, 0) pt1 = Point(1, 1, 1) pt2 = Point(1, 2, 1) pt3 = Point(2, 2, 1) pt4 = Point(2, 1, 1) pt5 = Point(1, 1, 2) pt6 = Point(1, 2, 2) pt7 = Point(2, 2, 2) pt8 = Point(2, 1, 2) segment = Segment(pt1, pt2) line = Line(pt1, pt2) line1 = Line(pt5, pt6) line2 = Line(Point(0, 0, -1), Point(1, 1, -1)) plane = Plane(pt1, pt2, pt3) plane1 = Plane(pt5, pt6, pt7) plane2 = Plane(Point(0, 0, -1), Point(0, 1, -1), Point(1, 0, -1)) polygon = PolygonRegular([pt1, pt2, pt3, pt4]) polygon1 = PolygonRegular( [Point(1, 1, -1), Point(1, 2, -1), Point(2, 2, -1), Point(2, 1, -1)]) bottom_facet = PolygonRegular([pt5, pt6, pt7, pt8]) prism = PrismRegular(polygon, bottom_facet) prism1 = PrismRegular(polygon.translated(Vector(0, 0, 10)), polygon.translated(Vector(0, 0, 11))) segment1 = Segment(pt1, pt3) segment2 = Segment(pt2, pt4) segment3 = Segment(pt5, pt7) segment4 = Segment(Point(1, 1, -1), Point(1, 2, -1)) if debug_flag: print() # pt-ANY print('pt-pt: ', dc.distance(pt, pt1)) # 1.0 print('pt-line: ', dc.distance(pt, line)) # sqrt(2) print('pt-seg: ', dc.distance(pt, segment)) # sqrt(3) print('pt-plane: ', dc.distance(pt, plane)) # 1.0 print('pt-poly: ', dc.distance(pt, polygon)) # sqrt(3) print('pt-prism: ', dc.distance(pt, prism)) # sqrt(3) # line-ANY print('line-pt: ', dc.distance(line, pt)) # 1.0 print('line-line, 0: ', dc.distance(line, line)) # 0.0 print('line-line, 1: ', dc.distance(line, line1)) # 1.0 print('line-seg: ', dc.distance(line1, segment)) # 1.0 print('line-plane: ', dc.distance(line1, plane)) # 1.0 print('line-poly: ', dc.distance(line1, polygon)) # 1.0 print('line-prism, 0: ', dc.distance(line, prism)) # 0.0 print('line-prism, 0: ', dc.distance(line2, prism)) # 2.0 # segment-ANY print('segment-pt: ', dc.distance(segment, pt)) # sqrt(3) print('segment-line: ', dc.distance(segment, line)) # 0.0 print('segment-line: ', dc.distance(segment, line1)) # 1.0 print('segment-seg: ', dc.distance(segment1, segment2)) # 0.0 print('segment-seg: ', dc.distance(segment, segment3)) # 1.0 print('segment-plane: ', dc.distance(segment3, plane)) # 1.0 print('segment-poly: ', dc.distance(segment3, polygon)) # 1.0 print('segment-prism: ', dc.distance(segment4, prism)) # 2.0 # plane-ANY print('plane-pt: ', dc.distance(plane, pt)) # 1.0 print('plane-line ', dc.distance(plane, line1)) # 1.0 print('plane-seg ', dc.distance(plane, segment4)) # 2.0 print('plane-plane ', dc.distance(plane, plane)) # 0.0 print('plane-plane ', dc.distance(plane, plane1)) # 1.0 print('plane-poly ', dc.distance(plane1, polygon)) # 1.0 print('plane-prism ', dc.distance(plane2, prism)) # 2.0 # polygon-ANY print('polygon-pt: ', dc.distance(polygon, pt)) # sqrt(3) print('polygon-line: ', dc.distance(polygon, line1)) # 1.0 print('polygon-seg: ', dc.distance(polygon, segment4)) # 2.0 print('polygon-plane: ', dc.distance(polygon, plane2)) # 2.0 print('polygon-polygon: ', dc.distance(polygon, polygon)) # 0.0 print('polygon-polygon: ', dc.distance(polygon, bottom_facet)) # 1.0 print('polygon-prism: ', dc.distance(polygon1, prism)) # 2.0 # prism-ANY print('prism-pt ', dc.distance(prism, pt)) # sqrt(3) print('prism-line ', dc.distance(prism, line2)) # 2.0 print('prism-seg ', dc.distance(prism, segment4)) # 2.0 print('prism-plane ', dc.distance(prism, plane2)) # 2.0 print('prism-polygon ', dc.distance(prism, polygon1)) # 2.0 print('prism-prism, 0: ', dc.distance(prism, prism)) # 0.0 print('prism-prism, 1: ', dc.distance(prism, prism1)) # 9.0
def test_affinity(debug_flag=True): pt = Point(1.5, 1.5, 1) pt8 = Point(1.5, 1.5, 1.5) pt0 = Point(1, 1, 1) pt1 = Point(1, 2, 1) pt2 = Point(2, 2, 1) pt3 = Point(2, 1, 1) pt4 = Point(1, 1, 2) pt5 = Point(1, 2, 2) pt6 = Point(2, 2, 2) pt7 = Point(2, 1, 2) line = Line(pt0, pt2) segment = Segment(pt0, pt2) segment1 = Segment(Point(0, 0, 1), Point(3, 3, 1)) plane = Plane(pt0, pt1, pt2) polygon = PolygonRegular([pt0, pt1, pt2, pt3]) polygon1 = PolygonRegular( [Point(0, 0, 1), Point(0, 3, 1), Point(3, 3, 1), Point(3, 0, 1)]) polygon2 = PolygonRegular([ Point(1.5, 1.5, 1.5), Point(1.5, 1.6, 1.5), Point(1.6, 1.6, 1.5), Point(1.6, 1.5, 1.5) ]) top_facet = PolygonRegular([pt4, pt5, pt6, pt7]) prism = PrismRegular(top_facet, polygon) prism1 = PrismRegular( PolygonRegular( [Point(0, 0, 2), Point(0, 3, 2), Point(3, 3, 2), Point(3, 0, 2)]), PolygonRegular( [Point(0, 0, 1), Point(0, 3, 1), Point(3, 3, 1), Point(3, 0, 1)])) ac = AffinityChecker() if debug_flag: print() # point affinity print('\t', 'pt to line', ac.check(pt, line)) # True print('\t', 'pt to segment', ac.check(pt, segment)) # True print('\t', 'pt to plane', ac.check(pt, plane)) # True print('\t', 'pt to polygon', ac.check(pt, polygon)) # True print('\t', 'pt to prism', ac.check(pt8, prism)) # True # line affinity print('\t', 'line to plane', ac.check(line, plane)) # True # segment affinity print('\t', 'segment to line', ac.check(segment, line)) # True print('\t', 'segment to segment', ac.check(segment, segment1)) # True print('\t', 'segment to plane', ac.check(segment, plane)) # True print('\t', 'segment to polygon', ac.check(segment, polygon)) # True print('\t', 'segment to prism', ac.check(segment, prism)) # True # polygon affinity print('\t', 'polygon to plane', ac.check(polygon, plane)) # True print('\t', 'polygon to polygon', ac.check(polygon, polygon1)) # True print('\t', 'polygon to prism', ac.check(polygon2, prism)) # True # prism affinity print('\t', 'prism to prism', ac.check(prism, prism1)) # True