]> Creatis software - crea.git/commitdiff
Message manager can now be observed (to get messages) with a boost::signal/slot mecha...
authorguigues <guigues>
Thu, 26 Feb 2009 12:04:10 +0000 (12:04 +0000)
committerguigues <guigues>
Thu, 26 Feb 2009 12:04:10 +0000 (12:04 +0000)
src/CMakeLists.txt
src/creaMessageManager.cxx
src/creaMessageManager.h
src/creaVtk.h
src/creaVtk.txx

index b1f7cdbe6627585dcdf26b118a2d9e2ca1abdbf1..f077378a72bb6538356f692a5f4b0ecdfe55753b 100644 (file)
@@ -50,6 +50,7 @@ ADD_LIBRARY(crea ${CREA_BUILD_SHARED} ${SRCS})
 TARGET_LINK_LIBRARIES(crea
   ${WXWIDGETS_LIBRARIES}
   ${VTK_LIBRARIES}
+  ${BOOST_LIBRARIES}
 )
 #  ${GDCM_LIBRARIES}
 #  ${SQLITE_LIBRARIES})
index dc187102932b1970f8dc98f49e87a6630af14538..c880e9c8dd29711d43dcfb769f60d9ad033fa4eb 100644 (file)
@@ -3,8 +3,8 @@
   Program:   crea
   Module:    $RCSfile: creaMessageManager.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/09/26 14:09:55 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2009/02/26 12:04:10 $
+  Version:   $Revision: 1.2 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 namespace crea 
 {
 
+  //===========================================================================
   MessageManager::MessageManager() 
-    : mMaxMessageLength(8)//, mAllLevel(0), mMaxLevel(9)
-    
+    : mMaxMessageLength(8),
+      mSendToCout(true)
   {
     std::string key;
+
     key ="all";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Minimum level for all kind of messages";
+    mMessageMap[key] = new MessageType(0,"Minimum level for all kind of messages");
     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
+
+    key ="max";
+    mMessageMap[key] = new MessageType(9,"Maximum level for all kind of messages");
+    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
+
+    key ="info";
+    mMessageMap[key] = new MessageType(1,"Information messages");
+    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
+
+    key ="warning";
+    mMessageMap[key] = new MessageType(1,"Warning messages");
+    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
+
+   /*
     key ="max";
     mMessageLevel[key] = 9;
     mMessageHelp[key] = "Maximum level for all kind of messages";
@@ -53,107 +68,56 @@ namespace crea
     mMessageLevel[key] = 1;
     mMessageHelp[key] = "Information messages";
     if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    /*
-    key ="Kernel";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Messages generated by the core classes of the lib";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key ="process";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Messages related to box processing";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "Help";
-    mMessageLevel[key] = 1;
-    mMessageHelp[key] = "Help messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "Error";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Error messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "Warning";
-    mMessageLevel[key] = 1;
-    mMessageHelp[key] = "Warning messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "Output";
-    mMessageLevel[key] = 1;
-    mMessageHelp[key] = "Output messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "debug";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Debug messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "Config";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Configuration related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "data";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Data related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "wx";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Widgets related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "gui";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Graphical user interface related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "object";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "object memory related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "package";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Packages related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "connection";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Connections related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
-    key = "modified";
-    mMessageLevel[key] = 0;
-    mMessageHelp[key] = "Modified related messages";
-    if (mMaxMessageLength<key.length()) mMaxMessageLength = key.length();
     */
-   }
 
 
+   }
+  //===========================================================================
 
 
+  //===========================================================================
   MessageManager::~MessageManager() 
   {
     //      std::cout << "~MessageManager"<<std::endl;
+    MessageMapType::iterator i;
+    for (i=mMessageMap.begin(); i!=mMessageMap.end(); ++i)
+      {
+       delete i->second;
+      }
   }
-  
+  //===========================================================================
 
 
+  //===========================================================================
   MessageManager* MessageManager::GetInstance() 
   { 
     static MessageManager* m = 0;
     if (!m) m = new MessageManager();
     return m; 
   }
