This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrganicHead.cpp
63 lines (58 loc) · 1.95 KB
/
OrganicHead.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
#include "OrganicHead.h"
#include <math.h>
#include <algorithm>
OrganicHead::OrganicHead(float l, float h, float w, float gs, double t, OrganicShapes os)
:length(l), height(h), width(w), gridsize(gs), threshold(t), shape(os)
{
initializeCubes();
}
double OrganicHead::getIsoLevel(Vec3f p) {
float radius[3]{ length / 2, height / 2, width / 2 };
switch (shape) {
case ELLIPSOID: {
// the ellipsoid function : 1/x_rad x^2 + 1/y_rad y^2 + 1/z_rad z^2 = 1
double coef[3] = { 1 / pow(radius[0], 2), 1 / pow(radius[1], 2), 1 / pow(radius[2], 2) };
double isoLevel = 0;
for (int i = 0; i < 3; ++i)
isoLevel += (coef[i] * pow(p[i] - radius[i], 2));
return isoLevel;
}
case HYPERBOLOID: {
// the hyperboloid function: 1/x_rad x^2 + 1/y_rad y^2 - 1/z_rad z^2 = 1
double coef[3] = { 1 / (radius[0] * radius[0]), 1 / (radius[1] * radius[1]), - 1 / (radius[2] * radius[2]) };
double isoLevel = 0;
for (int i = 0; i < 3; ++i)
isoLevel += (coef[i] * pow(p[i] - radius[i], 2));
return isoLevel;
}
}
}
void OrganicHead::initializeCubes() {
// GRIDCELL has p (Vec3f) for all the points, and val (double) for all the isovalues
for (float x = 0; x < length; x += gridsize) {
for (float y = 0; y < height; y += gridsize) {
for (float z = 0; z < width; z += gridsize) {
GRIDCELL g;
g.p[0] = { x, y, z };
g.p[1] = { x + gridsize, y, z };
g.p[2] = { x + gridsize, y + gridsize, z };
g.p[3] = { x, y + gridsize, z };
g.p[4] = { x, y, z + gridsize };
g.p[5] = { x + gridsize, y, z + gridsize };
g.p[6] = { x + gridsize, y + gridsize, z + gridsize };
g.p[7] = { x, y + gridsize, z + gridsize };
for (int index = 0; index < 8; ++index) {
g.val[index] = getIsoLevel(g.p[index]);
}
MarchingCube cube{ g };
cubes.push_back(cube);
}
}
}
}
void OrganicHead::draw() {
// call polygonize for all the cubes in me
std::for_each(cubes.begin(), cubes.end(), [&](MarchingCube cube) {
cube.Polygonise(threshold);
});
}