Repository navigation
Expand file tree
/
Copy pathEdgeDihedralAngleAttribute.cpp
More file actions
87 lines (75 loc) · 2.88 KB
/
Copy pathEdgeDihedralAngleAttribute.cpp
File metadata and controls
87 lines (75 loc) · 2.88 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
/* This file is part of PyMesh. Copyright (c) 2015 by Qingnan Zhou */
#include "EdgeDihedralAngleAttribute.h"
#include <array>
#include <cmath>
#include <iostream>
#include <tbb/tbb.h>
#include <mutex>
#include <thread>
#include <Mesh.h>
#include <Core/Exception.h>
#include <Math/MatrixUtils.h>
#include <Misc/Multiplet.h>
#include <Misc/MultipletMap.h>
using namespace PyMesh;
void EdgeDihedralAngleAttribute::compute_from_mesh(Mesh& mesh) {
using EdgeMap = DupletMap<std::array<size_t, 2>>;
const size_t dim = mesh.get_dim();
const size_t num_faces = mesh.get_num_faces();
const size_t vertex_per_face = mesh.get_vertex_per_face();
if (dim != 3) {
throw NotImplementedError(
"Dihedral angle computation supports 3D mesh only.");
}
if (!mesh.has_attribute("face_normal"))
mesh.add_attribute("face_normal");
MatrixFr face_normals =
MatrixUtils::reshape<MatrixFr>(mesh.get_attribute("face_normal"),
num_faces, dim);
if (!face_normals.allFinite()) {
std::cerr << "Warning: Face normal contains nan or inf!" << std::endl;
std::cerr << " Dihedral angle computation would be unreliable."
<< std::endl;
}
EdgeMap edge_map;
for (size_t i=0; i<num_faces; i++) {
const auto f = mesh.get_face(i);
for (size_t j=0; j<vertex_per_face; j++) {
const Duplet key(f[j], f[(j+1)%vertex_per_face]);
edge_map.insert(key, {i, j});
}
}
auto angle = [&face_normals](size_t f1, size_t f2) {
const Eigen::Ref<Vector3F>& n1 = face_normals.row(f1);
const Eigen::Ref<Vector3F>& n2 = face_normals.row(f2);
return atan2(n1.cross(n2).norm(), n1.dot(n2));
};
VectorF& dihedral_angles = m_values;
dihedral_angles = VectorF::Zero(num_faces * vertex_per_face);
auto compute_kernel = [&angle, &dihedral_angles, vertex_per_face](
EdgeMap::const_range_type& r) {
for (const auto& entry : r) {
const auto& key = entry.first;
const auto& val = entry.second;
const size_t num_adj_faces = val.size();
assert(num_adj_faces > 0);
Float normal_angle = 0.0;
if (num_adj_faces == 1) {
normal_angle = 0.0;
} else if (num_adj_faces == 2) {
normal_angle = angle(val[0][0], val[1][0]);
} else {
for (size_t i=0; i<num_adj_faces; i++) {
for (size_t j=i+1; j<num_adj_faces; j++) {
normal_angle = std::max(normal_angle,
angle(val[i][0], val[j][0]));
}
}
}
for (const auto& itr : val) {
dihedral_angles[itr[0] * vertex_per_face + itr[1]] = normal_angle;
}
}
};
tbb::parallel_for(edge_map.range(), compute_kernel);
}