def __init_primitive(self, point1_x, point1_y, point1_z, point2_x, point2_y, point2_z): """ Init by prepared params. """ self.begin = Point(point1_x, point1_y, point1_z) self.end = Point(point2_x, point2_y, point2_z)
def __init_primitive(self, point1_x, point1_y, point1_z, point2_x, point2_y, point2_z): """ Init by prepared params. """ self.point1 = Point(point1_x, point1_y, point1_z) self.point2 = Point(point2_x, point2_y, point2_z) dx = point2_x - point1_x dy = point2_y - point1_y dz = point2_z - point1_z self.parallel_vector = Vector(dx, dy, dz)
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 __init_primitive(self, vertices): self.vertices = vertices x = 0 y = 0 z = 0 for pt in vertices: x += pt.x y += pt.y z += pt.z self.N = len(vertices) self.central_angle = 2 * math.pi / self.N self.center = Point(x / self.N, y / self.N, z / self.N) # TODO - check with help of DistanceCalculator #dc = DistanceCalculator() #self.edge_length = dc.distance(vertices[0], vertices[1]) dx = vertices[1].x - vertices[0].x dy = vertices[1].y - vertices[0].y dz = vertices[1].z - vertices[0].z self.edge_length = (dx**2 + dy**2 + dz**2)**0.5 self.outer_radius = self.edge_length / 2 / math.sin( self.central_angle / 2) self.inner_radius = (self.outer_radius**2 - self.edge_length**2 / 4)**0.5 self.containing_plane = Plane(vertices[0], vertices[1], vertices[2]) triangle_area = 0.5 * math.sin( self.central_angle) * self.outer_radius**2 self.area = self.N * triangle_area
def plane_from_brackets(self, brackets): """ Makes a plane from a line called 'brackets': brackets == (pt.x, pt.y, pt.z; normal.x, normal.y, normal.z) Returns a Plane that may be used in mypymath. """ pt_coords = brackets.split('(')[1].split(';')[0].split(', ') ptx = float(pt_coords[0]) pty = float(pt_coords[1]) ptz = float(pt_coords[2]) pt = Point(ptx, pty, ptz) normal_coords = brackets.split('(')[1].split(';')[1].split(')')[0] normal_coords = normal_coords.split(', ') nx = float(normal_coords[0]) ny = float(normal_coords[1]) nz = float(normal_coords[2]) normal = Vector(nx, ny, nz) return Plane(pt, normal)
def __distance_segment_polygon(self, segment, polygon): distance = self.__distance_segment_segment edge = Segment(polygon.vertices[0], polygon.vertices[-1]) min_edge_distance = distance(segment, edge) for i in range(1, len(polygon.vertices)): edge = Segment(polygon.vertices[i], polygon.vertices[i - 1]) min_edge_distance = min(min_edge_distance, distance(segment, edge)) pl = polygon.containing_plane if self.__distance_segment_plane(segment, pl) != 0: return min_edge_distance #ptCross = begin + alpha*Vector(segment) pt = segment.begin vec = Vector(segment) alpha = ((-pl.a*pt.x - pl.b*pt.y - pl.c*pt.z - pl.d) / (vec.x + vec.y + vec.z)) ptCross = Point(pt.x + alpha*vec.x, pt.y + alpha*vec.y, pt.z + alpha*vec.z) if self.__distance_point_polygon(ptCross, polygon) == 0: return 0 return min_edge_distance
def __distance_line_polygon(self, line, polygon): distance = self.__distance_line_segment edge = Segment(polygon.vertices[0], polygon.vertices[-1]) # start value min_distance_to_edge = distance(line, edge) for i in range(1, len(polygon.vertices)): edge = Segment(polygon.vertices[i], polygon.vertices[i - 1]) min_distance_to_edge = min(min_distance_to_edge, distance(line, edge)) pl = polygon.containing_plane vec = line.parallel_vector pt = line.point1 if self.__distance_line_plane(line, pl) != 0: return min_distance_to_edge #ptCross = point1 + alpha*parallel_vector alpha = ((-pl.a*pt.x - pl.b*pt.y - pl.c*pt.z - pl.d) / (vec.x + vec.y + vec.z)) ptCross = Point(pt.x + alpha*vec.x, pt.y + alpha*vec.y, pt.z + alpha*vec.z) ac = AffinityChecker() if ac.check(ptCross, polygon): return 0 return min_distance_to_edge
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 __init__(self, param1, param2, param3=None, param4=None): # (a, b, c, d) or # --- 3 pts --- # (pt1, pt2, pt3) or # (pt, vec) or # --- 4 pts --- # (vec, vec) or # (seg, seg) or # (line, line) self.name = 'plane' if (isinstance(param1, (float, int)) and isinstance(param2, (float, int)) and isinstance(param3, (float, int)) and isinstance(param4, (float, int))): #Init directly by a, b, c, d a = param1 b = param2 c = param3 d = param4 self.__init_primitive(a, b, c, d) return elif isinstance(param1, Point): point1_x = param1.x point1_y = param1.y point1_z = param1.z if isinstance(param2, Vector): # Init by point and normal normal_x = param2.x normal_y = param2.y normal_z = param2.z a = normal_x b = normal_y c = normal_z d = -point1_x * a - point1_y * b - point1_z * c elif isinstance(param2, Point) and isinstance(param3, Point): point1 = param1 point2 = param2 point3 = param3 self.__init_three_points(point1, point2, point3) return elif (isinstance(param1, (Segment, Vector, Line)) and isinstance(param2, (Segment, Vector, Line))): try: # segment point1 = param1.begin point2 = param1.end except: try: # line point1 = param1.point1 point2 = param1.point2 except: # vector point1 = Point(0, 0, 0) point2 = Point(param1) try: # segment point3 = param2.begin point4 = param2.end except: try: # line point3 = param2.point1 point4 = param2.point2 except: # vector point3 = Point(0, 0, 0) point4 = Point(param2) # # Check whether four points are in single plane # by calculating det (vec12, vec13, vec14) # which is proportional to the tetrahedra volume # with vertices in points 1, 2, 3 and 4. # After that it is possible to call __init_three_points # with any 3 of these 4 points # (i've chosen points 1, 2 and 4). # becaues in case of 2 vectors pt1 == pt3, # so they are at a single line. # elements = [ point2.x - point1.x, point2.y - point1.y, point2.z - point1.z, point3.x - point1.x, point3.y - point1.y, point3.z - point1.z, point4.x - point1.x, point4.y - point1.y, point4.z - point1.z ] if Matrix3(elements=elements).det() == 0: self.__init_three_points(point1, point2, point4) else: print('Error in Plane.__init__:', 'there are 4 points that do not lie in a single plane') return None return else: print('Error in Plane.__init__:', 'incorrect type of some argument') return None self.__init_primitive(a, b, c, d)
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 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