Home › Forum › SOFA › Programming with SOFA › [SOLVED] VectorData merge function question
Tagged: 64_bits, Linux_ubuntu, SOFA_1706, vectorData Data merge copy
- This topic has 1 reply, 2 voices, and was last updated 7 years, 3 months ago by jnbrunet.
-
AuthorPosts
-
16 August 2017 at 11:15 #9921ErwanDouailleBlocked
Hey,
I was looking for copying VectorData and i found the merge function.
Here is the source code of merge function :/// merging several Data from a VectorData into a large Data (of the same type) static void merge(Data<T>& outputData, const vectorData<T>& vectorData) { size_t nbInput = vectorData.size(); size_t nbElems = 0; for( size_t i=0 ; i<nbInput ; ++i ) nbElems += vectorData[i]->getValue().size(); helper::WriteOnlyAccessor< Data<T> > out = outputData; out.clear(); out.reserve(nbElems); for( size_t i=0 ; i<nbInput ; ++i ) { helper::ReadAccessor< Data<T> > in = vectorData[i]; for( size_t j=0, jend=in.size() ; j<jend ; ++j ) out.push_back(in[j]); } }
But I encounter than Data was the “output” and it feels weird from me …
The code of merge function, use push_back data in data, which looks like Data is a VectorData.Finally here is my question, why Data has the same behavior of VectorData (push_back) ? What is the real difference between Data and VectorData, if Data can be considered as a vector ?
16 August 2017 at 15:30 #9922jnbrunetModeratorHi Erwan,
It seems the merge function is only used by
MergeVectors<VecT>::update()
(SofaGeneralEngine/MergeVectors.inl). In this case, theData<T>& out
variable you see will be of typeData<VecT>
. So, when expending the templates, theout.push_back
is in fact callingVecT.push_back()
since the WriteOnlyAccessor is creating a bridge betweenData<T>.push_back()
andT.push_back()
.I’m pretty sure that if you call this merge function with a
T
template that does not define apush_back()
function somewhere, the compilation will fail.For your second question,
Data<T>
can be of any type T, be it a vector or not.vectoData<T>
is of typevector<Data<T>>
which is always a vector. BUT, in the case of this merge function, theData<T>
must be of type vector since the template must have the push_back function. So here,Data<T>
is a vector andvectorData<T>
is a vector of vector.This merge function is what is often call a flattening function :
[[0,1],[2,3]] will become [0,1,2,3].
Hope that helps.
-
AuthorPosts
- You must be logged in to reply to this topic.