fix: display.txt 缺少渲染参数导致盒子不透明
外部引擎(C/C++)直接写 display.txt 时只输出基础物理参数 (DT/NSTEP/method 等),缺少 alpha/ball_color/box_color 等渲染参数,draw.py 读取不到 alpha 回退默认 0.2。 修复: 1. param.json 新增渲染参数(alpha/ball_color/box_color/...) 2. C/C++ 引擎 SimParams 新增对应字段 & JSON 读取 3. C/C++ write_display_txt 写入所有渲染参数 header 4. param.json ball_color/box_color 用数组统一存储
This commit is contained in:
+13
@@ -895,6 +895,19 @@ def run_engine(engine, input_dir, output_dir, config):
|
|||||||
"gravity_strength": float(config.get("gravity_strength", 1.0)),
|
"gravity_strength": float(config.get("gravity_strength", 1.0)),
|
||||||
"driving_force": int(config.get("driving_force", 0)),
|
"driving_force": int(config.get("driving_force", 0)),
|
||||||
"save_trajectory": int(config.get("save_trajectory", 0)),
|
"save_trajectory": int(config.get("save_trajectory", 0)),
|
||||||
|
# 渲染参数(用于 display.txt header)
|
||||||
|
"alpha": config.get("alpha", 0.2),
|
||||||
|
"ball_radius": float(config.get("ball_radius", 0.5)),
|
||||||
|
"ball_color": [float(config.get("ball_color_r", 0.9)),
|
||||||
|
float(config.get("ball_color_g", 0.2)),
|
||||||
|
float(config.get("ball_color_b", 0.2))],
|
||||||
|
"box_color": [float(config.get("box_color_r", 0.8)),
|
||||||
|
float(config.get("box_color_g", 0.8)),
|
||||||
|
float(config.get("box_color_b", 0.85))],
|
||||||
|
"use_marker": int(config.get("use_marker", 0)),
|
||||||
|
"camera_distance": float(config.get("camera_distance", 40.0)),
|
||||||
|
"camera_elevation": float(config.get("camera_elevation", 0)),
|
||||||
|
"camera_azimuth": float(config.get("camera_azimuth", 0)),
|
||||||
}
|
}
|
||||||
param_path = os.path.join(script_dir, "engines", engine, "param.json")
|
param_path = os.path.join(script_dir, "engines", engine, "param.json")
|
||||||
os.makedirs(os.path.dirname(param_path), exist_ok=True)
|
os.makedirs(os.path.dirname(param_path), exist_ok=True)
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ typedef struct {
|
|||||||
double gravity_strength; /* 万有引力强度 */
|
double gravity_strength; /* 万有引力强度 */
|
||||||
int driving_force; /* 驱动力开关 */
|
int driving_force; /* 驱动力开关 */
|
||||||
int save_trajectory; /* 是否保存完整轨迹文件 */
|
int save_trajectory; /* 是否保存完整轨迹文件 */
|
||||||
|
double alpha[6]; /* 盒子透明度 */
|
||||||
|
double ball_radius;
|
||||||
|
double ball_color[3];
|
||||||
|
double box_color[3];
|
||||||
|
int use_marker;
|
||||||
|
double camera_distance, camera_elevation, camera_azimuth;
|
||||||
} SimParams;
|
} SimParams;
|
||||||
|
|
||||||
/* ========================================================================
|
/* ========================================================================
|
||||||
@@ -271,6 +277,20 @@ static void json_read_double3(const char *json, const char *key, double out[3])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void json_read_double6(const char *json, const char *key, double out[6]) {
|
||||||
|
char search[256];
|
||||||
|
snprintf(search, sizeof(search), "\"%s\"", key);
|
||||||
|
const char *p = strstr(json, search);
|
||||||
|
if (!p) { for (int i=0;i<6;i++) out[i]=0; return; }
|
||||||
|
p = strchr(p, '[');
|
||||||
|
if (!p) { for (int i=0;i<6;i++) out[i]=0; return; }
|
||||||
|
p++;
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == ',' || *p == ']') p++;
|
||||||
|
out[i] = strtod(p, (char**)&p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 读取 param.json */
|
/* 读取 param.json */
|
||||||
static int g_gravity_field = 1;
|
static int g_gravity_field = 1;
|
||||||
static int g_gravity_interaction = 0;
|
static int g_gravity_interaction = 0;
|
||||||
@@ -306,6 +326,15 @@ static SimParams read_params(const char *path) {
|
|||||||
p.gravity_strength = json_read_double(buf, "gravity_strength");
|
p.gravity_strength = json_read_double(buf, "gravity_strength");
|
||||||
p.driving_force = json_read_int(buf, "driving_force");
|
p.driving_force = json_read_int(buf, "driving_force");
|
||||||
p.save_trajectory = json_read_int(buf, "save_trajectory");
|
p.save_trajectory = json_read_int(buf, "save_trajectory");
|
||||||
|
/* 渲染参数 */
|
||||||
|
json_read_double6(buf, "alpha", p.alpha);
|
||||||
|
p.ball_radius = json_read_double(buf, "ball_radius");
|
||||||
|
json_read_double3(buf, "ball_color", p.ball_color);
|
||||||
|
json_read_double3(buf, "box_color", p.box_color);
|
||||||
|
p.use_marker = json_read_int(buf, "use_marker");
|
||||||
|
p.camera_distance = json_read_double(buf, "camera_distance");
|
||||||
|
p.camera_elevation = json_read_double(buf, "camera_elevation");
|
||||||
|
p.camera_azimuth = json_read_double(buf, "camera_azimuth");
|
||||||
g_gravity_field = p.gravity_field;
|
g_gravity_field = p.gravity_field;
|
||||||
g_gravity_interaction = p.gravity_interaction;
|
g_gravity_interaction = p.gravity_interaction;
|
||||||
g_elastic_force = p.elastic_force;
|
g_elastic_force = p.elastic_force;
|
||||||
@@ -880,6 +909,21 @@ static void write_display_txt(const char *path, const Trajectory *traj,
|
|||||||
fprintf(f, "dynamic_steps: %d\n", dynamic_steps);
|
fprintf(f, "dynamic_steps: %d\n", dynamic_steps);
|
||||||
fprintf(f, "T_total: %.16g\n", T_total);
|
fprintf(f, "T_total: %.16g\n", T_total);
|
||||||
fprintf(f, "box_a: %.16g\n", params->box_a);
|
fprintf(f, "box_a: %.16g\n", params->box_a);
|
||||||
|
fprintf(f, "alpha: %.16g,%.16g,%.16g,%.16g,%.16g,%.16g\n",
|
||||||
|
params->alpha[0], params->alpha[1], params->alpha[2],
|
||||||
|
params->alpha[3], params->alpha[4], params->alpha[5]);
|
||||||
|
fprintf(f, "ball_radius: %.16g\n", params->ball_radius);
|
||||||
|
fprintf(f, "ball_color_r: %.16g\n", params->ball_color[0]);
|
||||||
|
fprintf(f, "ball_color_g: %.16g\n", params->ball_color[1]);
|
||||||
|
fprintf(f, "ball_color_b: %.16g\n", params->ball_color[2]);
|
||||||
|
fprintf(f, "box_color_r: %.16g\n", params->box_color[0]);
|
||||||
|
fprintf(f, "box_color_g: %.16g\n", params->box_color[1]);
|
||||||
|
fprintf(f, "box_color_b: %.16g\n", params->box_color[2]);
|
||||||
|
fprintf(f, "use_marker: %d\n", params->use_marker);
|
||||||
|
fprintf(f, "camera_distance: %.16g\n", params->camera_distance);
|
||||||
|
fprintf(f, "camera_elevation: %.16g\n", params->camera_elevation);
|
||||||
|
fprintf(f, "camera_azimuth: %.16g\n", params->camera_azimuth);
|
||||||
|
fprintf(f, "\n");
|
||||||
|
|
||||||
if (params->driving_force) {
|
if (params->driving_force) {
|
||||||
fprintf(f, "driving_force: 1\n");
|
fprintf(f, "driving_force: 1\n");
|
||||||
|
|||||||
+45
-1
@@ -44,6 +44,14 @@ struct SimParams {
|
|||||||
double gravity_strength = 1.0;
|
double gravity_strength = 1.0;
|
||||||
int driving_force = 0;
|
int driving_force = 0;
|
||||||
int save_trajectory = 1;
|
int save_trajectory = 1;
|
||||||
|
double alpha[6] = {0,0,0,0,0,0};
|
||||||
|
double ball_radius = 0.5;
|
||||||
|
double ball_color[3] = {0.9, 0.2, 0.2};
|
||||||
|
double box_color[3] = {0.8, 0.8, 0.85};
|
||||||
|
int use_marker = 0;
|
||||||
|
double camera_distance = 40.0;
|
||||||
|
double camera_elevation = 0;
|
||||||
|
double camera_azimuth = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
@@ -152,6 +160,21 @@ static void json_read_double3(const std::string &json, const std::string &key, d
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 读取 JSON 数组到 double[6] */
|
||||||
|
static void json_read_double6(const std::string &json, const std::string &key, double out[6]) {
|
||||||
|
auto pos = json.find("\"" + key + "\"");
|
||||||
|
if (pos == std::string::npos) { for (int i=0;i<6;i++) out[i]=0; return; }
|
||||||
|
pos = json.find('[', pos);
|
||||||
|
if (pos == std::string::npos) { for (int i=0;i<6;i++) out[i]=0; return; }
|
||||||
|
pos++;
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
while (pos < json.size() && (json[pos]==' '||json[pos]=='\t'||json[pos]=='\n'||json[pos]==','||json[pos]==']')) pos++;
|
||||||
|
char *end;
|
||||||
|
out[i] = std::strtod(json.c_str() + pos, &end);
|
||||||
|
pos = end - json.c_str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 解析 param.json */
|
/* 解析 param.json */
|
||||||
static SimParams read_params(const std::string &path) {
|
static SimParams read_params(const std::string &path) {
|
||||||
std::string buf = read_file(path);
|
std::string buf = read_file(path);
|
||||||
@@ -174,6 +197,14 @@ static SimParams read_params(const std::string &path) {
|
|||||||
// save_trajectory 默认 1(全量),仅在 JSON 中存在该 key 时覆盖
|
// save_trajectory 默认 1(全量),仅在 JSON 中存在该 key 时覆盖
|
||||||
if (json_has_key(buf, "save_trajectory"))
|
if (json_has_key(buf, "save_trajectory"))
|
||||||
p.save_trajectory = json_read_int(buf, "save_trajectory");
|
p.save_trajectory = json_read_int(buf, "save_trajectory");
|
||||||
|
json_read_double6(buf, "alpha", p.alpha);
|
||||||
|
p.ball_radius = json_read_double(buf, "ball_radius");
|
||||||
|
json_read_double3(buf, "ball_color", p.ball_color);
|
||||||
|
json_read_double3(buf, "box_color", p.box_color);
|
||||||
|
p.use_marker = json_read_int(buf, "use_marker");
|
||||||
|
p.camera_distance = json_read_double(buf, "camera_distance");
|
||||||
|
p.camera_elevation = json_read_double(buf, "camera_elevation");
|
||||||
|
p.camera_azimuth = json_read_double(buf, "camera_azimuth");
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -717,7 +748,20 @@ static void write_display_txt(
|
|||||||
f << "dynamic_steps: " << (params.NT - params.warmup_steps) << "\n";
|
f << "dynamic_steps: " << (params.NT - params.warmup_steps) << "\n";
|
||||||
f << "T_total: " << T_total << "\n";
|
f << "T_total: " << T_total << "\n";
|
||||||
f << "box_a: " << params.box_a << "\n";
|
f << "box_a: " << params.box_a << "\n";
|
||||||
f << "driving_force: " << params.driving_force << "\n\n";
|
f << "driving_force: " << params.driving_force << "\n";
|
||||||
|
f << "alpha: " << params.alpha[0] << "," << params.alpha[1] << "," << params.alpha[2] << ","
|
||||||
|
<< params.alpha[3] << "," << params.alpha[4] << "," << params.alpha[5] << "\n";
|
||||||
|
f << "ball_radius: " << params.ball_radius << "\n";
|
||||||
|
f << "ball_color_r: " << params.ball_color[0] << "\n";
|
||||||
|
f << "ball_color_g: " << params.ball_color[1] << "\n";
|
||||||
|
f << "ball_color_b: " << params.ball_color[2] << "\n";
|
||||||
|
f << "box_color_r: " << params.box_color[0] << "\n";
|
||||||
|
f << "box_color_g: " << params.box_color[1] << "\n";
|
||||||
|
f << "box_color_b: " << params.box_color[2] << "\n";
|
||||||
|
f << "use_marker: " << params.use_marker << "\n";
|
||||||
|
f << "camera_distance: " << params.camera_distance << "\n";
|
||||||
|
f << "camera_elevation: " << params.camera_elevation << "\n";
|
||||||
|
f << "camera_azimuth: " << params.camera_azimuth << "\n\n";
|
||||||
|
|
||||||
f << std::fixed << std::setprecision(6);
|
f << std::fixed << std::setprecision(6);
|
||||||
for (int t = 0; t < n_steps; t++) {
|
for (int t = 0; t < n_steps; t++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user