-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarchingCube.cpp
More file actions
165 lines (148 loc) · 6.69 KB
/
Copy pathMarchingCube.cpp
File metadata and controls
165 lines (148 loc) · 6.69 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2025 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <SofaImplicitField/config.h>
#include <SofaImplicitField/MarchingCube.h>
#include <sofa/helper/MarchingCubeUtility.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <SofaImplicitField/components/geometry/ScalarField.h>
#include <sofa/type/Vec.h>
namespace sofaimplicitfield
{
void MarchingCube::generateSurfaceMesh(const double isoval, const double mstep, const double invStep,
const Vec3d& gridmin, const Vec3d& gridmax,
std::function<void(std::vector<Vec3d>&, std::vector<double>&)> getFieldValueAt,
SeqCoord& tmpPoints, SeqTriangles& tmpTriangles)
{
int nx = floor((gridmax.x() - gridmin.x()) * invStep) + 1 ;
int ny = floor((gridmax.y() - gridmin.y()) * invStep) + 1 ;
int nz = floor((gridmax.z() - gridmin.z()) * invStep) + 1 ;
// Marching cubes only works for a grid size larger than two
if( nz < 2 || ny < 2 || nx < 2 )
return;
double cx,cy,cz;
int z,mk;
const int *tri;
// Creates two planes
CubeData c{{-1,-1,-1},0};
planes.resize(2*nx*ny);
for(size_t i=0;i<planes.size();++i)
{
planes[i] = c;
}
// Keep two pointers for the first plane and the seconds
P0 = planes.begin();
P1 = planes.begin()+nx*ny;
const int dx = 1;
const int dy = nx;
z = 0;
auto fillPlane = [getFieldValueAt](std::vector<Vec3d> &positions, std::vector<double>& output,
double mstep, double gridmin_y, double gridmin_x, int ny, int nx, float cz,
std::vector<CubeData>::iterator itDestPlane)
{
for (int i=0, y = 0 ; y < ny ; ++y)
{
double cy = gridmin_y + mstep * y ;
for (int x = 0 ; x < nx ; ++x)
{
double cx = gridmin_x + mstep * x ;
positions[i++].set(cx, cy, cz );
}
}
getFieldValueAt(positions, output) ;
for(auto res : output){
itDestPlane->data = res;
itDestPlane++;
}
};
std::vector<Vec3d> positions;
std::vector<double> output;
positions.resize(nx*ny);
output.resize(nx*ny);
fillPlane(positions, output, mstep, gridmin.y(), gridmin.x(), ny, nx, gridmin.z(), P0);
for (z=1; z<=nz; ++z)
{
fillPlane(positions, output, mstep, gridmin.y(), gridmin.x(), ny, nx, gridmin.z() + mstep * z, P1);
int edgecube[12];
const int edgepts[12] = {0,1,0,1,0,1,0,1,2,2,2,2};
typename std::vector<CubeData>::iterator base = planes.begin();
int ip0 = P0-base;
int ip1 = P1-base;
edgecube[0] = (ip0 -dy);
edgecube[1] = (ip0 );
edgecube[2] = (ip0 );
edgecube[3] = (ip0-dx );
edgecube[4] = (ip1 -dy);
edgecube[5] = (ip1 );
edgecube[6] = (ip1 );
edgecube[7] = (ip1-dx );
edgecube[8] = (ip1-dx-dy);
edgecube[9] = (ip1-dy );
edgecube[10] = (ip1 );
edgecube[11] = (ip1-dx );
unsigned int di = nx;
for(int y=1; y<ny; y++)
{
// First column is all zero
int x=0;
++di;
for(x=1; x<nx; x++)
{
Vec3d pos(x, y, z);
if (((P1+di)->data>isoval)^((P1+di-dx)->data>isoval))
{
(P1+di)->p[0] = addPoint(tmpPoints, 0, pos,gridmin, (P1+di)->data,(P1+di-dx)->data, mstep, isoval);
}
if (((P1+di)->data>isoval)^((P1+di-dy)->data>isoval))
{
(P1+di)->p[1] = addPoint(tmpPoints, 1, pos,gridmin,(P1+di)->data,(P1+di-dy)->data, mstep, isoval);
}
if (((P1+di)->data>isoval)^((P0+di)->data>isoval))
{
(P1+di)->p[2] = addPoint(tmpPoints, 2, pos,gridmin,(P1+di)->data,(P0+di)->data, mstep, isoval);
}
// All points should now be created
if ((P0+di-dx-dy)->data > isoval) mk = 1;
else mk=0;
if ((P0+di -dy)->data > isoval) mk|= 2;
if ((P0+di )->data > isoval) mk|= 4;
if ((P0+di-dx )->data > isoval) mk|= 8;
if ((P1+di-dx-dy)->data > isoval) mk|= 16;
if ((P1+di -dy)->data > isoval) mk|= 32;
if ((P1+di )->data > isoval) mk|= 64;
if ((P1+di-dx )->data > isoval) mk|= 128;
tri=sofa::helper::MarchingCubeTriTable[mk];
while (*tri>=0)
{
typename std::vector<CubeData>::iterator b = base+di;
addFace(tmpTriangles,
(b+edgecube[tri[0]])->p[edgepts[tri[0]]],
(b+edgecube[tri[1]])->p[edgepts[tri[1]]],
(b+edgecube[tri[2]])->p[edgepts[tri[2]]], tmpPoints.size());
tri+=3;
}
++di;
}
}
std::swap(P0, P1);
}
}
}