+  //===========================================================================
 
-  void MessageManager::RegisterMessageType(std::string key, 
-                                                 std::string help,
-                                                 unsigned char default_level) 
+  //===========================================================================
+  void MessageManager::RegisterMessageType(const std::string& key, 
+                                          const std::string& help,
+                                          unsigned char default_level) 
   {
-    GetInstance()->mMessageLevel[key] = default_level;
-    GetInstance()->mMessageHelp[key] = help;
+    GetInstance()->mMessageMap[key] = new MessageType(default_level,help);
+
     if (GetInstance()->mMaxMessageLength<key.length()) 
       GetInstance()->mMaxMessageLength = key.length();
   }
+  //===========================================================================
 
-
-
-  void MessageManager::SetMessageLevel(std::string key, 
-                                             unsigned char level) 
+  //===========================================================================
+  void MessageManager::SetMessageLevel(const std::string& key, 
+                                      unsigned char level) 
   {
-    std::map<std::string,int>::iterator i;
-    i = GetInstance()->mMessageLevel.find(key);
-    if (i!=GetInstance()->mMessageLevel.end()) 
+    MessageMapType::iterator i;
+    i = GetInstance()->mMessageMap.find(key);
+    if (i!=GetInstance()->mMessageMap.end()) 
       {
-       (*i).second = level;
+       (*i).second->Level = level;
       }
     else 
       {
@@ -162,24 +126,48 @@ namespace crea
       }
     
   }
+  //===========================================================================  
   
