Last active
March 20, 2020 18:08
-
-
Save tetov/bd82d147d8afb7fbe6752682dea7fd89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################# | |
# Lists and stuff | |
################# | |
def remap_values(values, from_domain, to_domain, include_clipped=False): | |
# TODO: Debug (reverse values compared to remap values in gh) | |
from_min, from_max = from_domain | |
to_min, to_max = to_domain | |
from_range = from_max - from_min | |
to_range = to_max - to_min | |
remapped_values = [] | |
if not hasattr(values, "__iter__"): | |
values = [values] | |
for value in values: | |
new_value = (((value - from_min) * to_range) / from_range) + to_min | |
if to_min < new_value < to_max: | |
remapped_values.append(new_value) | |
elif include_clipped: | |
remapped_values.append(new_value) | |
if len(remapped_values) == 1: | |
return remapped_values[0] | |
else: | |
return remapped_values | |
def flatten_list(l): | |
return [item for sublist in l for item in sublist] | |
def shift_list(seq, shift=1): | |
"""Shift indices of list, wrapping at end. | |
Notes | |
----- | |
Source: https://stackoverflow.com/a/29498813 | |
""" | |
return seq[-shift:] + seq[:-shift] | |
def wrap_list(l, i): | |
"""Return value at index, wrapping if necessary. | |
Parameters | |
---------- | |
l : list | |
i : int | |
Index. | |
""" | |
return l[i % len(l)] | |
def flip_matrix(listlike): | |
"""Rotate 2D-array. | |
Parameters | |
---------- | |
listlike: iterable | |
array to rotate | |
Returns | |
------- | |
iterable | |
rotated array | |
Notes | |
----- | |
Taken from https://stackoverflow.com/a/496056 | |
""" | |
return zip(*listlike[::-1]) | |
######################## | |
# Geometry but not Rhino | |
######################## | |
def ensure_frame(frame_like): | |
from compas import IPY | |
import compas.geometry as cg | |
if isinstance(frame_like, cg.Frame): | |
return frame_like | |
if isinstance(frame_like, cg.Plane): | |
return cg.Frame.from_plane(frame_like) | |
if IPY: | |
import Rhino.Geometry as rg.Plane | |
from compas_rcf.rhino import rgplane_to_cgframe | |
from compas_rcf.rhino import rgpoint_to_cgpoint | |
if isinstance(frame_like, rg.Plane): | |
return rgplane_to_cgframe(frame_like) | |
if isinstance(frame_like, rg.Point3d): | |
pt = rgpoint_to_cgpoint(frame_like) | |
return cg.Frame(pt, cg.Vector(0, 1, 0), cg.Vector(1, 0, 0)) | |
raise TypeError( | |
"Can't convert {} to compas.geometry.Frame".format(type(frame_like)) | |
) | |
def get_offset_frame(frame, distance): | |
"""Offset a frame in its Z axis direction. | |
Parameters | |
---------- | |
frame : `class`:compas.geometry.Frame | |
Frame to offset | |
distance : float | |
Translation distance in mm | |
Returns | |
------- | |
`class`:compas.geometry.Frame | |
""" | |
offset_vector = frame.zaxis * distance * -1 | |
T = cg.Translation(offset_vector) | |
return frame.transformed(T) | |
def rand_vector(dimensions): | |
"""Create a random vector. | |
Pads vector if fewer than three dimensions up to three dimensions. | |
Parameters | |
---------- | |
dimensions : int | |
Returns | |
------- | |
`class`:compas.geometry.Vector | |
Unitized random vector | |
Notes | |
----- | |
Adapted from `Stack Overflow <https://stackoverflow.com/a/8453514>`_ | |
""" | |
import random | |
import compas.geometry as cg | |
v = [random.gauss(0, 1) for i in range(dimensions)] | |
magnitude = sum(x ** 2 for x in v) ** 0.5 | |
vector = [x / magnitude for x in v] | |
if len(vector) < 3: | |
for i in range(3 - len(vector)): | |
vector += [0] | |
vector = cg.Vector(vector) | |
vector.unitize() | |
return vector | |
################### | |
# Rhino/Grasshopper | |
################### | |
def get_centroid(closed_crv): | |
import Rhino.Geometry as rg | |
area_obj = rg.AreaMassProperties.Compute(closed_crv) | |
return area_obj.Centroid | |
def reparametrize_crvs(crv_or_crvs, domain=(0, 1)): | |
import Rhino.Geometry as rg | |
interval = rg.Interval(*domain) | |
new_crvs = [] | |
if not isinstance(crv_or_crvs, list): | |
got_list = False | |
crv_or_crvs = list(crv_or_crvs) | |
else: | |
got_list = True | |
new_crvs = [crv.DuplicateCurve() for crv in crv_or_crvs] | |
for crv in new_crvs: | |
crv.Domain = interval | |
if not got_list: | |
return new_crvs[1] | |
else: | |
return new_crvs | |
def array_to_list(array): | |
return list(array) | |
def list_of_arrays_to_list_of_lists(list_of_arrays): | |
list_of_lists = [] | |
for array in list_of_arrays: | |
list_of_lists.append(array_to_list(array)) | |
return list_of_lists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment