FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_vis_define_parameters_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 
6 #include "hecmw_vis_ray_trace.h"
7 #include "hecmw_vis_mem_util.h"
8 
9 #include "hecmw_font_texture.h"
10 #include "hecmw_malloc.h"
11 
12 void find_color_minmax(double *var, int *empty_flag, int nx, int ny, int nz,
13  double *mincolor, double *maxcolor) {
14  int i;
15  double value;
16 
17  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
18  if (empty_flag[i] == 1) {
19  value = var[i];
20  if (value < *mincolor) *mincolor = value;
21  if (value > *maxcolor) *maxcolor = value;
22  }
23  }
24  return;
25 }
26 void generate_histogram_graph(double tmincolor, double tmaxcolor, double *var,
27  int *empty_flag, int nx, int ny, int nz,
28  int mynode, int pesize, HECMW_Comm VIS_COMM,
29  int color_system_type) {
30  int i, j, k, m;
31  double delta, value, color[3];
32  int count[500], t_count[500], max_number, max_length, start_x, end_x, start_y,
33  end_y;
34  FILE *fp;
35  double *graph;
36  BITMAPFILEHEADER header; /* File header */
37 
38  unsigned char r, g, b;
39  int ri, gi, bi;
40  BITMAPINFOHEADER info;
41 
42  int start_xs, start_ys;
43  char buf[128];
44  int output7[7][7];
45 
46  delta = (tmaxcolor - tmincolor) / 500.0;
47  for (i = 0; i < 500; i++) {
48  count[i] = 0;
49  t_count[i] = 0;
50  }
51  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
52  if (empty_flag[i] == 1) {
53  j = (int)((var[i] - tmincolor) / delta);
54  if (j < 0) j = 0;
55  if (j > 499) j = 499;
56  count[j]++;
57  }
58  }
59  if (pesize > 1)
60  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
61  else {
62  for (i = 0; i < 500; i++) t_count[i] = count[i];
63  }
64  if (mynode == 0) {
65  fp = fopen("histogram.bmp", "wb");
66  if (fp == NULL) {
67  fprintf(stderr, "Cannot generate the histogram output file\n");
68  HECMW_vis_print_exit("Cannot generate the histogram output file");
69  }
70  graph = (double *)HECMW_calloc(400 * 530 * 3, sizeof(double));
71  if (graph == NULL) {
72  fprintf(stderr, "There is no enough memory for graph\n");
73  fclose(fp);
74  HECMW_vis_print_exit("There is no enough memory for graph");
75  }
76  for (i = 0; i < 400 * 530 * 3; i++) graph[i] = 0.0;
77  max_number = 0;
78  for (i = 0; i < 500; i++) {
79  if (t_count[i] > max_number) max_number = t_count[i];
80  }
81  if (max_number == 0) {
82  fprintf(stderr,
83  "Cannot generate histogram graph, the number of voxels is 0\n");
84  exit(0);
85  }
86  max_length = (int)(400 - 30 - 5 - 45 * 1.5);
87  start_x = (int)(5 + 45 * 1.5 + 15);
88  start_y = 15;
89  for (j = 0; j < 500; j++) {
90  end_x =
91  (int)((double)t_count[j] * (double)max_length / (double)max_number +
92  start_x) +
93  2;
94  value = j / 500.0;
95  value2_to_rgb(value, color, color_system_type);
96  for (i = start_x; i < end_x; i++) {
97  graph[((j + 15) * 400 + i) * 3] = color[0];
98  graph[((j + 15) * 400 + i) * 3 + 1] = color[1];
99  graph[((j + 15) * 400 + i) * 3 + 2] = color[2];
100  }
101  }
102  /*start mark scales */
103  start_y = 15;
104  end_y = 515;
105  for (k = 0; k < 11; k++) {
106  value = tmincolor + (tmaxcolor - tmincolor) / 10.0 * k;
107  start_ys = start_y + (int)((double)500.0 / 10 * k) - (int)7 / 2;
108  start_xs = 15;
109  sprintf(buf, "%3.2E", value);
110  for (m = 0; m < 9; m++) {
111  font7_generate(buf[8 - m], output7);
112  for (j = 0; j < 7; j++)
113  for (i = 0; i < 7; i++) {
114  graph[((start_ys + j) * 400 + start_xs - i) * 3] =
115  (double)output7[6 - j][i];
116  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 1] =
117  (double)output7[6 - j][i];
118  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 2] =
119  (double)output7[6 - j][i];
120  }
121  start_xs += 7;
122  if ((value >= 0) && (m == 0)) start_xs -= 7;
123  }
124  if ((k != 0) && (k != 10)) {
125  start_ys += (int)7 / 2;
126 
127  for (i = start_x; i < start_x + 5; i++)
128  for (j = 0; j < 3; j++) graph[(start_ys * 400 + i) * 3 + j] = 1.0;
129  }
130  }
131  header.bfSize = 54 + 3 * 400 * 530;
132 #ifdef CONVERSE_ORDER
133  header.bfSize = change_unsigned_int_order(54 + 3 * 400 * 530);
134 #endif
135  header.bfReserved1 = 0;
136 #ifdef CONVERSE_ORDER
138 #endif
139 
140  header.bfReserved2 = 0;
141 #ifdef CONVERSE_ORDER
143 #endif
144 
145  header.bfOffBits = 54;
146 #ifdef CONVERSE_ORDER
148 #endif
149 
150  info.biBitCount = 24;
151 #ifdef CONVERSE_ORDER
153 #endif
154 
155  info.biSize = 40;
156 #ifdef CONVERSE_ORDER
158 #endif
159 
160  info.biWidth = 400;
161 #ifdef CONVERSE_ORDER
162  info.biWidth = change_int_order(400);
163 #endif
164 
165  info.biHeight = 530;
166 #ifdef CONVERSE_ORDER
167  info.biHeight = change_int_order(530);
168 #endif
169 
170  info.biSizeImage = 3 * 400 * 530;
171 #ifdef CONVERSE_ORDER
172  info.biSizeImage = change_unsigned_int_order(3 * 400 * 530);
173 #endif
174 
175  info.biClrImportant = 0;
176 #ifdef CONVERSE_ORDER
178 #endif
179 
180  info.biClrUsed = 0;
181 #ifdef CONVERSE_ORDER
183 #endif
184 
185  info.biCompression = 0;
186 #ifdef CONVERSE_ORDER
188 #endif
189 
190  info.biPlanes = 1;
191 #ifdef CONVERSE_ORDER
193 #endif
194 
195  info.biXPelsPerMeter = 3780;
196 #ifdef CONVERSE_ORDER
197  info.biXPelsPerMeter = change_int_order(3780);
198 #endif
199 
200  info.biYPelsPerMeter = 3780;
201 #ifdef CONVERSE_ORDER
202  info.biYPelsPerMeter = change_int_order(3780);
203 #endif
204 
205  putc('B', fp);
206  putc('M', fp);
207  fwrite(&(header.bfSize), sizeof(unsigned int), 1, fp);
208  fwrite(&header.bfReserved1, sizeof(unsigned short int), 1, fp);
209  fwrite(&header.bfReserved2, sizeof(unsigned short int), 1, fp);
210  fwrite(&header.bfOffBits, sizeof(unsigned int), 1, fp);
211  fwrite(&info, 40, 1, fp);
212  for (j = 0; j < 530; j++)
213  for (i = 400 - 1; i >= 0; i--) {
214  ri = (int)(graph[(j * 400 + i) * 3] * 256);
215  gi = (int)(graph[(j * 400 + i) * 3 + 1] * 256);
216  bi = (int)(graph[(j * 400 + i) * 3 + 2] * 256);
217  if (ri < 0) ri = 0;
218  if (ri > 255) ri = 255;
219  if (gi < 0) gi = 0;
220  if (gi > 255) gi = 255;
221  if (bi < 0) bi = 0;
222  if (bi > 255) bi = 255;
223  r = ri;
224  g = gi;
225  b = bi;
226  putc(b, fp);
227  putc(g, fp);
228  putc(r, fp);
229  }
230 
231  fclose(fp);
232  HECMW_free(graph);
233  }
234  return;
235 }
236 
237 void generate_interval_point(double tmincolor, double tmaxcolor, double *var,
238  int *empty_flag, int nx, int ny, int nz,
239  int mynode, int pesize, HECMW_Comm VIS_COMM,
240  double *interval_point) {
241  int i, j;
242  double delta;
243  int count[500], t_count[500], tmp_count[500], sum_count, interv_count, sum,
244  current_j;
245  delta = (tmaxcolor - tmincolor) / 500.0;
246  for (i = 0; i < 500; i++) {
247  count[i] = 0;
248  t_count[i] = 0;
249  }
250  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
251  if (empty_flag[i] == 1) {
252  j = (int)((var[i] - tmincolor) / delta);
253  if (j < 0) j = 0;
254  if (j > 499) j = 499;
255  count[j]++;
256  }
257  }
258  if (pesize > 1)
259  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
260  else {
261  for (i = 0; i < 500; i++) t_count[i] = count[i];
262  }
263  sum_count = 0;
264  for (i = 0; i < 500; i++) {
265  sum_count += t_count[i];
266  tmp_count[i] = t_count[i];
267  }
268  interv_count = (int)sum_count / 10;
269  current_j = 0;
270  interval_point[0] = tmincolor;
271  interval_point[1] = 0.0;
272  for (i = 1; i < 10; i++) {
273  interval_point[i * 2 + 1] = (double)i / 10.0;
274  sum = 0;
275  j = current_j;
276  while ((j < 500) && (sum < interv_count)) {
277  sum += t_count[j];
278  j++;
279  }
280  j--;
281  interval_point[i * 2] =
282  ((double)j / 500.0 +
283  1.0 / 500.0 *
284  (1.0 - (double)(sum - interv_count) / (double)tmp_count[j])) *
285  (tmaxcolor - tmincolor) +
286  tmincolor;
287  t_count[j] = sum - interv_count;
288  current_j = j;
289  }
290  interval_point[20] = tmaxcolor;
291  interval_point[21] = 1.0;
292  fprintf(stderr, "The automatic color mapping set is :\n");
293  for (i = 0; i < 11; i++)
294  fprintf(stderr, "%lf %lf \n", interval_point[i * 2],
295  interval_point[i * 2 + 1]);
296  return;
297 }
298 
299 void output_histogram(double tmincolor, double tmaxcolor, double *var,
300  int *empty_flag, int nx, int ny, int nz, int mynode,
301  int pesize, HECMW_Comm VIS_COMM) {
302  int i, j;
303  double delta;
304  int count[100], t_count[100];
305  FILE *fp;
306 
307  delta = (tmaxcolor - tmincolor) / 100.0;
308  for (i = 0; i < 100; i++) {
309  count[i] = 0;
310  t_count[i] = 0;
311  }
312  for (i = 0; i < (nx + 1) * (ny + 1) * (nz + 1); i++) {
313  if (empty_flag[i] == 1) {
314  j = (int)((var[i] - tmincolor) / delta);
315  if (j < 0) j = 0;
316  if (j > 99) j = 99;
317  count[j]++;
318  }
319  }
320  if (pesize > 1)
321  HECMW_Allreduce(count, t_count, 100, HECMW_INT, HECMW_SUM, VIS_COMM);
322  else {
323  for (i = 0; i < 100; i++) t_count[i] = count[i];
324  }
325  if (mynode == 0) {
326  fp = fopen("histogram.file", "w");
327  if (fp == NULL) {
328  fprintf(stderr, "Cannot generate the histogram output file\n");
329  HECMW_vis_print_exit("Cannot generate the histogram output file");
330  }
331  for (i = 0; i < 100; i++)
332  fprintf(fp, "%d %d -----(%lf --- %lf)\n", i, t_count[i],
333  tmincolor + i * delta, tmincolor + (i + 1) * delta);
334  fclose(fp);
335  }
336  return;
337 }
338 
339 void find_minmax(double *voxel_dxyz, double *voxel_orig_xyz, int mynode,
340  double range[6]) {
341  int i;
342  for (i = 0; i < 6; i++)
343  range[i] = voxel_orig_xyz[mynode * 3 + i / 2] +
344  (i % 2) * voxel_dxyz[mynode * 3 + i / 2];
345  return;
346 }
347 
348 void transform_range_vertex(double range[6], double vertex[24]) {
349  vertex[0 * 3] = vertex[4 * 3] = vertex[7 * 3] = vertex[3 * 3] = range[0];
350  vertex[1 * 3] = vertex[5 * 3] = vertex[6 * 3] = vertex[2 * 3] = range[1];
351  vertex[0 * 3 + 1] = vertex[1 * 3 + 1] = vertex[5 * 3 + 1] =
352  vertex[4 * 3 + 1] = range[2];
353  vertex[3 * 3 + 1] = vertex[2 * 3 + 1] = vertex[6 * 3 + 1] =
354  vertex[7 * 3 + 1] = range[3];
355  vertex[0 * 3 + 2] = vertex[1 * 3 + 2] = vertex[2 * 3 + 2] =
356  vertex[3 * 3 + 2] = range[4];
357  vertex[4 * 3 + 2] = vertex[5 * 3 + 2] = vertex[6 * 3 + 2] =
358  vertex[7 * 3 + 2] = range[5];
359  return;
360 }
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
void generate_interval_point(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_minmax(double *voxel_dxyz, double *voxel_orig_xyz, int mynode, double range[6])
void output_histogram(double tmincolor, double tmaxcolor, double *var, int *empty_flag, int nx, int ny, int nz, int mynode, int pesize, HECMW_Comm VIS_COMM)
void find_color_minmax(double *var, int *empty_flag, int nx, int ny, int nz, double *mincolor, double *maxcolor)
void generate_histogram_graph(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 transform_range_vertex(double range[6], double vertex[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 HECMW_vis_print_exit(char *var)
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