Skip to content

Commit

Permalink
fixed properties (#1051)
Browse files Browse the repository at this point in the history
* fixed properties

* fix formatting

* fix compile

* consolidate debug functions
  • Loading branch information
elalish authored Nov 17, 2024
1 parent 7de71d4 commit 1810105
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 102 deletions.
61 changes: 61 additions & 0 deletions include/manifold/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <limits>
#include <vector>

#ifdef MANIFOLD_DEBUG
#include <chrono>
#endif

#include "manifold/linalg.h"

namespace manifold {
Expand Down Expand Up @@ -586,4 +590,61 @@ struct ExecutionParams {
bool cleanupTriangles = true;
};
/** @} */

#ifdef MANIFOLD_DEBUG
inline std::ostream& operator<<(std::ostream& stream, const Box& box) {
return stream << "min: " << box.min << ", "
<< "max: " << box.max;
}

inline std::ostream& operator<<(std::ostream& stream, const Rect& box) {
return stream << "min: " << box.min << ", "
<< "max: " << box.max;
}

/**
* Print the contents of this vector to standard output. Only exists if compiled
* with MANIFOLD_DEBUG flag.
*/
template <typename T>
void Dump(const std::vector<T>& vec) {
std::cout << "Vec = " << std::endl;
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << i << ", " << vec[i] << ", " << std::endl;
}
std::cout << std::endl;
}

template <typename T>
void Diff(const std::vector<T>& a, const std::vector<T>& b) {
std::cout << "Diff = " << std::endl;
if (a.size() != b.size()) {
std::cout << "a and b must have the same length, aborting Diff"
<< std::endl;
return;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != b[i])
std::cout << i << ": " << a[i] << ", " << b[i] << std::endl;
}
std::cout << std::endl;
}

struct Timer {
std::chrono::high_resolution_clock::time_point start, end;

void Start() { start = std::chrono::high_resolution_clock::now(); }

void Stop() { end = std::chrono::high_resolution_clock::now(); }

float Elapsed() {
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
}
void Print(std::string message) {
std::cout << "----------- " << std::round(Elapsed()) << " ms for "
<< message << std::endl;
}
};
#endif
} // namespace manifold
41 changes: 41 additions & 0 deletions include/manifold/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
#include <iosfwd> // For forward definitions of std::ostream
#include <type_traits> // For std::enable_if, std::is_same, std::declval

#ifdef MANIFOLD_DEBUG
#include <iomanip>
#include <iostream>
#endif

// In Visual Studio 2015, `constexpr` applied to a member function implies
// `const`, which causes ambiguous overload resolution
#if defined(_MSC_VER) && (_MSC_VER <= 1900)
Expand Down Expand Up @@ -2360,6 +2365,42 @@ struct converter<std::array<T, 4>, vec<T, 4>> {
}
};
/** @} */

#ifdef MANIFOLD_DEBUG
template <class T>
std::ostream &operator<<(std::ostream &out, const vec<T, 1> &v) {
return out << '{' << v[0] << '}';
}
template <class T>
std::ostream &operator<<(std::ostream &out, const vec<T, 2> &v) {
return out << '{' << v[0] << ',' << v[1] << '}';
}
template <class T>
std::ostream &operator<<(std::ostream &out, const vec<T, 3> &v) {
return out << '{' << v[0] << ',' << v[1] << ',' << v[2] << '}';
}
template <class T>
std::ostream &operator<<(std::ostream &out, const vec<T, 4> &v) {
return out << '{' << v[0] << ',' << v[1] << ',' << v[2] << ',' << v[3] << '}';
}

template <class T, int M>
std::ostream &operator<<(std::ostream &out, const mat<T, M, 1> &m) {
return out << '{' << m[0] << '}';
}
template <class T, int M>
std::ostream &operator<<(std::ostream &out, const mat<T, M, 2> &m) {
return out << '{' << m[0] << ',' << m[1] << '}';
}
template <class T, int M>
std::ostream &operator<<(std::ostream &out, const mat<T, M, 3> &m) {
return out << '{' << m[0] << ',' << m[1] << ',' << m[2] << '}';
}
template <class T, int M>
std::ostream &operator<<(std::ostream &out, const mat<T, M, 4> &m) {
return out << '{' << m[0] << ',' << m[1] << ',' << m[2] << ',' << m[3] << '}';
}
#endif
} // namespace linalg

