ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
format_utils.h
浏览该文件的文档.
1 #pragma once
2 #include "vde/core/point.h"
3 #include <string>
4 #include <sstream>
5 #include <iomanip>
6 #include <cmath>
7 #include <cstdio>
8 
9 namespace vde::foundation {
10 
31 inline std::string fmt_double(double v, int precision = 12) {
32  if (std::isnan(v)) return "0.0";
33  if (std::isinf(v)) return v > 0 ? "1e30" : "-1e30";
34  std::ostringstream ss;
35  ss << std::setprecision(precision) << std::scientific << v;
36  std::string s = ss.str();
37  // Ensure always has exponent sign (e.g., "1.0e+00" not "1.0e00")
38  auto epos = s.find('e');
39  if (epos != std::string::npos) {
40  if (epos + 1 < s.size() && s[epos + 1] != '-' && s[epos + 1] != '+') {
41  s.insert(epos + 1, "+");
42  }
43  }
44  return s;
45 }
46 
63 inline std::string fmt_iges_real(double v, int /*precision*/ = 12) {
64  auto s = fmt_double(v, 12);
65  auto pos = s.find('e');
66  if (pos != std::string::npos) s[pos] = 'D';
67  return s;
68 }
69 
88 inline std::string fmt_point(const core::Point3D& p, bool use_d = false) {
89  using FmtFn = std::string (*)(double, int);
90  FmtFn fn = use_d ? static_cast<FmtFn>(fmt_iges_real) : fmt_double;
91  return fn(p.x(), 12) + "," + fn(p.y(), 12) + "," + fn(p.z(), 12);
92 }
93 
105 inline std::string pad72(const std::string& s) {
106  std::string result = s;
107  if (result.size() > 72) result.resize(72);
108  result.resize(72, ' ');
109  return result;
110 }
111 
130 inline std::string format_iges_line(const std::string& data, char section_char, int seq) {
131  std::string line = pad72(data);
132  char seq_buf[9];
133  std::snprintf(seq_buf, sizeof(seq_buf), "%c%07d", section_char, seq);
134  line += std::string(seq_buf);
135  return line + "\n";
136 }
137 
138 } // namespace vde::foundation
std::string pad72(const std::string &s)
将字符串填充到恰好 72 个字符
Definition: format_utils.h:105
std::string fmt_point(const core::Point3D &p, bool use_d=false)
格式化三维点为逗号分隔字符串
Definition: format_utils.h:88
std::string format_iges_line(const std::string &data, char section_char, int seq)
格式化 IGES 段落行
Definition: format_utils.h:130
std::string fmt_iges_real(double v, int=12)
IGES 格式实数输出
Definition: format_utils.h:63
std::string fmt_double(double v, int precision=12)
以 CAD 级别精度格式化双精度浮点数
Definition: format_utils.h:31
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
自适应双精度谓词后端(默认)
Definition: error_codes.h:4