-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScalarField.cpp
141 lines (104 loc) · 3.5 KB
/
ScalarField.cpp
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
/*********************************************************************
* Author : Himangshu Saikia
* Email : [email protected]
* Project : Merge Tree Library
*
*********************************************************************
*/
#include "ScalarField.h"
namespace mtlib {
ScalarField::ScalarField() {}
ScalarField::~ScalarField() {}
void ScalarField::init(const Dimensions& d) {
m_dims.nx = d.nx;
m_dims.ny = d.ny;
m_dims.nz = d.nz;
m_data.clear();
m_data.resize(m_dims.getNumVoxels());
minValue_ = std::numeric_limits<double>::max();
maxValue_ = std::numeric_limits<double>::lowest();
}
void ScalarField::setDataAt(const size_t& idx, const double val) {
m_data[idx] = val;
minValue_ = std::min(minValue_, val);
maxValue_ = std::max(maxValue_, val);
}
double ScalarField::F(const size_t& idx) const {
return m_data[idx];
}
Dimensions ScalarField::getDims() const {
return m_dims;
}
Vertex3D ScalarField::getVertex3(const size_t& idx, const Dimensions& dims) {
return Vertex3D(idx % dims.nx, (idx / dims.nx) % dims.ny, (idx / (dims.nx * dims.ny)) % dims.nz);
}
size_t ScalarField::getVertex(const Vertex3D& v, const Dimensions& dims) {
return dims.nx * (dims.ny * v.z + v.y) + v.x;
}
void ScalarField::getNeighbors(const size_t& idx, const Dimensions& dims, std::vector<size_t>& neighbors) {
neighbors.clear();
const int d[3] = { -1, 0, 1 };
Vertex3D v = getVertex3(idx, dims);
int X = (int)v.x;
int Y = (int)v.y;
int Z = (int)v.z;
for (size_t i = 0; i < 3; ++i)
{
if ((X + d[i] < 0) || (X + d[i] > dims.nx - 1)) continue;
for (size_t j = 0; j < 3; ++j)
{
if ((Y + d[j] < 0) || (Y + d[j] > dims.ny - 1)) continue;
for (size_t k = 0; k < 3; ++k)
{
if ((Z + d[k] < 0) || (Z + d[k] > dims.nz - 1)) continue;
if (d[i] == 0 && d[j] == 0 && d[k] == 0) continue;
//printf("[x y z] = [%d %d %d]\n", dx[i], dy[j], dz[k]);
Vertex3D nv(v.x + d[i], v.y + d[j], v.z + d[k]);
neighbors.push_back(getVertex(nv, dims));
}
}
}
}
double ScalarField::normEuclideanDist2(const size_t & idx1, const size_t & idx2, const Dimensions& dims)
{
Vertex3D v1 = getVertex3(idx1, dims);
Vertex3D v2 = getVertex3(idx2, dims);
Vertex3Df v1f(static_cast<double>(v1.x) / dims.nx, static_cast<double>(v1.y) / dims.ny, static_cast<double>(v1.z) / dims.nz);
Vertex3Df v2f(static_cast<double>(v2.x) / dims.nx, static_cast<double>(v2.y) / dims.ny, static_cast<double>(v2.z) / dims.nz);
return (v1f.x - v2f.x)*(v1f.x - v2f.x) + (v1f.y - v2f.y)*(v1f.y - v2f.y) + (v1f.z - v2f.z)*(v1f.z - v2f.z);
}
void ScalarField::sortMap(std::function<bool(Voxel, Voxel)> comp, std::vector<size_t>& vertexOrder) const {
size_t numVox = m_dims.getNumVoxels();
vertexOrder.clear();
vertexOrder.resize(numVox);
std::vector<Voxel> Map(numVox);
for (size_t i = 0; i < numVox; i++) {
Map[i].idx = i;
Map[i].val = F(i);
}
sort(Map.begin(), Map.end(), comp);
for (size_t i = 0; i < Map.size(); i++) {
vertexOrder[i] = Map[i].idx;
}
}
double ScalarField::getValueRange() const
{
return maxValue_ - minValue_;
}
double ScalarField::getMinVal() const
{
return minValue_;
}
double ScalarField::getMaxVal() const
{
return maxValue_;
}
double ScalarField::getHeightDifference(const size_t & idx1, const size_t & idx2) const
{
return std::fabs(F(idx1) - F(idx2));
}
double ScalarField::getNormalizedHeightDifference(const size_t & idx1, const size_t & idx2) const
{
return std::fabs(F(idx1) - F(idx2)) / (maxValue_ - minValue_);
}
}