Skip to content

Commit

Permalink
mesh: implement RemoveDegenerateFaces() and RemoveDuplicatedVertices()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcseacave committed Sep 1, 2024
1 parent b1d8f0d commit 2090fa9
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 89 deletions.
11 changes: 5 additions & 6 deletions apps/InterfaceCOLMAP/InterfaceCOLMAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,12 @@ struct Camera {

struct CameraHash {
size_t operator()(const Camera& camera) const {
const size_t h1(std::hash<String>()(camera.model));
const size_t h2(std::hash<uint32_t>()(camera.width));
const size_t h3(std::hash<uint32_t>()(camera.height));
size_t h(h1 ^ ((h2 ^ (h3 << 1)) << 1));
size_t seed = std::hash<String>()(camera.model);
std::hash_combine(seed, camera.width);
std::hash_combine(seed, camera.height);
for (REAL p: camera.params)
h = std::hash<REAL>()(p) ^ (h << 1);
return h;
std::hash_combine(seed, p);
return seed;
}
};
struct CameraEqualTo {
Expand Down
2 changes: 1 addition & 1 deletion apps/ReconstructMesh/ReconstructMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ bool Export3DProjections(Scene& scene, const String& inputFileName) {
const Mesh::Octree octree(scene.mesh.vertices, [](Mesh::Octree::IDX_TYPE size, Mesh::Octree::Type /*radius*/) {
return size > 256;
});
scene.mesh.ListIncidenteFaces();
scene.mesh.ListIncidentFaces();

// save 3D coord in the output file
const Image& imgToExport = scene.images[imgID];
Expand Down
2 changes: 1 addition & 1 deletion apps/Viewer/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class EVTComputeOctree : public Event
Scene::OctreeMesh octMesh(scene.mesh.vertices, [](Scene::OctreeMesh::IDX_TYPE size, Scene::OctreeMesh::Type /*radius*/) {
return size > 256;
});
scene.mesh.ListIncidenteFaces();
scene.mesh.ListIncidentFaces();
pScene->octMesh.Swap(octMesh);
} else
if (!scene.pointcloud.IsEmpty()) {
Expand Down
74 changes: 38 additions & 36 deletions libs/Common/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ class cList
}

// construct a list containing size initialized elements
cList(IDX size) : _size(size), _vectorSize(size), _vector((TYPE*)operator new[] (size * sizeof(TYPE)))
cList(IDX size) : _size(size), _vectorSize(size), _vector((TYPE*)operator new[] (static_cast<size_t>(size) * sizeof(TYPE)))
{
ASSERT(size > 0 && size < NO_INDEX);
_ArrayConstruct(_vector, size);
}

// construct a list containing size initialized elements and allocated space for _reserved elements
cList(IDX size, IDX _reserved) : _size(size), _vectorSize(_reserved), _vector((TYPE*)operator new[] (_reserved * sizeof(TYPE)))
cList(IDX size, IDX _reserved) : _size(size), _vectorSize(_reserved), _vector((TYPE*)operator new[] (static_cast<size_t>(_reserved) * sizeof(TYPE)))
{
ASSERT(_reserved >= size && _reserved < NO_INDEX);
_ArrayConstruct(_vector, size);
Expand All @@ -142,23 +142,21 @@ class cList
ASSERT(_size == 0);
return;
}
_vector = (TYPE*)(operator new[] (_vectorSize * sizeof(TYPE)));
_vector = (TYPE*)(operator new[] (static_cast<size_t>(_vectorSize) * sizeof(TYPE)));
_ArrayCopyConstruct(_vector, rList._vector, _size);
}
#ifdef _SUPPORT_CPP11
// copy constructor: creates a move-copy of the given list
// move constructor: creates a move-copy of the given list
cList(cList&& rList) : _size(rList._size), _vectorSize(rList._vectorSize), _vector(rList._vector)
{
rList._Init();
}
#endif

// constructor a list from a raw data array
explicit inline cList(TYPE* pDataBegin, TYPE* pDataEnd) : _size((IDX)(pDataEnd-pDataBegin)), _vectorSize(_size)
{
if (_vectorSize == 0)
return;
_vector = (TYPE*) operator new[] (_vectorSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(_vectorSize) * sizeof(TYPE));
_ArrayCopyConstruct(_vector, pDataBegin, _size);
}

Expand All @@ -172,6 +170,11 @@ class cList
_Release();
}

