Skip to content

Commit

Permalink
Remove mesh (#950)
Browse files Browse the repository at this point in the history
* removed a bunch of mesh

* compiles

* fix test

* cleanup

* fix compile issues
  • Loading branch information
elalish authored Sep 27, 2024
1 parent 31d6da0 commit 9f62dbb
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 320 deletions.
7 changes: 5 additions & 2 deletions bindings/wasm/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ std::vector<Manifold> SplitByPlane(Manifold& m, vec3 normal,
}

void CollectVertices(std::vector<vec3>& verts, const Manifold& manifold) {
Mesh mesh = manifold.GetMesh();
verts.insert(verts.end(), mesh.vertPos.begin(), mesh.vertPos.end());
const MeshGL64 mesh = manifold.GetMeshGL64();
const auto numVert = mesh.NumVert();
for (size_t v = 0; v < numVert; ++v) {
verts.push_back(mesh.GetVertPos(v));
}
}
} // namespace man_js
72 changes: 0 additions & 72 deletions meshIO/src/meshIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,76 +258,4 @@ void ExportMesh(const std::string& filename, const MeshGL& mesh,

ExportScene(scene, filename);
}

/**
* Saves the Mesh to the desired file type, determined from the extension
* specified. In the case of .glb/.gltf, this will save in version 2.0.
*
* This is a very simple export function and is intended primarily as a
* demonstration. Generally users of this library will need to modify this to
* write all the important properties for their application and read any custom
* data structures.
*
* @param filename The file extension must be one that Assimp supports for
* export. GLB & 3MF are recommended.
* @param mesh The mesh to export, likely from Manifold.GetMesh().
* @param options The options currently only affect an exported GLB's material.
* Pass {} for defaults.
*/
void ExportMesh(const std::string& filename, const Mesh& mesh,
const ExportOptions& options) {
if (mesh.triVerts.size() == 0) {
std::cout << filename << " was not saved because the input mesh was empty."
<< std::endl;
return;
}

std::string type = GetType(filename);
const bool isYup = type == "glb2" || type == "gltf2";

aiScene* scene = CreateScene(options);
aiMesh* mesh_out = scene->mMeshes[0];

mesh_out->mNumVertices = mesh.vertPos.size();
mesh_out->mVertices = new aiVector3D[mesh_out->mNumVertices];
if (!options.faceted) {
DEBUG_ASSERT(
mesh.vertNormal.size() == mesh.vertPos.size(), userErr,
"vertNormal must be the same length as vertPos when faceted is false.");
mesh_out->mNormals = new aiVector3D[mesh_out->mNumVertices];
}
if (!options.mat.vertColor.empty()) {
DEBUG_ASSERT(mesh.vertPos.size() == options.mat.vertColor.size(), userErr,
"If present, vertColor must be the same length as vertPos.");
mesh_out->mColors[0] = new aiColor4D[mesh_out->mNumVertices];
}

for (size_t i = 0; i < mesh_out->mNumVertices; ++i) {
const vec3& v = mesh.vertPos[i];
mesh_out->mVertices[i] =
isYup ? aiVector3D(v.y, v.z, v.x) : aiVector3D(v.x, v.y, v.z);
if (!options.faceted) {
const vec3& n = mesh.vertNormal[i];
mesh_out->mNormals[i] =
isYup ? aiVector3D(n.y, n.z, n.x) : aiVector3D(n.x, n.y, n.z);
}
if (!options.mat.vertColor.empty()) {
const vec4& c = options.mat.vertColor[i];
mesh_out->mColors[0][i] = aiColor4D(c.r, c.g, c.b, c.a);
}
}

mesh_out->mNumFaces = mesh.triVerts.size();
mesh_out->mFaces = new aiFace[mesh_out->mNumFaces];

for (size_t i = 0; i < mesh_out->mNumFaces; ++i) {
aiFace& face = mesh_out->mFaces[i];
face.mNumIndices = 3;
face.mIndices = new glm::uint[face.mNumIndices];
for (int j : {0, 1, 2}) face.mIndices[j] = mesh.triVerts[i][j];
}

ExportScene(scene, filename);
}

} // namespace manifold
16 changes: 11 additions & 5 deletions src/manifold/include/manifold/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,25 @@ struct MeshGLP {

bool Merge();

vec3 GetVertPos(size_t i) const {
glm::vec<3, Precision> GetVertPos(size_t i) const {
size_t offset = i * numProp;
return vec3(vertProperties[offset], vertProperties[offset + 1],
vertProperties[offset + 2]);
return glm::vec<3, Precision>(vertProperties[offset],
vertProperties[offset + 1],
vertProperties[offset + 2]);
}

glm::vec<3, I> GetTriVerts(size_t i) const {
size_t offset = 3 * i;
return glm::vec<3, I>(triVerts[offset], triVerts[offset + 1],
triVerts[offset + 2]);
}

glm::vec<4, Precision> GetTangent(size_t i) const {
size_t offset = 4 * i;
return glm::vec<4, Precision>(
halfedgeTangent[offset], halfedgeTangent[offset + 1],
halfedgeTangent[offset + 2], halfedgeTangent[offset + 3]);
}
};

/**
Expand Down Expand Up @@ -168,7 +176,6 @@ class Manifold {
Manifold& operator=(Manifold&&) noexcept;

Manifold(const MeshGL&);
Manifold(const Mesh&);
Manifold(const MeshGL64&);

static Manifold Smooth(const MeshGL&,
Expand Down Expand Up @@ -204,7 +211,6 @@ class Manifold {
* Details of the manifold
*/
///@{
Mesh GetMesh() const;
MeshGL GetMeshGL(ivec3 normalIdx = ivec3(0)) const;
MeshGL64 GetMeshGL64(ivec3 normalIdx = ivec3(0)) const;
bool IsEmpty() const;
Expand Down
50 changes: 0 additions & 50 deletions src/manifold/src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,56 +251,6 @@ uint32_t Manifold::Impl::ReserveIDs(uint32_t n) {
return Manifold::Impl::meshIDCounter_.fetch_add(n, std::memory_order_relaxed);
}

Manifold::Impl::Impl(const Mesh& mesh, const MeshRelationD& relation,
const std::vector<double>& propertyTolerance,
bool hasFaceIDs)
: vertPos_(mesh.vertPos), halfedgeTangent_(mesh.halfedgeTangent) {
meshRelation_ = {relation.originalID, relation.numProp, relation.properties,
relation.meshIDtransform};

Vec<ivec3> triVerts;
for (size_t i = 0; i < mesh.triVerts.size(); ++i) {
const ivec3 tri = mesh.triVerts[i];
// Remove topological degenerates
if (tri[0] != tri[1] && tri[1] != tri[2] && tri[2] != tri[0]) {
triVerts.push_back(tri);
if (relation.triRef.size() > 0) {
meshRelation_.triRef.push_back(relation.triRef[i]);
}
if (relation.triProperties.size() > 0) {
meshRelation_.triProperties.push_back(relation.triProperties[i]);
}
}
}

if (!IsIndexInBounds(triVerts)) {
MarkFailure(Error::VertexOutOfBounds);
return;
}

CreateHalfedges(triVerts);
if (!IsManifold()) {
MarkFailure(Error::NotManifold);
return;
}

CalculateBBox();
if (!IsFinite()) {
MarkFailure(Error::NonFiniteVertex);
return;
}
SetPrecision(mesh.precision);

SplitPinchedVerts();

CalculateNormals();

InitializeOriginal();

SimplifyTopology();
Finish();
}

/**
* Create either a unit tetrahedron, cube or octahedron. The cube is in the
* first octant, while the others are symmetric about the origin.
Expand Down
4 changes: 0 additions & 4 deletions src/manifold/src/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ struct Manifold::Impl {
meshRelation_.originalID = -1;
}

Impl(const Mesh&, const MeshRelationD& relation,
const std::vector<double>& propertyTolerance = {},
bool hasFaceIDs = false);

inline void ForVert(int halfedge, std::function<void(int halfedge)> func) {
int current = halfedge;
do {
Expand Down
44 changes: 0 additions & 44 deletions src/manifold/src/manifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,50 +293,6 @@ Manifold::Manifold(const MeshGL& meshGL)
Manifold::Manifold(const MeshGL64& meshGL64)
: pNode_(std::make_shared<CsgLeafNode>(std::make_shared<Impl>(meshGL64))) {}

/**
* Convert a Mesh into a Manifold. Will return an empty Manifold
* and set an Error Status if the Mesh is not an oriented 2-manifold. Will
* collapse degenerate triangles and unnecessary vertices.
*
* @param mesh The input Mesh.
*/
Manifold::Manifold(const Mesh& mesh) {
Impl::MeshRelationD relation;
pNode_ =
std::make_shared<CsgLeafNode>(std::make_shared<Impl>(mesh, relation));
}

/**
* This returns a Mesh of simple vectors of vertices and triangles suitable for
* saving or other operations outside of the context of this library.
*/
Mesh Manifold::GetMesh() const {
ZoneScoped;
const Impl& impl = *GetCsgLeafNode().GetImpl();

Mesh result;
result.precision = Precision();
result.vertPos.insert(result.vertPos.end(), impl.vertPos_.begin(),
impl.vertPos_.end());
result.vertNormal.insert(result.vertNormal.end(), impl.vertNormal_.begin(),
impl.vertNormal_.end());
result.halfedgeTangent.insert(result.halfedgeTangent.end(),
impl.halfedgeTangent_.begin(),
impl.halfedgeTangent_.end());

result.triVerts.resize(NumTri());
auto& triVerts = result.triVerts;
const auto& halfedges = impl.halfedge_;
for_each_n(autoPolicy(NumTri(), 1e5), countAt(0), NumTri(),
[&triVerts, &halfedges](const int tri) {
for (int i : {0, 1, 2}) {
triVerts[tri][i] = halfedges[3 * tri + i].startVert;
}
});

return result;
}

/**
* The most complete output of this library, returning a MeshGL that is designed
* to easily push into a renderer, including all interleaved vertex properties
Expand Down
25 changes: 0 additions & 25 deletions src/utilities/include/manifold/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,6 @@ using SimplePolygon = std::vector<vec2>;
*/
using Polygons = std::vector<SimplePolygon>;

/**
* The triangle-mesh input and output of this library.
*/
struct Mesh {
/// Required: The X-Y-Z positions of all vertices.
std::vector<vec3> vertPos;
/// Required: The vertex indices of the three triangle corners in CCW (from
/// the outside) order, for each triangle.
std::vector<ivec3> triVerts;
/// Optional: The X-Y-Z normal vectors of each vertex. If non-empty, must have
/// the same length as vertPos. If empty, these will be calculated
/// automatically.
std::vector<vec3> vertNormal;
/// Optional: The X-Y-Z-W weighted tangent vectors for smooth Refine(). If
/// non-empty, must be exactly three times as long as Mesh.triVerts. Indexed
/// as 3 * tri + i, representing the tangent from Mesh.triVerts[tri][i] along
/// the CCW edge. If empty, mesh is faceted.
std::vector<vec4> halfedgeTangent;
/// The absolute precision of the vertex positions, based on accrued rounding
/// errors. When creating a Manifold, the precision used will be the maximum
/// of this and a baseline precision from the size of the bounding box. Any
/// edge shorter than precision may be collapsed.
double precision = 0;
};

/**
* Defines which edges to sharpen and how much for the Manifold.Smooth()
* constructor.
Expand Down
65 changes: 49 additions & 16 deletions test/boolean_complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,64 @@ TEST(BooleanComplex, Cylinders) {
}

TEST(BooleanComplex, Subtract) {
Mesh firstMesh;
firstMesh.vertPos = {{0, 0, 0}, {1540, 0, 0},
{1540, 70, 0}, {0, 70, 0},
{0, 0, -278.282}, {1540, 70, -278.282},
{1540, 0, -278.282}, {0, 70, -278.282}};
MeshGL firstMesh;
firstMesh.vertProperties = {
0, 0, 0, //
1540, 0, 0, //
1540, 70, 0, //
0, 70, 0, //
0, 0, -278.282, //
1540, 70, -278.282, //
1540, 0, -278.282, //
0, 70, -278.282 //
};
firstMesh.triVerts = {
{0, 1, 2}, {2, 3, 0}, {4, 5, 6}, {5, 4, 7}, {6, 2, 1}, {6, 5, 2},
{5, 3, 2}, {5, 7, 3}, {7, 0, 3}, {7, 4, 0}, {4, 1, 0}, {4, 6, 1},
0, 1, 2, //
2, 3, 0, //
4, 5, 6, //
5, 4, 7, //
6, 2, 1, //
6, 5, 2, //
5, 3, 2, //
5, 7, 3, //
7, 0, 3, //
7, 4, 0, //
4, 1, 0, //
4, 6, 1, //
};

Mesh secondMesh;
secondMesh.vertPos = {
{2.04636e-12, 70, 50000}, {2.04636e-12, -1.27898e-13, 50000},
{1470, -1.27898e-13, 50000}, {1540, 70, 50000},
{2.04636e-12, 70, -28.2818}, {1470, -1.27898e-13, 0},
{2.04636e-12, -1.27898e-13, 0}, {1540, 70, -28.2818}};
secondMesh.triVerts = {{0, 1, 2}, {2, 3, 0}, {4, 5, 6}, {5, 4, 7},
{6, 2, 1}, {6, 5, 2}, {5, 3, 2}, {5, 7, 3},
{7, 0, 3}, {7, 4, 0}, {4, 1, 0}, {4, 6, 1}};
MeshGL secondMesh;
secondMesh.vertProperties = {
2.04636e-12, 70, 50000, //
2.04636e-12, -1.27898e-13, 50000, //
1470, -1.27898e-13, 50000, //
1540, 70, 50000, //
2.04636e-12, 70, -28.2818, //
1470, -1.27898e-13, 0, //
2.04636e-12, -1.27898e-13, 0, //
1540, 70, -28.2818 //
};
secondMesh.triVerts = {
0, 1, 2, //
2, 3, 0, //
4, 5, 6, //
5, 4, 7, //
6, 2, 1, //
6, 5, 2, //
5, 3, 2, //
5, 7, 3, //
7, 0, 3, //
7, 4, 0, //
4, 1, 0, //
4, 6, 1 //
};

Manifold first(firstMesh);
Manifold second(secondMesh);

first -= second;
first.GetMeshGL();
EXPECT_EQ(first.Status(), Manifold::Error::NoError);
}

TEST(BooleanComplex, Close) {
Expand Down
Loading

0 comments on commit 9f62dbb

Please sign in to comment.