34 #ifndef AIMS_IO_GIFTIMESHFORMAT_D_H 
   35 #define AIMS_IO_GIFTIMESHFORMAT_D_H 
   52     template<
int D, 
typename T>
 
   54       giiDataArray *
da, GiftiHeader & hdr) 
 
   56       int vnum = 
da->dims[0];
 
   58       std::vector<T> & tex = vol[texnum].
texture();
 
   61       for (j = 0; j < vnum; ++j) 
 
   63         tex.push_back(convertedNiftiValue<T> (
da->data, j, 
da->datatype));
 
   67     class GiftiReadExternalTexture: 
public Process 
 
   83     bool giftiReadExternalTexture(Process & p, 
const std::string &, Finder &) 
 
   85       GiftiReadExternalTexture & gp
 
   86           = 
static_cast<GiftiReadExternalTexture &
> (p);
 
   87       giiDataArray *
da = gp.da;
 
   88       int j, vnum = 
da->dims[0], 
nt = gp.nt;
 
  101       std::vector<T> & tex = ttex[ttex.size() - 1].data();
 
  103       for (j = 0; j < vnum; ++j) 
 
  105         tex.push_back(convertedNiftiValue<T> (
da->data, j, 
da->datatype));
 
  113       giiDataArray *
da, GiftiHeader & hdr, 
int nts) 
 
  118         textures = hdr.getProperty(
"textures");
 
  125       int ndim = 
da->num_dim;
 
  132       p.registerProcessType(
"Texture", 
"FLOAT",
 
  133                             &giftiReadExternalTexture<float> );
 
  134       p.registerProcessType(
"Texture", 
"POINT2DF",
 
  135                             &giftiReadExternalTexture<Point2df> );
 
  136       p.registerProcessType(
"Texture", 
"S16",
 
  137                             &giftiReadExternalTexture<int16_t> );
 
  138       p.registerProcessType(
"Texture", 
"S32",
 
  139                             &giftiReadExternalTexture<int32_t> );
 
  140       p.registerProcessType(
"Texture", 
"U32",
 
  141                             &giftiReadExternalTexture<uint32_t> );
 
  142       p.registerProcessType(
"Texture", 
"VECTOR_OF_2_S16",
 
  143                             &giftiReadExternalTexture<Point2d> );
 
  146       f.setObjectType(
"Texture");
 
  147       f.setDataType(dtype);
 
  148       if (p.execute(f, 
"")) 
 
  150         hdr.setProperty(
"textures", 
textures);
 
  156   template<
int D, 
typename T>
 
  158     D, T> & vol, 
const carto::AllocatorContext & ,
 
  169     gifti_image *gim = gifti_read_image(hdr.
name().c_str(), 1);
 
  177     int nda = gim->numDA, i;
 
  178     int tmesh = -1, ttex = -1, tnorm = -1, tpoly = -1, 
nts = 0;
 
  179     for (i = 0; i < nda; ++i) 
 
  181       giiDataArray *
da = gim->darray[i];
 
  184         gifti_free_image(gim);
 
  189       case NIFTI_INTENT_POINTSET: 
 
  192           char *ts = gifti_get_meta_value(&
da->meta, 
"Timestep");
 
  194                   sscanf(ts, 
"%d", &tmesh);
 
  195           int vnum = 
da->dims[0];
 
  199           if (
da->ind_ord == GIFTI_IND_ORD_UNDEF)
 
  200             std::cout << 
"GIFTI_IND_ORD_UNDEF" << std::endl;
 
  202           if (
da->ind_ord == GIFTI_IND_ORD_ROW_MAJOR)
 
  203             std::cout << 
"GIFTI_IND_ORD_ROW_MAJOR" << std::endl;
 
  205           if (
da->ind_ord == GIFTI_IND_ORD_COL_MAJOR)
 
  206             std::cout << 
"GIFTI_IND_ORD_COL_MAJOR " << vnum << std::endl;
 
  209           std::vector<Point3df> & vert = vol[tmesh].
vertex();
 
  213           if (
da->ind_ord == GIFTI_IND_ORD_ROW_MAJOR || 
da->ind_ord == GIFTI_IND_ORD_UNDEF)
 
  214             for (j = 0; j < vnum; ++j)
 
  216                 convertedNiftiValue<float> (
da->data, j* 3, 
da->datatype),
 
  217                 convertedNiftiValue<float> (
da->data, j * 3 + 1, 
da->datatype),
 
  218                 convertedNiftiValue<float> (
da->data, j * 3 + 2,
da->datatype)));
 
  220           if (
da->ind_ord == GIFTI_IND_ORD_COL_MAJOR)
 
  221             for (j = 0; j < vnum; ++j)
 
  223                 convertedNiftiValue<float> (
da->data, j , 
da->datatype),
 
  224                 convertedNiftiValue<float> (
da->data, j + vnum, 
da->datatype),
 
  225                 convertedNiftiValue<float> (
da->data, j + 2*vnum, 
da->datatype)));
 
  228       case NIFTI_INTENT_VECTOR: 
 
  231           char *ts = gifti_get_meta_value(&
da->meta, 
"Timestep");
 
  233             sscanf(ts, 
"%d", &tnorm);
 
  234           else if (tnorm < tmesh)
 
  236           int vnum = 
