contrib/mul/vimt3d/vimt3d_save.cxx
Go to the documentation of this file.
00001 // This is mul/vimt3d/vimt3d_save.cxx
00002 #include "vimt3d_save.h"
00003 //:
00004 // \file
00005 // \author Ian Scott
00006 
00007 #include <mbl/mbl_log.h>
00008 #include <vil3d/vil3d_image_resource.h>
00009 #include <vil3d/vil3d_new.h>
00010 #include <vil3d/vil3d_save.h>
00011 #include <vimt3d/vimt3d_transform_3d.h>
00012 #include <vimt3d/vimt3d_image_3d.h>
00013 #include <vimt3d/vimt3d_vil3d_v3i.h>
00014 
00015 
00016 static mbl_logger& logger()
00017 {
00018   static mbl_logger l("contrib.mul.vimt3d.save");
00019   return l;
00020 }
00021 
00022 //: Create a transform from the properties of image resource.
00023 // The transform will be from world co-ordinates in metres to image co-ordinates (or mm if requested).
00024 void vimt3d_save_transform(vil3d_image_resource_sptr &ir,
00025                            const vimt3d_transform_3d& trans, 
00026                            bool use_millimetres /*=false*/)
00027 {
00028   if (dynamic_cast<vimt3d_vil3d_v3i_image *>(ir.ptr()))
00029   {
00030     vgl_vector_3d<double> vox_per_mm = trans.delta(vgl_point_3d<double>(0,0,0), 
00031                                                    vgl_vector_3d<double>(1.0, 1.0, 1.0));
00032 
00033     // get the translation component
00034     double tx =  trans.matrix()(0,3);
00035     double ty =  trans.matrix()(1,3);
00036     double tz =  trans.matrix()(2,3);
00037 
00038     vimt3d_transform_3d tr;
00039     //const double units_scaling = use_millimetres ? 1000.0 : 1.0;
00040     tr.set_zoom_only (1000.0*vox_per_mm.x(),
00041                       1000.0*vox_per_mm.y(),
00042                       1000.0*vox_per_mm.z(), tx,ty,tz );
00043 
00044     static_cast<vimt3d_vil3d_v3i_image &>(*ir).set_world2im(tr);
00045   }
00046   else
00047   {
00048     vimt3d_transform_3d i2w=trans.inverse();
00049     vgl_vector_3d<double> dp = i2w.delta(vgl_point_3d<double> (0,0,0),
00050                                          vgl_vector_3d<double> (1.0, 1.0, 1.0));
00051     if (!ir->set_voxel_size(float(dp.x()),float(dp.y()),float(dp.z())))
00052       MBL_LOG(WARN, logger(), "vimt3d_save_transform(): Unable to include voxel sizes:"
00053               <<dp.x()<<','<<dp.y()<<','<<dp.z() );
00054   }
00055 }
00056 
00057 
00058 bool vimt3d_save(const vcl_string& path,
00059                  const vimt3d_image_3d& image,
00060                  bool use_millimetres /*=false*/)
00061 {
00062   const vimt3d_image_3d & iv = image;
00063   const vil3d_image_view_base & ib = iv.image_base();
00064 
00065   vil3d_image_resource_sptr ir = vil3d_new_image_resource(
00066     path.c_str(), ib.ni(), ib.nj(), ib.nk(), ib.nplanes(), ib.pixel_format(),
00067     vil3d_save_guess_file_format(path.c_str()));
00068 
00069   if (!ir)
00070     return false;
00071 
00072   ir->put_view(ib);
00073 
00074   vimt3d_save_transform(ir, image.world2im(), use_millimetres);
00075   return true;
00076 }