Repository navigation
Expand file tree
/
Copy pathFaceFrameAttribute.cpp
More file actions
68 lines (60 loc) · 2.21 KB
/
Copy pathFaceFrameAttribute.cpp
File metadata and controls
68 lines (60 loc) · 2.21 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
/* This file is part of PyMesh. Copyright (c) 2018 by Qingnan Zhou */
#include "FaceFrameAttribute.h"
#include <Mesh.h>
#include <Core/Exception.h>
using namespace PyMesh;
void FaceFrameAttribute::compute_from_mesh(Mesh& mesh) {
const size_t dim = mesh.get_dim();
switch (dim) {
case 2:
compute_2D_frame_field(mesh);
break;
case 3:
compute_3D_frame_field(mesh);
break;
default:
throw NotImplementedError("Unsupported dimension "
+ std::to_string(dim));
}
}
void FaceFrameAttribute::compute_2D_frame_field(Mesh& mesh) {
assert(mesh.get_dim() == 2);
const size_t num_faces = mesh.get_num_faces();
m_values.resize(num_faces * 4); // 2x2 matrix per face.
for (size_t i=0; i<num_faces; i++) {
m_values.segment<4>(i*4) << 1, 0, 0, 1;
}
return;
}
void FaceFrameAttribute::compute_3D_frame_field(Mesh& mesh) {
assert(mesh.get_dim() == 3);
const size_t num_faces = mesh.get_num_faces();
const size_t num_vertex_per_face = mesh.get_vertex_per_face();
if (!mesh.has_attribute("edge_length")) {
mesh.add_attribute("edge_length");
}
if (!mesh.has_attribute("face_normal")) {
mesh.add_attribute("face_normal");
}
const auto& faces = mesh.get_faces();
const auto& edge_lengths = mesh.get_attribute("edge_length");
const auto& normals = mesh.get_attribute("face_normal");
assert(normals.size() == num_faces * 3);
VectorF& frame_field = m_values;
frame_field = VectorF::Zero(num_faces * 6); // 2x3 matrix for each face.
for (size_t i=0; i<num_faces; i++) {
VectorF edges = edge_lengths.segment(
i*num_vertex_per_face, num_vertex_per_face);
Vector3F n = normals.segment<3>(i*3);
VectorF::Index max_idx;
edges.maxCoeff(&max_idx);
const Vector3F v0 = mesh.get_vertex(
faces[i*num_vertex_per_face + max_idx]);
const Vector3F v1 = mesh.get_vertex(
faces[i*num_vertex_per_face + (max_idx+1) % num_vertex_per_face]);
Vector3F e0 = v1 - v0;
e0.normalize();
Vector3F e1 = n.cross(e0);
frame_field.segment<6>(i*6) << e0, e1;
}
}