Skip to content

Commit 11b48f4

Browse files
committed
3D worley metaprogram to calculate kernel required
1 parent 805df90 commit 11b48f4

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ let package = Package(
1919
path: "sources/noise"),
2020
.testTarget(
2121
name: "NoiseTests",
22-
dependencies: ["Noise"],
22+
dependencies: ["Noise"],
2323
path: "tests/noise"),
24-
]
24+
],
25+
exclude: ["sources/meta"]
2526
)

sources/meta/cell.swift

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
struct Point3D
2+
{
3+
let x:Double,
4+
y:Double,
5+
z:Double
6+
7+
var r2:Double
8+
{
9+
return self.x*self.x + self.y*self.y + self.z*self.z
10+
}
11+
12+
init(_ x:Double, _ y:Double, _ z:Double)
13+
{
14+
self.x = x
15+
self.y = y
16+
self.z = z
17+
}
18+
19+
init(_ x:Int, _ y:Int, _ z:Int)
20+
{
21+
self.x = Double(x)
22+
self.y = Double(y)
23+
self.z = Double(z)
24+
}
25+
26+
static
27+
func + (_ a:Point3D, _ b:Double) -> Point3D
28+
{
29+
return Point3D(a.x + b, a.y + b, a.z + b)
30+
}
31+
32+
static
33+
func + (_ a:Point3D, _ b:Point3D) -> Point3D
34+
{
35+
return Point3D(a.x + b.x, a.y + b.y, a.z + b.z)
36+
}
37+
}
38+
39+
struct Cell3D
40+
{
41+
let root:Point3D
42+
43+
var min:Point3D
44+
{
45+
return self.root
46+
}
47+
48+
var max:Point3D
49+
{
50+
return self.root + 1
51+
}
52+
53+
init(_ point:Point3D)
54+
{
55+
self.root = point
56+
}
57+
58+
func distance_squared(from point:Point3D) -> Double
59+
{
60+
let v:Point3D = Point3D(Swift.max(self.min.x - point.x, 0, point.x - self.max.x),
61+
Swift.max(self.min.y - point.y, 0, point.y - self.max.y),
62+
Swift.max(self.min.z - point.z, 0, point.z - self.max.z))
63+
return v.r2
64+
}
65+
}
66+
67+
enum Colors
68+
{
69+
static
70+
let bold = "\u{001B}[1m",
71+
green = "\u{001B}[0;32m",
72+
green_bold = "\u{001B}[1;32m",
73+
74+
light_green = "\u{001B}[92m",
75+
light_green_bold = "\u{001B}[1;92m",
76+
77+
light_cyan = "\u{001B}[96m",
78+
light_cyan_bold = "\u{001B}[1;96m",
79+
80+
red = "\u{001B}[0;31m",
81+
red_bold = "\u{001B}[1;31m",
82+
83+
pink_bold = "\u{001B}[1m\u{001B}[38;5;204m",
84+
85+
off = "\u{001B}[0m"
86+
}
87+
88+
89+
func kernel3d()
90+
{
91+
let near:Point3D = Point3D(-0.5, 0.5, 0.5)
92+
93+
for k in -3 ..< 3
94+
{
95+
for j in -3 ..< 3
96+
{
97+
for i in -3 ..< 3
98+
{
99+
let cell = Cell3D(Point3D(i, j, k))
100+
101+
var r2:Double = cell.distance_squared(from: near)
102+
for point in [ Point3D(0, near.y, near.z),
103+
Point3D(near.x, 0, near.z),
104+
Point3D(near.x, near.y, 0),
105+
Point3D(0, 0, near.z),
106+
Point3D(near.x, 0, 0),
107+
Point3D(0, near.y, 0),
108+
Point3D(0, 0, 0)]
109+
{
110+
r2 = min(r2, cell.distance_squared(from: point))
111+
}
112+
113+
var output:String = pad(String(describing: r2), to: 5)
114+
115+
if r2 < 3
116+
{
117+
output = Colors.red_bold + output + Colors.off
118+
}
119+
120+
print(output, terminator: " ")
121+
}
122+
print()
123+
}
124+
print()
125+
}
126+
}
127+
128+
func pad(_ str:String, to n:Int) -> String
129+
{
130+
return str + String(repeating: " ", count: max(0, n - str.count))
131+
}
132+
133+
kernel3d()

0 commit comments

Comments
 (0)