Skip to content

Commit

Permalink
Merge pull request #124 from elalish/overflow
Browse files Browse the repository at this point in the history
Fix test overflow
  • Loading branch information
elalish authored May 17, 2022
2 parents bfc8e5c + 3545a49 commit a7791d0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 61 deletions.
115 changes: 74 additions & 41 deletions test/mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ namespace {

using namespace manifold;

Mesh Csaszar() {
Mesh csaszar;
csaszar.vertPos = {{-20, -20, -10}, //
{-20, 20, -15}, //
{-5, -8, 8}, //
{0, 0, 30}, //
{5, 8, 8}, //
{20, -20, -15}, //
{20, 20, -10}};
csaszar.triVerts = {{1, 3, 6}, //
{1, 6, 5}, //
{2, 5, 6}, //
{0, 2, 6}, //
{0, 6, 4}, //
{3, 4, 6}, //
{1, 2, 3}, //
{1, 4, 2}, //
{1, 0, 4}, //
{1, 5, 0}, //
{3, 5, 4}, //
{0, 5, 3}, //
{0, 3, 2}, //
{2, 4, 5}};
return csaszar;
}

void Identical(const Mesh& mesh1, const Mesh& mesh2) {
ASSERT_EQ(mesh1.vertPos.size(), mesh2.vertPos.size());
for (int i = 0; i < mesh1.vertPos.size(); ++i)
Expand Down Expand Up @@ -255,7 +281,8 @@ TEST(Manifold, Smooth) {
auto prop = smooth.GetProperties();
EXPECT_NEAR(prop.volume, 17.38, 0.1);
EXPECT_NEAR(prop.surfaceArea, 33.38, 0.1);
// ExportMesh("smoothTet.gltf", smooth.GetMesh());

if (options.exportModels) ExportMesh("smoothTet.glb", smooth.GetMesh(), {});
}

TEST(Manifold, SmoothSphere) {
Expand Down Expand Up @@ -295,52 +322,56 @@ TEST(Manifold, ManualSmooth) {
EXPECT_NEAR(prop.volume, 3.53, 0.01);
EXPECT_NEAR(prop.surfaceArea, 11.39, 0.01);

const Mesh out = interp.GetMesh();
ExportOptions options;
options.faceted = false;
options.mat.roughness = 0.1;

options.mat.vertColor.resize(interp.NumVert());
MeshRelation rel = interp.GetMeshRelation();
const glm::vec4 red(1, 0, 0, 1);
const glm::vec4 purple(1, 0, 1, 1);
for (int tri = 0; tri < interp.NumTri(); ++tri) {
for (int i : {0, 1, 2}) {
const glm::vec3& uvw = rel.barycentric[rel.triBary[tri].vertBary[i]];
const float alpha = glm::min(uvw[0], glm::min(uvw[1], uvw[2]));
options.mat.vertColor[out.triVerts[tri][i]] =
glm::mix(purple, red, glm::smoothstep(0.0f, 0.2f, alpha));
if (options.exportModels) {
const Mesh out = interp.GetMesh();
ExportOptions options;
options.faceted = false;
options.mat.roughness = 0.1;

options.mat.vertColor.resize(interp.NumVert());
MeshRelation rel = interp.GetMeshRelation();
const glm::vec4 red(1, 0, 0, 1);
const glm::vec4 purple(1, 0, 1, 1);
for (int tri = 0; tri < interp.NumTri(); ++tri) {
for (int i : {0, 1, 2}) {
const glm::vec3& uvw = rel.UVW(tri, i);
const float alpha = glm::min(uvw[0], glm::min(uvw[1], uvw[2]));
options.mat.vertColor[out.triVerts[tri][i]] =
glm::mix(purple, red, glm::smoothstep(0.0f, 0.2f, alpha));
}
}
ExportMesh("sharpenedSphere.glb", out, options);
}
// ExportMesh("sharpenedSphere.gltf", out, options);
}

TEST(Manifold, Csaszar) {
Manifold csaszar = Manifold::Smooth(ImportMesh("data/Csaszar.ply"));
Manifold csaszar = Manifold::Smooth(Csaszar());
csaszar.Refine(100);
ExpectMeshes(csaszar, {{70000, 140000}});
auto prop = csaszar.GetProperties();
EXPECT_NEAR(prop.volume, 84699, 10);
EXPECT_NEAR(prop.surfaceArea, 14796, 10);

// const Mesh out = csaszar.GetMesh();
// ExportOptions options;
// options.faceted = false;
// options.mat.roughness = 0.1;

// options.mat.vertColor.resize(csaszar.NumVert());
// MeshRelation rel = csaszar.GetMeshRelation();
// const glm::vec4 blue(0, 0, 1, 1);
// const glm::vec4 yellow(1, 1, 0, 1);
// for (int tri = 0; tri < csaszar.NumTri(); ++tri) {
// for (int i : {0, 1, 2}) {
// const glm::vec3& uvw = rel.barycentric[rel.triBary[tri].vertBary[i]];
// const float alpha = glm::min(uvw[0], glm::min(uvw[1], uvw[2]));
// options.mat.vertColor[out.triVerts[tri][i]] =
// glm::mix(yellow, blue, glm::smoothstep(0.0f, 0.2f, alpha));
// }
// }
// ExportMesh("smoothCsaszar.gltf", out, options);
if (options.exportModels) {
const Mesh out = csaszar.GetMesh();
ExportOptions options;
options.faceted = false;
options.mat.roughness = 0.1;

options.mat.vertColor.resize(csaszar.NumVert());
MeshRelation rel = csaszar.GetMeshRelation();
const glm::vec4 blue(0, 0, 1, 1);
const glm::vec4 yellow(1, 1, 0, 1);
for (int tri = 0; tri < csaszar.NumTri(); ++tri) {
for (int i : {0, 1, 2}) {
const glm::vec3& uvw = rel.barycentric[rel.triBary[tri].vertBary[i]];
const float alpha = glm::min(uvw[0], glm::min(uvw[1], uvw[2]));
options.mat.vertColor[out.triVerts[tri][i]] =
glm::mix(yellow, blue, glm::smoothstep(0.0f, 0.2f, alpha));
}
}
ExportMesh("smoothCsaszar.glb", out, options);
}
}

/**
Expand Down Expand Up @@ -443,7 +474,7 @@ TEST(Manifold, MeshRelationRefine) {
std::vector<Mesh> input;
std::map<int, int> meshID2idx;

input.push_back(ImportMesh("data/Csaszar.ply"));
input.push_back(Csaszar());
Manifold csaszar(input[0]);

std::vector<int> meshIDs = csaszar.GetMeshIDs();
Expand Down Expand Up @@ -513,7 +544,8 @@ TEST(Boolean, Coplanar) {
ExpectMeshes(out, {{32, 64}});
EXPECT_EQ(out.NumDegenerateTris(), 0);
EXPECT_EQ(out.Genus(), 1);
// ExportMesh("coplanar.gltf", out.GetMesh(), {});

if (options.exportModels) ExportMesh("coplanar.glb", out.GetMesh(), {});

RelatedOp(cylinder, cylinder2, out);
}
Expand All @@ -540,7 +572,8 @@ TEST(Boolean, FaceUnion) {
auto prop = cubes.GetProperties();
EXPECT_NEAR(prop.volume, 2, 1e-5);
EXPECT_NEAR(prop.surfaceArea, 10, 1e-5);
// ExportMesh("faceUnion.gltf", cubes.GetMesh(), {});

if (options.exportModels) ExportMesh("faceUnion.glb", cubes.GetMesh(), {});
}

TEST(Boolean, EdgeUnion) {
Expand Down Expand Up @@ -723,9 +756,9 @@ TEST(Boolean, Gyroid) {
EXPECT_TRUE(gyroid.IsManifold());
EXPECT_TRUE(gyroid.MatchesTriNormals());
EXPECT_LE(gyroid.NumDegenerateTris(), 12);
// ExportMesh("gyroidpuzzle1.gltf", gyroid.Extract(), {});
Manifold result = gyroid + gyroid2;
ExportMesh("gyroidUnion.gltf", result.GetMesh(), {});

if (options.exportModels) ExportMesh("gyroidUnion.glb", result.GetMesh(), {});

EXPECT_TRUE(result.IsManifold());
EXPECT_TRUE(result.MatchesTriNormals());
Expand Down
18 changes: 10 additions & 8 deletions utilities/include/sparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
// limitations under the License.

#pragma once
#include <math.h>
#include <thrust/binary_search.h>
#include <thrust/gather.h>
#include <thrust/remove.h>
#include <thrust/sort.h>
#include <thrust/unique.h>
#include <thrust/execution_policy.h>

#include "structs.h"
#include "utils.h"
#include "vec_dh.h"

#include <math.h>

namespace manifold {

/** @ingroup Private */
Expand Down Expand Up @@ -124,7 +122,9 @@ class SparseIndices {
"Different number of values than indicies!");
auto zBegin = zip(v.begin(), x.begin(), begin(false), begin(true));
auto zEnd = zip(v.end(), x.end(), end(false), end(true));
size_t size = thrust::remove_if(thrust::device, zBegin, zEnd, firstNonFinite<T>()) - zBegin;
size_t size =
thrust::remove_if(thrust::device, zBegin, zEnd, firstNonFinite<T>()) -
zBegin;
v.resize(size);
x.resize(size, -1);
p.resize(size, -1);
Expand All @@ -142,10 +142,12 @@ class SparseIndices {
VecDH<char> found(size);
VecDH<int> temp(size);
thrust::fill(thrust::device, result.begin(), result.end(), missingVal);
thrust::binary_search(thrust::device, beginPQ(), endPQ(), pqBegin, pqEnd, found.begin());
thrust::lower_bound(thrust::device, beginPQ(), endPQ(), pqBegin, pqEnd, temp.begin());
thrust::gather_if(thrust::device, temp.begin(), temp.end(), found.begin(), val.begin(),
result.begin());
thrust::binary_search(thrust::device, beginPQ(), endPQ(), pqBegin, pqEnd,
found.begin());
thrust::lower_bound(thrust::device, beginPQ(), endPQ(), pqBegin, pqEnd,
temp.begin());
thrust::gather_if(thrust::device, temp.begin(), temp.end(), found.begin(),
val.begin(), result.begin());
return result;
}

Expand Down
19 changes: 7 additions & 12 deletions utilities/include/vec_dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@
// limitations under the License.

#pragma once
#include <iostream>
#include <thrust/universal_vector.h>
#include <thrust/execution_policy.h>
#include "structs.h"
#include <thrust/universal_vector.h>

namespace manifold {

/*
* Host and device vector implementation. This uses `thrust::universal_vector` for
* storage, so data can be moved by the hardware on demand, allows using more
* memory than the available GPU memory, reduce memory overhead and provide
* Host and device vector implementation. This uses `thrust::universal_vector`
* for storage, so data can be moved by the hardware on demand, allows using
* more memory than the available GPU memory, reduce memory overhead and provide
* speedup due to less synchronization.
*
* Due to https://github.com/NVIDIA/thrust/issues/1690 , `push_back` operations
Expand All @@ -40,9 +38,7 @@ namespace manifold {
template <typename T>
class VecDH {
public:
VecDH() {
impl_ = thrust::universal_vector<T>();
}
VecDH() { impl_ = thrust::universal_vector<T>(); }

VecDH(int size, T val = T()) {
impl_.resize(size, val);
Expand Down Expand Up @@ -119,8 +115,7 @@ class VecDH {
}

int size() const {
if (!cacheModified)
return impl_.size();
if (!cacheModified) return impl_.size();
return cache.size();
}

Expand Down Expand Up @@ -263,7 +258,7 @@ class VecDH {
cache.reserve(n);
}

void Dump() const {
void Dump() const {
syncCache();
manifold::Dump(cache);
}
Expand Down

0 comments on commit a7791d0

Please sign in to comment.