SelectionByPID.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23  \ *---------------------------------------------------------------------------*/
24 
25 #include "MeshSelection.hpp"
26 
27 namespace mimmo{
28 
33  m_name = "mimmo.SelectionByPID";
34 };
35 
40 SelectionByPID::SelectionByPID(const bitpit::Config::Section & rootXML){
41 
42  m_name = "mimmo.SelectionByPID";
43 
44  std::string fallback_name = "ClassNONE";
45  std::string input = rootXML.get("ClassName", fallback_name);
46  input = bitpit::utils::string::trim(input);
47  if(input == "mimmo.SelectionByPID"){
48  absorbSectionXML(rootXML);
49  }else{
51  };
52 }
53 
60  m_name = "mimmo.SelectionByPID";
61  setGeometry(target);
62  setPID(pidlist);
63 };
64 
69 
74  m_activePID = other.m_activePID;
75  m_setPID = other.m_setPID;
76 };
77 
82  swap(other);
83  return *this;
84 };
85 
92 {
93  std::swap(m_setPID, x.m_setPID);
94  std::swap(m_activePID, x.m_activePID);
96 }
97 
101 void
103 
104 
106  bool built = m_arePortsBuilt;
107 
108  built = (built && createPortIn<long, SelectionByPID>(this, &SelectionByPID::setPID, M_VALUELI));
109  built = (built && createPortIn<std::vector<long>, SelectionByPID>(this, &SelectionByPID::setPID, M_VECTORLI2));
110 
111  m_arePortsBuilt = built;
112 };
113 
114 
121  livector1D result(m_activePID.size());
122  int counter = 0;
123  for(auto && val : m_activePID){
124  result[counter] = val.first;
125  ++counter;
126  }
127  return result;
128 };
136  livector1D result;
137  for(auto && val : m_activePID){
138  if(val.second == active) result.push_back(val.first);
139  }
140  return(result);
141 };
142 
143 
148 void
150  if(target == nullptr) return;
151  if(target->getType() == 3) return; //does not work with point cloud for now.
152  m_geometry = target;
153  m_topo = target->getType();
154 
155  std::unordered_set<long> & pids = target->getPIDTypeList();
156  std::unordered_set<long>::iterator it, itE = pids.end();
157 
158  for(it = pids.begin(); it!=itE; ++it){
159  m_activePID.insert(std::make_pair(*it,false));
160  }
161 };
162 
168 void
170  if(m_setPID.count(-1) >0 || (!m_setPID.empty() && i==-1)) m_setPID.clear();
171  m_setPID.insert(i);
172 
173 };
174 
180 void
182  for(auto && index : list){
183  setPID(index);
184  }
185 };
193 void
195  if(i>0){
196  if(m_setPID.count(i) >0) m_setPID.erase(i);
197  }else{
198  m_setPID.clear();
199  }
200 };
201 
207 void
209  for(auto && index : list){
210  removePID(index);
211  }
212 };
213 
217 void
219  m_subpatch.reset();
220  m_activePID.clear();
222 };
223 
230  livector1D result;
231  std::set<long> extraction;
232 
233  syncPIDList();
234  livector1D pids = getActivePID();
235 
236  for(auto && pid : pids){
237  livector1D local = getGeometry()->extractPIDCells(pid);
238  extraction.insert(local.begin(),local.end());
239  }
240 
241  //check if dual selection is triggered
242 
243  if(m_dual){
244  livector1D totID = getGeometry()->getCellsIds();
245  result.resize(totID.size() - extraction.size());
246  if(result.size() == 0) return result;
247 
248  std::sort(totID.begin(), totID.end());
249  int counter = 0;
250  auto tot_it = totID.begin();
251  auto cand_it = extraction.begin();
252  while(tot_it != totID.end()){
253  long val = *tot_it;
254  if (cand_it == extraction.end() || val != *cand_it) {
255  result[counter] = val;
256  ++counter;
257  } else {
258  ++cand_it;
259  }
260  ++tot_it;
261  }
262  }else{
263  result.insert(result.end(), extraction.begin(), extraction.end());
264  }
265  return result;
266 };
267 
273 void
275  if(getGeometry() == nullptr) return;
276 
277  if(m_setPID.count(-1) == 0){
278 
279  std::unordered_set<long>::iterator itU;
280  livector1D negative;
281  for(itU = m_setPID.begin(); itU != m_setPID.end(); ++itU){
282  long value = *itU;
283  if(m_activePID.count(value) > 0) m_activePID[value] = true;
284  else negative.push_back(value);
285  }
286 
287  for(auto &val: negative) m_setPID.erase(val);
288 
289 
290  }else{
291  m_setPID.clear();
292  for(auto &val: m_activePID ){
293  m_setPID.insert(val.first);
294  val.second = true;
295 
296  }
297  }
298 }
299 
305 void
306 SelectionByPID::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
307 
308  BITPIT_UNUSED(name);
309  //start absorbing
310  BaseManipulation::absorbSectionXML(slotXML, name);
311 
312  if(slotXML.hasOption("Dual")){
313  std::string input = slotXML.get("Dual");
314  input = bitpit::utils::string::trim(input);
315  bool value = false;
316  if(!input.empty()){
317  std::stringstream ss(input);
318  ss >> value;
319  }
320  setDual(value);
321  }
322 
323  int nPID = 0;
324  if(slotXML.hasOption("nPID")){
325  std::string input = slotXML.get("nPID");
326  input = bitpit::utils::string::trim(input);
327  nPID = 0;
328  if(!input.empty()){
329  std::stringstream ss(input);
330  ss>>nPID;
331  std::max(0, nPID);
332  }
333  }
334  livector1D pidlist(nPID, -1);
335 
336  if(slotXML.hasOption("PID")){
337  std::string input = slotXML.get("PID");
338  input = bitpit::utils::string::trim(input);
339  if(!input.empty()){
340  std::stringstream ss(input);
341  for(auto &val: pidlist){
342  ss>>val;
343  }
344  }
345  }
346 
347  setPID(pidlist);
348 
349 };
350 
356 void
357 SelectionByPID::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
358 
359  BITPIT_UNUSED(name);
360 
361  BaseManipulation::flushSectionXML(slotXML, name);
362  int value = m_dual;
363  slotXML.set("Dual", std::to_string(value));
364 
365 
366  livector1D selected = getActivePID(true);
367  int size = selected.size();
368 
369 
370  if(size != 0){
371  slotXML.set("nPID", std::to_string(size));
372 
373  std::stringstream ss;
374  for(int i=0; i<size; ++i){
375  ss<<selected[i]<<'\t';
376  }
377 
378  slotXML.set("PID", ss.str());
379  }
380 
381 };
382 
383 }
Abstract Interface for selection classes.
void setPID(long i=-1)
livector1D extractSelection()
void removePID(long i=-1)
SelectionByPID & operator=(SelectionByPID other)
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
std::vector< long > livector1D
mimmo::MimmoSharedPointer< MimmoObject > m_subpatch
void warningXML(bitpit::Logger *log, std::string name)
livector1D getActivePID(bool active=true)
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
void swap(SelectionByPID &) noexcept
Selection using target mesh Part Identifiers.
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
void swap(GenericSelection &x) noexcept
#define M_VALUELI
#define M_VECTORLI2
void setDual(bool flag=false)
void setGeometry(mimmo::MimmoSharedPointer< MimmoObject >)
MimmoSharedPointer is a custom implementation of shared pointer.
MimmoSharedPointer< MimmoObject > getGeometry()
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")