# marching-cubes **Repository Path**: wangjq4214/marching-cubes ## Basic Information - **Project Name**: marching-cubes - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-12-05 - **Last Updated**: 2021-12-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Marching Cubes > 代码没有使用 OpenGL 将图形绘制出来,尝试后效果并不好,直接展示 PhotoShop 中的结果。 ## 主要代码 ```cpp void MarchingCubes::exec() { int nx = data->get_n(); int ny = data->get_n(); int nz = data->get_n(); const mc_vec3i size = {static_cast(nx), static_cast(ny), static_cast(nz)}; mc_float vs[8] = {0, 0, 0, 0, 0, 0, 0, 0}; m_uint edge_indices[12]; auto *slab_idx = new mc_vec3i[nx * ny * 2]; auto field = data->get_field(); for (m_uint z = 0; z < nz - 1; z++) { for (m_uint y = 0; y < ny - 1; y++) { for (m_uint x = 0; x < nx - 1; x++) { vs[0] = field[mc_internal_to_index1D(x, y, z, size)]; vs[1] = field[mc_internal_to_index1D(x + 1, y, z, size)]; vs[2] = field[mc_internal_to_index1D(x, y + 1, z, size)]; vs[3] = field[mc_internal_to_index1D(x + 1, y + 1, z, size)]; vs[4] = field[mc_internal_to_index1D(x, y, z + 1, size)]; vs[5] = field[mc_internal_to_index1D(x + 1, y, z + 1, size)]; vs[6] = field[mc_internal_to_index1D(x, y + 1, z + 1, size)]; vs[7] = field[mc_internal_to_index1D(x + 1, y + 1, z + 1, size)]; const int config_n = ((vs[0] < 0) << 0) | ((vs[1] < 0) << 1) | ((vs[2] < 0) << 2) | ((vs[3] < 0) << 3) | ((vs[4] < 0) << 4) | ((vs[5] < 0) << 5) | ((vs[6] < 0) << 6) | ((vs[7] < 0) << 7); if (config_n == 0 || config_n == 255) continue; if (y == 0 && z == 0) mc_internal_compute_edge(slab_idx, vs[0], vs[1], 0, x, y, z, size); if (z == 0) mc_internal_compute_edge(slab_idx, vs[2], vs[3], 0, x, y + 1, z, size); if (y == 0) mc_internal_compute_edge(slab_idx, vs[4], vs[5], 0, x, y, z + 1, size); mc_internal_compute_edge(slab_idx, vs[6], vs[7], 0, x, y + 1, z + 1, size); if (x == 0 && z == 0) mc_internal_compute_edge(slab_idx, vs[0], vs[2], 1, x, y, z, size); if (z == 0) mc_internal_compute_edge(slab_idx, vs[1], vs[3], 1, x + 1, y, z, size); if (x == 0) mc_internal_compute_edge(slab_idx, vs[4], vs[6], 1, x, y, z + 1, size); mc_internal_compute_edge(slab_idx, vs[5], vs[7], 1, x + 1, y, z + 1, size); if (x == 0 && y == 0) mc_internal_compute_edge(slab_idx, vs[0], vs[4], 2, x, y, z, size); if (y == 0) mc_internal_compute_edge(slab_idx, vs[1], vs[5], 2, x + 1, y, z, size); if (x == 0) mc_internal_compute_edge(slab_idx, vs[2], vs[6], 2, x, y + 1, z, size); mc_internal_compute_edge(slab_idx, vs[3], vs[7], 2, x + 1, y + 1, z, size); edge_indices[0] = slab_idx[mc_internal_to_index1D_slab(x, y, z, size)].x; edge_indices[1] = slab_idx[mc_internal_to_index1D_slab(x, y + 1, z, size)].x; edge_indices[2] = slab_idx[mc_internal_to_index1D_slab(x, y, z + 1, size)].x; edge_indices[3] = slab_idx[mc_internal_to_index1D_slab(x, y + 1, z + 1, size)].x; edge_indices[4] = slab_idx[mc_internal_to_index1D_slab(x, y, z, size)].y; edge_indices[5] = slab_idx[mc_internal_to_index1D_slab(x + 1, y, z, size)].y; edge_indices[6] = slab_idx[mc_internal_to_index1D_slab(x, y, z + 1, size)].y; edge_indices[7] = slab_idx[mc_internal_to_index1D_slab(x + 1, y, z + 1, size)].y; edge_indices[8] = slab_idx[mc_internal_to_index1D_slab(x, y, z, size)].z; edge_indices[9] = slab_idx[mc_internal_to_index1D_slab(x + 1, y, z, size)].z; edge_indices[10] = slab_idx[mc_internal_to_index1D_slab(x, y + 1, z, size)].z; edge_indices[11] = slab_idx[mc_internal_to_index1D_slab(x + 1, y + 1, z, size)].z; const uint64_t &config = mc_internal_marching_cube_tris[config_n]; const size_t n_triangles = config & 0xF; const size_t n_indices = n_triangles * 3; const size_t &indexBase = mesh->indices.size(); int offset = 4; for (size_t i = 0; i < n_indices; i++) { const int edge = (config >> offset) & 0xF; mesh->indices.push_back(edge_indices[edge]); offset += 4; } for (size_t i = 0; i < n_triangles; i++) { mc_internal_accumulate_normal(mesh->indices[indexBase + i * 3 + 0], mesh->indices[indexBase + i * 3 + 1], mesh->indices[indexBase + i * 3 + 2]); } } } } for (auto &normal: mesh->normals) { normal = Utils::mc_internal_normalize(normal); } delete[] slab_idx; } ``` 通过存储 Marching Cubes 划分情况进行匹配,通过线性插值确定值。 ## 实验结果 img