Skip to content

Commit

Permalink
dense: automatic caching of depth-maps
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcseacave committed Jul 31, 2024
1 parent 7b01622 commit b2a0d93
Show file tree
Hide file tree
Showing 14 changed files with 662 additions and 137 deletions.
9 changes: 7 additions & 2 deletions libs/Common/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -1257,10 +1257,15 @@ class cList
{
ASSERT(newVectorSize > _vectorSize);
// grow by 50% or at least to minNewVectorSize
const IDX expoVectorSize(_vectorSize + (_vectorSize>>1));
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));
if (growCapacity > maxGrowCapacity)
expoVectorSize = _vectorSize + maxGrowCapacity / sizeof(TYPE);
// allocate a larger chunk of memory, copy the data and delete the old chunk
if (newVectorSize < expoVectorSize)
newVectorSize = expoVectorSize;
// allocate a larger chunk of memory, copy the data and delete the old chunk
TYPE* const tmp(_vector);
_vector = (TYPE*) operator new[] (newVectorSize * sizeof(TYPE));
_ArrayMoveConstruct<true>(_vector, tmp, _size);
Expand Down
115 changes: 115 additions & 0 deletions libs/Common/ListFIFO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* ListFIFO.h
*
* Copyright (c) 2014-2024 SEACAVE
*
* Author(s):
*
* cDc <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Additional Terms:
*
* You are required to preserve legal notices and author attributions in
* that material or in the Appropriate Legal Notices displayed by works
* containing it.
*/

#pragma once
#ifndef _MVS_LISTFIFO_H_
#define _MVS_LISTFIFO_H_


// I N C L U D E S /////////////////////////////////////////////////

#include <list>
#include <unordered_map>


// S T R U C T S ///////////////////////////////////////////////////

namespace SEACAVE {

// Tracks accesses of some hash-able type T and records the least recently accessed.
template<typename T>
class ListFIFO {
public:
// add or move an key to the front
void Put(const T& key) {
const auto it = map.find(key);
if (it != map.end()) {
// if key exists, remove it from its current position
order.erase(it->second);
}
// add the key to the front
order.push_front(key);
map[key] = order.begin();
}

// remove and return the least used key (from the back)
T Pop() {
ASSERT(!IsEmpty());
const T leastUsed = order.back();
order.pop_back();
map.erase(leastUsed);
return leastUsed;
}

// return the least used key (from the back)
const T& Back() {
ASSERT(!IsEmpty());
return order.back();
}

// check if the list is empty
bool IsEmpty() const {
return order.empty();
}

// get the size of the list
size_t Size() const {
return order.size();
}

// return true if the key is in the list
bool Contains(const T& key) const {
return map.find(key) != map.end();
}

// return the keys currently in cache
const std::list<T>& GetCachedValues() const {
return order;
}

// print the current order of elements
void PrintOrder() const {
std::cout << "Current order: ";
for (const auto& element : order) {
std::cout << element << " ";
}
std::cout << std::endl;
}

private:
std::list<T> order;
std::unordered_map<T, typename std::list<T>::iterator> map;
};
/*----------------------------------------------------------------*/

} // namespace SEACAVE

#endif
5 changes: 5 additions & 0 deletions libs/Common/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <optional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <iterator>
#include <chrono>
#include <cmath>
#include <ctime>
#include <random>
#include <thread>
#ifdef _USE_OPENMP
#include <omp.h>
#endif
Expand Down Expand Up @@ -1604,6 +1607,8 @@ class TDMatrix : public cv::Mat_<TYPE>
inline size_t elem_stride() const { ASSERT(dims == 2 && step[1] == sizeof(TYPE)); return step[1]; }
/// Compute the area of the 2D matrix
inline int area() const { ASSERT(dims == 2); return cols*rows; }
/// Compute the memory size of this matrix (in bytes)
inline size_t memory_size() const { return cv::Mat::total() * cv::Mat::elemSize(); }

/// Is this coordinate inside the 2D matrix?
template <typename T>
Expand Down
16 changes: 14 additions & 2 deletions libs/Common/Types.inl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace std {

//namespace tr1 {
// Specializations for unordered containers
template <> struct hash<SEACAVE::ImageRef>
{
Expand All @@ -23,7 +22,20 @@ template <> struct hash<SEACAVE::ImageRef>
return std::hash<uint64_t>()((const uint64_t&)v);
}
};
//} // namespace tr1

// 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);
if (!result.second)
result.first->second = value;
}
template <typename Key, typename T>
void MapPut(std::unordered_map<Key, T>* map, const Key& key, const T& value) {
auto result = map->emplace(key, value);
if (!result.second)
result.first->second = value;
}

} // namespace std

Expand Down
97 changes: 58 additions & 39 deletions libs/Common/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,48 +264,12 @@ String Util::GetCPUInfo()
#endif
return cpu;
}
/*----------------------------------------------------------------*/

