Debian Codegraph @

src/meshzoo/_moebius.py

raw
1 import numpy as np
2
3
4 def moebius(
5 num_twists: int = 1, # How many twists are there in the 'paper'?
6 nl: int = 60, # Number of nodes along the length of the strip
7 nw: int = 11, # Number of nodes along the width of the strip (>= 2)
8 variant: str = "classical",
9 ):
10 """Creates a simplistic triangular mesh on a slightly Möbius strip. The
11 Möbius strip here deviates slightly from the ordinary geometry in that it
12 is constructed in such a way that the two halves can be exchanged as to
13 allow better comparison with the pseudo-Möbius geometry.
14
15 The variant is either `'classical'` or `'smooth'`. The first is the classical
16 Möbius band parametrization, the latter a smoothed variant matching
17 `'pseudo'`.
18 """
19 # The width of the strip
20 width = 1.0
21 scale = 10.0
22
23 # radius of the strip when flattened out
24 r = 1.0
25
26 # seam displacement
27 alpha0 = 0.0 # pi / 2
28
29 # How flat the strip will be.
30 # Positive values result in left-turning Möbius strips, negative in
31 # right-turning ones.
32 # Also influences the width of the strip.
33 flatness = 1.0
34
35 # Generate suitable ranges for parametrization
36 u_range = np.linspace(0.0, 2 * np.pi, num=nl, endpoint=False)
37 v_range = np.linspace(-0.5 * width, 0.5 * width, num=nw)
38
39 # Create the vertices. This is based on the parameterization
40 # of the Möbius strip as given in
41 # <https://en.wikipedia.org/wiki/M%C3%B6bius_strip#Geometry_and_topology>
42 sin_u = np.sin(u_range)
43 cos_u = np.cos(u_range)
44 alpha = num_twists * 0.5 * u_range + alpha0
45 sin_alpha = np.sin(alpha)
46 cos_alpha = np.cos(alpha)
47
48 if variant == "classical":
49 a = cos_alpha
50 b = sin_alpha
51 reverse_seam = num_twists % 2 == 1
52 elif variant == "smooth":
53 # The fundamental difference with the ordinary Möbius band here are the
54 # squares.
55 # It is also possible to to abs() the respective sines and cosines, but
56 # this results in a non-smooth manifold.
57 a = np.copysign(cos_alpha**2, cos_alpha)
58 b = np.copysign(sin_alpha**2, sin_alpha)
59 reverse_seam = num_twists % 2 == 1
60 else:
61 assert variant == "pseudo"
62 a = cos_alpha**2
63 b = sin_alpha**2
64 reverse_seam = False
65
66 nodes = (
67 scale
68 * np.array(
69 [
70 np.outer(a * cos_u, v_range) + r * cos_u[:, np.newaxis],
71 np.outer(a * sin_u, v_range) + r * sin_u[:, np.newaxis],
72 np.outer(b, v_range) * flatness,
73 ]
74 )
75 .reshape(3, -1)
76 .T
77 )
78
79 cells = _create_elements(nl, nw, reverse_seam)
80 return nodes, cells
81
82
83 def _create_elements(nl, nw, reverse_seam):
84 cells = []
85 for i in range(nl - 1):
86 for j in range(nw - 1):
87 if (i + j) % 2 == 0:
88 cells.append([i * nw + j, (i + 1) * nw + j + 1, i * nw + j + 1])
89 cells.append([i * nw + j, (i + 1) * nw + j, (i + 1) * nw + j + 1])
90 else:
91 cells.append([i * nw + j, i * nw + j + 1, (i + 1) * nw + j])
92 cells.append([i * nw + j + 1, (i + 1) * nw + j, (i + 1) * nw + j + 1])
93
94 # close the geometry
95 i = nl - 1
96 if reverse_seam:
97 # Close the geometry upside down (odd Möbius fold)
98 for j in range(nw - 1):
99 if (i + j) % 2 == 0:
100 cells.append([i * nw + j, (nw - 1) - (j + 1), i * nw + j + 1])
101 cells.append([i * nw + j, (nw - 1) - j, (nw - 1) - (j + 1)])
102 else:
103 cells.append([i * nw + j, i * nw + j + 1, (nw - 1) - j])
104 cells.append([i * nw + j + 1, (nw - 1) - j, (nw - 1) - (j + 1)])
105 else:
106 # Close the geometry upside up (even Möbius fold)
107 for j in range(nw - 1):
108 if (i + j) % 2 == 0:
109 cells.append([i * nw + j, j + 1, i * nw + j + 1])
110 cells.append([i * nw + j, j, j + 1])
111 else:
112 cells.append([i * nw + j, i * nw + j + 1, j])
113 cells.append([i * nw + j + 1, j, j + 1])
114
115 return np.array(cells)