-  
-  
-  int MessageManager::GetMessageLevel(std::string key) 
+  //===========================================================================
+  int MessageManager::GetMessageLevel(const std::string& key) 
   {
-    int l = GetInstance()->mMessageLevel["all"];
-    std::map<std::string,int>::iterator i = 
-      GetInstance()->mMessageLevel.find(key);
-    if (i!=GetInstance()->mMessageLevel.end()) {
-      if ( (*i).second > l ) l = (*i).second;      
+    int l = GetInstance()->mMessageMap["all"]->Level;
+    MessageMapType::iterator i = GetInstance()->mMessageMap.find(key);
+    if (i!=GetInstance()->mMessageMap.end()) {
+      if ( (*i).second->Level > l ) l = (*i).second->Level;      
     }
-    int m = GetInstance()->mMessageLevel["max"];
+    int m = GetInstance()->mMessageMap["max"]->Level;
     if (l>m) l=m;
     return l;
   }
+  //===========================================================================
 
+  //===========================================================================
+  void MessageManager::SendMessagesToCout(bool v)
+  {
+    GetInstance()->mSendToCout = v;
+  }
+  //===========================================================================
 
+  //===========================================================================
+  void MessageManager::SendMessage(const std::string& key, const std::string& mess)
+  {
+    if (GetInstance()->mSendToCout)
+      {
+       std::cout << mess;
+      }
+    GetInstance()->mMessageMap[key]->Signal(mess);
+  }
+  //===========================================================================
 
+  //===========================================================================
+  void MessageManager::AddMessageObserver( const std::string& key, MessageCallbackType callback )
+  {
+    GetInstance()->mMessageMap[key]->Signal.connect(callback);
+  }
+  //===========================================================================
+  //===========================================================================
   void MessageManager::PrintInfo() 
   {
     creaMessage("info",1,"================ Messages =================" 
@@ -192,23 +180,23 @@ namespace crea
        creaMessageCont("info",1," "); 
       }
     creaMessageCont("info",1,"Level  Nature" << creaendl);
-    std::map<std::string,int>::iterator i;
-    std::map<std::string,std::string>::iterator j;  
-    for (i=GetInstance()->mMessageLevel.begin(),
-          j=GetInstance()->mMessageHelp.begin();
-        i!=GetInstance()->mMessageLevel.end();++i,++j) {
-      creaMessage("info",1, (*i).first);
-      for (int k=0;
-          k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
-          k++) {
-       creaMessageCont("info",1," ");
-      }
-      creaMessageCont("info",1, (*i).second << "\t" 
-                     << (*j).second << creaendl);
+    MessageMapType::iterator i;
+    for (i=GetInstance()->mMessageMap.begin();
+        i!=GetInstance()->mMessageMap.end();
+        ++i) 
+      {
+       creaMessage("info",1, (*i).first);
+       for (int k=0;
+            k<(int)(GetInstance()->mMaxMessageLength+2-(*i).first.length());
+            k++) {
+         creaMessageCont("info",1," ");
+       }
+       creaMessageCont("info",1, (*i).second->Level << "\t" 
+                       << (*i).second->Help << creaendl);
     }
     creaMessage("info",1,"===========================================" 
                << creaendl);
   }
-  
+    //===========================================================================
 
 }
index 690f56d72ab4306458c44faef4fd3fed71e25db1..0fe8110c12bf2a6fe2847a895fd7415a9df852f4 100644 (file)
@@ -91,7 +91,7 @@
 #define creaMessageTab ""
 #endif
 
-#define CREA_PREPEND_MESSAGE_WITH_SPACE
+//#define CREA_PREPEND_MESSAGE_WITH_SPACE
 #ifdef CREA_PREPEND_MESSAGE_WITH_SPACE
 #define creaMessageSpace(value)                        \
   crea::MessageManager::GetSpace(value)
   do {                                                 \
     creaOnMessageLevel(key,value)                      \
       {                                                        \
-       std::cout << creaMessageCode                    \
-                 << creaMessageTab                     \
-                 << creaMessageSpace(value)            \
-                 << MESSAGE;                           \
+       std::ostringstream s;                           \
+       s << creaMessageCode                            \
+         << creaMessageTab                             \
+         << creaMessageSpace(value)                    \
+         << MESSAGE;                                   \
+       crea::MessageManager::SendMessage(key,s.str()); \
       }                                                        \
   }                                                    \
   while (0)
 
 // Macro for continuing a message (when one wants to split the macro
 // call into multiple lines)
-#define creaMessageCont(key,value,MESSAGE)     \
-  do                                           \
-    {                                          \
-      creaOnMessageLevel(key,value)            \
-       {                                       \
-         std::cout << MESSAGE;                 \
-       }                                       \
-    }                                          \
-  while (0)
-
-#define creaMessageInc(key,value,MESSAGE)              \
+#define creaMessageCont(key,value,MESSAGE)             \
   do                                                   \
     {                                                  \
       creaOnMessageLevel(key,value)                    \
        {                                               \
-         std::cout << creaMessageCode                  \
-                   << creaMessageTab                   \
-                   << creaMessageSpace(value)          \
-                   << MESSAGE;                         \
-         crea::MessageManager::IncTab();               \
+       std::ostringstream s;                           \
+       s << MESSAGE;                                   \
+       crea::MessageManager::SendMessage(key,s.str()); \
        }                                               \
     }                                                  \
   while (0)
 
-#define creaMessageDec(key,value,MESSAGE)               \
+#define creaMessageInc(key,value,MESSAGE)              \
   do                                                   \
     {                                                  \
-      creaOnMessageLevel(key,value)                    \
-       {                                               \
-         crea::MessageManager::DecTab();               \
-         std::cout << creaMessageCode                  \
-                   << creaMessageTab                   \
-                   << creaMessageSpace(value)          \
-                   << MESSAGE;                         \
+       std::ostringstream s;                           \
+       s << creaMessageCode                            \
+         << creaMessageTab                             \
+         << creaMessageSpace(value)                    \
+         << MESSAGE;                                   \
+       crea::MessageManager::SendMessage(key,s.str()); \
+       crea::MessageManager::IncTab();                 \
        }                                               \
     }                                                  \
   while (0)
 
+#define creaMessageDec(key,value,MESSAGE)                      \
+  do                                                           \
+    {                                                          \
+      creaOnMessageLevel(key,value)                            \
+       {                                                       \
+         crea::MessageManager::DecTab();                       \
+         std::ostringstream s;                                 \
+         s << creaMessageCode                                  \
+           << creaMessageTab                                   \
+           << creaMessageSpace(value)                          \
+           << MESSAGE;                                         \
+         crea::MessageManager::SendMessage(key,s.str());       \
+       }                                                       \
+    }                                                          \
+  while (0)
+
 #define creaDecTab(key,value)                  \
   do                                           \
     {                                          \
 
 
 //===========================================================
+// Macros for debug messages
 #ifdef CREA_COMPILE_DEBUG_MESSAGES
-
-// Macro for debug messages
-#define creaDebugMessage(key,value,MESSAGE)             \
-  do                                                   \
-    {                                                  \
-      creaOnMessageLevel(key,value)                    \
-       {                                               \
-         std::cout << creaMessageCode                  \
-                   << creaMessageTab                   \
-                   << creaMessageSpace(value)          \
-                   << MESSAGE;                         \
-       }                                               \
-    }                                                  \
-  while (0)
-
-// Macro for continuing a debug message (when one wants to split the
-// macro call into multiple lines)
-#define creaDebugMessageCont(key,value,MESSAGE)        \
-  do                                           \
-    {                                          \
-      creaOnMessageLevel(key,value)            \
-       {                                       \
-         std::cout << MESSAGE;                 \
-       }                                       \
-    }                                          \
-  while (0)
-
-#define creaDebugMessageInc(key,value,MESSAGE)         \
-  do                                                   \
-    {                                                  \
-      creaOnMessageLevel(key,value)                    \
-       {                                               \
-         std::cout << creaMessageCode                  \
-                   << creaMessageTab                   \
-                   << creaMessageSpace(value)          \
-                   << MESSAGE;                         \
-         crea::MessageManager::IncTab();               \
-       }                                               \
-    }                                                  \
-  while (0)
-
-#define creaDebugMessageDec(key,value,MESSAGE)         \
-  do                                                   \
-    {                                                  \
-      creaOnMessageLevel(key,value)                    \
-       {                                               \
-         crea::MessageManager::DecTab();               \
-         std::cout << creaMessageCode                  \
-                   << creaMessageTab                   \
-                   << creaMessageSpace(value)          \
-                   << MESSAGE;                         \
-       }                                               \
-    }                                                  \
-  while (0)
-
-#define creaDebugDecTab(key,value)             \
-  do                                           \
-    {                                          \
-      creaOnMessageLevel(key,value)            \
-       {                                       \
-         crea::MessageManager::DecTab();       \
-       }                                       \
-    }                                          \
-  while (0)
-
-#define creaDebugIncTab(key,value)             \
-    do                                         \
-      {                                                \
-       creaOnMessageLevel(key,value)           \
-         {                                     \
-           crea::MessageManager::IncTab();     \
-         }                                     \
-      }                                                \
-    while (0)
-    
-#define creaDebugResetTab()                    \
-    do                                         \
-      {                                                \
-       crea::MessageManager::ResetTab();       \
-      }                                                \
-    while (0)
-
+#define creaDebugMessage(key,value,MESSAGE) creaMessage(key,value,MESSAGE)
+#define creaDebugMessageCont(key,value,MESSAGE)        creaMessageCont(key,value,MESSAGE)
+#define creaDebugMessageInc(key,value,MESSAGE) creaMessageInc(key,value,MESSAGE)  
+#define creaDebugMessageDec(key,value,MESSAGE) creaMessageDec(key,value,MESSAGE)
+#define creaDebugDecTab(key,value) creaDecTab(key,value)
+#define creaDebugIncTab(key,value) creaIncTab(key,value)
+#define creaDebugResetTab() creaResetTab()
 #else
 #define creaDebugMessage(key,value,MESSAGE) 
 #define creaDebugMessageCont(key,value,MESSAGE) 
 #define creaWarning(MESSAGE)                                           \
   do                                                                   \
     {                                                                  \
-      int lev = crea::MessageManager::GetMessageLevel("Warning");      \
+      int lev = crea::MessageManager::GetMessageLevel("warning");      \
       if (lev >0)                                                      \
        {                                                               \
          std::cerr << "!! WARNING !! " << MESSAGE << std::endl;        \
 #define creaendl std::endl
 //===========================================================
 
+// Signal/slot mechanism for message events
+#include <boost/signal.hpp>
+#include <boost/bind.hpp>
 
 namespace crea 
 {
-
+  
   class CREA_EXPORT MessageManager
   {
   public:
+    //=============================================
+    typedef boost::signal<void (const std::string&)>  MessageSignalType;
+    typedef MessageSignalType::slot_function_type MessageCallbackType;
+    //=============================================
     ///
     MessageManager();
     ///
@@ -387,14 +326,20 @@ namespace crea
     ///
     static MessageManager* GetInstance();
     ///
-    static void RegisterMessageType(std::string key, 
-                                    std::string help,
+    static void RegisterMessageType(const std::string& key, 
+                                    const std::string& help,
                                     unsigned char default_level = 9);
     ///
-    static void SetMessageLevel(std::string key, unsigned char level);
+    static void SetMessageLevel(const std::string& key, unsigned char level);
     ///
-    static int GetMessageLevel(std::string key);
+    static int GetMessageLevel(const std::string& key);
     ///  
+    static void SendMessage(const std::string& key, const std::string& mess);
+    ///
+    static void AddMessageObserver(const std::string& key, MessageCallbackType callback );
+    ///
+    static void SendMessagesToCout(bool v = true);
+    ///
     static std::string& GetTab() { static std::string s; return s; }
     ///
     static std::string GetSpace(int n) { 
@@ -407,11 +352,20 @@ namespace crea
     static void ResetTab() { GetTab() = std::string(""); }
     ///
     static void PrintInfo();
-
+    /// 
+    
   private:
-    std::map<std::string,int> mMessageLevel;
-    std::map<std::string,std::string> mMessageHelp;  
+    struct MessageType
+    {
+      MessageType(int l, const std::string& h) : Level(l), Help(h) {}
+      int Level;
+      std::string Help;
+      MessageSignalType Signal;
+    };
+    typedef std::map<std::string,MessageType*> MessageMapType;
+    MessageMapType mMessageMap;
     unsigned int mMaxMessageLength;
+    bool mSendToCout;
   };
   //===========================================================
   
index 3b205d1f54d935dc890b6dd5b5f181c05f843cfd..7eb5b18e4e75066d0385b90e394c71910ea66812 100644 (file)
@@ -15,11 +15,13 @@ namespace crea
                                                    int ny,
                                                    int nz);
 
+  /*
+    // Already provided by vtkTypeTraits<T>::VTKTypeID()
   template <class T>
-  vtkIdType GetVtkIdType(T&) { return VTK_VOID; }
+  int GetVtkType(T&) { return VTK_VOID; }
 
 #define SPECIALIZE_GETVTKIDTYPE(T,R) \
-  template <> vtkIdType GetVtkIdType<T>(T&) { return R; }  
+  template <> int GetVtkType<T>(T&) { return R; }  
 
   SPECIALIZE_GETVTKIDTYPE(char,VTK_CHAR)
   SPECIALIZE_GETVTKIDTYPE(signed char,VTK_SIGNED_CHAR)
@@ -34,6 +36,7 @@ namespace crea
   SPECIALIZE_GETVTKIDTYPE(double,VTK_DOUBLE)
 
 #undef SPECIALIZE_GETVTKIDTYPE
+  */
 }
 
 #include <creaVtk.txx>
index 4d39387958debb94ce4b6316ccdff53b8986af9d..bbebc1dad8e79fe1702f3559ff6d3b4847e21e37 100644 (file)
@@ -1,6 +1,20 @@
 #include <vtkDataArrayTemplate.h>
+
+#include <vtkCharArray.h>
+#include <vtkSignedCharArray.h>
+#include <vtkUnsignedCharArray.h>
+#include <vtkShortArray.h>
+#include <vtkUnsignedShortArray.h>
+#include <vtkIntArray.h>
+#include <vtkUnsignedIntArray.h>
+#include <vtkLongArray.h>
+#include <vtkUnsignedLongArray.h>
+#include <vtkFloatArray.h>
+#include <vtkDoubleArray.h>
+
 #include <vtkPointData.h>
-#include <typeinfo> 
+#include <vtkTypeTraits.h>
+#include <creaMessageManager.h>
 namespace crea
 {
   
@@ -10,30 +24,52 @@ namespace crea
                                                    int ny,
                                                    int nz)
   {
-    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
-    std::cout << typeid(T).name() << std::endl;
+    //    std::cout << "NV "<<nx<<" " <<ny<<" " << nz<<std::endl;
+    //    std::cout <<  vtkTypeTraits<T>::SizedName() << std::endl;
     vtkImageData *image = vtkImageData::New();    
     image->SetNumberOfScalarComponents(1);
-    image->SetScalarType(GetVtkIdType<T>(*data));
+    int vtktype = vtkTypeTraits<T>::VTKTypeID();
+    image->SetScalarType(vtktype);
     image->SetDimensions(nx,ny,nz);
     image->SetSpacing(1,1,1);
-    std::cout << "alloc"<<std::endl;
     image->AllocateScalars();
-    std::cout << "alloc ok"<<std::endl;
-    vtkDataArrayTemplate<T>* array 
-      = (vtkDataArrayTemplate<T>*)vtkDataArrayTemplate<T>::New();
-    std::cout << "data array alloc ok"<<std::endl;
-    //    array->SetNumberOfComponents( 1 );
-    std::cout << "nb compo ok"<<std::endl;
-    // The last param of SetArray is set to 1 to keep the class from deleting the array when it cleans up or reallocates memory.
+    vtkDataArray* array = 0;
+    switch (vtktype)
+      {
+      case VTK_CHAR:
+       array = vtkCharArray::New(); break;
+      case VTK_SIGNED_CHAR:
+       array = vtkSignedCharArray::New(); break;
+      case VTK_UNSIGNED_CHAR:
+       array = vtkUnsignedCharArray::New(); break;
+      case VTK_SHORT:
+       array = vtkShortArray::New(); break;
+      case VTK_UNSIGNED_SHORT:
+       array = vtkUnsignedShortArray::New(); break;
+      case VTK_INT:
+       array = vtkIntArray::New(); break;      
+      case VTK_UNSIGNED_INT:
+       array = vtkUnsignedIntArray::New(); break;
+      case VTK_LONG:
+       array = vtkLongArray::New(); break;
+      case VTK_UNSIGNED_LONG:
+       array = vtkUnsignedLongArray::New(); break;
+      case VTK_FLOAT:
+       array = vtkFloatArray::New(); break;
+      case VTK_DOUBLE:
+       array = vtkDoubleArray::New(); break;
+      default:
+       creaGlobalError("NewVtkImageDataFromRaw : type "
+                 <<vtkTypeTraits<T>::SizedName()
+                 <<" non implemented");
+      }
+    vtkDataArrayTemplate<T>* tarray 
+      = dynamic_cast<vtkDataArrayTemplate<T>*>(array);
+    array->SetNumberOfComponents( 1 );
     size_t size = (long)nx*(long)ny*(long)nz;
-    std::cout << "SetArray"<<std::endl;
-    array->SetArray( data, size, 1 );
-    //Pay attention in particular to the last param of SetArray:
-    //http://www.vtk.org/doc/nightly/html/classvtkUnsignedShortArray.html#z798_0
-   std::cout << "SetScalars"<<std::endl;
-     image->GetPointData( )->SetScalars( array );
-   std::cout << "Delete"<<std::endl;
+    // The last param of SetArray is set to 1 to keep the class from deleting the array when it cleans up or reallocates memory.
+    tarray->SetArray( data, size, 1 );
+    image->GetPointData( )->SetScalars( tarray );
     array->Delete( );
     return image;
   }