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_triangle():
points, cells = meshzoo.triangle(4)
assert len(points) == 15
assert _near_equal(numpy.sum(points, axis=0), [0.0, 0.0])
assert len(cells) == 16
import numpy
import meshzoo
from helpers import _near_equal
def test_triangle():
points, cells = meshzoo.triangle(4)
assert len(points) == 15
assert _near_equal(numpy.sum(points, axis=0), [0.0, 0.0])
assert len(cells) == 16
if __name__ == "__main__":
points, cells = meshzoo.triangle(5000)
# import meshio
plot_flat_gamut(
plot_planckian_locus=False,
xy_to_2d=xy_to_2d,
axes_labels=axes_labels,
plot_rgb_triangle=plot_rgb_triangle,
fill_horseshoe=mesh_resolution is None,
)
if mesh_resolution is not None:
# dir_path = os.path.dirname(os.path.realpath(__file__))
# with open(os.path.join(dir_path, 'data/gamut_triangulation.yaml')) as f:
# data = yaml.safe_load(f)
# points = numpy.array(data['points'])
# cells = numpy.array(data['cells'])
bary, cells = meshzoo.triangle(mesh_resolution)
corners = numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]).T
points = numpy.dot(corners, bary).T
edges, _ = meshzoo.create_edges(cells)
pts = xy_to_2d(points.T).T
lines = pts[edges].T
plt.plot(*lines, color="0.8", zorder=0)
_plot_ellipses(centers, offsets, xy_to_2d, ellipse_scaling, facecolor=ellipse_color)
def _plot_rgb_triangle(xy_to_2d, bright=True):
# plot sRGB triangle
# discretization points
n = 50
# Get all RGB values that sum up to 1.
rgb_linear, _ = meshzoo.triangle(n)
if bright:
# For the x-y-diagram, it doesn't matter if the values are scaled in any way.
# After all, the tranlation to XYZ is linear, and then to xyY it's (X/(X+Y+Z),
# Y/(X+Y+Z), Y), so the factor will only be present in the last component which
# is discarded. To make the plot a bit brighter, scale the colors up as much as
# possible.
rgb_linear /= numpy.max(rgb_linear, axis=0)
srgb_linear = SrgbLinear()
xyz = srgb_linear.to_xyz100(rgb_linear)
xyy_vals = xy_to_2d(_xyy_from_xyz100(xyz)[:2])
# Unfortunately, one cannot use tripcolors with explicit RGB specification
# (see ). As a
# workaround, associate range(n) data with the points and create a colormap
# that associates the integer values with the respective RGBs.
# # plot statistics
# axes0 = problem.get_ellipse_axes(alpha0).T.flatten()
# plt.plot(axes0, label='axes lengths before')
# axes1 = problem.get_ellipse_axes(out.x).T.flatten()
# plt.plot(axes1, label='axes lengths opt')
# plt.legend()
# plt.grid()
# Plot unperturbed MacAdam
# colorio.plot_luo_rigg(
# ellipse_scaling=1,
colorio.save_macadam(
"macadam-native.png", ellipse_scaling=10, plot_rgb_triangle=False, n=n
)
points, cells = meshzoo.triangle(
corners=numpy.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]), n=n
)
# https://bitbucket.org/fenics-project/dolfin/issues/845/initialize-mesh-from-vertices
editor = MeshEditor()
mesh = Mesh()
editor.open(mesh, "triangle", 2, 2)
editor.init_vertices(points.shape[0])
editor.init_cells(cells.shape[0])
for k, point in enumerate(points):
editor.add_vertex(k, point[:2])
for k, cell in enumerate(cells):
editor.add_cell(k, cell)
editor.close()
V = FunctionSpace(mesh, "CG", 1)
def _plot_srgb_gamut(self, k0, level, bright=False):
import meshzoo
# Get all RGB values that sum up to 1.
bary, triangles = meshzoo.triangle(n=50)
corners = numpy.array([[0, 0], [1, 0], [0, 1]]).T
srgb_vals = numpy.dot(corners, bary).T
srgb_vals = numpy.column_stack([srgb_vals, 1.0 - numpy.sum(srgb_vals, axis=1)])
# matplotlib is sensitive when it comes to srgb values, so take good care here
assert numpy.all(srgb_vals > -1.0e-10)
srgb_vals[srgb_vals < 0.0] = 0.0
# Use bisection to
srgb_linear = SrgbLinear()
tol = 1.0e-5
# Use zeros() instead of empty() here to avoid invalid values when setting up
# the cmap below.
self_vals = numpy.zeros((srgb_vals.shape[0], 3))
srgb_linear_vals = numpy.zeros((srgb_vals.shape[0], 3))
mask = numpy.ones(srgb_vals.shape[0], dtype=bool)
self.target = 0.002
self.J /= self.target
# dir_path = os.path.dirname(os.path.realpath(__file__))
# with open(os.path.join(dir_path, '../colorio/data/gamut_triangulation.yaml')) as f:
# data = yaml.safe_load(f)
# self.points = numpy.column_stack([
# data['points'], numpy.zeros(len(data['points']))
# ])
# self.cells = numpy.array(data['cells'])
# self.points, self.cells = colorio.xy_gamut_mesh(0.15)
self.points, self.cells = meshzoo.triangle(
n, corners=numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
)
# https://bitbucket.org/fenics-project/dolfin/issues/845/initialize-mesh-from-vertices
editor = MeshEditor()
mesh = Mesh()
editor.open(mesh, "triangle", 2, 2)
editor.init_vertices(self.points.shape[0])
editor.init_cells(self.cells.shape[0])
for k, point in enumerate(self.points):
editor.add_vertex(k, point)
for k, cell in enumerate(self.cells):
editor.add_cell(k, cell)
editor.close()
self.V = FunctionSpace(mesh, "CG", 1)