Compare commits
3 Commits
waic-hand
...
waic-hand-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf7907ce6f | ||
|
|
466bb5dec7 | ||
|
|
5af3c2862b |
275
scripts/extract_excel_data.py
Normal file
275
scripts/extract_excel_data.py
Normal file
@@ -0,0 +1,275 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
提取 展会数据-H版本.xlsx 中每一组数据并按指定格式输出。
|
||||
|
||||
数据组织:5 个数据组(1#、3#、7#、8#、10#),由空行分隔。
|
||||
每组包含 10 行指标:
|
||||
指标 / 1s 均值 / 240s 输出值 / 240s 均值 / 误差(%) /
|
||||
精度 / b值平均值 / AD值增幅 / AD值漂移 / 增幅/漂移
|
||||
每行有 12 个数值列(C~N),与 指标 行的 12 个挡位一一对应。
|
||||
|
||||
输出格式(示例):
|
||||
1#, 115181, 365308, 485911, ...,
|
||||
3#, 116825, 371568, 475160, ...,
|
||||
|
||||
用法:
|
||||
python extract_excel_data.py [xlsx_path] [选项]
|
||||
--row LABEL 只输出指定指标行(如 "1s 均值"),可多次指定
|
||||
--skip-indicators V 跳过指定挡位(按 指标 行数值匹配),可多次指定
|
||||
--with-title 在每行首部加上组编号(如 "1#, 115181, ...")
|
||||
--raw 保留原始数值(不取整)
|
||||
--annotate 在每行末尾附加 " // 组号 指标名" 注释
|
||||
-o FILE 输出到文件(默认输出到标准输出)
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import openpyxl
|
||||
|
||||
# 默认 Excel 文件路径
|
||||
DEFAULT_XLSX = os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"..",
|
||||
"展会数据-H版本.xlsx",
|
||||
)
|
||||
|
||||
|
||||
def is_blank_row(row_values):
|
||||
"""判断是否为空白行(全为 None 或空字符串)。"""
|
||||
for v in row_values:
|
||||
if v is None:
|
||||
continue
|
||||
if isinstance(v, str) and v.strip() == "":
|
||||
continue
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_group_header(row_values):
|
||||
"""判断是否为组标题行(A 列为类似 '1#' '3#' 的编号)。"""
|
||||
first = row_values[0]
|
||||
if first is None:
|
||||
return False
|
||||
return isinstance(first, str) and first.strip().endswith("#")
|
||||
|
||||
|
||||
def extract_data_rows(ws):
|
||||
"""
|
||||
返回一个列表,每个元素是 (group_name, row_label, [12 个数值])。
|
||||
group_name 为当前组编号;row_label 为 B 列标签;数值列表为 C~N 列。
|
||||
"""
|
||||
results = []
|
||||
current_group = None
|
||||
|
||||
for row in ws.iter_rows(min_row=1, max_row=ws.max_row, values_only=True):
|
||||
row = list(row[:14]) # 仅取前 14 列
|
||||
if is_blank_row(row):
|
||||
continue
|
||||
|
||||
if is_group_header(row):
|
||||
current_group = row[0]
|
||||
label = row[1] if len(row) > 1 and row[1] is not None else "指标"
|
||||
values = row[2:]
|
||||
results.append((current_group, label, values))
|
||||
continue
|
||||
|
||||
label = row[1] if len(row) > 1 and row[1] is not None else ""
|
||||
values = row[2:]
|
||||
if current_group is None:
|
||||
continue
|
||||
results.append((current_group, label, values))
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def parse_skip_indicators(values):
|
||||
"""
|
||||
把命令行传入的 --skip-indicators 值解析为浮点数集合。
|
||||
支持 "0.97" 或 "0.97,1.97" 形式。
|
||||
"""
|
||||
out = set()
|
||||
if not values:
|
||||
return out
|
||||
for raw in values:
|
||||
for token in str(raw).split(","):
|
||||
token = token.strip()
|
||||
if not token:
|
||||
continue
|
||||
try:
|
||||
out.add(float(token))
|
||||
except ValueError:
|
||||
print(f"警告:无法解析指标值 '{token}',已忽略。", file=sys.stderr)
|
||||
return out
|
||||
|
||||
|
||||
def indicators_match(a, b, tol=1e-9):
|
||||
"""浮点数比较(容忍微小误差)。"""
|
||||
if a is None or b is None:
|
||||
return False
|
||||
try:
|
||||
return abs(float(a) - float(b)) < tol
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def format_value(v, raw=False):
|
||||
"""
|
||||
将单元格值格式化为字符串。
|
||||
raw=False: 数值四舍五入到整数(匹配示例样式)。
|
||||
raw=True : 保留原始数值。
|
||||
"""
|
||||
if v is None:
|
||||
return "0"
|
||||
if isinstance(v, bool):
|
||||
return "1" if v else "0"
|
||||
if isinstance(v, (int,)):
|
||||
return str(v)
|
||||
if isinstance(v, float):
|
||||
if raw:
|
||||
if v.is_integer():
|
||||
return str(int(v))
|
||||
return f"{v:g}" if abs(v) >= 1e-4 else repr(v)
|
||||
return str(int(round(v)))
|
||||
s = str(v).strip()
|
||||
if s == "":
|
||||
return "0"
|
||||
try:
|
||||
f = float(s)
|
||||
if raw:
|
||||
if f.is_integer():
|
||||
return str(int(f))
|
||||
return f"{f:g}"
|
||||
return str(int(round(f)))
|
||||
except ValueError:
|
||||
return s
|
||||
|
||||
|
||||
def format_line(values, raw=False):
|
||||
"""把数值列表格式化为 'v1, v2, ..., vn,' 形式。"""
|
||||
return ", ".join(format_value(v, raw=raw) for v in values) + ","
|
||||
|
||||
|
||||
def parse_args():
|
||||
p = argparse.ArgumentParser(
|
||||
description="提取展会数据 Excel 中的每一行数据。",
|
||||
)
|
||||
p.add_argument(
|
||||
"xlsx",
|
||||
nargs="?",
|
||||
default=DEFAULT_XLSX,
|
||||
help="Excel 文件路径(默认: 展会数据-H版本.xlsx)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--row",
|
||||
action="append",
|
||||
default=None,
|
||||
metavar="LABEL",
|
||||
help='只输出指定指标行(如 "1s 均值"),可多次指定。'
|
||||
"未指定时输出全部行。",
|
||||
)
|
||||
p.add_argument(
|
||||
"--skip-indicators",
|
||||
action="append",
|
||||
default=None,
|
||||
metavar="V",
|
||||
help="跳过指定挡位(按 指标 行数值匹配)。可多次指定,"
|
||||
"也支持逗号分隔。例: --skip-indicators 0.97 --skip-indicators 1.97",
|
||||
)
|
||||
p.add_argument(
|
||||
"--with-title",
|
||||
action="store_true",
|
||||
help="在每行首部加上组编号(如 \"1#, 115181, 365308, ...\")",
|
||||
)
|
||||
p.add_argument(
|
||||
"--raw",
|
||||
action="store_true",
|
||||
help="保留原始数值(不取整)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--annotate",
|
||||
action="store_true",
|
||||
help="在每行末尾附加 // 组号 指标名 注释",
|
||||
)
|
||||
p.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
default=None,
|
||||
help="输出文件路径(默认输出到标准输出)",
|
||||
)
|
||||
return p.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
xlsx_path = os.path.abspath(args.xlsx)
|
||||
|
||||
if not os.path.exists(xlsx_path):
|
||||
print(f"错误:找不到文件 {xlsx_path}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
wb = openpyxl.load_workbook(xlsx_path, data_only=True)
|
||||
ws = wb.active
|
||||
|
||||
rows = extract_data_rows(ws)
|
||||
if not rows:
|
||||
print("未找到任何数据。", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# ===== 列筛选(先做,依赖 指标 行) =====
|
||||
skip_set = parse_skip_indicators(args.skip_indicators)
|
||||
if skip_set:
|
||||
filtered = []
|
||||
current_indicators = None
|
||||
for group, label, values in rows:
|
||||
if label == "指标":
|
||||
current_indicators = values
|
||||
if current_indicators is None:
|
||||
# 没有任何组提供 指标 行作对照,原样保留
|
||||
filtered.append((group, label, values))
|
||||
continue
|
||||
kept = [
|
||||
v for v, ind in zip(values, current_indicators)
|
||||
if not any(indicators_match(ind, s) for s in skip_set)
|
||||
]
|
||||
filtered.append((group, label, kept))
|
||||
rows = filtered
|
||||
|
||||
# ===== 行筛选(在列筛选之后) =====
|
||||
if args.row:
|
||||
wanted = {name.strip() for name in args.row}
|
||||
rows = [(g, l, v) for (g, l, v) in rows if l in wanted]
|
||||
if not rows:
|
||||
print(
|
||||
f"未找到匹配的行 {args.row}。可用行名请参考 Excel 文件 B 列。",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
out_lines = []
|
||||
last_group = None
|
||||
for group, label, values in rows:
|
||||
if last_group is not None and group != last_group:
|
||||
out_lines.append("") # 组之间插入空行
|
||||
last_group = group
|
||||
|
||||
line = format_line(values, raw=args.raw)
|
||||
if args.with_title:
|
||||
line = f"{group}, {line}"
|
||||
if args.annotate:
|
||||
line = f"{line} // {group} {label}"
|
||||
out_lines.append(line)
|
||||
|
||||
text = "\n".join(out_lines) + "\n"
|
||||
if args.output:
|
||||
with open(args.output, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
print(f"已写入 {len(rows)} 行数据到 {args.output}", file=sys.stderr)
|
||||
else:
|
||||
sys.stdout.write(text)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
341
src/app.rs
341
src/app.rs
@@ -62,6 +62,7 @@ pub struct EskinDesktopApp {
|
||||
latest_matrix_rows: u32,
|
||||
latest_matrix_cols: u32,
|
||||
last_sample_at: Option<Instant>,
|
||||
last_color_debug_at: Option<Instant>,
|
||||
breakout_visible: bool,
|
||||
breakout_game: BreakoutGame,
|
||||
live_rates: LiveRateMeters,
|
||||
@@ -175,6 +176,7 @@ impl EskinDesktopApp {
|
||||
latest_matrix_rows: MATRIX_ROWS,
|
||||
latest_matrix_cols: MATRIX_COLS,
|
||||
last_sample_at: None,
|
||||
last_color_debug_at: None,
|
||||
breakout_visible: false,
|
||||
breakout_game: BreakoutGame::default(),
|
||||
live_rates: LiveRateMeters::new(),
|
||||
@@ -339,23 +341,17 @@ impl EskinDesktopApp {
|
||||
self.latest_raw_matrix.extend_from_slice(&sample.matrix);
|
||||
self.latest_matrix_rows = sample.rows;
|
||||
self.latest_matrix_cols = sample.cols;
|
||||
self.log_hand_color_mapping_debug(&sample.matrix);
|
||||
|
||||
self.analyze_spatial_force(&sample.matrix);
|
||||
|
||||
// Keep JE-Skin's summary path separate from the optional spatial-force vector.
|
||||
let raw_total = sample
|
||||
.matrix
|
||||
.iter()
|
||||
.fold(0_u64, |sum, value| sum + *value as u64)
|
||||
.min(u32::MAX as u64) as u32;
|
||||
let force = raw_to_g1(raw_total).min(25.6);
|
||||
let force = if force <= 0.1 { 0.0 } else { force };
|
||||
let force =
|
||||
update_hand_signal_histories(&mut self.hand_signal_histories, &sample.matrix)
|
||||
.min(25.6);
|
||||
self.signal_history.push(force);
|
||||
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
|
||||
self.signal_history.remove(0);
|
||||
}
|
||||
|
||||
update_hand_signal_histories(&mut self.hand_signal_histories, &sample.matrix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -587,6 +583,7 @@ impl EskinDesktopApp {
|
||||
self.pressure_matrix.fill([0.0, 0.0]);
|
||||
self.hand_pressure.clear();
|
||||
self.last_sample_at = None;
|
||||
self.last_color_debug_at = None;
|
||||
self.force_estimator.reset();
|
||||
self.latest_spatial_force = None;
|
||||
self.latest_hand_spatial_forces = [None; HAND_FINGERTIP_COUNT];
|
||||
@@ -606,6 +603,50 @@ impl EskinDesktopApp {
|
||||
SerialMode::Hand => ActiveMode::Hand(HandGatewayMode { range: 0..7000 }),
|
||||
}
|
||||
}
|
||||
|
||||
fn log_hand_color_mapping_debug(&mut self, raw: &[u32]) {
|
||||
let now = Instant::now();
|
||||
if self
|
||||
.last_color_debug_at
|
||||
.is_some_and(|last| now.duration_since(last) < Duration::from_secs(1))
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.last_color_debug_at = Some(now);
|
||||
|
||||
let mut parts = Vec::with_capacity(HAND_FORCE_PANEL_COUNT);
|
||||
let mut start = 0;
|
||||
for (panel_index, (range, cell_count)) in HAND_SENSOR_PANEL_COLOR_RANGES
|
||||
.iter()
|
||||
.copied()
|
||||
.zip(HAND_SENSOR_PANEL_CELL_COUNTS)
|
||||
.enumerate()
|
||||
{
|
||||
let end = start + cell_count;
|
||||
let peak = raw
|
||||
.get(start..end)
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.copied()
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
let cell_range = range.per_cell(cell_count);
|
||||
let [intensity, _] = cell_range.normalize(peak);
|
||||
parts.push(format!(
|
||||
"{} raw_peak={} range_total_max={:.0} cell_range_max={:.0} intensity={:.3}",
|
||||
match panel_index {
|
||||
0..=4 => format!("F{}", panel_index + 1),
|
||||
5 => "Palm-H".to_owned(),
|
||||
_ => "Palm-V".to_owned(),
|
||||
},
|
||||
peak,
|
||||
range.max,
|
||||
cell_range.max,
|
||||
intensity
|
||||
));
|
||||
start = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn hand_image_pixel_to_screen(rect: egui::Rect, point_px: [f32; 2]) -> egui::Pos2 {
|
||||
@@ -665,26 +706,23 @@ fn paint_spatial_force_arrow(
|
||||
fn update_hand_signal_histories(
|
||||
histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
|
||||
raw_values: &[u32],
|
||||
) {
|
||||
) -> f32 {
|
||||
let mut offset = 0usize;
|
||||
for (index, (history, sample_count)) in histories.iter_mut().zip(HAND_FORCE_SEGMENT_COUNTS).enumerate() {
|
||||
let mut total_force = 0.0;
|
||||
for (index, (history, sample_count)) in histories
|
||||
.iter_mut()
|
||||
.zip(HAND_FORCE_SEGMENT_COUNTS)
|
||||
.enumerate()
|
||||
{
|
||||
let raw_total = raw_values
|
||||
.get(offset..offset + sample_count)
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.fold(0_u64, |sum, value| sum + *value as u64)
|
||||
.min(u32::MAX as u64) as u32;
|
||||
let force = match index {
|
||||
0 => raw_to_g1(raw_total).min(20.6),
|
||||
1 => raw_to_g2(raw_total).min(20.6),
|
||||
2 => raw_to_g3(raw_total).min(20.6),
|
||||
3 => raw_to_g4(raw_total).min(20.6),
|
||||
4 => raw_to_g5(raw_total).min(20.6),
|
||||
_ => raw_to_gd(raw_total).min(20.6),
|
||||
};
|
||||
// let force = raw_to_g1(raw_total).min(25.6);
|
||||
let force = raw_to_hand_segment_g(index, raw_total).min(20.6);
|
||||
let force = if force <= 0.1 { 0.0 } else { force };
|
||||
|
||||
total_force += force;
|
||||
history.push(force);
|
||||
if history.len() > SUMMARY_POINTS_PER_SERIES {
|
||||
history.remove(0);
|
||||
@@ -692,14 +730,29 @@ fn update_hand_signal_histories(
|
||||
|
||||
offset += sample_count;
|
||||
}
|
||||
|
||||
total_force
|
||||
}
|
||||
|
||||
fn raw_to_hand_segment_g(index: usize, raw: u32) -> f32 {
|
||||
match index {
|
||||
0 => raw_to_g1(raw),
|
||||
1 => raw_to_g2(raw),
|
||||
2 => raw_to_g3(raw),
|
||||
3 => raw_to_g4(raw),
|
||||
4 => raw_to_g5(raw),
|
||||
// 5 => raw_to_g6(raw),
|
||||
// 6 => raw_to_g7(raw),
|
||||
_ => raw_to_gd(raw),
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_to_gd(raw: u32) -> f32 {
|
||||
const RAW: [u32; 12] = [
|
||||
0, 21382, 108507, 123183, 147405, 171105, 192395, 250443, 231423, 350560, 396616, 429444,
|
||||
const RAW: [u32; 11] = [
|
||||
0, 486, 5310, 8130, 10005, 12883, 14700, 18141, 19877, 26270, 32648,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 12] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0, 2557.0,
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
];
|
||||
|
||||
if raw <= RAW[0] {
|
||||
@@ -731,7 +784,7 @@ fn raw_to_gd(raw: u32) -> f32 {
|
||||
|
||||
fn raw_to_g1(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 20239, 121454, 171532, 223968, 241467, 293500, 322744, 436294, 523275, 633083,
|
||||
0, 106443, 365678, 449286, 532425, 608187, 685522, 797058, 886997, 1082570, 1244793,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
@@ -766,7 +819,7 @@ fn raw_to_g1(raw: u32) -> f32 {
|
||||
|
||||
fn raw_to_g2(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 5398, 52896, 86945, 122546, 145001, 165933, 220575, 241578, 350285, 449530,
|
||||
0, 111143, 310153, 375593, 427920, 497433, 548676, 648392, 715118, 877487, 994887,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
@@ -801,7 +854,7 @@ fn raw_to_g2(raw: u32) -> f32 {
|
||||
|
||||
fn raw_to_g3(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 4920, 35945, 46499, 65566, 69613, 89971, 122321, 146384, 203426, 269474,
|
||||
0, 66898, 271692, 354094, 443663, 534994, 603246, 721942, 837520, 1030111, 1216809,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
@@ -836,7 +889,7 @@ fn raw_to_g3(raw: u32) -> f32 {
|
||||
|
||||
fn raw_to_g4(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 11346, 92229, 139822, 182282, 220004, 259898, 307303, 377922, 460755, 587834,
|
||||
0, 122337, 381708, 480527, 577445, 677301, 762306, 906751, 1019182, 1254601, 1415051,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
@@ -870,6 +923,76 @@ fn raw_to_g4(raw: u32) -> f32 {
|
||||
}
|
||||
|
||||
fn raw_to_g5(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 82466, 287752, 356167, 433286, 506855, 604014, 671789, 770692, 887287, 1032058,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
];
|
||||
|
||||
if raw <= RAW[0] {
|
||||
return FORCE_CENTI_N[0] / 100.0;
|
||||
}
|
||||
|
||||
if raw >= RAW[RAW.len() - 1] {
|
||||
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
|
||||
}
|
||||
|
||||
let mut left = 0usize;
|
||||
let mut right = RAW.len() - 1;
|
||||
while left + 1 < right {
|
||||
let mid = (left + right) / 2;
|
||||
if RAW[mid] <= raw {
|
||||
left = mid;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
|
||||
let raw_left = RAW[left] as f32;
|
||||
let raw_right = RAW[right] as f32;
|
||||
let span = (raw_right - raw_left).max(1.0);
|
||||
let ratio = (raw as f32 - raw_left) / span;
|
||||
|
||||
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
|
||||
}
|
||||
|
||||
fn raw_to_g6(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 0, 4290, 8096, 10567, 14856, 19685, 43856, 84675, 114001, 129068,
|
||||
];
|
||||
const FORCE_CENTI_N: [f32; 11] = [
|
||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
||||
];
|
||||
|
||||
if raw <= RAW[0] {
|
||||
return FORCE_CENTI_N[0] / 100.0;
|
||||
}
|
||||
|
||||
if raw >= RAW[RAW.len() - 1] {
|
||||
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
|
||||
}
|
||||
|
||||
let mut left = 0usize;
|
||||
let mut right = RAW.len() - 1;
|
||||
while left + 1 < right {
|
||||
let mid = (left + right) / 2;
|
||||
if RAW[mid] <= raw {
|
||||
left = mid;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
|
||||
let raw_left = RAW[left] as f32;
|
||||
let raw_right = RAW[right] as f32;
|
||||
let span = (raw_right - raw_left).max(1.0);
|
||||
let ratio = (raw as f32 - raw_left) / span;
|
||||
|
||||
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
|
||||
}
|
||||
|
||||
fn raw_to_g7(raw: u32) -> f32 {
|
||||
const RAW: [u32; 11] = [
|
||||
0, 10081, 40910, 49944, 64291, 68299, 79282, 97767, 113567, 140520, 167132,
|
||||
];
|
||||
@@ -952,6 +1075,79 @@ fn split_viewport_body(rect: egui::Rect) -> egui::Rect {
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct PressureColorRange {
|
||||
min: f32,
|
||||
max: f32,
|
||||
gamma: f32,
|
||||
}
|
||||
|
||||
// The shader begins its orange-to-red transition at intensity 0.66. With this
|
||||
// gamma, a raw cell value at 80% of its configured range maps to that point.
|
||||
const PRESSURE_COLOR_GAMMA: f32 = 1.5;
|
||||
|
||||
impl PressureColorRange {
|
||||
const fn new(min: u32, max: u32) -> Self {
|
||||
Self {
|
||||
min: min as f32,
|
||||
max: max as f32,
|
||||
gamma: PRESSURE_COLOR_GAMMA,
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(self, value: u32) -> [f32; 2] {
|
||||
let raw = value as f32;
|
||||
let span = (self.max - self.min).max(1.0);
|
||||
let mapped = ((raw - self.min) / span).clamp(0.0, 1.0);
|
||||
|
||||
let intensity = if raw <= self.min + 4.0 {
|
||||
0.0
|
||||
} else {
|
||||
mapped.powf(self.gamma)
|
||||
};
|
||||
|
||||
let display_value = if raw <= self.min + 4.0 {
|
||||
0.0
|
||||
} else {
|
||||
raw.round().min(9999.0)
|
||||
};
|
||||
|
||||
[intensity, display_value]
|
||||
}
|
||||
|
||||
fn per_cell(self, cell_count: usize) -> Self {
|
||||
let cells = cell_count.max(1) as f32;
|
||||
Self {
|
||||
min: self.min / cells,
|
||||
max: self.max / cells,
|
||||
gamma: self.gamma,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_COLOR_RANGE: PressureColorRange = PressureColorRange::new(0, 7000);
|
||||
|
||||
const HAND_SENSOR_PANEL_COLOR_RANGES: [PressureColorRange; HAND_FORCE_PANEL_COUNT] = [
|
||||
PressureColorRange::new(0, 1244793),
|
||||
PressureColorRange::new(0, 994887),
|
||||
PressureColorRange::new(0, 1216809),
|
||||
PressureColorRange::new(0, 1415051),
|
||||
PressureColorRange::new(0, 1032058),
|
||||
PressureColorRange::new(0, 32648),
|
||||
PressureColorRange::new(0, 32648),
|
||||
];
|
||||
|
||||
const HAND_FINGER_SENSOR_CELLS: usize = 12 * 7;
|
||||
const HAND_SENSOR_PANEL_CELL_COUNTS: [usize; HAND_FORCE_PANEL_COUNT] = [
|
||||
HAND_FINGER_SENSOR_CELLS,
|
||||
HAND_FINGER_SENSOR_CELLS,
|
||||
HAND_FINGER_SENSOR_CELLS,
|
||||
HAND_FINGER_SENSOR_CELLS,
|
||||
HAND_FINGER_SENSOR_CELLS,
|
||||
5 * 14,
|
||||
11 * 4,
|
||||
];
|
||||
|
||||
fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut PressureFrame) {
|
||||
normalized.fill([0.0, 0.0]);
|
||||
|
||||
@@ -971,28 +1167,37 @@ fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut
|
||||
}
|
||||
|
||||
fn normalize_pressure_samples(raw: &[u32]) -> PressureSamples {
|
||||
raw.iter().copied().map(normalize_pressure_value).collect()
|
||||
raw.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.map(|(index, value)| {
|
||||
hand_sensor_panel_for_index(index)
|
||||
.map(|(range, cell_count)| range.per_cell(cell_count).normalize(value))
|
||||
.unwrap_or_else(|| DEFAULT_COLOR_RANGE.normalize(value))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn hand_sensor_panel_for_index(index: usize) -> Option<(PressureColorRange, usize)> {
|
||||
let mut start = 0;
|
||||
|
||||
for (range, cell_count) in HAND_SENSOR_PANEL_COLOR_RANGES
|
||||
.iter()
|
||||
.copied()
|
||||
.zip(HAND_SENSOR_PANEL_CELL_COUNTS)
|
||||
{
|
||||
let end = start + cell_count;
|
||||
if index < end {
|
||||
return Some((range, cell_count));
|
||||
}
|
||||
start = end;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn normalize_pressure_value(value: u32) -> [f32; 2] {
|
||||
const RANGE_MIN: f32 = 0.0;
|
||||
const RANGE_MAX: f32 = 7000.0;
|
||||
const DISPLAY_GAMMA: f32 = 0.45;
|
||||
|
||||
let raw_value = value as f32;
|
||||
let mapped = ((raw_value - RANGE_MIN) / (RANGE_MAX - RANGE_MIN)).clamp(0.0, 1.0);
|
||||
let intensity = if raw_value <= RANGE_MIN + 4.0 {
|
||||
0.0
|
||||
} else {
|
||||
mapped.powf(DISPLAY_GAMMA)
|
||||
};
|
||||
let display_value = if raw_value <= RANGE_MIN + 4.0 {
|
||||
0.0
|
||||
} else {
|
||||
raw_value.round().min(9999.0)
|
||||
};
|
||||
|
||||
[intensity, display_value]
|
||||
DEFAULT_COLOR_RANGE.normalize(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1007,8 +1212,8 @@ mod tests {
|
||||
|
||||
let [low_intensity, low_label] = normalize_pressure_value(100);
|
||||
assert!(
|
||||
low_intensity > 0.10,
|
||||
"low hand-gateway values should be visible in the wgpu dot matrix"
|
||||
low_intensity > 0.0,
|
||||
"low hand-gateway values should retain a non-zero response"
|
||||
);
|
||||
assert_eq!(low_label, 100.0);
|
||||
|
||||
@@ -1016,6 +1221,40 @@ mod tests {
|
||||
assert_eq!(max_intensity, 1.0);
|
||||
assert_eq!(max_label, 7000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pressure_normalization_starts_red_transition_at_eighty_percent() {
|
||||
let range = PressureColorRange::new(0, 10_000);
|
||||
let [intensity, _] = range.normalize(8_000);
|
||||
|
||||
assert!(
|
||||
(intensity - 0.66).abs() < 0.002,
|
||||
"80% of the configured raw range should map to the shader's red-transition point"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hand_panel_ranges_cover_both_palm_boards() {
|
||||
let total_cells: usize = HAND_SENSOR_PANEL_CELL_COUNTS.iter().sum();
|
||||
let mut raw = vec![0; total_cells];
|
||||
let mut start = 0;
|
||||
|
||||
for (range, cell_count) in HAND_SENSOR_PANEL_COLOR_RANGES
|
||||
.iter()
|
||||
.copied()
|
||||
.zip(HAND_SENSOR_PANEL_CELL_COUNTS)
|
||||
{
|
||||
raw[start] = (range.max / cell_count as f32).ceil() as u32;
|
||||
start += cell_count;
|
||||
}
|
||||
|
||||
let normalized = normalize_pressure_samples(&raw);
|
||||
let palm_horizontal_start = HAND_SENSOR_PANEL_CELL_COUNTS[..5].iter().sum::<usize>();
|
||||
let palm_vertical_start = palm_horizontal_start + HAND_SENSOR_PANEL_CELL_COUNTS[5];
|
||||
|
||||
assert_eq!(normalized[palm_horizontal_start][0], 1.0);
|
||||
assert_eq!(normalized[palm_vertical_start][0], 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for EskinDesktopApp {
|
||||
|
||||
@@ -44,22 +44,22 @@ pub struct HandGatewayMode {
|
||||
|
||||
const HAND_TIP_MATRICES: [HandTipMatrix; 5] = [
|
||||
HandTipMatrix {
|
||||
center_px: [265.0, 495.0],
|
||||
center_px: [263.5, 495.0],
|
||||
size_px: [70.0, 120.0],
|
||||
angle_rad: -0.40,
|
||||
},
|
||||
HandTipMatrix {
|
||||
center_px: [359.0, 260.0],
|
||||
center_px: [362.0, 260.0],
|
||||
size_px: [70.0, 120.0],
|
||||
angle_rad: -0.17,
|
||||
angle_rad: -0.158,
|
||||
},
|
||||
HandTipMatrix {
|
||||
center_px: [482.0, 228.0],
|
||||
center_px: [480.5, 228.0],
|
||||
size_px: [70.0, 120.0],
|
||||
angle_rad: -0.03,
|
||||
angle_rad: -0.02,
|
||||
},
|
||||
HandTipMatrix {
|
||||
center_px: [596.0, 265.0],
|
||||
center_px: [594.0, 265.0],
|
||||
size_px: [70.0, 120.0],
|
||||
angle_rad: 0.10,
|
||||
},
|
||||
@@ -532,12 +532,13 @@ impl BackgroundRenderResources {
|
||||
let hand_membrane_instances = build_hand_membrane_instances(
|
||||
hand_image_texture.width as f32,
|
||||
hand_image_texture.height as f32,
|
||||
&[],
|
||||
);
|
||||
let hand_membrane_instance_buffer =
|
||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Hand Fingertip Membrane Instance Buffer"),
|
||||
contents: bytemuck::cast_slice(&hand_membrane_instances),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
||||
});
|
||||
let hand_dot_instances = build_hand_dot_instances(
|
||||
rows,
|
||||
@@ -654,6 +655,17 @@ impl BackgroundRenderResources {
|
||||
hand_pressure
|
||||
};
|
||||
|
||||
self.hand_membrane_instances = build_hand_membrane_instances(
|
||||
self.hand_image_texture.width as f32,
|
||||
self.hand_image_texture.height as f32,
|
||||
hand_pressure,
|
||||
);
|
||||
queue.write_buffer(
|
||||
&self.hand_membrane_instance_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&self.hand_membrane_instances),
|
||||
);
|
||||
|
||||
// Hand mode uses UV-anchored fingertip matrices over hand.png.
|
||||
// Rebuild their instance positions here so pressure colors update every frame.
|
||||
self.hand_dot_instances = build_hand_dot_instances(
|
||||
@@ -1139,24 +1151,48 @@ fn create_hand_palm_dot_pipeline(
|
||||
})
|
||||
}
|
||||
|
||||
fn build_hand_membrane_instances(image_width: f32, image_height: f32) -> Vec<GlyphInstance> {
|
||||
fn build_hand_membrane_instances(
|
||||
image_width: f32,
|
||||
image_height: f32,
|
||||
pressure: &[[f32; 2]],
|
||||
) -> Vec<GlyphInstance> {
|
||||
HAND_TIP_MATRICES
|
||||
.iter()
|
||||
.map(|tip| {
|
||||
.enumerate()
|
||||
.map(|(tip_index, tip)| {
|
||||
let uv_x = tip.center_px[0] / image_width.max(1.0);
|
||||
let uv_y = tip.center_px[1] / image_height.max(1.0);
|
||||
let membrane_size = [tip.size_px[0] * 1.44, tip.size_px[1] * 1.36];
|
||||
let tip_intensity = hand_tip_peak_intensity(pressure, tip_index);
|
||||
|
||||
GlyphInstance {
|
||||
// Membrane shaders read xy as hand.png UV.
|
||||
world_position: [uv_x, uv_y, 0.0, 1.0],
|
||||
// Store angle and an oversized source-image pixel size for the floating sensor film.
|
||||
style: [tip.angle_rad, membrane_size[0], membrane_size[1], 0.0],
|
||||
// Store angle, source-image pixel size, and the live fingertip peak intensity.
|
||||
style: [
|
||||
tip.angle_rad,
|
||||
membrane_size[0],
|
||||
membrane_size[1],
|
||||
tip_intensity,
|
||||
],
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn hand_tip_peak_intensity(pressure: &[[f32; 2]], tip_index: usize) -> f32 {
|
||||
let start = tip_index * HAND_FINGER_SENSOR_CELLS;
|
||||
let end = start + HAND_FINGER_SENSOR_CELLS;
|
||||
|
||||
pressure
|
||||
.get(start..end)
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.map(|sample| sample[0])
|
||||
.fold(0.0, f32::max)
|
||||
.clamp(0.0, 1.0)
|
||||
}
|
||||
|
||||
fn build_hand_palm_chip_instances(image_width: f32, image_height: f32) -> Vec<GlyphInstance> {
|
||||
HAND_PALM_CHIPS
|
||||
.iter()
|
||||
|
||||
@@ -38,8 +38,9 @@ pub fn load_texture(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
) -> anyhow::Result<texture::Texture> {
|
||||
let data = load_binary(file_name)?;
|
||||
texture::Texture::from_bytes(device, queue, &data, file_name)
|
||||
// let data = load_binary(file_name)?;
|
||||
let data = include_bytes!("../res/hand.png");
|
||||
texture::Texture::from_bytes(device, queue, data, file_name)
|
||||
}
|
||||
|
||||
pub fn load_model(
|
||||
|
||||
@@ -5,7 +5,7 @@ const SENSOR_COUNT: usize = SENSOR_ROWS * SENSOR_COLS;
|
||||
const TOTAL_PRESSURE_LOW_THRESHOLD: f32 = 500.0;
|
||||
const COP_STABILITY_FRAMES_REQUIRED: usize = 15;
|
||||
|
||||
const POST_INIT_WINDOW_CNT: usize = 100;
|
||||
const POST_INIT_WINDOW_CNT: usize = 50;
|
||||
const POST_INIT_STABLE_CNT: usize = 50;
|
||||
const POST_INIT_STABLE_THRESH: f32 = 0.1;
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ fn sample_range_color(value: f32) -> vec3f {
|
||||
return mix(range_stop_color(1u), range_stop_color(2u), local);
|
||||
}
|
||||
|
||||
let local = smoothstep(0.66, 1.0, t);
|
||||
let local = smoothstep(0.66, 0.92, t);
|
||||
return mix(range_stop_color(2u), range_stop_color(3u), local);
|
||||
}
|
||||
|
||||
@@ -368,6 +368,7 @@ fn hand_image_uv_to_clip(image_uv: vec2f) -> vec2f {
|
||||
struct HandMembraneVertexOutput {
|
||||
@builtin(position) clip_position: vec4f,
|
||||
@location(0) local: vec2f,
|
||||
@location(1) intensity: f32,
|
||||
}
|
||||
|
||||
fn rotate_2d(point: vec2f, angle: f32) -> vec2f {
|
||||
@@ -417,12 +418,17 @@ fn vs_hand_membrane(vertex: DotVertexInput, instance: DotInstanceInput) -> HandM
|
||||
var out: HandMembraneVertexOutput;
|
||||
out.clip_position = vec4f(hand_image_uv_to_clip(image_uv), 0.0, 1.0);
|
||||
out.local = vertex.local;
|
||||
out.intensity = saturate(instance.style.w);
|
||||
return out;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_hand_membrane(in: HandMembraneVertexOutput) -> @location(0) vec4f {
|
||||
let p = in.local;
|
||||
let live = saturate(in.intensity);
|
||||
let response = smoothstep(0.03, 0.96, live);
|
||||
let hot = smoothstep(0.72, 0.96, live);
|
||||
let live_color = sample_range_color(live);
|
||||
|
||||
let halo_shape = fingertip_film_alpha(p, 0.72, -0.20, 0.055);
|
||||
let panel = fingertip_film_alpha(p, 0.66, -0.22, 0.035);
|
||||
@@ -472,20 +478,21 @@ fn fs_hand_membrane(in: HandMembraneVertexOutput) -> @location(0) vec4f {
|
||||
|
||||
let glass_base = vec3f(0.006, 0.055, 0.105);
|
||||
let glass_cyan = vec3f(0.025, 0.42, 0.58);
|
||||
let rim_color = vec3f(0.10, 0.88, 1.00);
|
||||
let glass_live = mix(glass_cyan, live_color * 0.58, response);
|
||||
let rim_color = mix(vec3f(0.10, 0.88, 1.00), live_color, response);
|
||||
let extrusion_color = vec3f(0.004, 0.080, 0.110);
|
||||
let extrusion_edge_color = vec3f(0.015, 0.30, 0.38);
|
||||
let bevel_highlight = vec3f(0.48, 1.00, 1.00);
|
||||
let extrusion_edge_color = mix(vec3f(0.015, 0.30, 0.38), live_color * 0.72, response);
|
||||
let bevel_highlight = mix(vec3f(0.48, 1.00, 1.00), live_color, response * 0.82);
|
||||
let bevel_dark_color = vec3f(0.004, 0.035, 0.060);
|
||||
let dot_color = vec3f(0.08, 0.48, 0.58);
|
||||
let pressure_color = dot_color;
|
||||
let pressure_hot_color = dot_color;
|
||||
let dot_color = mix(vec3f(0.08, 0.48, 0.58), live_color, response);
|
||||
let pressure_color = mix(dot_color, live_color, response);
|
||||
let pressure_hot_color = live_color;
|
||||
|
||||
let color = extrusion_color * extrusion * 0.90
|
||||
let membrane_color = extrusion_color * extrusion * 0.90
|
||||
+ extrusion_edge_color * extrusion * halo_shape * 0.32
|
||||
+ glass_base * panel * 0.26
|
||||
+ glass_cyan * edge_fade * 0.46
|
||||
+ glass_cyan * broad_bevel * 0.24
|
||||
+ glass_live * edge_fade * 0.46
|
||||
+ glass_live * broad_bevel * 0.24
|
||||
+ rim_color * rim * 0.88
|
||||
+ bevel_highlight * bevel_light * 1.35
|
||||
+ bevel_dark_color * bevel_dark * 0.95
|
||||
@@ -495,6 +502,10 @@ fn fs_hand_membrane(in: HandMembraneVertexOutput) -> @location(0) vec4f {
|
||||
+ pressure_color * dots * pressure * 0.82
|
||||
+ pressure_hot_color * dots * pressure_hot * 0.90;
|
||||
|
||||
let live_wash = live_color * response * (inner * 0.12 + dots * 0.72 + rim * 0.30)
|
||||
+ live_color * hot * (inner * 0.08 + dots * 0.22);
|
||||
let color = membrane_color + live_wash;
|
||||
|
||||
let alpha = clamp(
|
||||
extrusion * 0.44
|
||||
+ panel * 0.055
|
||||
@@ -508,9 +519,9 @@ fn fs_hand_membrane(in: HandMembraneVertexOutput) -> @location(0) vec4f {
|
||||
+ dots * pressure * 0.10,
|
||||
0.0,
|
||||
0.90,
|
||||
);
|
||||
) + response * (dots * 0.07 + rim * 0.035) + hot * dots * 0.04;
|
||||
|
||||
return output_color(color, alpha);
|
||||
return output_color(color, clamp(alpha, 0.0, 0.96));
|
||||
}
|
||||
|
||||
@vertex
|
||||
@@ -593,7 +604,9 @@ fn vs_hand_palm_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVe
|
||||
let shaped = smoothstep(0.0, 1.0, intensity);
|
||||
|
||||
let center = hand_image_uv_to_clip(instance.world_position.xy);
|
||||
let pixel_size = u.glyph.x * mix(0.22, 0.34, shaped);
|
||||
// Palm boards have more tightly packed cells than the fingertips, so use
|
||||
// a larger circular mask to keep each sensor visibly readable.
|
||||
let pixel_size = u.glyph.x * mix(0.40, 0.54, shaped);
|
||||
let ndc_offset = vertex.local * vec2f(pixel_size / u.viewport.x, pixel_size / u.viewport.y) * 2.0;
|
||||
|
||||
var out: DotVertexOutput;
|
||||
|
||||
Reference in New Issue
Block a user