Skip to content

Commit a389a3e

Browse files
committed
Improved debug code and partial solution #993
1 parent 0da40ee commit a389a3e

File tree

8 files changed

+125
-94
lines changed

8 files changed

+125
-94
lines changed

src/cpp/geometry/operations/boolean-utils/clip-mesh.h

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,15 @@
1313

1414
namespace fuzzybools
1515
{
16-
static void clipMesh(Geometry& source, Geometry& target, BVH& targetBVH, Geometry& result, bool invert, bool flip, bool keepBoundary)
17-
{
18-
glm::dvec3 targetCenter;
19-
glm::dvec3 targetExtents;
20-
target.GetCenterExtents(targetCenter, targetExtents);
21-
22-
for (uint32_t i = 0; i < source.numFaces; i++)
23-
{
24-
Face tri = source.GetFace(i);
25-
glm::dvec3 a = source.GetPoint(tri.i0);
26-
glm::dvec3 b = source.GetPoint(tri.i1);
27-
glm::dvec3 c = source.GetPoint(tri.i2);
28-
29-
glm::dvec3 n = computeNormal(a, b, c);
30-
31-
glm::dvec3 triCenter = (a + b + c) * 1.0 / 3.0;
32-
33-
auto isInsideTarget = MeshLocation::INSIDE;
34-
35-
if (IsInsideCenterExtents(triCenter, targetCenter, targetExtents))
36-
{
37-
isInsideTarget = isInsideMesh(triCenter, n, *targetBVH.ptr, targetBVH).loc;
38-
}
39-
else
40-
{
41-
isInsideTarget = MeshLocation::OUTSIDE;
42-
}
43-
44-
if ((isInsideTarget == MeshLocation::INSIDE && !invert) || (isInsideTarget == MeshLocation::OUTSIDE && invert) || (isInsideTarget == MeshLocation::BOUNDARY && keepBoundary))
45-
{
46-
// emit triangle
47-
if (flip)
48-
{
49-
result.AddFace(a, c, b);
50-
}
51-
else
52-
{
53-
result.AddFace(a, b, c);
54-
}
55-
}
56-
}
57-
}
58-
5916
static void doubleClipSingleMesh(Geometry& mesh, BVH& bvh1, BVH& bvh2, Geometry& result)
60-
{
17+
{
18+
#ifdef CSG_DEBUG_OUTPUT
19+
std::vector<std::vector<glm::dvec2>> edgesPrinted;
20+
#endif
21+
6122
for (uint32_t i = 0; i < mesh.numFaces; i++)
6223
{
24+
bool doit = false;
6325
Face tri = mesh.GetFace(i);
6426
glm::dvec3 a = mesh.GetPoint(tri.i0);
6527
glm::dvec3 b = mesh.GetPoint(tri.i1);
@@ -69,8 +31,10 @@ namespace fuzzybools
6931

7032
if (!aabb.intersects(bvh2.box))
7133
{
34+
// Why is this commented?
35+
7236
// when subtracting, if box is outside the second operand, its guaranteed to remain
73-
//result.AddFace(a, b, c);
37+
// result.AddFace(a, b, c);
7438
//continue;
7539
}
7640
else if (!aabb.intersects(bvh1.box))
@@ -111,12 +75,17 @@ namespace fuzzybools
11175
// normals face away from eachother, we can keep this face
11276
// furthermore, since the first operand is the first added, we don't flip
11377
result.AddFace(a, b, c);
78+
doit = true;
11479
}
11580
}
11681
else
11782
{
11883
if (isInside2 == MeshLocation::INSIDE || isInside1 == MeshLocation::OUTSIDE)
11984
{
85+
if(isInside1 == MeshLocation::INSIDE)
86+
{
87+
doit = true;
88+
}
12089
// inside 2, with subtract, means don't include
12190
// outside 1, with subtract, means don't include
12291
}
@@ -129,10 +98,12 @@ namespace fuzzybools
12998
if (glm::dot(n, isInside2Loc.normal) < 0)
13099
{
131100
result.AddFace(a, b, c);
101+
doit = true;
132102
}
133103
else
134104
{
135105
result.AddFace(b, a, c);
106+
doit = true;
136107
}
137108
}
138109
else if (isInside1 == MeshLocation::BOUNDARY)
@@ -141,19 +112,36 @@ namespace fuzzybools
141112
if (glm::dot(n, isInside1Loc.normal) < 0)
142113
{
143114
result.AddFace(b, a, c);
115+
doit = true;
144116
}
145117
else
146118
{
147119
result.AddFace(a, b, c);
120+
doit = true;
148121
}
149122
}
150123
else
151124
{
152125
result.AddFace(a, b, c);
126+
doit = true;
153127
}
154128
}
155129
}
130+
131+
#ifdef CSG_DEBUG_OUTPUT
132+
if (doit || true)
133+
{
134+
edgesPrinted.push_back({ glm::dvec2(a.z + a.x/2, a.y+ a.x/2), glm::dvec2(b.z+ b.x/2, b.y+ b.x/2)});
135+
edgesPrinted.push_back({ glm::dvec2(a.z+ a.x/2, a.y+ a.x/2), glm::dvec2(c.z+ c.x/2, c.y+ c.x/2) });
136+
edgesPrinted.push_back({ glm::dvec2(b.z+ b.x/2, b.y+ b.x/2), glm::dvec2(c.z+ c.x/2, c.y+ c.x/2) });
137+
DumpSVGLines(edgesPrinted, L"final_tri.html");
138+
}
139+
#endif
156140
}
141+
142+
#ifdef CSG_DEBUG_OUTPUT
143+
DumpSVGLines(edgesPrinted, L"final_tri.html");
144+
#endif
157145
}
158146

