Ejemplo n.º 1
0
void load_volume_node(tinyxml2::XMLElement *elem, Scene &scene, std::stack<Transform> &transform_stack, const std::string &file){
	if (!elem->Attribute("name")){
		std::cout << "Scene error: Volume nodes require a name" << std::endl;
		std::exit(1);
	}
	std::string name = elem->Attribute("name");
	Transform t;
	read_transform(elem, t);
	t = transform_stack.top() * t;
	Volume *vol = load_volume(elem->FirstChildElement("volume"), scene.get_volume_cache(), file);
	if (!vol){
		std::cout << "Scene error: could not load volume attached to volume node " << name << std::endl;
		std::exit(1);
	}
	if (scene.get_volume_root() == nullptr){
		scene.set_volume_root(std::make_unique<VolumeNode>(vol, t, name));
	}
	else {
		auto &children = scene.get_volume_root()->get_children();
		children.push_back(std::make_unique<VolumeNode>(vol, t, name));
	}
	transform_stack.push(t);
}
Ejemplo n.º 2
0
int
main(int argc,
     char* argv[])
{
  IoParams params;
  try
  {
    params.parse(argc,argv);
  }
  catch (const std::exception& excp)
  {
    std::cerr << " Exception while parsing cmd-line\n"
    << excp.what() << std::endl;
    exit(1);
  }
  catch (...)
  {
    std::cerr << " unhandled exception caught while parsing cmd line\n";
    exit(1);
  }


  boost::shared_ptr<gmp::VolumeMorph> pmorph(new gmp::VolumeMorph);

  if ( !params.strTemplate.empty() )
  {
    MRI* mri = MRIread( const_cast<char*>(params.strTemplate.c_str()) );
    if (!mri)
      std::cerr << " Failed to open template mri "
                << params.strTemplate << std::endl;
    else
      pmorph->set_volGeom_fixed(mri);
    MRIfree(&mri);
  }
  if ( !params.strSubject.empty() )
  {
    MRI* mri = MRIread( const_cast<char*>(params.strSubject.c_str()) );
    if (!mri)
      std::cerr << " Failed to open subject mri "
                << params.strSubject << std::endl;
    else
      pmorph->set_volGeom_moving(mri);
    MRIfree(&mri);
  }

  IoParams::MorphContainerType::iterator it;
  for ( it = params.items.begin();
        it != params.items.end();
        ++it )
  {
    if ( it->type == "affine" )
    {
      boost::shared_ptr<gmp::AffineTransform3d>
      paffine( new gmp::AffineTransform3d);
      float* pf = read_transform( it->file.c_str() );
      if ( !pf )
      {
        std::cerr << " failed to read transform\n";
        exit(1);
      }
      paffine->set_pars( pf);
      pmorph->m_transforms.push_back( paffine );

    }
    else if ( it->type == "volume" )
    {
      boost::shared_ptr<gmp::DeltaTransform3d>
      pvol( new gmp::DeltaTransform3d);
      MRI* field = MRIread
                   ( const_cast<char*>( it->file.c_str() ) );
      if ( !field )
      {
        std::cerr << " failed to read field "  << std::endl;
        exit(1);
      }
      pvol->set_field(field);

      pmorph->m_transforms.push_back( pvol );
    }
    else if ( it->type == "gcam" )
    {
      boost::shared_ptr<gmp::DeltaTransform3d>
      pvol( new gmp::DeltaTransform3d);
      GCA_MORPH* gcam = GCAMread( const_cast<char*>( it->file.c_str() ) );
      if ( !gcam )
      {
        std::cerr << " failed to read GCAM " << std::endl;
        exit(1);
      }
      // create bogus MRI to hold the geometry
      MRI* mri_template = MRIread( const_cast<char*>
                                   ( params.strTemplate.c_str() ) );
      MRI* mriBuf = MRIalloc( gcam->image.width,
                              gcam->image.height,
                              gcam->image.depth,
                              MRI_UCHAR);
      useVolGeomToMRI( &gcam->image, mriBuf );
      GCAMrasToVox(gcam, mriBuf );
      MRIfree( &mriBuf );

      std::cout << " atlas geometry = "
      << gcam->atlas.width << " , " << gcam->atlas.height
      << " , " << gcam->atlas.depth << std::endl
      << " image geometry = "
      << gcam->image.width << " , " << gcam->image.height
      << " , " << gcam->image.depth << std::endl;


      MRI* mri = MRIallocSequence( mri_template->width,
                                   mri_template->height,
                                   mri_template->depth,
                                   MRI_FLOAT,
                                   3);
      MRIcopyHeader(mri_template, mri);
      g_vDbgCoords = params.vDbgCoords;
      try
      {
        //MRI* mask =
        CopyGcamToDeltaField(gcam, mri);

        pvol->set_field(mri);
        //pvol->set_mask(mask);
        pmorph->m_transforms.push_back( pvol );
      }
      catch (const std::string& e)
      {
        std::cerr << " Exception caught while processing GCAM node\n"
        << e << std::endl;
        exit(1);
      }
      MRIfree(&mri_template);
    }
    else if ( it->type == "morph" )
    {
      boost::shared_ptr<gmp::VolumeMorph> tmpMorph(new gmp::VolumeMorph);
      try
      {
        tmpMorph->load( it->file.c_str() );
      }
      catch (const char* msg)
      {
        std::cerr << " Exception caught while loading morph in file "
        << it->file << std::endl;
        exit(1);
      }
      for ( gmp::VolumeMorph::TransformContainerType::iterator transformIter
            = tmpMorph->m_transforms.begin();
            transformIter != tmpMorph->m_transforms.end();
            ++transformIter )
        pmorph->m_transforms.push_back( *transformIter );

    }
    else if ( it->type == "mesh" )
    {
      boost::shared_ptr<gmp::FemTransform3d> pfem(new gmp::FemTransform3d);
      boost::shared_ptr<CMesh3d> pmesh(new CMesh3d);
      pmesh->load( it->file.c_str() );
      pfem->m_sharedMesh = pmesh;
      pmorph->m_transforms.push_back(pfem);
    }
    else
    {
      std::cerr << " unhandled transform type "
      << it->type << std::endl;
    }
  } // next it

  // finally write morph file
  try
  {
    pmorph->save( params.strOut.c_str() );
  }
  catch (const char* msg)
  {
    std::cerr << " Exception caught while saving morph\n"
    << msg << std::endl;
    exit(1);
  }

  return 0;
}
Ejemplo n.º 3
0
gint read_frame(FILE *fp, gint n, struct model_pak *model)
{
    gint status;
    gdouble rmax, v1[3], v2[3];
    gpointer camera=NULL;
    GString *err_text;

    g_assert(model != NULL);

    rmax = model->rmax;

    /* conventional or transformation style animation */
    if (model->transform_list)
    {
        /* NEW - process transformation list as an animation */
        status = read_transform(n, model);
    }
    else
    {
        g_assert(fp != NULL);

        ARR3SET(v1, model->centroid);
        ARR3SET(v2, model->offset);

        status = read_raw_frame(fp, n, model);
        if (status)
        {
            err_text = g_string_new("");
            g_string_printf(err_text, "Error reading frame: %d\n", n);
            gui_text_show(ERROR, err_text->str);
            g_string_free(err_text, TRUE);
            return(1);
        }

        /* setup (have to save camera) */
        camera = camera_dup(model->camera);
        model_prep(model);
        model->camera = camera;
        g_free(model->camera_default);
        model->camera_default = camera;

        ARR3SET(model->centroid, v1);
        ARR3SET(model->offset, v2);
    }

    /* apply desired constraint */
    if (model->periodic && !model->anim_fix)
    {
        switch (model->anim_confine)
        {
        case PBC_CONFINE_ATOMS:
            coords_confine_cores(model->cores, model);

        case PBC_CONFINE_MOLS:
            coords_compute(model);
            connect_bonds(model);
            connect_molecules(model);
            break;
        }
    }

    if (model->anim_noscale)
        model->rmax = rmax;

    return(0);
}