da->dims[0];
 
  238           std::vector<Point3df> & 
norm = vol[tnorm].
normal();
 
  242           if (
da->ind_ord == GIFTI_IND_ORD_ROW_MAJOR || 
da->ind_ord == GIFTI_IND_ORD_UNDEF)
 
  243             for (j = 0; j < vnum; ++j)
 
  245                 convertedNiftiValue<float> (
da->data, j * 3, 
da->datatype),
 
  246                 convertedNiftiValue<float> (
da->data, j * 3 + 1, 
da->datatype),
 
  247                 convertedNiftiValue<float> (
da->data, j * 3 + 2, 
da->datatype)));
 
  249           if (
da->ind_ord == GIFTI_IND_ORD_COL_MAJOR)
 
  250             for (j = 0; j < vnum; ++j)
 
  252                 convertedNiftiValue<float> (
da->data, j , 
da->datatype),
 
  253                 convertedNiftiValue<float> (
da->data, j + vnum, 
da->datatype),
 
  254                 convertedNiftiValue<float> (
da->data, j + 2*vnum, 
da->datatype)));
 
  258       case NIFTI_INTENT_TRIANGLE: 
 
  261           char *ts = gifti_get_meta_value(&
da->meta, 
"Timestep");
 
  263                   sscanf(ts, 
"%d", &tpoly);
 
  264           else if (tpoly < tmesh)
 
  266           int vnum = 
da->dims[0];
 
  268           std::vector<AimsVector<unsigned, D> > & poly = vol[tpoly].
