games101作業04:Bézier 曲線
内容
Bézier 曲線是一種用于計算機圖形學的參數曲線。在本次作業中,你需要實 de Casteljau 算法來繪制由 4 個控制點表示的 Bézier 曲線 (當你正确實作該算法時,你可以支援繪制由更多點來控制的 Bézier 曲線)。你需要修改的函數在提供的 main.cpp 檔案中。
- bezier:該函數實作繪制 Bézier 曲線的功能。它使用一個控制點序列和一個OpenCV::Mat 對象作為輸入,沒有傳回值。它會使 t 在 0 到 1 的範圍内進行疊代,并在每次疊代中使 t 增加一個微小值。對于每個需要計算的 t,将調用另一個recursive_bezier,然後該函數将傳回在Bézier 曲線上 t處的點。最後,将傳回的點繪制在 OpenCV::Mat 對象上。
- recursive_bezier:該函數使用一個控制點序列和一個浮點數 t 作為輸入,實作 de Casteljau 算法來傳回 Bézier 曲線上對應點的坐标。、
基礎内容
參考
代碼
#include <chrono>
#include <iostream>
#include <opencv2/opencv.hpp>
std::vector<cv::Point2f> control_points;
void mouse_handler(int event, int x, int y, int flags, void *userdata)
{
if (event == cv::EVENT_LBUTTONDOWN && control_points.size() < 4)
{
std::cout << "Left button of the mouse is clicked - position (" << x << ", "
<< y << ")" << '\n';
control_points.emplace_back(x, y);
}
}
void naive_bezier(const std::vector<cv::Point2f> &points, cv::Mat &window)
{
auto &p_0 = points[0];
auto &p_1 = points[1];
auto &p_2 = points[2];
auto &p_3 = points[3];
for (double t = 0.0; t <= 1.0; t += 0.001)
{
auto point = std::pow(1 - t, 3) * p_0 + 3 * t * std::pow(1 - t, 2) * p_1 +
3 * std::pow(t, 2) * (1 - t) * p_2 + std::pow(t, 3) * p_3;
window.at<cv::Vec3b>(point.y, point.x)[2] = 255;
}
}
int factorial(int x)
{
int f;
if (x == 0 || x == 1)
f = 1;
else
f = factorial(x - 1) * x;
return f;
}
cv::Point2f recursive_bezier(const std::vector<cv::Point2f> &control_points, float t)
{
// TODO: Implement de Casteljau's algorithm
// 數學方法
// int n = control_points.size();
// cv::Point2f point = {0.0f, 0.0f};
// for (int i = 0; i < n; i++)
// {
// int c = factorial(n - 1) / (factorial(i) * factorial(n - 1 - i));
// point += c * std::pow(t, i) * std::pow(1 - t, n - 1 - i) * control_points[i];
// }
// 遞歸方法
if(control_points.size() == 2)
{
return control_points[0] + t * (control_points[1] - control_points[0]);
}
std::vector<cv::Point2f> vec;
for (int i = 0; i < control_points.size() - 1; i++)
{
vec.push_back(control_points[i] + t * (control_points[i + 1] - control_points[i]));
}
return recursive_bezier(vec, t);
}
//将點顯示在螢幕
void bezier(const std::vector<cv::Point2f> &control_points, cv::Mat &window)
{
// TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's
// recursive Bezier algorithm.
for (double t = 0.0; t<=1.0; t+=0.001){
cv::Point2f point = recursive_bezier(control_points, t);
window.at<cv::Vec3b>(point.y,point.x)[1] = 255;
}
}
//對BezierCurve進行插值
/*
void bezier(const std::vector<cv::Point2f> &control_points, cv::Mat &window)
{
// TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's
// recursive Bezier algorithm.
for (double t = 0.0; t <= 1.0; t += 0.001)
{
cv::Point2f point = recursive_bezier(control_points, t);
window.at<cv::Vec3b>(point.y, point.x)[1] = 255;
//anti-aliasing
float x = point.x - std::floor(point.x);
float y = point.y - std::floor(point.y);
int x_flag = x < 0.5f ? -1 : 1;
int y_flag = y < 0.5f ? -1 : 1;
// 距離采樣點最近的4個坐标點
cv::Point2f p00 = cv::Point2f(std::floor(point.x) + 0.5f, std::floor(point.y) + 0.5f);
cv::Point2f p01 = cv::Point2f(std::floor(point.x + x_flag * 1.0f) + 0.5f, std::floor(point.y) + 0.5f);
cv::Point2f p10 = cv::Point2f(std::floor(point.x) + 0.5f, std::floor(point.y + y_flag * 1.0f) + 0.5f);
cv::Point2f p11 = cv::Point2f(std::floor(point.x + x_flag * 1.0f) + 0.5f, std::floor(point.y + y_flag * 1.0f) + 0.5f);
std::vector<cv::Point2f> vec;
vec.push_back(p01);
vec.push_back(p10);
vec.push_back(p11);
// 計算最近的坐标點與采樣點距離
cv::Point2f distance = p00 - point;
float len = sqrt(distance.x * distance.x + distance.y * distance.y);
// 對邊緣點進行着色
for(auto p:vec)
{
// 根據距離比, 計算邊緣點影響系數
cv::Point2f d = p - point;
float l = sqrt(d.x * d.x + d.y * d.y);
float percnet = len / l;
cv::Vec3d color = window.at<cv::Vec3b>(p.y, p.x);
// 此處簡單粗暴取最大值
color[1] = std::max(color[1], (double)255 * percnet);
window.at<cv::Vec3b>(p.y, p.x) = color;
}
}
}*/
int main()
{
cv::Mat window = cv::Mat(700, 700, CV_8UC3, cv::Scalar(0));
cv::cvtColor(window, window, cv::COLOR_BGR2RGB);
cv::namedWindow("Bezier Curve", cv::WINDOW_AUTOSIZE);
cv::setMouseCallback("Bezier Curve", mouse_handler, nullptr);
int key = -1;
while (key != 27)
{
for (auto &point : control_points)
{
cv::circle(window, point, 3, {255, 255, 255}, 3);
}
if (control_points.size() == 4)
{
//naive_bezier(control_points, window);
bezier(control_points, window);
cv::imshow("Bezier Curve", window);
cv::imwrite("my_bezier_curve.png", window);
key = cv::waitKey(0);
return 0;
}
cv::imshow("Bezier Curve", window);
key = cv::waitKey(20);
}
return 0;
}