diff --git a/content/lessons/networking/_order.yaml b/content/lessons/networking/_order.yaml index 580efc7c..52a19818 100644 --- a/content/lessons/networking/_order.yaml +++ b/content/lessons/networking/_order.yaml @@ -1,6 +1,7 @@ - routers - wifi - bluetooth +- cell-towers - latency-and-bandwidth - packet-switching - tcp diff --git a/content/lessons/networking/cell-towers/ar.md b/content/lessons/networking/cell-towers/ar.md new file mode 100644 index 00000000..edf859fb --- /dev/null +++ b/content/lessons/networking/cell-towers/ar.md @@ -0,0 +1,72 @@ +--- +title: 'أبراج الاتصالات: كيف ينقلك هاتفك من برج إلى آخر' +summary: >- + يسمع هاتفك عدة أبراج في وقت واحد، وحين تتحرك ينقل مكالمتك من برج إلى الذي + يليه، لكنه ينتظر حتى يصبح البرج الجديد أقوى بوضوح، فلا يتأرجح بين البرجين. +parameters: + d: d — بُعدك عن البرج A (البرج B على بُعد 6 km منه) + n: n — سرعة ضعف الإشارة (2 في الريف المفتوح، 4 في وسط المدينة) + H: H — هامش التسليم +variables: + - الإشارة التي يستقبلها هاتفك من برج، بوحدة dBm (من البرج A أو من البرج B) + - الإشارة على بُعد 1 km من البرج، وهي هنا −75 dBm + - سرعة ضعف الإشارة مع المسافة، 2 في الريف المفتوح وحتى 4 في وسط مدينة مزدحمة + - بُعدك عن البرج بالكيلومتر (عن البرج A أو عن البرج B) + - هامش التسليم، أي بكم يجب أن يزيد البرج B قوةً قبل أن ينتقل إليه الهاتف، بوحدة dB +--- + +## Try it + +1. اسحب d من 0.2 نحو 6، فيمشي هاتفك في الطريق من البرج A إلى البرج B. ينزل المنحنى الأزرق ويصعد البرتقالي. +2. قف عند 3 km في المنتصف. البرجان متساويان في القوة، ومع ذلك يبقى الهاتف على البرج A. تابع المشي حتى يعبر خط التسليم المتقطع. +3. ارفع H فيبتعد خط التسليم عن المنتصف. وارفع n (مدينة أكثر ازدحامًا) فينزل المنحنيان أسرع، وتهبط إشارتك أكثر قبل الانتقال. + +## Real-life examples + +- **في الطريق إلى المدرسة.** على بُعد 1.5 km من البرج A في حي سكني، الإشارة نحو −80 dBm بأربعة أعمدة كاملة، والبرج B على بُعد 4.5 km لا يقترب منه. +- **في السيارة عبر الريف.** الأرض المفتوحة تُضعف الإشارة ببطء (n = 2.4). بعد المنتصف بقليل صار البرج B أقوى بـ 3.5 dB، أي أكثر من هامش 3 dB، فانتقلت المكالمة إليه دون أن تشعر. +- **في وسط المدينة، عالقًا على البرج القديم.** مدينة مزدحمة (n = 3.8) وهامش واسع قدره 10 dB. عند 3.8 km صار البرج B أقوى بـ 9 dB لا بـ 10، فيبقى الهاتف على البرج A عند نحو −97 dBm بعمودين فقط. + +## Test yourself + +1. امشِ مسافة تكفي لينقلك هاتفك إلى البرج B. +2. قف عند 3.5 km وابقَ على البرج A، مع أن البرج B صار أقوى هناك. +3. في مدينة مزدحمة (n يساوي 3.5 أو أكثر)، اجعل إشارتك تهبط دون −95 dBm قبل أن يترك الهاتف البرج A. + +## Intuition + +برج الاتصالات جهاز راديو ينادي في كل الاتجاهات. كلما ابتعدت عنه خفت صوته، والمباني تمتص منه أكثر. يسمع هاتفك كل الأبراج القريبة، ويقيس قوة كل واحد منها بوحدة dBm. في الرسم، المنحنى الأزرق هو البرج A والبرتقالي هو البرج B. اسحب d وراقب هاتفك يتحرك في الطريق بينهما. + +في منتصف الطريق يتساوى البرجان. قد تتوقع أن يقفز الهاتف إلى البرج B هناك، لكنه ينتظر حتى يصبح B أقوى بوضوح، بمقدار H ديسيبل، وهذا هو هامش التسليم. لولا هذا الهامش لتأرجح هاتف واقف قرب المنتصف بين البرجين كل ثانية مع كل اهتزاز في الإشارة. لذلك يحدث الانتقال بعد المنتصف بقليل، عند الخط المتقطع. والخط العريض هو الإشارة التي يستعملها هاتفك فعلًا، فهي تهبط وأنت تبتعد عن A، ثم تقفز إلى أعلى حين تنتقل المكالمة إلى B. + +## Formal + +تضعف الإشارة مع المسافة وفق نموذج المسافة اللوغاريتمي، $P = P_1 - 10\,n\,\log_{10} d$ بوحدة dBm. هنا $P_1$ هي الإشارة على بُعد 1 km من البرج (−75 dBm في هذا الدرس)، و$d$ بالكيلومتر. الأس $n$ يساوي 2 في الهواء المفتوح، ويرتفع إلى 3 أو 4 في المدن، لأن المباني تحجب الموجات وتبعثرها. وكلما صارت المسافة عشرة أضعاف خسرت $10n$ ديسيبل. + +اطرح إشارة برج من إشارة الآخر فيختفي الحدّ $P_1$، ويبقى $P_B - P_A = 10\,n\,\log_{10} (d_A / d_B)$. ينتقل الهاتف حين يتجاوز هذا الفرق الهامش $H$. وإذا مشيت من A إلى B على طريق طوله $D$ (هنا 6 km)، يحدث الانتقال عند $d_A = D\,r/(1+r)$، حيث $r = 10^{H/(10n)}$. هذه النقطة بعد المنتصف بقليل، وتبتعد عنه أكثر كلما كبر $H$ أو صغر $n$. + +## Advanced + +في شبكات LTE و5G يقيس هاتفك إشارة مرجعية من كل خلية قريبة (RSRP، وهي قراءة dBm التي تتابعها هنا)، ويرسل القياسات إلى الشبكة، والشبكة هي التي تقرر. القاعدة السابقة هي «الحدث A3» المعياري، أي أن تصبح خلية مجاورة أفضل من الخلية الحالية بهامش محدد، وأن يدوم ذلك مدة تسمى time-to-trigger، عادة بين 40 و640 ms. وهذا الشرط الزمني حارس ثانٍ ضد التأرجح، فلحظة قصيرة بين مبنيين لا تكفي للانتقال. + +يحمل البرج عادة ثلاث لوحات، تغطي كل واحدة قطاعًا من 120°، فالبرج الواحد في الحقيقة ثلاث خلايا، وقد تنتقل بين قطاعاته وأنت تدور حوله. والهاتف الخامل يقوم بنسخة أخف تسمى إعادة اختيار الخلية (cell reselection) دون أن يسأل الشبكة، أما أثناء مكالمة أو جلسة بيانات فالشبكة هي التي توجّه الانتقال. ثم إن الخلية لا تحدّها الإشارة وحدها، فكل من فيها يتقاسم سعتها، ولهذا تضيف المدن خلايا أكثر وأصغر بدل أبراج أعلى صوتًا. + +## Derivation + +1. اكتب الصيغة نفسها لكل برج بمسافتك إليه، $d_A$ إلى البرج A و$d_B$ إلى البرج B. +2. اطرح إحداهما من الأخرى. يختفي $P_1$، ويصير الفرق بين لوغاريتمين لوغاريتمَ نسبة، فلا يهم إلا النسبة بين المسافتين. +3. لا ينتقل الهاتف لحظة يصبح B أقوى. ينتظر حتى يتجاوز الفرق الهامش $H$، وبهذا لا يتأرجح بين البرجين قرب المنتصف. + +## Real world + +### مكالمات تصمد على الطريق السريع +بسرعة 100 km/h تعبر كل خلية في المدينة في نحو دقيقة. وينقل التسليم مكالمتك من برج إلى آخر في بضع عشرات من أجزاء الألف من الثانية، فلا تسمع الانتقال أبدًا. + +### أعمدة تختفي في المصعد +المصعد المعدني أو القبو يضيف عشرات الديسيبلات من الخسارة فوق خسارة المسافة. فإذا لم يسمع الهاتف برجًا أقوى من نحو −115 dBm كتب «لا توجد خدمة». + +### خلايا صغيرة في المدن +في وسط المدينة تضعف الإشارة بسرعة، ويتقاسم آلاف الناس كل خلية. لذلك يضع المشغّلون خلايا صغيرة على أعمدة الإنارة وأسطح المباني، بين الواحدة والأخرى بضع مئات من الأمتار. + +### الملاعب والمهرجانات +حين يجتمع خمسون ألف شخص في ملعب واحد، يجلب المشغّلون أبراجًا على شاحنات توقف بجانبه طوال اليوم، فيخدم كل برج عددًا أقل من الهواتف. diff --git a/content/lessons/networking/cell-towers/en.md b/content/lessons/networking/cell-towers/en.md new file mode 100644 index 00000000..7e7375e0 --- /dev/null +++ b/content/lessons/networking/cell-towers/en.md @@ -0,0 +1,73 @@ +--- +title: 'Cell towers: how your phone hands you from one to the next' +summary: >- + Your phone hears several cell towers at once, and as you move it hands your call + from one to the next, waiting until the new one is clearly better so it never + flips back and forth. +parameters: + d: d — how far you are from tower A (tower B is 6 km away) + n: n — how fast the signal fades (2 open country, 4 dense city) + H: H — the handover margin +variables: + - the signal your phone receives from a tower, in dBm (from tower A or from tower B) + - the signal 1 km from a tower, here −75 dBm + - 'how fast the signal fades with distance: 2 in open country, up to 4 in a dense city' + - your distance from a tower, in km (to tower A or to tower B) + - 'the handover margin: how much stronger tower B must be before the phone switches, in dB' +--- + +## Try it + +1. Drag d from 0.2 towards 6: your phone walks down the road from tower A to tower B. The blue curve falls and the orange one rises. +2. Stop at 3 km, the middle. Both towers are equally strong, yet the phone stays on tower A. Keep walking until it crosses the dashed handover line. +3. Raise H and the handover line moves further past the middle. Raise n (a denser city) and both curves sink faster, so your bars drop lower before the switch. + +## Real-life examples + +- **Walking to school in the suburbs.** 1.5 km from tower A in a suburb, the signal is about −80 dBm, four full bars, and tower B, 4.5 km away, is nowhere close. +- **Driving through the countryside.** Open land fades the signal slowly (n = 2.4). Just past the middle, tower B is already 3.5 dB stronger, more than the 3 dB margin, so the call has moved to it without you noticing. +- **Downtown, stuck on the old tower.** A dense city (n = 3.8) and a wide 10 dB margin. At 3.8 km tower B is 9 dB stronger, but not 10, so the phone stays on tower A at about −97 dBm, just two bars. + +## Test yourself + +1. Walk far enough that your phone hands you over to tower B. +2. Stand at 3.5 km and stay on tower A, even though tower B is already stronger there. +3. In a dense city (n of 3.5 or more), make your signal sink below −95 dBm before the phone lets go of tower A. + +## Intuition + +A cell tower is a radio that calls out in every direction. The further you walk, the fainter it sounds, and buildings soak up even more of it. Your phone hears every tower nearby and keeps score of each one in dBm. In the picture, the blue curve is tower A and the orange curve is tower B. Drag d and watch your phone move along the road between them. + +Halfway along, the two towers sound equally strong. You might expect the phone to jump to tower B right there, but it waits until B is clearly better, by H decibels: the handover margin. Without it, a phone standing near the middle would flip between the towers every second as the signal flickers. So the switch happens a little past the middle, at the dashed line. The thick line is the signal your phone actually uses: it sags as you leave A, then jumps up when the call moves to B. + +## Formal + +Signal fades with distance following the log-distance model, $P = P_1 - 10\,n\,\log_{10} d$ in dBm. Here $P_1$ is the signal 1 km from the tower (−75 dBm in this lesson) and $d$ is in kilometres. The exponent $n$ is 2 in open air and grows to 3 or 4 in cities, where buildings block and scatter the waves. Every tenfold step in distance costs $10n$ decibels. + +Subtract one tower's signal from the other's and the $P_1$ terms cancel, leaving $P_B - P_A = 10\,n\,\log_{10} (d_A / d_B)$. The phone hands over when this difference passes the margin $H$. Walking from A to B along a road of length $D$ (6 km here), that happens at $d_A = D\,r/(1+r)$, where $r = 10^{H/(10n)}$. That point is just past the middle, and further past it when $H$ is bigger or $n$ is smaller. + +## Advanced + +In LTE and 5G your phone measures a reference signal from every nearby cell (RSRP, the dBm figure you are reading here) and reports it, and the network makes the decision. The rule above is the standard "A3 event": a neighbour better than the serving cell by an offset, for a set time-to-trigger, usually 40 to 640 ms. The time condition is a second guard against ping-pong, so a brief gap between two buildings is not enough to switch. + +A tower usually carries three panels, each covering a 120° sector, so one mast is really three cells, and walking around it can hand you between its own sectors. An idle phone does a lighter version called cell reselection without asking the network; only during a call or a data session does the network steer the handover. And a cell is limited by more than signal: everyone in it shares its capacity, which is why cities add more, smaller cells rather than louder towers. + +## Derivation + +1. Write the same formula for each tower, using your distance to it: $d_A$ to tower A and $d_B$ to tower B. +2. Subtract one from the other. $P_1$ cancels, and a difference of two logarithms becomes the logarithm of a ratio, so only the ratio of the two distances matters. +3. The phone does not switch the moment B is stronger. It waits until the difference passes the margin $H$, so it does not bounce between the towers near the middle. + +## Real world + +### Calls that survive a motorway +At 100 km/h you cross each cell of a town in about a minute. Handover moves your call from tower to tower in a few tens of milliseconds, so you never hear the switch. + +### Bars that vanish in a lift +A metal lift or a basement adds tens of decibels of loss on top of the distance. If the phone hears no tower stronger than about −115 dBm, it shows "No service". + +### Small cells in cities +Downtown, signals fade fast and thousands of people share each cell, so carriers add small cells on lamp posts and rooftops a few hundred metres apart. + +### Stadiums and festivals +When 50,000 people gather in one field, carriers bring cell towers on trucks and park them beside it for the day, so each tower serves fewer phones. diff --git a/content/lessons/networking/cell-towers/lesson.yaml b/content/lessons/networking/cell-towers/lesson.yaml new file mode 100644 index 00000000..60c295e5 --- /dev/null +++ b/content/lessons/networking/cell-towers/lesson.yaml @@ -0,0 +1,62 @@ +id: cell-towers +subject: networking +difficulty: high-school +equation: P = P_1 - 10\,n\,\log_{10} d +concepts: +- cellular-network +- handover +- path-loss +- signal-strength +prerequisites: +- wifi +related: +- dialer-codes +- latency-and-bandwidth +visualization: + type: cell-towers +visualization3d: + type: cell-towers +parameters: + d: + min: 0.2 + max: 5.8 + step: 0.1 + value: 1.5 + unit: km + n: + min: 2 + max: 4 + step: 0.1 + value: 3 + H: + min: 0 + max: 10 + step: 1 + value: 3 + unit: dB +variables: +- P +- P_1 +- n +- d +- H +derivation: +- P_A = P_1 - 10\,n\,\log_{10} d_A \qquad P_B = P_1 - 10\,n\,\log_{10} d_B +- P_B - P_A = 10\,n\,\log_{10} \dfrac{d_A}{d_B} +- '\text{switch to B when } P_B - P_A > H' +charts: [] +primary: d +presets: +- d: 1.5 + n: 3 + H: 3 +- d: 3.5 + n: 2.4 + H: 3 +- d: 3.8 + n: 3.8 + H: 10 +challenges: +- check: 10*n*log10(d/(6-d)) > H +- check: abs(d - 3.5) < 0.05 and 10*n*log10(d/(6-d)) <= H +- check: 'n >= 3.5 and (10*n*log10(d/(6-d)) > H ? -75 - 10*n*log10(6-d) : -75 - 10*n*log10(d)) < -95' diff --git a/content/lessons/networking/dialer-codes/lesson.yaml b/content/lessons/networking/dialer-codes/lesson.yaml index 9fb95e2c..de815003 100644 --- a/content/lessons/networking/dialer-codes/lesson.yaml +++ b/content/lessons/networking/dialer-codes/lesson.yaml @@ -14,6 +14,8 @@ related: - packet-switching visualization: type: mmi-dialer +visualization3d: + type: mmi-dialer parameters: code: min: 0 diff --git a/content/ui/ar/cell-towers.yaml b/content/ui/ar/cell-towers.yaml new file mode 100644 index 00000000..fac1d1ab --- /dev/null +++ b/content/ui/ar/cell-towers.yaml @@ -0,0 +1,21 @@ +towers.hint: ▶ اسحب هاتفك على الطريق (أو منزلق d) وراقب أي برج يستعمل +towers.towerA: البرج A +towers.towerB: البرج B +towers.onA: على البرج A +towers.onB: على البرج B +towers.handover: التسليم +towers.middle: المنتصف +towers.fromA: من البرج A +towers.fromB: من البرج B +towers.uses: ما يستعمله هاتفك +towers.waits: B أقوى، والهاتف ينتظر +towers.bars: '{n|zero=لا أعمدة|one=عمود واحد|two=عمودان|few=# أعمدة|many=# عمودًا|other=# عمود}' +towers.noBars: لا توجد خدمة +towers.axisX: البُعد عن البرج A (km) +towers.axisY: الإشارة (dBm) +towers.caption: 'يسمع هاتفك البرجين طوال الوقت، ولا ينقل مكالمتك إلا حين يتفوق البرج الجديد على القديم بالهامش H' +towers.r.dA: إلى البرج A +towers.r.dB: إلى البرج B +towers.r.diff: B ناقص A (الهامش {h} dB) +towers.r.on: متصل بالبرج +towers.r.bars: الأعمدة diff --git a/content/ui/ar/cell-towers3d.yaml b/content/ui/ar/cell-towers3d.yaml new file mode 100644 index 00000000..84fc3140 --- /dev/null +++ b/content/ui/ar/cell-towers3d.yaml @@ -0,0 +1,12 @@ +viz3d.towers.walk: ▶ امشِ إلى B +viz3d.towers.pause: ❚❚ توقف +viz3d.towers.restart: ↺ عُد إلى A +viz3d.towers.you: أنت +viz3d.towers.on: 'على البرج {t}' +viz3d.towers.weaker: 'البرج B أضعف بـ {db}' +viz3d.towers.waiting: 'البرج B أقوى بـ {db}، لكن ليس بهامش {h} بعد' +viz3d.towers.moved: 'تفوق البرج B على A بأكثر من {h}، فانتقلت المكالمة' +viz3d.towers.place0: ريف مفتوح +viz3d.towers.place1: ضاحية +viz3d.towers.place2: مدينة +viz3d.towers.place3: مدينة مزدحمة diff --git a/content/ui/ar/mmi-dialer3d.yaml b/content/ui/ar/mmi-dialer3d.yaml new file mode 100644 index 00000000..38e38b23 --- /dev/null +++ b/content/ui/ar/mmi-dialer3d.yaml @@ -0,0 +1,27 @@ +viz3d.mmi.you: هاتفك +viz3d.mmi.tower: برج الاتصالات +viz3d.mmi.carrier: المشغّل +viz3d.mmi.other: الهاتف الآخر +viz3d.mmi.typing: تكتب الرمز… +viz3d.mmi.out: في طريقه إلى المشغّل +viz3d.mmi.back: الرد في طريقه إليك +viz3d.mmi.replied: 'وصل الرد بعد {ms}' +viz3d.mmi.local: يجيب الهاتف بنفسه، ولا يخرج منه شيء +viz3d.mmi.app: يسلّمه الهاتف إلى تطبيق فيه، ولا يخرج منه شيء +viz3d.mmi.calling: يصل المشغّل إلى الهاتف الآخر عبر البرج القريب منه +viz3d.mmi.ringing: الهاتف الآخر يرنّ +viz3d.mmi.zero: '{ms}، فلا رحلة أصلًا' +viz3d.mmi.slow: 'أبطأ من الحقيقة {k} مرة' +viz3d.mmi.scr.wait: جارٍ تشغيل الرمز… +viz3d.mmi.scr.call: جارٍ الاتصال… +viz3d.mmi.scr.cf: تحويل المكالمات +viz3d.mmi.scr.cf1: 'الصوت: لا تحويل' +viz3d.mmi.scr.ussd: حسابي +viz3d.mmi.scr.ussd1: 1 الرصيد +viz3d.mmi.scr.ussd2: 2 الباقات +viz3d.mmi.scr.ussd3: 3 الشحن +viz3d.mmi.scr.app: اختبار +viz3d.mmi.scr.app1: معلومات الهاتف +viz3d.mmi.scr.app2: إحصاءات الاستخدام +viz3d.mmi.scr.app3: معلومات Wi-Fi +viz3d.mmi.scr.ring: مكالمة واردة diff --git a/content/ui/en/cell-towers.yaml b/content/ui/en/cell-towers.yaml new file mode 100644 index 00000000..46543587 --- /dev/null +++ b/content/ui/en/cell-towers.yaml @@ -0,0 +1,21 @@ +towers.hint: ▶ drag your phone along the road (or the d slider) and watch which tower it uses +towers.towerA: tower A +towers.towerB: tower B +towers.onA: on tower A +towers.onB: on tower B +towers.handover: handover +towers.middle: middle +towers.fromA: from tower A +towers.fromB: from tower B +towers.uses: what your phone uses +towers.waits: B stronger, phone still waits +towers.bars: '{n|one=# bar|other=# bars}' +towers.noBars: no service +towers.axisX: distance from tower A (km) +towers.axisY: signal (dBm) +towers.caption: 'your phone listens to both towers all the time; it moves your call only when the new one beats the old one by the margin H' +towers.r.dA: to tower A +towers.r.dB: to tower B +towers.r.diff: B minus A (margin {h} dB) +towers.r.on: connected to tower +towers.r.bars: bars diff --git a/content/ui/en/cell-towers3d.yaml b/content/ui/en/cell-towers3d.yaml new file mode 100644 index 00000000..46e8be99 --- /dev/null +++ b/content/ui/en/cell-towers3d.yaml @@ -0,0 +1,12 @@ +viz3d.towers.walk: ▶ walk to B +viz3d.towers.pause: ❚❚ pause +viz3d.towers.restart: ↺ back to A +viz3d.towers.you: you +viz3d.towers.on: 'on tower {t}' +viz3d.towers.weaker: 'tower B is {db} weaker' +viz3d.towers.waiting: 'tower B is {db} stronger, but not by the {h} margin yet' +viz3d.towers.moved: 'tower B beat A by more than {h}, so the call moved' +viz3d.towers.place0: open country +viz3d.towers.place1: suburbs +viz3d.towers.place2: city +viz3d.towers.place3: dense city diff --git a/content/ui/en/mmi-dialer3d.yaml b/content/ui/en/mmi-dialer3d.yaml new file mode 100644 index 00000000..c21b40da --- /dev/null +++ b/content/ui/en/mmi-dialer3d.yaml @@ -0,0 +1,27 @@ +viz3d.mmi.you: your phone +viz3d.mmi.tower: cell tower +viz3d.mmi.carrier: the carrier +viz3d.mmi.other: the other phone +viz3d.mmi.typing: typing the code… +viz3d.mmi.out: on its way to the carrier +viz3d.mmi.back: the reply is on its way back +viz3d.mmi.replied: 'reply after {ms}' +viz3d.mmi.local: answered inside the phone — nothing leaves it +viz3d.mmi.app: handed to an app on the phone — nothing leaves it +viz3d.mmi.calling: the carrier finds the other phone, through the tower near it +viz3d.mmi.ringing: the other phone rings +viz3d.mmi.zero: '{ms} — no trip at all' +viz3d.mmi.slow: 'shown {k}× slower than real' +viz3d.mmi.scr.wait: Running code… +viz3d.mmi.scr.call: Calling… +viz3d.mmi.scr.cf: Call forwarding +viz3d.mmi.scr.cf1: 'Voice: not forwarded' +viz3d.mmi.scr.ussd: My account +viz3d.mmi.scr.ussd1: 1 Balance +viz3d.mmi.scr.ussd2: 2 Bundles +viz3d.mmi.scr.ussd3: 3 Top up +viz3d.mmi.scr.app: Testing +viz3d.mmi.scr.app1: Phone information +viz3d.mmi.scr.app2: Usage statistics +viz3d.mmi.scr.app3: Wi-Fi information +viz3d.mmi.scr.ring: Incoming call diff --git a/src/components/MathText.vue b/src/components/MathText.vue index 34879a27..150c1aef 100644 --- a/src/components/MathText.vue +++ b/src/components/MathText.vue @@ -4,9 +4,11 @@ import Katex from './Katex.vue' // Plain text with inline $LaTeX$ segments. In right-to-left text (Arabic), plain Latin runs such as // "x = -5" or "F/m" are isolated as left-to-right islands so the bidi algorithm cannot scramble them. +// So is a negative number, with its unit if one follows ("−95 dBm"); left in the Arabic run, its sign +// would land on the wrong side and read "95−". const props = defineProps<{ text: string }>() const RTL_CHARS = /[\u0590-\u08FF]/ -const LATIN_RUN = /([A-Za-z](?:[A-Za-z0-9 =+\-−×·*/^().,<>≤≥%']*[A-Za-z0-9)%'])?)/ +const LATIN_RUN = /((?:(?≤≥%']*[A-Za-z0-9)%'])?|(?(() => { const rtl = RTL_CHARS.test(props.text) diff --git a/src/viz/networking/CellTowers.vue b/src/viz/networking/CellTowers.vue new file mode 100644 index 00000000..6f1b1059 --- /dev/null +++ b/src/viz/networking/CellTowers.vue @@ -0,0 +1,168 @@ + + + diff --git a/src/viz/networking/MmiDialer.vue b/src/viz/networking/MmiDialer.vue index 5c6a9417..5c5609de 100644 --- a/src/viz/networking/MmiDialer.vue +++ b/src/viz/networking/MmiDialer.vue @@ -2,6 +2,7 @@ import { computed } from 'vue' import { t, isRtl, type Key } from '../../i18n' import Readouts from '../../components/Readouts.vue' +import { codeAt, type Dest, type Seg } from './mmiCodes' // networking/dialer-codes. params code (0..4, which string is dialed), rtt (one round trip to the // carrier, ms). The dialer is a small parser: all digits → a phone call; a string wrapped in * and # @@ -13,22 +14,6 @@ defineEmits<{ set: [name: string, value: number] }>() const W = 800 const H = 480 -type Seg = { s: string; k: 'px' | 'sc' | 'sx' | 'num' } -type Dest = 'phone' | 'network' | 'app' | 'call' -interface Code { - seg: Seg[] - kind: Key - dest: Dest - answer: Key - n: 0 | 1 -} -const CODES: Code[] = [ - { seg: [{ s: '070 123 4567', k: 'num' }], kind: 'mmi.c.number', dest: 'call', answer: 'mmi.a.call', n: 0 }, - { seg: [{ s: '*#', k: 'px' }, { s: '06', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.mmi', dest: 'phone', answer: 'mmi.a.imei', n: 0 }, - { seg: [{ s: '*#', k: 'px' }, { s: '21', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.ss', dest: 'network', answer: 'mmi.a.cf', n: 1 }, - { seg: [{ s: '*', k: 'px' }, { s: '123', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.ussd', dest: 'network', answer: 'mmi.a.ussd', n: 1 }, - { seg: [{ s: '*#*#', k: 'px' }, { s: '4636', k: 'sc' }, { s: '#*#*', k: 'sx' }], kind: 'mmi.c.secret', dest: 'app', answer: 'mmi.a.secret', n: 0 }, -] // The figure stays left-to-right like every figure on the site, but its sentences take the reader's // direction, so Arabic keeps "50 ms" and "#" in place. A start-aligned label flips to "end" in Arabic // so it still grows rightwards from its x. @@ -36,8 +21,7 @@ const dir = computed(() => (isRtl() ? 'rtl' : 'ltr')) const start = computed(() => (isRtl() ? 'end' : 'start')) const SEG_COLOR: Record = { px: 'var(--accent-2)', sc: 'var(--accent)', sx: 'var(--accent-2)', num: 'var(--fg)' } -const idx = computed(() => Math.min(Math.max(Math.round(props.params.code ?? 0), 0), CODES.length - 1)) -const code = computed(() => CODES[idx.value]) +const code = computed(() => codeAt(props.params.code)) const rtt = computed(() => Math.min(Math.max(props.params.rtt ?? 50, 10), 300)) const raw = computed(() => code.value.seg.map((s) => s.s).join('')) const time = computed(() => code.value.n * rtt.value) diff --git a/src/viz/networking/cellModel.ts b/src/viz/networking/cellModel.ts new file mode 100644 index 00000000..0be187ae --- /dev/null +++ b/src/viz/networking/cellModel.ts @@ -0,0 +1,19 @@ +// The cell-towers lesson's model, shared by the flat picture (CellTowers.vue) and the 3D scene +// (CellTowers3D.vue): two towers SPAN km apart, the log-distance signal P = P1 − 10·n·log10(d), and a +// phone walking from tower A towards tower B that moves to B once B beats A by the margin H. +export const SPAN = 6 +export const P1 = -75 + +const clamp = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), hi) +export const readParams = (p: Record) => ({ d: clamp(p.d ?? 1.5, 0.2, 5.8), n: clamp(p.n ?? 3, 2, 4), H: clamp(p.H ?? 3, 0, 10) }) + +/** Received signal in dBm at `km` from a tower. */ +export const signal = (km: number, n: number) => P1 - 10 * n * Math.log10(km) +/** Walking from A, the distance where tower B first beats tower A by H: SPAN·r/(1+r), r = 10^(H/10n). */ +export function handoverAt(n: number, H: number) { + const r = 10 ** (H / (10 * n)) + return (SPAN * r) / (1 + r) +} +export const onTowerB = (d: number, n: number, H: number) => signal(SPAN - d, n) - signal(d, n) > H +/** Bars as most phones draw them from LTE signal strength. */ +export const barsOf = (dbm: number) => (dbm >= -85 ? 4 : dbm >= -95 ? 3 : dbm >= -105 ? 2 : dbm >= -115 ? 1 : 0) diff --git a/src/viz/networking/mmiCodes.ts b/src/viz/networking/mmiCodes.ts new file mode 100644 index 00000000..e8045b69 --- /dev/null +++ b/src/viz/networking/mmiCodes.ts @@ -0,0 +1,22 @@ +import type { Key } from '../../i18n' + +// The five strings the dialer-codes lesson types, shared by the flat picture (MmiDialer.vue) and the +// 3D scene (MmiDialer3D.vue) so both agree on what each one is and where it goes. n is the number +// of round trips to the carrier before the answer appears. +export type Seg = { s: string; k: 'px' | 'sc' | 'sx' | 'num' } +export type Dest = 'phone' | 'network' | 'app' | 'call' +export interface Code { + seg: Seg[] + kind: Key + dest: Dest + answer: Key + n: 0 | 1 +} +export const CODES: Code[] = [ + { seg: [{ s: '070 123 4567', k: 'num' }], kind: 'mmi.c.number', dest: 'call', answer: 'mmi.a.call', n: 0 }, + { seg: [{ s: '*#', k: 'px' }, { s: '06', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.mmi', dest: 'phone', answer: 'mmi.a.imei', n: 0 }, + { seg: [{ s: '*#', k: 'px' }, { s: '21', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.ss', dest: 'network', answer: 'mmi.a.cf', n: 1 }, + { seg: [{ s: '*', k: 'px' }, { s: '123', k: 'sc' }, { s: '#', k: 'sx' }], kind: 'mmi.c.ussd', dest: 'network', answer: 'mmi.a.ussd', n: 1 }, + { seg: [{ s: '*#*#', k: 'px' }, { s: '4636', k: 'sc' }, { s: '#*#*', k: 'sx' }], kind: 'mmi.c.secret', dest: 'app', answer: 'mmi.a.secret', n: 0 }, +] +export const codeAt = (v: number | undefined) => CODES[Math.min(Math.max(Math.round(v ?? 0), 0), CODES.length - 1)] diff --git a/src/viz3d/networking/CellTowers3D.vue b/src/viz3d/networking/CellTowers3D.vue new file mode 100644 index 00000000..df504b79 --- /dev/null +++ b/src/viz3d/networking/CellTowers3D.vue @@ -0,0 +1,309 @@ + + + diff --git a/src/viz3d/networking/MmiDialer3D.vue b/src/viz3d/networking/MmiDialer3D.vue new file mode 100644 index 00000000..c4f74d81 --- /dev/null +++ b/src/viz3d/networking/MmiDialer3D.vue @@ -0,0 +1,468 @@ + + + diff --git a/src/viz3d/networking/parts.ts b/src/viz3d/networking/parts.ts new file mode 100644 index 00000000..98813e38 --- /dev/null +++ b/src/viz3d/networking/parts.ts @@ -0,0 +1,133 @@ +import * as THREE from 'three' +import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry.js' +import type { Theme } from '../useThree' +import { isRtl, locale } from '../../i18n' + +// Pieces of a small town shared by the networking scenes: phones, cell towers, houses and labels. +// Canvas textures are pushed into `keep` so the scene can dispose of them when it unmounts. + +export const FONT = 'Inter, "IBM Plex Sans Arabic", system-ui, sans-serif' +export const hex = (c: THREE.Color) => '#' + c.getHexString() + +export function canvasTexture(w: number, h: number, keep: THREE.Texture[]) { + const c = document.createElement('canvas') + c.width = w + c.height = h + const tex = new THREE.CanvasTexture(c) + tex.colorSpace = THREE.SRGBColorSpace + tex.anisotropy = 4 + keep.push(tex) + return { c, g: c.getContext('2d')!, tex } +} + +/** + * A label that always faces the camera, on a soft plate so it reads over the ground grid. Its size is + * fixed on screen (height is a share of the view), so a far tower reads as well as a near phone. + * `side` pins the plate's left or right edge to the label's position, so a label beside something near + * the edge of the view grows towards the middle instead of off the screen. + */ +export function makeTag(height: number, keep: THREE.Texture[], side: 'center' | 'left' | 'right' = 'center') { + const { c, g, tex } = canvasTexture(1024, 112, keep) + const sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map: tex, transparent: true, depthTest: false, sizeAttenuation: false })) + sprite.scale.set((height * c.width) / c.height, height, 1) + sprite.center.set(side === 'left' ? 0 : side === 'right' ? 1 : 0.5, 0.5) + sprite.renderOrder = 10 + let last = '' + function paint(text: string, color: THREE.Color, plate: THREE.Color) { + const key = `${text}|${hex(color)}|${hex(plate)}|${locale.value}` + if (key === last) return + last = key + g.clearRect(0, 0, c.width, c.height) + if (text) { + g.font = `600 56px ${FONT}` + g.direction = isRtl() ? 'rtl' : 'ltr' + g.textAlign = 'center' + g.textBaseline = 'middle' + const w = Math.min(c.width - 8, g.measureText(text).width + 52) + const x0 = side === 'left' ? 4 : side === 'right' ? c.width - 4 - w : (c.width - w) / 2 + g.globalAlpha = 0.9 + g.fillStyle = hex(plate) + g.beginPath() + g.roundRect(x0, 10, w, c.height - 20, 26) + g.fill() + g.globalAlpha = 1 + g.fillStyle = hex(color) + g.fillText(text, x0 + w / 2, c.height / 2 + 2, w - 40) + } + tex.needsUpdate = true + } + return { sprite, paint } +} + +/** A phone standing in a little dock, its screen a canvas `px` pixels wide that the scene paints. */ +export function makePhone(w: number, h: number, theme: Theme, px: number, keep: THREE.Texture[]) { + const group = new THREE.Group() + const body = new THREE.Mesh(new RoundedBoxGeometry(w, h, 0.18, 4, 0.08), new THREE.MeshStandardMaterial({ color: theme.fg, roughness: 0.45 })) + body.castShadow = true + group.add(body) + const sw = w * 0.88 + const sh = h * 0.9 + const screen = canvasTexture(px, Math.round((px * sh) / sw), keep) + const face = new THREE.Mesh(new THREE.PlaneGeometry(sw, sh), new THREE.MeshBasicMaterial({ map: screen.tex, toneMapped: false })) + face.position.z = 0.092 + group.add(face) + const dock = new THREE.Mesh(new THREE.BoxGeometry(w * 0.8, 0.22, 0.7), new THREE.MeshStandardMaterial({ color: theme.muted, roughness: 0.8 })) + dock.position.set(0, -h / 2 - 0.13, -0.05) + dock.castShadow = dock.receiveShadow = true + group.add(dock) + return { group, screen } +} + +/** A lattice mast with three antenna panels near the top; the panels sit at height − 0.75. */ +export function makeTower(height: number, theme: Theme) { + const group = new THREE.Group() + // a four-legged lattice mast: the wireframe of an open, tapered square prism draws its legs, rungs and braces + const lattice = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.55, height, 4, 7, true), new THREE.MeshBasicMaterial({ color: theme.muted, wireframe: true })) + lattice.position.y = height / 2 + lattice.rotation.y = Math.PI / 4 + group.add(lattice) + // three antenna panels, one per 120° sector + for (let i = 0; i < 3; i++) { + const a = (i * 2 * Math.PI) / 3 + 0.5 + const panel = new THREE.Mesh(new THREE.BoxGeometry(0.28, 1, 0.1), new THREE.MeshStandardMaterial({ color: theme.bg, roughness: 0.6 })) + panel.position.set(Math.sin(a) * 0.3, height - 0.75, Math.cos(a) * 0.3) + panel.rotation.y = a + panel.castShadow = true + group.add(panel) + } + const beacon = new THREE.Mesh(new THREE.SphereGeometry(0.1, 10, 8), new THREE.MeshBasicMaterial({ color: theme.neg })) + beacon.position.y = height + 0.1 + group.add(beacon) + const footing = new THREE.Mesh(new THREE.BoxGeometry(1.4, 0.12, 1.4), new THREE.MeshStandardMaterial({ color: theme.line, roughness: 1 })) + footing.position.y = 0.06 + footing.receiveShadow = true + group.add(footing) + return group +} + +export function makeHouse(theme: Theme, w: number, d: number, h: number) { + const group = new THREE.Group() + const walls = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), new THREE.MeshStandardMaterial({ color: theme.bg, roughness: 0.9 })) + walls.position.y = h / 2 + walls.castShadow = walls.receiveShadow = true + const roof = new THREE.Mesh(new THREE.ConeGeometry(Math.hypot(w, d) / 2 + 0.1, h * 0.6, 4), new THREE.MeshStandardMaterial({ color: theme.muted, roughness: 0.9 })) + roof.position.y = h + h * 0.3 + roof.rotation.y = Math.PI / 4 + roof.castShadow = true + group.add(walls, roof) + return group +} + +/** Every material under obj fades together, to dim the places that take no part. */ +export function fader(obj: THREE.Object3D) { + const mats: THREE.Material[] = [] + obj.traverse((o: any) => { + if (o.material) ((o.material.transparent = true), mats.push(o.material)) + }) + let cur = -1 + return (o: number) => { + if (o === cur) return + cur = o + mats.forEach((m) => (m.opacity = o)) + } +} diff --git a/src/viz3d/useThree.ts b/src/viz3d/useThree.ts index 0273835c..da3dedd4 100644 --- a/src/viz3d/useThree.ts +++ b/src/viz3d/useThree.ts @@ -47,6 +47,11 @@ export function useThree(container: Ref, setup: (ctx: T renderer.setPixelRatio(Math.min(devicePixelRatio, 2)) renderer.shadowMap.enabled = true renderer.shadowMap.type = THREE.PCFSoftShadowMap + // setSize(…, false) below sizes only the drawing buffer (box × pixel ratio); without these the + // canvas shows at that buffer size, so on a 125 % or 200 % screen the scene overflows its box and + // is cropped. + renderer.domElement.style.width = '100%' + renderer.domElement.style.height = '100%' el.appendChild(renderer.domElement) const scene = new THREE.Scene() const camera = new THREE.PerspectiveCamera(opts.fov ?? 45, 1, 0.1, 500)