159147
static void doubleClipSingleMesh2(Geometry& mesh, BVH& bvh1, BVH& bvh2, Geometry& result)

src/cpp/geometry/operations/boolean-utils/fuzzy-bools.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ namespace fuzzybools
1616

1717
auto geom = Normalize(sp);
1818

19-
DumpGeometry(geom, L"Post-normalize.obj");
19+
#ifdef CSG_DEBUG_OUTPUT
20+
DumpGeometry(geom, L"Post-normalize.obj");
21+
#endif
2022

2123
return fuzzybools::clipSubtract(geom, bvh1, bvh2);
2224
}

src/cpp/geometry/operations/boolean-utils/geometry.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ namespace fuzzybools
5656

5757
inline void AddPoint(glm::dvec3& pt, glm::dvec3& n)
5858
{
59-
//vertexData.reserve((numPoints + 1) * VERTEX_FORMAT_SIZE_FLOATS);
60-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 0] = pt.x;
61-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 1] = pt.y;
62-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 2] = pt.z;
6359
vertexData.push_back(pt.x);
6460
vertexData.push_back(pt.y);
6561
vertexData.push_back(pt.z);
@@ -78,10 +74,6 @@ namespace fuzzybools
7874
if (messages) { printf("NaN in geom!\n"); }
7975
}
8076

81-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 3] = n.x;
82-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 4] = n.y;
83-
//vertexData[numPoints * VERTEX_FORMAT_SIZE_FLOATS + 5] = n.z;
84-
8577
numPoints += 1;
8678
}
8779

@@ -90,7 +82,6 @@ namespace fuzzybools
9082
glm::dvec3 normal;
9183

