Repository navigation
Expand file tree
/
Copy pathVoxelVolumeAttribute.cpp
More file actions
147 lines (127 loc) · 5.45 KB
/
Copy pathVoxelVolumeAttribute.cpp
File metadata and controls
147 lines (127 loc) · 5.45 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This file is part of PyMesh. Copyright (c) 2015 by Qingnan Zhou */
#include "VoxelVolumeAttribute.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <tbb/tbb.h>
#include <Core/EigenTypedef.h>
#include <Mesh.h>
using namespace PyMesh;
namespace VoxelVolumeAttributeHelper {
Float compute_signed_tet_volume(
const Vector3F& v0, const Vector3F& v1,
const Vector3F& v2, const Vector3F& v3) {
// Formula: volume = <a x b, c> / 6
// where a,b and c are edge vector from a single vertex.
// <., .> is inner product.
return ((v1 - v0).cross(v2 - v0)).dot(v3 - v0) / 6.0;
}
Float compute_abs_tet_volume(
const Vector3F& v0, const Vector3F& v1,
const Vector3F& v2, const Vector3F& v3) {
return fabs(compute_signed_tet_volume(v0, v1, v2, v3));
}
}
using namespace VoxelVolumeAttributeHelper;
void VoxelVolumeAttribute::compute_from_mesh(Mesh& mesh) {
const size_t num_voxels = mesh.get_num_voxels();
const size_t num_vertex_per_voxel = mesh.get_vertex_per_voxel();
VectorF& volumes = m_values;
volumes = VectorF::Zero(num_voxels);
if (num_voxels > 0) {
if (num_vertex_per_voxel == 4) {
tbb::parallel_for(tbb::blocked_range<size_t>(0, num_voxels),
[this, &mesh, &volumes](const tbb::blocked_range<size_t>& r) {
for (size_t i=r.begin(); i<r.end(); i++) {
volumes[i] = compute_signed_tet_volume(mesh, i);
}
}
);
} else if (num_vertex_per_voxel == 8) {
tbb::parallel_for(tbb::blocked_range<size_t>(0, num_voxels),
[this, &mesh, &volumes](const tbb::blocked_range<size_t>& r) {
for (size_t i=r.begin(); i<r.end(); i++) {
volumes[i] = compute_signed_hex_volume(mesh, i);
}
}
);
} else {
std::cerr << "Unknown voxel type with " << num_vertex_per_voxel
<< " per voxel." << std::endl;
return;
}
}
}
Float VoxelVolumeAttribute::compute_signed_tet_volume(Mesh& mesh, size_t voxel_idx) {
VectorI voxel = mesh.get_voxel(voxel_idx);
assert(voxel.size() == 4);
Vector3F v[4] = {
mesh.get_vertex(voxel[0]),
mesh.get_vertex(voxel[1]),
mesh.get_vertex(voxel[2]),
mesh.get_vertex(voxel[3])
};
return ::compute_signed_tet_volume(v[0], v[1], v[2], v[3]);
}
Float VoxelVolumeAttribute::compute_signed_hex_volume(Mesh& mesh, size_t voxel_idx) {
VectorI voxel = mesh.get_voxel(voxel_idx);
assert(voxel.size() == 8);
// v
// 3----------2
// |\ ^ |\
// | \ | | \
// | \ | | \
// | 7------+---6
// | | +-- |-- | -> u
// 0---+---\--1 |
// \ | \ \ |
// \ | \ \ |
// \| w \|
// 4----------5
Vector3F v[8] = {
mesh.get_vertex(voxel[0]),
mesh.get_vertex(voxel[1]),
mesh.get_vertex(voxel[2]),
mesh.get_vertex(voxel[3]),
mesh.get_vertex(voxel[4]),
mesh.get_vertex(voxel[5]),
mesh.get_vertex(voxel[6]),
mesh.get_vertex(voxel[7])
};
Vector3F face_centers[6] = {
0.25 * (v[0] + v[1] + v[2] + v[3]),
0.25 * (v[4] + v[5] + v[6] + v[7]),
0.25 * (v[0] + v[1] + v[5] + v[4]),
0.25 * (v[2] + v[3] + v[7] + v[6]),
0.25 * (v[0] + v[4] + v[7] + v[3]),
0.25 * (v[1] + v[2] + v[6] + v[5]),
};
Vector3F centroid = 0.125 * (
v[0] + v[1] + v[2] + v[3] +
v[4] + v[5] + v[6] + v[7]);
return
::compute_abs_tet_volume(centroid, face_centers[0], v[0], v[1]) +
::compute_abs_tet_volume(centroid, face_centers[0], v[1], v[2]) +
::compute_abs_tet_volume(centroid, face_centers[0], v[2], v[3]) +
::compute_abs_tet_volume(centroid, face_centers[0], v[3], v[0]) +
::compute_abs_tet_volume(centroid, face_centers[1], v[4], v[5]) +
::compute_abs_tet_volume(centroid, face_centers[1], v[5], v[6]) +
::compute_abs_tet_volume(centroid, face_centers[1], v[6], v[7]) +
::compute_abs_tet_volume(centroid, face_centers[1], v[7], v[4]) +
::compute_abs_tet_volume(centroid, face_centers[2], v[0], v[1]) +
::compute_abs_tet_volume(centroid, face_centers[2], v[1], v[5]) +
::compute_abs_tet_volume(centroid, face_centers[2], v[5], v[4]) +
::compute_abs_tet_volume(centroid, face_centers[2], v[4], v[0]) +
::compute_abs_tet_volume(centroid, face_centers[3], v[2], v[3]) +
::compute_abs_tet_volume(centroid, face_centers[3], v[3], v[7]) +
::compute_abs_tet_volume(centroid, face_centers[3], v[7], v[6]) +
::compute_abs_tet_volume(centroid, face_centers[3], v[6], v[2]) +
::compute_abs_tet_volume(centroid, face_centers[4], v[0], v[4]) +
::compute_abs_tet_volume(centroid, face_centers[4], v[4], v[7]) +
::compute_abs_tet_volume(centroid, face_centers[4], v[7], v[3]) +
::compute_abs_tet_volume(centroid, face_centers[4], v[3], v[0]) +
::compute_abs_tet_volume(centroid, face_centers[5], v[1], v[2]) +
::compute_abs_tet_volume(centroid, face_centers[5], v[2], v[6]) +
::compute_abs_tet_volume(centroid, face_centers[5], v[6], v[5]) +
::compute_abs_tet_volume(centroid, face_centers[5], v[5], v[1]) ;
}