File: random_labeled_image.h

package info (click to toggle)
cgal 4.13-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 101,504 kB
  • sloc: cpp: 703,154; ansic: 163,044; sh: 674; fortran: 616; python: 411; makefile: 115
file content (53 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (3)
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
#include <CGAL/Image_3.h>
#include <CGAL/Random.h>
#include <algorithm>

CGAL::Image_3 random_labeled_image()
{
  const int dim = 400;
  const unsigned char number_of_spheres = 50;
  const int max_radius_of_spheres = 10;
  const int radius_of_big_sphere = 80;
  _image* image = _createImage(dim, dim, dim, 1,
                               1.f, 1.f, 1.f, 1,
                               WK_FIXED, SGN_UNSIGNED);
  unsigned char* ptr = (unsigned char*)(image->data);
  std::fill(ptr, ptr+dim*dim*dim, '\0');

  std::ptrdiff_t center = dim / 2;
  CGAL::Random rand(0);
  for(unsigned char n = number_of_spheres; n > 0 ; --n) {
    std::size_t i, j, k;
    do {
      i = rand.uniform_smallint(1 + max_radius_of_spheres,
                                dim-2 - max_radius_of_spheres);
      j = rand.uniform_smallint(1 + max_radius_of_spheres,
                                dim-2 - max_radius_of_spheres);
      k = rand.uniform_smallint(1 + max_radius_of_spheres,
                                dim-2 - max_radius_of_spheres);
    } while ( ( CGAL::square(double(center) - double(i)) +
                CGAL::square(double(center) - double(j)) +
                CGAL::square(double(center) - double(k)) )
              <
              CGAL::square(double(radius_of_big_sphere) + 4 * max_radius_of_spheres) );
    std::ptrdiff_t radius = max_radius_of_spheres;
    if(n==1) {
      i = j = k = center;
      radius = radius_of_big_sphere;
    }
    for(std::ptrdiff_t ii = - radius; ii <= radius; ++ii)
    {
      for(std::ptrdiff_t jj = - radius; jj <= radius; ++jj)
      {
        for(std::ptrdiff_t kk = - radius; kk <= radius; ++kk)
        {
          if(ii*ii + jj*jj + kk*kk > radius * radius) continue;
          using CGAL::IMAGEIO::static_evaluate;
          static_evaluate<unsigned char>(image, i+ii, j+jj, k+kk) = n;
        }
      }
    }
  }
  _writeImage(image, "random-image.inr");
  return CGAL::Image_3(image);
}