Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_centroid_points(points, centroid):
if centroid is None:
x, y, z = 0.0, 0.0, 0.0
else:
x, y, z = centroid
assert centroid_points(points) == [pytest.approx(x, 0.001), pytest.approx(y, 0.001), pytest.approx(z, 0.001)]
vkey : hashable
The identifier of the vertex.
Returns
-------
list
The components of the normal vector.
"""
vectors = []
for hfkey in self.vertex_halffaces(vkey):
if self.is_halfface_on_boundary(hfkey):
vectors.append(self.halfface_normal(hfkey))
return normalize_vector(centroid_points(vectors))
def center(self):
xyz = [self.mesh.vertex_coordinates(key) for key in self.mesh.vertices()]
cx, cy, cz = centroid_points(xyz)
for key, attr in self.mesh.vertices(True):
attr['x'] -= cx
attr['y'] -= cy
def center(self):
xyz = [self.network.vertex_coordinates(key) for key in self.network.vertices()]
cx, cy, cz = centroid_points(xyz)
for key, attr in self.network.vertices(True):
attr['x'] -= cx
attr['y'] -= cy
def draw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1],
centroid=True, layer=None, **kwargs):
edges = [] if not edges else edges
faces = [] if not faces else faces
mp = centroid_points(vertices) if centroid else [0, 0, 0]
vertices = [subtract_vectors(vertex, mp) for vertex in vertices]
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(vertices, edges, faces)
mesh.update(calc_edges=True)
obj = bpy.data.objects.new(name, mesh)
obj.show_wire = True
obj.location = mp
set_object_color(obj, color)
link_objects([obj], layer=layer)
return obj
subd.add_face([a, key, d, c])
del subd.face[fkey]
# update coordinates
# ======================================================================
# these are the coordinates before updating
key_xyz = {key: subd.vertex_coordinates(key) for key in subd.vertex}
# move each edge point to the average of the neighboring centroids and
# the original end points
for w in edgepoints:
x, y, z = centroid_points([key_xyz[nbr] for nbr in subd.halfedge[w]])
subd.vertex[w]['x'] = x
subd.vertex[w]['y'] = y
subd.vertex[w]['z'] = z
# move each vertex to the weighted average of itself, the neighboring
# centroids and the neighboring mipoints
for key in mesh.vertices():
if key in fixed:
continue
if key in bkeys:
nbrs = set(bkey_edgepoints[key])
nbrs = [key_xyz[nbr] for nbr in nbrs]
e = 0.5
for key in mesh:
if key in fixed:
continue
ep = key_xyz[key]
points = []
for nbr in mesh.vertex_neighbours(key):
sp = key_xyz[nbr]
vec = [ep[i] - sp[i] for i in range(3)]
lvec = sum(vec[i] ** 2 for i in range(3)) ** 0.5
uvec = [vec[i] / lvec for i in range(3)]
lvec = min(lvec, lmax)
lvec = max(lvec, lmin)
points.append([sp[i] + lvec * uvec[i] for i in range(3)])
c = centroid_points(points)
# update
attr = mesh.vertex[key]
attr['x'] += d * (c[0] - ep[0])
attr['y'] += d * (c[1] - ep[1])
attr['z'] += d * (c[2] - ep[2])
if callback:
callback(mesh, k, callback_args)
nbrs = [key_xyz[nbr] for nbr in nbrs]
e = 0.5
v = 0.5
E = [coord * e for coord in centroid_points(nbrs)]
V = [coord * v for coord in key_xyz[key]]
x, y, z = [E[_] + V[_] for _ in range(3)]
else:
fnbrs = [mesh.face_centroid(fkey) for fkey in mesh.vertex_faces(key) if fkey is not None]
nbrs = [key_xyz[nbr] for nbr in subd.halfedge[key]]
n = float(len(nbrs))
f = 1.0 / n
e = 2.0 / n
v = (n - 3.0) / n
F = centroid_points(fnbrs)
E = centroid_points(nbrs)
V = key_xyz[key]
x = f * F[0] + e * E[0] + v * V[0]
y = f * F[1] + e * E[1] + v * V[1]
z = f * F[2] + e * E[2] + v * V[2]
subd.vertex[key]['x'] = x
subd.vertex[key]['y'] = y
subd.vertex[key]['z'] = z
mesh = subd
return mesh
raise Exception('Callback is not callable.')
fixed = fixed or []
fixed = set(fixed)
for k in range(kmax):
key_xyz = {key: mesh.vertex_coordinates(key) for key in mesh.vertices()}
for key in mesh.vertices():
if key in fixed:
continue
p = key_xyz[key]
nbrs = mesh.vertex_neighbours(key)
c = centroid_points([key_xyz[nbr] for nbr in nbrs])
# update
attr = mesh.vertex[key]
attr['x'] += d * (c[0] - p[0])
attr['y'] += d * (c[1] - p[1])
attr['z'] += d * (c[2] - p[2])
if callback:
callback(mesh, k, callback_args)
positions = [[] for _ in range(len(vertices))]
for face in iter(faces):
points = [vertices[index] for index in face]
plane = bestfit_plane(points)
projections = project_points_plane(points, plane)
for i, index in enumerate(face):
positions[index].append(projections[i])
for index, vertex in enumerate(vertices):
if index in fixed:
continue
x, y, z = centroid_points(positions[index])
vertex[0] = x
vertex[1] = y
vertex[2] = z
if callback:
callback(k, callback_args)