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/opencv/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_OPENCV_DRAW_UTILS_HPP
23#define EOS_RENDER_OPENCV_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
31#include "opencv2/core/core.hpp"
32#include "opencv2/imgproc/imgproc.hpp"
33
34namespace eos {
35namespace render {
36
52inline void draw_wireframe(cv::Mat image, const core::Mesh& mesh, Eigen::Matrix4f modelview,
53 Eigen::Matrix4f projection, Eigen::Vector4f viewport,
54 cv::Scalar color = cv::Scalar(0, 255, 0, 255))
55{
56 for (const auto& triangle : mesh.tvi)
57 {
58 const auto p1 = project(mesh.vertices[triangle[0]], modelview, projection, viewport);
59 const auto p2 = project(mesh.vertices[triangle[1]], modelview, projection, viewport);
60 const auto p3 = project(mesh.vertices[triangle[2]], modelview, projection, viewport);
61 if (render::detail::are_vertices_ccw_in_screen_space<float>(p1.head<2>(), p2.head<2>(), p3.head<2>()))
62 {
63 cv::line(image, cv::Point(p1.x(), p1.y()), cv::Point(p2.x(), p2.y()), color);
64 cv::line(image, cv::Point(p2.x(), p2.y()), cv::Point(p3.x(), p3.y()), color);
65 cv::line(image, cv::Point(p3.x(), p3.y()), cv::Point(p1.x(), p1.y()), color);
66 }
67 }
68};
69
82inline cv::Mat draw_texcoords(core::Mesh mesh, cv::Mat image = cv::Mat())
83{
84 using cv::Point2f;
85 using cv::Scalar;
86 if (image.empty())
87 {
88 image = cv::Mat(512, 512, CV_8UC4, Scalar(0.0f, 0.0f, 0.0f, 255.0f));
89 }
90
91 for (const auto& triIdx : mesh.tvi)
92 {
93 cv::line(
94 image,
95 Point2f(mesh.texcoords[triIdx[0]][0] * image.cols, mesh.texcoords[triIdx[0]][1] * image.rows),
96 Point2f(mesh.texcoords[triIdx[1]][0] * image.cols, mesh.texcoords[triIdx[1]][1] * image.rows),
97 Scalar(255.0f, 0.0f, 0.0f));
98 cv::line(
99 image,
100 Point2f(mesh.texcoords[triIdx[1]][0] * image.cols, mesh.texcoords[triIdx[1]][1] * image.rows),
101 Point2f(mesh.texcoords[triIdx[2]][0] * image.cols, mesh.texcoords[triIdx[2]][1] * image.rows),
102 Scalar(255.0f, 0.0f, 0.0f));
103 cv::line(
104 image,
105 Point2f(mesh.texcoords[triIdx[2]][0] * image.cols, mesh.texcoords[triIdx[2]][1] * image.rows),
106 Point2f(mesh.texcoords[triIdx[0]][0] * image.cols, mesh.texcoords[triIdx[0]][1] * image.rows),
107 Scalar(255.0f, 0.0f, 0.0f));
108 }
109 return image;
110};
111
112} /* namespace render */
113} /* namespace eos */
114
115#endif /* EOS_RENDER_OPENCV_DRAW_UTILS_HPP */
cv::Mat draw_texcoords(core::Mesh mesh, cv::Mat image=cv::Mat())
Definition: draw_utils.hpp:82
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
std::vector< Eigen::Vector2f > texcoords
Texture coordinates.
Definition: Mesh.hpp:48