Skip to content

Commit

Permalink
some linting changes. also something not important
Browse files Browse the repository at this point in the history
  • Loading branch information
YuXHe15 committed Dec 18, 2023
1 parent f5113af commit 89fbd10
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 91 deletions.
66 changes: 47 additions & 19 deletions amworkflow/src/geometries/composite_geometry.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
from amworkflow.src.geometries.simple_geometry import create_edge, create_wire, create_face, create_prism, random_polygon_constructor, angle_of_two_arrays, laterality_indicator, angular_bisector, p_center_of_mass, linear_interpolate, Pnt, Segments
from amworkflow.src.geometries.operator import reverse, geom_copy, translate, rotate_face, fuser, hollow_carver, cutter3D, bender
from amworkflow.src.geometries.property import topo_explorer, p_bounding_box,shortest_distance_point_line, p_get_face_area
from amworkflow.src.geometries.builder import solid_maker
from OCC.Core.gp import gp_Pnt
import numpy as np
from OCC.Core.TopoDS import TopoDS_Face, TopoDS_Shell, TopoDS_Shape
from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet
from amworkflow.src.geometries.property import get_face_center_of_mass
from amworkflow.src.geometries.builder import sewer
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import itertools
import copy as cp
import itertools

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from matplotlib.patches import Polygon
from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet
from OCC.Core.gp import gp_Pnt
from OCC.Core.TopoDS import TopoDS_Face, TopoDS_Shape, TopoDS_Shell

from amworkflow.src.geometries.builder import sewer, solid_maker
from amworkflow.src.geometries.operator import (
bender,
cutter3D,
fuser,
geom_copy,
hollow_carver,
reverse,
rotate_face,
translate,
)
from amworkflow.src.geometries.property import (
get_face_center_of_mass,
p_bounding_box,
p_get_face_area,
shortest_distance_point_line,
topo_explorer,
)
from amworkflow.src.geometries.simple_geometry import (
Pnt,
Segments,
angle_of_two_arrays,
angular_bisector,
create_edge,
create_face,
create_prism,
create_wire,
laterality_indicator,
linear_interpolate,
p_center_of_mass,
random_polygon_constructor,
)
from amworkflow.src.utils.meter import timer


