userselection.cc 11.9 KB
Newer Older
jcl's avatar
jcl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2008-2010                                       
// e-mail:   Jean-Charles.Lambert@oamp.fr                                      
// address:  Dynamique des galaxies                                            
//           Laboratoire d'Astrophysique de Marseille                          
//           Ple de l'Etoile, site de Chteau-Gombert                         
//           38, rue Frdric Joliot-Curie                                     
//           13388 Marseille cedex 13 France                                   
//           CNRS U.M.R 6110                                                   
// ============================================================================

#include <iostream>
#include <sstream>
#include "userselection.h"
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include "assert.h"
#include "ctools.h"
jclamber's avatar
jclamber committed
20
#include "uns.h"
jcl's avatar
jcl committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
namespace uns {

// ============================================================================
// constructor                                                                 
UserSelection::UserSelection()
{
  nbody   = 0;
  nsel    = 0;
  indx    = NULL;
  pos     = 0;
}
// ============================================================================
// destructor                                                                  
UserSelection::~UserSelection()
{
36
  if (indx) {
jcl's avatar
jcl committed
37 38 39 40 41 42 43 44 45 46 47 48 49
    delete [] indx;
  }
}
// ============================================================================
// setSelection:                                                               
// according to User's selection (string select) and the differents components 
// (ranges, components) inside the snapshot, the POV is filled up.             
// the user make a selection "_sel" and  "_crv" is an array of
// available component for the snapshot.
// This function will build an array of indexes selected by the user.
// This function takes care in which order the user has selected the particles and build
// the array of selected indexes accordingly.
bool UserSelection::setSelection(const std::string _sel,
50
                                 const ComponentRangeVector * _crv,bool _nodata)
jcl's avatar
jcl committed
51
{
52 53 54 55 56 57 58 59 60 61 62 63 64 65
  nodata = _nodata; // are there data selected ?
  bool status;
  if (!nodata) {
    pos = 0; // current component selection
    select =_sel; // cop selection
    crv    = _crv;   // link component range vector
    comp_bits = 0;   // no bits yet
    assert(crv);                   // must not be NULL
    assert((*crv)[0].type=="all"); // first entry must be "all"
    nbody = (*crv)[0].n;           // #bodies max in the snapshot
    if (indx) {
      delete [] indx;
    }
    indx = new t_indexes_tab[nbody];
jcl's avatar
jcl committed
66
    for (int i=0;i<nbody;i++) {
67 68
      indx[i].i=-1;    // reset indexes
      indx[i].p=10000; // set position to high number
jcl's avatar
jcl committed
69
    }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    nsel = 0;
    min = max = -1;
    crvsel.clear();  // crv to stro selected component
    pov.clear();
    status=parse();
    if (status || 1 ) { // we force here
      t_indexes_tab * indx2 = new t_indexes_tab[nbody];
      for (int i=0;i<nbody;i++) {
        indx2[i].i=-1;    // reset indexes
        indx2[i].p=10000; // set position to high number
      }
      int ptr=0;
      for (unsigned int i=0;i<pov.size();i++) {
        for (int j=pov[i].first; j<=pov[i].last; j++) {
          indx2[ptr].i = indx[j].i;
          indx2[ptr].p = indx[j].p;
          assert(indx2[ptr].i!=-1);
          assert(indx[j].p == (int) i);
          assert(ptr<nbody);
          ptr++;
        }
jcl's avatar
jcl committed
91
      }
92 93 94 95 96 97
      //assert(ptr==nbody);

      delete [] indx;
      indx=indx2;
      // resise crvsel
      crvResize(crvsel);
jcl's avatar
jcl committed
98
    }
99
  } else { // parse comp_bits with no_data
jclamber's avatar
jclamber committed
100
    select_order.clear();
101
    status=parse();
jclamber's avatar
jclamber committed
102 103 104 105 106 107 108 109
    if (select_order.size()==1 && select_order[0]==-1) { // "all" has been selected
      // set to default
      select_order.clear();
      for (int i=0;i<6;i++) {
        select_order.push_back(i);
      }

    }
jcl's avatar
jcl committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
  return status;
}
// ============================================================================
// parse:                                                                      
bool UserSelection::parse()
{
  bool status=true;
  std::string current_s,next_s;
  next_s = select;
  while ((current_s=parseString(next_s)) != "") {// && (status)) {  // || 1 force parsing
    status=checkComponent(current_s);
  }
  return status;
}
// ============================================================================
// checkComponent                                                              
// find out the component stored in the string                                 
bool UserSelection::checkComponent(const std::string comp)
{
  bool status=true;
  int fail;
  if ((fail=isRange(comp)))
    if((fail=isComponent(comp)))
      status=false;
  return status;
}
// ============================================================================
// isRange                                                                     
// return true is the component is a range of particles                        
int UserSelection::isRange(const std::string comp)
{
  int status;
  std::vector<int> store;
  int ppos=0;
  bool stop=false;
  int cpt=0;
  while (! stop) {
    size_t found = comp.find(':',ppos);
    if (found!=std::string::npos) {
      if (found > (size_t) (ppos)) {
151 152 153 154 155 156
        cpt++;
        std::string str=comp.substr(ppos,found-ppos);
        std::istringstream ss(str);
        int val;
        ss>>val;
        store.push_back(val);
jcl's avatar
jcl committed
157 158 159 160
      }
      ppos=found+1; //
    } else { // no more ":"
      if (cpt>0) {
161 162 163 164 165
        std::string str=comp.substr(ppos);
        std::istringstream ss(str);
        int val;
        ss>>val;
        store.push_back(val);
jcl's avatar
jcl committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      }
      stop=true;
    }
  }
  for (std::vector<int>::iterator it=store.begin(); it!=store.end(); it++) {
    //std::cerr << " range i ="<< it-store.begin() << "\n";
    //std::cerr << " value   =" << *it << "\n";
  }
  if (cpt>0) { // one ":" has been found
    int step=1;
    int first=store[0];
    int last=first;
    if (store.size()>1) {
      last = store[1];
    }
    if (store.size()>2) {
      step = store[2];
    }
    assert(last>=first);
    int npart=last-first+1; // #part
    assert(npart<=nbody);
    fillIndexes(comp,first,last,step,pos); // fill indexes array
    pos++;
    status=0;
  }
  else {
    status=1;
  }
  return status;
}
// ============================================================================
// isComponent                                                                 
// return true is the component is component type                              
int UserSelection::isComponent(const std::string comp)
{
  int status;
  // Regular expression => all|halo|disk ......
203
  const char *  rx[] = {"all","halo","dm","disk","bulge","stars","gas","bndry","halo2",NULL};
jcl's avatar
jcl committed
204 205 206 207 208
  int i=0;
  int match=-1;
  while (rx[i] && match==-1) {
    if (rx[i]) {
      std::string tmp=rx[i];
jclamber's avatar
jclamber committed
209 210 211 212 213 214
      if (tmp == comp) {
        match=i;
        if (comp=="dm") {
          match=i-1;
        }
      }
jcl's avatar
jcl committed
215 216 217 218 219 220 221 222 223 224 225 226
    }
    i++;
  }
  if (match == -1) { // not match
    status=1;        // misformated
  }
  else {
    int first,last,step=1;
    // get component's type
    std::string type=rx[match];
    int offset;
    int icrv=ComponentRange::getIndexMatchType(crv,type,offset);
jclamber's avatar
jclamber committed
227
    if (icrv != -1 && !nodata) {
jcl's avatar
jcl committed
228 229 230 231 232 233 234 235 236 237 238 239
      assert(icrv<(int)crv->size());
      comp_bits |= tools::Ctools::compBits(type);
      first=(*crv)[icrv].first;
      last =(*crv)[icrv].last;
      assert(last>=first);
      int npart=last-first+1; // #part
      assert(npart<=nbody);
      status=0;
      fillIndexes(comp,first,last,step,pos); // fill indexes array
      pos++;
    }
    else {
jclamber's avatar
jclamber committed
240 241 242
      if (nodata) { // no data known yet (like ramses)
        comp_bits |= tools::Ctools::compBits(type); // one more bits component
        select_order.push_back(CunsIn::s_mapCompInt[comp]); // save order
243 244 245
      } else {
        status=4;      // type does not exist
      }
jcl's avatar
jcl committed
246 247 248 249 250 251 252 253 254 255 256 257 258
    }
  }
  return status;
}
// ============================================================================
// fillIndexes                                                                 
// fill indexes array with new range of particles                              
void UserSelection::fillIndexes(const std::string comp,const int first, const int last, const int step, int pos)
{
  int npart=(last-first+1)/step;
  assert(npart<=nbody);
  for (int i=first; i<=last; i+=step) {
    if (indx[i].i==-1) nsel++; // one more particles
259
    indx[i].i = i;             // set new particles
jcl's avatar
jcl committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    indx[i].p = pos;
    assert(nsel<=nbody);
  }
  // add a new object in the structure
  ParticlesObject po;
  po.first=first;
  po.last=last;
  po.step=step;
  po.npart=last-first+1;
  po.pos=pos;
  pov.push_back(po);
  
  // store selected component in a CRV vector
  uns::ComponentRange cr;
  cr.setData(first,last);
  cr.setType(comp);
  cr.setPosition(pos);
  crvsel.push_back(cr);
  
  int nlast=first+npart-1;
  // step =1  by default now
  findMinMax(first,nlast); // find min and max for later processing
282 283
  // like resizing cvrsel according user selected
  // data
jcl's avatar
jcl committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}
// ============================================================================
// parseString                                                                 
// return the string at the position after the next ',' otherwise ""           
std::string UserSelection::parseString(std::string & next_string)
{
  std::string return_string;
  std::string::size_type coma=next_string.find(",",0);  // try to find ","
  if (coma != std::string::npos) { // found ","
    return_string = next_string.substr(0,coma);
    next_string   = next_string.substr(coma+1,next_string.length());
  } else {                         // not found
    return_string = next_string;
    next_string = "";
  }
  return return_string;
}
// ============================================================================
// findMinMax                                                                  
//                                                                             
void UserSelection::findMinMax(const int first, const int last)
{
  if (min == -1 ) min=first;
  if (max == -1 ) max=last;
  if (first<min) {
    min = first;
    max = last;
  }
}
// ============================================================================
// crvResize                                                                  
//                                                                             
void UserSelection::crvResize(ComponentRangeVector & mycrv)
{
318 319
  //  std::cerr << "BEFORE Listing of the Users' selectef CRV\n";
  //  ComponentRange::list(&mycrv);
jcl's avatar
jcl committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  
  // sort mycrv according to its smaller "first" field
  std::sort(mycrv.begin(),mycrv.end(),ComponentRange::compareFirst);
  
  int new_max=max;
  int next_first=0;
  // p
  for (std::vector<ComponentRange>::iterator icrv=mycrv.begin(); icrv<mycrv.end();icrv++) {
    new_max = crvPermut((*icrv),min,new_max,next_first);
  }
  // sort mycrv according to its initial "positions"
  std::sort(mycrv.begin(),mycrv.end(),ComponentRange::comparePos);
  
  //
  //std::cerr << "AFTER Listing of the Users' selectef CRV\n";
  //ComponentRange::list(&mycrv);
}
// ============================================================================
// crvResize                                                                  
//                                                                             
int UserSelection::crvPermut(ComponentRange& cr,int min, int max, int &next_first)
{
  int ret;
  int npart=(cr.last-cr.first+1);
  if (cr.last > max) ret=cr.last;  // new max
  else ret=max;              // keep max

  if (cr.first <= max ) {       // first <= max
    cr.first -= min;            // left move
    cr.last=cr.first+npart-1;      // resize last
  }
  else {                     // first > max
    cr.first = next_first;      // move to the right of latest object
    cr.last=cr.first+npart-1;      // resize last
  }
  next_first=cr.last+1;
  cr.setData(cr.first,cr.last,cr.type);
357
  return ret;
jcl's avatar
jcl committed
358 359 360
}
} //namespace uns