polygon();
 
  273             gifti_free_image( gim );
 
  277           if (
da->ind_ord == GIFTI_IND_ORD_ROW_MAJOR || 
da->ind_ord == GIFTI_IND_ORD_UNDEF)
 
  278             for (j = 0; j < vnum; ++j)
 
  282               for (k = 0; k < D; ++k)
 
  283                 p[k] = convertedNiftiValue<unsigned> (
da->data, j * D + k,
 
  287           if (
da->ind_ord == GIFTI_IND_ORD_COL_MAJOR)
 
  288             for (j = 0; j < vnum; ++j)
 
  292               for (k = 0; k < D; ++k)
 
  293                 p[k] = convertedNiftiValue<unsigned> (
da->data, j + k*vnum, 
da->datatype);
 
  297       case NIFTI_INTENT_TIME_SERIES:
 
  300         giftiReadTexture(vol, ttex, 
da, hdr, 
nts);
 
  308           giftiReadTexture(vol, ttex, 
da, hdr, 0);
 
  329     for (i = 0; i < tmesh; ++i) 
 
  332       std::vector<Point3df> & vert = surf.
vertex();
 
  333       std::vector<AimsVector<uint, D> > & poly = surf.
polygon();
 
  334       typename std::vector<AimsVector<uint, D> >::iterator ip;
 
  335       unsigned nvertex = vert.size();
 
  336       for (ip = poly.begin(); ip != poly.end(); ++ip) 
 
  339         for (
int j = 0; j < D; ++j)
 
  340           if (pol[j] >= nvertex) 
 
  345               std::cerr << 
"Broken mesh: polygon pointing to a " 
  346                 << 
"vertex out of range" << std::endl;
 
  355       if (surf.
normal().size() != vert.size())
 
  360     gifti_free_image(gim);
 
  366   template<
int D, 
typename T>
 
  379       gifti_image *gim = hdr.giftiImageBase();
 
  380       std::string fname = hdr.
name();
 
  382       int hdrmeshda = 0, hdrnormda = 0, hdrpolyda = 0, hdrtexda = 0;
 
  390         da_info = thdr.
getProperty(
"GIFTI_dataarrays_info");
 
  393         for (; it->isValid(); it->next()) 
 
  396           if (el->getProperty(
"encoding", encoding))
 
  406       if (!options().isNull()) 
 
  412           normal = (bool) n->getScalar();
 
  414           if ((
int) a->getScalar() != 0) 
 
  416             encoding = (int) a->getScalar();
 
  428       for( is=thing.begin(); is!=es; ++is, ++t )
 
  433           const std::vector<Point3df> & vert = surf.
vertex();
 
  435           gifti_add_empty_darray( gim, 1 );
 
  436           giiDataArray * 
da = gim->darray[nda];
 
  437           gifti_set_DA_defaults( 
da );
 
  438           da->intent = NIFTI_INTENT_POINTSET;
 
  439           da->datatype = NIFTI_TYPE_FLOAT32;
 
  441           da->dims[0] = vert.size();
 
  447           da->nvals = vert.size() * 3;
 
  450           da->encoding = encoding;
 
  455           gifti_alloc_DA_data( gim, &nda, 1 );
 
  456           unsigned i, n = vert.size();
 
  458           float *buf = 
reinterpret_cast<float *
>( 
da->data );
 
  461             buf[i*3] = vert[i][0];
 
  462             buf[i*3+1] = vert[i][1];
 
  463             buf[i*3+2] = vert[i][2];
 
  469             std::ostringstream ts;
 
  471             gifti_add_to_meta( &
da->meta, 
"Timestep", ts.str().c_str(), 1 );
 
  494           = GiftiHeader::giftiFindHdrDA( hdrmeshda, da_info,
 
  495                           "NIFTI_INTENT_POINTSET" );
 
  499                   GiftiHeader::giftiCopyMetaToGii( dainf, 
da );
 
  506           const std::vector<Point3df> & 
norm = surf.
normal();
 
  510             gifti_add_empty_darray( gim, 1 );
 
  511             giiDataArray * 
da = gim->darray[nda];
 
  512             gifti_set_DA_defaults( 
da );
 
  513             da->intent = NIFTI_INTENT_VECTOR;
 
  514             da->datatype = NIFTI_TYPE_FLOAT32;
 
  516             da->dims[0] = 
norm.size();
 
  522             da->nvals = 
norm.size() * 3;
 
  525             da->encoding = encoding;
 
  527             gifti_alloc_DA_data( gim, &nda, 1 );
 
  528             unsigned i, n = 
norm.size();
 
  529             float *buf = 
reinterpret_cast<float *
>( 
da->data );
 
  532               buf[i*3] = 
norm[i][0];
 
  533               buf[i*3+1] = 
norm[i][1];
 
  534               buf[i*3+2] = 
norm[i][2];
 
  540               std::ostringstream ts;
 
  542               gifti_add_to_meta( &
da->meta, 
"Timestep", ts.str().c_str(), 1 );
 
  547             = GiftiHeader::giftiFindHdrDA( hdrnormda, da_info,
 
  548                             "NIFTI_INTENT_VECTOR" );
 
  552               GiftiHeader::giftiCopyMetaToGii( dainf, 
da );
 
  559           const std::vector<AimsVector<unsigned, D> > & poly
 
  562           gifti_add_empty_darray( gim, 1 );
 
  563           giiDataArray * 
da = gim->darray[nda];
 
  564           gifti_set_DA_defaults( 
da );
 
  565           da->intent = NIFTI_INTENT_TRIANGLE;
 
  566           da->datatype = NIFTI_TYPE_INT32;
 
  568           da->dims[0] = poly.size();
 
  574           da->nvals = poly.size() * D;
 
  577           da->encoding = encoding;
 
  579           gifti_alloc_DA_data( gim, &nda, 1 );
 
  580           unsigned i, j, n = poly.size();
 
  581           int *buf = 
reinterpret_cast<int *
>( 
da->data );
 
  584           buf[i*D+j] = poly[i][j];
 
  589             std::ostringstream ts;
 
  591             gifti_add_to_meta( &
da->meta, 
"Timestep", ts.str().c_str(), 1 );
 
  596           = GiftiHeader::giftiFindHdrDA( hdrpolyda, da_info,
 
  597             "NIFTI_INTENT_TRIANGLE" );
 
  601             GiftiHeader::giftiCopyMetaToGii( dainf, 
da );
 
  605         const std::vector<T> & tex = surf.
texture();
 
  606         hdr.giftiAddTexture( gim, tex );
 
  610         = GiftiHeader::giftiFindHdrDA( hdrtexda, da_info, 
"" );
 
  614           GiftiHeader::giftiCopyMetaToGii( dainf,
 
  615             gim->darray[gim->numDA-1] );
 
  620       hdr.giftiAddExternalTextures( gim, hdrtexda, da_info );
 
  623       hdr.giftiAddLabelTable( gim );
 
  633         for( t=0; t<nda; ++t )
 
  635           giiDataArray *
da = gim->darray[t];
 
  636           if( 
da->intent == NIFTI_INTENT_POINTSET
 
  637                           && ( first || 
da->numCS == 0 ) )
 
  647       gifti_write_image( gim, fname.c_str(), 1 );
 
  650       gifti_free_image( gim );
 
  663     catch( std::exception & e )
 
The template class to manage a mesh.
const std::vector< Point3df > & normal() const
Get a const reference to the vector of normals.
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of vertices.
void updateNormals()
Update/Compute the normals.
const std::vector< T > & texture() const
Get a const reference to the vector of textures.
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons.
The template class to manage a mesh with time if needed.
void setHeader(const aims::PythonHeader &hdr)
Set the header.
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons of the 0 surface.
const std::vector< T > & texture() const
Get a const reference to the vector of textures of the 0 surface.
const aims::PythonHeader & header() const
Get the header.
std::map< int, AimsSurface< D, T > >::const_iterator const_iterator
const std::vector< Point3df > & normal() const
Get a const reference to the vector of normals of the 0 surface.
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of verteces of the surface of index 0.
void reserve(size_t size)
Object reference(Object &value)
virtual bool getProperty(const std::string &, Object &) const
virtual bool removeProperty(const std::string &)
virtual bool hasProperty(const std::string &) const
static void launchErrnoExcept(const std::string &filename="")
The class for EcatSino data write operation.
std::string giftiTextureDataType(int dtype, int &ndim, int *dims, int intent, int &ntime)
std::vector< Object > ObjectVector
AIMSDATA_API float norm(const Tensor &thing)