def check_tgc(self, name, expected_points): shape = self.lookup_shape(name) expected = Vector(expected_points) actual = cta.flatten_numbers( [shape.base, shape.height, shape.a, shape.b, shape.c, shape.d]) if not expected.is_same(actual): self.fail("{0} != {1}".format(expected, actual))
def __init__(self, name, sketch, revolve_center=None, revolve_axis=None, radius=None, angle=None, copy=False): Primitive.__init__(self, name=name) self.sketch = sketch self.revolve_center = Vector(revolve_center, copy=copy) if revolve_center else Vector.O3() self.revolve_axis = Vector(revolve_axis, copy=copy) if revolve_axis else Vector.Z3() self.radius = Vector(radius, copy=copy) if radius else Vector.X3() self.angle = 180 if angle is None else float(angle)
def hilbert_pipe(file_name, size=10, recursions=4, dc=0.2, direction=ZP, variant=0, x_vec=(1, 0, 0), y_vec=(0, 1, 0), z_vec=(0, 0, 1)): l = float(size) / (2**recursions) d = dc * l r = d points = [(x, d, 0, r) for x in generate_points(recursions, direction=direction, variant=variant, x_vec=Vector(x_vec) * l, y_vec=Vector(y_vec) * l, z_vec=Vector(z_vec) * l)] with WDB(file_name, "3D Hilbert space filling curve with pipes") as brl_db: pipe_name = "hilbert_3d.s" brl_db.pipe(pipe_name, points=points) region_name = "hilbert_3d.r" brl_db.region(name=region_name, tree=pipe_name, rgb_color=(64, 180, 96))
def __init__(self, name, base=(0, 0, 0), u_vec=(1, 0, 0), v_vec=(0, 1, 0), vertices=None, curves=None, copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.u_vec = Vector(u_vec, copy=copy) self.v_vec = Vector(v_vec, copy=copy) if vertices is None: vertices = [] elif not isinstance(vertices, list): vertices = list(vertices) for i in range(0, len(vertices)): vertices[i] = Vector(vertices[i], copy=copy) self.vertices = vertices if curves is None: curves = [] elif not isinstance(curves, list): curves = list(curves) self.curves = curves for i in xrange(0, len(curves)): self.add_curve_segment(curves[i], segment_index=i, copy=copy)
def hilbert_3D_test(): l = 1 d = 0.2 * l r = d with WDB("hilbert_3d_test.g", "Hilbert-pipe 3D") as brl_db: for o1 in (-1, 1): for o2 in (-1, 1): for crt_dir in NEGATE: points_generator = generate_points(2, direction=crt_dir, order=(o1, o2), x_vec=(l, 0, 0), y_vec=(0, l, 0), z_vec=(0, 0, l)) segments = [(x, d, 0, r) for x in points_generator] shape_name = "hilbert_pipe_{}{}{}{}{}.s".format( o1, o2, *crt_dir) region_name = "hilbert_pipe_{}{}{}{}{}.r".format( o1, o2, *crt_dir) if min(crt_dir) < 0: rgb_color = Vector("255, 0, 0") else: rgb_color = Vector("0, 255, 0") if o1 < 0 or o2 < 0: rgb_color *= 0.3 rgb_color += 60 brl_db.pipe(shape_name, points=segments) brl_db.region(name=region_name, tree=shape_name, rgb_color=tuple([int(c) for c in rgb_color]))
def __init__(self, name, base=(0, 0, 0), height=(0, 0, 1), a_vec=(0, 1, 0), b_mag=0.5, base_neck_ratio=0.2, copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.height = Vector(height, copy=copy) self.a_vec = Vector(a_vec, copy=copy) self.b_mag = b_mag self.base_neck_ratio = base_neck_ratio
def angle_from_points(p1, p2, p3): """ Calculates the angle p1-p2-p3. The result is in the range [0, pi] """ p1 = Vector(p1, copy=False) p2 = Vector(p2, copy=False) p3 = Vector(p3, copy=False) return Triangle.angle_between_vectors(p1 - p2, p3 - p2)
def __init__(self, name, center=(0, 0, 0), normal=(1, 0, 0), magnitude=1, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.normal = Vector(normal, copy=copy) self.magnitude = magnitude
def generate_points(iterations, start_point=(0, 0, 0), u_vec=(0, 1, 0), v_vec=(0, 0, 1)): crt_point = Vector(start_point, copy=True) u_vec = Vector(u_vec, copy=False) v_vec = Vector(v_vec, copy=False) yield crt_point for x in generate_steps(iterations): crt_point = crt_point + (x[0] * u_vec) + (x[1] * v_vec) yield crt_point
def __init__(self, name, base=(0, 0, 0), height=(0, 0, 1), r_base=0.5, r_end=0.2, copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.height = Vector(height, copy=copy) self.r_base = r_base self.r_end = r_end
def __init__(self, name, center=(0, 0, 0), n=(0, 0, 1), r_revolution=1, r_cross=0.2, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.n = Vector(n, copy=copy) self.r_revolution = r_revolution self.r_cross = r_cross
def __init__(self, name, base=(0, 0, 0), height=(-1, 0, 0), breadth=(0, 0, 1), half_width=0.5, copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.height = Vector(height, copy=copy) self.breadth = Vector(breadth, copy=copy) self.half_width = half_width
def __init__(self, name, center=(0, 0, 0), n=(0, 0, 1), s_major=(0, 0.5, 0.5), r_revolution=1, r_minor=0.2, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.n = Vector(n, copy=copy) self.s_major = Vector(s_major, copy=copy) self.r_revolution = r_revolution self.r_minor = r_minor
class Superell(Primitive): def __init__(self,name, center=(0, 0, 0), a=(1, 0, 0), b=(0, 1, 0), c=(0, 0, 1), n=0, e=0, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy) self.a = Vector(a, copy) self.b = Vector(b, copy) self.c = Vector(c, copy) self.n = n self.e = e def __repr__(self): result = "{}({}, v={}, a={}, b={}, c={}, n={}, e={} )" return result.format( self.__class__.__name__, self.name, repr(self.center), repr(self.a), repr(self.b), repr(self.c), self.n, self.e ) def update_params(self, params): params.update({ "center": self.center, "a": self.a, "b": self.b, "c": self.c, "n": self.n, "e": self.e }) def copy(self): return Superell(self.name, self.center, self.a, self.b, self.c, self.n, self.e, copy=True) def has_same_data(self, other): return self.e == other.e and \ self.n == other.n and \ self.center.is_same(other.center) and \ self.a.is_same(other.a) and \ self.b.is_same(other.b) and \ self.c.is_same(other.c) @staticmethod def from_wdb(name, data): return Superell( name=name, center=data.v, a=data.a, b=data.b, c=data.c, n=data.n, e=data.e, )
def __init__(self, name, base=(0, 0, 0), height=(0, 0, 1), n_major=(0, 1, 0), r_major=1, r_minor=0.5, copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.height = Vector(height, copy=copy) self.n_major = Vector(n_major, copy=copy) self.r_major = r_major self.r_minor = r_minor
def __init__(self, name, sketch, base=None, height=None, u_vec=None, v_vec=None, copy=False): Primitive.__init__(self, name=name) self.sketch = sketch self.base = Vector(base, copy=copy) if base else Vector.O3() self.height = Vector(height, copy=copy) if height else Vector.Z3() self.u_vec = Vector(u_vec, copy=copy) if u_vec else Vector.X3() self.v_vec = Vector(v_vec, copy=copy) if v_vec else Vector.Y3()
def from_wdb(name, data): vertices = [] for i in range(0, data.vert_count): vertices.append(Vector(data.verts[i])) result = Sketch( name=name, base=Vector(data.V), u_vec=Vector(data.u_vec), v_vec=Vector(data.v_vec), vertices=vertices ) curves = data.curve for i in range(0, curves.count): curve = curves.segment[i] magic = librt.cast(curve, librt.POINTER(librt.struct_line_seg)).contents.magic reverse = bool(curves.reverse[i]) result.add_curve_segment(magic, curve, reverse=reverse) return result
def __init__(self,name, center=(0, 0, 0), a=(1, 0, 0), b=(0, 1, 0), c=(0, 0, 1), n=0, e=0, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy) self.a = Vector(a, copy) self.b = Vector(b, copy) self.c = Vector(c, copy) self.n = n self.e = e
def __init__(self, name, mode=1, orientation=1, flags=0, vertices=None, faces=None, copy=False): Primitive.__init__(self, name=name) self.mode = mode self.orientation = orientation self.flags = flags if vertices is None: vertices = [] elif not isinstance(vertices, list): vertices = list(vertices) for i in range(0, len(vertices)): vertices[i] = Vector(vertices[i], copy=copy) self.vertices = vertices if faces is None: faces = [] elif not isinstance(faces, list): curves = list(faces) self.faces = faces for i in range(0, len(faces)): self.add_face(faces[i])
def __new__(cls, name, base=(0, 0, 0), height=(0, 0, 1), radius=1, copy=False): base = Vector(base, copy=copy) height = Vector(height, copy=copy) a = height.construct_normal() b = a.cross(height) return TGC(name=name, base=base, height=height, a=a, b=b, c=a, d=b, copy=False)
class Grip(Primitive): def __init__(self, name, center=(0, 0, 0), normal=(1, 0, 0), magnitude=1, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.normal = Vector(normal, copy=copy) self.magnitude = magnitude def __repr__(self): result = "{}({}, center={}, normal={}, magnitude={})" return result.format(self.__class__.__name__, self.name, self.center, self.normal, self.magnitude) def update_params(self, params): params.update({ "center": self.center, "normal": self.normal, "magnitude": self.magnitude }) def copy(self): return Grip(self.name, self.center, self.normal, self.magnitude, copy=True) def has_same_data(self, other): return self.center.is_same(other.center) and \ self.normal.is_same(other.normal) and \ self.magnitude == other.magnitude @staticmethod def from_wdb(name, data): return Grip(name=name, center=data.center, normal=data.normal, magnitude=data.mag)
def generate_points(iterations, direction=XP, order=(1, 1), variant=0, start_point=(0, 0, 0), x_vec=(1, 0, 0), y_vec=(0, 1, 0), z_vec=(0, 0, 1)): crt_point = Vector(start_point, copy=True) x_vec = Vector(x_vec, copy=False) y_vec = Vector(y_vec, copy=False) z_vec = Vector(z_vec, copy=False) yield crt_point for step in generate_steps(iterations, direction=direction, order=order, variant=variant): crt_point = crt_point + (step[0] * x_vec) + (step[1] * y_vec) + (step[2] * z_vec) yield crt_point
def __init__(self, name, file_name, x_dim=1, y_dim=1, z_dim=1, low_thresh=0, high_thresh=128, cell_size=(1, 1, 1), mat=Transform.unit(), copy=False): Primitive.__init__(self, name=name) if not os.path.isfile(file_name): raise ValueError("File {} does not exist !".format(file_name)) self.file_name = file_name self.x_dim = x_dim self.y_dim = y_dim self.z_dim = z_dim self.low_thresh = low_thresh self.high_thresh = high_thresh self.cell_size = Vector(cell_size, copy=copy) self.mat = Transform(mat, copy=copy, force=True)
class Grip(Primitive): def __init__(self, name, center=(0, 0, 0), normal=(1, 0, 0), magnitude=1, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.normal = Vector(normal, copy=copy) self.magnitude = magnitude def __repr__(self): result = "{}({}, center={}, normal={}, magnitude={})" return result.format( self.__class__.__name__, self.name, self.center, self.normal, self.magnitude ) def update_params(self, params): params.update({ "center": self.center, "normal": self.normal, "magnitude": self.magnitude }) def copy(self): return Grip(self.name, self.center, self.normal, self.magnitude, copy=True) def has_same_data(self, other): return self.center.is_same(other.center) and \ self.normal.is_same(other.normal) and \ self.magnitude == other.magnitude @staticmethod def from_wdb(name, data): return Grip( name=name, center=data.center, normal=data.normal, magnitude=data.mag )
def vertex_index(self, value, copy=False): vertex_count = len(self.vertices) if isinstance(value, numbers.Integral): if value > vertex_count - 1: raise ValueError("Invalid vertex index: {}".format(value)) return value value = Vector(value, copy=copy) if len(value) != 2: raise ValueError("Sketches need 2D vertexes, but got: {}".format(value)) for i in range(0, vertex_count): if self.vertices[i].is_same(value): return i self.vertices.append(value) return vertex_count
def __new__(cls, point=(0, 0, 1), field_strength=1, sweat=0, copy=False): """ Parse a MetaballCtrlPoint from multiple possible inputs: >>> x = MetaballCtrlPoint("0, 0, 1", 1, 0) >>> x.point.is_same((0, 0, 1)) True >>> (x.field_strength, x.sweat) == (1, 0) True >>> x[0] is x.point True >>> [x.field_strength, x.sweat] == x[1:] True >>> x is MetaballCtrlPoint(x) True >>> y = MetaballCtrlPoint(x, copy=True) >>> x is y False >>> x.is_same(y) True >>> x.is_same(MetaballCtrlPoint((0, 0, 1), 1, 0)) True >>> x.is_same(MetaballCtrlPoint(point=(0, 0, 1), field_strength=1, sweat=0)) True >>> x.is_same(MetaballCtrlPoint([(0, 0, 1), 1, 0])) True >>> x.is_same(MetaballCtrlPoint(("0, 0, 1", 1, 0))) True """ types.ListType, types.TupleType if isinstance(point, MetaballCtrlPoint): if copy: return MetaballCtrlPoint(*point, copy=True) else: return point result = collections.Sequence.__new__(cls) is_non_string_sequence = isinstance( point, collections.Sequence) and not isinstance(point, str) if is_non_string_sequence and len(point) == 3 and isinstance( point[0], (types.ListType, types.TupleType, types.StringType, Vector)): # this means the parameters were wrapped in a sequence, so we unwrap them point, field_strength, sweat = point result.items = [ Vector(point, copy=copy), float(field_strength), float(sweat) ] return result
def __init__(self, name, base=(0, 0, 0), height=(0, 0, 1), a=(0, 1, 0), b=(0.5, 0, 0), c=(0, 0.5, 0), d=(1, 0, 0), copy=False): Primitive.__init__(self, name=name) self.base = Vector(base, copy=copy) self.height = Vector(height, copy=copy) self.a = Vector(a, copy=copy) self.b = Vector(b, copy=copy) self.c = Vector(c, copy=copy) self.d = Vector(d, copy=copy)
def __new__(cls, name, base=(0, 0, 0), n=(0, 0, 1), h=1, r_base=1, r_top=0.5, copy=False): return TGC(name=name, base=base, height=Vector(n, copy=True) * h, a=(0, -r_base, 0), b=(r_base, 0, 0), c=(0, -r_top, 0), d=(r_top, 0, 0), copy=copy)
def angle_between_vectors(v1, v2): """ Calculates the angle between <v1> and <v2>. The result is in the range [0, pi] On degenerate input (any of the vectors of norm 0) will raise ValueError. """ v1 = Vector(v1, copy=False) v2 = Vector(v2, copy=False) n1 = v1.norm() n2 = v2.norm() if n1 == 0 or n2 == 0: raise ValueError("Can't calculate angle between zero length vectors !") cos_a = v1.dot(v2) / (n1 * n2) cos_a = max(-1.0, min(1.0, cos_a)) return math.acos(cos_a)
def __new__(cls, point, d_outer=0.5, d_inner=0.3, r_bend=1, copy=False): """ Parse a PipePoint from multiple possible inputs: >>> x = PipePoint("0, 1, 2", 3, 2, 5) >>> x.point.is_same((0, 1, 2)) True >>> (x.d_outer, x.d_inner, x.r_bend) == (3, 2, 5) True >>> x[0] is x.point True >>> [x.d_outer, x.d_inner, x.r_bend] == x[1:] True >>> x is PipePoint(x) True >>> y = PipePoint(x, copy=True) >>> x is y False >>> x.is_same(y) True >>> x.is_same(PipePoint((0, 1, 2), 3, 2, 5)) True >>> x.is_same(PipePoint(point=(0, 1, 2), d_inner=2, d_outer=3, r_bend=5)) True >>> x.is_same(PipePoint([(0, 1, 2), 3, 2, 5])) True >>> x.is_same(PipePoint(("0, 1, 2", 3, 2, 5))) True """ if isinstance(point, PipePoint): if copy: return PipePoint(*point, copy=True) else: return point result = collections.Sequence.__new__(cls) is_non_string_sequence = isinstance( point, collections.Sequence) and not isinstance(point, str) if is_non_string_sequence and len(point) == 4: # this means the parameters were wrapped in a sequence, so we unwrap them point, d_outer, d_inner, r_bend = point result.items = [ Vector(point, copy=copy), float(d_outer), float(d_inner), float(r_bend) ] return result
def from_wdb(name, data): points=[] points_head = data.metaball_ctrl_head.forw crt_head = points_head while(1): crt_point = ctypes.cast(crt_head, ctypes.POINTER(librt.wdb_metaballpt)).contents crt_head = crt_point.l.forw points.append(( Vector(crt_point.coord), crt_point.fldstr, crt_point.sweat )) if ctypes.addressof(points_head) == ctypes.addressof(crt_head): points[-1:] = [] break return Metaball( name=name, threshold=data.threshold, method=data.method, points=points, copy=False, )
def __init__( self, name, file_name, x_dim=1, y_dim=1, z_dim=1, low_thresh=0, high_thresh=128, cell_size=(1, 1, 1), mat=Transform.unit(), copy=False, ): Primitive.__init__(self, name=name) if not os.path.isfile(file_name): raise ValueError("File {} does not exist !".format(file_name)) self.file_name = file_name self.x_dim = x_dim self.y_dim = y_dim self.z_dim = z_dim self.low_thresh = low_thresh self.high_thresh = high_thresh self.cell_size = Vector(cell_size, copy=copy) self.mat = Transform(mat, copy=copy, force=True)
class VOL(Primitive): def __init__( self, name, file_name, x_dim=1, y_dim=1, z_dim=1, low_thresh=0, high_thresh=128, cell_size=(1, 1, 1), mat=Transform.unit(), copy=False, ): Primitive.__init__(self, name=name) if not os.path.isfile(file_name): raise ValueError("File {} does not exist !".format(file_name)) self.file_name = file_name self.x_dim = x_dim self.y_dim = y_dim self.z_dim = z_dim self.low_thresh = low_thresh self.high_thresh = high_thresh self.cell_size = Vector(cell_size, copy=copy) self.mat = Transform(mat, copy=copy, force=True) def __repr__(self): result = ( "{}({}, file_name={}, x_dim={}, y_dim={}, z_dim={}, " "low_thresh={}, high_thresh={} cell_size={}, mat={})" ) return result.format( self.__class__.__name__, self.file_name, self.name, self.x_dim, self.y_dim, self.z_dim, self.low_thresh, self.high_thresh, repr(self.cell_size), repr(self.mat), ) def update_params(self, params): params.update( { "file_name": self.file_name, "x_dim": self.x_dim, "y_dim": self.y_dim, "z_dim": self.z_dim, "low_thresh": self.low_thresh, "high_thresh": self.high_thresh, "cell_size": self.cell_size, "mat": self.mat, } ) def copy(self): return VOL( self.name, file_name=self.file_name, x_dim=self.x_dim, y_dim=self.y_dim, z_dim=self.z_dim, low_thresh=self.low_thresh, high_thresh=self.high_thresh, cell_size=self.cell_size, mat=self.mat, copy=True, ) def has_same_data(self, other): if not ( self.file_name == other.file_name, self.x_dim == other.x_dim and self.y_dim == other.y_dim and self.z_dim == other.z_dim and self.low_thresh == other.low_thresh and self.high_thresh == other.high_thresh, ): return False return self.cell_size.is_same(other.cell_size) and np.allclose(self.mat, other.mat) @staticmethod def from_wdb(name, data): return VOL( name=name, file_name=data.file, x_dim=data.xdim, y_dim=data.ydim, z_dim=data.zdim, low_thresh=data.lo, high_thresh=data.hi, cell_size=data.cellsize, mat=cta.transform_from_pointer(data.mat), )
def __init__(self, name, center, a, b, c, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.a = Vector(a, copy=copy) self.b = Vector(b, copy=copy) self.c = Vector(c, copy=copy)
def check_arb(self, name, expected_points): shape = self.lookup_shape(name) expected = Vector(expected_points) if not expected.is_same(shape.points): self.fail("{0} != {1}".format(expected, shape.points))
def test_arbn_defaults(self): shape = self.lookup_shape("arbn.s") expected = Vector((1, 0, 0, 1, -1, 0, 0, 1, 0, 1, 0, 1, 0, -1, 0, 1, 0, 0, 1, 1, 0, 0, -1, 1)) self.assertTrue(expected.is_same(cta.flatten_numbers(shape.planes)))
def check_tgc(self, name, expected_points): shape = self.lookup_shape(name) expected = Vector(expected_points) actual = cta.flatten_numbers([shape.base, shape.height, shape.a, shape.b, shape.c, shape.d]) if not expected.is_same(actual): self.fail("{0} != {1}".format(expected, actual))
class Ellipsoid(Primitive): def __init__(self, name, center, a, b, c, copy=False): Primitive.__init__(self, name=name) self.center = Vector(center, copy=copy) self.a = Vector(a, copy=copy) self.b = Vector(b, copy=copy) self.c = Vector(c, copy=copy) def __repr__(self): return "ELL(name={0}, center={1}, a={2}, b={3}, c={4})".format( self.name, repr(self.center), repr(self.a), repr(self.b), repr(self.c) ) def __iter__(self): yield self.center yield self.a yield self.b yield self.c def update_params(self, params): params.update({ "center": self.center, "a": self.a, "b": self.b, "c": self.c, }) def copy(self): return Ellipsoid(self.name, self.center, self.a, self.b, self.c, copy=True) def has_same_data(self, other): return all(map(Vector.is_same, self, other)) def _get_radius(self): """ Returns the radius if this is a Sphere, None otherwise. Can also be used as a test if this is a sphere: >>> x = Ellipsoid("test", "0, 0, 0", (10, 0, 0), [0, 10, 0], Vector("0, 0, 10")) >>> x.radius 10.0 >>> x.a = Vector("1, 0, 0") >>> x.radius is None True """ mag_a = self.a.norm() mag_b = self.b.norm() mag_c = self.c.norm() if np.allclose((mag_a, mag_b), (mag_b, mag_c)): return mag_a else: return None radius = property(_get_radius) @staticmethod def from_wdb(name, data): return Ellipsoid( name=name, center=data.v, a=data.a, b=data.b, c=data.c, )