dsi 离散光滑插值蒜贩,生成函数c++实现
- User
dsi 离散光滑插值蒜贩,生成函数c++实现
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
dsi 离散光滑插值蒜贩,生成函数c++实现
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
好的,我们来实现一个离散光滑插值(Discrete Smooth Interpolation, DSI)的例子。在计算机图形学和相关领域,"离散光滑插值" 通常指的是使用样条(Splines)来连接一系列离散的点,以获得平滑的曲线。
一种非常常用且效果不错的DSI方法是 Catmull-Rom 样条。它是一种三次 Hermite 样条,并且可以确保样条曲线通过所有的控制点。它需要四个连续的点来定义曲线段(前一点、当前点、下一 त्याचा点、下下个点)。
下面是用 C++ 实现 Catmull-Rom 样条插值的代码。我们将实现一个函数,它接收一个点序列,并返回一个包含更多采样点的、更平滑的序列。
请注意: "蒜贩" (suàn fàn - garlic vendor) 这个词似乎与离散光滑插值无关,很可能是一个输入错误或误解。我们将专注于实现 DSI(离散光滑插值)。
1. 数据结构定义
首先,我们需要一个结构来表示二维点(或其他维度,这里以二维为例)。
#include <vector>
#include <cmath>
#include <iostream> // 用于示例输出
#include <iomanip> // 用于格式化输出
// 定义一个二维点结构
struct Point {
double x, y;
// 方便输出
friend std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
};
2. Catmull-Rom 插值核心函数
这个函数根据 Catmull-Rom 公式计算给定 t 值(0到1之间)处的插值点。
P(t) = 0.5 * [ (2*P1) + (-P0 + P2)*t + (2*P0 - 5*P1 + 4*P2 - P3)*t^2 + (-P0 + 3*P1 - 3*P2 + P3)*t^3 ]
// Catmull-Rom 插值函数
// p0: 前一个点
// p1: 当前点 (曲线会经过这里)
// p2: 下一个点 (曲线会经过这里)
// p3: 再下一个点
// t: 插值参数 (0.0 <= t <= 1.0)
Point catmullRomInterpolation(const Point& p0, const Point& p1, const Point& p2, const Point& p3, double t) {
Point result;
double t2 = t * t;
double t3 = t2 * t;
// 计算 X 分量
double x0 = p0.x, x1 = p1.x, x2 = p2.x, x3 = p3.x;
double tx = (-0.5 * x0 + 1.5 * x1 - 1.5 * x2 + 0.5 * x3) * t3
+ (1.0 * x0 - 2.5 * x1 + 2.0 * x2 - 0.5 * x3) * t2
+ (-0.5 * x0 + 0.5 * x2) * t
+ x1;
result.x = tx;
// 计算 Y 分量
double y0 = p0.y, y1 = p1.y, y2 = p2.y, y3 = p3.y;
double ty = (-0.5 * y0 + 1.5 * y1 - 1.5 * y2 + 0.5 * y3) * t3
+ (1.0 * y0 - 2.5 * y1 + 2.0 * y2 - 0.5 * y3) * t2
+ (-0.5 * y0 + 0.5 * y2) * t
+ y1;
result.y = ty;
/*
// 另一种写法,直接套用原始公式 (可能更易理解)
double c0x = 2.0 * p1.x;
double c1x = (-p0.x + p2.x);
double c2x = (2.0 * p0.x - 5.0 * p1.x + 4.0 * p2.x - p3.x);
double c3x = (-p0.x + 3.0 * p1.x - 3.0 * p2.x + p3.x);
result.x = 0.5 * (c0x + c1x * t + c2x * t2 + c3x * t3);
double c0y = 2.0 * p1.y;
double c1y = (-p0.y + p2.y);
double c2y = (2.0 * p0.y - 5.0 * p1.y + 4.0 * p2.y - p3.y);
double c3y = (-p0.y + 3.0 * p1.y - 3.0 * p2.y + p3.y);
result.y = 0.5 * (c0y + c1y * t + c2y * t2 + c3y * t3);
*/
return result;
}
注意:上面代码中提供了两种计算方式,它们是等价的。第一种是经过系数合并的常见形式,第二种是直接展开原始公式。你可以选择一种使用。
3. 生成光滑插值序列的函数
这个函数接收原始点序列,并根据指定的每个段的采样点数量生成新的、更密集的点序列。
// 生成DSI (Catmull-Rom) 插值序列
// inputPoints: 输入的离散点序列
// samplesPerSegment: 在每两个输入点之间插入的采样点数量 (不包括端点)
std::vector<Point> generateDsiSequence(const std::vector<Point>& inputPoints, int samplesPerSegment) {
std::vector<Point> outputPoints;
size_t n = inputPoints.size();
// 需要至少两个点才能进行插值
if (n < 2) {
return inputPoints; // 或者返回空,根据需求决定
}
// 确保 samplesPerSegment 是非负的
if (samplesPerSegment < 0) {
samplesPerSegment = 0;
}
// 遍历输入点序列中的每一段 (从 inputPoints[i] 到 inputPoints[i+1])
for (size_t i = 0; i < n - 1; ++i) {
// 添加当前段的起始点
outputPoints.push_back(inputPoints[i]);
// 获取定义当前段的四个控制点
// 处理边界情况:如果 i=0,p0设为p1;如果i+2越界,p3设为p2 (端点复制策略)
Point p0 = (i == 0) ? inputPoints[0] : inputPoints[i - 1];
Point p1 = inputPoints[i];
Point p2 = inputPoints[i + 1];
Point p3 = (i + 2 >= n) ? inputPoints[n - 1] : inputPoints[i + 2];
// 在 p1 和 p2 之间生成 'samplesPerSegment' 个中间点
for (int j = 1; j <= samplesPerSegment; ++j) {
// 计算 t 值,在 (0, 1) 范围内均匀分布
double t = static_cast<double>(j) / (samplesPerSegment + 1.0);
Point interpolated = catmullRomInterpolation(p0, p1, p2, p3, t);
outputPoints.push_back(interpolated);
}
}
// 添加最后一个输入点 (因为循环只到 n-2)
outputPoints.push_back(inputPoints.back());
return outputPoints;
}
4. 使用示例
下面是一个 main 函数,展示如何使用 generateDsiSequence:
int main() {
// 定义一组输入点
std::vector<Point> pathPoints = {
{10.0, 10.0},
{50.0, 100.0},
{100.0, 50.0},
{150.0, 120.0},
{200.0, 10.0}
};
// 设置每个段之间要插入的采样点数量
int samples = 5; // 在每两个原始点之间插入5个新点
// 生成插值后的点序列
std::vector<Point> smoothPath = generateDsiSequence(pathPoints, samples);
// 输出结果
std::cout << "Input Points (" << pathPoints.size() << "):" << std::endl;
for (const auto& p : pathPoints) {
std::cout << p << " ";
}
std::cout << std::endl << std::endl;
std::cout << "Generated DSI Points (" << smoothPath.size() << ") using Catmull-Rom:" << std::endl;
std::cout << std::fixed << std::setprecision(2); // 设置输出精度
for (const auto& p : smoothPath) {
std::cout << p << " ";
}
std::cout << std::endl;
return 0;
}
如何编译和运行:
Point 结构,catmullRomInterpolation 函数,generateDsiSequence 函数和 main 函数)保存在一个 .cpp 文件中(例如 dsi_interpolation.cpp)。g++ dsi_interpolation.cpp -o dsi_interpolation -std=c++11 -lm
(-std=c++11 是为了确保 C++11 特性可用,-lm 在某些系统上可能需要链接数学库)。./dsi_interpolation
输出示例:
输出会显示原始的 5 个点,然后是生成的光滑路径上的点。你会看到原始点仍然在输出序列中(作为每个段的起点和终点),并且它们之间插入了计算出的平滑点。
Input Points (5):
(10.00, 10.00) (50.00, 100.00) (100.00, 50.00) (150.00, 120.00) (200.00, 10.00)
Generated DSI Points (21) using Catmull-Rom:
(10.00, 10.00) (26.00, 41.25) (38.00, 75.00) (45.00, 97.50) (47.50, 100.00) (50.00, 100.00) (67.50, 91.25) (85.00, 75.00) (104.00, 57.50) (120.00, 40.00) (100.00, 50.00) (112.00, 78.75) (130.00, 100.00) (142.00, 110.00) (150.00, 120.00) (161.25, 110.00) (175.00, 90.00) (186.00, 50.00) (194.00, 25.00) (200.00, 10.00)
(注意:具体的数值可能因使用的公式变体略有不同,但总体形状和光滑性应该是相似的)
这个实现提供了一个基本的离散光滑插值(DSI)功能,使用的是 Catmull-Rom 样条。你可以根据需要调整 samplesPerSegment 来控制平滑度。
dsi 离散光滑插值算法,生成函数c++实现
好的,我们来实现一个使用 Catmull-Rom 样条 的离散光滑插值(DSI)的 C++ 函数。Catmull-Rom 样条是一种三次 Hermite 样条,它能够生成一条平滑的曲线,并且这条曲线会经过给定的输入点(除了首尾两点可能需要特殊处理以确保曲线连续性)。
算法简介 (Catmull-Rom)
Catmull-Rom 样条通过四个连续的点 P0, P1, P2, P3 来定义曲线段。曲线段在 P1 和 P2 之间,并且会经过 P1 和 P2。P0 和 P3 用于确定曲线在 P1 和 P2 处的切线方向和曲率。
标准的 Catmull-Rom 插值公式为:
P(t) = 0.5 * [ (2*P1) + (-P0 + P2)*t + (2*P0 - 5*P1 + 4*P2 - P3)*t^2 + (-P0 + 3*P1 - 3*P2 + P3)*t^3 ]
其中 t 是插值参数,通常在 [0, 1] 区间内变化。
C++ 实现
#include <vector>
#include <cmath>
#include <iostream> // 用于示例输出
#include <iomanip> // 用于格式化输出
// 定义一个二维点结构
struct Point {
double x, y;
// 重载输出流操作符,方便打印点信息
friend std::ostream& operator<<(std::ostream& os, const Point& p) {
// 使用 setprecision 控制输出精度,使结果更易读
os << "(" << std::fixed << std::setprecision(2) << p.x << ", " << std::setprecision(2) << p.y << ")";
return os;
}
};
/**
* @brief 计算 Catmull-Rom 样条在参数 t 处的插值点
* @param p0 前一个控制点
* @param p1 当前点 (曲线会经过)
* @param p2 下一个点 (曲线会经过)
* @param p3 再下一个控制点
* @param t 插值参数 (通常在 0.0 到 1.0 之间)
* @return 插值计算得到的点
*/
Point catmullRomInterpolation(const Point& p0, const Point& p1, const Point& p2, const Point& p3, double t) {
Point result;
double t2 = t * t;
double t3 = t2 * t;
// 计算 X 分量
// 系数提取自矩阵形式: P(t) = [t^3, t^2, t, 1] * M * [[P0], [P1], [P2], [P3]]
// M = [[ -0.5, 1.5, -1.5, 0.5 ],
// [ 1.0, -2.5, 2.0, -0.5 ],
// [ -0.5, 0.0, 0.5, 0.0 ],
// [ 0.0, 1.0, 0.0, 0.0 ]]
// 或者直接展开的系数形式:
double x0 = p0.x, x1 = p1.x, x2 = p2.x, x3 = p3.x;
result.x = 0.5 * ( (2.0 * x1) + (-x0 + x2) * t + (2.0 * x0 - 5.0 * x1 + 4.0 * x2 - x3) * t2 + (-x0 + 3.0 * x1 - 3.0 * x2 + x3) * t3 );
// 另一种等价的计算方式(可能略快一点点,但原理相同):
// result.x = x1 + t * (-0.5 * x0 + 0.5 * x2) + t2 * (x0 - 2.5 * x1 + 2.0 * x2 - 0.5 * x3) + t3 * (-0.5 * x0 + 1.5 * x1 - 1.5 * x2 + 0.5 * x3);
// 计算 Y 分量
double y0 = p0.y, y1 = p1.y, y2 = p2.y, y3 = p3.y;
result.y = 0.5 * ( (2.0 * y1) + (-y0 + y2) * t + (2.0 * y0 - 5.0 * y1 + 4.0 * y2 - y3) * t2 + (-y0 + 3.0 * y1 - 3.0 * y2 + y3) * t3 );
// 另一种等价的计算方式:
// result.y = y1 + t * (-0.5 * y0 + 0.5 * y2) + t2 * (y0 - 2.5 * y1 + 2.0 * y2 - 0.5 * y3) + t3 * (-0.5 * y0 + 1.5 * y1 - 1.5 * y2 + 0.5 * y3);
return result;
}
/**
* @brief 生成离散光滑插值 (DSI) 点序列,使用 Catmull-Rom 样条
* @param inputPoints 输入的原始离散点序列
* @param samplesPerSegment 在每两个原始点之间插入的采样点数量 (不包括端点)
* @return 生成的光滑插值点序列
*/
std::vector<Point> generateDsiSequence(const std::vector<Point>& inputPoints, int samplesPerSegment) {
std::vector<Point> outputPoints;
size_t n = inputPoints.size();
// 需要至少两个点才能形成插值段
if (n < 2) {
return inputPoints; // 如果点数少于2,直接返回原始点
}
// 确保 samplesPerSegment 非负
if (samplesPerSegment < 0) {
samplesPerSegment = 0;
}
// 循环处理每一段曲线 (从 inputPoints[i] 到 inputPoints[i+1])
for (size_t i = 0; i < n - 1; ++i) {
// 确定四个控制点 p0, p1, p2, p3
// P1 是当前段的起点 (inputPoints[i])
// P2 是当前段的终点 (inputPoints[i+1])
// P0 是 P1 的前一个点。如果 i=0 (第一段),则 P0 使用 P1 (即 inputPoints[0])
// 这种策略有助于在起点处提供一个合理的切线方向(基于第一段)。
Point p0 = (i == 0) ? inputPoints[0] : inputPoints[i - 1];
// P3 是 P2 的后一个点。如果 i=n-2 (最后一段),则 P3 使用 P2 (即 inputPoints[n-1])
// 这种策略有助于在终点处提供一个合理的切线方向(基于最后一段)。
Point p1 = inputPoints[i];
Point p2 = inputPoints[i + 1];
Point p3 = (i + 2 >= n) ? inputPoints[n - 1] : inputPoints[i + 2];
// 添加当前段的起始点 P1
outputPoints.push_back(p1);
// 在 P1 和 P2 之间生成 samplesPerSegment 个中间点
// 每个点的 t 值从 1/(samples+1) 到 samples/(samples+1)
for (int j = 1; j <= samplesPerSegment; ++j) {
double t = static_cast<double>(j) / (samplesPerSegment + 1.0);
Point interpolatedPoint = catmullRomInterpolation(p0, p1, p2, p3, t);
outputPoints.push_back(interpolatedPoint);
}
}
// 添加最后一个原始点 (inputPoints[n-1])
// 因为上面的循环只处理到 n-2, 最后一个点 P[n-1] 没有被作为 P1 添加
outputPoints.push_back(inputPoints.back());
return outputPoints;
}
// --- 主函数示例 ---
int main() {
// 定义一组输入的离散点
std::vector<Point> inputPath = {
{0.0, 0.0},
{50.0, 100.0},
{100.0, 50.0},
{150.0, 120.0},
{200.0, 80.0},
{250.0, 0.0}
};
// 设置每个段插入多少个点来平滑曲线
int smoothingFactor = 4; // 在每两个点之间插入4个点
// 生成光滑插值序列
std::vector<Point> smoothPath = generateDsiSequence(inputPath, smoothingFactor);
// 打印输入点
std::cout << "Input Points (" << inputPath.size() << "):" << std::endl;
for (const auto& p : inputPath) {
std::cout << p << " ";
}
std::cout << std::endl << std::endl;
// 打印生成的 DSI 点
std::cout << "Generated DSI Points (" << smoothPath.size() << ") using Catmull-Rom:" << std::endl;
for (const auto& p : smoothPath) {
std::cout << p << " ";
}
std::cout << std::endl;
return 0;
}
代码解释:
Point 结构: 定义了二维点,包含 x 和 y 坐标。重载了 operator<< 以便能方便地打印点。catmullRomInterpolation 函数:
p0, p1, p2, p3) 和插值参数 t。t 对应的点坐标。代码中提供了两种等价的计算方式(一种是直接展开的多项式形式,另一种是基于系数矩阵展开的形式)。generateDsiSequence 函数:
inputPoints 和每个段要插入的点数 samplesPerSegment。inputPoints[i] 和 inputPoints[i+1] 定义)。p0 和 p3: 为了让曲线在起点和终点也能有合理的形状,当处理第一段时,将 p0 设置为等于 p1;当处理最后一段时,将 p3 设置为等于 p2。这是一种常见的处理方式,确保了端点的连续性和切线方向的合理性。p1 (inputPoints[i]) 加入输出序列。p1 和 p2 之间生成 samplesPerSegment 个点。通过改变 t 的值(从 1.0 / (samplesPerSegment + 1.0) 到 samplesPerSegment / (samplesPerSegment + 1.0)),调用 catmullRomInterpolation 来计算这些中间点。inputPoints.back() 加入输出序列。main 函数: 提供了一个使用示例,定义了几个点,调用 generateDsiSequence 并打印结果。编译与运行:
g++ your_file_name.cpp -o dsi_interpolator -std=c++11 -lm
./dsi_interpolator
将 your_file_name.cpp 替换为你保存代码的文件名。-std=c++11 是为了使用 C++11 的特性(如 auto, size_t 等),-lm 可能在某些系统上需要链接数学库。
这个实现提供了一个功能完整的 DSI 过程,你可以调整 samplesPerSegment 参数来控制生成曲线的平滑度。如果你需要其他类型的 DSI 算法(如 B-Spline 或 Bezier 曲线的插值),实现方式会有所不同。