-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooldev.cpp
More file actions
105 lines (104 loc) · 3.63 KB
/
Copy pathTooldev.cpp
File metadata and controls
105 lines (104 loc) · 3.63 KB
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
#include<iostream>
#include<algorithm>
#include<Eigen/Core>
#include<Eigen/Geometry>
#include"Tooldev.h"
#define WIDTH 512.0f
#define HEIGHT 512.0f
#define LOG_INFO(msg) Logger::log(INFO, msg)
#define LOG_WARN(msg) Logger::log(WARNING, msg)
#define LOG_ERROR(msg) Logger::log(ERROR, msg)
float getVectorX(const Vector2f& v) {
return v.x();
}
float getVectorY(const Vector2f& v) {
return v.y();
}
void check_bound_width2D(float& w) {
int f = 0;
if (w <= 0.0f) {
w = 0;
f = 1;
}
else if (w >= WIDTH) {
w = WIDTH - 1;
f = 1;
}
if (f) {
LOG_ERROR("width : out of screen range");
}
}
void check_bound_height(float& h) {
int f = 0;
if (h <= 0.0f) {
h = 0;
f = 1;
}
else if (h >= HEIGHT) {
h = HEIGHT - 1;
f = 1;
}
if (f) {
LOG_ERROR("height : out of screen range");
}
}
float get_points_distance(const Vector2f& v) {
float d = v.norm();
return d;
}
float cross2D(const Vector2f& v1, const Vector2f& v2) { // (edge,point)
return v1.x() * v2.y() - v1.y() * v2.x();
}
float get_triangle_area(const Vector2f& p1, const Vector2f& p2, const Vector2f& p3) {
Vector2f p1p2, p1p3;
p1p3 << p3 - p1;
p1p2 << p2 - p1;
float s = fabs(cross2D(p1p3, p1p2)) * 0.5f;
return s;
}
Matrix4f getmatr_view_transform(Vector3f& pos_cam, Vector3f& target_pos, Vector3f& up) {
Vector3f vec_cam_to_tar = (pos_cam - target_pos).normalized();
Vector3f vec_right = up.cross(vec_cam_to_tar).normalized();
Vector3f vec_up_correct = vec_cam_to_tar.cross(vec_right).normalized();
Matrix4f trans_view;
trans_view <<
vec_right.x(), vec_right.y(), vec_right.z(), -vec_right.dot(pos_cam),
vec_up_correct.x(), vec_up_correct.y(), vec_up_correct.z(), -vec_up_correct.dot(pos_cam),
vec_cam_to_tar.x(), vec_cam_to_tar.y(), vec_cam_to_tar.z(), -vec_cam_to_tar.dot(pos_cam),
0, 0, 0, 1;
return trans_view;
}
Matrix4f getmatr_perspective_transform(float fov_degree, float aspect_ratio, float near_face, float far_face) {
float fov_rads = fov_degree * acosf(-1) / 180.0f;
float top = tanf(fov_rads * 0.5f) * near_face;
float right = top * aspect_ratio;
Matrix4f trans_persp;
trans_persp <<
near_face / right, 0, 0, 0,
0, near_face / top, 0, 0,
0, 0, -(far_face + near_face) / (far_face - near_face), -2*far_face*near_face/(far_face-near_face),
0, 0, -1, 0;
return trans_persp;
}
Matrix4f getmatr_VP(float fov_degree, float width,float height, float near_face, float far_face, Vector3f& pos_cam, Vector3f& target_pos, Vector3f& up) {
float aspect_ratio = width / height;
Matrix4f mat_V = getmatr_view_transform(pos_cam, target_pos, up);
Matrix4f mat_P = getmatr_perspective_transform(fov_degree, aspect_ratio, near_face, far_face);
Matrix4f mat_VP = mat_P * mat_V;
return mat_VP;
}
Vector2f_1_w world_to_screen(Matrix4f mat_VP,Vertex& p,float width,float height) {
Vector4f point_4f(p.get_pos_x(), p.get_pos_y(), p.get_pos_z(), 1.0f);
Vector4f point_clip_4f = mat_VP * point_4f;
if (point_clip_4f.w() <= 1e-6f) {
return { Vector2f(-1, -1),0 };
}
Vector3f ndc = point_clip_4f.head<3>() / point_clip_4f.w();
if (ndc.x() < -1 || ndc.x() > 1 || ndc.y() < -1 || ndc.y() > 1 || ndc.z() < -1 || ndc.z() > 1) {
return { Vector2f(-1, -1),0 };
}
float x = (ndc.x() + 1.0f) * 0.5f * width;
float y = (ndc.y() + 1.0f) * 0.5f * height;
float inv_w = 1.0f / point_clip_4f.w();
return { Vector2f(x, y),inv_w };
}