mockgallib/src

slice.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SLICE_H
#define SLICE_H 1

#include <vector>
#include <cmath>
#include "halo.h"

class Slice {
 public:
  Slice(const float boxsize[], const float sky_box[], const float center[]);
  void transform(Halo* const h) const;

  float boxsize[3];
  int n;
 private:
  int n_[3];     // number of subvolumes that fits the cuboid for each direction
  int coord_[3]; // x[coord[i]]
  float x0_[3];  // left cornor coordinate of position after slicing
};

#endif

slice.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdlib>
#include <cmath>
#include <cassert>
#include "slice.h"

using namespace std;

Slice::Slice(const float cuboid_box[], const float sky_box[], const float center[] )
{
  int n1[3], n2[3];

  // Find the number of sky_boxes that fits boxsize
  
  // x y z
  n1[1]= (int) floor(cuboid_box[1]/sky_box[1]);
  n1[2]= (int) floor(cuboid_box[2]/sky_box[2]);

  // x z y
  n2[1]= (int) floor(cuboid_box[2]/sky_box[1]);
  n2[2]= (int) floor(cuboid_box[1]/sky_box[2]);


  n_[0]= (int) floor(cuboid_box[0]/sky_box[0]);
  
  if(n2[1]*n2[2] > n1[1]*n1[2]) {
    // We can fit more slices by flipping y and z coordinates
    coord_[0]= 0; coord_[1]= 2; coord_[2]= 1;
    n_[1]= n2[1]; n_[2]= n2[2];
  }
  else {
    coord_[0]= 0; coord_[1]= 1; coord_[2]= 2;
    n_[1]= n1[1]; n_[2]= n1[2];
  }

  for(int k=0; k<3; ++k) {
    boxsize[k]= cuboid_box[k]/n_[coord_[k]];
    x0_[k]= center[k] - 0.5f*boxsize[k];
  }

  n= n_[0]*n_[1]*n_[2];
}


void Slice::transform(Halo* const h) const {
  // a copy is necessary because coord[] could change coordinates
  float x[]= {h->x[0], h->x[1], h->x[2]};
  
  int index= (int)(((floorf(x[coord_[0]]/boxsize[0])*n_[1]
		     + floorf(x[coord_[1]]/boxsize[1]))*n_[0]
		     + floorf(x[coord_[2]]/boxsize[2])));
  
  h->x[0]= x0_[0] + fmodf(x[coord_[0]], boxsize[0]);
  h->x[1]= x0_[1] + fmodf(x[coord_[1]], boxsize[1]);
  h->x[2]= x0_[2] + fmodf(x[coord_[2]], boxsize[2]);
  
  h->slice= index;
}