Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define a test for simple offset booleans #998

Merged
merged 20 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions test/boolean_complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifdef MANIFOLD_CROSS_SECTION
#include "manifold/cross_section.h"
#endif
#include "../src/utils.h" // For RotateUp
#include "manifold/manifold.h"
#include "manifold/polygon.h"
#include "test.h"
Expand Down Expand Up @@ -1030,4 +1031,160 @@ TEST(BooleanComplex, HullMask) {
MeshGL mesh = ret.GetMeshGL();
}

TEST(BooleanComplex, SimpleOffset) {
std::string file = __FILE__;
std::string dir = file.substr(0, file.rfind('/'));
MeshGL seeds = ImportMesh(dir + "/models/" + "Generic_Twin_91.1.t0.glb");
EXPECT_TRUE(seeds.NumTri() > 10);
EXPECT_TRUE(seeds.NumVert() > 10);
// Find unique edges
std::set<std::pair<int, int>> edges;
for (size_t i = 0; i < seeds.NumTri(); i++) {
int eind[3];
for (int j = 0; j < 3; j++) eind[j] = seeds.triVerts[i*3+j];
int e11, e12, e21, e22, e31, e32;
e11 = (eind[0] < eind[1]) ? eind[0] : eind[1];
e12 = (eind[1] < eind[0]) ? eind[0] : eind[1];
e21 = (eind[1] < eind[2]) ? eind[1] : eind[2];
e22 = (eind[2] < eind[1]) ? eind[1] : eind[2];
e31 = (eind[2] < eind[0]) ? eind[2] : eind[0];
e32 = (eind[0] < eind[2]) ? eind[2] : eind[0];
edges.insert(std::make_pair(e11, e12));
edges.insert(std::make_pair(e21, e22));
edges.insert(std::make_pair(e31, e32));
starseeker marked this conversation as resolved.
Show resolved Hide resolved
}
manifold::Manifold c;
// Vertex Spheres
Manifold sph = Manifold::Sphere(1, 8);
for (size_t i = 0; i < seeds.NumVert(); i++) {
vec3 vpos(seeds.vertProperties[3*i+0], seeds.vertProperties[3*i+1],
seeds.vertProperties[3 * i + 2]);
Manifold vsph = sph.Translate(vpos);
manifold::Manifold left = c;
manifold::Manifold right(vsph);
if (!right.NumTri()) continue;
c = left.Boolean(right, manifold::OpType::Add);
starseeker marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TRUE(c.NumTri() > 0);
}
// Edge Cylinders
std::set<std::pair<int, int>>::iterator e_it;
for (e_it = edges.begin(); e_it != edges.end(); ++e_it) {
vec3 ev1 = vec3(seeds.vertProperties[3*e_it->first+0],
seeds.vertProperties[3*e_it->first+1],
seeds.vertProperties[3*e_it->first+2]);
vec3 ev2 = vec3(seeds.vertProperties[3*e_it->second+0],
seeds.vertProperties[3*e_it->second+1],
seeds.vertProperties[3*e_it->second+2]);
vec3 edge = ev2 - ev1;
double len = la::length(edge);
if (len < std::numeric_limits<float>::min()) continue;
// TODO - workaround, shouldn't be necessary
if (len < 0.03) continue;
manifold::Manifold origin_cyl = manifold::Manifold::Cylinder(len, 1, 1, 8);
vec3 evec(-1*edge.x, -1*edge.y, edge.z);
manifold::Manifold rotated_cyl = origin_cyl.Transform(manifold::RotateUp(evec));
manifold::Manifold right = rotated_cyl.Translate(ev1);
if (!right.NumTri())
continue;
manifold::Manifold left = c;
c = left.Boolean(right, manifold::OpType::Add);
EXPECT_TRUE(c.NumTri() > 0);
}
// Triangle Volumes
starseeker marked this conversation as resolved.
Show resolved Hide resolved
for (size_t i = 0; i < seeds.NumTri(); i++) {
int eind[3];
for (int j = 0; j < 3; j++)
eind[j] = seeds.triVerts[i*3+j];
vec3 ev1 = vec3(seeds.vertProperties[3*eind[0]+0],
seeds.vertProperties[3*eind[0]+1],
seeds.vertProperties[3*eind[0]+2]);
vec3 ev2 = vec3(seeds.vertProperties[3*eind[1]+0],
seeds.vertProperties[3*eind[1]+1],
seeds.vertProperties[3*eind[1]+2]);
vec3 ev3 = vec3(seeds.vertProperties[3*eind[2]+0],
seeds.vertProperties[3*eind[2]+1],
seeds.vertProperties[3*eind[2]+2]);
vec3 a = ev1 - ev3;
vec3 b = ev2 - ev3;
vec3 n = la::normalize(la::cross(a, b));
// Extrude the points above and below the plane of the triangle
vec3 pnts[6];
pnts[0] = ev1 + n;
pnts[1] = ev2 + n;
pnts[2] = ev3 + n;
pnts[3] = ev1 - n;
pnts[4] = ev2 - n;
pnts[5] = ev3 - n;
// Construct the points and faces of the new manifold
double pts[3*6];
/* 1 */
pts[0] = pnts[4].x;
pts[1] = pnts[4].y;
pts[2] = pnts[4].z;
/* 2 */
pts[3] = pnts[3].x;
pts[4] = pnts[3].y;
pts[5] = pnts[3].z;
/* 3 */
pts[6] = pnts[0].x;
pts[7] = pnts[0].y;
pts[8] = pnts[0].z;
/* 4 */
pts[9] = pnts[1].x;
pts[10] = pnts[1].y;
pts[11] = pnts[1].z;
/* 5 */
pts[12] = pnts[5].x;
pts[13] = pnts[5].y;
pts[14] = pnts[5].z;
/* 6 */
pts[15] = pnts[2].x;
pts[16] = pnts[2].y;
pts[17] = pnts[2].z;
int faces[24];
// 1 2 5
faces[ 0] = 0;
faces[ 1] = 1;
faces[ 2] = 4;
// 3 4 6
faces[ 3] = 2;
faces[ 4] = 3;
faces[ 5] = 5;
// 2 1 4
faces[ 6] = 1;
faces[ 7] = 0;
faces[ 8] = 3;
// 4 3 2
faces[ 9] = 3;
faces[10] = 2;
faces[11] = 1;
// 4 1 5
faces[12] = 3;
faces[13] = 0;
faces[14] = 4;
// 5 6 4
faces[15] = 4;
faces[16] = 5;
faces[17] = 3;
// 6 5 2
faces[18] = 5;
faces[19] = 4;
faces[20] = 1;
// 2 3 6
faces[21] = 1;
faces[22] = 2;
faces[23] = 5;
manifold::MeshGL64 tri_m;
for (int j = 0; j < 18; j++)
tri_m.vertProperties.insert(tri_m.vertProperties.end(), pts[j]);
for (int j = 0; j < 24; j++)
tri_m.triVerts.insert(tri_m.triVerts.end(), faces[j]);
manifold::Manifold left = c;
manifold::Manifold right(tri_m);
if (!right.NumTri()) continue;
c = left.Boolean(right, manifold::OpType::Add);
EXPECT_TRUE(c.NumTri() > 0);
starseeker marked this conversation as resolved.
Show resolved Hide resolved
}
}

#endif
Binary file added test/models/Generic_Twin_91.1.t0.glb
Binary file not shown.
Loading