9284
double area = areaOfTriangle(a, b, c);
93-
// if (!computeSafeNormal(a, b, c, normal, EPS_SMALL))
9485
if (!computeSafeNormal(a, b, c, normal, toleranceAddFace))
9586
{
9687
// bail out, zero area triangle

src/cpp/geometry/operations/boolean-utils/intersect-ray-tri.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace fuzzybools
3333
const Vec& v2,
3434
Vec& hitPosition,
3535
double& t,
36+
double& d_plane,
3637
bool infiniteLength = false
3738
)
3839
{
@@ -61,6 +62,11 @@ namespace fuzzybools
6162
of intersection.
6263
*/
6364
double NdotRayDirection = glm::dot(n, dir);
65+
66+
// Vec n_norm = glm::normalize(n);
67+
// Vec dir_norm = glm::normalize(dir);
68+
// double NdotRayDirection = glm::dot(n_norm, dir_norm);
69+
6470
// if (std::fabs(NdotRayDirection) < toleranceParallelTight * nLength * dirLength)
6571
if (std::fabs(NdotRayDirection) < toleranceParallelTight)
6672
return false;
@@ -78,7 +84,9 @@ namespace fuzzybools
7884
(b) Compute t. The distance from the origin to the point of intersection is t |dir|, where
7985
|dir| is the length of dir.
8086
*/
81-
t = -(glm::dot(n, origin) + d) / NdotRayDirection;
87+
double d_origin = glm::dot(n, origin);
88+
89+
t = -(d_origin + d) / NdotRayDirection;
8290
/*
8391
(c) Check if the triangle is behind the ray. If so, return false.
8492
*/
@@ -89,6 +97,7 @@ namespace fuzzybools
8997
*/
9098
Vec p = origin + t * dir;
9199

100+
d_plane = (glm::dot(t * dir, glm::normalize(n)));
92101
/*
93102
Step 2
94103
------
@@ -116,25 +125,28 @@ namespace fuzzybools
116125
Vec edge0 = v1 - v0;
117126
Vec v0p = p - v0;
118127
c = glm::cross(edge0, v0p);
119-
cLength = c.length();
128+
cLength = c.length();
129+
double valdot = glm::dot(n, c);
120130
// if (glm::dot(n, c) < -toleranceInsideOutside * nLength * cLength) return false;
121-
if (glm::dot(n, c) < -toleranceInsideOutside) return false;
131+
if (valdot < -toleranceInsideOutside) return false;
122132

123133
// edge 1
124134
Vec edge1 = v2 - v1;
125135
Vec v1p = p - v1;
126136
c = glm::cross(edge1, v1p);
127-
cLength = c.length();
137+
cLength = c.length();
138+
valdot = glm::dot(n, c);
128139
// if (glm::dot(n, c) < -toleranceInsideOutside * nLength * cLength) return false;
129-
if (glm::dot(n, c) < -toleranceInsideOutside) return false;
140+
if (valdot < -toleranceInsideOutside) return false;
130141

131142
// edge 2
132143
Vec edge2 = v0 - v2;
133144
Vec v2p = p - v2;
134145
c = glm::cross(edge2, v2p);
135-
cLength = c.length();
146+
cLength = c.length();
147+
valdot = glm::dot(n, c);
136148
// if (glm::dot(n, c) < -toleranceInsideOutside * nLength * cLength) return false;
137-
if (glm::dot(n, c) < -toleranceInsideOutside) return false;
149+
if (valdot < -toleranceInsideOutside) return false;
138150

139151
hitPosition = p;
140152

src/cpp/geometry/operations/boolean-utils/is-inside-mesh.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace fuzzybools
4545
BVH& bvh
4646
)
4747
{
48-
Vec dir(1.0, 1.1, 1.4); // From where does this come?
48+
Vec dir(1.0, 1.1, 1.4); // From where does this come? // This is an arbitrary direction to evaluate inside/outside
4949
int winding = 0;
5050

5151
InsideResult result;
@@ -63,16 +63,15 @@ namespace fuzzybools
6363

6464
Vec intersection;
6565
double distance;
66-
bool hasIntersection = intersect_ray_triangle(pt, pt + dir, a, b, c, intersection, distance, true);
66+
double d_plane;
67+
bool hasIntersection = intersect_ray_triangle(pt, pt + dir, a, b, c, intersection, distance, d_plane, true);
6768
if (hasIntersection)
6869
{
6970
Vec otherNormal = computeNormal(a, b, c); // normalised
7071
double d = glm::dot(otherNormal, dir);
7172
double dn = glm::dot(otherNormal, normal);
72-
if (std::fabs(distance) < toleranceBoundaryPoint)
73+
if (std::fabs(d_plane) < toleranceBoundaryPoint)
7374
{
74-
75-
7675
if (dn > 1.0 - toleranceParallel)
7776
{
7877
/*
@@ -101,15 +100,16 @@ namespace fuzzybools
101100
}
102101
}
103102

103+
// It will never reach the --else-- code
104104
// if (true || d >= 0)
105-
if (true || d > toleranceParallelTight)
106-
{
107-
winding++;
108-
}
109-
else
110-
{
111-
winding--;
112-
}
105+
// if (true || d > toleranceParallelTight)
106+
// {
107+
winding++;
108+
// }
109+
// else
110+
// {
111+
// winding--;
112+
// }
113113
}
114114

115115
// Continue to search.

src/cpp/geometry/operations/boolean-utils/obj-exporter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace fuzzybools
3434

3535
static void DumpGeometry(const Geometry& geom, std::wstring filename)
3636
{
37-
return;
3837
size_t offset = 0;
3938
writeFile(filename, ToObj(geom, offset));
4039
}

0 commit comments

Comments
 (0)