def depressCrvs(crvs, paths, startPt, radius, sd): newCrvs = [] for i in range(len(crvs)): divPts = rs.DivideCurve(crvs[i], 100) if i < len(crvs) - 1: cntPt01 = centerCrv(crvs[i]) cntPt02 = centerCrv(crvs[i + 1]) horVec = rs.VectorCreate(cntPt01, cntPt02) for j in range(len(divPts)): path = rs.PointClosestObject(divPts[j], paths)[0] param = rs.CurveClosestPoint(path, divPts[j]) close = rs.EvaluateCurve(path, param) dist = rs.Distance(close, divPts[j]) tan = rs.CurveTangent(crvs[i], param) vec = [0, 0, -1] #rs.VectorCrossProduct(horVec,tan) testVec = rs.VectorCreate(cntPt01, divPts[j]) if rs.VectorDotProduct(vec, testVec) < 0: rs.VectorReverse(vec) vec = rs.VectorUnitize(vec) border = 1 entry = 1 if j > len(divPts) / 2: border = rs.Distance(rs.CurveEndPoint(crvs[i]), divPts[j]) else: border = rs.Distance(rs.CurveStartPoint(crvs[i]), divPts[j]) if border < sd * 3: border = border / (sd * 3) entryDist = rs.Distance(startPt, divPts[j]) if entryDist < sd * 10: entry = entryDist / (sd * 10) if dist < sd * 2: val = radius * (bellCrv(dist, sd)) divPts[j] = rs.PointAdd(divPts[j], vec * val * border * entry) newCrvs.append(rs.AddCurve(divPts)) return divPts
def depressCrvs(srf, crvs, paths, startPt, radius, sd): newCrvs = [] for i in range(len(crvs)): divPts = rs.DivideCurve(crvs[i], 400) for j in range(len(divPts)): path = rs.PointClosestObject(divPts[j], paths)[0] param = rs.CurveClosestPoint(path, divPts[j]) close = rs.EvaluateCurve(path, param) srfParam = rs.SurfaceClosestPoint(srf, close) vec = rs.SurfaceNormal(srf, srfParam) dist = rs.Distance(close, divPts[j]) vec = rs.VectorUnitize(vec) border = 1 entry = 1 if j > len(divPts) / 2: border = rs.Distance(rs.CurveEndPoint(crvs[i]), divPts[j]) else: border = rs.Distance(rs.CurveStartPoint(crvs[i]), divPts[j]) if border < sd * 3: border = border / (sd * 3) else: border = 1 entryDist = rs.Distance(startPt, divPts[j]) if entryDist < sd * 10: entry = entryDist / (sd * 10) else: entry = 1 if dist < sd * 2: val = radius * (bellCrv(dist, sd)) divPts[j] = rs.PointAdd(divPts[j], vec * val * border * entry) newCrvs.append(rs.AddCurve(divPts)) return divPts
def shoot_rays(room): """Performs the raytracing process using the room properties. Calculates all reflections, including geometry, times and energy values per segment. Parameters ---------- room: object The room object to be analyzed. """ # TODO: Figure out if it is possible to cut ray one reflection short. # TODO: how to get rid of deepcopy? it is super slow. # TODO: should propably use some generators, is that possible? directions = room.source.directions ref_srf = [room.surfaces[gk]['guid'] for gk in room.surfaces] ref_map = {room.surfaces[sk]['guid']: sk for sk in room.surfaces} room.ray_times = {dk: {} for dk in directions} room.ray_lengths = {dk: {} for dk in directions} room.ray_powers = {dk: {} for dk in directions} room.ray_lines = {dk: {} for dk in directions} for dk in directions: dir = directions[dk] src_ = room.source.xyz w = room.source.ray_power[dk] min_w = room.source.ray_minpower[dk] time = 0 i = 0 min_power = False while time < room.ctime and not min_power: i += 1 ray = rs.ShootRay(ref_srf, src_, dir, 2) srf = rs.PointClosestObject(ray[0], ref_srf)[0] mp_list = [] if i > 0: sk = ref_map[str(srf)] abs = room.materials[room.surfaces[sk]['material']].absorption for wk in w: w[wk] *= (1 - abs[wk]) mp_list.append(w[wk] < min_w[wk]) min_power = all(mp_list) l = distance_point_point(ray[0], ray[1]) t = int((l / 343.0) * 1000) room.ray_times[dk][i] = t room.ray_lengths[dk][i] = l room.ray_powers[dk][i] = deepcopy(w) room.ray_lines[dk][i] = ((ray[0].X, ray[0].Y, ray[0].Z), (ray[1].X, ray[1].Y, ray[1].Z)) dir = vector_from_points(ray[1], ray[2]) src_ = ray[1] time += t
def MapPointClouds(pointclouds): #consider just two point clouds pointcloud_a = pointclouds[0] pointcloud_b = pointclouds[1] for point_a in pointcloud_a: #get the closest point in the other pointcloud closest_point = rs.PointClosestObject(point_a, pointcloud_b) #get the distance between point_a and the closest_point distance = rs.Distance(point_a, closest_point) if (distance > 0): point_1 = rs.AddPoint(point_a) point_2 = rs.AddPoint(closest_point)
def congregate(objs, threshold, loops): scaleFactOrig = .1 for j in range(loops): scaleF = ((loops-j)/loops) * scaleFactOrig print scaleF for i, pt1 in enumerate(objs): tempList = list(objs) del tempList[i] pt2 = rs.PointClosestObject(pt1, tempList)[1] vec = rs.VectorCreate(pt2, pt1) dist = rs.Distance(pt2, pt1) if dist < threshold: vec = rs.VectorReverse(vec) vec2 = rs.VectorScale(vec, scaleF) rs.MoveObject(pt1, vec2) line = rs.AddLine(pt1, pt2) rs.DeleteObject(line) return objs
def mesh_smooth_boundary(mesh,fixed,crvs, k=1, d=0.5): """Smoothen the input mesh by moving each vertex to the centroid of its neighbours. Note: This is a node-per-node version of Laplacian smoothing with umbrella weights. Parameters: k (int): The number of smoothing iterations. Defaults to `1`. d (float): Scale factor for (i.e. damping of) the displacement vector. Defaults to `0.5`. Returns: None """ def centroid(points): p = len(points) return [coord / p for coord in map(sum, zip(*points))] boundary = set(mesh.vertices_on_boundary()) for _ in range(k): key_xyz = dict((key, (attr['x'], attr['y'], attr['z'])) for key, attr in mesh.vertices_iter(True)) for key in key_xyz: if (key in boundary) and (key not in fixed): nbrs = mesh.vertex_neighbours(key) points = [key_xyz[nbr] for nbr in nbrs] cx, cy, cz = centroid(points) x, y, z = key_xyz[key] tx, ty, tz = d * (cx - x), d * (cy - y), d * (cz - z) mesh.vertex[key]['x'] += tx mesh.vertex[key]['y'] += ty mesh.vertex[key]['z'] += tz pt = mesh.vertex[key]['x'],mesh.vertex[key]['y'],mesh.vertex[key]['z'] pt = rs.PointClosestObject(pt,crvs)[1] mesh.vertex[key]['x'] = pt[0] mesh.vertex[key]['y'] = pt[1] mesh.vertex[key]['z'] = pt[2]
def callback(k, args): if k % 10 == 0: rs.Prompt(str(k)) # constrain all non-fixed to a surface or guide curves for key, attr in mesh.vertices(data=True): if attr['fixed']: continue if attr['srf']: srf = attr['srf'] x, y, z = rs.BrepClosestPoint(srf, mesh.vertex_coordinates(key))[0] attr['x'] = x attr['y'] = y attr['z'] = z elif attr['crv']: crv = attr['crv'] x, y, z = rs.PointClosestObject(mesh.vertex_coordinates(key), [crv])[1] attr['x'] = x attr['y'] = y attr['z'] = z conduit.redraw()
fixed = [] # assign attributes for key in mesh.vertices(): if key in bound: pt = mesh.vertex_coordinates(key) pt_geo = geometric_key(pt, precision) # check if pt has same coordinates as any fixed point if pt_geo in pts_fixed_geo: mesh.set_vertex_attribute(key, 'fixed', True) fixed.append(key) else: # assign guid of closest curve to vertex mesh.set_vertex_attribute( key, 'crv', rs.PointClosestObject(pt, guid_crvs)[0]) else: # assign surface guid to vertex mesh.set_vertex_attribute(key, 'srf', srf) # initialize conduit conduit = MeshConduit(mesh) # run smoothing with conduit with conduit.enabled(): mesh_smooth_area(mesh, fixed=fixed, kmax=200, damping=0.5, callback=callback)
def get_dist(p): co = rs.PointClosestObject(p,lines) d = rs.Distance(p,co[1]) return d - radius