-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesh.cpp
76 lines (67 loc) · 2.18 KB
/
mesh.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
/*
* mesh.cpp
*
* Created on: Dec 11, 2013
* Author: Owner
*/
#include "mesh.h"
#include <math.h>
Mesh::Mesh() {
width = 0;
height = 0;
depth = 0;
threshold = 0;
}
/* A mesh WIDTH boxes wide, HEIGHT boxes tall, DEPTH boxes deep. */
Mesh::Mesh(int width, int height, int depth, float rightbound, float leftbound, float highbound, float lowbound, float closebound, float farbound) {
this->width = width;
this->height = height;
this->depth = depth;
float cubeW = (rightbound - leftbound) / (float) width;
float cubeH = (highbound - lowbound) / (float) height; //height of a cube
float cubeD = (closebound - farbound) / (float) depth;
threshold = .1 * min(cubeW, min(cubeH, cubeD));
for (float z = closebound; z >= (farbound - threshold); z-=cubeD) {
for (float y = highbound; y >= (lowbound - threshold); y-=cubeH) {
for (float x = leftbound; x <= (rightbound + threshold); x+=cubeW) {
mesh.push_back(CubeVertex(Point3D(x, y, z)));
}
}
}
}
CubeVertex Mesh::getVertexAt(int x, int y, int z) {
return mesh[(z * (width+1) * (height+1)) + (y * (width+1)) + x]; //wat
}
void Mesh::setVertexAt(int x, int y, int z, const CubeVertex &c) {
mesh[(z * (width+1) * (height+1)) + (y * (height+1)) + x] = c;
}
void Mesh::updateColors() {
#pragma omp parallel for
for (int i = 0; i < (int) mesh.size(); i++) {
Particle p = mesh[i].getParticle();
mesh[i].setColor(pSystem.colorFunction(p));
}
}
void Mesh::marchingCubes(vector<float> &triangles, vector<float> &normals) {
Cube c;
vector<CubeVertex> cv;
vector<Point3D> newtriangs;
//#pragma omp parallel for
for (int z = 0; z < depth; z++) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
cv.resize(0); //reset the vertices
cv.push_back(getVertexAt(x, y+1, z+1)); //Corner 0, according to Bourke's picture
cv.push_back(getVertexAt(x+1, y+1, z+1));
cv.push_back(getVertexAt(x+1, y+1, z));
cv.push_back(getVertexAt(x, y+1, z));
cv.push_back(getVertexAt(x, y, z+1));
cv.push_back(getVertexAt(x+1, y, z+1));
cv.push_back(getVertexAt(x+1, y, z));
cv.push_back(getVertexAt(x, y, z));
c.setVertices(cv);
c.getTriangles(0.5, triangles, normals, threshold);
}
}
}
}