// move the content from the given list
inline cList& operator=(cList&& rList)
{
return CopyOfRemove(rList);
}
// copy the content from the given list
inline cList& operator=(const cList& rList)
{
Expand All @@ -185,16 +188,14 @@ class cList
if (bForceResize || _vectorSize < rList._vectorSize) {
_Release();
_vectorSize = rList._vectorSize;
_vector = (TYPE*) operator new[] (_vectorSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(_vectorSize) * sizeof(TYPE));
_ArrayCopyConstruct(_vector, rList._vector, rList._size);
} else if (_size >= rList._size) {
_ArrayDestruct(_vector+rList._size, _size-rList._size);
_ArrayCopyRestrict(_vector, rList._vector, rList._size);
} else {
if (_size >= rList._size) {
_ArrayDestruct(_vector+rList._size, _size-rList._size);
_ArrayCopyRestrict(_vector, rList._vector, rList._size);
} else {
_ArrayCopyRestrict(_vector, rList._vector, _size);
_ArrayCopyConstruct(_vector+_size, rList._vector+_size, rList._size-_size);
}
_ArrayCopyRestrict(_vector, rList._vector, _size);
_ArrayCopyConstruct(_vector+_size, rList._vector+_size, rList._size-_size);
}
_size = rList._size;
return *this;
Expand All @@ -207,16 +208,14 @@ class cList
if (bForceResize || _vectorSize < nSize) {
_Release();
_vectorSize = nSize;
_vector = (TYPE*) operator new[] (_vectorSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(_vectorSize) * sizeof(TYPE));
_ArrayCopyConstruct(_vector, pData, nSize);
} else if (_size >= nSize) {
_ArrayDestruct(_vector+nSize, _size-nSize);
_ArrayCopyRestrict(_vector, pData, nSize);
} else {
if (_size >= nSize) {
_ArrayDestruct(_vector+nSize, _size-nSize);
_ArrayCopyRestrict(_vector, pData, nSize);
} else {
_ArrayCopyRestrict(_vector, pData, _size);
_ArrayCopyConstruct(_vector+_size, pData+_size, nSize-_size);
}
_ArrayCopyRestrict(_vector, pData, _size);
_ArrayCopyConstruct(_vector+_size, pData+_size, nSize-_size);
}
_size = nSize;
return *this;
Expand All @@ -231,8 +230,7 @@ class cList
_size = rList._size;
_vectorSize = rList._vectorSize;
_vector = rList._vector;
rList._vector = NULL;
rList._size = rList._vectorSize = 0;
rList._Init();
return *this;
}

Expand Down Expand Up @@ -318,7 +316,7 @@ class cList
// Set the allocated memory (normally used for types without constructor).
inline void Memset(uint8_t val)
{
memset(_vector, val, _size * sizeof(TYPE));
memset(_vector, val, static_cast<size_t>(_size) * sizeof(TYPE));
}
inline void MemsetValue(ARG_TYPE val)
{
Expand All @@ -336,7 +334,7 @@ class cList
_vector = NULL;
return;
}
_vector = (TYPE*) operator new[] (newSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(newSize) * sizeof(TYPE));
_ArrayConstruct(_vector, newSize);
}

Expand Down Expand Up @@ -408,7 +406,11 @@ class cList
}
inline size_t GetDataSize() const
{
return sizeof(TYPE)*_size;
return sizeof(TYPE)*static_cast<size_t>(_size);
}
inline size_t GetMemorySize() const
{
return sizeof(cList)+sizeof(TYPE)*static_cast<size_t>(_vectorSize);
}

