Skip to content

Commit

Permalink
meshgen3 subdivision resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
harry7557558 committed Mar 7, 2024
1 parent ca5e79e commit bed81d7
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 143 deletions.
32 changes: 32 additions & 0 deletions meshgen3/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,38 @@
<span title="smooth shading"><input type="checkbox" id="checkbox-normal" />normal</span>&ensp;
<span title="Light theme" style="display:none;"><input type="checkbox" id="checkbox-light" checked disabled /></span>
<br />
<span>domain&nbsp;radius&nbsp;<select id="select-br">
<option value="0.1">0.1</option>
<option value="0.2">0.2</option>
<option value="0.5">0.5</option>
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
</select></span>
<br/>
<span>initial&nbsp;res&nbsp;<select id="select-bn">
<option value="32">32</option>
<option value="48" selected>48</option>
<option value="64">64</option>
<option value="96">96</option>
<option value="128">128</option>
<option value="192">192</option>
<option value="256">256</option>
</select>&nbsp;target&nbsp;res&nbsp;<select id="select-dn">
<option value="64">64</option>
<option value="96">96</option>
<option value="128">128</option>
<option value="192" selected>192</option>
<option value="256">256</option>
<option value="384">384</option>
<option value="512">512</option>
<!-- <option value="768">768</option>
<option value="1024">1024</option> -->
</select></span>
<br/>
<button onclick="decimateMesh(false, false)">optimize mesh</button>
<button onclick="decimateMesh(true, false)">+shape</button>
<!-- <button onclick="decimateMesh(true, true)">+angle</button> -->
Expand Down
48 changes: 35 additions & 13 deletions meshgen3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <cstdio>
#include <random>

#include "render.h"

#include "meshgen_tet_implicit.h"

#include "../include/write_model.h"
Expand All @@ -31,6 +29,23 @@ struct DecimationParameters {
};


namespace MeshgenParams {
vec3 bc;
vec3 br;
ivec3 bn;
int nd;
};

EXTERN EMSCRIPTEN_KEEPALIVE
void setMeshgenParams(float br, int bn, int nd) {
MeshgenParams::bc = vec3(0);
MeshgenParams::br = vec3(br);
MeshgenParams::bn = ivec3(bn);
MeshgenParams::nd = nd;
}

#include "render.h"

void generateMesh(
std::string funDeclaration,
std::vector<vec3> &verts,
Expand All @@ -40,7 +55,8 @@ void generateMesh(

GlBatchEvaluator3 evaluator(funDeclaration);
int batchEvalCount = 0;
vec3 bc = vec3(0), br = vec3(2);
vec3 bc = MeshgenParams::bc;
vec3 br = MeshgenParams::br;
MeshgenTetImplicit::ScalarFieldFBatch Fs = [&](size_t n, const vec3 *p, float *v) {
// printf("Batch eval %d %d\n", ++batchEvalCount, (int)n);
evaluator.evaluateFunction(n, p, v);
Expand Down Expand Up @@ -88,8 +104,8 @@ void generateMesh(
verts, tets, faces, edges, 5, Fs,
constraint, isConstrained);
#else
const ivec3 bn = ivec3(36);
const int nd = 2;
const ivec3 bn = MeshgenParams::bn;
const int nd = MeshgenParams::nd;
vec3 expd = 1.0f + 0.02f/vec3(bn-1)*exp2f(-nd);
MeshgenTetImplicit::marchingCubes(
Fs, bc-expd*br, bc+expd*br,
Expand All @@ -99,7 +115,8 @@ void generateMesh(
if (decimate.decimate) {
MeshgenTetImplicit::restoreEdges(faces, edges);
MeshgenTetImplicit::MeshDecimatorEC(verts, faces, edges,
0.3f/length(vec3(bn-1))*exp2f(-nd),
// 0.3f/length(vec3(bn-1))*exp2f(-nd),
0.02f/sqrt(length(vec3(bn-1))*exp2f(nd)),
decimate.shapeCost, decimate.angleCost).decimateMesh();
}
else {
Expand Down Expand Up @@ -223,6 +240,17 @@ namespace Prepared {
std::vector<ivec4> edges;
}

EXTERN EMSCRIPTEN_KEEPALIVE
void regenerateMesh() {
float t0 = getTimePast();
generateMesh(glslFun,
Prepared::verts, Prepared::tets, Prepared::faces, Prepared::edges,
{ false, 0.0f, 0.0f });
float t1 = getTimePast();
printf("Total %.2g secs.\n \n", t1 - t0);
renderModel = prepareMesh(Prepared::verts, Prepared::tets, Prepared::faces, Prepared::edges);
}

void mainGUICallback() {
if (newGlslFun.empty())
return;
Expand All @@ -236,13 +264,7 @@ void mainGUICallback() {
return;
#endif

float t0 = getTimePast();
generateMesh(glslFun,
Prepared::verts, Prepared::tets, Prepared::faces, Prepared::edges,
{ false, 0.0f, 0.0f });
float t1 = getTimePast();
printf("Total %.2g secs.\n \n", t1 - t0);
renderModel = prepareMesh(Prepared::verts, Prepared::tets, Prepared::faces, Prepared::edges);
regenerateMesh();
}

EXTERN EMSCRIPTEN_KEEPALIVE
Expand Down
16 changes: 8 additions & 8 deletions meshgen3/marching_cubes.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void marchingCubes(
auto getI = [&](int i, int j, int k) {
return (k*bnd.y+j)*bnd.x+i;
};
vec3 noise = 1e-2f/vec3(bn-1)*exp2f(-nd);
vec3 noise = vec3(1e-2f);
auto idxToPoint = [&](int idx) {
int k = idx / (bnd.x*bnd.y);
int j = (idx / bnd.x) % bnd.y;
Expand Down Expand Up @@ -268,7 +268,7 @@ void marchingCubes(

// construct triangles
for (int t = 0; MC_TRIG_TABLE[cubeIndex][t] != -1; t += 3) {
trigs.push_back(vec3(
trigs.push_back(ivec3(
eidx[MC_TRIG_TABLE[cubeIndex][t]],
eidx[MC_TRIG_TABLE[cubeIndex][t+1]],
eidx[MC_TRIG_TABLE[cubeIndex][t+2]]
Expand Down Expand Up @@ -304,12 +304,12 @@ void marchingCubes(
vertices.resize(edges.size());
#if 0
// linear interpolation
for (std::pair<ivec2, int> ei : edges) {
int i1 = ei.first.x, i2 = ei.first.y;
float v1 = vals[i1];
float v2 = vals[i2];
for (int i = 0; i < (int)edges.size(); i++) {
int i1 = (int)(edges[i] >> 32), i2 = (int)edges[i];
float v1 = samples[i1];
float v2 = samples[i2];
float t = v1 / (v1 - v2);
vertices[ei.second] = idxToPoint(i1) * (1 - t) + idxToPoint(i2) * t;
vertices[i] = idxToPoint(i1) * (1.0f-t) + idxToPoint(i2) * t;
}
#else
// quadratic interpolation
Expand All @@ -318,7 +318,7 @@ void marchingCubes(
std::vector<float> edgevc(edges.size());
for (int i = 0; i < (int)edges.size(); i++) {
int i1 = (int)(edges[i] >> 32), i2 = (int)edges[i];
edgei[i] = vec2(i1, i2);
edgei[i] = ivec2(i1, i2);
edgep[i] = 0.5f*(idxToPoint(i1)+idxToPoint(i2));
}
Fs(edgep.size(), &edgep[0], &edgevc[0]);
Expand Down
Loading

0 comments on commit bed81d7

Please sign in to comment.