mightymandel v16

GPU-based Mandelbrot set explorer

find_ref.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <mpfr.h>
14 
15 #include "image.h"
16 #include "logging.h"
17 #include "utility.h"
18 #include "find_ref.h"
19 
26 enum find_ref_t find_ref(struct ref_set *refs, struct blob_set *blobset, int width, int height, const mpfr_t centerx, const mpfr_t centery, const mpfr_t radius, double max_glitch, int max_blob) {
27  float *glitched = image_get_glitched(width, height);
28  int blob_count = 0;
29  struct blob *blobs = find_blobs1(&blob_count, width, height, glitched);
30  enum find_ref_t retval = find_ref_failure;
31  if (blobs) {
32  int count = 0;
33  for (int i = 0; i < blob_count; ++i) {
34  count += blobs[i].count;
35  }
36  double percent = 100 * count / (double) (width * height);
37  log_message(LOG_INFO, "glitched pixels: %8d/%d (%f%%)\n", count, width * height, percent);
38  log_message(LOG_INFO, "largest blob: %d\n", blobs[0].count);
39  if (percent <= max_glitch || blobs[0].count <= max_blob) {
40  retval = find_ref_complete;
41  } else {
42  mpfr_t cx, cy;
43  mpfr_inits2(53, cx, cy, (mpfr_ptr) 0);
44  for (int i = 0; i < blob_count; ++i) {
45  int blob_count2 = 0;
46  struct blob *blobs2 = find_blobs2(&blob_count2, width, height, glitched, &blobs[i]);
47  if (blobs2) {
48  for (int j = 0; j < blob_count2; ++j) {
49  if (! blob_set_contains(blobset, &blobs2[j])) {
50  blob_set_insert(blobset, &blobs2[j]);
51  int x = round(blobs2[j].i / (double) blobs2[j].count);
52  int y = round(blobs2[j].j / (double) blobs2[j].count);
53  pixel_coordinate(cx, cy, width, height, centerx, centery, radius, x + 0.5, y + 0.5);
54  if (! ref_set_contains(refs, cx, cy)) {
55  ref_set_insert(refs, cx, cy);
56  retval = find_ref_success;
57  break;
58  }
59  }
60  }
61  free(blobs2);
62  }
63  if (retval == find_ref_success) {
64  break;
65  }
66  }
67  mpfr_clears(cx, cy, (mpfr_ptr) 0);
68  }
69  free(blobs);
70  } else {
71  retval = find_ref_complete;
72  }
73  free(glitched);
74  return retval;
75 }
76 
77 #if 0
78  // find highest iteration glitched pixels in image
79  float *glitched = image_get_glitched(width, height);
80  float *iterations = image_get_iterations(width, height);
81  int count = 0;
82  struct pixel *pxs = image_find_peak_glitches(glitched, iterations, width, height, &count);
83  double percent = 100 * count / (double) (width * height);
84  log_message(LOG_INFO, "glitched pixels: %8d/%d (%f%%)\n", count, width * height, percent);
85  free(glitched);
86  free(iterations);
87  // find a glitched pixel not already used as a reference
88  enum find_ref_t retval = find_ref_failure;
89  if (count) {
90  mpfr_t cx, cy;
91  mpfr_inits2(53, cx, cy, (mpfr_ptr) 0);
92  for (int k = 0; k < count; ++k) {
93  pixel_coordinate(cx, cy, width, height, centerx, centery, radius, pxs[k].i + 0.5, pxs[k].j + 0.5);
94  if (! ref_set_contains(refs, cx, cy)) {
95  ref_set_insert(refs, cx, cy);
96  retval = find_ref_success;
97  break;
98  }
99  }
100  mpfr_clears(cx, cy, (mpfr_ptr) 0);
101  } else {
102  retval = find_ref_complete;
103  }
104  free(pxs);
105  return retval;
106 }
107 #endif
108 
109 #if 0
110  // find reference points in blobs, largest first
111  enum find_ref_t retval;
112  if (index) {
113  qsort(blobs, index, sizeof(struct blob), cmp_blob_count_desc);
114  if (blobs[0].count > 0) {
115  retval = find_ref_failure;
116  for (int i = 0; i < index; ++i) {
117  if (blob_set_contains(blobset, blobs[i].count, blobs[i].i, blobs[i].j)) {
118  continue;
119  }
120  if (blobs[i].count > 0) {
121  blob_set_insert(blobset, blobs[i].count, blobs[i].i, blobs[i].j);
122  mpfr_t cx, cy, r;
123  mpfr_inits2(53, cx, cy, r, (mpfr_ptr) 0);
124  int px = round(blobs[i].i / (double) blobs[i].count);
125  int py = round(blobs[i].j / (double) blobs[i].count);
126  pixel_coordinate(cx, cy, width, height, centerx, centery, radius, px + 0.5, py + 0.5);
127  if (! ref_set_contains(refs, cx, cy)) {
128  ref_set_insert(refs, cx, cy);
129  retval = find_ref_success;
130  break;
131  }
132  mpfr_clears(cx, cy, r, (mpfr_ptr) 0);
133  } else {
134  break;
135  }
136  if (retval == find_ref_success) {
137  break;
138  }
139  }
140  } else {
141  retval = find_ref_complete;
142  }
143  } else {
144  retval = find_ref_complete;
145  }
146  free(blobs); blobs = 0;
147  return retval;
148 #endif
149 
150 #if 0
151  int pixels = width * height;
152  // find highest iteration glitched pixel in highest iteration glitched region
153  int M = width / 8;
154  int N = height / 8;
155  struct pixel *reg = malloc((width + M - 1)/(M/2) * (height + N - 1)/(N/2) * sizeof(struct pixel));
156  int regs = 0;
157  for (int j = 0; j < height; j += N/2) {
158  for (int i = 0; i < width; i += M/2) {
159  int count = 0;
160  double local_total = 0;
161  double local_max = -1;
162  int local_i = 0;
163  int local_j = 0;
164  for (int jj = 0; jj < N; ++jj) {
165  int jjj = j + jj;
166  if (! (jjj < height)) { continue; }
167  for (int ii = 0; ii < M; ++ii) {
168  int iii = i + ii;
169  if (! (iii < width)) { continue; }
170  int kkk = jjj * width + iii;
171  if (data[kkk]) {
172  count += 1;
173  local_total += data_n[kkk];
174  if (data_n[kkk] > local_max) {
175  local_max = data_n[kkk];
176  local_i = i;
177  local_j = j;
178  }
179  }
180  }
181  }
182  if (count) {
183  //local_total;// /= count;
184  reg[regs].n = local_total;
185  reg[regs].i = local_i;
186  reg[regs].j = local_j;
187  regs++;
188  }
189  }
190  }
191  log_message(LOG_NOTICE, "%d\n", regs);
192 #endif