namespace std {
Expand Down
2 changes: 1 addition & 1 deletion src/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct UpdateProperties {
auto old = std::atomic_exchange(
reinterpret_cast<std::atomic<uint8_t>*>(&counters[propVert]),
static_cast<uint8_t>(1));
if (old == 1) return;
if (old == 1) continue;

for (int p = 0; p < oldNumProp; ++p) {
properties[numProp * propVert + p] =
Expand Down
97 changes: 0 additions & 97 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
#include <mutex>
#include <unordered_map>

#ifdef MANIFOLD_DEBUG
#include <chrono>
#include <iomanip>
#include <iostream>
#endif

#include "./vec.h"
#include "manifold/common.h"

Expand Down Expand Up @@ -230,95 +224,4 @@ inline mat4 Mat4(mat3x4 a) {
}
inline mat3 Mat3(mat2x3 a) { return mat3({a[0], 0}, {a[1], 0}, {a[2], 1}); }

#ifdef MANIFOLD_DEBUG

template <class T>
std::ostream& operator<<(std::ostream& out, const la::vec<T, 1>& v) {
return out << '{' << v[0] << '}';
}
template <class T>
std::ostream& operator<<(std::ostream& out, const la::vec<T, 2>& v) {
return out << '{' << v[0] << ',' << v[1] << '}';
}
template <class T>
std::ostream& operator<<(std::ostream& out, const la::vec<T, 3>& v) {
return out << '{' << v[0] << ',' << v[1] << ',' << v[2] << '}';
}
template <class T>
std::ostream& operator<<(std::ostream& out, const la::vec<T, 4>& v) {
return out << '{' << v[0] << ',' << v[1] << ',' << v[2] << ',' << v[3] << '}';
}

template <class T, int M>
std::ostream& operator<<(std::ostream& out, const la::mat<T, M, 1>& m) {
return out << '{' << m[0] << '}';
}
template <class T, int M>
std::ostream& operator<<(std::ostream& out, const la::mat<T, M, 2>& m) {
return out << '{' << m[0] << ',' << m[1] << '}';
}
template <class T, int M>
std::ostream& operator<<(std::ostream& out, const la::mat<T, M, 3>& m) {
return out << '{' << m[0] << ',' << m[1] << ',' << m[2] << '}';
}
template <class T, int M>
std::ostream& operator<<(std::ostream& out, const la::mat<T, M, 4>& m) {
return out << '{' << m[0] << ',' << m[1] << ',' << m[2] << ',' << m[3] << '}';
}

inline std::ostream& operator<<(std::ostream& stream, const Box& box) {
return stream << "min: " << box.min << ", "
<< "max: " << box.max;
}

inline std::ostream& operator<<(std::ostream& stream, const Rect& box) {
return stream << "min: " << box.min << ", "
<< "max: " << box.max;
}

/**
* Print the contents of this vector to standard output. Only exists if compiled
* with MANIFOLD_DEBUG flag.
*/
template <typename T>
void Dump(const std::vector<T>& vec) {
std::cout << "Vec = " << std::endl;
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << i << ", " << vec[i] << ", " << std::endl;
}
std::cout << std::endl;
}

template <typename T>
void Diff(const std::vector<T>& a, const std::vector<T>& b) {
std::cout << "Diff = " << std::endl;
if (a.size() != b.size()) {
std::cout << "a and b must have the same length, aborting Diff"
<< std::endl;
return;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != b[i])
std::cout << i << ": " << a[i] << ", " << b[i] << std::endl;
}
std::cout << std::endl;
}

struct Timer {
std::chrono::high_resolution_clock::time_point start, end;

void Start() { start = std::chrono::high_resolution_clock::now(); }

void Stop() { end = std::chrono::high_resolution_clock::now(); }

float Elapsed() {
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
}
void Print(std::string message) {
std::cout << "----------- " << std::round(Elapsed()) << " ms for "
<< message << std::endl;
}
};
#endif
} // namespace manifold
1 change: 1 addition & 0 deletions test/samples_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ TEST(Samples, Scallop) {
3, colorCurvature);
EXPECT_NEAR(scallop.Volume(), 39.9, 0.1);
EXPECT_NEAR(scallop.SurfaceArea(), 79.3, 0.1);
EXPECT_EQ(scallop.NumVert(), scallop.NumPropVert());
CheckGL(scallop);

#ifdef MANIFOLD_EXPORT
Expand Down
4 changes: 2 additions & 2 deletions test/smooth_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ TEST(Smooth, TruncatedCone) {
Manifold smooth = cone.SmoothOut().RefineToLength(0.5).CalculateNormals(0);
EXPECT_NEAR(smooth.Volume(), 1158.61, 0.01);
EXPECT_NEAR(smooth.SurfaceArea(), 768.12, 0.01);
MeshGL out = smooth.GetMeshGL();
CheckGL(out);
CheckGL(smooth, false);

Manifold smooth1 = cone.SmoothOut(180, 1).RefineToLength(0.5);
Manifold smooth2 = cone.SmoothOut(180, 0).RefineToLength(0.5);
EXPECT_NEAR(smooth2.Volume(), smooth1.Volume(), 0.01);
EXPECT_NEAR(smooth2.SurfaceArea(), smooth1.SurfaceArea(), 0.01);

#ifdef MANIFOLD_EXPORT
MeshGL out = smooth.GetMeshGL();
ExportOptions options2;
options2.faceted = false;
options2.mat.normalIdx = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void RelatedGL(const Manifold& out, const std::vector<MeshGL>& originals,
void ExpectMeshes(const Manifold& manifold,
const std::vector<MeshSize>& meshSize);
void CheckStrictly(const Manifold& manifold);
void CheckGL(const Manifold& manifold);
void CheckGL(const Manifold& manifold, bool noMerge = true);
#ifdef MANIFOLD_EXPORT
Manifold ReadMesh(const std::string& filename);
#endif
Expand Down
5 changes: 4 additions & 1 deletion test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,12 @@ void CheckStrictly(const Manifold& manifold) {
EXPECT_EQ(manifold.NumDegenerateTris(), 0);
}

void CheckGL(const Manifold& manifold) {
void CheckGL(const Manifold& manifold, bool noMerge) {
ASSERT_FALSE(manifold.IsEmpty());
const MeshGL meshGL = manifold.GetMeshGL();
if (noMerge) {
EXPECT_EQ(manifold.NumVert(), meshGL.NumVert());
}
EXPECT_EQ(meshGL.mergeFromVert.size(), meshGL.mergeToVert.size());
EXPECT_EQ(meshGL.mergeFromVert.size(), meshGL.NumVert() - manifold.NumVert());
EXPECT_EQ(meshGL.runIndex.size(), meshGL.runOriginalID.size() + 1);
Expand Down

0 comments on commit 1810105

Please sign in to comment.