Skip to content

Commit

Permalink
common: implement assert with message
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcseacave committed Sep 23, 2024
1 parent efce2f6 commit 208a441
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 141 deletions.
8 changes: 6 additions & 2 deletions apps/Tests/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ bool UnitTests()
bool PipelineTest(bool verbose=false)
{
TD_TIMER_START();
#if 0 && defined(_USE_CUDA)
// force CPU for testing even if CUDA is available
SEACAVE::CUDA::desiredDeviceID = -2;
#endif
Scene scene;
if (!scene.Load(MAKE_PATH("scene.mvs"))) {
VERBOSE("ERROR: TestDataset failed loading the scene!");
Expand All @@ -88,15 +92,15 @@ bool PipelineTest(bool verbose=false)
}
if (verbose)
scene.pointcloud.Save(MAKE_PATH("scene_dense.ply"));
if (!scene.ReconstructMesh() || scene.mesh.faces.size() < 40000u) {
if (!scene.ReconstructMesh() || scene.mesh.faces.size() < 25000u) {
VERBOSE("ERROR: TestDataset failed reconstructing the mesh!");
return false;
}
if (verbose)
scene.mesh.Save(MAKE_PATH("scene_dense_mesh.ply"));
constexpr float decimate = 0.7f;
scene.mesh.Clean(decimate);
if (!ISINSIDE(scene.mesh.faces.size(), 25000u, 45000u)) {
if (!ISINSIDE(scene.mesh.faces.size(), 20000u, 30000u)) {
VERBOSE("ERROR: TestDataset failed cleaning the mesh!");
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion libs/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ namespace SEACAVE { extern int g_nVerbosityLevel; }
#define LOG_ERR() GET_LOG() //or std::cerr


#ifdef PRINT_ASSERT_MSG
#undef PRINT_ASSERT_MSG
#define PRINT_ASSERT_MSG(exp, ...) {std::cout << SEACAVE::PrintMessageToString("ASSERTION FAILED: (" #exp ") ", __VA_ARGS__) << std::endl;}
#endif


// macros simplifying the task of composing file paths;
// WORKING_FOLDER and WORKING_FOLDER_FULL must be defined as strings
// containing the relative/full path to the working folder
Expand All @@ -86,7 +92,7 @@ namespace SEACAVE {
class String;
extern String g_strWorkingFolder; // empty by default (current folder)
extern String g_strWorkingFolderFull; // full path to current folder
}
} // namespace SEACAVE
#define WORKING_FOLDER g_strWorkingFolder // empty by default (current folder)
#define WORKING_FOLDER_FULL g_strWorkingFolderFull // full path to current folder
#endif
Expand Down
18 changes: 13 additions & 5 deletions libs/Common/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@
#define SAFE_RELEASE(p) { if (p!=NULL) { (p)->Release(); (p)=NULL; } }


#define PRINT_ASSERT_MSG(exp, ...)

#ifdef _DEBUG

#ifdef _MSC_VER
Expand All @@ -224,26 +226,32 @@
#include <cstdlib>
#include <crtdbg.h>
#ifdef _INC_CRTDBG
#define ASSERT(exp, ...) {if (!(exp) && 1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, #exp)) _CrtDbgBreak();}
#define SIMPLE_ASSERT(exp) {if (!(exp) && 1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, #exp)) _CrtDbgBreak();}
#define ASSERT(exp, ...) {static bool bIgnore(false); if (!bIgnore && !(exp)) {PRINT_ASSERT_MSG(exp, __VA_ARGS__); if (!(bIgnore = !(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, #exp)))) _CrtDbgBreak();}}
#else
#define ASSERT(exp, ...) {if (!(exp)) __debugbreak();}
#define SIMPLE_ASSERT(exp) {if (!(exp)) __debugbreak();}
#define ASSERT(exp, ...) {if (!(exp)) {PRINT_ASSERT_MSG(exp, __VA_ARGS__); __debugbreak();}}
#endif // _INC_CRTDBG
#define TRACE(...) {TCHAR buffer[2048]; _sntprintf(buffer, 2048, __VA_ARGS__); OutputDebugString(buffer);}
#else // _MSC_VER
#include <assert.h>
#define ASSERT(exp, ...) assert(exp)
#define SIMPLE_ASSERT(exp) {if (!(exp)) assert(exp);}
#define ASSERT(exp, ...) {if (!(exp)) {PRINT_ASSERT_MSG(exp, __VA_ARGS__); assert(exp);}}
#define TRACE(...)
#endif // _MSC_VER

#else

#ifdef _RELEASE
#define SIMPLE_ASSERT(exp)
#define ASSERT(exp, ...)
#else
#ifdef _MSC_VER
#define ASSERT(exp, ...) {if (!(exp)) __debugbreak();}
#define SIMPLE_ASSERT(exp) {if (!(exp)) __debugbreak();}
#define ASSERT(exp, ...) {if (!(exp)) {PRINT_ASSERT_MSG(exp, __VA_ARGS__); __debugbreak();}}
#else // _MSC_VER
#define ASSERT(exp, ...) {if (!(exp)) __builtin_trap();}
#define SIMPLE_ASSERT(exp) {if (!(exp)) __builtin_trap();}
#define ASSERT(exp, ...) {if (!(exp)) {PRINT_ASSERT_MSG(exp, __VA_ARGS__); __builtin_trap();}}
#endif // _MSC_VER
#endif
#define TRACE(...)
Expand Down
12 changes: 6 additions & 6 deletions libs/Common/Rotation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1176,8 +1176,8 @@ void TRMatrixBase<TYPE>::SetFromHV(const Vec& xxx, const Vec& yyy)
template <typename TYPE>
TRMatrixBase<TYPE>& TRMatrixBase<TYPE>::SetFromDir2Dir(const Vec& dir0, const Vec& dir1)
{
ASSERT(ISEQUAL(norm(dir0), TYPE(1)));
ASSERT(ISEQUAL(norm(dir1), TYPE(1)));
ASSERT(ISEQUAL(norm(dir0), TYPE(1)), "Norm = ", norm(dir0));
ASSERT(ISEQUAL(norm(dir1), TYPE(1)), "Norm = ", norm(dir1));
const TYPE cos01(CLAMP(dir1.dot(dir0), TYPE(-1), TYPE(1)));
const TYPE sin01Sq(TYPE(1) - SQUARE(cos01));
if (sin01Sq > EPSILONTOLERANCE<TYPE>()) {
Expand All @@ -1203,8 +1203,8 @@ TRMatrixBase<TYPE>& TRMatrixBase<TYPE>::SetFromDir2Dir(const Vec& dir0, const Ve
template <typename TYPE>
void TRMatrixBase<TYPE>::SetFromDirUpGL(const Vec& viewDir, const Vec& viewUp)
{
ASSERT(ISEQUAL(norm(viewDir), TYPE(1)));
ASSERT(ISEQUAL(norm(viewUp), TYPE(1)));
ASSERT(ISEQUAL(norm(viewDir), TYPE(1)), "Norm = ", norm(viewDir));
ASSERT(ISEQUAL(norm(viewUp), TYPE(1)), "Norm = ", norm(viewUp));
const Vec right(normalized(cross(viewDir, viewUp)));
const Vec up(normalized(cross(right, viewDir)));
const Vec forward(viewDir * TYPE(-1)); // convert to right handed system
Expand All @@ -1214,8 +1214,8 @@ void TRMatrixBase<TYPE>::SetFromDirUpGL(const Vec& viewDir, const Vec& viewUp)
template <typename TYPE>
void TRMatrixBase<TYPE>::SetFromDirUp(const Vec& viewDir, const Vec& viewUp)
{
ASSERT(ISEQUAL(norm(viewDir), TYPE(1)));
ASSERT(ISEQUAL(norm(viewUp), TYPE(1)));
ASSERT(ISEQUAL(norm(viewDir), TYPE(1)), "Norm = ", norm(viewDir));
ASSERT(ISEQUAL(norm(viewUp), TYPE(1)), "Norm = ", norm(viewUp));
const Vec right(normalized(cross(viewDir, viewUp)));
const Vec up(normalized(cross(viewDir, right)));
const Vec& forward(viewDir);
Expand Down
23 changes: 21 additions & 2 deletions libs/Common/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,25 @@ typedef TAliasCast<double,int32_t> CastD2I;
#endif


// functions simplifying the task of printing messages
namespace SEACAVE {
// print the given message composed of any number of arguments to the given stream
template<typename... Args>
std::ostringstream& PrintMessageToStream(std::ostringstream& oss, Args&&... args) {
// fold expression to insert all arguments into the stream
(oss << ... << args);
return oss;
}
// print the given message composed of any number of arguments to a string
template<typename... Args>
std::string PrintMessageToString(Args&&... args) {
std::ostringstream oss;
(oss << ... << args);
return oss.str();
}
} // namespace SEACAVE


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

#include "Strings.h"
Expand Down Expand Up @@ -681,13 +700,13 @@ constexpr T factorial(T n) {
}
template<typename T>
constexpr T combinations(const T& n, const T& k) {
ASSERT(n >= k);
SIMPLE_ASSERT(n >= k);
#if 1
T num = n;
const T den = factorial(k);
for (T i=n-k+1; i<n; ++i)
num *= i;
ASSERT(num%den == 0);
SIMPLE_ASSERT(num%den == 0);
return num/den;
#else
return factorial(n) / (factorial(k)*factorial(n-k));
Expand Down
4 changes: 2 additions & 2 deletions libs/Common/Util.inl
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ inline TPoint3<TYPE> PerspectiveCorrectBarycentricCoordinates(const TPoint3<TYPE
// Encodes/decodes a normalized 3D vector in two parameters for the direction
template<typename T, typename TR>
inline void Normal2Dir(const TPoint3<T>& d, TPoint2<TR>& p) {
ASSERT(ISEQUAL(norm(d), T(1)));
ASSERT(ISEQUAL(norm(d), T(1)), "Norm = ", norm(d));
p.x = TR(atan2(d.y, d.x));
p.y = TR(acos(d.z));
}
Expand All @@ -762,7 +762,7 @@ inline void Dir2Normal(const TPoint2<T>& p, TPoint3<TR>& d) {
d.x = TR(cos(p.x)*siny);
d.y = TR(sin(p.x)*siny);
d.z = TR(cos(p.y));
ASSERT(ISEQUAL(norm(d), TR(1)));
ASSERT(ISEQUAL(norm(d), TR(1)), "Norm = ", norm(d));
}
// Encodes/decodes a 3D vector in two parameters for the direction and one parameter for the scale
template<typename T, typename TR>
Expand Down
157 changes: 42 additions & 115 deletions libs/MVS/DepthMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,130 +663,57 @@ void DepthEstimator::ProcessPixel(IDX idx)
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
neighborsClose.Empty();
#endif
if (dir == LT2RB) {
// direction from left-top to right-bottom corner
if (x0.x > nSizeHalfWindow) {
const ImageRef nx(x0.x-1, x0.y);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighbors.emplace_back(nx);
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
#else
neighbors.emplace_back(NeighborData{nx,ndepth,normalMap0(nx)});
const auto AddDirectionNeighbor = [this] (const ImageRef& nx) {
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f), "Norm = ", norm(normalMap0(nx)));
neighbors.emplace_back(nx);
neighborsClose.emplace_back(NeighborEstimate{ndepth, normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
}
});
#else
neighbors.emplace_back(NeighborData{nx,ndepth,normalMap0(nx)});
#endif
}
if (x0.y > nSizeHalfWindow) {
const ImageRef nx(x0.x, x0.y-1);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighbors.emplace_back(nx);
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
#else
neighbors.emplace_back(NeighborData{nx,ndepth,normalMap0(nx)});
};
const auto AddDirection = [this] (const ImageRef& nx) {
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f), "Norm = ", norm(normalMap0(nx)));
neighborsClose.emplace_back(NeighborEstimate{ndepth, normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
}
}
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
if (x0.x < size.width-nSizeHalfWindow) {
const ImageRef nx(x0.x+1, x0.y);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
}
}
if (x0.y < size.height-nSizeHalfWindow) {
const ImageRef nx(x0.x, x0.y+1);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
}
}
};
if (dir == LT2RB) {
// direction from left-top to right-bottom corner
if (x0.x > nSizeHalfWindow)
AddDirectionNeighbor(ImageRef(x0.x-1, x0.y));
if (x0.y > nSizeHalfWindow)
AddDirectionNeighbor(ImageRef(x0.x, x0.y-1));
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
if (x0.x < size.width-nSizeHalfWindow)
AddDirection(ImageRef(x0.x+1, x0.y));
if (x0.y < size.height-nSizeHalfWindow)
AddDirection(ImageRef(x0.x, x0.y+1));
#endif
} else {
ASSERT(dir == RB2LT);
// direction from right-bottom to left-top corner
if (x0.x < size.width-nSizeHalfWindow) {
const ImageRef nx(x0.x+1, x0.y);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighbors.emplace_back(nx);
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
#else
neighbors.emplace_back(NeighborData{nx,ndepth,normalMap0(nx)});
#endif
}
}
if (x0.y < size.height-nSizeHalfWindow) {
const ImageRef nx(x0.x, x0.y+1);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighbors.emplace_back(nx);
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
#else
neighbors.emplace_back(NeighborData{nx,ndepth,normalMap0(nx)});
#endif
}
}
if (x0.x < size.width-nSizeHalfWindow)
AddDirectionNeighbor(ImageRef(x0.x+1, x0.y));
if (x0.y < size.height-nSizeHalfWindow)
AddDirectionNeighbor(ImageRef(x0.x, x0.y+1));
#if DENSE_SMOOTHNESS != DENSE_SMOOTHNESS_NA
if (x0.x > nSizeHalfWindow) {
const ImageRef nx(x0.x-1, x0.y);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
}
}
if (x0.y > nSizeHalfWindow) {
const ImageRef nx(x0.x, x0.y-1);
const Depth ndepth(depthMap0(nx));
if (ndepth > 0) {
ASSERT(ISEQUAL(norm(normalMap0(nx)), 1.f));
neighborsClose.emplace_back(NeighborEstimate{ndepth,normalMap0(nx)
#if DENSE_SMOOTHNESS == DENSE_SMOOTHNESS_PLANE
, Cast<float>(image0.camera.TransformPointI2C(Point3(nx, ndepth)))
#endif
});
}
}
if (x0.x > nSizeHalfWindow)
AddDirection(ImageRef(x0.x-1, x0.y));
if (x0.y > nSizeHalfWindow)
AddDirection(ImageRef(x0.x, x0.y-1));
#endif
}
float& conf = confMap0(x0);
Expand Down Expand Up @@ -1027,7 +954,7 @@ DepthEstimator::PixelEstimate DepthEstimator::PerturbEstimate(const PixelEstimat
}
perturbation *= 0.5f;
}
ASSERT(ISEQUAL(norm(ptbEst.normal), 1.f));
ASSERT(ISEQUAL(norm(ptbEst.normal), 1.f), "Norm = ", norm(ptbEst.normal));

return ptbEst;
}
Expand Down
4 changes: 2 additions & 2 deletions libs/MVS/DepthMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ struct MVS_API DepthEstimator {
inline Normal RandomNormal(const Point3f& viewRay) {
Normal normal;
Dir2Normal(Point2f(rnd.randomRange(FD2R(0.f),FD2R(180.f)), rnd.randomRange(FD2R(90.f),FD2R(180.f))), normal);
ASSERT(ISEQUAL(norm(normal), 1.f));
ASSERT(ISEQUAL(norm(normal), 1.f), "Norm = ", norm(normal));
return normal.dot(viewRay) > 0 ? -normal : normal;
}

Expand All @@ -463,7 +463,7 @@ struct MVS_API DepthEstimator {
const float cosAngLen(normal.dot(viewDir));
if (cosAngLen >= 0)
normal = RMatrixBaseF(normal.cross(viewDir), MINF((ACOS(cosAngLen/norm(viewDir))-FD2R(90.f))*1.01f, -0.001f)) * normal;
ASSERT(ISEQUAL(norm(normal), 1.f));
ASSERT(ISEQUAL(norm(normal), 1.f), "Norm = ", norm(normal));
}

static bool ImportIgnoreMask(const Image&, const Image8U::Size&, uint16_t nIgnoreMaskLabel, BitMatrix&, Image8U* =NULL);
Expand Down
2 changes: 1 addition & 1 deletion libs/MVS/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ size_t Scene::DrawCircle(PointCloud& pc, PointCloud::PointArr& outCircle, const
const float fAngle(fStartAngle + fAngleBetweenPoints * pIdx);
ASSERT(fAngle <= FTWO_PI);
const Normal n(cos(fAngle), sin(fAngle), 0);
ASSERT(ISEQUAL(norm(n), 1.f));
ASSERT(ISEQUAL(norm(n), 1.f), "Norm = ", norm(n));
const Point3f newPoint(circleCenter + circleRadius * n);
// select cameras seeing this point
PointCloud::ViewArr views;
Expand Down
Loading

0 comments on commit 208a441

Please sign in to comment.