-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cpp
108 lines (103 loc) · 4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <filesystem>
// #include <experimental/filesystem> // uncomment here if the <filesystem> cannot be included above
//
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "Eigen/Core"
//
#include "parse_svg.h"
/***
* signed area of a triangle connecting points (p0, p1, p2) in counter-clockwise order.
* @param p0 1st point xy-coordinate
* @param p1 2nd point xy-coordinate
* @param p2 3rd point xy-coordinate
* @return signed area (float)
*/
float area(
const Eigen::Vector2f &p0,
const Eigen::Vector2f &p1,
const Eigen::Vector2f &p2) {
const auto v01 = p1 - p0;
const auto v02 = p2 - p0;
// return 0.5f * (v01[0] * v02[1] - v01[1] * v02[0]); // right handed coordinate
return 0.5f * (v01[1] * v02[0] - v01[0] * v02[1]); // left-handed coordinate (because pixel y-coordinate is going down)
}
/***
* compute number of intersection of a ray against a line segment
* @param org ray origin
* @param dir ray direction (unit normal)
* @param ps one of the two end points
* @param pe the other end point
* @return number of intersection
*/
int number_of_intersection_ray_against_edge(
const Eigen::Vector2f &org,
const Eigen::Vector2f &dir,
const Eigen::Vector2f &ps,
const Eigen::Vector2f &pe) {
auto a = area(org, org + dir, ps);
auto b = area(org, pe, org + dir);
auto c = area(org, ps, pe);
auto d = area(dir+ps, ps, pe);
if (a * b > 0.f && d * c < 0.f) { return 1; }
return 0;
// the following code was a bug
//auto d = area(org + dir, ps, pe);
//if (a * b > 0.f && d * c > 0.f && fabs(d) > fabs(c)) { return 1; }
}
/***
*
* @param org ray origin
* @param dir ray direction (unit vector)
* @param ps one of the two end points
* @param pc control point
* @param pe the other end point
* @return the number of intersections
*/
int number_of_intersection_ray_against_quadratic_bezier(
const Eigen::Vector2f &org,
const Eigen::Vector2f &dir,
const Eigen::Vector2f &ps,
const Eigen::Vector2f &pc,
const Eigen::Vector2f &pe) {
// comment out below to do the assignment
return number_of_intersection_ray_against_edge(org, dir, ps, pe);
// write some code below to find the intersection between ray and the quadratic
}
int main() {
const auto input_file_path = std::filesystem::path(PROJECT_SOURCE_DIR) / ".." / "asset" / "r.svg";
const auto [width, height, shape] = acg::svg_get_image_size_and_shape(input_file_path);
if (width == 0) { // something went wrong in loading the function
std::cout << "file open failure" << std::endl;
abort();
}
const std::vector<std::string> outline_path = acg::svg_outline_path_from_shape(shape);
const std::vector<std::vector<acg::Edge>> loops = acg::svg_loops_from_outline_path(outline_path);
//
std::vector<unsigned char> img_data(width * height, 255); // grayscale image initialized white
for (unsigned int ih = 0; ih < height; ++ih) {
for (unsigned int iw = 0; iw < width; ++iw) {
const auto org = Eigen::Vector2f(iw + 0.5, ih + 0.5); // pixel center
const auto dir = Eigen::Vector2f(60., 20.); // search direction
int count_cross = 0;
for (const auto &loop: loops) { // loop over loop (letter R have internal/external loops)
for (const auto &edge: loop) { // loop over edge in the loop
if (edge.is_bezier) { // in case the edge is a quadratic Bézier
count_cross += number_of_intersection_ray_against_quadratic_bezier(
org, dir,
edge.ps, edge.pc, edge.pe);
} else { // in case the edge is a line segment
count_cross += number_of_intersection_ray_against_edge(
org, dir,
edge.ps, edge.pe);
}
}
}
if (count_cross % 2 == 1) { // Jordan's curve theory
img_data[ih * width + iw] = 0; // paint black if it is inside
}
}
}
const auto output_file_path = std::filesystem::path(PROJECT_SOURCE_DIR) / "output.png";
stbi_write_png(output_file_path.string().c_str(), width, height, 1, img_data.data(), width);
}