2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0 */
|
2020-08-28 10:56:44 -04:00
|
|
|
|
|
|
|
#include "testing/testing.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BLI_array.hh"
|
|
|
|
#include "BLI_map.hh"
|
|
|
|
#include "BLI_math_mpq.hh"
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
#include "BLI_math_vec_mpq_types.hh"
|
2020-08-28 10:56:44 -04:00
|
|
|
#include "BLI_mesh_boolean.hh"
|
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
2020-08-28 11:43:56 -04:00
|
|
|
#ifdef WITH_GMP
|
2020-08-28 10:56:44 -04:00
|
|
|
namespace blender::meshintersect::tests {
|
|
|
|
|
|
|
|
constexpr bool DO_OBJ = false;
|
|
|
|
|
|
|
|
/* Build and hold an IMesh from a string spec. Also hold and own resources used by IMesh. */
|
|
|
|
class IMeshBuilder {
|
|
|
|
public:
|
|
|
|
IMesh imesh;
|
|
|
|
IMeshArena arena;
|
|
|
|
|
|
|
|
/* "Edge orig" indices are an encoding of <input face#, position in face of start of edge>. */
|
|
|
|
static constexpr int MAX_FACE_LEN = 1000; /* Used for forming "orig edge" indices only. */
|
|
|
|
|
|
|
|
static int edge_index(int face_index, int facepos)
|
|
|
|
{
|
|
|
|
return face_index * MAX_FACE_LEN + facepos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<int, int> face_and_pos_for_edge_index(int e_index)
|
|
|
|
{
|
|
|
|
return std::pair<int, int>(e_index / MAX_FACE_LEN, e_index % MAX_FACE_LEN);
|
|
|
|
}
|
|
|
|
|
2022-03-17 10:04:45 +11:00
|
|
|
/**
|
2020-08-28 10:56:44 -04:00
|
|
|
* Spec should have form:
|
2022-03-17 10:04:45 +11:00
|
|
|
* <pre>
|
|
|
|
* #verts #faces
|
|
|
|
* mpq_class mpq_class mpq_clas [#verts lines]
|
|
|
|
* int int int ... [#faces lines; indices into verts for given face]
|
|
|
|
* </pre>
|
2020-08-28 10:56:44 -04:00
|
|
|
*/
|
|
|
|
IMeshBuilder(const char *spec)
|
|
|
|
{
|
|
|
|
std::istringstream ss(spec);
|
|
|
|
std::string line;
|
|
|
|
getline(ss, line);
|
|
|
|
std::istringstream hdrss(line);
|
|
|
|
int nv, nf;
|
|
|
|
hdrss >> nv >> nf;
|
|
|
|
if (nv == 0 || nf == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
arena.reserve(nv, nf);
|
|
|
|
Vector<const Vert *> verts;
|
|
|
|
Vector<Face *> faces;
|
|
|
|
bool spec_ok = true;
|
|
|
|
int v_index = 0;
|
|
|
|
while (v_index < nv && spec_ok && getline(ss, line)) {
|
|
|
|
std::istringstream iss(line);
|
|
|
|
mpq_class p0;
|
|
|
|
mpq_class p1;
|
|
|
|
mpq_class p2;
|
|
|
|
iss >> p0 >> p1 >> p2;
|
|
|
|
spec_ok = !iss.fail();
|
|
|
|
verts.append(arena.add_or_find_vert(mpq3(p0, p1, p2), v_index));
|
|
|
|
++v_index;
|
|
|
|
}
|
|
|
|
if (v_index != nv) {
|
|
|
|
spec_ok = false;
|
|
|
|
}
|
|
|
|
int f_index = 0;
|
|
|
|
while (f_index < nf && spec_ok && getline(ss, line)) {
|
|
|
|
std::istringstream fss(line);
|
|
|
|
Vector<const Vert *> face_verts;
|
|
|
|
Vector<int> edge_orig;
|
|
|
|
int fpos = 0;
|
|
|
|
while (spec_ok && fss >> v_index) {
|
|
|
|
if (v_index < 0 || v_index >= nv) {
|
|
|
|
spec_ok = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
face_verts.append(verts[v_index]);
|
|
|
|
edge_orig.append(edge_index(f_index, fpos));
|
|
|
|
++fpos;
|
|
|
|
}
|
|
|
|
Face *facep = arena.add_face(face_verts, f_index, edge_orig);
|
|
|
|
faces.append(facep);
|
|
|
|
++f_index;
|
|
|
|
}
|
|
|
|
if (f_index != nf) {
|
|
|
|
spec_ok = false;
|
|
|
|
}
|
|
|
|
if (!spec_ok) {
|
|
|
|
std::cout << "Bad spec: " << spec;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
imesh = IMesh(faces);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static int all_shape_zero(int /*t*/)
|
2020-08-28 10:56:44 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, Empty)
|
|
|
|
{
|
|
|
|
IMeshArena arena;
|
|
|
|
IMesh in;
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(in, BoolOpType::None, 1, all_shape_zero, true, false, &arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 0);
|
|
|
|
EXPECT_EQ(out.face_size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, TetTetTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
0 0 1
|
|
|
|
2 0 1
|
|
|
|
1 2 1
|
|
|
|
1 1 3
|
|
|
|
0 2 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::None, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 11);
|
|
|
|
EXPECT_EQ(out.face_size(), 20);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb2(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out2 = boolean_trimesh(
|
|
|
|
mb2.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb2.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out2.populate_vert();
|
|
|
|
EXPECT_EQ(out2.vert_size(), 10);
|
|
|
|
EXPECT_EQ(out2.face_size(), 16);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out2, "tettet_union_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb3(spec);
|
|
|
|
IMesh out3 = boolean_trimesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb3.imesh,
|
|
|
|
BoolOpType::Union,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&mb3.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out3.populate_vert();
|
|
|
|
EXPECT_EQ(out3.vert_size(), 10);
|
|
|
|
EXPECT_EQ(out3.face_size(), 16);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out3, "tettet_union_binary_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb4(spec);
|
|
|
|
IMesh out4 = boolean_trimesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb4.imesh,
|
|
|
|
BoolOpType::Union,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
&mb4.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out4.populate_vert();
|
|
|
|
EXPECT_EQ(out4.vert_size(), 10);
|
|
|
|
EXPECT_EQ(out4.face_size(), 16);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out4, "tettet_union_binary_self_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb5(spec);
|
|
|
|
IMesh out5 = boolean_trimesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb5.imesh,
|
|
|
|
BoolOpType::Intersect,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&mb5.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out5.populate_vert();
|
|
|
|
EXPECT_EQ(out5.vert_size(), 4);
|
|
|
|
EXPECT_EQ(out5.face_size(), 4);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out5, "tettet_intersect_binary_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb6(spec);
|
|
|
|
IMesh out6 = boolean_trimesh(
|
|
|
|
mb6.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
&mb6.arena);
|
|
|
|
out6.populate_vert();
|
|
|
|
EXPECT_EQ(out6.vert_size(), 6);
|
|
|
|
EXPECT_EQ(out6.face_size(), 8);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out6, "tettet_difference_binary_tm");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb7(spec);
|
|
|
|
IMesh out7 = boolean_trimesh(
|
|
|
|
mb7.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 1 : 0; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
&mb7.arena);
|
|
|
|
out7.populate_vert();
|
|
|
|
EXPECT_EQ(out7.vert_size(), 8);
|
|
|
|
EXPECT_EQ(out7.face_size(), 12);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out7, "tettet_difference_rev_binary_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, TetTet2Trimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 1 -1
|
|
|
|
7/8 -1/2 -1
|
|
|
|
-7/8 -1/2 -1
|
|
|
|
0 0 1
|
|
|
|
0 1 0
|
|
|
|
7/8 -1/2 0
|
|
|
|
-7/8 -1/2 0
|
|
|
|
0 0 2
|
|
|
|
0 3 1
|
|
|
|
0 1 2
|
|
|
|
1 3 2
|
|
|
|
2 3 0
|
|
|
|
4 7 5
|
|
|
|
4 5 6
|
|
|
|
5 7 6
|
|
|
|
6 7 4
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 10);
|
|
|
|
EXPECT_EQ(out.face_size(), 16);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet2_union_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, CubeTetTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(12 16
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
0 1/2 1/2
|
|
|
|
1/2 -1/4 1/2
|
|
|
|
-1/2 -1/4 1/2
|
|
|
|
0 0 3/2
|
|
|
|
0 1 3
|
|
|
|
0 3 2
|
|
|
|
2 3 7
|
|
|
|
2 7 6
|
|
|
|
6 7 5
|
|
|
|
6 5 4
|
|
|
|
4 5 1
|
|
|
|
4 1 0
|
|
|
|
2 6 4
|
|
|
|
2 4 0
|
|
|
|
7 3 1
|
|
|
|
7 1 5
|
|
|
|
8 11 9
|
|
|
|
8 9 10
|
|
|
|
9 11 10
|
|
|
|
10 11 8
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 14);
|
|
|
|
EXPECT_EQ(out.face_size(), 24);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubetet_union_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, BinaryTetTetTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
0 0 1
|
|
|
|
2 0 1
|
|
|
|
1 2 1
|
|
|
|
1 1 3
|
|
|
|
0 2 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_trimesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Intersect,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 4);
|
|
|
|
EXPECT_EQ(out.face_size(), 4);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "binary_tettet_isect_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, TetTetCoplanarTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 1 0
|
|
|
|
7/8 -1/2 0
|
|
|
|
-7/8 -1/2 0
|
|
|
|
0 0 1
|
|
|
|
0 1 0
|
|
|
|
7/8 -1/2 0
|
|
|
|
-7/8 -1/2 0
|
|
|
|
0 0 -1
|
|
|
|
0 3 1
|
|
|
|
0 1 2
|
|
|
|
1 3 2
|
|
|
|
2 3 0
|
|
|
|
4 5 7
|
|
|
|
4 6 5
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 5);
|
|
|
|
EXPECT_EQ(out.face_size(), 6);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet_coplanar_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, TetInsideTetTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
-1 -3/4 -1/2
|
|
|
|
3 -3/4 -1/2
|
|
|
|
1 13/4 -1/2
|
|
|
|
1 5/4 7/2
|
|
|
|
0 2 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 4);
|
|
|
|
EXPECT_EQ(out.face_size(), 4);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tetinsidetet_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, TetBesideTetTrimesh)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
3 0 0
|
|
|
|
5 0 0
|
|
|
|
4 2 0
|
|
|
|
4 1 2
|
|
|
|
0 2 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
2021-03-07 18:13:19 -05:00
|
|
|
IMesh out = boolean_trimesh(
|
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 8);
|
|
|
|
EXPECT_EQ(out.face_size(), 8);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tetbesidetet_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_trimesh, DegenerateTris)
|
|
|
|
{
|
|
|
|
const char *spec = R"(10 10
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
0 0 1
|
|
|
|
2 0 1
|
|
|
|
1 2 1
|
|
|
|
1 1 3
|
|
|
|
0 0 0
|
|
|
|
1 0 0
|
|
|
|
0 2 1
|
|
|
|
0 8 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
0 1 9
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_trimesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Intersect,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 5 ? 0 : 1; },
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
&mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 4);
|
|
|
|
EXPECT_EQ(out.face_size(), 4);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "degenerate_tris_tm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, TetTet)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 0 0
|
|
|
|
2 0 0
|
|
|
|
1 2 0
|
|
|
|
1 1 2
|
|
|
|
0 0 1
|
|
|
|
2 0 1
|
|
|
|
1 2 1
|
|
|
|
1 1 3
|
|
|
|
0 2 1
|
|
|
|
0 1 3
|
|
|
|
1 2 3
|
|
|
|
2 0 3
|
|
|
|
4 6 5
|
|
|
|
4 5 7
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb.imesh, BoolOpType::None, 1, all_shape_zero, true, false, nullptr, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 11);
|
|
|
|
EXPECT_EQ(out.face_size(), 13);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb2(spec);
|
|
|
|
IMesh out2 = boolean_mesh(
|
|
|
|
mb2.imesh,
|
|
|
|
BoolOpType::None,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb2.arena);
|
|
|
|
out2.populate_vert();
|
|
|
|
EXPECT_EQ(out2.vert_size(), 11);
|
|
|
|
EXPECT_EQ(out2.face_size(), 13);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCube)
|
|
|
|
{
|
|
|
|
const char *spec = R"(16 12
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
1/2 1/2 1/2
|
|
|
|
1/2 1/2 5/2
|
|
|
|
1/2 5/2 1/2
|
|
|
|
1/2 5/2 5/2
|
|
|
|
5/2 1/2 1/2
|
|
|
|
5/2 1/2 5/2
|
|
|
|
5/2 5/2 1/2
|
|
|
|
5/2 5/2 5/2
|
|
|
|
0 1 3 2
|
|
|
|
6 2 3 7
|
|
|
|
4 6 7 5
|
|
|
|
0 4 5 1
|
|
|
|
0 2 6 4
|
|
|
|
3 1 5 7
|
|
|
|
8 9 11 10
|
|
|
|
14 10 11 15
|
|
|
|
12 14 15 13
|
|
|
|
8 12 13 9
|
|
|
|
8 10 14 12
|
|
|
|
11 9 13 15
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(mb.imesh, "cube_cube_in");
|
|
|
|
}
|
|
|
|
IMesh out = boolean_mesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, nullptr, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 20);
|
|
|
|
EXPECT_EQ(out.face_size(), 12);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubecube_union");
|
|
|
|
}
|
|
|
|
|
|
|
|
IMeshBuilder mb2(spec);
|
|
|
|
IMesh out2 = boolean_mesh(
|
|
|
|
mb2.imesh,
|
|
|
|
BoolOpType::None,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 6 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb2.arena);
|
|
|
|
out2.populate_vert();
|
|
|
|
EXPECT_EQ(out2.vert_size(), 22);
|
|
|
|
EXPECT_EQ(out2.face_size(), 18);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out2, "cubecube_none");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCone)
|
|
|
|
{
|
|
|
|
const char *spec = R"(14 12
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
0 1/2 3/4
|
|
|
|
119/250 31/200 3/4
|
|
|
|
147/500 -81/200 3/4
|
|
|
|
0 0 7/4
|
|
|
|
-147/500 -81/200 3/4
|
|
|
|
-119/250 31/200 3/4
|
|
|
|
0 1 3 2
|
|
|
|
2 3 7 6
|
|
|
|
6 7 5 4
|
|
|
|
4 5 1 0
|
|
|
|
2 6 4 0
|
|
|
|
7 3 1 5
|
|
|
|
8 11 9
|
|
|
|
9 11 10
|
|
|
|
10 11 12
|
|
|
|
12 11 13
|
|
|
|
13 11 8
|
|
|
|
8 9 10 12 13)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
2021-03-07 18:13:19 -05:00
|
|
|
mb.imesh, BoolOpType::Union, 1, all_shape_zero, true, false, nullptr, &mb.arena);
|
2020-08-28 10:56:44 -04:00
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 14);
|
|
|
|
EXPECT_EQ(out.face_size(), 12);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubeccone");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCubeCoplanar)
|
|
|
|
{
|
|
|
|
const char *spec = R"(16 12
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
-1/2 -1/2 1
|
|
|
|
-1/2 -1/2 2
|
|
|
|
-1/2 1/2 1
|
|
|
|
-1/2 1/2 2
|
|
|
|
1/2 -1/2 1
|
|
|
|
1/2 -1/2 2
|
|
|
|
1/2 1/2 1
|
|
|
|
1/2 1/2 2
|
|
|
|
0 1 3 2
|
|
|
|
2 3 7 6
|
|
|
|
6 7 5 4
|
|
|
|
4 5 1 0
|
|
|
|
2 6 4 0
|
|
|
|
7 3 1 5
|
|
|
|
8 9 11 10
|
|
|
|
10 11 15 14
|
|
|
|
14 15 13 12
|
|
|
|
12 13 9 8
|
|
|
|
10 14 12 8
|
|
|
|
15 11 9 13
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Union,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 6 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 16);
|
|
|
|
EXPECT_EQ(out.face_size(), 12);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubecube_coplanar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, TetTeTCoplanarDiff)
|
|
|
|
{
|
|
|
|
const char *spec = R"(8 8
|
|
|
|
0 1 0
|
|
|
|
7/8 -1/2 0
|
|
|
|
-7/8 -1/2 0
|
|
|
|
0 0 1
|
|
|
|
0 1 0
|
|
|
|
7/8 -1/2 0
|
|
|
|
-7/8 -1/2 0
|
|
|
|
0 0 -1
|
|
|
|
0 3 1
|
|
|
|
0 1 2
|
|
|
|
1 3 2
|
|
|
|
2 3 0
|
|
|
|
4 5 7
|
|
|
|
4 6 5
|
|
|
|
5 6 7
|
|
|
|
6 4 7
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 4 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 4);
|
|
|
|
EXPECT_EQ(out.face_size(), 4);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "tettet_coplanar_diff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCubeStep)
|
|
|
|
{
|
|
|
|
const char *spec = R"(16 12
|
|
|
|
0 -1 0
|
|
|
|
0 -1 2
|
|
|
|
0 1 0
|
|
|
|
0 1 2
|
|
|
|
2 -1 0
|
|
|
|
2 -1 2
|
|
|
|
2 1 0
|
|
|
|
2 1 2
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
0 1 3 2
|
|
|
|
2 3 7 6
|
|
|
|
6 7 5 4
|
|
|
|
4 5 1 0
|
|
|
|
2 6 4 0
|
|
|
|
7 3 1 5
|
|
|
|
8 9 11 10
|
|
|
|
10 11 15 14
|
|
|
|
14 15 13 12
|
|
|
|
12 13 9 8
|
|
|
|
10 14 12 8
|
|
|
|
15 11 9 13
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 6 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 12);
|
|
|
|
EXPECT_EQ(out.face_size(), 8);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubecubestep");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCyl4)
|
|
|
|
{
|
|
|
|
const char *spec = R"(16 12
|
|
|
|
0 1 -1
|
|
|
|
0 1 1
|
|
|
|
1 0 -1
|
|
|
|
1 0 1
|
|
|
|
0 -1 -1
|
|
|
|
0 -1 1
|
|
|
|
-1 0 -1
|
|
|
|
-1 0 1
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
0 1 3 2
|
|
|
|
2 3 5 4
|
|
|
|
3 1 7 5
|
|
|
|
4 5 7 6
|
|
|
|
6 7 1 0
|
|
|
|
0 2 4 6
|
|
|
|
8 9 11 10
|
|
|
|
10 11 15 14
|
|
|
|
14 15 13 12
|
|
|
|
12 13 9 8
|
|
|
|
10 14 12 8
|
|
|
|
15 11 9 13
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 6 ? 1 : 0; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 16);
|
|
|
|
EXPECT_EQ(out.face_size(), 20);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubecyl4");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubeCubesubdivDiff)
|
|
|
|
{
|
|
|
|
/* A cube intersected by a subdivided cube that intersects first cubes edges exactly. */
|
|
|
|
const char *spec = R"(26 22
|
|
|
|
2 1/3 2
|
|
|
|
2 -1/3 2
|
|
|
|
2 -1/3 0
|
|
|
|
2 1/3 0
|
|
|
|
0 -1/3 2
|
|
|
|
0 1/3 2
|
|
|
|
0 1/3 0
|
|
|
|
0 -1/3 0
|
|
|
|
1 1/3 2
|
|
|
|
1 -1/3 2
|
|
|
|
1 1/3 0
|
|
|
|
1 -1/3 0
|
|
|
|
0 -1/3 1
|
|
|
|
0 1/3 1
|
|
|
|
2 1/3 1
|
|
|
|
2 -1/3 1
|
|
|
|
1 1/3 1
|
|
|
|
1 -1/3 1
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
|
|
|
17 9 4 12
|
|
|
|
13 6 7 12
|
|
|
|
15 2 3 14
|
|
|
|
11 7 6 10
|
|
|
|
16 13 5 8
|
|
|
|
9 1 0 8
|
|
|
|
4 9 8 5
|
|
|
|
14 16 8 0
|
|
|
|
2 11 10 3
|
|
|
|
15 1 9 17
|
|
|
|
2 15 17 11
|
|
|
|
3 10 16 14
|
|
|
|
10 6 13 16
|
|
|
|
1 15 14 0
|
|
|
|
5 13 12 4
|
|
|
|
11 17 12 7
|
|
|
|
19 21 20 18
|
|
|
|
21 25 24 20
|
|
|
|
25 23 22 24
|
|
|
|
23 19 18 22
|
|
|
|
18 20 24 22
|
|
|
|
23 25 21 19
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t < 16 ? 1 : 0; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 16);
|
|
|
|
EXPECT_EQ(out.face_size(), 10);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubecubesubdivdiff");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(boolean_polymesh, CubePlane)
|
|
|
|
{
|
|
|
|
const char *spec = R"(12 7
|
|
|
|
-2 -2 0
|
|
|
|
2 -2 0
|
|
|
|
-2 2 0
|
|
|
|
2 2 0
|
|
|
|
-1 -1 -1
|
|
|
|
-1 -1 1
|
|
|
|
-1 1 -1
|
|
|
|
-1 1 1
|
|
|
|
1 -1 -1
|
|
|
|
1 -1 1
|
|
|
|
1 1 -1
|
|
|
|
1 1 1
|
2020-10-02 09:48:41 +10:00
|
|
|
0 1 3 2
|
|
|
|
4 5 7 6
|
|
|
|
6 7 11 10
|
|
|
|
10 11 9 8
|
|
|
|
8 9 5 4
|
|
|
|
6 10 8 4
|
2020-08-28 10:56:44 -04:00
|
|
|
11 7 5 9
|
|
|
|
)";
|
|
|
|
|
|
|
|
IMeshBuilder mb(spec);
|
|
|
|
IMesh out = boolean_mesh(
|
|
|
|
mb.imesh,
|
|
|
|
BoolOpType::Difference,
|
|
|
|
2,
|
|
|
|
[](int t) { return t >= 1 ? 0 : 1; },
|
|
|
|
false,
|
2021-03-07 18:13:19 -05:00
|
|
|
false,
|
2020-08-28 10:56:44 -04:00
|
|
|
nullptr,
|
|
|
|
&mb.arena);
|
|
|
|
out.populate_vert();
|
|
|
|
EXPECT_EQ(out.vert_size(), 8);
|
|
|
|
EXPECT_EQ(out.face_size(), 6);
|
|
|
|
if (DO_OBJ) {
|
|
|
|
write_obj_mesh(out, "cubeplane");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace blender::meshintersect::tests
|
2020-08-28 11:43:56 -04:00
|
|
|
#endif
|