eos 1.4.0
Loading...
Searching...
No Matches
draw_utils.hpp
1/*
2 * eos - A 3D Morphable Model fitting library written in modern C++11/14.
3 *
4 * File: include/eos/render/draw_utils.hpp
5 *
6 * Copyright 2017-2019, 2023 Patrik Huber
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#pragma once
21
22#ifndef EOS_RENDER_DRAW_UTILS_HPP
23#define EOS_RENDER_DRAW_UTILS_HPP
24
25#include "eos/core/Mesh.hpp"
26#include "eos/render/matrix_projection.hpp"
27#include "eos/render/detail/utils.hpp"
28
29#include "Eigen/Core"
30
31namespace eos {
32namespace render {
33
49inline void draw_line(core::Image3u& image, float x0, float y0, float x1, float y1, Eigen::Vector3f color)
50{
51 auto plot_line_low = [&image, &color](float x0, float y0, float x1, float y1) {
52 float dx = x1 - x0;
53 float dy = y1 - y0;
54 int yi = 1;
55 if (dy < 0)
56 {
57 yi = -1;
58 dy = -dy;
59 }
60
61 float D = 2 * dy - dx;
62 float y = y0;
63
64 for (int x = x0; x <= x1; ++x) // for x from x0 to x1
65 {
66 image(y, x) = {color[0], color[1], color[2]}; // plot(x, y);
67 if (D > 0)
68 {
69 y = y + yi;
70 D = D - 2 * dx;
71 }
72 D = D + 2 * dy;
73 }
74 };
75
76 auto plot_line_high = [&image, &color](float x0, float y0, float x1, float y1) {
77 float dx = x1 - x0;
78 float dy = y1 - y0;
79 int xi = 1;
80 if (dx < 0)
81 {
82 xi = -1;
83 dx = -dx;
84 }
85
86 float D = 2 * dx - dy;
87 float x = x0;
88
89 for (int y = y0; y <= y1; ++y) // for y from y0 to y1
90 {
91 image(y, x) = {color[0], color[1], color[2]}; // plot(x, y);
92 if (D > 0)
93 {
94 x = x + xi;
95 D = D - 2 * dy;
96 }
97 D = D + 2 * dx;
98 }
99 };
100
101 if (abs(y1 - y0) < abs(x1 - x0))
102 {
103 if (x0 > x1)
104 {
105 plot_line_low(x1, y1, x0, y0);
106 } else
107 {
108 plot_line_low(x0, y0, x1, y1);
109 }
110 } else
111 {
112 if (y0 > y1)
113 {
114 plot_line_high(x1, y1, x0, y0);
115 } else
116 {
117 plot_line_high(x0, y0, x1, y1);
118 }
119 }
120};
121
134inline void draw_wireframe(core::Image3u& image, const core::Mesh& mesh, Eigen::Matrix4f modelview,
135 Eigen::Matrix4f projection, Eigen::Vector4f viewport,
136 Eigen::Vector3f color = Eigen::Vector3f(0, 255, 0))
137{
138 for (const auto& triangle : mesh.tvi)
139 {
140 const auto p1 = project(mesh.vertices[triangle[0]], modelview, projection, viewport);
141 const auto p2 = project(mesh.vertices[triangle[1]], modelview, projection, viewport);
142 const auto p3 = project(mesh.vertices[triangle[2]], modelview, projection, viewport);
143 if (render::detail::are_vertices_ccw_in_screen_space<float>(p1.head<2>(), p2.head<2>(), p3.head<2>()))
144 {
145 draw_line(image, p1.x(), p1.y(), p2.x(), p2.y(), color);
146 draw_line(image, p2.x(), p2.y(), p3.x(), p3.y(), color);
147 draw_line(image, p3.x(), p3.y(), p1.x(), p1.y(), color);
148 }
149 }
150};
151
152} /* namespace render */
153} /* namespace eos */
154
155#endif /* EOS_RENDER_DRAW_UTILS_HPP */
Class to represent images.
Definition: Image.hpp:44
void draw_line(core::Image3u &image, float x0, float y0, float x1, float y1, Eigen::Vector3f color)
Definition: draw_utils.hpp:49
Eigen::Vector3< T > project(const Eigen::Vector3< T > &point_3d, const Eigen::Matrix4< T > &modelview_matrix, const Eigen::Matrix4< T > &projection_matrix, const Eigen::Vector4< T > &viewport)
Definition: matrix_projection.hpp:148
void draw_wireframe(core::Image3u &image, const core::Mesh &mesh, Eigen::Matrix4f modelview, Eigen::Matrix4f projection, Eigen::Vector4f viewport, Eigen::Vector3f color=Eigen::Vector3f(0, 255, 0))
Definition: draw_utils.hpp:134
core::Image4u render(const core::Mesh &mesh, const Eigen::Matrix4f &model_view_matrix, const Eigen::Matrix4f &projection_matrix, int viewport_width, int viewport_height, bool enable_backface_culling=false, bool enable_near_clipping=true, bool enable_far_clipping=true)
Definition: render.hpp:52
Namespace containing all of eos's 3D model fitting functionality.
This class represents a 3D mesh consisting of vertices, vertex colour information and texture coordin...
Definition: Mesh.hpp:45
std::vector< Eigen::Vector3f > vertices
3D vertex positions.
Definition: Mesh.hpp:46
std::vector< std::array< int, 3 > > tvi
Triangle vertex indices.
Definition: Mesh.hpp:50