inline TYPE* Begin() const
Expand Down Expand Up @@ -1271,15 +1273,15 @@ class cList
// grow by 50% or at least to minNewVectorSize
IDX expoVectorSize(_vectorSize + (_vectorSize>>1));
// cap growth for very large vectors
const IDX maxGrowCapacity(3*1024*1024*1024ull/*3GB*/);
const IDX growCapacity((expoVectorSize - _vectorSize) * sizeof(TYPE));
const size_t maxGrowCapacity(3*1024*1024*1024ull/*3GB*/);
const size_t growCapacity(static_cast<size_t>(expoVectorSize - _vectorSize) * sizeof(TYPE));
if (growCapacity > maxGrowCapacity)
expoVectorSize = _vectorSize + maxGrowCapacity / sizeof(TYPE);
expoVectorSize = _vectorSize + static_cast<IDX>(maxGrowCapacity / sizeof(TYPE));
// allocate a larger chunk of memory, copy the data and delete the old chunk
if (newVectorSize < expoVectorSize)
newVectorSize = expoVectorSize;
TYPE* const tmp(_vector);
_vector = (TYPE*) operator new[] (newVectorSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(newVectorSize) * sizeof(TYPE));
_ArrayMoveConstruct<true>(_vector, tmp, _size);
_vectorSize = newVectorSize;
operator delete[] (tmp);
Expand All @@ -1301,7 +1303,7 @@ class cList
_vector = NULL;
} else {
TYPE* const tmp(_vector);
_vector = (TYPE*) operator new[] (_vectorSize * sizeof(TYPE));
_vector = (TYPE*) operator new[] (static_cast<size_t>(_vectorSize) * sizeof(TYPE));
_ArrayMoveConstruct<true>(_vector, tmp, _vectorSize);
operator delete[] (tmp);
}
Expand Down Expand Up @@ -1363,7 +1365,7 @@ class cList
(src+n)->~TYPE();
}
} else {
const size_t _size(sizeof(TYPE)*n);
const size_t _size(sizeof(TYPE)*static_cast<size_t>(n));
if (bRestrict)
memcpy((void*)dst, (const void*)src, _size);
else
Expand All @@ -1380,7 +1382,7 @@ class cList
(src+n)->~TYPE();
}
} else {
const size_t _size(sizeof(TYPE)*n);
const size_t _size(sizeof(TYPE)*static_cast<size_t>(n));
if (useConstruct == 1)
while (n--)
(dst+n)->~TYPE();
Expand Down Expand Up @@ -1410,7 +1412,7 @@ class cList
typedef std::vector<Type> VectorType;
inline cList(const VectorType& rList) { CopyOf(&rList[0], rList.size()); }
#ifdef _SUPPORT_CPP11
inline cList(std::initializer_list<Type> l) : _size(0), _vectorSize((size_type)l.size()), _vector(NULL) { ASSERT(l.size()<NO_INDEX); if (_vectorSize == 0) return; _vector = (Type*) operator new[] (_vectorSize*sizeof(Type)); const Type* first(l.begin()); do new(_vector + _size++) Type(*first++); while (first!=l.end()); }
inline cList(std::initializer_list<Type> l) : _size(0), _vectorSize((size_type)l.size()), _vector(NULL) { ASSERT(l.size()<NO_INDEX); if (_vectorSize == 0) return; _vector = (Type*) operator new[] (static_cast<size_t>(_vectorSize)*sizeof(Type)); const Type* first(l.begin()); do new(_vector + _size++) Type(*first++); while (first!=l.end()); }
#endif
inline bool empty() const { return IsEmpty(); }
inline size_type size() const { return GetSize(); }
Expand Down
62 changes: 54 additions & 8 deletions libs/Common/Types.inl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,63 @@

namespace std {

// Specializations for unordered containers
template <> struct hash<SEACAVE::ImageRef>
{
typedef SEACAVE::ImageRef argument_type;
typedef size_t result_type;
result_type operator()(const argument_type& v) const {
return std::hash<uint64_t>()((const uint64_t&)v);
// combine hash values (as in boost)
namespace {
template <class T>
inline void hash_combine(std::size_t& seed, T const& v) {
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t& seed, Tuple const& tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple, 0> {
static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); }
};
} // namespace

// hash specialization for pairs/tuples
template <typename T, typename U>
struct hash<std::pair<T, U>> {
std::size_t operator()(const std::pair<T, U>& x) const {
size_t seed = std::hash<T>()(x.first);
hash_combine<U>(seed, x.second);
return seed;
}
};
template <typename... T>
struct hash<std::tuple<T...>> {
size_t operator()(const std::tuple<T...>& t) const {
size_t seed = 0;
HashValueImpl<std::tuple<T...>>::apply(seed, t);
return seed;
}
};

// hash specializations for OpenCV points
template <typename T>
struct hash<cv::Point_<T>> {
size_t operator()(const cv::Point_<T>& v) const {
size_t seed = std::hash<T>()(v.x);
std::hash_combine(seed, v.y);
return seed;
}
};
template <typename T>
struct hash<cv::Point3_<T>> {
size_t operator()(const cv::Point3_<T>& v) const {
size_t seed = std::hash<T>()(v.x);
std::hash_combine(seed, v.y);
std::hash_combine(seed, v.z);
return seed;
}
};

// Adds the given key-value pair in the map, overwriting the current value if the key exists
// adds the given key-value pair in the map, overwriting the current value if the key exists
template <typename Key, typename T>
void MapPut(std::map<Key, T>* map, const Key& key, const T& value) {
auto result = map->emplace(key, value);
Expand Down
Loading

0 comments on commit 2090fa9

Please sign in to comment.