Skip to content

Commit 228ebcc

Browse files
committed
check & warn for inf/nan values in input
1 parent fd31e98 commit 228ebcc

18 files changed

+168
-4
lines changed

include/polyscope/camera_parameters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class CameraParameters {
8787
// create/test 'invalid' params
8888
static CameraParameters createInvalid();
8989
bool isValid() const;
90+
bool isfinite() const;
9091

9192
// The intrinsic & extrinsics parameters that define the camera
9293
CameraIntrinsics intrinsics;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
2+
3+
#pragma once
4+
5+
#include <polyscope/numeric_helpers.h>
6+
#include <polyscope/options.h>
7+
8+
#include <cmath>
9+
#include <glm/glm.hpp>
10+
#include <glm/gtx/compatibility.hpp>
11+
#include <type_traits>
12+
13+
namespace polyscope {
14+
15+
template <typename T>
16+
void checkInvalidValues(std::string name, const std::vector<T>& data) {
17+
if (options::warnForInvalidValues) {
18+
for (const T& val : data) {
19+
if (!allComponentsFinite(val)) {
20+
warning("Invalid +-inf or NaN values detected.\n(set warnForInvalidValues=false to disable this warning)",
21+
"In buffer: " + name);
22+
break;
23+
}
24+
}
25+
}
26+
}
27+
28+
} // namespace polyscope

include/polyscope/color_quantity.ipp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace polyscope {
44

55
template <typename QuantityT>
66
ColorQuantity<QuantityT>::ColorQuantity(QuantityT& quantity_, const std::vector<glm::vec3>& colors_)
7-
: quantity(quantity_), colors(&quantity, quantity.uniquePrefix() + "colors", colorsData), colorsData(colors_) {}
7+
: quantity(quantity_), colors(&quantity, quantity.uniquePrefix() + "colors", colorsData), colorsData(colors_) {
8+
colors.checkInvalidValues();
9+
}
810

911
template <typename QuantityT>
1012
void ColorQuantity<QuantityT>::buildColorUI() {}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
2+
3+
#pragma once
4+
5+
#include <cmath>
6+
#include <glm/glm.hpp>
7+
#include <glm/gtx/compatibility.hpp>
8+
#include <type_traits>
9+
10+
namespace polyscope {
11+
12+
13+
// Base case: call the scalar version
14+
template <typename T>
15+
bool allComponentsFinite(const T& x) {
16+
return std::isfinite(x);
17+
}
18+
19+
template <>
20+
inline bool allComponentsFinite<glm::vec2>(const glm::vec2& x) {
21+
return glm::all(glm::isfinite(x));
22+
}
23+
template <>
24+
inline bool allComponentsFinite<glm::vec3>(const glm::vec3& x) {
25+
return glm::all(glm::isfinite(x));
26+
}
27+
template <>
28+
inline bool allComponentsFinite<glm::vec4>(const glm::vec4& x) {
29+
return glm::all(glm::isfinite(x));
30+
}
31+
32+
template <>
33+
inline bool allComponentsFinite<glm::uvec2>(const glm::uvec2& x) {
34+
return true;
35+
}
36+
template <>
37+
inline bool allComponentsFinite<glm::uvec3>(const glm::uvec3& x) {
38+
return true;
39+
}
40+
template <>
41+
inline bool allComponentsFinite<glm::uvec4>(const glm::uvec4& x) {
42+
return true;
43+
}
44+
45+
template <>
46+
inline bool allComponentsFinite<glm::mat2x2>(const glm::mat2x2& x) {
47+
for (size_t i = 0; i < 2; i++)
48+
if (!allComponentsFinite(glm::row(x, i))) return false;
49+
return true;
50+
}
51+
template <>
52+
inline bool allComponentsFinite<glm::mat3x3>(const glm::mat3x3& x) {
53+
for (size_t i = 0; i < 3; i++)
54+
if (!allComponentsFinite(glm::row(x, i))) return false;
55+
return true;
56+
}
57+
template <>
58+
inline bool allComponentsFinite<glm::mat4x4>(const glm::mat4x4& x) {
59+
for (size_t i = 0; i < 4; i++)
60+
if (!allComponentsFinite(glm::row(x, i))) return false;
61+
return true;
62+
}
63+
64+
template <>
65+
inline bool allComponentsFinite<std::array<glm::vec3, 2>>(const std::array<glm::vec3, 2>& x) {
66+
for (size_t i = 0; i < x.size(); i++)
67+
if (!glm::all(glm::isfinite(x[i]))) return false;
68+
return true;
69+
}
70+
template <>
71+
inline bool allComponentsFinite<std::array<glm::vec3, 3>>(const std::array<glm::vec3, 3>& x) {
72+
for (size_t i = 0; i < x.size(); i++)
73+
if (!glm::all(glm::isfinite(x[i]))) return false;
74+
return true;
75+
}
76+
template <>
77+
inline bool allComponentsFinite<std::array<glm::vec3, 4>>(const std::array<glm::vec3, 4>& x) {
78+
for (size_t i = 0; i < x.size(); i++)
79+
if (!glm::all(glm::isfinite(x[i]))) return false;
80+
return true;
81+
}
82+
83+
84+
} // namespace polyscope

include/polyscope/options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ extern bool giveFocusOnShow;
6666
// If true, hide the polyscope window when a show() command finishes (default: true)
6767
extern bool hideWindowAfterShow;
6868

69+
// Give warnings for inf/nan values
70+
extern bool warnForInvalidValues;
71+
6972
// === Scene options
7073

7174
// Behavior of the ground plane

include/polyscope/parameterization_quantity.ipp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ ParameterizationQuantity<QuantityT>::ParameterizationQuantity(QuantityT& quantit
5353
gridBackgroundColor(quantity.uniquePrefix() + "#gridBackgroundColor", render::RGB_PINK),
5454
altDarkness(quantity.uniquePrefix() + "#altDarkness", 0.5), cMap(quantity.uniquePrefix() + "#cMap", "phase")
5555
56-
57-
{}
56+
{
57+
coords.checkInvalidValues();
58+
}
5859
5960
6061
template <typename QuantityT>

include/polyscope/render/managed_buffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class ManagedBuffer : public virtual WeakReferrable {
8989
bool dataGetsComputed; // if true, the value gets computed on-demand by calling computeFunc()
9090
std::function<void()> computeFunc; // (optional) callback which populates the `data` buffer
9191

92+
// sanity check helper
93+
void checkInvalidValues();
9294

9395
// mark as texture, set size
9496
void setTextureSize(uint32_t sizeX);

include/polyscope/scalar_quantity.ipp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
22

33
#include "imgui.h"
4+
#include "polyscope/check_invalid_values.h"
45
#include "polyscope/utilities.h"
6+
57
namespace polyscope {
68

79
template <typename QuantityT>
@@ -17,6 +19,7 @@ ScalarQuantity<QuantityT>::ScalarQuantity(QuantityT& quantity_, const std::vecto
1719
isolineDarkness(quantity.uniquePrefix() + "isolineDarkness", 0.7)
1820

1921
{
22+
values.checkInvalidValues();
2023
hist.updateColormap(cMap.get());
2124
hist.buildHistogram(values.data);
2225

include/polyscope/vector_quantity.ipp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
22

3-
43
#include "polyscope/standardize_data_array.h"
54

65
namespace polyscope {
@@ -125,6 +124,7 @@ VectorQuantity<QuantityT>::VectorQuantity(QuantityT& quantity_, const std::vecto
125124
: VectorQuantityBase<QuantityT>(quantity_, vectorType_),
126125
vectors(&quantity_, quantity_.uniquePrefix() + "#values", vectorsData), vectorRoots(vectorRoots_),
127126
vectorsData(vectors_) {
127+
vectors.checkInvalidValues();
128128
this->updateMaxLength();
129129
}
130130
@@ -236,6 +236,9 @@ TangentVectorQuantity<QuantityT>::TangentVectorQuantity(QuantityT& quantity_,
236236
tangentBasisY(&quantity_, quantity_.uniquePrefix() + "#basisY", tangentBasisYData), vectorRoots(vectorRoots_),
237237
tangentVectorsData(tangentVectors_), tangentBasisXData(tangentBasisX_), tangentBasisYData(tangentBasisY_),
238238
nSym(nSym_) {
239+
tangentVectors.checkInvalidValues();
240+
tangentBasisX.checkInvalidValues();
241+
tangentBasisY.checkInvalidValues();
239242
this->updateMaxLength();
240243
}
241244

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ SET(HEADERS
266266
${INCLUDE_ROOT}/camera_parameters.ipp
267267
${INCLUDE_ROOT}/camera_view.h
268268
${INCLUDE_ROOT}/camera_view.ipp
269+
${INCLUDE_ROOT}/check_invalid_values.h
269270
${INCLUDE_ROOT}/color_management.h
270271
${INCLUDE_ROOT}/color_image_quantity.h
271272
${INCLUDE_ROOT}/color_render_image_quantity.h
@@ -293,6 +294,7 @@ SET(HEADERS
293294
${INCLUDE_ROOT}/implicit_helpers.h
294295
${INCLUDE_ROOT}/implicit_helpers.ipp
295296
${INCLUDE_ROOT}/messages.h
297+
${INCLUDE_ROOT}/numeric_helpers.h
296298
${INCLUDE_ROOT}/options.h
297299
${INCLUDE_ROOT}/parameterization_quantity.h
298300
${INCLUDE_ROOT}/parameterization_quantity.ipp

0 commit comments

Comments
 (0)