Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[all] Convert some loops to range-based #4485

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ IncrSAP::IncrSAP()

IncrSAP::~IncrSAP()
{
for(int i = 0 ; i < 3 ; ++i)
for(EndPointList::iterator it = _end_points[i].begin() ; it != _end_points[i].end() ; ++it)
for(auto& _end_point : _end_points)
for(EndPointList::iterator it = _end_point.begin() ; it != _end_point.end() ; ++it)
delete (*it);
}



void IncrSAP::purge(){
for(int i = 0 ; i < 3 ; ++i){
for(EndPointList::iterator it = _end_points[i].begin() ; it != _end_points[i].end() ; ++it)
for(auto& _end_point : _end_points){
for(EndPointList::iterator it = _end_point.begin() ; it != _end_point.end() ; ++it)
delete (*it);

_end_points[i].clear();
_end_point.clear();
}

_boxes.clear();
Expand Down Expand Up @@ -209,8 +209,8 @@ inline void IncrSAP::addCollisionModel(core::CollisionModel *cm)

EndPointID * endPts[6];
for(Size i = 0 ; i < cube_model->getSize() ; ++i){
for(int j = 0 ; j < 6 ; ++j)
endPts[j] = new EndPointID;
for(auto& endPt : endPts)
endPt = new EndPointID;

ISAPBox & new_box = _boxes[old_size + i];
new_box.cube = Cube(cube_model,i);
Expand Down Expand Up @@ -269,9 +269,9 @@ void IncrSAP::updateEndPoints(){


void IncrSAP::setEndPointsID(){
for(int dim = 0 ; dim < 3 ; ++dim){
for(auto& _end_point : _end_points){
int ID = 0;
for(EndPointList::iterator it = _end_points[dim].begin() ; it != _end_points[dim].end() ; ++it){
for(EndPointList::iterator it = _end_point.begin() ; it != _end_point.end() ; ++it){
(**it).ID = ID;
++ID;
}
Expand All @@ -282,8 +282,8 @@ void IncrSAP::setEndPointsID(){
void IncrSAP::reinitDetection(){
_colliding_elems.clear();
const CompPEndPoint comp;
for(int j = 0 ; j < 3 ; ++j){
std::sort(_end_points[j].begin(),_end_points[j].end(),comp);
for(auto& _end_point : _end_points){
std::sort(_end_point.begin(),_end_point.end(),comp);
}
setEndPointsID();
}
Expand All @@ -293,17 +293,16 @@ void IncrSAP::reinitDetection(){
void IncrSAP::showEndPoints()const{
for(int j = 0 ; j < 3 ; ++j){
msg_info() <<"dimension "<<j<<"===========" ;
for(EndPointList::const_iterator it = _end_points[j].begin() ; it != _end_points[j].end() ; ++it){
const EndPointID & end_pt = (**it);
for(const auto it : _end_points[j]){
const EndPointID & end_pt = (*it);
end_pt.show();
}
}
}


void IncrSAP::showBoxes()const{
for(size_t i = 0 ; i < _boxes.size() ; ++i){
const ISAPBox & box = _boxes[i];
for(const auto & box : _boxes){
std::stringstream tmp;

tmp <<"collision model "<<box.cube.getCollisionModel()->getLast()<<" index "<<box.cube.getExternalChildren().first.getIndex()<<msgendl ;
Expand Down Expand Up @@ -388,9 +387,9 @@ void IncrSAP::boxPrune(){
else{ // we encounter a min possible intersection between it and active_boxes
int new_box = (**it).boxID();

for(unsigned int i = 0 ; i < active_boxes.size() ; ++i){
for(const int active_box : active_boxes){

addIfCollide(new_box,active_boxes[i],axis1,axis2);
addIfCollide(new_box,active_box,axis1,axis2);
}
active_boxes.push_back(new_box);
}
Expand Down Expand Up @@ -496,15 +495,15 @@ bool IncrSAP::assertion_inferior(EndPointList::iterator begin_it,const EndPointL
bool IncrSAP::assertion_end_points_sorted() const{
const CompPEndPoint inferior;
int n = 0;
for(int dim = 0 ; dim < 3 ; ++dim){
for(const auto& _end_point : _end_points){
[[maybe_unused]] int ID = 0;
EndPointList::const_iterator next_it2;
for(EndPointList::const_iterator it2 = _end_points[dim].begin() ; it2 != _end_points[dim].end() ; ++it2){
for(EndPointList::const_iterator it2 = _end_point.begin() ; it2 != _end_point.end() ; ++it2){
assert((**it2).ID == ID);

next_it2 = it2;
++next_it2;
if(next_it2 != _end_points[dim].end()){
if(next_it2 != _end_point.end()){
assert((**next_it2).ID == ID + 1);

if(!inferior(*it2,*next_it2)){
Expand Down Expand Up @@ -620,8 +619,7 @@ void IncrSAP::updateMovingBoxes(){
EndPointID updated_min;
EndPointID updated_max;

for(unsigned int i = 0 ; i < _boxes.size() ; ++i){
ISAPBox & cur_box = _boxes[i];
for(auto& cur_box : _boxes){
for(int dim = 0 ; dim < 3 ; ++dim){
min_updated = false;
max_updated = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,9 +1205,8 @@ bool LocalMinDistance::testValidity(Point &p, const Vec3 &PQ) const
const auto& edgesAroundVertex = topology->getEdgesAroundVertex(p.getIndex());
Vec3 nMean;

for (unsigned int i=0; i<trianglesAroundVertex.size(); i++)
for (unsigned int t : trianglesAroundVertex)
{
const unsigned int t = trianglesAroundVertex[i];
const auto& ptr = topology->getTriangle(t);
Vec3 nCur = (x[ptr[1]]-x[ptr[0]]).cross(x[ptr[2]]-x[ptr[0]]);
nCur.normalize();
Expand All @@ -1216,9 +1215,8 @@ bool LocalMinDistance::testValidity(Point &p, const Vec3 &PQ) const

if (trianglesAroundVertex.empty())
{
for (unsigned int i=0; i<edgesAroundVertex.size(); i++)
for (unsigned int e : edgesAroundVertex)
{
const unsigned int e = edgesAroundVertex[i];
const auto& ped = topology->getEdge(e);
Vec3 l = (pt - x[ped[0]]) + (pt - x[ped[1]]);
l.normalize();
Expand All @@ -1240,9 +1238,8 @@ bool LocalMinDistance::testValidity(Point &p, const Vec3 &PQ) const
}
}

for (unsigned int i=0; i<edgesAroundVertex.size(); i++)
for (unsigned int e : edgesAroundVertex)
{
const unsigned int e = edgesAroundVertex[i];
const auto& ped = topology->getEdge(e);
Vec3 l = (pt - x[ped[0]]) + (pt - x[ped[1]]);
l.normalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ void TriangleModelInRegularGrid::init()
while ( found )
{
found = false;
for ( vector<TopologicalMapping*>::iterator it = topoVec.begin(); it != topoVec.end(); ++it )
for (const auto& v : topoVec)
{
if ( ( *it )->getTo() == _higher_topo )
if ( v->getTo() == _higher_topo )
{
found = true;
_topoMapping = *it;
_topoMapping = v;
_higher_topo = _topoMapping->getFrom();
if ( !_higher_topo ) break;
const sofa::simulation::Node* node = static_cast< sofa::simulation::Node* > ( _higher_topo->getContext() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ void TriangleOctreeModel::computeBoundingTree(int maxDepth)
t.n() = cross(*pt[1]-*pt[0],*pt[2]-*pt[0]);
t.n().normalize();

for (int p=0; p<3; p++)
for (auto& p : pt)
{


for(int c=0; c<3; c++)
{
if ((*pt[p])[c] > maxElem[c]) maxElem[c] = (*pt[p])[c];
if ((*pt[p])[c] < minElem[c]) minElem[c] = (*pt[p])[c];
if ((*p)[c] > maxElem[c]) maxElem[c] = (*p)[c];
if ((*p)[c] < minElem[c]) minElem[c] = (*p)[c];

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ void CollisionResponse::createNewContacts(const core::collision::ContactManager:
{
std::stringstream errorStream;

for (DetectionOutputMap::const_iterator outputsIt = outputsMap.begin(),
outputsItEnd = outputsMap.end(); outputsIt != outputsItEnd ; ++outputsIt)
for (const auto& [models, output] : outputsMap)
{
const auto contactInsert = contactMap.insert(ContactMap::value_type(outputsIt->first, core::collision::Contact::SPtr()));
const auto contactInsert = contactMap.insert(ContactMap::value_type(models, core::collision::Contact::SPtr()));
const ContactMap::iterator contactIt = contactInsert.first;
if (contactInsert.second) //insertion success
{
// new contact
core::CollisionModel* model1 = outputsIt->first.first;
core::CollisionModel* model2 = outputsIt->first.second;
core::CollisionModel* model1 = models.first;
core::CollisionModel* model2 = models.second;

dmsg_error_when(model1 == nullptr || model2 == nullptr) << "Contact found with an invalid collision model";

Expand Down Expand Up @@ -182,15 +181,15 @@ void CollisionResponse::createNewContacts(const core::collision::ContactManager:
setContactTags(model1, model2, contact);
contact->f_printLog.setValue(notMuted());
contact->init();
contact->setDetectionOutputs(outputsIt->second);
contact->setDetectionOutputs(output);
++nbContact;
}
}
}
else
{
// pre-existing and still active contact
contactIt->second->setDetectionOutputs(outputsIt->second);
contactIt->second->setDetectionOutputs(output);
++nbContact;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ RuleBasedContactManager::RuleBasedContactManager()

RuleBasedContactManager::~RuleBasedContactManager()
{
for(std::map<std::string,Data<std::string>*>::iterator it = variablesData.begin(),
itend = variablesData.end(); it != itend; ++it)
for(const auto& d : variablesData)
{
//this->removeData(it->second);
delete it->second;
delete d.second;
}
}

Expand Down Expand Up @@ -125,11 +123,11 @@ std::string RuleBasedContactManager::getContactResponse(core::CollisionModel* mo
if (!response1.empty()) return response1;
else if (!response2.empty()) return response2;

const type::vector<Rule>& r = rules.getValue();
for (type::vector<Rule>::const_iterator it = r.begin(), itend = r.end(); it != itend; ++it)
const type::vector<Rule>& rulesValue = rules.getValue();
for (const auto& rule : rulesValue)
{
if (it->match(model1, model2) || it->match(model2, model1))
return replaceVariables(it->response); // rule it matched
if (rule.match(model1, model2) || rule.match(model2, model1))
return replaceVariables(rule.response); // rule it matched
}
// no rule matched
return replaceVariables(CollisionResponse::getContactResponse(model1, model2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,8 @@ void LCPConstraintSolver::build_Coarse_Compliance(std::vector<int> &constraint_m
dmsg_error_when(sizeCoarseSystem==0) <<"no constraint" ;

_Wcoarse.resize(sizeCoarseSystem,sizeCoarseSystem);
for (unsigned int i=0; i<l_constraintCorrections.size(); i++)
for (const auto& cc : l_constraintCorrections)
{
core::behavior::BaseConstraintCorrection* cc = l_constraintCorrections[i];
cc->getComplianceWithConstraintMerge(&_Wcoarse, constraint_merge);
}
}
Expand Down Expand Up @@ -752,9 +751,8 @@ int LCPConstraintSolver::nlcp_gaussseidel_unbuilt(SReal *dfree, SReal *f, std::v
}


for (unsigned int i=0; i<l_constraintCorrections.size(); i++)
for (const auto& cc : l_constraintCorrections)
{
core::behavior::BaseConstraintCorrection* cc = l_constraintCorrections[i];
cc->resetForUnbuiltResolution(f, contact_sequence);
}

Expand Down Expand Up @@ -1009,9 +1007,8 @@ int LCPConstraintSolver::lcp_gaussseidel_unbuilt(SReal *dfree, SReal *f, std::ve
}


for (unsigned int i=0; i<l_constraintCorrections.size(); i++)
for (const auto& cc : l_constraintCorrections)
{
core::behavior::BaseConstraintCorrection* cc = l_constraintCorrections[i];
cc->resetForUnbuiltResolution(f, contact_sequence);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,24 @@ void FixedProjectiveConstraint<Rigid3Types>::draw(const core::visual::VisualPara

if (d_fixAll.getValue())
{
for (unsigned i = 0; i < x.size(); i++)
points.push_back(x[i].getCenter());
for (const auto& xi : x)
points.push_back(xi.getCenter());
}
else
{
if( x.size() < indices.size() )
{
for (unsigned i=0; i<x.size(); i++ )
{
points.push_back(x[indices[i]].getCenter());
}
}
else
{
for (SetIndex::const_iterator it = indices.begin(); it != indices.end(); ++it)
points.push_back(x[*it].getCenter());
for (const unsigned int index : indices)
{
points.push_back(x[index].getCenter());
}
}
}

Expand Down Expand Up @@ -121,13 +125,13 @@ void FixedProjectiveConstraint<Rigid2Types>::draw(const core::visual::VisualPara

if(d_fixAll.getValue())
{
for (unsigned i=0; i<x.size(); i++ )
vertices.emplace_back(x[i].getCenter()[0], x[i].getCenter()[1], 0.0);
for (const auto& xi : x)
vertices.emplace_back(xi.getCenter()[0], xi.getCenter()[1], 0.0);
}
else
{
for (SetIndex::const_iterator it = indices.begin(); it != indices.end(); ++it)
vertices.emplace_back(x[*it].getCenter()[0], x[*it].getCenter()[1], 0.0);
for (const unsigned int index : indices)
vertices.emplace_back(x[index].getCenter()[0], x[index].getCenter()[1], 0.0);
}

vparams->drawTool()->drawPoints(vertices, 10, color);
Expand Down
Loading
Loading