[Rcpp-devel] good style for std::vector of C++ struct -> named list of named lists

Dr Gregory Jefferis jefferis at mrc-lmb.cam.ac.uk
Wed Jun 24 13:56:59 CEST 2015


Hello all,

I am wrapping a very simple C++ library in a package (to read wavefront 
3D obj files). The results come back as a std::vector of a struct which 
contains various fields using datatypes such as float, float[], 
std::string. I am going to return this as a list of lists. Each of the 
sublists will contain a set of named elements defined by the struct.

My question is what is the most efficient way to do this in present 
Rcpp? To overcome verbosity of using Named, I have opted for a macro 
that looks something like this:

   #define MNAME(X) Named(#X, m.X)

(see snippet below). Is there a better way to do this?

Also I feel there must a better way to to make a numeric vector from

   float ambient[3];

than

   Named("ambient", NumericVector::create(m.ambient[0], m.ambient[1], 
m.ambient[2]))

But I am not finding it. Many thanks for any suggestions!

Best,

Greg.

#define MNAME(X) Named(#X, m.X)
   List ml;
   for(unsigned int i=0; i<materials.size(); i++) {
     tinyobj::material_t m=materials[i];
     /* typedef struct {
         std::string name;

         float ambient[3];
         float diffuse[3];
         float specular[3];
         float transmittance[3];
         float emission[3];
         float shininess;
         float ior;      // index of refraction
         float dissolve; // 1 == opaque; 0 == fully transparent
         // illumination model (see 
http://www.fileformat.info/format/material/)
         int illum;

         std::string ambient_texname;
         std::string diffuse_texname;
         std::string specular_texname;
         std::string normal_texname;
         std::map<std::string, std::string> unknown_parameter;
       } material_t; */
     ml[m.name]=List::create(Named("ambient", 
NumericVector::create(m.ambient[0], m.ambient[1], m.ambient[2])),
                             Named("diffuse", 
NumericVector::create(m.diffuse[0], m.diffuse[1], m.diffuse[2])),
                             Named("specular", 
NumericVector::create(m.specular[0], m.specular[1], m.specular[2])),
                             Named("transmittance", 
NumericVector::create(m.transmittance[0], m.transmittance[1], 
m.transmittance[2])),
                             Named("emission", 
NumericVector::create(m.emission[0], m.emission[1], m.emission[2])),
                             MNAME(shininess),
                             MNAME(ior), MNAME(dissolve), MNAME(illum),
                             MNAME(ambient_texname), 
MNAME(diffuse_texname),
                             MNAME(specular_texname), 
MNAME(normal_texname));
   }


--
Gregory Jefferis, PhD
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK

http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu


More information about the Rcpp-devel mailing list