Skip to Content.
Sympa Menu

cgal-discuss - Re: [cgal-discuss] Problem in example mesh_a_3d_gray_image.cpp

Subject: CGAL users discussion list

List archive

Re: [cgal-discuss] Problem in example mesh_a_3d_gray_image.cpp


Chronological Thread 
  • From: Laurent Rineau <>
  • To:
  • Subject: Re: [cgal-discuss] Problem in example mesh_a_3d_gray_image.cpp
  • Date: Tue, 21 Aug 2007 15:16:35 +0200
  • Organization: Inria, Sophia Antipolis, FRANCE

On Tuesday 21 August 2007 14:57:47

wrote:
> Hi every one,
>
> I used the example examples/Surface_mesher/mesh_a_3d_gray_image.cpp
> with no problem as a command line.
> I have done an interface using MS-VC++ and it seems that it is not
> possible to use the main function two times. I converted this function
> into a function taking as input every parameters (an 3D image, a
> sphere origin coordinate, a sphere radius, the 3 parameters of the
> mesh quality, and the threshold). When I use my interface the first
> time, it works but if I call again this function, it doesn't work
> anymore. The variable "image" doesn't seem to be well instantiate.

Well, that may be a problem that I already know. The class
Gray_level_image_3<FT, Point> is not correct, as regards the memory
management. I have a patch in hand, but I had not have enough time to test
it. Can you please apply the attached patch to you CGAL installation
directory, and test if it fixes your problem without impacting the
performance? The patch makes use of <boost/shared_ptr.hpp>.

--
Laurent Rineau
INRIA - Sophia Antipolis
BP 93, 2004 Route des Lucioles
06902 Sophia Antipolis Cedex FRANCE
Tel: +33 4 92 38 78 62
--- include/CGAL/Gray_level_image_3.h	2007-05-31 21:01:34.000000000 +0200
+++ include/CGAL/Gray_level_image_3.h	2007-08-21 15:10:09.000000000 +0200
@@ -21,6 +21,8 @@
 
 #include <CGAL/basic.h>
 
+#include <boost/shared_ptr.hpp>
+
 #ifdef CGAL_SURFACE_MESHER_DEBUG_GRAY_LEVEL_IMAGE_3_CONSTRUCTOR
 #include <boost/format.hpp>
 #endif
@@ -44,7 +46,18 @@
   template <typename FT, typename Point>
 class Gray_level_image_3
 {
-  _image *image;
+  struct Image_deleter {
+    void operator()(_image* image)
+    {
+#ifdef CGAL_SURFACE_MESHER_DEBUG_GRAY_LEVEL_IMAGE_3_CONSTRUCTOR
+      std::cerr << ::boost::format("Deletion of image %1%.\n") % image;
+      ::_freeImage(image);
+#endif
+    }
+  };
+  typedef boost::shared_ptr<_image> Image_shared_ptr;
+  Image_shared_ptr image_ptr;
+
   float isovalue;
   float min_x, min_y, min_z;
   float max_x, max_y, max_z;
@@ -52,8 +65,7 @@
 
 public:
   Gray_level_image_3(const char* file, float isoval)
-    : image(0),
-      isovalue(isoval),
+    : isovalue(isoval),
       min_x(0.f),
       min_y(0.f),
       min_z(0.f),
@@ -66,28 +78,21 @@
     std::cerr << 
       ::boost::format("Constructing a Gray_level_image_3(\"%1%\")... ") % file;
 #endif
-    image = ::_readImage(file);
-    if( image != 0 )
+    image_ptr = Image_shared_ptr(::_readImage(file), Image_deleter());
+    if( image_ptr.get() != 0 )
     {
 #ifdef CGAL_SURFACE_MESHER_DEBUG_GRAY_LEVEL_IMAGE_3_CONSTRUCTOR
-      std::cerr << ::boost::format(" = %1%\n") % image;
+      std::cerr << ::boost::format(" = %1%\n") % image_ptr.get();
 #endif
       is_valid = true;
-      ::convertImageTypeToFloat(image);
+      ::convertImageTypeToFloat(image_ptr.get());
       isovalue=isoval;
-      ::_get_image_bounding_box(image,
+      ::_get_image_bounding_box(image_ptr.get(),
 				&min_x, &min_y, &min_z,
 				&max_x, &max_y, &max_z);
     }
   }
 
-  // BUG
-  /** @FIXME This destructor should call ::freeImage(), but objects of type
-      Gray_level_image_3 are copied a lot of time during the execution of
-      the Surface_mesher algorithm. image should perhaps be a smart
-      pointer. We need to bench if the handling of smart pointer has a
-      cost.
-   */
   ~Gray_level_image_3()
   {
 #ifdef CGAL_SURFACE_MESHER_DEBUG_GRAY_LEVEL_IMAGE_3_CONSTRUCTOR
@@ -115,7 +120,7 @@
     if (!inside(X,Y,Z))
       return FT(1);
     else{
-      float value = ::triLinInterp(image, X, Y, Z); 
+      float value = ::triLinInterp(image_ptr.get(), X, Y, Z); 
 
       if (value > isovalue) // inside
 	return FT(-1);



Archive powered by MHonArc 2.6.16.

Top of Page