_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q22900 | unbool | train | def unbool(element, true=object(), false=object()):
"""
A hack to make True and 1 and False and 0 unique for ``uniq``.
"""
if element | python | {
"resource": ""
} |
q22901 | uniq | train | def uniq(container):
"""
Check if all of a container's elements are unique.
Successively tries first to rely that the elements are hashable, then
falls back on them being sortable, and finally falls back on brute
force.
"""
try:
return len(set(unbool(i) for i in container)) == len(container)
except TypeError:
try:
sort = sorted(unbool(i) for i in container)
sliced = itertools.islice(sort, 1, None)
for i, j in zip(sort, sliced):
| python | {
"resource": ""
} |
q22902 | FormatChecker.checks | train | def checks(self, format, raises=()):
"""
Register a decorated function as validating a new format.
Arguments:
format (str):
The format that the decorated function will check.
raises (Exception):
The exception(s) raised by the | python | {
"resource": ""
} |
q22903 | _generate_legacy_type_checks | train | def _generate_legacy_type_checks(types=()):
"""
Generate newer-style type checks out of JSON-type-name-to-type mappings.
Arguments:
types (dict):
A mapping of type names to their Python types
Returns:
A dictionary of definitions to pass to `TypeChecker`
"""
types = dict(types)
def gen_type_check(pytypes):
pytypes = _utils.flatten(pytypes)
def type_check(checker, instance):
if isinstance(instance, bool):
if bool not in pytypes:
| python | {
"resource": ""
} |
q22904 | extend | train | def extend(validator, validators=(), version=None, type_checker=None):
"""
Create a new validator class by extending an existing one.
Arguments:
validator (jsonschema.IValidator):
an existing validator class
validators (collections.Mapping):
a mapping of new validator callables to extend with, whose
structure is as in `create`.
.. note::
Any validator callables with the same name as an existing one
will (silently) replace the old validator callable entirely,
effectively overriding any validation done in the "parent"
validator class.
If you wish to instead extend the behavior of a parent's
validator callable, delegate and call it directly in the new
validator function by retrieving it using
``OldValidator.VALIDATORS["validator_name"]``.
version (str):
a version for the new validator class
type_checker (jsonschema.TypeChecker):
a type checker, used when applying the :validator:`type` validator.
If unprovided, the type checker of the extended
`jsonschema.IValidator` will be carried along.`
Returns:
a new `jsonschema.IValidator` class extending the one provided
.. note:: Meta Schemas
The new validator | python | {
"resource": ""
} |
q22905 | validator_for | train | def validator_for(schema, default=_LATEST_VERSION):
"""
Retrieve the validator class appropriate for validating the given schema.
Uses the :validator:`$schema` property that should be present in the given
schema to look up the appropriate validator class.
Arguments:
schema (collections.Mapping or bool):
the schema to look at
default:
the default to return if the appropriate validator class cannot be
determined.
If unprovided, the default is to return
the latest supported draft.
"""
if schema is True or schema is False or u"$schema" not | python | {
"resource": ""
} |
q22906 | RefResolver.resolving | train | def resolving(self, ref):
"""
Resolve the given ``ref`` and enter its resolution scope.
Exits the scope on exit of this context manager.
Arguments:
ref (str):
| python | {
"resource": ""
} |
q22907 | ErrorTree.total_errors | train | def total_errors(self):
"""
The total number of errors in the entire tree, including children.
"""
| python | {
"resource": ""
} |
q22908 | setup | train | def setup(app):
"""
Install the plugin.
Arguments:
app (sphinx.application.Sphinx):
the Sphinx application context
"""
app.add_config_value("cache_path", "_cache", "")
try:
os.makedirs(app.config.cache_path)
except OSError as error:
if error.errno != | python | {
"resource": ""
} |
q22909 | fetch_or_load | train | def fetch_or_load(spec_path):
"""
Fetch a new specification or use the cache if it's current.
Arguments:
cache_path:
the path to a cached specification
"""
headers = {}
try:
modified = datetime.utcfromtimestamp(os.path.getmtime(spec_path))
date = modified.strftime("%a, %d %b %Y %I:%M:%S UTC")
headers["If-Modified-Since"] = date
except OSError as error:
if error.errno != errno.ENOENT:
| python | {
"resource": ""
} |
q22910 | namedAny | train | def namedAny(name):
"""
Retrieve a Python object by its fully qualified name from the global Python
module namespace. The first part of the name, that describes a module,
will be discovered and imported. Each subsequent part of the name is
treated as the name of an attribute of the object specified by all of the
name which came before it. For example, the fully-qualified name of this
object is 'twisted.python.reflect.namedAny'.
@type name: L{str}
@param name: The name of the object to return.
@raise InvalidName: If the name is an empty string, starts or ends with
a '.', or is otherwise syntactically incorrect.
@raise ModuleNotFound: If the name is syntactically correct but the
module it specifies cannot be imported because it does not appear to
exist.
@raise ObjectNotFound: If the name is syntactically correct, includes at
least one '.', but the module it specifies cannot be imported because
it does not appear to exist.
@raise AttributeError: If an attribute of an object along the way cannot be
accessed, or a module along the way is not found.
@return: the Python object identified by 'name'.
"""
if not name:
raise InvalidName('Empty module name')
names = name.split('.')
# if the name starts or ends with a '.' or contains '..', the __import__
# will raise an 'Empty module name' | python | {
"resource": ""
} |
q22911 | face_adjacency_unshared | train | def face_adjacency_unshared(mesh):
"""
Return the vertex index of the two vertices not in the shared
edge between two adjacent faces
Parameters
----------
mesh : Trimesh object
Returns
-----------
vid_unshared : (len(mesh.face_adjacency), 2) int
Indexes of mesh.vertices
"""
# the non- shared vertex index is the same shape as face_adjacnecy
# just holding vertex indices rather than face indices
vid_unshared = np.zeros_like(mesh.face_adjacency,
dtype=np.int64)
# loop through both columns of face adjacency
for i, adjacency in enumerate(mesh.face_adjacency.T):
| python | {
"resource": ""
} |
q22912 | face_adjacency_radius | train | def face_adjacency_radius(mesh):
"""
Compute an approximate radius between adjacent faces.
Parameters
--------------
mesh : trimesh.Trimesh
Returns
-------------
radii : (len(self.face_adjacency),) float
Approximate radius between faces
Parallel faces will have a value of np.inf
span : (len(self.face_adjacency),) float
Perpendicular projection distance of two
unshared vertices onto the shared edge
"""
# solve for the radius of the adjacent faces
# distance
# R = ------------------
# 2 * sin(theta / 2)
nonzero = mesh.face_adjacency_angles > np.radians(.01)
denominator = np.abs(
2.0 * np.sin(mesh.face_adjacency_angles[nonzero] / 1.0))
# consider the distance between the non- shared vertices of the
# face adjacency pair as the key distance
point_pairs = mesh.vertices[mesh.face_adjacency_unshared]
vectors = np.diff(point_pairs,
axis=1).reshape((-1, 3))
| python | {
"resource": ""
} |
q22913 | vertex_adjacency_graph | train | def vertex_adjacency_graph(mesh):
"""
Returns a networkx graph representing the vertices and
their connections in the mesh.
Parameters
----------
mesh : Trimesh object
Returns
---------
graph : networkx.Graph
Graph representing vertices and edges between
them where vertices are nodes and edges are edges
Examples
----------
| python | {
"resource": ""
} |
q22914 | shared_edges | train | def shared_edges(faces_a, faces_b):
"""
Given two sets of faces, find the edges which are in both sets.
Parameters
---------
faces_a: (n,3) int, set of faces
faces_b: (m,3) int, set of faces
Returns
---------
shared: (p, 2) int, set of edges
"""
| python | {
"resource": ""
} |
q22915 | connected_edges | train | def connected_edges(G, nodes):
"""
Given graph G and list of nodes, return the list of edges that
are connected to nodes
"""
nodes_in_G = collections.deque()
for node in nodes:
if not G.has_node(node):
continue
| python | {
"resource": ""
} |
q22916 | facets | train | def facets(mesh, engine=None):
"""
Find the list of parallel adjacent faces.
Parameters
---------
mesh : trimesh.Trimesh
engine : str
Which graph engine to use:
('scipy', 'networkx', 'graphtool')
Returns
---------
facets : sequence of (n,) int
Groups of face indexes of
parallel adjacent faces.
"""
# what is the radius of a circle that passes through the perpendicular
# projection of the vector between the two non- shared vertices
# onto the shared edge, with the face normal from the two adjacent faces
radii = mesh.face_adjacency_radius
# what is the span perpendicular to the shared edge
span = mesh.face_adjacency_span
# a very arbitrary formula for declaring two adjacent faces
# parallel in a way that is hopefully (and anecdotally) robust
# to numeric error
# a common failure mode is two faces that are very narrow with a slight
# angle between them, so here we divide by the perpendicular span | python | {
"resource": ""
} |
q22917 | split | train | def split(mesh,
only_watertight=True,
adjacency=None,
engine=None):
"""
Split a mesh into multiple meshes from face connectivity.
If only_watertight is true, it will only return watertight meshes
and will attempt single triangle/quad repairs.
Parameters
----------
mesh: Trimesh
only_watertight: if True, only return watertight components
adjacency: (n,2) list of face adjacency to override using the plain
adjacency calculated automatically.
engine: str, which engine to use. ('networkx', 'scipy', or 'graphtool')
Returns
----------
meshes: list of Trimesh objects
"""
if adjacency is None:
adjacency = mesh.face_adjacency
# if only watertight the shortest thing | python | {
"resource": ""
} |
q22918 | connected_component_labels | train | def connected_component_labels(edges, node_count=None):
"""
Label graph nodes from an edge list, using scipy.sparse.csgraph
Parameters
----------
edges : (n, 2) int
Edges of a graph
node_count : int, or None
The largest node in the graph.
Returns
---------
labels : (node_count,) int
| python | {
"resource": ""
} |
q22919 | split_traversal | train | def split_traversal(traversal,
edges,
edges_hash=None):
"""
Given a traversal as a list of nodes, split the traversal
if a sequential index pair is not in the given edges.
Parameters
--------------
edges : (n, 2) int
Graph edge indexes
traversal : (m,) int
Traversal through edges
edge_hash : (n,)
Edges sorted on axis=1 and
passed to grouping.hashable_rows
Returns
---------------
split : sequence of (p,) int
"""
traversal = np.asanyarray(traversal,
dtype=np.int64)
# hash edge rows for contains checks
if edges_hash is None:
edges_hash = grouping.hashable_rows(
np.sort(edges, axis=1))
# turn the (n,) traversal into (n-1,2) edges
trav_edge = np.column_stack((traversal[:-1],
traversal[1:]))
# hash each edge so we can compare to edge set
trav_hash = grouping.hashable_rows(
np.sort(trav_edge, axis=1))
# check if each edge is contained in edge set
contained = np.in1d(trav_hash, edges_hash)
# exit early if every edge of traversal exists
if contained.all():
# just reshape one traversal
split = [traversal]
else:
# find contiguous groups of contained edges
blocks = grouping.blocks(contained,
| python | {
"resource": ""
} |
q22920 | fill_traversals | train | def fill_traversals(traversals, edges, edges_hash=None):
"""
Convert a traversal of a list of edges into a sequence of
traversals where every pair of consecutive node indexes
is an edge in a passed edge list
Parameters
-------------
traversals : sequence of (m,) int
Node indexes of traversals of a graph
edges : (n, 2) int
Pairs of connected node indexes
edges_hash : None, or (n,) int
Edges sorted along axis 1 then hashed
using grouping.hashable_rows
Returns
--------------
splits : sequence of (p,) int
Node indexes of connected traversals
"""
# make sure edges are correct type
edges = np.asanyarray(edges, dtype=np.int64)
# make sure edges are sorted
edges.sort(axis=1)
# if there are no traversals just return edges
if len(traversals) == 0:
return edges.copy()
# hash edges for contains checks
if edges_hash is None:
edges_hash = grouping.hashable_rows(edges)
splits = []
for nodes in traversals:
# split traversals to remove edges
# that don't actually exist
splits.extend(split_traversal(
traversal=nodes,
edges=edges,
| python | {
"resource": ""
} |
q22921 | traversals | train | def traversals(edges, mode='bfs'):
"""
Given an edge list, generate a sequence of ordered
depth first search traversals, using scipy.csgraph routines.
Parameters
------------
edges : (n,2) int, undirected edges of a graph
mode : str, 'bfs', or 'dfs'
Returns
-----------
traversals: (m,) sequence of (p,) int,
ordered DFS or BFS traversals of the graph.
"""
edges = np.asanyarray(edges, dtype=np.int64)
if len(edges) == 0:
return []
elif not util.is_shape(edges, (-1, 2)):
raise ValueError('edges are not (n,2)!')
# pick the traversal method
mode = str(mode).lower().strip()
if mode == 'bfs': | python | {
"resource": ""
} |
q22922 | edges_to_coo | train | def edges_to_coo(edges, count=None, data=None):
"""
Given an edge list, return a boolean scipy.sparse.coo_matrix
representing the edges in matrix form.
Parameters
------------
edges : (n,2) int
Edges of a graph
count : int
The total number of nodes in the graph
if None: count = edges.max() + 1
data : (n,) any
Assign data to each edge, if None will
be bool True for each specified edge
Returns
------------
matrix: (count, count) scipy.sparse.coo_matrix
Sparse COO
"""
edges = np.asanyarray(edges, dtype=np.int64)
if not (len(edges) == 0 or
util.is_shape(edges, (-1, 2))):
raise ValueError('edges must be (n,2)!')
# | python | {
"resource": ""
} |
q22923 | smoothed | train | def smoothed(mesh, angle):
"""
Return a non- watertight version of the mesh which will
render nicely with smooth shading by disconnecting faces
at sharp angles to each other.
Parameters
---------
mesh : trimesh.Trimesh
Source geometry
angle : float
Angle in radians, adjacent faces which have normals
below this angle will be smoothed
Returns
---------
smooth : trimesh.Trimesh
Geometry with disconnected face patches
"""
# if the mesh has no adjacent faces return a copy
if len(mesh.face_adjacency) == 0:
return mesh.copy()
# face pairs below angle threshold
angle_ok = mesh.face_adjacency_angles <= angle
# subset of face adjacency
| python | {
"resource": ""
} |
q22924 | graph_to_svg | train | def graph_to_svg(graph):
"""
Turn a networkx graph into an SVG string, using graphviz dot.
Parameters
----------
graph: networkx graph
Returns
---------
svg: string, pictoral | python | {
"resource": ""
} |
q22925 | multigraph_paths | train | def multigraph_paths(G, source, cutoff=None):
"""
For a networkx MultiDiGraph, find all paths from a source node
to leaf nodes. This function returns edge instance numbers
in addition to nodes, unlike networkx.all_simple_paths.
Parameters
---------------
G : networkx.MultiDiGraph
Graph to evaluate
source : hashable
Node to start traversal at
cutoff : int
Number of nodes to visit
If None will visit all nodes
Returns
----------
traversals : (n,) list of [(node, edge instance index), ] paths
Traversals of the multigraph
"""
if cutoff is None:
cutoff = (len(G.edges()) * len(G.nodes())) + 1
# the path starts at the node specified
current = [(source, 0)]
# traversals we need to go back and do
queue = []
# completed paths
traversals = []
for i in range(cutoff):
# paths are stored as (node, instance) so
# get the node of the last place visited
current_node = current[-1][0]
| python | {
"resource": ""
} |
q22926 | multigraph_collect | train | def multigraph_collect(G, traversal, attrib=None):
"""
Given a MultiDiGraph traversal, collect attributes along it.
Parameters
-------------
G: networkx.MultiDiGraph
traversal: (n) list of (node, instance) tuples
attrib: dict key, name to collect. If None, will return all
Returns
-------------
collected: (len(traversal) - 1) list of attributes
| python | {
"resource": ""
} |
q22927 | kwargs_to_matrix | train | def kwargs_to_matrix(**kwargs):
"""
Turn a set of keyword arguments into a transformation matrix.
"""
matrix = np.eye(4)
if 'matrix' in kwargs:
# a matrix takes precedence over other options
matrix = kwargs['matrix']
elif 'quaternion' in kwargs:
matrix = transformations.quaternion_matrix(kwargs['quaternion'])
elif ('axis' in kwargs) and ('angle' in kwargs):
matrix = transformations.rotation_matrix(kwargs['angle'],
kwargs['axis'])
else:
| python | {
"resource": ""
} |
q22928 | TransformForest.update | train | def update(self, frame_to, frame_from=None, **kwargs):
"""
Update a transform in the tree.
Parameters
---------
frame_from : hashable object
Usually a string (eg 'world').
If left as None it will be set to self.base_frame
frame_to : hashable object
Usually a string (eg 'mesh_0')
matrix : (4,4) float
Homogenous transformation matrix
quaternion : (4,) float
Quaternion ordered [w, x, y, z]
axis : (3,) float
Axis of rotation
angle : float
Angle of rotation, in radians
translation : (3,) float
Distance to translate
geometry : hashable
Geometry object name, e.g. 'mesh_0'
"""
# save a random number for this update
self._updated = np.random.random()
# if no frame specified, use base frame
if frame_from is None:
frame_from = self.base_frame
# convert various kwargs to a single matrix
matrix = kwargs_to_matrix(**kwargs)
# create the edge attributes
attr = {'matrix': matrix, 'time': time.time()}
# pass through | python | {
"resource": ""
} |
q22929 | TransformForest.md5 | train | def md5(self):
"""
"Hash" of transforms
Returns
-----------
md5 : str
Approximate hash of transforms
"""
| python | {
"resource": ""
} |
q22930 | TransformForest.copy | train | def copy(self):
"""
Return a copy of the current TransformForest
Returns
------------
copied: TransformForest
"""
copied | python | {
"resource": ""
} |
q22931 | TransformForest.to_flattened | train | def to_flattened(self, base_frame=None):
"""
Export the current transform graph as a flattened
"""
if base_frame is None:
base_frame = self.base_frame
flat = {}
for node in | python | {
"resource": ""
} |
q22932 | TransformForest.to_gltf | train | def to_gltf(self, scene):
"""
Export a transforms as the 'nodes' section of a GLTF dict.
Flattens tree.
Returns
--------
gltf : dict
with keys:
'nodes': list of dicts
"""
# geometry is an OrderedDict
# {geometry key : index}
mesh_index = {name: i for i, name
in enumerate(scene.geometry.keys())}
# save the output
gltf = collections.deque([])
for node in self.nodes:
# don't include edge for base frame
if node == self.base_frame:
continue
# get the transform and geometry from the graph
transform, geometry = self.get(
frame_to=node, frame_from=self.base_frame)
gltf.append({
'matrix': transform.T.reshape(-1).tolist(),
'name': node})
| python | {
"resource": ""
} |
q22933 | TransformForest.from_edgelist | train | def from_edgelist(self, edges, strict=True):
"""
Load transform data from an edge list into the current
scene graph.
Parameters
-------------
edgelist : (n,) tuples
(node_a, node_b, {key: value})
strict : bool
If true, raise a ValueError when a
malformed edge is passed in a tuple.
"""
# loop through each edge
for edge in edges:
# edge contains attributes
| python | {
"resource": ""
} |
q22934 | TransformForest.nodes | train | def nodes(self):
"""
A list of every node in the graph.
Returns
-------------
nodes: (n,) array, of node names
| python | {
"resource": ""
} |
q22935 | TransformForest.nodes_geometry | train | def nodes_geometry(self):
"""
The nodes in the scene graph with geometry attached.
Returns
------------
nodes_geometry: (m,) array, of node names
"""
nodes = np.array([
| python | {
"resource": ""
} |
q22936 | TransformForest.get | train | def get(self, frame_to, frame_from=None):
"""
Get the transform from one frame to another, assuming they are connected
in the transform tree.
If the frames are not connected a NetworkXNoPath error will be raised.
Parameters
---------
frame_from: hashable object, usually a string (eg 'world').
If left as None it will be set to self.base_frame
frame_to: hashable object, usually a string (eg 'mesh_0')
Returns
---------
transform: (4,4) homogenous transformation matrix
"""
if frame_from is None:
frame_from = self.base_frame
cache_key = str(frame_from) + ':' + str(frame_to)
cached = self._cache[cache_key]
if cached is not None:
return cached
transform = np.eye(4)
path = self._get_path(frame_from, frame_to)
for i in range(len(path) - 1):
data, direction = self.transforms.get_edge_data_direction( | python | {
"resource": ""
} |
q22937 | TransformForest.show | train | def show(self):
"""
Plot the graph layout of the scene.
"""
import matplotlib.pyplot as plt
| python | {
"resource": ""
} |
q22938 | TransformForest._get_path | train | def _get_path(self, frame_from, frame_to):
"""
Find a path between two frames, either from cached paths or
from the transform graph.
Parameters
---------
frame_from: a frame key, usually a string
eg, 'world'
frame_to: a frame key, usually a string
eg, 'mesh_0'
Returns
----------
path: (n) list of frame keys
| python | {
"resource": ""
} |
q22939 | is_ccw | train | def is_ccw(points):
"""
Check if connected planar points are counterclockwise.
Parameters
-----------
points: (n,2) float, connected points on a plane
Returns
----------
ccw: bool, True if points are counterclockwise
"""
points = np.asanyarray(points, dtype=np.float64)
if (len(points.shape) != 2 or
points.shape[1] != 2):
| python | {
"resource": ""
} |
q22940 | concatenate | train | def concatenate(paths):
"""
Concatenate multiple paths into a single path.
Parameters
-------------
paths: list of Path, Path2D, or Path3D objects
Returns
-------------
concat: Path, Path2D, or Path3D object
"""
# if only one path object just return copy
if len(paths) == 1:
return paths[0].copy()
# length of vertex arrays
vert_len = np.array([len(i.vertices) for i in paths])
# how much to offset each paths vertex indices by
offsets = np.append(0.0, np.cumsum(vert_len))[:-1].astype(np.int64)
# resulting entities
entities = []
# resulting vertices
vertices = []
# resulting metadata
metadata = {}
for path, offset in zip(paths, offsets):
# update metadata
metadata.update(path.metadata) | python | {
"resource": ""
} |
q22941 | filter_humphrey | train | def filter_humphrey(mesh,
alpha=0.1,
beta=0.5,
iterations=10,
laplacian_operator=None):
"""
Smooth a mesh in-place using laplacian smoothing
and Humphrey filtering.
Articles
"Improved Laplacian Smoothing of Noisy Surface Meshes"
J. Vollmer, R. Mencl, and H. Muller
Parameters
------------
mesh : trimesh.Trimesh
Mesh to be smoothed in place
alpha : float
Controls shrinkage, range is 0.0 - 1.0
If 0.0, not considered
If 1.0, no smoothing
beta : float
Controls how aggressive smoothing is
If 0.0, no smoothing
If 1.0, full aggressiveness
iterations : int
Number of passes to run filter
laplacian_operator : None or scipy.sparse.coo.coo_matrix
Sparse matrix laplacian operator
Will be autogenerated if None
"""
# if the laplacian operator | python | {
"resource": ""
} |
q22942 | filter_taubin | train | def filter_taubin(mesh,
lamb=0.5,
nu=0.5,
iterations=10,
laplacian_operator=None):
"""
Smooth a mesh in-place using laplacian smoothing
and taubin filtering.
Articles
"Improved Laplacian Smoothing of Noisy Surface Meshes"
J. Vollmer, R. Mencl, and H. Muller
Parameters
------------
mesh : trimesh.Trimesh
Mesh to be smoothed in place.
lamb : float
Controls shrinkage, range is 0.0 - 1.0
nu : float
Controls dilation, range is 0.0 - 1.0
Nu shall be between 0.0 < 1.0/lambda - 1.0/nu < 0.1
iterations : int
Number of passes to run the filter
laplacian_operator : None or scipy.sparse.coo.coo_matrix
Sparse matrix laplacian operator
Will be autogenerated if None
"""
# if the laplacian operator was not passed create it | python | {
"resource": ""
} |
q22943 | laplacian_calculation | train | def laplacian_calculation(mesh, equal_weight=True):
"""
Calculate a sparse matrix for laplacian operations.
Parameters
-------------
mesh : trimesh.Trimesh
Input geometry
equal_weight : bool
If True, all neighbors will be considered equally
If False, all neightbors will be weighted by inverse distance
Returns
----------
laplacian : scipy.sparse.coo.coo_matrix
Laplacian operator
"""
# get the vertex neighbors from the cache
neighbors = mesh.vertex_neighbors
# avoid hitting crc checks in loops
vertices = mesh.vertices.view(np.ndarray)
# stack neighbors to 1D arrays
col = np.concatenate(neighbors)
row = np.concatenate([[i] * len(n)
for i, n in enumerate(neighbors)])
if equal_weight:
# equal weights for each neighbor
data = np.concatenate([[1.0 / len(n)] * len(n)
for n in neighbors])
else:
# umbrella weights, distance-weighted | python | {
"resource": ""
} |
q22944 | is_circle | train | def is_circle(points, scale, verbose=False):
"""
Given a set of points, quickly determine if they represent
a circle or not.
Parameters
-------------
points: (n,2) float, points in space
scale: float, scale of overall drawing
verbose: bool, print all fit messages or not
Returns
-------------
control: (3,2) float, points in space, OR
None, if not a circle
"""
# make sure input is a numpy array
points = np.asanyarray(points)
scale = float(scale)
# can only be a circle if the first and last point are the
# same (AKA is a closed path)
if np.linalg.norm(points[0] - points[-1]) > tol.merge:
return None
box = points.ptp(axis=0)
# | python | {
"resource": ""
} |
q22945 | merge_colinear | train | def merge_colinear(points, scale):
"""
Given a set of points representing a path in space,
merge points which are colinear.
Parameters
----------
points: (n, d) set of points (where d is dimension)
scale: float, scale of drawing
Returns
----------
merged: (j, d) set of points with colinear and duplicate
points merged, where (j < n)
"""
points = np.asanyarray(points, dtype=np.float64)
scale = float(scale)
if len(points.shape) != 2 or points.shape[1] != 2:
raise ValueError('only for 2D points!')
# if there's less than 3 points nothing to merge
if len(points) < 3:
return points.copy()
# the vector from one point to the next
direction = points[1:] - points[:-1]
# the length of the direction vector
direction_norm = np.linalg.norm(direction, axis=1)
# | python | {
"resource": ""
} |
q22946 | resample_spline | train | def resample_spline(points, smooth=.001, count=None, degree=3):
"""
Resample a path in space, smoothing along a b-spline.
Parameters
-----------
points: (n, dimension) float, points in space
smooth: float, smoothing amount
count: number of samples in output
degree: int, degree of spline polynomial
Returns
---------
resampled: (count, dimension) | python | {
"resource": ""
} |
q22947 | points_to_spline_entity | train | def points_to_spline_entity(points, smooth=None, count=None):
"""
Create a spline entity from a curve in space
Parameters
-----------
points: (n, dimension) float, points in space
smooth: float, smoothing amount
count: int, number of samples in result
Returns
---------
entity: entities.BSpline object with points indexed at zero
control: (m, dimension) float, new vertices for entity
"""
from scipy.interpolate import splprep
if count is None:
count = len(points)
if smooth is None:
smooth = 0.002
points = np.asanyarray(points, dtype=np.float64)
closed = np.linalg.norm(points[0] - points[-1]) < tol.merge
knots, control, degree = splprep(points.T, s=smooth)[0]
| python | {
"resource": ""
} |
q22948 | simplify_basic | train | def simplify_basic(drawing, process=False, **kwargs):
"""
Merge colinear segments and fit circles.
Parameters
-----------
drawing: Path2D object, will not be modified.
Returns
-----------
simplified: Path2D with circles.
"""
if any(i.__class__.__name__ != 'Line'
for i in drawing.entities):
log.debug('Path contains non- linear entities, skipping')
return drawing
# we are going to do a bookkeeping to avoid having
# to recompute literally everything when simplification is ran
cache = copy.deepcopy(drawing._cache)
# store new values
vertices_new = collections.deque()
entities_new = collections.deque()
# avoid thrashing cache in loop
scale = drawing.scale
# loop through (n, 2) closed paths
for discrete in drawing.discrete:
# check to see if the closed entity is a circle
circle = is_circle(discrete,
scale=scale)
if circle is not None:
# the points are circular enough for our high standards
# so replace them with a closed Arc entity
entities_new.append(entities.Arc(points=np.arange(3) +
len(vertices_new),
closed=True))
vertices_new.extend(circle)
else:
# not a circle, so clean up colinear segments
# then save it as a single line entity
points = merge_colinear(discrete, scale=scale)
# references for new vertices
indexes = np.arange(len(points)) + len(vertices_new)
# discrete curves are always closed
indexes[-1] = indexes[0]
# append new vertices and entity
| python | {
"resource": ""
} |
q22949 | simplify_spline | train | def simplify_spline(path, smooth=None, verbose=False):
"""
Replace discrete curves with b-spline or Arc and
return the result as a new Path2D object.
Parameters
------------
path : trimesh.path.Path2D
Input geometry
smooth : float
Distance to smooth
Returns
------------
simplified : Path2D
Consists of Arc and BSpline entities
"""
new_vertices = []
new_entities = []
scale = path.scale
for discrete in path.discrete:
circle = is_circle(discrete,
scale=scale,
verbose=verbose)
if circle is not None:
# the points are circular enough for our high standards
# so replace them with a closed Arc entity
new_entities.append(entities.Arc(points=np.arange(3) +
len(new_vertices),
| python | {
"resource": ""
} |
q22950 | boolean | train | def boolean(meshes, operation='difference'):
"""
Run an operation on a set of meshes
"""
script = operation + '(){'
for i in range(len(meshes)):
| python | {
"resource": ""
} |
q22951 | parse_mtl | train | def parse_mtl(mtl):
"""
Parse a loaded MTL file.
Parameters
-------------
mtl : str or bytes
Data from an MTL file
Returns
------------
mtllibs : list of dict
Each dict has keys: newmtl, map_Kd, Kd
"""
# decode bytes if necessary
if hasattr(mtl, 'decode'):
mtl = mtl.decode('utf-8')
mtllib = None
mtllibs = []
# use universal newline splitting
for line in str.splitlines(str(mtl).strip()):
| python | {
"resource": ""
} |
q22952 | export_wavefront | train | def export_wavefront(mesh,
include_normals=True,
include_texture=True):
"""
Export a mesh as a Wavefront OBJ file
Parameters
-----------
mesh: Trimesh object
Returns
-----------
export: str, string of OBJ format output
"""
# store the multiple options for formatting
# a vertex index for a face
face_formats = {('v',): '{}',
('v', 'vn'): '{}//{}',
('v', 'vt'): '{}/{}',
('v', 'vn', 'vt'): '{}/{}/{}'}
# we are going to reference face_formats with this
face_type = ['v']
export = 'v '
export += util.array_to_string(mesh.vertices,
col_delim=' ',
row_delim='\nv ',
digits=8) + '\n'
if include_normals and 'vertex_normals' in mesh._cache:
# if vertex normals are stored in cache | python | {
"resource": ""
} |
q22953 | RayMeshIntersector._scale | train | def _scale(self):
"""
Scaling factor for precision.
"""
if self._scale_to_box:
# scale vertices to approximately a cube to help with
| python | {
"resource": ""
} |
q22954 | RayMeshIntersector._scene | train | def _scene(self):
"""
A cached version of the pyembree scene.
"""
return _EmbreeWrap(vertices=self.mesh.vertices,
| python | {
"resource": ""
} |
q22955 | RayMeshIntersector.intersects_location | train | def intersects_location(self,
ray_origins,
ray_directions,
multiple_hits=True):
"""
Return the location of where a ray hits a surface.
Parameters
----------
ray_origins: (n,3) float, origins of rays
ray_directions: (n,3) float, direction (vector) of rays
Returns
---------
locations: (n) sequence of (m,3) intersection points
index_ray: (n,) int, list of ray index
index_tri: (n,) int, list of triangle (face) indexes
| python | {
"resource": ""
} |
q22956 | RayMeshIntersector.intersects_id | train | def intersects_id(self,
ray_origins,
ray_directions,
multiple_hits=True,
max_hits=20,
return_locations=False):
"""
Find the triangles hit by a list of rays, including
optionally multiple hits along a single ray.
Parameters
----------
ray_origins: (n,3) float, origins of rays
ray_directions: (n,3) float, direction (vector) of rays
multiple_hits: bool, if True will return every hit along the ray
if False will only return first hit
return_locations: bool, should we return hit locations or not
Returns
----------
index_tri: (m,) int, index of triangle the ray hit
index_ray: (m,) int, index of ray
locations: (m,3) float, locations in space
"""
# make sure input is _dtype for embree
ray_origins = np.asanyarray(
deepcopy(ray_origins),
dtype=np.float64)
ray_directions = np.asanyarray(ray_directions,
dtype=np.float64)
ray_directions = util.unitize(ray_directions)
# since we are constructing all hits, save them to a deque then
# stack into (depth, len(rays)) at the end
result_triangle = deque()
result_ray_idx = deque()
result_locations = deque()
# the mask for which rays are still active
current = np.ones(len(ray_origins), dtype=np.bool)
if multiple_hits or return_locations:
# how much to offset ray to transport to the other side of face
distance = np.clip(_ray_offset_factor * self._scale,
_ray_offset_floor,
np.inf)
ray_offsets = ray_directions * distance
# grab the planes from triangles
plane_origins = self.mesh.triangles[:, 0, :]
plane_normals = self.mesh.face_normals
# use a for loop rather than a while to ensure this exits
# if a ray is offset from a triangle | python | {
"resource": ""
} |
q22957 | RayMeshIntersector.intersects_first | train | def intersects_first(self,
ray_origins,
ray_directions):
"""
Find the index of the first triangle a ray hits.
Parameters
----------
ray_origins: (n,3) float, origins of rays
ray_directions: (n,3) float, direction (vector) | python | {
"resource": ""
} |
q22958 | RayMeshIntersector.intersects_any | train | def intersects_any(self,
ray_origins,
ray_directions):
"""
Check if a list of rays hits the surface.
Parameters
----------
ray_origins: (n,3) float, origins of rays
| python | {
"resource": ""
} |
q22959 | _attrib_to_transform | train | def _attrib_to_transform(attrib):
"""
Extract a homogenous transform from a dictionary.
Parameters
------------
attrib: dict, optionally containing 'transform'
Returns
------------
transform: (4, 4) float, homogeonous transformation
"""
transform = np.eye(4, dtype=np.float64)
if 'transform' in attrib:
# | python | {
"resource": ""
} |
q22960 | check | train | def check(a, b, digits):
"""
Check input ranges, convert them to vector form,
and get a fixed precision integer version of them.
Parameters
--------------
a : (2, ) or (2, n) float
Start and end of a 1D interval
b : (2, ) or (2, n) float
Start and end of a 1D interval
digits : int
How many digits to consider
Returns
--------------
a : (2, n) float
Ranges as vector
b : (2, n) float
Ranges as vector
a_int : (2, n) int64
Ranges rounded to digits, as vector
b_int : (2, n) int64
Ranges rounded to digits, as vector
is_1D : bool
If True, input was single pair of ranges
"""
a = np.array(a, dtype=np.float64)
b = np.array(b, dtype=np.float64)
if | python | {
"resource": ""
} |
q22961 | intersection | train | def intersection(a, b, digits=8):
"""
Given a pair of ranges, merge them in to
one range if they overlap at all
Parameters
--------------
a : (2, ) float
Start and end of a 1D interval
b : (2, ) float
Start and end of a 1D interval
digits : int
How many digits to consider
Returns
--------------
intersects : bool or (n,) bool
Indicates if the ranges overlap at all
new_range : (2, ) or (2, 2) float
The unioned range from the two inputs,
or both of the original ranges if not overlapping
"""
# check shape and convert
a, b, a_int, b_int, is_1D = check(a, b, digits)
# what are the starting and ending points of the overlap
overlap = np.zeros(a.shape, dtype=np.float64)
# A fully overlaps B
current = np.logical_and(a_int[:, 0] <= b_int[:, 0],
a_int[:, 1] >= b_int[:, 1])
overlap[current] = b[current]
# B fully overlaps A
current = np.logical_and(a_int[:, 0] >= b_int[:, 0],
a_int[:, 1] <= b_int[:, 1])
overlap[current] = a[current]
# A starts B ends
# A:, 0 B:, 0 A:, 1 | python | {
"resource": ""
} |
q22962 | geometry_hash | train | def geometry_hash(geometry):
"""
Get an MD5 for a geometry object
Parameters
------------
geometry : object
Returns
------------
MD5 : str
"""
if hasattr(geometry, 'md5'):
# for most of our trimesh objects
md5 = geometry.md5()
elif hasattr(geometry, 'tostring'):
| python | {
"resource": ""
} |
q22963 | render_scene | train | def render_scene(scene,
resolution=(1080, 1080),
visible=True,
**kwargs):
"""
Render a preview of a scene to a PNG.
Parameters
------------
scene : trimesh.Scene
Geometry to be rendered
resolution : (2,) int
Resolution in pixels
kwargs : **
Passed to SceneViewer
Returns
---------
render : bytes
Image in PNG format
"""
window = SceneViewer(scene,
start_loop=False,
visible=visible,
resolution=resolution,
**kwargs)
if visible is None:
visible = platform.system() != 'Linux'
# need to run loop twice to display anything
for i | python | {
"resource": ""
} |
q22964 | SceneViewer.add_geometry | train | def add_geometry(self, name, geometry, **kwargs):
"""
Add a geometry to the viewer.
Parameters
--------------
name : hashable
Name that references geometry
geometry : Trimesh, Path2D, Path3D, PointCloud
Geometry to display in the viewer window
kwargs **
Passed to rendering.convert_to_vertexlist
"""
# convert geometry to constructor args
args = rendering.convert_to_vertexlist(geometry, **kwargs)
# create the indexed vertex list
self.vertex_list[name] = self.batch.add_indexed(*args)
# save the MD5 of the geometry
self.vertex_list_hash[name] = geometry_hash(geometry)
# save the rendering mode from the constructor args
| python | {
"resource": ""
} |
q22965 | SceneViewer.reset_view | train | def reset_view(self, flags=None):
"""
Set view to the default view.
Parameters
--------------
flags : None or dict
If any view key passed override the default
e.g. {'cull': False}
"""
self.view = {
'cull': True,
'axis': False,
'fullscreen': False,
'wireframe': False,
| python | {
"resource": ""
} |
q22966 | SceneViewer.init_gl | train | def init_gl(self):
"""
Perform the magic incantations to create an
OpenGL scene using pyglet.
"""
# default background color is white-ish
background = [.99, .99, .99, 1.0]
# if user passed a background color use it
if 'background' in self.kwargs:
try:
# convert to (4,) uint8 RGBA
background = to_rgba(self.kwargs['background'])
# convert to 0.0 - 1.0 float
background = background.astype(np.float64) / 255.0
| python | {
"resource": ""
} |
q22967 | SceneViewer._gl_enable_lighting | train | def _gl_enable_lighting(scene):
"""
Take the lights defined in scene.lights and
apply them as openGL lights.
"""
gl.glEnable(gl.GL_LIGHTING)
# opengl only supports 7 lights?
for i, light in enumerate(scene.lights[:7]):
# the index of which light we have
lightN = eval('gl.GL_LIGHT{}'.format(i))
# get the transform for the light by name
matrix = scene.graph[light.name][0]
# convert light object to glLightfv calls
multiargs = rendering.light_to_gl(
| python | {
"resource": ""
} |
q22968 | SceneViewer.update_flags | train | def update_flags(self):
"""
Check the view flags, and call required GL functions.
"""
# view mode, filled vs wirefrom
if self.view['wireframe']:
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
else:
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
# set fullscreen or windowed
self.set_fullscreen(fullscreen=self.view['fullscreen'])
# backface culling on or off
if self.view['cull']:
gl.glEnable(gl.GL_CULL_FACE)
else:
gl.glDisable(gl.GL_CULL_FACE)
| python | {
"resource": ""
} |
q22969 | SceneViewer.on_resize | train | def on_resize(self, width, height):
"""
Handle resized windows.
"""
width, height = self._update_perspective(width, height)
self.scene.camera.resolution = (width, height)
| python | {
"resource": ""
} |
q22970 | SceneViewer.on_mouse_press | train | def on_mouse_press(self, x, y, buttons, modifiers):
"""
Set the start point of the drag.
"""
self.view['ball'].set_state(Trackball.STATE_ROTATE)
if (buttons == pyglet.window.mouse.LEFT):
ctrl = (modifiers & pyglet.window.key.MOD_CTRL)
shift = (modifiers & pyglet.window.key.MOD_SHIFT)
if (ctrl and shift):
self.view['ball'].set_state(Trackball.STATE_ZOOM)
elif shift:
self.view['ball'].set_state(Trackball.STATE_ROLL)
elif ctrl:
| python | {
"resource": ""
} |
q22971 | SceneViewer.on_mouse_drag | train | def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
"""
Pan or rotate the view.
"""
| python | {
"resource": ""
} |
q22972 | SceneViewer.on_mouse_scroll | train | def on_mouse_scroll(self, x, y, dx, dy):
"""
Zoom the view.
"""
self.view['ball'].scroll(dy) | python | {
"resource": ""
} |
q22973 | SceneViewer.on_key_press | train | def on_key_press(self, symbol, modifiers):
"""
Call appropriate functions given key presses.
"""
magnitude = 10
if symbol == pyglet.window.key.W:
self.toggle_wireframe()
elif symbol == pyglet.window.key.Z:
self.reset_view()
elif symbol == pyglet.window.key.C:
self.toggle_culling()
elif symbol == pyglet.window.key.A:
self.toggle_axis()
elif symbol == pyglet.window.key.Q:
self.on_close()
elif symbol == pyglet.window.key.M:
self.maximize()
elif symbol == pyglet.window.key.F:
self.toggle_fullscreen()
if symbol in [
pyglet.window.key.LEFT,
pyglet.window.key.RIGHT,
pyglet.window.key.DOWN,
pyglet.window.key.UP,
]:
self.view['ball'].down([0, 0])
if symbol == pyglet.window.key.LEFT:
| python | {
"resource": ""
} |
q22974 | SceneViewer.on_draw | train | def on_draw(self):
"""
Run the actual draw calls.
"""
self._update_meshes()
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glLoadIdentity()
# pull the new camera transform from the scene
transform_camera = self.scene.graph.get(
frame_to='world',
frame_from=self.scene.camera.name)[0]
# apply the camera transform to the matrix stack
gl.glMultMatrixf(rendering.matrix_to_gl(transform_camera))
# we want to render fully opaque objects first,
# followed by objects which have transparency
node_names = collections.deque(self.scene.graph.nodes_geometry)
# how many nodes did we start with
count_original = len(node_names)
count = -1
# if we are rendering an axis marker at the world
if self._axis:
# we stored it as a vertex list
self._axis.draw(mode=gl.GL_TRIANGLES)
while len(node_names) > 0:
count += 1
current_node = node_names.popleft()
# get the transform from world to geometry and mesh name
transform, geometry_name = self.scene.graph[current_node]
# if no geometry at this frame continue without rendering
if geometry_name is None:
continue
# if a geometry is marked as fixed apply the inverse view transform
if self.fixed is not None and geometry_name in self.fixed:
# remove altered camera transform from fixed geometry
transform_fix = np.linalg.inv(
np.dot(self._initial_camera_transform, transform_camera))
# apply the transform so the fixed geometry doesn't move
transform = np.dot(transform, transform_fix)
# get a reference to the mesh so we can check transparency
mesh = self.scene.geometry[geometry_name]
if mesh.is_empty:
continue
# add a new matrix to the model stack
gl.glPushMatrix()
# transform by the nodes transform
gl.glMultMatrixf(rendering.matrix_to_gl(transform))
# draw an axis marker for each mesh frame
if self.view['axis'] == 'all':
| python | {
"resource": ""
} |
q22975 | SceneViewer.save_image | train | def save_image(self, file_obj):
"""
Save the current color buffer to a file object
in PNG format.
Parameters
-------------
file_obj: file name, or file- like object
"""
manager = pyglet.image.get_buffer_manager()
colorbuffer = manager.get_color_buffer()
| python | {
"resource": ""
} |
q22976 | unit_conversion | train | def unit_conversion(current, desired):
"""
Calculate the conversion from one set of units to another.
Parameters
---------
current : str
Unit system values are in now (eg 'millimeters')
desired : str
Unit system we'd like values in (eg 'inches')
Returns
---------
conversion : float
| python | {
"resource": ""
} |
q22977 | units_from_metadata | train | def units_from_metadata(obj, guess=True):
"""
Try to extract hints from metadata and if that fails
guess based on the object scale.
Parameters
------------
obj: object
Has attributes 'metadata' (dict) and 'scale' (float)
guess : bool
If metadata doesn't indicate units, guess from scale
Returns
------------
units: str
A guess of what the units might be
"""
# try to guess from metadata
for key in ['file_name', 'name']:
if key not in obj.metadata:
continue
# get the string which might contain unit hints
hints = obj.metadata[key].lower()
if 'unit' in hints:
# replace all delimiter options with white space
for delim in '_-.':
hints = hints.replace(delim, ' ')
# loop through each hint
for hint in hints.strip().split():
# key word is "unit" or "units"
if 'unit' not in hint:
| python | {
"resource": ""
} |
q22978 | _convert_units | train | def _convert_units(obj, desired, guess=False):
"""
Given an object with scale and units try to scale
to different units via the object's `apply_scale`.
Parameters
---------
obj : object
With apply_scale method (i.e. Trimesh, Path2D, etc)
desired : str
Units desired (eg 'inches')
guess: bool
Whether we are allowed to guess the units
if they are not specified.
"""
if obj.units is None:
# try to extract units from metadata
# if nothing specified in metadata and not allowed
# to guess will raise a ValueError
obj.units = | python | {
"resource": ""
} |
q22979 | export_path | train | def export_path(path,
file_type=None,
file_obj=None,
**kwargs):
"""
Export a Path object to a file- like object, or to a filename
Parameters
---------
file_obj: None, str, or file object
A filename string or a file-like object
file_type: None or str
File type, e.g.: 'svg', 'dxf'
kwargs : passed to loader
Returns
---------
exported : str or bytes
Data exported
"""
# if file object is a string it is probably a file path
# so we can split the extension to | python | {
"resource": ""
} |
q22980 | export_dict | train | def export_dict(path):
"""
Export a path as a dict of kwargs for the Path constructor.
"""
export_entities = [e.to_dict() for e in path.entities]
| python | {
"resource": ""
} |
q22981 | _write_export | train | def _write_export(export, file_obj=None):
"""
Write a string to a file.
If file_obj isn't specified, return the string
Parameters
---------
export: a string of the export data
file_obj: a file-like object or a filename
"""
if file_obj is None:
return export
if hasattr(file_obj, 'write'): | python | {
"resource": ""
} |
q22982 | sample_surface | train | def sample_surface(mesh, count):
"""
Sample the surface of a mesh, returning the specified
number of points
For individual triangle sampling uses this method:
http://mathworld.wolfram.com/TrianglePointPicking.html
Parameters
---------
mesh: Trimesh object
count: number of points to return
Returns
---------
samples: (count,3) points in space on the surface of mesh
face_index: (count,) indices of faces for each sampled point
"""
# len(mesh.faces) float, array of the areas
# of each face of the mesh
area = mesh.area_faces
# total area (float)
area_sum = np.sum(area)
# cumulative area (len(mesh.faces))
area_cum = np.cumsum(area)
face_pick = np.random.random(count) * area_sum
face_index = np.searchsorted(area_cum, face_pick)
# pull triangles | python | {
"resource": ""
} |
q22983 | volume_mesh | train | def volume_mesh(mesh, count):
"""
Use rejection sampling to produce points randomly distributed
in the volume of a mesh.
Parameters
----------
mesh: Trimesh object
count: int, number of samples desired
Returns
----------
samples: (n,3) float, points in the volume of the mesh.
| python | {
"resource": ""
} |
q22984 | volume_rectangular | train | def volume_rectangular(extents,
count,
transform=None):
"""
Return random samples inside a rectangular volume.
Parameters
----------
extents: (3,) float, side lengths of rectangular solid
count: int, number of points to return
transform: (4,4) float, transformation matrix
Returns
---------
samples: (count, 3) float, points in | python | {
"resource": ""
} |
q22985 | sample_surface_even | train | def sample_surface_even(mesh, count):
"""
Sample the surface of a mesh, returning samples which are
approximately evenly spaced.
Parameters
---------
mesh: Trimesh object
count: number of points to return
Returns
---------
samples: (count,3) points in space on the surface of mesh
face_index: (count,) indices of faces for each sampled point
| python | {
"resource": ""
} |
q22986 | sample_surface_sphere | train | def sample_surface_sphere(count):
"""
Correctly pick random points on the surface of a unit sphere
Uses this method:
http://mathworld.wolfram.com/SpherePointPicking.html
Parameters
----------
count: int, number of points to return
Returns
----------
points: (count,3) float, list of random points on a unit sphere
| python | {
"resource": ""
} |
q22987 | parameters_to_segments | train | def parameters_to_segments(origins, vectors, parameters):
"""
Convert a parametric line segment representation to
a two point line segment representation
Parameters
------------
origins : (n, 3) float
Line origin point
vectors : (n, 3) float
Unit line directions
parameters : (n, 2) float
Start and end distance pairs for each line
| python | {
"resource": ""
} |
q22988 | colinear_pairs | train | def colinear_pairs(segments,
radius=.01,
angle=.01,
length=None):
"""
Find pairs of segments which are colinear.
Parameters
-------------
segments : (n, 2, (2, 3)) float
Two or three dimensional line segments
radius : float
Maximum radius line origins can differ
and be considered colinear
angle : float
Maximum angle in radians segments can
differ and still be considered colinear
length : None or float
If specified, will additionally require
that pairs have a mean vertex distance less
than this value from each other to qualify.
Returns
------------
pairs : (m, 2) int
Indexes of segments which are colinear
"""
from scipy import spatial
# convert segments to parameterized origins
# which are | python | {
"resource": ""
} |
q22989 | unique | train | def unique(segments, digits=5):
"""
Find unique line segments.
Parameters
------------
segments : (n, 2, (2|3)) float
Line segments in space
digits : int
How many digits to consider when merging vertices
Returns
-----------
unique : (m, 2, (2|3)) float
Segments with duplicates merged
"""
segments = np.asanyarray(segments, dtype=np.float64)
# find segments as unique indexes so we can find duplicates
inverse = | python | {
"resource": ""
} |
q22990 | overlap | train | def overlap(origins, vectors, params):
"""
Find the overlap of two parallel line segments.
Parameters
------------
origins : (2, 3) float
Origin points of lines in space
vectors : (2, 3) float
Unit direction vectors of lines
params : (2, 2) float
Two (start, end) distance pairs
Returns
------------
length : float
Overlapping length
overlap : (n, 2, 3) float
Line segments for overlapping distance
"""
# copy inputs and make sure shape is correct
origins = np.array(origins).reshape((2, 3))
vectors = np.array(vectors).reshape((2, 3))
params = np.array(params).reshape((2, 2))
if tol.strict:
# convert input to parameters before flipping
# to make sure we didn't screw it up
truth = parameters_to_segments(origins,
vectors,
params)
# this function only works on parallel lines
dot = np.dot(*vectors)
assert np.isclose(np.abs(dot), 1.0, atol=.01)
# if two vectors are reversed
if dot < 0.0:
| python | {
"resource": ""
} |
q22991 | _load_texture | train | def _load_texture(file_name, resolver):
"""
Load a texture from a file into a PIL image.
"""
file_data = resolver.get(file_name)
image | python | {
"resource": ""
} |
q22992 | _parse_material | train | def _parse_material(effect, resolver):
"""
Turn a COLLADA effect into a trimesh material.
"""
# Compute base color
baseColorFactor = np.ones(4)
baseColorTexture = None
if isinstance(effect.diffuse, collada.material.Map):
try:
baseColorTexture = _load_texture(
effect.diffuse.sampler.surface.image.path, resolver)
except BaseException:
log.warning('unable to load base texture',
exc_info=True)
elif effect.diffuse is not None:
baseColorFactor = effect.diffuse
# Compute emission color
emissiveFactor = np.zeros(3)
emissiveTexture = None
if isinstance(effect.emission, collada.material.Map):
try:
emissiveTexture = _load_texture(
effect.diffuse.sampler.surface.image.path, resolver)
except BaseException:
log.warning('unable to load emissive texture',
exc_info=True)
elif effect.emission is not None:
emissiveFactor = effect.emission[:3]
# Compute roughness
roughnessFactor = 1.0
if (not isinstance(effect.shininess, collada.material.Map)
and effect.shininess is not None):
roughnessFactor = np.sqrt(2.0 / (2.0 + effect.shininess))
# Compute metallic factor
metallicFactor = 0.0
| python | {
"resource": ""
} |
q22993 | _unparse_material | train | def _unparse_material(material):
"""
Turn a trimesh material into a COLLADA material.
"""
# TODO EXPORT TEXTURES
if isinstance(material, visual.texture.PBRMaterial):
diffuse = material.baseColorFactor
if diffuse is not None:
diffuse = list(diffuse)
emission = material.emissiveFactor
if emission is not None:
emission = [float(emission[0]), float(emission[1]),
float(emission[2]), 1.0]
shininess = material.roughnessFactor
if shininess is not None:
shininess = 2.0 / shininess**2 - 2.0
effect = collada.material.Effect(
uuid.uuid4().hex, params=[], shadingtype='phong',
| python | {
"resource": ""
} |
q22994 | load_zae | train | def load_zae(file_obj, resolver=None, **kwargs):
"""
Load a ZAE file, which is just a zipped DAE file.
Parameters
-------------
file_obj : file object
Contains ZAE data
resolver : trimesh.visual.Resolver
Resolver to load additional assets
kwargs : dict
Passed to load_collada
Returns
------------
loaded : dict
Results of loading
"""
# a dict, {file name : file object}
archive = util.decompress(file_obj,
file_type='zip')
# load the first file with a .dae extension
| python | {
"resource": ""
} |
q22995 | load_off | train | def load_off(file_obj, **kwargs):
"""
Load an OFF file into the kwargs for a Trimesh constructor
Parameters
----------
file_obj : file object
Contains an OFF file
Returns
----------
loaded : dict
kwargs for Trimesh constructor
"""
header_string = file_obj.readline()
if hasattr(header_string, 'decode'):
header_string = header_string.decode('utf-8')
header_string = header_string.strip().upper()
if not header_string == 'OFF':
raise NameError('Not an OFF file! Header was ' +
header_string)
header = np.array(
file_obj.readline().strip().split()).astype(np.int64)
vertex_count, face_count = header[:2]
# read the rest of the file
| python | {
"resource": ""
} |
q22996 | load_msgpack | train | def load_msgpack(blob, **kwargs):
"""
Load a dict packed with msgpack into kwargs for
a Trimesh constructor
Parameters
----------
blob : bytes
msgpack packed dict containing
keys 'vertices' and 'faces'
Returns
----------
loaded : dict
Keyword args for Trimesh constructor, aka
| python | {
"resource": ""
} |
q22997 | discretize_bspline | train | def discretize_bspline(control,
knots,
count=None,
scale=1.0):
"""
Given a B-Splines control points and knot vector, return
a sampled version of the curve.
Parameters
----------
control : (o, d) float
Control points of the b- spline
knots : (j,) float
B-spline knots
count : int
Number of line segments to discretize the spline
If not specified will be calculated as something reasonable
Returns
----------
discrete : (count, dimension) float
Points on a polyline version of the B-spline
"""
# evaluate the b-spline using scipy/fitpack
from scipy.interpolate import splev
# (n, d) control points where d is the dimension of vertices
control = np.asanyarray(control, dtype=np.float64)
| python | {
"resource": ""
} |
q22998 | binomial | train | def binomial(n):
"""
Return all binomial coefficients for a given order.
For n > 5, scipy.special.binom is used, below we hardcode
to avoid the scipy.special dependency.
Parameters
--------------
n : int
Order
Returns
---------------
binom : (n + 1,) int
Binomial coefficients of a given order
"""
if n == 1:
return [1, 1]
elif n == 2:
return [1, 2, 1]
elif n == | python | {
"resource": ""
} |
q22999 | Path.process | train | def process(self):
"""
Apply basic cleaning functions to the Path object, in- place.
"""
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.