def to_global_geometry(self, world_frame=Frame.worldXY()): geometry = self.to_local_geometry_xy() transformed_geometry = [] T = Transformation.from_frame_to_frame(self.frame, world_frame) for part in geometry: transformed_part = [] for point in part: p_point = Point(point['x'], point['y'], point['z']) transformed_part.append(p_point.transformed(T)) transformed_geometry.append(transformed_part) return transformed_geometry
def to_global_geometry(self, world_frame=Frame.worldXY()): geometry = self.to_local_geometry_xy() transformed_geometry = [] gusset_local_frame = Frame([0, 0, 0], [1, 0, 0], [0, 1, 0]) T1 = Transformation.from_frame_to_frame(gusset_local_frame, self.frame) T2 = Transformation.from_frame_to_frame(self.frame, world_frame) transformed_part = [] # default for part in geometry: transformed_part = [] for point in part: p_point = Point(point['x'], point['y'], point['z']) transformed_part.append(p_point.transformed(T1)) transformed_geometry.append(transformed_part) return transformed_geometry
class Part(FromToData, FromToJson, FromToPickle, Datastructure): """Wasp basic Part """ __module__ = 'compas_wasp' #def __init__(self, name, geometry, connections, collider, attributes, dim=None, id=None, field=None): def __init__(self, _name=None, _geometry=None, _connections=[]): super(Part, self).__init__() self.name = _name self.id = None self.geo = _geometry #self.field = field self.connections = [] self.active_connections = [] count = 0 for conn in _connections: conn.part = self.name conn.id = count self.connections.append(conn) self.active_connections.append(count) count += 1 self.transformation = Transformation() self.center = Point(0,0,0) if self.geo is not None: center_coords = self.geo.centroid() self.center = Point(center_coords[0], center_coords[1], center_coords[2]) #self.collider = collider """ ##part size if dim is not None: self.dim = dim else: max_collider_dist = None for coll_geo in self.collider.geometry: for v in coll_geo.Vertices: dist = self.center.DistanceTo(v) if dist > max_collider_dist or max_collider_dist is None: max_collider_dist = dist self.dim = max_collider_dist """ self.parent = None self.children = [] """ self.attributes = [] if len(attributes) > 0: self.attributes = attributes self.is_constrained = False """ ## override Rhino .ToString() method (display name of the class in Gh) def ToString(self): return "WaspPart [name: %s, id: %s]" % (self.name, self.id) @property def data(self): """dict : A data dict representing the part data structure for serialisation. The dict has the following structure: * 'aaa' => dict Note ---- All dictionary keys are converted to their representation value (``repr(key)``) to ensure compatibility of all allowed key types with the JSON serialisation format, which only allows for dict keys that are strings. """ data = {'name' : self.name, 'id' : self.id, 'geo' : self.geo.to_data(), 'connections' : [c.to_data() for c in self.connections], 'active_connections' : self.active_connections, 'transformation' : list(self.transformation), 'parent' : self.parent, 'children' : self.children} return data @data.setter def data(self, data): p_name = data.get('name') or None p_id = data.get('id') or None p_geo = data.get('geo') or {} p_connections = data.get('connections') or [] p_active_connections = data.get('active_connections') or [] p_transformation = data.get('transformation') or [] p_parent = data.get('parent') or None p_children = data.get('children') or [] self.name = p_name self.id = p_id self.geo = Mesh.from_data(p_geo) self.connections = [Connection.from_data(c) for c in p_connections] self.active_connections = p_active_connections self.transformation = Transformation.from_matrix(p_transformation) center_coords = self.geo.centroid() self.center = Point(center_coords[0], center_coords[1], center_coords[2]) self.parent = p_parent self.children = p_children def centroid(self): """Compute the centroid of the block. Returns ------- point The XYZ location of the centroid. """ return centroid_points([self.geo.vertex_coordinates(key) for key in self.geo.vertices()]) ## reset the part and connections according to new provided aggregation rules def reset_part(self, rules): count = 0 self.active_connections = [] for conn in self.connections: conn.generate_rules_table(rules) self.active_connections.append(count) count += 1 ## return a dictionary containing all part data def return_part_data(self): data_dict = {} data_dict['name'] = self.name data_dict['id'] = self.id data_dict['geo'] = self.geo data_dict['connections'] = self.connections data_dict['transform'] = self.transformation #data_dict['collider'] = self.collider data_dict['center'] = self.center data_dict['parent'] = self.parent data_dict['children'] = self.children #data_dict['attributes'] = self.attributes return data_dict ## return a transformed copy of the part def transform(self, trans, transform_sub_parts=False): geo_trans = get_transformed_mesh(self.geo, trans) #collider_trans = self.collider.transform(trans) connections_trans = [] for conn in self.connections: connections_trans.append(conn.transform(trans)) """ attributes_trans = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_trans.append(attr.transform(trans)) """ #part_trans = Part(self.name, geo_trans, connections_trans, collider_trans, attributes_trans, dim=self.dim, id=self.id, field=self.field) part_trans = Part(self.name, geo_trans, connections_trans) part_trans.transformation = trans return part_trans ## return a copy of the part def copy(self): geo_copy = self.geo.copy() #collider_copy = self.collider.copy() connections_copy = [] for conn in self.connections: connections_copy.append(conn.copy()) """ attributes_copy = [] if len(self.attributes) > 0: for attr in self.attributes: attributes_copy.append(attr.copy()) """ #part_copy = Part(self.name, geo_copy, connections_copy, collider_copy, attributes_copy, dim=self.dim, id=self.id, field=self.field) part_copy = Part(self.name, geo_copy, connections_copy) part_copy.transformation = self.transformation return part_copy ## return transformed center point of the part def transform_center(self, trans): center_trans = self.center.transformed(trans) return center_trans ## return transformed collider def transform_collider(self, trans): pass
"""Transformation between two frames. """ from compas.geometry import Point from compas.geometry import Frame from compas.geometry import Transformation F1 = Frame.worldXY() F2 = Frame([1.5, 1, 0], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) P = Point(2, 2, 2) # local point in F1 # transformation between 2 frames F1, F2 T = Transformation.from_frame_to_frame(F1, F2) # Transform geometry (=point P) into another coordinate frame print(P.transformed(T))
faces = list(mesh.faces()) intersections = [] reflections = [] normals = [] centers = [] for ray in rays: base, vector = ray hits = igl.intersection_ray_mesh(ray, mesh) if hits: i = hits[0][0] d = hits[0][3] if i in faces: n = scale_vector(mesh.face_normal(i), -1) x = add_vectors(base, scale_vector(vector, d)) p = Point(*x) r = base.transformed(Reflection.from_plane((p, n))) n = add_vectors(n, x) n = Point(*n) intersections.append(p) reflections.append(r) normals.append(n) centers.append(mesh.face_centroid(i)) # ============================================================================== # Visualisation # ============================================================================== viewer = ObjectViewer() viewer.add(mesh, settings={
import math from compas.geometry import Point from compas.geometry import Quaternion from compas.geometry import Rotation from compas.geometry import Transformation from compas.geometry import Translation # transform with identity matrix x = Transformation() a = Point(1, 0, 0) b = a.transformed(x) assert a == b # translate t = Translation.from_vector([5, 1, 0]) b = a.transformed(t) assert b == [6, 1, 0] # in-place transform r = Rotation.from_axis_and_angle([0, 0, 1], math.pi) a.transform(r) assert str(a) == str(Point(-1.0, 0.0, 0.0)) # rotation from quaternion q = Quaternion(0.918958, -0.020197, -0.151477, 0.363544) assert q.is_unit R = Rotation.from_quaternion(q) assert R.quaternion == q