_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q23400 | polygon_hash | train | def polygon_hash(polygon):
"""
Return a vector containing values representitive of
a particular polygon.
Parameters
---------
polygon : shapely.geometry.Polygon
Input geometry
Returns
---------
hashed: (6), float
Representitive values representing input polygon
"""
result = np.array(
| python | {
"resource": ""
} |
q23401 | random_polygon | train | def random_polygon(segments=8, radius=1.0):
"""
Generate a random polygon with a maximum number of sides and approximate radius.
Parameters
---------
segments: int, the maximum number of sides the random polygon will have
radius: float, the approximate radius of the polygon desired
Returns
---------
polygon: shapely.geometry.Polygon object with random exterior, and no interiors.
"""
angles = np.sort(np.cumsum(np.random.random(
segments) * np.pi | python | {
"resource": ""
} |
q23402 | polygon_scale | train | def polygon_scale(polygon):
"""
For a Polygon object, return the diagonal length of the AABB.
Parameters
------------
polygon: shapely.geometry.Polygon object
| python | {
"resource": ""
} |
q23403 | paths_to_polygons | train | def paths_to_polygons(paths, scale=None):
"""
Given a sequence of connected points turn them into
valid shapely Polygon objects.
Parameters
-----------
paths : (n,) sequence
Of (m,2) float, closed paths
scale: float
Approximate scale of drawing for precision
Returns
-----------
polys: (p,) list
shapely.geometry.Polygon
None
"""
polygons = [None] * len(paths)
for i, path in enumerate(paths):
if len(path) < 4:
# since the first and last vertices are identical in
# a closed loop a 4 | python | {
"resource": ""
} |
q23404 | repair_invalid | train | def repair_invalid(polygon, scale=None, rtol=.5):
"""
Given a shapely.geometry.Polygon, attempt to return a
valid version of the polygon through buffering tricks.
Parameters
-----------
polygon: shapely.geometry.Polygon object
rtol: float, how close does a perimeter have to be
scale: float, or None
Returns
----------
repaired: shapely.geometry.Polygon object
Raises
----------
ValueError: if polygon can't be repaired
"""
if hasattr(polygon, 'is_valid') and polygon.is_valid:
return polygon
# basic repair involves buffering the polygon outwards
# this will fix a subset of problems.
basic = polygon.buffer(tol.zero)
# if it returned multiple polygons check the largest
if util.is_sequence(basic):
basic = basic[np.argmax([i.area for i in basic])]
# check perimeter of result against original perimeter
if basic.is_valid and np.isclose(basic.length,
polygon.length,
rtol=rtol):
return basic
if scale is None:
distance = tol.buffer * polygon_scale(polygon)
else:
distance = tol.buffer * scale
# if there are no interiors, we can work with just the exterior
# ring, which is often more reliable
if len(polygon.interiors) == 0:
# try buffering the exterior of the polygon
# the interior will be offset by -tol.buffer
| python | {
"resource": ""
} |
q23405 | export_gltf | train | def export_gltf(scene,
extras=None,
include_normals=False):
"""
Export a scene object as a GLTF directory.
This puts each mesh into a separate file (i.e. a `buffer`)
as opposed to one larger file.
Parameters
-----------
scene : trimesh.Scene
Scene to be exported
Returns
----------
export : dict
Format: {file name : file data}
"""
# if we were passed a bare Trimesh or Path3D object
if (not util.is_instance_named(scene, "Scene")
and hasattr(scene, "scene")):
scene = scene.scene()
# create the header and buffer data
tree, buffer_items | python | {
"resource": ""
} |
q23406 | load_gltf | train | def load_gltf(file_obj=None,
resolver=None,
**mesh_kwargs):
"""
Load a GLTF file, which consists of a directory structure
with multiple files.
Parameters
-------------
file_obj : None or file-like
Object containing header JSON, or None
resolver : trimesh.visual.Resolver
Object which can be used to load other files by name
**mesh_kwargs : dict
Passed to mesh constructor
Returns
--------------
kwargs : dict
Arguments to create scene
"""
| python | {
"resource": ""
} |
q23407 | load_glb | train | def load_glb(file_obj, resolver=None, **mesh_kwargs):
"""
Load a GLTF file in the binary GLB format into a trimesh.Scene.
Implemented from specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
Parameters
------------
file_obj : file- like object
Containing GLB data
Returns
------------
kwargs : dict
Kwargs to instantiate a trimesh.Scene
"""
# save the start position of the file for referencing
# against lengths
start = file_obj.tell()
# read the first 20 bytes which contain section lengths
head_data = file_obj.read(20)
head = np.frombuffer(head_data, dtype="<u4")
# check to make sure first index is gltf
# and second is 2, for GLTF 2.0
if head[0] != _magic["gltf"] or head[1] != 2:
raise ValueError("file is not GLTF 2.0")
# overall file length
# first chunk length
# first chunk type
length, chunk_length, chunk_type = head[2:]
# first chunk should be JSON header
if chunk_type != _magic["json"]:
raise ValueError("no initial JSON header!")
# uint32 causes an error in read, so we convert to native int
# for the length passed to read, for the JSON header
json_data = file_obj.read(int(chunk_length))
# convert to text
if hasattr(json_data, "decode"):
json_data = json_data.decode("utf-8")
# load the json header to native dict
header = json.loads(json_data)
# read the binary data referred to by GLTF as 'buffers'
buffers = []
while (file_obj.tell() - start) < length:
# the last | python | {
"resource": ""
} |
q23408 | _mesh_to_material | train | def _mesh_to_material(mesh, metallic=0.0, rough=0.0):
"""
Create a simple GLTF material for a mesh using the most
commonly occurring color in that mesh.
Parameters
------------
mesh: trimesh.Trimesh
Mesh to create a material from
Returns
------------
material: dict
In GLTF material format
"""
try:
# just get the most commonly occurring color
color = mesh.visual.main_color
except BaseException:
color = np.array([100, 100, 100, 255], dtype=np.uint8)
# convert uint color to 0.0-1.0 float color
| python | {
"resource": ""
} |
q23409 | _create_gltf_structure | train | def _create_gltf_structure(scene,
extras=None,
include_normals=False):
"""
Generate a GLTF header.
Parameters
-------------
scene : trimesh.Scene
Input scene data
extras : JSON serializable
Will be stored in the extras field
include_normals : bool
Include vertex normals in output file?
Returns
---------------
tree : dict
Contains required keys for a GLTF scene
buffer_items : list
Contains bytes of data
"""
# we are defining a single scene, and will be setting the
# world node to the 0th index
tree = {
"scene": 0,
"scenes": [{"nodes": [0]}],
"asset": {"version": "2.0",
"generator": "github.com/mikedh/trimesh"},
"accessors": [],
"meshes": [],
"materials": [],
"cameras": [_convert_camera(scene.camera)]}
if extras is not None:
tree['extras'] = extras
# grab the flattened scene graph in GLTF's format
nodes = scene.graph.to_gltf(scene=scene)
tree.update(nodes)
buffer_items = []
for name, geometry in scene.geometry.items():
if util.is_instance_named(geometry, "Trimesh"):
# add the mesh
| python | {
"resource": ""
} |
q23410 | _byte_pad | train | def _byte_pad(data, bound=4):
"""
GLTF wants chunks aligned with 4- byte boundaries
so this function will add padding to the end of a
chunk of bytes so that it aligns with a specified
boundary size
Parameters
--------------
data : bytes
| python | {
"resource": ""
} |
q23411 | _append_path | train | def _append_path(path, name, tree, buffer_items):
"""
Append a 2D or 3D path to the scene structure and put the
data into buffer_items.
Parameters
-------------
path : trimesh.Path2D or trimesh.Path3D
Source geometry
name : str
Name of geometry
tree : dict
Will be updated with data from path
buffer_items
Will have buffer appended with path data
"""
# convert the path to the unnamed args for
# a pyglet vertex list
vxlist = rendering.path_to_vertexlist(path)
tree["meshes"].append({
"name": name,
"primitives": [{
"attributes": {"POSITION": len(tree["accessors"])},
| python | {
"resource": ""
} |
q23412 | _parse_materials | train | def _parse_materials(header, views):
"""
Convert materials and images stored in a GLTF header
and buffer views to PBRMaterial objects.
Parameters
------------
header : dict
Contains layout of file
views : (n,) bytes
Raw data
Returns
------------
materials : list
List of trimesh.visual.texture.Material objects
"""
try:
import PIL.Image
except ImportError:
log.warning("unable to load textures without pillow!")
return None
# load any images
images = None
if "images" in header:
# images are referenced by index
images = [None] * len(header["images"])
# loop through images
for i, img in enumerate(header["images"]):
# get the bytes representing an image
blob = views[img["bufferView"]]
# i.e. 'image/jpeg'
# mime = img['mimeType']
try:
# load the buffer into a PIL image
images[i] = PIL.Image.open(util.wrap_as_stream(blob))
except BaseException:
log.error("failed to load image!", exc_info=True)
# store materials which reference images
materials = []
if "materials" in header:
for mat in header["materials"]:
| python | {
"resource": ""
} |
q23413 | _convert_camera | train | def _convert_camera(camera):
"""
Convert a trimesh camera to a GLTF camera.
Parameters
------------
camera : trimesh.scene.cameras.Camera
Trimesh camera object
Returns
-------------
gltf_camera : dict
Camera represented as a GLTF dict
"""
result = {
| python | {
"resource": ""
} |
q23414 | FilePathResolver.get | train | def get(self, name):
"""
Get an asset.
Parameters
-------------
name : str
Name of the asset
Returns
------------
data : bytes
Loaded data from asset
"""
| python | {
"resource": ""
} |
q23415 | ZipResolver.get | train | def get(self, name):
"""
Get an asset from the ZIP archive.
Parameters
-------------
name : str
Name of the asset
Returns
-------------
data : bytes
Loaded data from asset
"""
# not much we can do with that
if name is None:
return
# if name isn't in archive try some similar values
if name not in self.archive:
if hasattr(name, 'decode'):
name = name.decode('utf-8')
# try with cleared whitespace, split paths
for option in [name,
name.lstrip('./'),
name.strip(),
| python | {
"resource": ""
} |
q23416 | WebResolver.get | train | def get(self, name):
"""
Get a resource from the remote site.
Parameters
-------------
name : str
Asset name, i.e. 'quadknot.obj.mtl'
"""
# do import here to keep soft dependency
import requests
# append base url to requested name
| python | {
"resource": ""
} |
q23417 | Geometry.apply_translation | train | def apply_translation(self, translation):
"""
Translate the current mesh.
Parameters
----------
translation : (3,) float
Translation in XYZ
"""
translation = np.asanyarray(translation, dtype=np.float64)
if translation.shape != (3,):
| python | {
"resource": ""
} |
q23418 | Geometry.apply_scale | train | def apply_scale(self, scaling):
"""
Scale the mesh equally on all axis.
Parameters
----------
scaling : float
Scale factor to apply to the mesh
"""
scaling = float(scaling)
if not np.isfinite(scaling):
raise ValueError('Scaling | python | {
"resource": ""
} |
q23419 | Geometry.bounding_box | train | def bounding_box(self):
"""
An axis aligned bounding box for the current mesh.
Returns
----------
aabb : trimesh.primitives.Box
Box object with transform and extents defined
representing the axis aligned bounding box of | python | {
"resource": ""
} |
q23420 | Geometry.bounding_box_oriented | train | def bounding_box_oriented(self):
"""
An oriented bounding box for the current mesh.
Returns
---------
obb : trimesh.primitives.Box
Box object with transform and extents defined
representing the minimum volume oriented bounding box of the mesh
"""
from . import primitives, bounds
| python | {
"resource": ""
} |
q23421 | Geometry.bounding_sphere | train | def bounding_sphere(self):
"""
A minimum volume bounding sphere for the current mesh.
Note that the Sphere primitive returned has an unpadded, exact
sphere_radius so while the distance of every vertex of the current
mesh from sphere_center will be less than sphere_radius, the faceted
sphere primitive may not contain every vertex
Returns
--------
minball: trimesh.primitives.Sphere
Sphere primitive containing current mesh
"""
| python | {
"resource": ""
} |
q23422 | Geometry.bounding_cylinder | train | def bounding_cylinder(self):
"""
A minimum volume bounding cylinder for the current mesh.
Returns
--------
mincyl : trimesh.primitives.Cylinder
Cylinder primitive containing current mesh
"""
from . import primitives, | python | {
"resource": ""
} |
q23423 | export_mesh | train | def export_mesh(mesh, file_obj, file_type=None, **kwargs):
"""
Export a Trimesh object to a file- like object, or to a filename
Parameters
---------
file_obj : str, file-like
Where should mesh be exported to
file_type : str or None
Represents file type (eg: 'stl')
Returns
----------
exported : bytes or str
Result of exporter
"""
# if we opened a file object in this function
# we will want to close it when we're done
was_opened = False
if util.is_string(file_obj):
if file_type is None:
file_type = (str(file_obj).split('.')[-1]).lower()
| python | {
"resource": ""
} |
q23424 | export_off | train | def export_off(mesh, digits=10):
"""
Export a mesh as an OFF file, a simple text format
Parameters
-----------
mesh : trimesh.Trimesh
Geometry to export
digits : int
Number of digits to include on floats
Returns
-----------
export : str
OFF format output
"""
# make sure specified digits is an int
digits = int(digits)
# prepend a 3 (face count) to each face
faces_stacked = np.column_stack((np.ones(len(mesh.faces)) * 3,
mesh.faces)).astype(np.int64)
export = 'OFF\n'
# the | python | {
"resource": ""
} |
q23425 | export_dict | train | def export_dict(mesh, encoding=None):
"""
Export a mesh to a dict
Parameters
------------
mesh : Trimesh object
Mesh to be exported
encoding : str, or None
'base64'
Returns
-------------
"""
def encode(item, dtype=None):
if encoding is None:
return item.tolist()
else:
if dtype is None:
dtype = item.dtype
return util.array_to_encoded(item, dtype=dtype, encoding=encoding)
# metadata keys we explicitly want to preserve
# sometimes there are giant datastructures we don't
# care about in metadata which causes exports to be
# extremely slow, so skip all but known good keys
meta_keys = ['units', 'file_name', 'file_path']
metadata = {k: v for k, v | python | {
"resource": ""
} |
q23426 | minify | train | def minify(path):
"""
Load a javascript file and minify.
Parameters
------------
path: str, path of resource
"""
if 'http' in path:
data = requests.get(path).content.decode(
'ascii', errors='ignore')
else:
with open(path, 'rb') as f:
# some of these assholes use unicode spaces -_-
data = f.read().decode('ascii',
| python | {
"resource": ""
} |
q23427 | circle_pattern | train | def circle_pattern(pattern_radius,
circle_radius,
count,
center=[0.0, 0.0],
angle=None,
**kwargs):
"""
Create a Path2D representing a circle pattern.
Parameters
------------
pattern_radius : float
Radius of circle centers
circle_radius : float
The radius of each circle
count : int
Number of circles in the pattern
center : (2,) float
Center of pattern
angle : float
If defined pattern will span this angle
If None, pattern will be evenly spaced
Returns
-------------
pattern : trimesh.path.Path2D
Path containing circular pattern
"""
from .path import Path2D
if angle is None:
angles = np.linspace(0.0, np.pi * 2.0, count + 1)[:-1]
elif isinstance(angle, float) or isinstance(angle, int):
angles = np.linspace(0.0, angle, count)
else:
raise ValueError('angle must be float or int!')
# centers of circles
centers = np.column_stack((
np.cos(angles), np.sin(angles))) * pattern_radius
vert = []
ents = []
for circle_center in centers:
# (3,3) center points of arc
three = | python | {
"resource": ""
} |
q23428 | plane_transform | train | def plane_transform(origin, normal):
"""
Given the origin and normal of a plane find the transform
that will move that plane to be coplanar with the XY plane.
Parameters
----------
origin : (3,) float
Point that lies on the plane
normal : (3,) float
Vector that points along normal of plane
Returns
--------- | python | {
"resource": ""
} |
q23429 | align_vectors | train | def align_vectors(a, b, return_angle=False):
"""
Find a transform between two 3D vectors.
Implements the method described here:
http://ethaneade.com/rot_between_vectors.pdf
Parameters
--------------
a : (3,) float
Source vector
b : (3,) float
Target vector
return_angle : bool
If True return the angle between the two vectors
Returns
-------------
transform : (4, 4) float
Homogenous transform from a to b
angle : float
Angle between vectors in radians
Only returned if return_angle
"""
# copy of input vectors
a = np.array(a, dtype=np.float64, copy=True)
b = np.array(b, dtype=np.float64, copy=True)
# make sure vectors are 3D
if a.shape != (3,) or b.shape != (3,):
raise ValueError('only works for (3,) vectors')
# unitize input vectors
a /= np.linalg.norm(a)
b /= np.linalg.norm(b)
# projection of a onto b
dot = np.dot(a, b)
# are vectors just reversed
if dot < (tol.zero - 1):
# a reversed vector is 180 degrees
angle = np.pi
# get an arbitrary perpendicular vector to a
perp = util.generate_basis(a)[0] * np.eye(3)
# (3, 3) rotation from a to b
rotation = (2 * np.dot(perp, perp.T)) - np.eye(3)
# are vectors already the same
elif dot > | python | {
"resource": ""
} |
q23430 | vector_angle | train | def vector_angle(pairs):
"""
Find the angles between pairs of unit vectors.
Parameters
----------
pairs : (n, 2, 3) float
Unit vector pairs
Returns
----------
angles : (n,) float
Angles between vectors in radians
"""
pairs = np.asanyarray(pairs, dtype=np.float64)
if len(pairs) == 0:
| python | {
"resource": ""
} |
q23431 | triangulate_quads | train | def triangulate_quads(quads):
"""
Given a set of quad faces, return them as triangle faces.
Parameters
-----------
quads: (n, 4) int
Vertex indices of quad faces
Returns
-----------
faces : (m, 3) int
Vertex indices of triangular faces
"""
| python | {
"resource": ""
} |
q23432 | mean_vertex_normals | train | def mean_vertex_normals(vertex_count,
faces,
face_normals,
**kwargs):
"""
Find vertex normals from the mean of the faces that contain
that vertex.
Parameters
-----------
vertex_count : int
The number of vertices faces refer to
faces : (n, 3) int
List of vertex indices
face_normals : (n, 3) float
Normal vector for each face
Returns
-----------
vertex_normals : (vertex_count, 3) float
Normals for every vertex
Vertices unreferenced by faces will be zero.
"""
def summed_sparse():
# use a sparse matrix of which face contains each vertex to
# figure out the summed normal at each vertex
# allow cached sparse matrix to be passed
if 'sparse' in kwargs:
sparse = kwargs['sparse']
else:
sparse = index_sparse(vertex_count, faces)
summed = sparse.dot(face_normals)
return summed
def summed_loop():
# loop through every face, in tests was ~50x slower than
| python | {
"resource": ""
} |
q23433 | index_sparse | train | def index_sparse(column_count, indices):
"""
Return a sparse matrix for which vertices are contained in which faces.
Returns
---------
sparse: scipy.sparse.coo_matrix of shape (column_count, len(faces))
dtype is boolean
Examples
----------
In [1]: sparse = faces_sparse(len(mesh.vertices), mesh.faces)
In [2]: sparse.shape
Out[2]: (12, 20)
In [3]: mesh.faces.shape
Out[3]: (20, 3)
In [4]: mesh.vertices.shape
Out[4]: (12, 3)
In [5]: dense = sparse.toarray().astype(int)
In [6]: dense
Out[6]:
array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0, | python | {
"resource": ""
} |
q23434 | get_json | train | def get_json(file_name='../dxf.json.template'):
"""
Load the JSON blob into native objects
""" | python | {
"resource": ""
} |
q23435 | write_json | train | def write_json(template, file_name='../dxf.json.template'):
"""
Write a | python | {
"resource": ""
} |
q23436 | replace_whitespace | train | def replace_whitespace(text, SAFE_SPACE='|<^>|', insert=True):
"""
Replace non-strippable whitepace in a string with a safe space
"""
if insert:
# replace whitespace with safe space chr
args = (' ', SAFE_SPACE)
else:
# replace safe space chr | python | {
"resource": ""
} |
q23437 | discretize_arc | train | def discretize_arc(points,
close=False,
scale=1.0):
"""
Returns a version of a three point arc consisting of
line segments.
Parameters
---------
points : (3, d) float
Points on the arc where d in [2,3]
close : boolean
If True close the arc into a circle
scale : float
What is the approximate overall drawing scale
Used to establish order of magnitude for precision
Returns
---------
discrete : (m, d) float
Connected points in space
"""
# make sure points are (n, 3)
points, is_2D = util.stack_3D(points, return_2D=True)
# find the center of the points
center_info = arc_center(points)
center, R, N, angle = (center_info['center'],
center_info['radius'],
center_info['normal'],
center_info['span'])
# if requested, close arc into a circle
if close:
angle = np.pi * 2
# the number of facets, based on the angle criteria
count_a = angle / res.seg_angle
count_l = ((R * angle)) / (res.seg_frac * scale)
# figure out the number of line segments
count = np.max([count_a, count_l])
# force at LEAST 4 points for the arc
# otherwise the endpoints will diverge
count = np.clip(count, 4, np.inf)
count = int(np.ceil(count))
V1 = util.unitize(points[0] - center)
V2 = util.unitize(np.cross(-N, | python | {
"resource": ""
} |
q23438 | to_threepoint | train | def to_threepoint(center, radius, angles=None):
"""
For 2D arcs, given a center and radius convert them to three
points on the arc.
Parameters
-----------
center : (2,) float
Center point on the plane
radius : float
Radius of arc
angles : (2,) float
Angles in radians for start and end angle
if not specified, will default to (0.0, pi)
Returns
----------
three : (3, 2) float
Arc control points
"""
# if no angles provided assume we want a half circle
if angles is None:
angles = [0.0, np.pi]
# force angles to float64
angles = np.asanyarray(angles, dtype=np.float64)
if angles.shape != (2,):
raise ValueError('angles must be (2,)!')
# provide the wrap around
if angles[1] < angles[0]:
| python | {
"resource": ""
} |
q23439 | abspath | train | def abspath(rel):
"""
Take paths relative to the current file and
convert them to absolute paths.
Parameters
------------
rel : str
Relative path, IE '../stuff'
Returns
| python | {
"resource": ""
} |
q23440 | load_pyassimp | train | def load_pyassimp(file_obj,
file_type=None,
resolver=None,
**kwargs):
"""
Use the pyassimp library to load a mesh from a file object
and type or file name if file_obj is a string
Parameters
---------
file_obj: str, or file object
File path or object containing mesh data
file_type : str
File extension, aka 'stl'
resolver : trimesh.visual.resolvers.Resolver
Used to load referenced data (like texture files)
kwargs : dict
Passed through to mesh constructor
Returns
---------
scene : trimesh.Scene
Native trimesh copy of assimp scene
"""
def LP_to_TM(lp):
# try to get the vertex colors attribute
colors = (np.reshape(lp.colors, (-1, 4))
[:, :3] * 255).round().astype(np.uint8)
# If no vertex colors, try to extract them from the material
if len(colors) == 0:
if 'diffuse' in lp.material.properties.keys():
colors = np.array(lp.material.properties['diffuse'])
# pass kwargs through to mesh constructor
mesh_kwargs = copy.deepcopy(kwargs)
# add data from the LP_Mesh
mesh_kwargs.update({'vertices': lp.vertices,
'vertex_normals': lp.normals,
'faces': lp.faces,
'vertex_colors': colors})
return mesh_kwargs
# did we open the file inside this function
opened = False
# not a file object
if not hasattr(file_obj, 'read'):
# if there is no read attribute
# we assume we've been passed a file name
file_type = (str(file_obj).split('.')[-1]).lower()
file_obj = open(file_obj, 'rb')
opened = True
# we need files to be bytes
elif not hasattr(file_obj, 'mode') or file_obj.mode != 'rb':
# assimp will crash on anything that isn't binary
# so if we have a text mode file or anything else
# grab the data, encode as bytes, and then use stream
data = file_obj.read()
if hasattr(data, 'encode'):
data = data.encode('utf-8')
file_obj = util.wrap_as_stream(data)
# load the scene using pyassimp
scene = pyassimp.load(file_obj,
file_type=file_type)
# save a record of mesh names used so we
# don't have to do queries on mesh_id.values()
mesh_names = set()
# save a mapping for {id(mesh) : name}
mesh_id = {}
# save results as {name : Trimesh}
meshes = {}
# loop through scene LPMesh objects
for m in scene.meshes:
# skip meshes without tri/quad faces
if m.faces.shape[1] not in [3, 4]:
continue
# if this mesh has the name of an existing mesh
if m.name in mesh_names:
# make it the name plus the unique ID of the object
| python | {
"resource": ""
} |
q23441 | load_cyassimp | train | def load_cyassimp(file_obj,
file_type=None,
resolver=None,
**kwargs):
"""
Load a file using the cyassimp bindings.
The easiest way to install these is with conda:
conda install -c menpo/label/master cyassimp
Parameters
---------
file_obj: str, or file object
File path or object containing mesh data
file_type : str
File extension, aka 'stl'
resolver : trimesh.visual.resolvers.Resolver
Used to load referenced data (like texture files)
kwargs : dict
Passed through to mesh constructor
Returns
---------
meshes : (n,) list of dict
Contain kwargs for Trimesh constructor
"""
if hasattr(file_obj, 'read'):
# if it has a read attribute it is probably a file object
with tempfile.NamedTemporaryFile(
suffix=str(file_type)) as file_temp:
file_temp.write(file_obj.read())
# file name should be bytes
scene = cyassimp.AIImporter(
| python | {
"resource": ""
} |
q23442 | load_path | train | def load_path(obj, file_type=None, **kwargs):
"""
Load a file to a Path object.
Parameters
-----------
obj : One of the following:
- Path, Path2D, or Path3D objects
- open file object (dxf or svg)
- file name (dxf or svg)
- shapely.geometry.Polygon
- shapely.geometry.MultiLineString
- dict with kwargs for Path constructor
- (n,2,(2|3)) float, line segments
file_type : str
Type of file is required if file
object passed.
Returns
---------
path : Path, Path2D, Path3D object
Data as a native trimesh Path object
"""
if isinstance(obj, Path):
# we have been passed a Path object so
# do nothing and return the passed object
return obj
elif util.is_file(obj):
# for open file objects use loaders
loaded = path_loaders[file_type](obj,
file_type=file_type)
obj.close()
elif util.is_string(obj):
# strings passed are evaluated as file objects
with open(obj, 'rb') as file_obj:
# get the file type from the extension
file_type = os.path.splitext(obj)[-1][1:].lower()
# | python | {
"resource": ""
} |
q23443 | _create_path | train | def _create_path(entities,
vertices,
metadata=None,
**kwargs):
"""
Turn entities and vertices into a Path2D or a Path3D
object depending on dimension of vertices.
Parameters
-----------
entities : list
Entity objects that reference vertex indices
vertices : (n, 2) or (n, 3) float
Vertices in space
metadata : dict
Any metadata about the path object
Returns
-----------
as_path : Path2D or Path3D object
Args in native trimesh object form
"""
# make sure vertices are numpy array
vertices = np.asanyarray(vertices, dtype=np.float64)
# check dimension of vertices to decide on object type
| python | {
"resource": ""
} |
q23444 | split_scene | train | def split_scene(geometry):
"""
Given a geometry, list of geometries, or a Scene
return them as a single Scene object.
Parameters
----------
geometry : splittable
Returns
---------
scene: trimesh.Scene
"""
# already a scene, so return it
if util.is_instance_named(geometry, 'Scene'):
return geometry
# a list of things
if util.is_sequence(geometry):
metadata = {}
for g in geometry:
| python | {
"resource": ""
} |
q23445 | append_scenes | train | def append_scenes(iterable, common=['world']):
"""
Concatenate multiple scene objects into one scene.
Parameters
-------------
iterable : (n,) Trimesh or Scene
Geometries that should be appended
common : (n,) str
Nodes that shouldn't be remapped
Returns
------------
result : trimesh.Scene
Scene containing all geometry
"""
if isinstance(iterable, Scene):
return iterable
# save geometry in dict
geometry = {}
# save transforms as edge tuples
edges = []
# nodes which shouldn't be remapped
common = set(common)
# nodes which are consumed and need to be remapped
consumed = set()
def node_remap(node):
"""
Remap node to new name if necessary
Parameters
-------------
node : hashable
Node name in original scene
Returns
-------------
name : hashable
Node name in concatenated scene
"""
# if we've already remapped a node use it
if node in map_node:
return map_node[node]
# if a node is consumed and isn't one of the nodes
# we're going to hold common between scenes remap it
if node not in common and node in consumed:
name = str(node) + '-' + util.unique_id().upper()
map_node[node] = name
node = name
# keep track of which nodes have been used
# in the current scene
current.add(node)
return node
# loop through every geometry
for s in iterable:
# allow Trimesh/Path2D geometry to be passed
if hasattr(s, 'scene'):
s = s.scene()
# if we don't have a scene raise an exception
if not isinstance(s, Scene):
raise ValueError('{} is not a scene!'.format(
type(s).__name__))
# remap geometries if they have been consumed
map_geom = {}
for k, v in s.geometry.items():
# if a geometry already exists add a UUID to the name
if k in geometry:
| python | {
"resource": ""
} |
q23446 | Scene.add_geometry | train | def add_geometry(self,
geometry,
node_name=None,
geom_name=None,
parent_node_name=None,
transform=None):
"""
Add a geometry to the scene.
If the mesh has multiple transforms defined in its
metadata, they will all be copied into the
TransformForest of the current scene automatically.
Parameters
----------
geometry : Trimesh, Path2D, Path3D PointCloud or list
Geometry to initially add to the scene
base_frame : str or hashable
Name of base frame
metadata : dict
Any metadata about the scene
graph : TransformForest or None
A passed transform graph to use
Returns
----------
node_name : str
Name of node in self.graph
"""
if geometry is None:
return
# PointCloud objects will look like a sequence
elif util.is_sequence(geometry):
# if passed a sequence add all elements
for value in geometry:
self.add_geometry(
geometry=value,
node_name=node_name,
geom_name=geom_name,
parent_node_name=parent_node_name,
transform=transform,
)
return
elif isinstance(geometry, dict):
# if someone passed us a dict of geometry
for key, value in geometry.items():
self.add_geometry(value, geom_name=key)
return
# get or create a name to reference the geometry by
if geom_name is not None:
# if name is passed use it
name = geom_name
elif 'name' in geometry.metadata:
# if name is in metadata use it
| python | {
"resource": ""
} |
q23447 | Scene.md5 | train | def md5(self):
"""
MD5 of scene which will change when meshes or
transforms are changed
Returns
--------
hashed: str, MD5 hash of scene
"""
# start with transforms hash
hashes = [self.graph.md5()]
for g in self.geometry.values():
if hasattr(g, 'md5'):
hashes.append(g.md5())
elif hasattr(g, 'tostring'):
hashes.append(str(hash(g.tostring())))
| python | {
"resource": ""
} |
q23448 | Scene.is_valid | train | def is_valid(self):
"""
Is every geometry connected to the root node.
Returns
-----------
is_valid : bool
Does every geometry have a transform
"""
if len(self.geometry) == 0:
return True
try:
referenced = {self.graph[i][1]
for i in self.graph.nodes_geometry}
except BaseException: | python | {
"resource": ""
} |
q23449 | Scene.bounds_corners | train | def bounds_corners(self):
"""
A list of points that represent the corners of the
AABB of every geometry in the scene.
This can be useful if you want to take the AABB in
a specific frame.
Returns
-----------
corners: (n, 3) float, points in space
"""
# the saved corners of each instance
corners_inst = []
# (n, 3) float corners of each geometry
corners_geom = {k: bounds_module.corners(v.bounds)
| python | {
"resource": ""
} |
q23450 | Scene.bounds | train | def bounds(self):
"""
Return the overall bounding box of the scene.
Returns
--------
bounds: (2,3) float points | python | {
"resource": ""
} |
q23451 | Scene.triangles | train | def triangles(self):
"""
Return a correctly transformed polygon soup of the
current scene.
Returns
----------
triangles: (n,3,3) float, triangles in space
"""
triangles = collections.deque()
triangles_node = collections.deque()
for node_name in self.graph.nodes_geometry:
# which geometry does this node refer to
transform, geometry_name = self.graph[node_name]
# get the actual potential mesh instance
| python | {
"resource": ""
} |
q23452 | Scene.geometry_identifiers | train | def geometry_identifiers(self):
"""
Look up geometries by identifier MD5
Returns
---------
identifiers: dict, identifier md5: key in self.geometry
"""
| python | {
"resource": ""
} |
q23453 | Scene.duplicate_nodes | train | def duplicate_nodes(self):
"""
Return a sequence of node keys of identical meshes.
Will combine meshes duplicated by copying in space with different keys in
self.geometry, as well as meshes repeated by self.nodes.
Returns
-----------
duplicates: (m) sequence of keys to self.nodes that represent
identical geometry
"""
# if there is no geometry we can have no duplicate nodes
if len(self.geometry) == 0:
return []
# geometry name : md5 of mesh
mesh_hash = {k: int(m.identifier_md5, 16)
for k, m in self.geometry.items()}
# the name of nodes in the scene | python | {
"resource": ""
} |
q23454 | Scene.set_camera | train | def set_camera(self,
angles=None,
distance=None,
center=None,
resolution=None,
fov=None):
"""
Create a camera object for self.camera, and add
a transform to self.graph for it.
If arguments are not passed sane defaults will be figured
out which show the mesh roughly centered.
Parameters
-----------
angles : (3,) float
Initial euler angles in radians
distance : float
Distance from centroid
center : (3,) float
Point camera should be center on
camera : Camera object
Object that stores camera parameters
"""
if fov is None:
fov = np.array([60, 45])
# if no geometry nothing to set camera to
if len(self.geometry) == 0:
return
# set with no rotation by default
if angles is None:
angles = np.zeros(3)
rotation = transformations.euler_matrix(*angles)
| python | {
"resource": ""
} |
q23455 | Scene.camera | train | def camera(self):
"""
Get the single camera for the scene. If not manually
set one will abe automatically generated.
Returns
----------
camera : trimesh.scene.Camera
Camera object defined for the scene
"""
# no camera | python | {
"resource": ""
} |
q23456 | Scene.lights | train | def lights(self):
"""
Get a list of the lights in the scene. If nothing is
set it will generate some automatically.
Returns
-------------
lights : [trimesh.scene.lighting.Light]
Lights in the scene.
"""
| python | {
"resource": ""
} |
q23457 | Scene.rezero | train | def rezero(self):
"""
Move the current scene so that the AABB of the whole
scene is centered at the origin.
Does this by changing the base frame to a new, offset
base frame.
"""
if self.is_empty or np.allclose(self.centroid, 0.0):
# early exit since what we want already exists
return
# the transformation to move the overall scene to AABB centroid
matrix = np.eye(4)
matrix[:3, 3] = -self.centroid
# we are | python | {
"resource": ""
} |
q23458 | Scene.dump | train | def dump(self):
"""
Append all meshes in scene to a list of meshes.
Returns
----------
dumped: (n,) list, of Trimesh objects transformed to their
location the scene.graph
"""
result = collections.deque()
for node_name in self.graph.nodes_geometry:
| python | {
"resource": ""
} |
q23459 | Scene.convex_hull | train | def convex_hull(self):
"""
The convex hull of the whole scene
Returns
---------
hull: Trimesh object, convex hull of all meshes in scene
"""
| python | {
"resource": ""
} |
q23460 | Scene.export | train | def export(self, file_type=None):
"""
Export a snapshot of the current scene.
Parameters
----------
file_type: what encoding to use for meshes
ie: dict, dict64, stl
Returns
----------
export: dict with keys:
meshes: list of meshes, encoded as per file_type
transforms: edge list of transforms, eg:
((u, v, {'matrix' : np.eye(4)}))
"""
file_type = str(file_type).strip().lower()
if file_type == 'gltf':
return gltf.export_gltf(self)
elif file_type == 'glb':
return gltf.export_glb(self)
export = {'graph': self.graph.to_edgelist(),
'geometry': {},
'scene_cache': {'bounds': self.bounds.tolist(),
'extents': self.extents.tolist(),
'centroid': self.centroid.tolist(),
'scale': self.scale}}
if file_type is None:
file_type = {'Trimesh': 'ply',
'Path2D': 'dxf'}
# if the mesh has an export method use it
# otherwise put the mesh
# itself into the export object
for geometry_name, geometry in self.geometry.items():
if hasattr(geometry, 'export'):
if isinstance(file_type, dict):
# case where we have export types that are different
# for different classes of objects.
for query_class, query_format in file_type.items():
if util.is_instance_named(geometry, | python | {
"resource": ""
} |
q23461 | Scene.save_image | train | def save_image(self, resolution=(1024, 768), **kwargs):
"""
Get a PNG image of a scene.
Parameters
-----------
resolution: (2,) int, resolution to render image
**kwargs: passed to SceneViewer constructor
Returns
-----------
png: bytes, render of | python | {
"resource": ""
} |
q23462 | Scene.units | train | def units(self):
"""
Get the units for every model in the scene, and
raise a ValueError if there are mixed units.
Returns
-----------
units : str
Units for every model in the scene
"""
existing = [i.units for i in self.geometry.values()]
if any(existing[0] != | python | {
"resource": ""
} |
q23463 | Scene.units | train | def units(self, value):
"""
Set the units for every model in the scene without
converting any units just setting the tag.
Parameters
------------
value : str
| python | {
"resource": ""
} |
q23464 | Scene.convert_units | train | def convert_units(self, desired, guess=False):
"""
If geometry has units defined convert them to new units.
Returns a new scene with geometries and transforms scaled.
Parameters
----------
desired : str
Desired final unit system: 'inches', 'mm', etc.
guess : bool
Is the converter allowed to guess scale when models
don't have it specified in their metadata.
Returns
----------
scaled : trimesh.Scene
Copy of scene with scaling applied and units set
for every model
"""
# if there is no geometry do nothing
if len(self.geometry) == 0:
return self.copy()
current = self.units
if current is None:
# will raise ValueError if not in metadata
# and not allowed to guess
current = units.units_from_metadata(self, guess=guess)
| python | {
"resource": ""
} |
q23465 | Scene.explode | train | def explode(self, vector=None, origin=None):
"""
Explode a scene around a point and vector.
Parameters
-----------
vector : (3,) float or float
Explode radially around a direction vector or spherically
origin : (3,) float
Point to explode around
"""
if origin is None:
origin = self.centroid
if vector is None:
vector = self.scale / 25.0
vector = np.asanyarray(vector, dtype=np.float64)
origin = np.asanyarray(origin, dtype=np.float64)
| python | {
"resource": ""
} |
q23466 | Scene.scaled | train | def scaled(self, scale):
"""
Return a copy of the current scene, with meshes and scene
transforms scaled to the requested factor.
Parameters
-----------
scale : float
Factor to scale meshes and transforms
Returns
-----------
scaled : trimesh.Scene
A copy of the current scene but scaled
"""
scale = float(scale)
# matrix for 2D scaling
scale_2D = np.eye(3) * scale
# matrix for 3D scaling
scale_3D = np.eye(4) * scale
# preallocate transforms and geometries
nodes = self.graph.nodes_geometry
transforms = np.zeros((len(nodes), 4, 4))
geometries = [None] * len(nodes)
# collect list of transforms
for i, node in enumerate(nodes):
transforms[i], geometries[i] = self.graph[node]
# result is a copy
result = self.copy()
# remove all existing transforms
result.graph.clear()
for group in grouping.group(geometries):
# hashable reference to self.geometry
geometry = geometries[group[0]]
# original transform from world to geometry
original = transforms[group[0]]
# transform for geometry
new_geom = np.dot(scale_3D, original) | python | {
"resource": ""
} |
q23467 | Scene.copy | train | def copy(self):
"""
Return a deep copy of the current scene
Returns
----------
copied : trimesh.Scene
Copy of the current scene
"""
# use the geometries copy method to
# allow them to handle references to unpickle-able objects
geometry = {n: g.copy() for n, g in | python | {
"resource": ""
} |
q23468 | Scene.show | train | def show(self, viewer=None, **kwargs):
"""
Display the current scene.
Parameters
-----------
viewer: str 'gl': open a pyglet window
str,'notebook': return ipython.display.HTML
None: automatically pick based on whether or not
we are in an ipython notebook
smooth : bool
Turn on or off automatic smooth shading
"""
if viewer is None:
# check to see if we are in a notebook or not
from ..viewer import in_notebook
viewer = | python | {
"resource": ""
} |
q23469 | available_formats | train | def available_formats():
"""
Get a list of all available loaders
Returns
-----------
loaders : list
Extensions of available loaders
i.e. 'stl', 'ply', 'dxf', etc.
| python | {
"resource": ""
} |
q23470 | load_mesh | train | def load_mesh(file_obj,
file_type=None,
resolver=None,
**kwargs):
"""
Load a mesh file into a Trimesh object
Parameters
-----------
file_obj : str or file object
File name or file with mesh data
file_type : str or None
Which file type, e.g. 'stl'
kwargs : dict
Passed to Trimesh constructor
Returns
----------
mesh : trimesh.Trimesh or trimesh.Scene
Loaded geometry data
"""
# parse the file arguments into clean loadable form
(file_obj, # file- like object
file_type, # str, what kind of file
metadata, # dict, any metadata from file name
opened, # bool, did we open the file ourselves
resolver # object to load referenced resources
) = parse_file_args(file_obj=file_obj,
| python | {
"resource": ""
} |
q23471 | load_compressed | train | def load_compressed(file_obj,
file_type=None,
resolver=None,
mixed=False,
**kwargs):
"""
Given a compressed archive load all the geometry that
we can from it.
Parameters
----------
file_obj : open file-like object
Containing compressed data
file_type : str
Type of the archive file
mixed : bool
If False, for archives containing both 2D and 3D
data will only load the 3D data into the Scene.
Returns
----------
scene : trimesh.Scene
Geometry loaded in to a Scene object
"""
# parse the file arguments into clean loadable form
(file_obj, # file- like object
file_type, # str, what kind of file
metadata, # dict, any metadata from file name
opened, # bool, did we open the file ourselves
resolver # object to load referenced resources
) = parse_file_args(file_obj=file_obj,
file_type=file_type,
resolver=resolver)
try:
# a dict of 'name' : file-like object
files = util.decompress(file_obj=file_obj,
file_type=file_type)
# store loaded geometries as a list
geometries = []
# so loaders can access textures/etc
resolver = visual.resolvers.ZipResolver(files)
# try to save the files with meaningful metadata
if 'file_path' in metadata:
archive_name = metadata['file_path']
else:
archive_name = 'archive'
# populate our available formats
if mixed:
available = available_formats()
else:
# all types contained in ZIP archive
contains = set(util.split_extension(n).lower()
for n in files.keys())
# if there are no mesh formats available
if contains.isdisjoint(mesh_formats()):
available = path_formats()
else:
available = mesh_formats()
for name, data in files.items():
| python | {
"resource": ""
} |
q23472 | load_remote | train | def load_remote(url, **kwargs):
"""
Load a mesh at a remote URL into a local trimesh object.
This must be called explicitly rather than automatically
from trimesh.load to ensure users don't accidentally make
network requests.
Parameters
------------
url : string
URL containing mesh file
**kwargs : passed to `load` | python | {
"resource": ""
} |
q23473 | load_kwargs | train | def load_kwargs(*args, **kwargs):
"""
Load geometry from a properly formatted dict or kwargs
"""
def handle_scene():
"""
Load a scene from our kwargs:
class: Scene
geometry: dict, name: Trimesh kwargs
graph: list of dict, kwargs for scene.graph.update
base_frame: str, base frame of graph
"""
scene = Scene()
scene.geometry.update({k: load_kwargs(v) for
k, v in kwargs['geometry'].items()})
for k in kwargs['graph']:
if isinstance(k, dict):
scene.graph.update(**k)
elif util.is_sequence(k) and len(k) == 3:
scene.graph.update(k[1], k[0], **k[2])
if 'base_frame' in kwargs:
scene.graph.base_frame = kwargs['base_frame']
if 'metadata' in kwargs:
scene.metadata.update(kwargs['metadata'])
return scene
def handle_trimesh_kwargs():
"""
Load information with vertices and faces into a mesh
or PointCloud object.
| python | {
"resource": ""
} |
q23474 | parse_file_args | train | def parse_file_args(file_obj,
file_type,
resolver=None,
**kwargs):
"""
Given a file_obj and a file_type try to turn them into a file-like
object and a lowercase string of file type.
Parameters
-----------
file_obj: str: if string represents a file path, returns
-------------------------------------------
file_obj: an 'rb' opened file object of the path
file_type: the extension from the file path
str: if string is NOT a path, but has JSON-like special characters
-------------------------------------------
file_obj: the same string passed as file_obj
file_type: set to 'json'
str: string is a valid URL
-------------------------------------------
file_obj: an open 'rb' file object with retrieved data
file_type: from the extension
str: string is not an existing path or a JSON-like object
-------------------------------------------
ValueError will be raised as we can't do anything with input
file like object: we cannot grab information on file_type automatically
-------------------------------------------
ValueError will be raised if file_type is None
file_obj: same as input
file_type: same as input
other object: like a shapely.geometry.Polygon, etc:
-------------------------------------------
file_obj: same as input
file_type: if None initially, set to the class name
(in lower case), otherwise passed through
file_type: str, type of file and handled according to above
Returns
-----------
file_obj: loadable object
file_type: str, lower case of the type of file (eg 'stl', 'dae', etc)
metadata: dict, any metadata
opened: bool, did we open the file or not
| python | {
"resource": ""
} |
q23475 | pack_rectangles | train | def pack_rectangles(rectangles, sheet_size, shuffle=False):
"""
Pack smaller rectangles onto a larger rectangle, using a binary
space partition tree.
Parameters
----------
rectangles : (n, 2) float
An array of (width, height) pairs
representing the rectangles to be packed.
sheet_size : (2,) float
Width, height of rectangular sheet
shuffle : bool
Whether or not to shuffle the insert order of the
smaller rectangles, as the final packing density depends
on insertion order.
Returns
---------
density : float
Area filled over total sheet area
offset : (m,2) float
Offsets to move rectangles to their packed location
inserted : (n,) bool
Which of the original rectangles were packed
consumed_box : (2,) float
Bounding box size of packed result
"""
offset = np.zeros((len(rectangles), 2))
inserted = np.zeros(len(rectangles), dtype=np.bool)
box_order = np.argsort(np.sum(rectangles**2, | python | {
"resource": ""
} |
q23476 | pack_paths | train | def pack_paths(paths, sheet_size=None):
"""
Pack a list of Path2D objects into a rectangle.
Parameters
------------
paths: (n,) Path2D
Geometry to be packed
Returns
------------
packed : trimesh.path.Path2D
Object containing input geometry
inserted : (m,) int
Indexes of paths inserted into result
"""
from .util import concatenate
if sheet_size is not None:
sheet_size = np.sort(sheet_size)[::-1]
quantity = []
for path in paths:
if 'quantity' in path.metadata:
quantity.append(path.metadata['quantity'])
else:
quantity.append(1)
# pack using exterior polygon (will OBB)
polygons = [i.polygons_closed[i.root[0]] for i | python | {
"resource": ""
} |
q23477 | multipack | train | def multipack(polygons,
sheet_size=None,
iterations=50,
density_escape=.95,
spacing=0.094,
quantity=None):
"""
Pack polygons into a rectangle by taking each Polygon's OBB
and then packing that as a rectangle.
Parameters
------------
polygons : (n,) shapely.geometry.Polygon
Source geometry
sheet_size : (2,) float
Size of rectangular sheet
iterations : int
Number of times to run the loop
density_escape : float
When to exit early (0.0 - 1.0)
spacing : float
How big a gap to leave between polygons
quantity : (n,) int, or None
Quantity of each Polygon
Returns
-------------
overall_inserted : (m,) int
Indexes of inserted polygons
packed : (m, 3, 3) float
Homogeonous transforms from original frame to packed frame
"""
from .polygons import polygons_obb
if quantity is None:
quantity = np.ones(len(polygons), dtype=np.int64)
else:
quantity = np.asanyarray(quantity, dtype=np.int64)
if len(quantity) != len(polygons):
raise ValueError('quantity must match polygons')
# find the oriented bounding box of the polygons
obb, rectangles = polygons_obb(polygons)
# pad all sides of the rectangle
| python | {
"resource": ""
} |
q23478 | RectangleBin.insert | train | def insert(self, rectangle):
"""
Insert a rectangle into the bin.
Parameters
-------------
rectangle: (2,) float, size of rectangle to insert
"""
rectangle = np.asanyarray(rectangle, dtype=np.float64)
for child in self.child:
if child is not None:
attempt = child.insert(rectangle)
if attempt is not None:
return attempt
if self.occupied:
return None
# compare the bin size to the insertion candidate size
size_test = self.extents - rectangle
# this means the inserted rectangle is too big for the cell
if np.any(size_test < -tol.zero):
return None
# since the cell is big enough for the current rectangle, either it
# is going to be inserted here, or the cell is going to be split
# either way, the cell is now occupied.
self.occupied = True
# this means the inserted rectangle fits perfectly
# since we already checked to see if it was negative, no abs is needed
| python | {
"resource": ""
} |
q23479 | RectangleBin.split | train | def split(self, length, vertical=True):
"""
Returns two bounding boxes representing the current
bounds split into two smaller boxes.
Parameters
-------------
length: float, length to split
vertical: bool, if True will split box vertically
Returns
-------------
box: (2,4) float, two bounding boxes consisting of:
[minx, miny, maxx, maxy]
"""
# also know as [minx, miny, maxx, maxy]
| python | {
"resource": ""
} |
q23480 | oriented_bounds_2D | train | def oriented_bounds_2D(points, qhull_options='QbB'):
"""
Find an oriented bounding box for an array of 2D points.
Parameters
----------
points : (n,2) float
Points in 2D.
Returns
----------
transform : (3,3) float
Homogenous 2D transformation matrix to move the
input points so that the axis aligned bounding box
is CENTERED AT THE ORIGIN.
rectangle : (2,) float
Size of extents once input points are transformed
by transform
"""
# make sure input is a numpy array
points = np.asanyarray(points, dtype=np.float64)
# create a convex hull object of our points
# 'QbB' is a qhull option which has it scale the input to unit
# box to avoid precision issues with very large/small meshes
convex = spatial.ConvexHull(
points, qhull_options=qhull_options)
# (n,2,3) line segments
hull_edges = convex.points[convex.simplices]
# (n,2) points on the convex hull
hull_points = convex.points[convex.vertices]
# direction of the edges of the hull polygon
edge_vectors = np.diff(hull_edges, axis=1).reshape((-1, 2))
# unitize vectors
edge_vectors /= np.linalg.norm(edge_vectors, axis=1).reshape((-1, 1))
# create a set of perpendicular vectors
perp_vectors = np.fliplr(edge_vectors) * [-1.0, 1.0]
| python | {
"resource": ""
} |
q23481 | corners | train | def corners(bounds):
"""
Given a pair of axis aligned bounds, return all
8 corners of the bounding box.
Parameters
----------
bounds : (2,3) or (2,2) float
Axis aligned bounds
Returns
----------
corners : (8,3) float
Corner vertices of the cube
"""
bounds = np.asanyarray(bounds, dtype=np.float64)
if util.is_shape(bounds, (2, 2)):
bounds = np.column_stack((bounds, [0, 0]))
elif not util.is_shape(bounds, (2, 3)):
raise ValueError('bounds must be (2,2) or (2,3)!')
minx, miny, minz, maxx, maxy, maxz = np.arange(6)
corner_index = np.array([minx, miny, minz,
| python | {
"resource": ""
} |
q23482 | contains | train | def contains(bounds, points):
"""
Do an axis aligned bounding box check on a list of points.
Parameters
-----------
bounds : (2, dimension) float
Axis aligned bounding box
points : (n, dimension) float
Points in space
Returns
-----------
points_inside : (n,) bool
True if points are inside the AABB
"""
# make sure we have correct input types
bounds = np.asanyarray(bounds, dtype=np.float64)
points = np.asanyarray(points, dtype=np.float64)
| python | {
"resource": ""
} |
q23483 | in_notebook | train | def in_notebook():
"""
Check to see if we are in an IPython or Jypyter notebook.
Returns
-----------
in_notebook : bool
Returns True if we are in a notebook
"""
try:
# function returns IPython context, but only in IPython
ipy = get_ipython() # NOQA
# we only want to render rich output in notebooks
# in terminals we definitely do not want to output HTML
name = str(ipy.__class__).lower()
terminal = 'terminal' in name
# spyder uses ZMQshell, and can appear to be a notebook
spyder | python | {
"resource": ""
} |
q23484 | _ScandinavianStemmer._r1_scandinavian | train | def _r1_scandinavian(self, word, vowels):
"""
Return the region R1 that is used by the Scandinavian stemmers.
R1 is the region after the first non-vowel following a vowel,
or is the null region at the end of the word if there is no
such non-vowel. But then R1 is adjusted so that the region
before it contains at least three letters.
:param word: The word whose region R1 is determined.
:type word: str or unicode
:param vowels: The vowels of the respective language that are
| python | {
"resource": ""
} |
q23485 | _StandardStemmer._r1r2_standard | train | def _r1r2_standard(self, word, vowels):
"""
Return the standard interpretations of the string regions R1 and R2.
R1 is the region after the first non-vowel following a vowel,
or is the null region at the end of the word if there is no
such non-vowel.
R2 is the region after the first non-vowel following a vowel
in R1, or is the null region at the end of the word if there
is no such non-vowel.
:param word: The word whose regions R1 and R2 are determined.
:type word: str or unicode
:param vowels: The vowels of the respective language that are
used to determine the regions R1 and R2.
:type vowels: unicode
:return: (r1,r2), the regions R1 and R2 for the respective word.
:rtype: tuple
:note: This helper method is invoked by the respective stem method of
the subclasses DutchStemmer, FinnishStemmer,
FrenchStemmer, GermanStemmer, ItalianStemmer,
PortugueseStemmer, RomanianStemmer, and SpanishStemmer.
It is not to be invoked directly!
| python | {
"resource": ""
} |
q23486 | _StandardStemmer._rv_standard | train | def _rv_standard(self, word, vowels):
"""
Return the standard interpretation of the string region RV.
If the second letter is a consonant, RV is the region after the
next following vowel. If the first two letters are vowels, RV is
the region after the next following consonant. Otherwise, RV is
the region after the third letter.
:param word: The word whose region RV is determined.
:type word: str or unicode
:param vowels: The vowels of the respective language that are
used to determine the region RV.
:type vowels: unicode
:return: the region RV for the respective word.
:rtype: unicode
:note: This helper method is invoked by the respective stem method of
the subclasses ItalianStemmer, PortugueseStemmer,
RomanianStemmer, and SpanishStemmer. It is not to be
invoked directly!
"""
rv = ""
| python | {
"resource": ""
} |
q23487 | FrenchStemmer.__rv_french | train | def __rv_french(self, word, vowels):
"""
Return the region RV that is used by the French stemmer.
If the word begins with two vowels, RV is the region after
the third letter. Otherwise, it is the region after the first
vowel not at the beginning of the word, or the end of the word
if these positions cannot be found. (Exceptionally, u'par',
u'col' or u'tap' at the beginning of a word is also taken to
define RV as the region to their right.)
:param word: The French word whose region RV is determined.
| python | {
"resource": ""
} |
q23488 | HungarianStemmer.__r1_hungarian | train | def __r1_hungarian(self, word, vowels, digraphs):
"""
Return the region R1 that is used by the Hungarian stemmer.
If the word begins with a vowel, R1 is defined as the region
after the first consonant or digraph (= two letters stand for
one phoneme) in the word. If the word begins with a consonant,
it is defined as the region after the first vowel in the word.
If the word does not contain both a vowel and consonant, R1
is the null region at the end of the word.
:param word: The Hungarian word whose region R1 is determined.
:type word: str or unicode
:param vowels: The Hungarian vowels that are used to determine
the region R1.
:type vowels: unicode
:param digraphs: The digraphs that are used to determine the
region R1.
:type digraphs: tuple
:return: the region R1 for the respective word.
:rtype: unicode
:note: This helper method is invoked by the stem method of the subclass | python | {
"resource": ""
} |
q23489 | RussianStemmer.__regions_russian | train | def __regions_russian(self, word):
"""
Return the regions RV and R2 which are used by the Russian stemmer.
In any word, RV is the region after the first vowel,
or the end of the word if it contains no vowel.
R2 is the region after the first non-vowel following
a vowel in R1, or the end of the word if there is no such non-vowel.
R1 is the region after the first non-vowel following a vowel,
or the end of the word if there is no such non-vowel.
:param word: The Russian word whose regions RV and R2 are determined.
:type word: str or unicode
:return: the regions RV and R2 for the respective Russian word.
:rtype: tuple
:note: This helper method is invoked by the stem method of the subclass
RussianStemmer. It is not to be invoked directly!
"""
r1 = ""
r2 = ""
rv = ""
vowels = ("A", "U", "E", "a", "e", "i", "o", "u", "y")
word = (word.replace("i^a", "A")
.replace("i^u", "U")
| python | {
"resource": ""
} |
q23490 | RussianStemmer.__cyrillic_to_roman | train | def __cyrillic_to_roman(self, word):
"""
Transliterate a Russian word into the Roman alphabet.
A Russian word whose letters consist of the Cyrillic
alphabet are transliterated into the Roman alphabet
in order to ease the forthcoming stemming process.
:param word: The word that is transliterated.
:type word: unicode
:return: the transliterated word.
:rtype: unicode
:note: This helper method is invoked by the stem method of the subclass
RussianStemmer. It is not to be invoked directly!
"""
word = (word.replace("\u0410", "a").replace("\u0430", "a")
.replace("\u0411", "b").replace("\u0431", "b")
.replace("\u0412", "v").replace("\u0432", "v")
.replace("\u0413", "g").replace("\u0433", "g")
.replace("\u0414", "d").replace("\u0434", "d")
.replace("\u0415", "e").replace("\u0435", "e")
.replace("\u0401", "e").replace("\u0451", "e")
.replace("\u0416", "zh").replace("\u0436", "zh")
.replace("\u0417", "z").replace("\u0437", "z")
| python | {
"resource": ""
} |
q23491 | RussianStemmer.__roman_to_cyrillic | train | def __roman_to_cyrillic(self, word):
"""
Transliterate a Russian word back into the Cyrillic alphabet.
A Russian word formerly transliterated into the Roman alphabet
in order to ease the stemming process, is transliterated back
into the Cyrillic alphabet, its original form.
:param word: The word that is transliterated.
:type word: str or unicode
:return: word, the transliterated word.
:rtype: unicode
:note: This helper method is invoked by the stem method of the subclass
RussianStemmer. It is not to be invoked directly!
"""
word = (word.replace("i^u", "\u044E").replace("i^a", "\u044F")
.replace("shch", "\u0449").replace("kh", "\u0445")
.replace("t^s", "\u0446").replace("ch", "\u0447")
.replace("e`", "\u044D").replace("i`", "\u0439")
.replace("sh", "\u0448").replace("k", "\u043A")
.replace("e", "\u0435").replace("zh", "\u0436")
.replace("a", "\u0430").replace("b", "\u0431")
| python | {
"resource": ""
} |
q23492 | SwedishStemmer.stem | train | def stem(self, word):
"""
Stem a Swedish word and return the stemmed form.
:param word: The word that is stemmed.
:type word: str or unicode
:return: The stemmed form.
:rtype: unicode
"""
word = word.lower()
r1 = self._r1_scandinavian(word, self.__vowels)
# STEP 1
for suffix in self.__step1_suffixes:
if r1.endswith(suffix):
if suffix == "s":
if word[-2] in self.__s_ending:
word = word[:-1]
r1 = r1[:-1]
else:
word = word[:-len(suffix)]
r1 = r1[:-len(suffix)]
break
# STEP | python | {
"resource": ""
} |
q23493 | deaccent | train | def deaccent(text):
"""
Remove accentuation from the given string.
"""
norm = unicodedata.normalize("NFD", text)
result = "".join(ch for | python | {
"resource": ""
} |
q23494 | tokenize | train | def tokenize(text, lowercase=False, deacc=False):
"""
Iteratively yield tokens as unicode strings, optionally also lowercasing them
and removing accent marks.
"""
if lowercase:
text = text.lower()
if deacc: | python | {
"resource": ""
} |
q23495 | clean_text_by_sentences | train | def clean_text_by_sentences(text, language="english", additional_stopwords=None):
""" Tokenizes a given text into sentences, applying filters and lemmatizing them.
Returns a | python | {
"resource": ""
} |
q23496 | clean_text_by_word | train | def clean_text_by_word(text, language="english", deacc=False, additional_stopwords=None):
""" Tokenizes a given text into words, applying filters and lemmatizing them.
Returns a dict of word -> syntacticUnit. """
init_textcleanner(language, additional_stopwords)
text_without_acronyms = replace_with_separator(text, "", [AB_ACRONYM_LETTERS]) | python | {
"resource": ""
} |
q23497 | _get_sentences_with_word_count | train | def _get_sentences_with_word_count(sentences, words):
""" Given a list of sentences, returns a list of sentences with a
total word count similar to the word count provided.
"""
word_count = 0
selected_sentences = []
# Loops until the word count is reached.
for sentence in sentences:
words_in_sentence = len(sentence.text.split())
# Checks if the inclusion of the sentence gives a better approximation
| python | {
"resource": ""
} |
q23498 | pagerank_weighted | train | def pagerank_weighted(graph, initial_value=None, damping=0.85):
"""Calculates PageRank for an undirected graph"""
if initial_value == None: initial_value = 1.0 / len(graph.nodes())
scores = dict.fromkeys(graph.nodes(), initial_value)
iteration_quantity = 0
for iteration_number in range(100):
iteration_quantity += 1
convergence_achieved = 0
for i in graph.nodes():
rank = 1 - damping
for j in graph.neighbors(i):
neighbors_sum = sum(graph.edge_weight((j, k)) for k in graph.neighbors(j))
| python | {
"resource": ""
} |
q23499 | Env.db_url | train | def db_url(self, var=DEFAULT_DATABASE_ENV, default=NOTSET, engine=None):
"""Returns a config dictionary, defaulting to | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.