eos 1.4.0
Loading...
Searching...
No Matches
normals.hpp
1/*
2 * eos - A 3D Morphable Model fitting library written in modern C++11/14.
3 *
4 * File: include/eos/render/normals.hpp
5 *
6 * Copyright 2014-2019 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_NORMALS_HPP
23#define EOS_RENDER_NORMALS_HPP
24
25#include "Eigen/Core"
26
27#include <vector>
28
29namespace eos {
30namespace render {
31
43inline Eigen::Vector3f compute_face_normal(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1,
44 const Eigen::Vector3f& v2)
45{
46 Eigen::Vector3f n = (v1 - v0).cross(v2 - v0); // v0-to-v1 x v0-to-v2
47 return n.normalized();
48};
49
61inline Eigen::Vector3f compute_face_normal(const Eigen::Vector4f& v0, const Eigen::Vector4f& v1,
62 const Eigen::Vector4f& v2)
63{
64 Eigen::Vector4f n = (v1 - v0).cross3(v2 - v0); // v0-to-v1 x v0-to-v2
65 return n.head<3>().normalized();
66};
67
77inline std::vector<Eigen::Vector3f>
78compute_face_normals(const std::vector<Eigen::Vector3f>& vertices,
79 const std::vector<std::array<int, 3>>& triangle_vertex_indices)
80{
81 std::vector<Eigen::Vector3f> face_normals;
82 for (const auto& tvi : triangle_vertex_indices)
83 {
84 const auto face_normal = compute_face_normal(vertices[tvi[0]], vertices[tvi[1]], vertices[tvi[2]]);
85 face_normals.push_back(face_normal);
86 }
87 return face_normals;
88};
89
101inline std::vector<Eigen::Vector3f>
102compute_vertex_normals(const std::vector<Eigen::Vector3f>& vertices,
103 const std::vector<std::array<int, 3>>& triangle_vertex_indices,
104 const std::vector<Eigen::Vector3f>& face_normals)
105{
106 std::vector<Eigen::Vector3f> per_vertex_normals;
107 // Initialise with zeros:
108 for (int i = 0; i < vertices.size(); ++i)
109 {
110 per_vertex_normals.emplace_back(Eigen::Vector3f(0.0f, 0.0f, 0.0f));
111 }
112
113 // Loop over the faces again:
114 for (int i = 0; i < triangle_vertex_indices.size(); ++i)
115 {
116 const auto& tvi = triangle_vertex_indices[i];
117 // Throw normal at each corner:
118 per_vertex_normals[tvi[0]] += face_normals[i];
119 per_vertex_normals[tvi[1]] += face_normals[i];
120 per_vertex_normals[tvi[2]] += face_normals[i];
121 }
122
123 // Take average via normalization:
124 for (auto& n : per_vertex_normals)
125 {
126 n.normalize();
127 }
128 return per_vertex_normals;
129};
130
131} /* namespace render */
132} /* namespace eos */
133
134#endif /* EOS_RENDER_NORMALS_HPP */
std::vector< Eigen::Vector3f > compute_face_normals(const std::vector< Eigen::Vector3f > &vertices, const std::vector< std::array< int, 3 > > &triangle_vertex_indices)
Definition: normals.hpp:78
Eigen::Vector3f compute_face_normal(const Eigen::Vector3f &v0, const Eigen::Vector3f &v1, const Eigen::Vector3f &v2)
Definition: normals.hpp:43
std::vector< Eigen::Vector3f > compute_vertex_normals(const std::vector< Eigen::Vector3f > &vertices, const std::vector< std::array< int, 3 > > &triangle_vertex_indices, const std::vector< Eigen::Vector3f > &face_normals)
Definition: normals.hpp:102
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.