Browse Source

mathutil: add some more assertion checks to PerlinNoise2

rdb 6 years ago
parent
commit
c1c74e2cd3
1 changed files with 7 additions and 0 deletions
  1. 7 0
      panda/src/mathutil/perlinNoise2.cxx

+ 7 - 0
panda/src/mathutil/perlinNoise2.cxx

@@ -19,6 +19,9 @@
  */
 double PerlinNoise2::
 noise(const LVecBase2d &value) const {
+  // If this triggers, you passed in 0 for table_size.
+  nassertr(!_index.empty(), make_nan(0.0));
+
   // Convert the vector to our local coordinate space.
   LVecBase2d vec = _input_xform.xform_point(value);
 
@@ -41,9 +44,13 @@ noise(const LVecBase2d &value) const {
   double v = fade(y);
 
   // Hash coordinates of the 4 square corners (A, B, A + 1, and B + 1)
+  nassertr(X >= 0 && X + 1 < _index.size(), make_nan(0.0));
   int A = _index[X] + Y;
   int B = _index[X + 1] + Y;
 
+  nassertr(A >= 0 && A + 1 < _index.size(), make_nan(0.0));
+  nassertr(B >= 0 && B + 1 < _index.size(), make_nan(0.0));
+
   // and add blended results from 4 corners of square.
   double result =
     lerp(v, lerp(u, grad(_index[A], x, y),