添加部分功能,以及等高线模型

This commit is contained in:
2509_92447106 committed 2026-11-14 10:06:45 +08:00
1 parent 1cca045f08
commit 8dcbd6f9d7
19 files changed
+550 -93

No files matched your search

+4 -4
View File
@@ -28,21 +28,21 @@ public sealed class ContourMapRuntime : IModelRuntime
public void Prepare(IModelContext context)
{
context.ReportProgress(0.15, "生成地形高度场");
_field = new TerrainField(192);
_field = new TerrainField(TerrainKind.Mountain, 192);
context.ReportProgress(0.70, "提取等高线");
_texts = LoadTexts(context.DataDirectory, context.Language);
context.ReportProgress(0.92, "准备界面");
context.Log($"[contour-map] 地形 {_field.Size}×{_field.Size}," +
$"海拔 {_field.MinHeight:0}~{_field.MaxHeight:0} m,等高距 {_field.Interval:0} m");
$"海拔 {_field.MinHeight:0}~{_field.MaxHeight:0} m,等高距 {_field.EffectiveInterval:0} m");
}
public Control CreateView(IModelContext context)
{
_field ??= new TerrainField(192);
_texts ??= LoadTexts(context.DataDirectory, context.Language);
return new ContourPage(_field, _texts).Build();
// 五种地体都在界面里现场生成(切换很快,不必预先生成全部)
return new ContourPage(_texts).Build();
}
public void Dispose()
+123 -6
View File
@@ -20,16 +20,31 @@ public sealed class ContourPage
private static readonly Color CWarn = Color.Parse("#FFB454");
private static readonly Color CGood = Color.Parse("#5FD69B");
private readonly TerrainField _field;
private TerrainField _field;
private readonly Dictionary<string, string> _texts;
private readonly Dictionary<TerrainKind, TerrainField> _cache = new();
private Border? _colorBtn;
private TextBlock? _colorBtnText;
private TerrainKind _kind = TerrainKind.Mountain;
private ContourView? _view;
private TextBlock? _readout;
private TextBlock? _featureText;
public ContourPage(TerrainField field, Dictionary<string, string> texts)
public ContourPage(Dictionary<string, string> texts)
{
_field = field;
_texts = texts;
_field = GetField(TerrainKind.Mountain);
}
/// <summary>取(并缓存)某类地体的高度场,切换时不重复计算。</summary>
private TerrainField GetField(TerrainKind kind)
{
if (!_cache.TryGetValue(kind, out var f))
{
f = new TerrainField(kind, 192);
_cache[kind] = f;
}
return f;
}
private string T(string key) => _texts.TryGetValue(key, out var v) ? v : key;
@@ -95,16 +110,84 @@ public sealed class ContourPage
stack.Children.Add(title);
stack.Children.Add(sub);
// 地体类型选择:教科书里的五种基本地形
var kinds = new (TerrainKind Kind, string Key)[]
{
(TerrainKind.Mountain, "kind.mountain"),
(TerrainKind.Basin, "kind.basin"),
(TerrainKind.Hill, "kind.hill"),
(TerrainKind.Plateau, "kind.plateau"),
(TerrainKind.Plain, "kind.plain"),
};
var tabs = new StackPanel
{
Orientation = Orientation.Horizontal,
Spacing = 6,
Margin = new Thickness(0, 9, 0, 0)
};
foreach (var (kind, key) in kinds)
{
var btn = MakeKindTab(kind, T(key));
tabs.Children.Add(btn);
}
var header = new StackPanel();
header.Children.Add(stack);
header.Children.Add(tabs);
return new Border
{
Background = B(CPanel),
BorderBrush = B(CLine),
BorderThickness = new Thickness(0, 0, 0, 1),
Padding = new Thickness(18, 11),
Child = stack
Padding = new Thickness(18, 11, 18, 12),
Child = header
};
}
/// <summary>地体类型按钮:选中高亮。</summary>
private Button MakeKindTab(TerrainKind kind, string label)
{
var b = new Button
{
Content = label,
FontSize = 11.5,
Padding = new Thickness(13, 5),
CornerRadius = new CornerRadius(8),
BorderThickness = new Thickness(1),
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
Tag = kind
};
void Style()
{
var on = _kind == kind;
b.Background = B(on ? CAccent : Color.Parse("#16263C"));
b.BorderBrush = B(on ? CAccent : Color.Parse("#263A56"));
b.Foreground = B(on ? Colors.White : CInkDim);
}
Style();
b.Click += (_, _) =>
{
_kind = kind;
_field = GetField(kind);
_view?.SetField(_field);
// 重新刷新所有 tab 的选中态
if (b.Parent is StackPanel sp)
foreach (var c in sp.Children)
if (c is Button sib && sib.Tag is TerrainKind)
{
var on = (TerrainKind)sib.Tag == _kind;
sib.Background = B(on ? CAccent : Color.Parse("#16263C"));
sib.BorderBrush = B(on ? CAccent : Color.Parse("#263A56"));
sib.Foreground = B(on ? Colors.White : CInkDim);
}
};
return b;
}
private Control BuildSidePanel()
{
var panel = new StackPanel { Spacing = 10, Margin = new Thickness(12, 0, 0, 0) };
@@ -203,8 +286,31 @@ public sealed class ContourPage
HorizontalAlignment = HorizontalAlignment.Right
};
// 右下角「上色」开关:点击一次去掉分层设色,只显示线稿,再点恢复
_colorBtnText = new TextBlock
{
Text = T("btn.color"),
FontSize = 11.5,
Foreground = B(Colors.White)
};
_colorBtn = new Border
{
Background = B(CAccent),
CornerRadius = new CornerRadius(8),
Padding = new Thickness(13, 6),
Margin = new Thickness(0, 0, 12, 0),
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
Child = _colorBtnText
};
_colorBtn.PointerPressed += (_, _) => ToggleColor();
var left = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
left.Children.Add(_readout);
Grid.SetColumn(_colorBtn, 1);
left.Children.Add(_colorBtn);
var row = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
row.Children.Add(_readout);
row.Children.Add(left);
Grid.SetColumn(_featureText, 1);
row.Children.Add(_featureText);
@@ -218,6 +324,17 @@ public sealed class ContourPage
};
}
/// <summary>切换分层设色。</summary>
private void ToggleColor()
{
if (_view == null) return;
_view.Colored = !_view.Colored;
if (_colorBtn != null)
_colorBtn.Background = B(_view.Colored ? CAccent : Color.Parse("#2A3B55"));
if (_colorBtnText != null)
_colorBtnText.Text = _view.Colored ? T("btn.colorOn") : T("btn.color");
}
private void OnHover(double u, double v, float height, TerrainFeature feature)
{
if (_readout == null || _featureText == null) return;
+23 -2
View File
@@ -27,7 +27,18 @@ public sealed class ContourView : Control
private static readonly Typeface Font = new("Noto Sans CJK SC, Microsoft YaHei, Arial");
public TerrainField Field { get; }
public TerrainField Field { get; private set; }
/// <summary>切换到另一块地形(重新提取等高线)。</summary>
public void SetField(TerrainField field)
{
Field = field;
_levels = Field.ContourLevels();
_contours.Clear();
foreach (var lv in _levels)
_contours.Add((lv, Field.ExtractContour(lv)));
InvalidateVisual();
}
/// <summary>等高线缓存:level -> 折线集合。</summary>
private readonly List<(float Level, List<List<(double U, double V)>> Lines)> _contours = new();
@@ -42,6 +53,7 @@ public sealed class ContourView : Control
private bool _showContours = true;
private bool _showLabels = true;
private bool _showSlope = true;
private bool _colored = true;
public bool ShowContours
{
@@ -61,6 +73,13 @@ public sealed class ContourView : Control
set => SetFlag(ref _showSlope, value);
}
/// <summary>是否分层设色(关闭后只看线稿,便于观察等高线本身)。</summary>
public bool Colored
{
get => _colored;
set => SetFlag(ref _colored, value);
}
private void SetFlag(ref bool slot, bool value)
{
if (slot == value) return;
@@ -234,7 +253,9 @@ public sealed class ContourView : Control
var u = (i + 0.5) / N; var v = (j + 0.5) / N;
var h = Field.SampleSmooth(u, v);
var (cr, cg, cb) = TerrainAnalyzer.ColorForHeight(h, Field.MinHeight, Field.MaxHeight);
var brush = new SolidColorBrush(Color.FromRgb(cr, cg, cb));
var brush = _colored
? new SolidColorBrush(Color.FromRgb(cr, cg, cb))
: new SolidColorBrush(Color.Parse("#F3F0E6"));
dc.FillRectangle(brush, new Rect(
r.X + r.Width * i / N,
r.Y + r.Height * j / N,
+16
View File
@@ -10,6 +10,14 @@ internal static class DefaultTexts
["app.title"] = "等高线地形图",
["app.subtitle"] = "七年级上册 · 地形图的判读:从立体地形到平面等高线",
["kind.mountain"] = "山地",
["kind.basin"] = "盆地",
["kind.hill"] = "丘陵",
["kind.plateau"] = "高原",
["kind.plain"] = "平原",
["btn.color"] = "上色",
["btn.colorOn"] = "取消上色",
["side.features"] = "五种地形部位",
["feature.peak"] = "山峰",
["feature.peakDesc"] = "等高线闭合,数值由中心向四周递减。",
@@ -38,6 +46,14 @@ internal static class DefaultTexts
["app.title"] = "Contour Map",
["app.subtitle"] = "Grade 7 · Reading terrain maps: from 3D relief to contour lines",
["kind.mountain"] = "Mountains",
["kind.basin"] = "Basin",
["kind.hill"] = "Hills",
["kind.plateau"] = "Plateau",
["kind.plain"] = "Plain",
["btn.color"] = "Colour",
["btn.colorOn"] = "No colour",
["side.features"] = "Five terrain features",
["feature.peak"] = "Peak",
["feature.peakDesc"] = "Closed contours; values decrease outward from the centre.",
+156 -6
View File
@@ -3,6 +3,16 @@ using System.Collections.Generic;
namespace NeoRF.ContourMap;
/// <summary>五种基本地形类型(教科书分类)。</summary>
public enum TerrainKind
{
Mountain, // 山地:海拔高、起伏大
Basin, // 盆地:四周高、中间低
Hill, // 丘陵:海拔较低、坡度和缓
Plateau, // 高原:海拔高、地面平坦、边缘陡
Plain // 平原:海拔低、地面平坦
}
/// <summary>
/// 地形高度场(等高线模型的数据核心)。
///
@@ -29,13 +39,25 @@ public sealed class TerrainField
/// <summary>等高距(米):教学图常用 100 m。</summary>
public float Interval { get; init; } = 100f;
public TerrainField(int size = 192)
/// <summary>这块地形属于哪种类型(决定界面标题与说明)。</summary>
public TerrainKind Kind { get; }
public TerrainField(TerrainKind kind = TerrainKind.Mountain, int size = 192)
{
Size = size;
Kind = kind;
Heights = new float[size, size];
Build();
Build(kind);
}
/// <summary>该类型对应的等高距:平原/丘陵用 20m,山地/高原/盆地用 100m。</summary>
public float EffectiveInterval => Kind switch
{
TerrainKind.Plain => 5f, // 平原起伏小,等高距要小才画得出线
TerrainKind.Hill => 10f, // 丘陵相对高差约 150 m
_ => 100f
};
/// <summary>归一化坐标 (u,v) ∈ [0,1],u 向东、v 向南。</summary>
public float HeightAt(double u, double v)
{
@@ -61,7 +83,19 @@ public sealed class TerrainField
return (float)(a * (1 - ty) + b * ty);
}
private void Build()
private void Build(TerrainKind kind)
{
switch (kind)
{
case TerrainKind.Mountain: BuildMountain(); break;
case TerrainKind.Basin: BuildBasin(); break;
case TerrainKind.Hill: BuildHill(); break;
case TerrainKind.Plateau: BuildPlateau(); break;
case TerrainKind.Plain: BuildPlain(); break;
}
}
private void BuildMountain()
{
// 各山体参数:中心(u,v)、高度、东西半径、南北半径
var peaks = new (double U, double V, double H, double RU, double RV)[]
@@ -116,6 +150,121 @@ public sealed class TerrainField
MaxHeight = max;
}
/// <summary>盆地:四周高、中间低,等高线由外向内递减。</summary>
private void BuildBasin()
{
var min = float.MaxValue; var max = float.MinValue;
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
var u = (double)x / (Size - 1);
var v = (double)y / (Size - 1);
var du = u - 0.5; var dv = v - 0.5;
var r = Math.Sqrt(du * du + dv * dv) * 2.0; // 0=中心 1=边缘
// 边缘抬升、中心下沉,形成碗状
double h = 160 + 900 * Math.Pow(Math.Clamp(r, 0, 1.15), 2.2);
// 一侧开个缺口(山口),让盆地向外开口
var gap = Math.Abs(Math.Atan2(dv, du) - 0.5);
if (gap < 0.35) h -= 320 * (1 - gap / 0.35) * (1 - Math.Clamp(r, 0, 1));
h += 14 * Math.Sin(u * 7.3) * Math.Cos(v * 6.1);
var hf = (float)Math.Max(0, h);
Heights[y, x] = hf;
if (hf < min) min = hf;
if (hf > max) max = hf;
}
}
MinHeight = min; MaxHeight = max;
}
/// <summary>丘陵:起伏和缓、海拔不高,等高线稀疏而弯曲。</summary>
private void BuildHill()
{
var bumps = new (double U, double V, double H, double R)[]
{
(0.30, 0.32, 120, 0.20), (0.66, 0.28, 95, 0.17),
(0.52, 0.62, 140, 0.22), (0.22, 0.72, 88, 0.16),
(0.78, 0.70, 110, 0.18),
};
var min = float.MaxValue; var max = float.MinValue;
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
var u = (double)x / (Size - 1);
var v = (double)y / (Size - 1);
double h = 60;
foreach (var b in bumps)
{
var du = (u - b.U) / b.R; var dv = (v - b.V) / b.R;
h += b.H * Math.Exp(-(du * du + dv * dv) * 1.3);
}
h += 10 * Math.Sin(u * 11.0) * Math.Cos(v * 9.0);
var hf = (float)Math.Max(0, h);
Heights[y, x] = hf;
if (hf < min) min = hf;
if (hf > max) max = hf;
}
}
MinHeight = min; MaxHeight = max;
}
/// <summary>高原:顶部平坦、边缘陡峭(等高线在中部稀疏、边缘密集)。</summary>
private void BuildPlateau()
{
var min = float.MaxValue; var max = float.MinValue;
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
var u = (double)x / (Size - 1);
var v = (double)y / (Size - 1);
// 到中心的距离,做平台 + 陡降
var du = (u - 0.5); var dv = (v - 0.5);
var r = Math.Sqrt(du * du + dv * dv) * 2.0;
double h;
if (r < 0.55) h = 1250; // 平坦顶面
else h = 1250 - 1000 * SmoothStep(0.55, 0.78, r); // 边缘陡降
// 顶面轻微起伏,避免完全平板
if (r < 0.55) h += 25 * Math.Sin(u * 6.0) * Math.Cos(v * 5.0);
h += 30 * (1 - Math.Clamp(r, 0, 1));
var hf = (float)Math.Max(0, h);
Heights[y, x] = hf;
if (hf < min) min = hf;
if (hf > max) max = hf;
}
}
MinHeight = min; MaxHeight = max;
}
/// <summary>平原:地面低平、起伏极小,等高线很少。</summary>
private void BuildPlain()
{
var min = float.MaxValue; var max = float.MinValue;
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
var u = (double)x / (Size - 1);
var v = (double)y / (Size - 1);
// 整体微倾 + 极缓的起伏
double h = 40 + 22 * u + 8 * Math.Sin(u * 4.2) * Math.Cos(v * 3.6);
var hf = (float)Math.Max(0, h);
Heights[y, x] = hf;
if (hf < min) min = hf;
if (hf > max) max = hf;
}
}
MinHeight = min; MaxHeight = max;
}
private static double SmoothStep(double a, double b, double x)
{
var t = Math.Clamp((x - a) / (b - a), 0, 1);
@@ -151,9 +300,10 @@ public sealed class TerrainField
public List<float> ContourLevels()
{
var levels = new List<float>();
var top = (float)(Math.Floor(MaxHeight / Interval) * Interval);
var bottom = (float)(Math.Ceiling(MinHeight / Interval) * Interval);
for (var h = bottom; h <= top + 0.01f; h += Interval)
var iv = EffectiveInterval;
var top = (float)(Math.Floor(MaxHeight / iv) * iv);
var bottom = (float)(Math.Ceiling(MinHeight / iv) * iv);
for (var h = bottom; h <= top + 0.01f; h += iv)
levels.Add(h);
levels.Sort();
return levels;