← Back to team overview

yade-users team mailing list archive

Re: Looping interactions and bodies


> I see generally 2 methods of iterating bodies and interactions. 
> 1st method:
> Interaction::Container::iterator ii =
> ncb->transientInteractions->begin();
> Interaction::Container::iterator iiEnd = ncb -> transientInteraction
> -> end();
> for( ; ii!=iiEnd; ++i){  }
> 2nd method:
> FOREACH(const shared_ptr<Interaction>& I, *ncb->interaction){ }
> What are the differences between the two methods [i.e. (*ii) from the
> first method or (I) in the second method] ??   In what circumstances
> do I choose one over the other?  

Use always the second one, it is functionally the same and more
readable. (transientInteractions is deprecated and is now reference to

The same for bodies. FOREACH also works for STL containers such as
vector<string> abc; FOREACH(string& s, abc){ ... }.

Make sure you avoid NULL (erased) bodies and non-real interactions:

FOREACH(const shared_ptr<Body>& b, *scene->bodies){
	if(!b) continue;
	/* ... */


FOREACH(const shared_ptr<Interaction>& i, *scene->interactions){
	if(!i->isReal()) continue;
	/* ... */

I was thinking about modification of the iterators so that such
bodies/interactions would be skipped silently (if desired), but that has
not happened (yet?).

Cheers, Vaclav

Follow ups