FrontISTR  6.0.0
Large-scale structural analysis program with finit element method
hecmw_vis_generate_histogram_vr.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include "hecmw_vis_bmp.h"
12 #include "hecmw_vis_font_texture.h"
13 #include "hecmw_vis_mem_util.h"
14 #include "hecmw_malloc.h"
15 
16 void find_color_minmax_vr(double *var, int *empty_flag, int nx, int ny, int nz,
17  double *mincolor, double *maxcolor) {
18  int i;
19  double value;
20 
21  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
22  if (empty_flag[i] == 1) {
23  value = var[i];
24  if (value < *mincolor) *mincolor = value;
25  if (value > *maxcolor) *maxcolor = value;
26  }
27  }
28  return;
29 }
30 void generate_histogram_graph_vr(double tmincolor, double tmaxcolor,
31  double *var, int *empty_flag, int nx, int ny,
32  int nz, int mynode, int pesize,
33  HECMW_Comm VIS_COMM, int color_system_type) {
34  int i, j, k, m;
35  double delta, value, color[3];
36  int count[500], t_count[500], max_number, max_length, start_x, end_x, start_y;
37  FILE *fp;
38  double *graph;
39  BITMAPFILEHEADER header; /* File header */
40 
41  unsigned char r, g, b;
42  int ri, gi, bi;
43  BITMAPINFOHEADER info;
44 
45  int start_xs, start_ys;
46  char buf[128];
47  int output7[7][7];
48 
49  delta = (tmaxcolor - tmincolor) / 500.0;
50  for (i = 0; i < 500; i++) {
51  count[i] = 0;
52  t_count[i] = 0;
53  }
54  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
55  if (empty_flag[i] == 1) {
56  j = (int)((var[i] - tmincolor) / delta);
57  if (j < 0) j = 0;
58  if (j > 499) j = 499;
59  count[j]++;
60  }
61  }
62  if (pesize > 1)
63  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
64  else {
65  for (i = 0; i < 500; i++) t_count[i] = count[i];
66  }
67  if (mynode == 0) {
68  fp = fopen("histogram.bmp", "wb");
69  if (fp == NULL)
70  HECMW_vis_print_exit("Cannot generate the histogram output file");
71  graph = (double *)HECMW_calloc(400 * 530 * 3, sizeof(double));
72  if (graph == NULL) HECMW_vis_memory_exit("graph");
73  for (i = 0; i < 400 * 530 * 3; i++) graph[i] = 0.0;
74  max_number = 0;
75  for (i = 0; i < 500; i++) {
76  if (t_count[i] > max_number) max_number = t_count[i];
77  }
78  if (max_number == 0)
80  "ERROR: HEC-MW-VIS-E2003:Cannot generate histogram graph, the number "
81  "of voxels is 0");
82  max_length = (int)(400 - 30 - 5 - 45 * 1.5);
83  start_x = (int)(5 + 45 * 1.5 + 15);
84  start_y = 15;
85  for (j = 0; j < 500; j++) {
86  end_x =
87  (int)((double)t_count[j] * (double)max_length / (double)max_number +
88  start_x) +
89  2;
90  value = j / 500.0;
91  value2_to_rgb(value, color, color_system_type);
92  for (i = start_x; i < end_x; i++) {
93  graph[((j + 15) * 400 + i) * 3] = color[0];
94  graph[((j + 15) * 400 + i) * 3 + 1] = color[1];
95  graph[((j + 15) * 400 + i) * 3 + 2] = color[2];
96  }
97  }
98  /*start mark scales */
99  start_y = 15;
100  for (k = 0; k < 11; k++) {
101  value = tmincolor + (tmaxcolor - tmincolor) / 10.0 * k;
102  start_ys = start_y + (int)((double)500.0 / 10 * k) - (int)7 / 2;
103  start_xs = 15;
104  snprintf(buf, sizeof(buf), "%3.2E", value);
105  for (m = 0; m < 9; m++) {
106  font7_generate(buf[8 - m], output7);
107  for (j = 0; j < 7; j++)
108  for (i = 0; i < 7; i++) {
109  graph[((start_ys + j) * 400 + start_xs - i) * 3] =
110  (double)output7[6 - j][i];
111  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 1] =
112  (double)output7[6 - j][i];
113  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 2] =
114  (double)output7[6 - j][i];
115  }
116  start_xs += 7;
117  if ((value >= 0) && (m == 0)) start_xs -= 7;
118  }
119  if ((k != 0) && (k != 10)) {
120  start_ys += (int)7 / 2;
121 
122  for (i = start_x; i < start_x + 5; i++)
123  for (j = 0; j < 3; j++) graph[(start_ys * 400 + i) * 3 + j] = 1.0;
124  }
125  }
126  header.bfSize = 54 + 3 * 400 * 530;
127 #ifdef CONVERSE_ORDER
128  header.bfSize = change_unsigned_int_order(54 + 3 * 400 * 530);
129 #endif
130  header.bfReserved1 = 0;
131 #ifdef CONVERSE_ORDER
133 #endif
134 
135  header.bfReserved2 = 0;
136 #ifdef CONVERSE_ORDER
138 #endif
139 
140  header.bfOffBits = 54;
141 #ifdef CONVERSE_ORDER
143 #endif
144 
145  info.biBitCount = 24;
146 #ifdef CONVERSE_ORDER
148 #endif
149 
150  info.biSize = 40;
151 #ifdef CONVERSE_ORDER
153 #endif
154 
155  info.biWidth = 400;
156 #ifdef CONVERSE_ORDER
157  info.biWidth = change_int_order(400);
158 #endif
159 
160  info.biHeight = 530;
161 #ifdef CONVERSE_ORDER
162  info.biHeight = change_int_order(530);
163 #endif
164 
165  info.biSizeImage = 3 * 400 * 530;
166 #ifdef CONVERSE_ORDER
167  info.biSizeImage = change_unsigned_int_order(3 * 400 * 530);
168 #endif
169 
170  info.biClrImportant = 0;
171 #ifdef CONVERSE_ORDER
173 #endif
174 
175  info.biClrUsed = 0;
176 #ifdef CONVERSE_ORDER
178 #endif
179 
180  info.biCompression = 0;
181 #ifdef CONVERSE_ORDER
183 #endif
184 
185  info.biPlanes = 1;
186 #ifdef CONVERSE_ORDER
188 #endif
189 
190  info.biXPelsPerMeter = 3780;
191 #ifdef CONVERSE_ORDER
192  info.biXPelsPerMeter = change_int_order(3780);
193 #endif
194 
195  info.biYPelsPerMeter = 3780;
196 #ifdef CONVERSE_ORDER
197  info.biYPelsPerMeter = change_int_order(3780);
198 #endif
199 
200  putc('B', fp);
201  putc('M', fp);
202  fwrite(&(header.bfSize), sizeof(unsigned int), 1, fp);
203  fwrite(&header.bfReserved1, sizeof(unsigned short int), 1, fp);
204  fwrite(&header.bfReserved2, sizeof(unsigned short int), 1, fp);
205  fwrite(&header.bfOffBits, sizeof(unsigned int), 1, fp);
206  fwrite(&info, 40, 1, fp);
207  for (j = 0; j < 530; j++)
208  for (i = 400 - 1; i >= 0; i--) {
209  ri = (int)(graph[(j * 400 + i) * 3] * 256);
210  gi = (int)(graph[(j * 400 + i) * 3 + 1] * 256);
211  bi = (int)(graph[(j * 400 + i) * 3 + 2] * 256);
212  if (ri < 0) ri = 0;
213  if (ri > 255) ri = 255;
214  if (gi < 0) gi = 0;
215  if (gi > 255) gi = 255;
216  if (bi < 0) bi = 0;
217  if (bi > 255) bi = 255;
218  r = ri;
219  g = gi;
220  b = bi;
221  putc(b, fp);
222  putc(g, fp);
223  putc(r, fp);
224  }
225 
226  fclose(fp);
227  HECMW_free(graph);
228  }
229  return;
230 }
231 
232 void generate_interval_point_vr(double tmincolor, double tmaxcolor, double *var,
233  int *empty_flag, int nx, int ny, int nz,
234  int mynode, int pesize, HECMW_Comm VIS_COMM,
235  double *interval_point) {
236  int i, j;
237  double delta;
238  int count[500], t_count[500], tmp_count[500], sum_count, interv_count, sum,
239  current_j;
240  delta = (tmaxcolor - tmincolor) / 500.0;
241  for (i = 0; i < 500; i++) {
242  count[i] = 0;
243  t_count[i] = 0;
244  }
245  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
246  if (empty_flag[i] == 1) {
247  j = (int)((var[i] - tmincolor) / delta);
248  if (j < 0) j = 0;
249  if (j > 499) j = 499;
250  count[j]++;
251  }
252  }
253  if (pesize > 1)
254  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
255  else {
256  for (i = 0; i < 500; i++) t_count[i] = count[i];
257  }
258  sum_count = 0;
259  for (i = 0; i < 500; i++) {
260  sum_count += t_count[i];
261  tmp_count[i] = t_count[i];
262  }
263  interv_count = (int)sum_count / 10;
264  current_j = 0;
265  interval_point[0] = tmincolor;
266  interval_point[1] = 0.0;
267  for (i = 1; i < 10; i++) {
268  interval_point[i * 2 + 1] = (double)i / 10.0;
269  sum = 0;
270  j = current_j;
271  while ((j < 500) && (sum < interv_count)) {
272  sum += t_count[j];
273  j++;
274  }
275  j--;
276  interval_point[i * 2] =
277  ((double)j / 500.0 +
278  1.0 / 500.0 *
279  (1.0 - (double)(sum - interv_count) / (double)tmp_count[j])) *
280  (tmaxcolor - tmincolor) +
281  tmincolor;
282  t_count[j] = sum - interv_count;
283  current_j = j;
284  }
285  interval_point[20] = tmaxcolor;
286  interval_point[21] = 1.0;
287  fprintf(stderr, "The automatic color mapping set is :\n");
288  for (i = 0; i < 11; i++)
289  fprintf(stderr, "%lf %lf \n", interval_point[i * 2],
290  interval_point[i * 2 + 1]);
291  return;
292 }
293 
294 void output_histogram_vr(double tmincolor, double tmaxcolor, double *var,
295  int *empty_flag, int nx, int ny, int nz, int mynode,
296  int pesize, HECMW_Comm VIS_COMM) {
297  int i, j;
298  double delta;
299  int count[100], t_count[100];
300  FILE *fp;
301 
302  delta = (tmaxcolor - tmincolor) / 100.0;
303  for (i = 0; i < 100; i++) {
304  count[i] = 0;
305  t_count[i] = 0;
306  }
307  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
308  if (empty_flag[i] == 1) {
309  j = (int)((var[i] - tmincolor) / delta);
310  if (j < 0) j = 0;
311  if (j > 99) j = 99;
312  count[j]++;
313  }
314  }
315  if (pesize > 1)
316  HECMW_Allreduce(count, t_count, 100, HECMW_INT, HECMW_SUM, VIS_COMM);
317  else {
318  for (i = 0; i < 100; i++) t_count[i] = count[i];
319  }
320  if (mynode == 0) {
321  fp = fopen("histogram.file", "w");
322  if (fp == NULL)
323  HECMW_vis_print_exit("Cannot generate the histogram output file");
324  for (i = 0; i < 100; i++)
325  fprintf(fp, "%d %d -----(%lf --- %lf)\n", i, t_count[i],
326  tmincolor + i * delta, tmincolor + (i + 1) * delta);
327  fclose(fp);
328  }
329  return;
330 }
331 
332 void find_minmax_vr(double *voxel_dxyz, double *voxel_orig_xyz, int mynode,
333  double range[6]) {
334  int i;
335  for (i = 0; i < 6; i++)
336  range[i] = voxel_orig_xyz[mynode * 3 + i / 2] +
337  (i % 2) * voxel_dxyz[mynode * 3 + i / 2];
338  return;
339 }
340 
341 void find_dis_minmax(double view_point_d[3], double vertex[24],
342  double dis_minmax[2]) {
343  int i;
344  double dis;
345  dis_minmax[0] = dis_minmax[1] =
346  sqrt(SQR(vertex[0]) + SQR(vertex[1]) + SQR(vertex[2]));
347  for (i = 0; i < 8; i++) {
348  dis = sqrt(SQR(vertex[i * 3] - view_point_d[0]) +
349  SQR(vertex[i * 3 + 1] - view_point_d[1]) +
350  SQR(vertex[i * 3 + 2] - view_point_d[2]));
351  if (dis < dis_minmax[0]) dis_minmax[0] = dis;
352  if (dis > dis_minmax[1]) dis_minmax[1] = dis;
353  }
354  return;
355 }
356 
357 void find_feap_minmax(int num_of_features, double *fea_point, double mincolor,
358  double maxcolor, double feap_minmax[2]) {
359  int i, j;
360  double t, mint, color;
361 
362  for (i = 0; i <= 255; i++) {
363  color = mincolor + (maxcolor - mincolor) * i / 255.0;
364  mint = 1.0E17;
365  for (j = 0; j < num_of_features; j++) {
366  t = fabs(color - fea_point[j]);
367  if (t < mint) mint = t;
368  }
369  if (mint > feap_minmax[1]) feap_minmax[1] = mint;
370  }
371  feap_minmax[0] = 0.0;
372  return;
373 }
374 
375 void find_feai_minmax(int num_of_features, double *fea_point, double mincolor,
376  double maxcolor, double feai_minmax[2]) {
377  int i, j;
378  double t, t1, t2, mint, color;
379 
380  for (i = 0; i <= 255; i++) {
381  color = mincolor + (maxcolor - mincolor) * i / 255.0;
382  mint = 1.0E17;
383  for (j = 0; j < num_of_features; j++) {
384  if ((t >= fea_point[j * 2]) && (t <= fea_point[j * 2 + 1]))
385  t = 0.0;
386  else {
387  t1 = fabs(color - fea_point[j * 2]);
388  t2 = fabs(color - fea_point[j * 2 + 1]);
389  if (t1 < t2)
390  t = t1;
391  else
392  t = t2;
393  }
394  if (t < mint) mint = t;
395  }
396  if (mint > feai_minmax[1]) feai_minmax[1] = mint;
397  }
398  feai_minmax[0] = 0.0;
399  return;
400 }
int HECMW_Allreduce(void *sendbuf, void *recvbuf, int count, HECMW_Datatype datatype, HECMW_Op op, HECMW_Comm comm)
Definition: hecmw_comm.c:404
#define HECMW_INT
Definition: hecmw_config.h:48
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
#define HECMW_SUM
Definition: hecmw_config.h:60
#define NULL
#define HECMW_calloc(nmemb, size)
Definition: hecmw_malloc.h:21
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
int change_int_order(int n)
void value2_to_rgb(double value, double color[3], int color_system_type)
void font7_generate(char input, int output[7][7])
unsigned short int change_short_int_order(unsigned short int n)
unsigned int change_unsigned_int_order(unsigned int n)
void generate_interval_point_vr(double tmincolor, double tmaxcolor, double *var, int *empty_flag, int nx, int ny, int nz, int mynode, int pesize, HECMW_Comm VIS_COMM, double *interval_point)
void find_feai_minmax(int num_of_features, double *fea_point, double mincolor, double maxcolor, double feai_minmax[2])
void find_color_minmax_vr(double *var, int *empty_flag, int nx, int ny, int nz, double *mincolor, double *maxcolor)
void find_dis_minmax(double view_point_d[3], double vertex[24], double dis_minmax[2])
void find_feap_minmax(int num_of_features, double *fea_point, double mincolor, double maxcolor, double feap_minmax[2])
void output_histogram_vr(double tmincolor, double tmaxcolor, double *var, int *empty_flag, int nx, int ny, int nz, int mynode, int pesize, HECMW_Comm VIS_COMM)
void generate_histogram_graph_vr(double tmincolor, double tmaxcolor, double *var, int *empty_flag, int nx, int ny, int nz, int mynode, int pesize, HECMW_Comm VIS_COMM, int color_system_type)
void find_minmax_vr(double *voxel_dxyz, double *voxel_orig_xyz, int mynode, double range[6])
void HECMW_vis_print_exit(char *var)
void HECMW_vis_memory_exit(char *var)
#define SQR(x)
double * voxel_dxyz
double * voxel_orig_xyz
unsigned short bfReserved1
Definition: hecmw_vis_bmp.h:12
unsigned short bfReserved2
Definition: hecmw_vis_bmp.h:13
unsigned int bfSize
Definition: hecmw_vis_bmp.h:11
unsigned int bfOffBits
Definition: hecmw_vis_bmp.h:14
unsigned int biCompression
Definition: hecmw_vis_bmp.h:25
unsigned int biSizeImage
Definition: hecmw_vis_bmp.h:26
unsigned short biBitCount
Definition: hecmw_vis_bmp.h:24
unsigned short biPlanes
Definition: hecmw_vis_bmp.h:23
unsigned int biClrUsed
Definition: hecmw_vis_bmp.h:29
unsigned int biSize
Definition: hecmw_vis_bmp.h:20
unsigned int biClrImportant
Definition: hecmw_vis_bmp.h:30