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

chores #833

Merged
merged 26 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ option(MANIFOLD_DEBUG "Enable debug tracing/timing" OFF)
option(MANIFOLD_PYBIND "Build python bindings" OFF)
option(MANIFOLD_CBIND "Build C (FFI) bindings" ON)
option(MANIFOLD_JSBIND "Build js binding" ${EMSCRIPTEN})
option(MANIFOLD_EXCEPTIONS "Build manifold with exception enabled" ON)
option(BUILD_SHARED_LIBS "Build shared library" ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

Expand Down Expand Up @@ -59,8 +60,13 @@ endif()

if(EMSCRIPTEN)
message("Building for Emscripten")
set(MANIFOLD_FLAGS -fexceptions -D_LIBCUDACXX_HAS_THREAD_API_EXTERNAL -D_LIBCUDACXX_HAS_THREAD_API_CUDA)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sALLOW_MEMORY_GROWTH=1 -fexceptions -sDISABLE_EXCEPTION_CATCHING=0")
if(MANIFOLD_EXCEPTIONS)
set(MANIFOLD_FLAGS -fexceptions -D_LIBCUDACXX_HAS_THREAD_API_EXTERNAL -D_LIBCUDACXX_HAS_THREAD_API_CUDA)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sALLOW_MEMORY_GROWTH=1 -fexceptions -sDISABLE_EXCEPTION_CATCHING=0")
else()
set(MANIFOLD_FLAGS -D_LIBCUDACXX_HAS_THREAD_API_EXTERNAL -D_LIBCUDACXX_HAS_THREAD_API_CUDA)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sALLOW_MEMORY_GROWTH=1")
endif()
set(MANIFOLD_PYBIND OFF)
set(BUILD_SHARED_LIBS OFF)
endif()
Expand Down
4 changes: 4 additions & 0 deletions src/manifold/include/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,20 @@ class CsgLeafNode;
struct MeshGL {
/// Number of property vertices
uint32_t NumVert() const {
#if MANIFOLD_EXCEPTIONS
if (vertProperties.size() / numProp >=
std::numeric_limits<unsigned int>::max())
throw std::out_of_range("mesh too large");
#endif
return vertProperties.size() / numProp;
};
/// Number of triangles
uint32_t NumTri() const {
#if MANIFOLD_EXCEPTIONS
if (vertProperties.size() / numProp >=
std::numeric_limits<unsigned int>::max())
throw std::out_of_range("mesh too large");
#endif
pca006132 marked this conversation as resolved.
Show resolved Hide resolved
return triVerts.size() / 3;
};

Expand Down
2 changes: 2 additions & 0 deletions src/manifold/src/boolean3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,10 @@ Boolean3::Boolean3(const Manifold::Impl &inP, const Manifold::Impl &inQ,
Intersect12(inQ, inP, s20, p2q0, s11, p1q1, z20, xyzz11, p2q1_, false);
PRINT("x21 size = " << x21_.size());

#if MANIFOLD_EXCEPTIONS
if (x12_.size() + x21_.size() >= std::numeric_limits<int>::max())
throw std::out_of_range("mesh too large");
#endif

Vec<int> p0 = p0q2.Copy(false);
p0q2.Resize(0);
Expand Down
4 changes: 4 additions & 0 deletions src/manifold/src/boolean_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ std::tuple<Vec<int>, Vec<int>> SizeOutput(
ZoneScoped;
Vec<int> sidesPerFacePQ(inP.NumTri() + inQ.NumTri(), 0);
// note: numFaceR <= facePQ2R.size() = sidesPerFacePQ.size() + 1
#if MANIFOLD_EXCEPTIONS
if (sidesPerFacePQ.size() + 1 >= std::numeric_limits<int>::max())
throw std::out_of_range("boolean result too large");
#endif

auto sidesPerFaceP = sidesPerFacePQ.view(0, inP.NumTri());
auto sidesPerFaceQ = sidesPerFacePQ.view(inP.NumTri(), inQ.NumTri());
Expand Down Expand Up @@ -544,9 +546,11 @@ void CreateProperties(Manifold::Impl &outR, const Manifold::Impl &inP,
propMissIdx[0].resize(inQ.NumPropVert(), -1);
propMissIdx[1].resize(inP.NumPropVert(), -1);

#if MANIFOLD_EXCEPTIONS
if (static_cast<size_t>(outR.NumVert()) * static_cast<size_t>(numProp) >=
std::numeric_limits<int>::max())
throw std::out_of_range("too many vertices");
#endif

outR.meshRelation_.properties.reserve(outR.NumVert() * numProp);
int idx = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/manifold/src/face_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ void Manifold::Impl::Face2Tri(const Vec<int>& faceEdge,
// prefix sum computation (assign unique index to each face) and preallocation
exclusive_scan(autoPolicy(triCount.size()), triCount.begin(), triCount.end(),
triCount.begin(), 0_z);
#if MANIFOLD_EXCEPTIONS
if (triCount.back() >= std::numeric_limits<int>::max())
throw std::out_of_range("too many triangles");
#endif
triVerts.resize(triCount.back());
triNormal.resize(triCount.back());
triRef.resize(triCount.back());
Expand Down
16 changes: 14 additions & 2 deletions src/manifold/src/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,14 @@ Manifold::Impl::Impl(const MeshGL& meshGL,

if (meshGL.numProp > 3 &&
static_cast<size_t>(numVert) * static_cast<size_t>(meshGL.numProp - 3) >=
std::numeric_limits<int>::max())
std::numeric_limits<int>::max()) {
#if MANIFOLD_EXCEPTIONS
throw std::out_of_range("mesh too large");
#else
MarkFailure(Error::InvalidConstruction);
return;
#endif
}

if (meshGL.numProp < 3) {
MarkFailure(Error::MissingPositionProperties);
Expand Down Expand Up @@ -486,8 +492,14 @@ Manifold::Impl::Impl(const Mesh& mesh, const MeshRelationD& relation,
const std::vector<float>& propertyTolerance,
bool hasFaceIDs)
: vertPos_(mesh.vertPos), halfedgeTangent_(mesh.halfedgeTangent) {
if (mesh.triVerts.size() >= std::numeric_limits<int>::max())
if (mesh.triVerts.size() >= std::numeric_limits<int>::max()) {
#if MANIFOLD_EXCEPTIONS
throw std::out_of_range("mesh too large");
#else
MarkFailure(Error::InvalidConstruction);
return;
#endif
}
meshRelation_ = {relation.originalID, relation.numProp, relation.properties,
relation.meshIDtransform};

Expand Down
4 changes: 4 additions & 0 deletions src/polygon/src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,12 @@ namespace manifold {
std::vector<glm::ivec3> TriangulateIdx(const PolygonsIdx &polys,
float precision) {
std::vector<glm::ivec3> triangles;
#if MANIFOLD_EXCEPTIONS
try {
#endif
EarClip triangulator(polys, precision);
triangles = triangulator.Triangulate();
#if MANIFOLD_EXCEPTIONS
#ifdef MANIFOLD_DEBUG
if (params.intermediateChecks) {
CheckTopology(triangles, polys);
Expand All @@ -897,6 +900,7 @@ std::vector<glm::ivec3> TriangulateIdx(const PolygonsIdx &polys,
} catch (const std::exception &e) {
#endif
}
#endif
return triangles;
}

Expand Down
26 changes: 24 additions & 2 deletions src/utilities/include/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

#pragma once
#if MANIFOLD_EXCEPTIONS
#include <exception>
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to include this per file, or can it just be wherever we define ASSERT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah probably just need it when we define ASSERT

#if TRACY_ENABLE && TRACY_MEMORY_USAGE
#include "tracy/Tracy.hpp"
#else
Expand Down Expand Up @@ -62,7 +64,9 @@ class Vec : public VecView<T> {
auto policy = autoPolicy(this->size_);
if (this->size_ != 0) {
this->ptr_ = reinterpret_cast<T *>(malloc(this->size_ * sizeof(T)));
#if MANIFOLD_EXCEPTIONS
if (this->ptr_ == nullptr) throw std::bad_alloc();
#endif
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3);
uninitialized_copy(policy, vec.begin(), vec.end(), this->ptr_);
}
Expand All @@ -74,7 +78,9 @@ class Vec : public VecView<T> {
auto policy = autoPolicy(this->size_);
if (this->size_ != 0) {
this->ptr_ = reinterpret_cast<T *>(malloc(this->size_ * sizeof(T)));
#if MANIFOLD_EXCEPTIONS
if (this->ptr_ == nullptr) throw std::bad_alloc();
#endif
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3);
uninitialized_copy(policy, vec.begin(), vec.end(), this->ptr_);
}
Expand Down Expand Up @@ -113,7 +119,9 @@ class Vec : public VecView<T> {
auto policy = autoPolicy(this->size_);
if (this->size_ != 0) {
this->ptr_ = reinterpret_cast<T *>(malloc(this->size_ * sizeof(T)));
#if MANIFOLD_EXCEPTIONS
if (this->ptr_ == nullptr) throw std::bad_alloc();
#endif
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3);
uninitialized_copy(policy, other.begin(), other.end(), this->ptr_);
}
Expand Down Expand Up @@ -157,7 +165,9 @@ class Vec : public VecView<T> {
void reserve(size_t n) {
if (n > capacity_) {
T *newBuffer = reinterpret_cast<T *>(malloc(n * sizeof(T)));
#if MANIFOLD_EXCEPTIONS
if (newBuffer == nullptr) throw std::bad_alloc();
#endif
TracyAllocS(newBuffer, n * sizeof(T), 3);
if (this->size_ > 0)
uninitialized_copy(autoPolicy(this->size_), this->ptr_,
Expand Down Expand Up @@ -186,7 +196,9 @@ class Vec : public VecView<T> {
T *newBuffer = nullptr;
if (this->size_ > 0) {
newBuffer = reinterpret_cast<T *>(malloc(this->size_ * sizeof(T)));
#if MANIFOLD_EXCEPTIONS
if (newBuffer == nullptr) throw std::bad_alloc();
#endif
TracyAllocS(newBuffer, this->size_ * sizeof(T), 3);
uninitialized_copy(autoPolicy(this->size_), this->ptr_,
this->ptr_ + this->size_, newBuffer);
Expand All @@ -203,12 +215,17 @@ class Vec : public VecView<T> {
size_t length = std::numeric_limits<size_t>::max()) {
if (length == std::numeric_limits<size_t>::max()) {
length = this->size_ - offset;
#if MANIFOLD_EXCEPTIONS
if (length < 0) throw std::out_of_range("Vec::view out of range");
} else if (offset + length > this->size_ || offset < 0) {
#endif
}
#if MANIFOLD_EXCEPTIONS
else if (offset + length > this->size_ || offset < 0) {
throw std::out_of_range("Vec::view out of range");
} else if (length < 0) {
throw std::out_of_range("Vec::view negative length is not allowed");
}
#endif
return VecView<T>(this->ptr_ + offset, length);
}

Expand All @@ -217,12 +234,17 @@ class Vec : public VecView<T> {
size_t length = std::numeric_limits<size_t>::max()) const {
if (length == std::numeric_limits<size_t>::max()) {
length = this->size_ - offset;
#if MANIFOLD_EXCEPTIONS
if (length < 0) throw std::out_of_range("Vec::cview out of range");
} else if (offset + length > this->size_ || offset < 0) {
#endif
}
#if MANIFOLD_EXCEPTIONS
else if (offset + length > this->size_ || offset < 0) {
throw std::out_of_range("Vec::cview out of range");
} else if (length < 0) {
throw std::out_of_range("Vec::cview negative length is not allowed");
}
#endif
return VecView<const T>(this->ptr_ + offset, length);
}

Expand Down
22 changes: 16 additions & 6 deletions src/utilities/include/vec_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#pragma once

#if MANIFOLD_EXCEPTIONS
#include <stdexcept>
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.


namespace manifold {

Expand Down Expand Up @@ -46,16 +48,16 @@ class VecView {
operator VecView<const T>() const { return {ptr_, size_}; }

inline const T &operator[](size_t i) const {
if (i >= size_) {
throw std::out_of_range("Vec out of range");
}
#if MANIFOLD_EXCEPTIONS
if (i >= size_) throw std::out_of_range("Vec out of range");
#endif
return ptr_[i];
}

inline T &operator[](size_t i) {
if (i >= size_) {
throw std::out_of_range("Vec out of range");
}
#if MANIFOLD_EXCEPTIONS
if (i >= size_) throw std::out_of_range("Vec out of range");
#endif
pca006132 marked this conversation as resolved.
Show resolved Hide resolved
return ptr_[i];
}

Expand All @@ -69,26 +71,34 @@ class VecView {
Iter end() { return ptr_ + size_; }

const T &front() const {
#if MANIFOLD_EXCEPTIONS
if (size_ == 0)
throw std::out_of_range("attempt to take the front of an empty vector");
#endif
return ptr_[0];
}

const T &back() const {
#if MANIFOLD_EXCEPTIONS
if (size_ == 0)
throw std::out_of_range("attempt to take the back of an empty vector");
#endif
return ptr_[size_ - 1];
}

T &front() {
#if MANIFOLD_EXCEPTIONS
if (size_ == 0)
throw std::out_of_range("attempt to take the front of an empty vector");
#endif
return ptr_[0];
}

T &back() {
#if MANIFOLD_EXCEPTIONS
if (size_ == 0)
throw std::out_of_range("attempt to take the back of an empty vector");
#endif
return ptr_[size_ - 1];
}

Expand Down