Skip to content

Commit

Permalink
u180324
Browse files Browse the repository at this point in the history
  • Loading branch information
peterropac authored Mar 18, 2024
1 parent aafff17 commit 6e99847
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 4 deletions.
113 changes: 113 additions & 0 deletions Code/examples/scalar/2D/erosion_dilation_image_old_2D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from pathlib import Path
from time import process_time

from spomso.cores.helper_functions import generate_grid, smarter_reshape, hard_binarization
from spomso.cores.geom_2d import PointCloud2D
from spomso.cores.geom import Points

# ----------------------------------------------------------------------------------------------------------------------
# PARAMETERS

# size of the volume
co_size = 4, 4
# resolution of the volume
co_resolution = 400, 400

show = "FIELD" # BINARY, FIELD
show_midplane = True

# ----------------------------------------------------------------------------------------------------------------------
# COORDINATE SYSTEM
coor, co_res_new = generate_grid(co_size, co_resolution)

# ----------------------------------------------------------------------------------------------------------------------
# CREATE SDFs

start_time = process_time()

# import an image from a directory and convert the image to greyscale
image_file_name = "dilation_erosion.png"
spomso_dir = Path(os.getcwd()).resolve().parents[2]
image_path = os.path.join(spomso_dir, "Files", "test_images", image_file_name)
image = Image.open(image_path).convert("LA")

# display the greyscale image
plt.imshow(image, cmap="binary_r")
plt.show()

# create a point cloud object
points = Points([])
# extract the point cloud from the greyscale image (first parameter)
# all the pixels in the image with a brightness value below the binary threshold (third parameter)
# are included in the point cloud
# and the positions of the points are calculated from the specified image size (second parameter)
points.from_image(image, (3, 1.5), binary_threshold=0.5)
cloud = points.cloud

# create an SDF from the point cloud
final = PointCloud2D(cloud)
# give the lines a thickness of 0.01
final.onion(0.01)

# evaluate the SDF of the geometry to create a signed distance field 2D map
final_pattern = final.create(coor)

end_time = process_time()
print("Evaluation Completed in {:.2f} seconds".format(end_time-start_time))

# ----------------------------------------------------------------------------------------------------------------------
# BINARIZATION
# Convert the distance field to a binary voxel map
# where 1 corresponds to the interior and 0 to the exterior of the geometry.

if show_midplane:
field = smarter_reshape(final_pattern, co_resolution)
if show=="BINARY":
pattern_2d = hard_binarization(field, 0)

if show=="BINARY":
pattern = hard_binarization(final_pattern, 0)

# ----------------------------------------------------------------------------------------------------------------------
# PLOT

print("Drawing results...")
# Mid-plane cross-section plot
if show_midplane and show=="BINARY":
fig, ax = plt.subplots(1, 1, figsize=(8.25, 8.25))
ax.imshow(pattern_2d[:, :].T,
cmap="binary_r",
extent=(-co_size[0]/2, co_size[0]/2,
-co_size[1]/2, co_size[1]/2),
origin="lower"
)
ax.grid()

fig.tight_layout()
plt.show()

if show_midplane and show == "FIELD":
fig, ax = plt.subplots(1, 1, figsize=(8.25, 8.25))
print(field.shape)
ax.imshow(field[:, :].T,
cmap="binary_r",
extent=(-co_size[0] / 2, co_size[0] / 2,
-co_size[1] / 2, co_size[1] / 2),
origin="lower"
)
cs = ax.contour(coor[0].reshape(co_res_new[0], co_res_new[1]),
coor[1].reshape(co_res_new[0], co_res_new[1]),
field[:, :],
cmap="plasma_r",
linewidths=2)
ax.clabel(cs, inline=True, fontsize=10)
ax.grid()
fig.tight_layout()
plt.show()


2 changes: 1 addition & 1 deletion Code/examples/scalar/2D/hourglass_2D.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Code/examples/scalar/2D/hourglass_2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
if polygon:
spc.polygon()