String Util::GetRAMInfo()
{
#if defined(_MSC_VER)

#ifdef _WIN64
MEMORYSTATUSEX memoryStatus;
memset(&memoryStatus, sizeof(MEMORYSTATUSEX), 0);
memoryStatus.dwLength = sizeof(memoryStatus);
::GlobalMemoryStatusEx(&memoryStatus);
const size_t nTotalPhys((size_t)memoryStatus.ullTotalPhys);
const size_t nTotalVirtual((size_t)memoryStatus.ullTotalVirtual);
#else
MEMORYSTATUS memoryStatus;
memset(&memoryStatus, sizeof(MEMORYSTATUS), 0);
memoryStatus.dwLength = sizeof(MEMORYSTATUS);
::GlobalMemoryStatus(&memoryStatus);
const size_t nTotalPhys((size_t)memoryStatus.dwTotalPhys);
const size_t nTotalVirtual((size_t)memoryStatus.dwTotalVirtual);
#endif

#elif defined(__APPLE__)

int mib[2] = {CTL_HW, HW_MEMSIZE};
const unsigned namelen = sizeof(mib) / sizeof(mib[0]);
size_t len = sizeof(size_t);
size_t nTotalPhys;
sysctl(mib, namelen, &nTotalPhys, &len, NULL, 0);
const size_t nTotalVirtual(nTotalPhys);

#else // __GNUC__

struct sysinfo info;
sysinfo(&info);
const size_t nTotalPhys((size_t)info.totalram);
const size_t nTotalVirtual((size_t)info.totalswap);

#endif // _MSC_VER
return formatBytes(nTotalPhys) + _T(" Physical Memory ") + formatBytes(nTotalVirtual) + _T(" Virtual Memory");
const MemoryInfo memInfo(GetMemoryInfo());
return formatBytes(memInfo.totalPhysical) + _T(" Physical Memory ") + formatBytes(memInfo.totalVirtual) + _T(" Virtual Memory");
}
/*----------------------------------------------------------------*/

String Util::GetOSInfo()
{
Expand Down Expand Up @@ -430,7 +394,6 @@ String Util::GetOSInfo()

#endif // _MSC_VER
}
/*----------------------------------------------------------------*/

String Util::GetDiskInfo(const String& path)
{
Expand Down Expand Up @@ -734,6 +697,62 @@ void Util::LogMemoryInfo()
#endif // _PLATFORM_X86



// get the total & free physical & virtual memory (in bytes)
Util::MemoryInfo Util::GetMemoryInfo()
{
#if defined(_MSC_VER) // windows

#ifdef _WIN64
MEMORYSTATUSEX status;
status.dwLength = sizeof(MEMORYSTATUSEX);
if (::GlobalMemoryStatusEx(&status) == FALSE) {
ASSERT(false);
return MemoryInfo();
}
return MemoryInfo(status.ullTotalPhys, status.ullAvailPhys, status.ullTotalVirtual, status.ullAvailVirtual);
#else
MEMORYSTATUS status;
status.dwLength = sizeof(MEMORYSTATUS);
if (::GlobalMemoryStatus(&status) == FALSE) {
ASSERT(false);
return MemoryInfo();
}
return MemoryInfo(status.dwTotalPhys, status.dwAvailPhys, status.dwTotalVirtual, status.dwAvailVirtual);
#endif


#elif defined(__APPLE__) // mac

int mib[2] ={CTL_HW, HW_MEMSIZE};
u_int namelen = sizeof(mib) / sizeof(mib[0]);
size_t len = sizeof(size_t);
size_t total_mem;
if (sysctl(mib, namelen, &total_mem, &len, NULL, 0) < 0) {
ASSERT(false);
return MemoryInfo();
}
return MemoryInfo(total_mem);

#else // __GNUC__ // linux

struct sysinfo info;
if (sysinfo(&info) != 0) {
ASSERT(false);
return MemoryInfo();
}
return MemoryInfo(
(size_t)info.totalram*(size_t)info.mem_unit,
(size_t)info.freeram*(size_t)info.mem_unit,
(size_t)info.totalswap*(size_t)info.mem_unit,
(size_t)info.freeswap*(size_t)info.mem_unit
);

#endif
}
/*----------------------------------------------------------------*/


// Parses a ASCII command line string and returns an array of pointers to the command line arguments,
// along with a count of such arguments, in a way that is similar to the standard C run-time
// argv and argc values.
Expand Down
10 changes: 10 additions & 0 deletions libs/Common/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,16 @@ class GENERAL_API Util
static void LogBuild();
static void LogMemoryInfo();

struct MemoryInfo {
size_t totalPhysical;
size_t freePhysical;
size_t totalVirtual;
size_t freeVirtual;
MemoryInfo(size_t tP = 0, size_t fP = 0, size_t tV = 0, size_t fV = 0)
: totalPhysical(tP), freePhysical(fP), totalVirtual(tV), freeVirtual(fV) {}
};
static MemoryInfo GetMemoryInfo();

static LPSTR* CommandLineToArgvA(LPCSTR CmdLine, size_t& _argc);
static String CommandLineToString(size_t argc, LPCTSTR* argv) {
String strCmdLine;
Expand Down
4 changes: 4 additions & 0 deletions libs/MVS/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ class MVS_API Camera : public CameraIntern

// compute the projection scale in this camera of the given world point
template <typename TYPE>
inline TYPE GetFootprintImage(TYPE depth) const {
return static_cast<TYPE>(GetFocalLength() / depth);
}
template <typename TYPE>
inline TYPE GetFootprintImage(const TPoint3<TYPE>& X) const {
#if 0
const TYPE fSphereRadius(1);
Expand Down
Loading

0 comments on commit b2a0d93

Please sign in to comment.