Subject: CGAL users discussion list
List archive
- From: Bryn Lloyd <>
- To:
- Subject: [cgal-discuss] Integer overflow for large images:
- Date: Tue, 14 Oct 2014 09:38:55 +0200
Dear CGAL developers
I would like to submit a patch for a problem which occurs when generating a 3D mesh from very large images. The detailed information is below:
OS, compiler:
- Windows 7 & 8, Visual Studio 2012
Build options:
- Configured using cmake for VS 2012, without GMP, without ZLIB
Description of the problem:
- Generating meshes from very large image (approx. 4e9 voxels) was not working as expected. Sometimes one half of the image was meshed correctly, but strange things were happening to the other half.
Kernel (3D Mesh Generation from images/label fields):
- I was using an old version of CGAL (3.8), however, looking at the newest version I can see that issue has not been fixed.
Solution:
- After debugging a bit, I found that the function 'Image_3::labellized_trilinear_interpolation' was using 'unsigned int' instead of e.g. 'std::uint64_t' or size_t. This causes an integer overflow, which breaks mesh generation.
Changing the code in Image_3.h and Image_to_labeled_function_wrapper.h to use size_t fixed the issue.
I attached a patch.
Best regards
Bryn
From d538c5631acbe1f3e92ea2a730c25584ba64940e Mon Sep 17 00:00:00 2001
From: Bryn Lloyd
<>
Date: Tue, 26 Aug 2014 10:38:39 +0200
Subject: [PATCH] fix: replace unsigned int by size_t for dimensions/shape
index to scalars pointer
---
cgal-3.8/include/CGAL/Image_3.h | 25
++++++++++++----------
.../Mesh_3/Image_to_labeled_function_wrapper.h | 6 +++---
2 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/cgal-3.8/include/CGAL/Image_3.h b/cgal-3.8/include/CGAL/Image_3.h
index 82179e1..ce9a02f 100644
--- a/cgal-3.8/include/CGAL/Image_3.h
+++ b/cgal-3.8/include/CGAL/Image_3.h
@@ -33,6 +33,7 @@
#include <limits>
#include <set>
+#include <cstdint>
class vtkImageData;
@@ -119,11 +120,13 @@ public:
image()->data = d;
}
- unsigned int xdim() const { return image_ptr->xdim; }
- unsigned int ydim() const { return image_ptr->ydim; }
- unsigned int zdim() const { return image_ptr->zdim; }
+ size_t xdim() const { return static_cast<size_t>( image_ptr->xdim ); }
+ size_t ydim() const { return static_cast<size_t>( image_ptr->ydim ); }
+ size_t zdim() const { return static_cast<size_t>( image_ptr->zdim ); }
- unsigned int size() const { return xdim() * ydim() * zdim(); }
+ size_t size() const {
+ return xdim() * ydim() * zdim();
+ }
double vx() const { return image_ptr->vx; }
double vy() const { return image_ptr->vy; }
@@ -232,10 +235,10 @@ Image_3::trilinear_interpolation(const Coord_type& x,
const Coord_type lx = x / image()->vx;
const Coord_type ly = y / image()->vy;
const Coord_type lz = z / image()->vz;
- const int dimx = xdim();
- const int dimy = ydim();
- const int dimz = zdim();
- const int dimxy = dimx*dimy;
+ const size_t dimx = xdim();
+ const size_t dimy = ydim();
+ const size_t dimz = zdim();
+ const size_t dimxy = dimx*dimy;
// images are indexed by (z,y,x)
const int i1 = (int)(lz);
@@ -417,9 +420,9 @@ Image_3::labellized_trilinear_interpolation(const
Coord_type& x,
// Check on double/float coordinates, because (int)-0.1 gives 0
if ( x < 0 || y < 0 || z < 0 ) return value_outside;
- const int dimx = xdim();
- const int dimy = ydim();
- const int dimz = zdim();
+ const size_t dimx = xdim();
+ const size_t dimy = ydim();
+ const size_t dimz = zdim();
// images are indexed by (z,y,x)
const int i1 = (int)(z / image()->vz);
diff --git a/cgal-3.8/include/CGAL/Mesh_3/Image_to_labeled_function_wrapper.h
b/cgal-3.8/include/CGAL/Mesh_3/Image_to_labeled_function_wrapper.h
index 2c83cd5..05aa6bc 100644
--- a/cgal-3.8/include/CGAL/Mesh_3/Image_to_labeled_function_wrapper.h
+++ b/cgal-3.8/include/CGAL/Mesh_3/Image_to_labeled_function_wrapper.h
@@ -85,9 +85,9 @@ public:
const int py = static_cast<int>(p.y()/r_im_.vy());
const int pz = static_cast<int>(p.z()/r_im_.vz());
- const int dimx = r_im_.xdim();
- const int dimy = r_im_.ydim();
- const int dimz = r_im_.zdim();
+ const size_t dimx = r_im_.xdim();
+ const size_t dimy = r_im_.ydim();
+ const size_t dimz = r_im_.zdim();
if(px < 0 ||
py < 0 ||
--
1.9.2.msysgit.0
- [cgal-discuss] Integer overflow for large images:, Bryn Lloyd, 10/14/2014
- Re: [cgal-discuss] Integer overflow for large images:, Jane Tournois, 10/17/2014
- Re: [cgal-discuss] Integer overflow for large images:, Bryn Lloyd, 10/20/2014
- Re: [cgal-discuss] Integer overflow for large images:, Jane Tournois, 10/17/2014
Archive powered by MHonArc 2.6.18.