# to get the sign of a SDF:
# to get the sign of an SDF:
# spc_sign = spc.sign(direct=True)
# the line can be recovered with:
# spc.boundary()
Expand Down
2 changes: 1 addition & 1 deletion Code/examples/scalar/2D/parametric_curve_2D.ipynb

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions Code/examples/scalar/2D/project_logo_2D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import numpy as np
import matplotlib.pyplot as plt

from time import process_time

from spomso.cores.helper_functions import generate_grid, smarter_reshape
from spomso.cores.post_processing import hard_binarization
from spomso.cores.geom_2d import Circle, SegmentedLine
from spomso.cores.combine import CombineGeometry

# ----------------------------------------------------------------------------------------------------------------------
# PARAMETERS

# size of the volume
co_size = 4.5, 4.5
# resolution of the volume
co_resolution = 400, 400

show = "FIELD" # BINARY, FIELD
show_midplane = True

export = True

# ----------------------------------------------------------------------------------------------------------------------
# COORDINATE SYSTEM
coor, co_res_new = generate_grid(co_size, co_resolution)

# ----------------------------------------------------------------------------------------------------------------------
# CREATE SDFs

start_time = process_time()


c1 = Circle(0.8)
c1.boundary()
c1.mirror((0.9, 0, 0), (-0.9, 0, 0))
c1.move((0, 0.25, 0))

points = [[0, -0.4, 0], [0.3, -0.65, 0], [0, -1.4, 0], [-0.3, -0.65, 0]]
line = SegmentedLine(points, closed=True)

# union = CombineGeometry("UNION")
# final = union.combine(c1, line)

union = CombineGeometry("SMOOTH_UNION2")
final = union.combine_parametric(c1, line, parameters=0.3)

logo_pattern = final.create(coor)

end_time = process_time()
print("Evaluation Completed in {:.2f} seconds".format(end_time-start_time))

# ----------------------------------------------------------------------------------------------------------------------
# BINARIZATION
# distance field to a binary voxel map, where 1 corresponds to the interior and 0 to the exterior of the geometry.

if show_midplane:
field = smarter_reshape(logo_pattern, co_resolution)
if show=="BINARY":
pattern_2d = hard_binarization(field, 0)

if show=="BINARY":
pattern = hard_binarization(logo_pattern, 0)

# ----------------------------------------------------------------------------------------------------------------------
# PLOT

print("Drawing results...")
# Mid-plane cross-section plot
if show_midplane and show=="BINARY":
fig, ax = plt.subplots(1,1, figsize=(8.25, 8.25))
ax.imshow(pattern_2d[:, :].T,
cmap="binary_r",
extent=(-co_size[0]/2, co_size[0]/2,
-co_size[1]/2, co_size[1]/2),
origin="lower"
)
ax.grid()

fig.tight_layout()
plt.show()

if show_midplane and show == "FIELD":
fig, ax = plt.subplots(1, 1, figsize=(8.25, 8.25))
print(field.shape)
# ax.imshow(field[:, :].T,
# cmap="binary_r",
# extent=(-co_size[0] / 2, co_size[0] / 2,
# -co_size[1] / 2, co_size[1] / 2),
# origin="lower",
# vmin=0,
# vmax=0.5
# )
cs = ax.contour(coor[0].reshape(co_res_new[0], co_res_new[1]),
coor[1].reshape(co_res_new[0], co_res_new[1]),
field[:, :],
cmap="binary_r",
levels=np.linspace(-0, 0.5, 9),
linewidths=2)
# ax.clabel(cs, inline=True, fontsize=10)
# ax.grid()
ax.axis('off')
fig.tight_layout()
plt.show()
if export:
fig.savefig(r"F:\ULJ FMF\2022-2023\SPOMSO\Files\Images\project_logo_220224_1.png",
dpi=300,
transparent=True)


2 changes: 1 addition & 1 deletion Code/examples/scalar/2D/surface_reconstruction_2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def reconstruction_function(field_, distance, threshold=0.02):

# create a point cloud object
points = Points(coordinates)
# create a SDF from the point cloud
# create an SDF from the point cloud
point_cloud = PointCloud2D(points.cloud)

# evaluate the SDF to create a signed distance field 2D map
Expand Down

0 comments on commit 6e99847

Please sign in to comment.