-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquad_diff.txt
More file actions
150 lines (147 loc) · 4.57 KB
/
quad_diff.txt
File metadata and controls
150 lines (147 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
diff --git a/app/components/quad-antenna-scene.tsx b/app/components/quad-antenna-scene.tsx
index f3f51dc..7a94299 100644
--- a/app/components/quad-antenna-scene.tsx
+++ b/app/components/quad-antenna-scene.tsx
@@ -1,9 +1,9 @@
import { Camera } from "@phosphor-icons/react";
import { ArcballControls } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
-import { useId, useMemo, useRef, useState } from "react";
+import { useId, useMemo, useRef, useState, useEffect } from "react";
import { Trans, useTranslation } from "react-i18next";
import {
BufferGeometry,
Line,
LineBasicMaterial,
@@ -13,10 +13,14 @@ import {
import { Button } from "~/components/ui/button";
import { Label } from "~/components/ui/label";
import { RadioGroup, RadioGroupItem } from "~/components/ui/radio-group";
import { Switch } from "~/components/ui/switch";
import { ElectricFieldWasm } from "./electric-field-wasm";
+import {
+ initAntennaWasm,
+ calculateAntennaGainBatch,
+} from "~/utils/antenna-physics-wasm";
interface QuadElementProps {
position: [number, number, number];
color?: string;
rotation?: [number, number, number];
@@ -143,46 +147,89 @@ function QuadAntenna({
function RadiationPattern({
_polarization,
}: {
_polarization: "horizontal" | "vertical";
}) {
- const geometry = useMemo(() => {
- const geo = new SphereGeometry(1, 60, 40);
- const posAttribute = geo.attributes.position;
- const vertex = new Vector3();
- const scale = 8;
-
- for (let i = 0; i < posAttribute.count; i++) {
- vertex.fromBufferAttribute(posAttribute, i);
- vertex.normalize();
-
- // Quad pattern: bidirectional along X-axis (boom is X)?
- // Wait, Yagi fires along X. Quad is YZ plane. Fires along X.
- // Gain is highest along X.
- // But polarization affects the E-field plane.
- // Code was using Math.abs(vertex.z) for gain? That would beam along Z.
- // If boom is X, we want beam along X.
- // Now elements are in YZ plane, beam should be X. Correct.
-
- // Directional cardioid pattern for 2-element Quad (Front-to-back ratio)
- // Normalized cardioid: (1 + cos(theta)) / 2
- // vertex is normalized, so vertex.x is cos(theta) where theta is angle from X-axis
- const gain = ((1 + vertex.x) / 2) ** 2; // Beaming along +X axis
-
- vertex.multiplyScalar(gain * scale);
- posAttribute.setXYZ(i, vertex.x, vertex.y, vertex.z);
- }
- geo.computeVertexNormals();
- return geo;
- }, []); // Pattern geometry is static for now, or could depend on polarization for minor shape changes?
+ const [geometry, setGeometry] = useState<BufferGeometry | null>(null);
+
+ useEffect(() => {
+ let isMounted = true;
+
+ const generateGeometry = async () => {
+ await initAntennaWasm();
+ if (!isMounted) return;
+
+ const geo = new SphereGeometry(1, 60, 40);
+ const posAttribute = geo.attributes.position;
+ const vertex = new Vector3();
+ const scale = 8;
+
+ const thetas: number[] = [];
+ const phis: number[] = [];
+
+ for (let i = 0; i < posAttribute.count; i++) {
+ vertex.fromBufferAttribute(posAttribute, i);
+ vertex.normalize();
+ phis.push(Math.atan2(vertex.z, vertex.x));
+ thetas.push(Math.asin(vertex.y));
+ }
+
+ let wasmGains: number[] = [];
+ try {
+ wasmGains = await calculateAntennaGainBatch(
+ "quad",
+ thetas,
+ phis,
+ 0.5,
+ 1,
+ false,
+ "60",
+ undefined,
+ );
+ } catch (error) {
+ console.warn("WASM batch calculation failed, using fallback", error);
+ }
+
+ for (let i = 0; i < posAttribute.count; i++) {
+ vertex.fromBufferAttribute(posAttribute, i);
+ vertex.normalize();
+
+ let gain = 0;
+ if (wasmGains.length > 0) {
+ gain = wasmGains[i];
+ } else {
+ // Fallback
+ gain = ((1 + vertex.x) / 2) ** 2;
+ }
+
+ vertex.multiplyScalar(gain * scale);
+ posAttribute.setXYZ(i, vertex.x, vertex.y, vertex.z);
+ }
+ geo.computeVertexNormals();
+
+ if (isMounted) {
+ setGeometry(geo);
+ }
+ };
+
+ generateGeometry();
+
+ return () => {
+ isMounted = false;
+ };
+ }, []);
useMemo(() => {
return () => {
- geometry.dispose();
+ if (geometry) {
+ geometry.dispose();
+ }
};
}, [geometry]);
+ if (!geometry) return null;
+
return (
<group position={[0, 2, 0]}>
<mesh geometry={geometry}>
<meshBasicMaterial
color="#22c55e"