Expand Down Expand Up @@ -273,7 +301,7 @@ class CreateWallByPointsUpdate():
def __init__(self, coords: list, th: float, height: float, is_close: bool = True):
self.coords = Pnt(coords).coords
self.height = height
self.R = 300
self.R = None
self.interpolate = 8
self.th = th
self.is_close = is_close
Expand All @@ -288,12 +316,12 @@ def __init__(self, coords: list, th: float, height: float, is_close: bool = True
self.rgt_coords = []
self.side_coords: list
self.create_sides()
# self.pnts = Segments(self.side_coords)
# self.G = nx.from_dict_of_lists(self.pnts.pts_digraph, create_using=nx.DiGraph)
self.pnts = Segments(self.side_coords)
self.G = nx.from_dict_of_lists(self.pnts.pts_digraph, create_using=nx.DiGraph)
# self.all_loops = list(nx.simple_cycles(self.H)) # Dangerous! Ran out of memory.
# self.loop_generator = nx.simple_cycles(self.G)
# self.check_pnt_in_wall()
# self.postprocessing()
self.loop_generator = nx.simple_cycles(self.G)
self.check_pnt_in_wall()
self.postprocessing()

def create_sides(self):
if self.R is not None:
Expand Down
217 changes: 145 additions & 72 deletions some_thoughts_20230822_new/try_new_thought/trail_3.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,155 @@
from amworkflow.src.geometries.composite_geometry import CreateWallByPointsUpdate
import numpy as np

from amworkflow.api import amWorkflow as aw
import cv2 as cv

th = 8
l = 10
height = 10
g = aw.geom
hth = th * 0.5
l = 20
display = True
p0 = g.pnt(0, hth, 0)
p1 = g.pnt(l * 0.5, hth)
p2 = g.pnt(l, (np.sqrt(3) * l) * 0.5 + hth)
p3 = g.pnt(2 * l, (np.sqrt(3) * l) * 0.5 + hth)
p4 = g.pnt(5 * l * 0.5, hth)
pu = [p0, p1, p2, p3, p4]
alist = np.array([list(i.Coord()) for i in pu])
put1 = g.p_translate(pu, [3 * l, 0, 0])
# for i in range(len(put1)):
# if i == 0:
# continue
# put1[i][0] -=hth
end_p = np.copy(put1[-1])
end_p[0] += l * 0.5
pm = pu + put1
pm.append(end_p)
# pm_cnt = g.p_center_of_mass(pm)
# pm_cnt[0] -=hth
pmr = g.p_rotate(pm, angle_z=np.pi)
# pmr = g.p_translate(pmr, np.array([-th,0,0]))
cnt2 = g.p_center_of_mass(pmr)
t_len = cnt2[1] * 2
pmrt = g.p_translate(pmr, [0, -t_len, 0])
pm_lt = np.vstack((alist, put1))
pm_lt = np.vstack((pm_lt, np.array(end_p)))
pmf = np.vstack((pm_lt, pmrt))
p5 = g.pnt(0, -(1.5*th + (np.sqrt(3) * l) * 0.5))
p6 = g.pnt(6 * l + th, -(1.5*th + (np.sqrt(3) * l) * 0.5))
p7 = g.pnt(6 * l + th, (1.5*th + (np.sqrt(3) * l) * 0.5))
p8 = g.pnt(0, (1.5*th + (np.sqrt(3) * l) * 0.5))
pout = [p5, p6, p7, p8]
pout_nd = [i.Coord() for i in pout]
pmfo = np.vstack((pmf, pout_nd))

wall = CreateWallByPointsUpdate(pmfo, th, height)
lft_coords = wall.lft_coords
rgt_coords = wall.rgt_coords
pieces = []
for i in range(len(lft_coords)-1):
pieces.append([lft_coords[i], lft_coords[i+1], rgt_coords[i+1], rgt_coords[i]])

def create_canvas(width: float, height: float):
image = np.zeros((height, width), dtype=np.uint8)
return image

def create_poly(pnts: list):
vertices = np.array(pnts, np.int32)
vertices = vertices.reshape((-1, 1, 2))
return vertices

def add_poly(image: np.ndarray, poly: np.ndarray):
cv.fillPoly(image, [poly], 255)

def find_contours(image):
contours, _ = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
from amworkflow.src.geometries.composite_geometry import CreateWallByPointsUpdate

# th = 8
# l = 10
# height = 10
# g = aw.geom
# hth = th * 0.5
# l = 20
# display = True
# p0 = g.pnt(0, hth, 0)
# p1 = g.pnt(l * 0.5, hth)
# p2 = g.pnt(l, (np.sqrt(3) * l) * 0.5 + hth)
# p3 = g.pnt(2 * l, (np.sqrt(3) * l) * 0.5 + hth)
# p4 = g.pnt(5 * l * 0.5, hth)
# pu = [p0, p1, p2, p3, p4]
# alist = np.array([list(i.Coord()) for i in pu])
# put1 = g.p_translate(pu, [3 * l, 0, 0])
# # for i in range(len(put1)):
# # if i == 0:
# # continue
# # put1[i][0] -=hth
# end_p = np.copy(put1[-1])
# end_p[0] += l * 0.5
# pm = pu + put1
# pm.append(end_p)
# # pm_cnt = g.p_center_of_mass(pm)
# # pm_cnt[0] -=hth
# pmr = g.p_rotate(pm, angle_z=np.pi)
# # pmr = g.p_translate(pmr, np.array([-th,0,0]))
# cnt2 = g.p_center_of_mass(pmr)
# t_len = cnt2[1] * 2
# pmrt = g.p_translate(pmr, [0, -t_len, 0])
# pm_lt = np.vstack((alist, put1))
# pm_lt = np.vstack((pm_lt, np.array(end_p)))
# pmf = np.vstack((pm_lt, pmrt))
# p5 = g.pnt(0, -(1.5*th + (np.sqrt(3) * l) * 0.5))
# p6 = g.pnt(6 * l + th, -(1.5*th + (np.sqrt(3) * l) * 0.5))
# p7 = g.pnt(6 * l + th, (1.5*th + (np.sqrt(3) * l) * 0.5))
# p8 = g.pnt(0, (1.5*th + (np.sqrt(3) * l) * 0.5))
# pout = [p5, p6, p7, p8]
# pout_nd = [i.Coord() for i in pout]
# pmfo = np.vstack((pmf, pout_nd))



def honeycomb_infill(
overall_length: float, line_width: float, honeycomb_num: int = 1
) -> np.ndarray:
"""Create honeycomb geometry.
Args:
length: Length of the honeycomb infill.
width: Width of the honeycomb infill.
line_width: Width of the honeycomb lines.
Returns:
points: list of points defining the honeycomb geometry.
"""

def half_honeycomb(origin: np.ndarray, side_length: float) -> np.ndarray:
"""Create half of a honeycomb geometry.
Args:
origin: Origin of the half honeycomb.
length: Length of the honeycomb infill.
width: Width of the honeycomb infill.
line_width: Width of the honeycomb lines.
Returns:
points: list of points defining half of the honeycomb geometry.
"""
points = np.zeros((5, 2))
points[0] = origin
points[1] = origin + np.array([0.5 * side_length, np.sqrt(3) * side_length / 2])
points[2] = points[1] + np.array([side_length, 0])
points[3] = points[2] + np.array([0.5 * side_length, -np.sqrt(3) * side_length / 2])
points[4] = points[3] + np.array([0.5 * side_length, 0])
return points

length = (overall_length - (3 * line_width)) / (3 * honeycomb_num)
start_point = np.array([0, line_width * 0.5])
offset = np.array([1.5 * line_width, 0])
overall_width = 3 * line_width + np.sqrt(3) * length
point_num = 16 + (honeycomb_num - 1) * 10
half_points = np.zeros((int(point_num / 2) - 2, 2))
half_points[0] = start_point
for i in range(honeycomb_num):
if i == 0:
start = start_point + offset
honeycomb_unit = half_honeycomb(start, length)
else:
start = start_point + offset + np.array([3 * length * i, 0])
honeycomb_unit = half_honeycomb(start, length)
half_points[i * 5 + 1 : i * 5 + 6] = honeycomb_unit
another_half = half_points.copy()
another_half[:, 1] = -another_half[:, 1]
another_half = np.flipud(another_half)
points = np.concatenate((half_points, another_half), axis=0)
outer_points = np.array(
[
[0, -overall_width * 0.5],
[overall_length, -overall_width * 0.5],
[overall_length, overall_width * 0.5],
[0, overall_width * 0.5],
]
)
points = np.concatenate((points, outer_points), axis=0)
return points


ppt = honeycomb_infill(150, 8, 1)


wall = CreateWallByPointsUpdate(ppt, 8, 2)
wall.visualize(all_polygons=False, display_central_path=True)
# lft_coords = wall.lft_coords
# rgt_coords = wall.rgt_coords
# pieces = []
# for i in range(len(lft_coords)-1):
# pieces.append([lft_coords[i], lft_coords[i+1], rgt_coords[i+1], rgt_coords[i]])

# def create_canvas(width: float, height: float):
# image = np.zeros((height, width), dtype=np.uint8)
# return image

# def create_poly(pnts: list):
# vertices = np.array(pnts, np.int32)
# vertices = vertices.reshape((-1, 1, 2))
# return vertices

# def add_poly(image: np.ndarray, poly: np.ndarray):
# cv.fillPoly(image, [poly], 255)

# def find_contours(image):
# contours, _ = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

return contours
# return contours

# poly = wall.Shape()
# wall.visualize(all_polygons=False, display_central_path=False)
# aw.tool.write_stl(poly, "sucess_new_scheme",store_dir="/home/yhe/Documents/new_am2/amworkflow/some_thoughts_20230822_new/try_new_thought")
image = create_canvas(150, 150)
for p in pieces:
poly = create_poly(p)
add_poly(image, poly)
contours = np.array(find_contours(image))[0].reshape(-1, 2)
print(contours)
# image = create_canvas(150, 150)
# for p in pieces:
# poly = create_poly(p)
# add_poly(image, poly)
# contours = np.array(find_contours(image))[0].reshape(-1, 2)
# print(contours)
# contour_image = cv.cvtColor(image, cv.COLOR_GRAY2BGR)
# cv.drawContours(contour_image, contours, -1, (0, 255, 0), 2)
# cv.imshow("Contours", contour_image)
Expand Down

0 comments on commit 89fbd10

Please sign in to comment.