Skip to content

Commit 6e634cd

Browse files
authored
Merge pull request #384 from YenPeiChen07/feature/fit-naca4-airfoil
Use cubic Bézier curve to sample NACA 4-digit airfoil
2 parents 8d0cc2f + 2508555 commit 6e634cd

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

modmesh/gui/naca.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,50 @@ def calc_naca4_points(number, npoint, open_trailing_edge=False,
9494
return ret
9595

9696

97-
def draw_naca4(world, number, npoint, fac, off_x, off_y, **kw):
97+
def calc_sample_points(points):
98+
# determine the number of sample points for a cubic bezier curve
99+
spacing = 0.01
100+
segments = np.hypot((points[:-1, 0] - points[1:, 0]),
101+
(points[:-1, 1] - points[1:, 1])) // spacing
102+
nsample = np.where(segments > 2, segments - 1, 2).astype(int)
103+
return nsample
104+
105+
106+
def linear_approximate(world, points):
107+
for it in range(points.shape[0] - 1):
108+
world.add_edge(points[it, 0], points[it, 1], 0,
109+
points[it + 1, 0], points[it + 1, 1], 0)
110+
return
111+
112+
113+
def cubic_bezier_approximate(world, points, nsample=None):
114+
Vector = core.Vector3dFp64
115+
116+
if nsample is None:
117+
nsample = calc_sample_points(points)
118+
for it in range(points.shape[0] - 1):
119+
p1 = points[it] + (1 / 3) * (points[it + 1] - points[it])
120+
p2 = points[it] + (2 / 3) * (points[it + 1] - points[it])
121+
122+
b = world.add_bezier([Vector(points[it, 0], points[it, 1], 0),
123+
Vector(p1[0], p1[1], 0),
124+
Vector(p2[0], p2[1], 0),
125+
Vector(points[it + 1, 0], points[it + 1, 1], 0)])
126+
b.sample(nsample[it])
127+
return
128+
129+
130+
def draw_naca4(world, number, npoint, fac, off_x, off_y,
131+
use_bezier=False, **kw):
98132
crds = calc_naca4_points(number=number, npoint=npoint, **kw)
99133
crds *= fac # scaling factor
100134
crds[:, 0] += off_x # offset in x
101135
crds[:, 1] += off_y # offset in y
102-
for it in range(crds.shape[0] - 1):
103-
e = world.add_edge(crds[it, 0], crds[it, 1], 0,
104-
crds[it + 1, 0], crds[it + 1, 1], 0)
105-
print(f"{it}: {e}")
106-
print("nedge:", world.nedge)
136+
137+
if use_bezier:
138+
cubic_bezier_approximate(world, crds)
139+
else:
140+
linear_approximate(world, crds)
107141
return crds
108142

109143

@@ -113,7 +147,7 @@ def runmain():
113147
"""
114148
w = core.WorldFp64()
115149
draw_naca4(w, number='0012', npoint=101, fac=5.0, off_x=0.0, off_y=2.0,
116-
open_trailing_edge=False, cosine_spacing=True)
150+
open_trailing_edge=False, use_bezier=True, cosine_spacing=True)
117151
wid = view.mgr.add3DWidget()
118152
wid.updateWorld(w)
119153
wid.showMark()

0 commit comments

Comments
 (0)