FrontISTR  6.0.0
Large-scale structural analysis program with finit element method
hecmw_vis_ray_trace.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 
8 #include <math.h>
9 #include "hecmw_vis_mem_util.h"
11 
12 /*
13 void transform_face_node(int ijkn[3], int current_ijk[3], int face_sect[3],
14 double vv[3],
15  int next_ijk[3]);
16  */
17 static int find_out_point(double orig_xyz[3], double r_dxyz[3], int ijk[3],
18  double in_point[3], double ray_direction[3],
19  int face_sect[3], double out_point[3]);
20 static void find_next_cell(int ijkn[3], int current_ijk[3], int face_sect[3],
21  double vv[3], int next_ijk[3]);
22 
23 static int find_first_inter_point(double view_p[3], double ray_direction[3],
24  double orig_xyz[3], double dxyz[3],
25  double first_p[3]) {
26  int i, j, mincomp;
27  double minmax[6], t[6], mint;
28 
29  for (i = 0; i < 3; i++) {
30  minmax[i * 2] = orig_xyz[i];
31  minmax[i * 2 + 1] = orig_xyz[i] + dxyz[i];
32  }
33  for (i = 0; i < 3; i++) {
34  if (fabs(ray_direction[i]) < EPSILON) {
35  t[i * 2] = 1.0E+17;
36  t[i * 2 + 1] = 1.0E+17;
37  } else {
38  t[i * 2] = (minmax[i * 2] - view_p[i]) / ray_direction[i];
39  t[i * 2 + 1] = (minmax[i * 2 + 1] - view_p[i]) / ray_direction[i];
40  }
41  }
42 
43  mint = 1.0E+17;
44  mincomp = -1;
45  for (i = 0; i < 6; i++) {
46  if ((mint > t[i]) && (t[i] > EPSILON)) {
47  for (j = 0; j < 3; j++) first_p[j] = view_p[j] + t[i] * ray_direction[j];
48  if ((first_p[0] >= minmax[0] - EPSILON) &&
49  (first_p[0] <= minmax[1] + EPSILON) &&
50  (first_p[1] >= minmax[2] - EPSILON) &&
51  (first_p[1] <= minmax[3] + EPSILON) &&
52  (first_p[2] >= minmax[4] - EPSILON) &&
53  (first_p[2] <= minmax[5] + EPSILON)) {
54  mint = t[i];
55  mincomp = i;
56  }
57  }
58  }
59  if ((mincomp >= 0) && (mincomp <= 5)) {
60  for (i = 0; i < 3; i++) first_p[i] = view_p[i] + mint * ray_direction[i];
61  }
62  /* for(i=0;i<3;i++) {
63  if(fabs(out_point[i]-minmax[i*2])<EPSILON)
64  face_sect[i]=i*2;
65  else if(fabs(out_point[i]-minmax[i*2+1])<EPSILON)
66  face_sect[i]=i*2+1;
67  }
68  */
69 
70  return (mincomp);
71 }
72 
73 int find_first_inter(double view_point_d[3], int r_level[3], double orig_xyz[3],
74  double dxyz[3], double r_dxyz[3], double ray_direction[3],
75  double first_p[3], int ijk[3]) {
76  int i;
77  int ini_face, intersection, con1, next_ijk[3], face_sect[3];
78  double out_point[3];
79 
80  ini_face = find_first_inter_point(view_point_d, ray_direction, orig_xyz, dxyz,
81  first_p);
82  if (ini_face == -1) { /*this ray has no intersection with dataset */
83  intersection = 0;
84  } else {
85  intersection = 1;
86  for (i = 0; i < 3; i++)
87  ijk[i] = (int)((first_p[i] - orig_xyz[i] - EPSILON) / r_dxyz[i]);
88  if ((ijk[0] < 0) || (ijk[0] > r_level[0] - 1) || (ijk[1] < 0) ||
89  (ijk[1] > r_level[1] - 1) || (ijk[2] < 0) || (ijk[2] > r_level[2] - 1))
91  "ERROR: HEC-MW-VIS-E2006:There is something wrong on finding the "
92  "first point");
93  for (i = 0; i < 3; i++) out_point[i] = 0.0;
94  con1 = find_out_point(orig_xyz, r_dxyz, ijk, first_p, ray_direction,
95  face_sect, out_point);
96  if (con1 == 0) {
97  /* start voxel maybe at the neighbour voxel*/
98  find_next_cell(r_level, ijk, face_sect, ray_direction, next_ijk);
99  if ((next_ijk[0] < 0) || (next_ijk[0] > r_level[0] - 1) ||
100  (next_ijk[1] < 0) || (next_ijk[1] > r_level[1] - 1) ||
101  (next_ijk[2] < 0) || (next_ijk[2] > r_level[2] - 1))
102  intersection = 0;
103 
104  for (i = 0; i < 3; i++) ijk[i] = next_ijk[i];
105  }
106  }
107  return intersection;
108 }
109 
110 static int find_out_point(double orig_xyz[3], double r_dxyz[3], int ijk[3],
111  double in_point[3], double ray_direction[3],
112  int face_sect[3], double out_point[3]) {
113  int i, j, mincomp, intersection;
114  double minmax[6], t[3], mint;
115  int i1, j1;
116  for (i = 0; i < 3; i++) face_sect[i] = -1;
117  for (i = 0; i < 6; i++) {
118  i1 = i / 2;
119  j1 = i % 2;
120  minmax[i] = orig_xyz[i1] + (ijk[i1] + j1) * r_dxyz[i1];
121  }
122  for (i = 0; i < 3; i++) {
123  if (ray_direction[i] > 0)
124  t[i] = (minmax[i * 2 + 1] - in_point[i]) / (ray_direction[i] + EPSILON);
125  else if (ray_direction[i] < 0)
126  t[i] = (in_point[i] - minmax[i * 2]) / (-ray_direction[i] + EPSILON);
127  }
128 
129  mint = 1.0E+17;
130  mincomp = -1;
131  for (i = 0; i < 3; i++) {
132  if ((mint > t[i]) && (t[i] > EPSILON)) {
133  for (j = 0; j < 3; j++)
134  out_point[j] = in_point[j] + t[i] * ray_direction[j];
135  if ((out_point[0] >= minmax[0] - EPSILON) &&
136  (out_point[0] <= minmax[1] + EPSILON) &&
137  (out_point[1] >= minmax[2] - EPSILON) &&
138  (out_point[1] <= minmax[3] + EPSILON) &&
139  (out_point[2] >= minmax[4] - EPSILON) &&
140  (out_point[2] <= minmax[5] + EPSILON)) {
141  mint = t[i];
142  mincomp = i;
143  }
144  }
145  }
146  if ((mincomp >= 0) && (mincomp <= 2)) {
147  intersection = 1;
148  for (i = 0; i < 3; i++)
149  out_point[i] = in_point[i] + mint * ray_direction[i];
150  } else {
151  intersection = 0;
152  for (i = 0; i < 3; i++) out_point[i] = in_point[i];
153  }
154  for (i = 0; i < 3; i++) {
155  if (fabs(out_point[i] - minmax[i * 2]) < EPSILON)
156  face_sect[i] = i * 2;
157  else if (fabs(out_point[i] - minmax[i * 2 + 1]) < EPSILON)
158  face_sect[i] = i * 2 + 1;
159  }
160 
161  return (intersection);
162 }
163 
164 static void find_next_cell(int ijkn[3], int current_ijk[3], int face_sect[3],
165  double vv[3], int next_ijk[3])
166 /*get the code of next cell*/
167 {
168  int j, m;
169  int face1_sect[3];
170 
171  for (j = 0; j < 3; j++) face1_sect[j] = face_sect[j];
172  for (j = 0; j < 3; j++) next_ijk[j] = current_ijk[j];
173  for (m = 0; m < 3; m++) {
174  if (face_sect[m] != -1) {
175  if (face_sect[m] == 0) {
176  if (vv[0] < 0) {
177  next_ijk[0] = current_ijk[0] - 1;
178  face1_sect[m] = 1;
179  if (next_ijk[0] < 0) break;
180  }
181  } else if (face_sect[m] == 1) {
182  if (vv[0] > 0) {
183  next_ijk[0] = current_ijk[0] + 1;
184  face1_sect[m] = 0;
185  if (next_ijk[0] > ijkn[0] - 1) break;
186  }
187  } else if (face_sect[m] == 2) {
188  if (vv[1] < 0) {
189  next_ijk[1] = current_ijk[1] - 1;
190  face1_sect[m] = 3;
191  if (next_ijk[1] < 0) break;
192  }
193  } else if (face_sect[m] == 3) {
194  if (vv[1] > 0) {
195  next_ijk[1] = current_ijk[1] + 1;
196  face1_sect[m] = 2;
197  if (next_ijk[1] > ijkn[1] - 1) break;
198  }
199  } else if (face_sect[m] == 4) {
200  if (vv[2] < 0) {
201  next_ijk[2] = current_ijk[2] - 1;
202  face1_sect[m] = 5;
203  if (next_ijk[2] < 0) break;
204  }
205  } else if (face_sect[m] == 5) {
206  if (vv[2] > 0) {
207  next_ijk[2] = current_ijk[2] + 1;
208  face1_sect[m] = 4;
209  if (next_ijk[2] > ijkn[2] - 1) break;
210  }
211  }
212  }
213  }
214 
215  for (m = 0; m < 3; m++) face_sect[m] = face1_sect[m];
216 
217  return;
218 }
219 
220 static int find_next_point(double orig_xyz[3], double r_dxyz[3], int r_level[3],
221  int current_ijk[3], double in_point[3],
222  double out_point[3], double ray_direction[3],
223  int next_ijk[3]) {
224  int flag, face_sect[3];
225 
226  flag = 1;
227  flag = find_out_point(orig_xyz, r_dxyz, current_ijk, in_point, ray_direction,
228  face_sect, out_point);
229  /* con1=find_out_point(vd, current_ijk, in_point,ray_direction,face_sect,out_point);
230  */ if (flag == 0)
232  "ERROR: HEC-MW-VIS-E2007:There is some problem in finding intersection "
233  "point");
234 
235  /* for(i=0;i<3;i++)
236  mid_point[i]=(out_point[i]+in_point[i])/2.0;
237  for(i=0;i<3;i++)
238  current_ijk[i]=(int)((mid_point[i]-vd->xyz0[i])/vd->dxyz[i]);
239  */
240  if (flag == 1) {
241  find_next_cell(r_level, current_ijk, face_sect, ray_direction, next_ijk);
242  if ((next_ijk[0] < 0) || (next_ijk[0] > r_level[0] - 1) ||
243  (next_ijk[1] < 0) || (next_ijk[1] > r_level[1] - 1) ||
244  (next_ijk[2] < 0) || (next_ijk[2] > r_level[2] - 1))
245  flag = -1;
246  }
247  return (flag);
248 }
249 
250 void ray_trace(int color_mapping_style, double *interval_point,
251  int transfer_function_style, double opa_value,
252  int num_of_features, double *fea_point, int interval_mapping_num,
253  int color_system_type, double k_ads[3], double orig_xyz[3],
254  double r_dxyz[3], int r_level[3], int *empty_flag, double *var,
255  double first_p[3], int first_ijk[3], double ray_direction[3],
256  double mincolor, double maxcolor, double accum_rgba[4],
257  double tav_length)
258 
259 /*void ray_trace(Parameter_vr *vr, VR_data *vd, Tree_pointer *root_tree,
260  Tree_pointer *voxel_p,
261  double point_o[3], double ray_direction[3], double
262  mincolor, double maxcolor,
263  int connect[8][6], int local_face[8][6], double
264  accum_rgba[4], double grad_minmax[2],
265  double feap_minmax[2], double feai_minmax[2],
266  double dis_minmax[2], double *opa_table, double
267  tav_length, int time_step)
268  */
269 {
270  int i, j;
271  double in_point[3], out_point[3];
272  int flag, single_empty_flag, value_flag, index, current_ijk[3], next_ijk[3];
273 
274  for (i = 0; i < 4; i++) accum_rgba[i] = 0.0;
275  flag = 1;
276  for (i = 0; i < 3; i++) in_point[i] = first_p[i];
277  for (i = 0; i < 3; i++) current_ijk[i] = first_ijk[i];
278  while ((accum_rgba[3] < 0.99) && (flag == 1)) {
279  /*
280 while((accum_rgba[3]<0.99) && (flag==1) && (accum_rgba[0]<0.99) &&
281 (accum_rgba[1]<0.99)
282 && (accum_rgba[2]<0.99)) {
283  */
284  flag = find_next_point(orig_xyz, r_dxyz, r_level, current_ijk, in_point,
285  out_point, ray_direction, next_ijk);
286 
287  single_empty_flag = 1;
288  value_flag = 1;
289  /* for(k=0;k<2;k++)
290  for(j=0;j<2;j++)
291  for(i=0;i<2;i++) {
292 
293  index=(current_ijk[2]+k)*(r_level[0]+1)*(r_level[1]+1)+(current_ijk[1]+j)*(r_level[0]+1)+
294  current_ijk[0]+i;
295  */
296  for (i = 0; i < 8; i++) {
297  j = (int)i % 4;
298  index = (current_ijk[2] + (int)(i / 4)) * (r_level[0] + 1) *
299  (r_level[1] + 1) +
300  (current_ijk[1] + (int)(j / 2)) * (r_level[0] + 1) +
301  current_ijk[0] + j % 2;
302  if (empty_flag[index] == 0) single_empty_flag = 0;
303  /* if(fabs(var[index])>EPSILON)
304  value_flag=1;
305  */
306  }
307  /* value_flag=1;
308 if(remove_0_display_on==1) {
309  value_flag=0;
310 for(i=0;i<8;i++) {
311 j
312  if(fabs(var[index])>EPSILON) {
313  value_flag=1;
314  break;
315  }
316  }
317 }
318 
319 overlap_flag=1;
320 for(k=0;k<3;k++) {
321  if(fabs(in_point[k]-(vd->xyz0[k]+vd->dxyz[k]))<EPSILON) {
322  overlap_flag=0;
323  break;
324  }
325 }
326  */
327 
328  /*
329 if((empty_flag==1) && (value_flag==1))
330  */
331  if ((single_empty_flag == 1) && (flag != 0) && (value_flag == 1))
332  compute_color_vr(current_ijk, color_mapping_style, interval_point,
333  transfer_function_style, opa_value, num_of_features,
334  fea_point, interval_mapping_num, color_system_type,
335  k_ads, r_level, orig_xyz, r_dxyz, var, accum_rgba,
336  mincolor, maxcolor, in_point, out_point, tav_length);
337  if (flag == 1) {
338  for (i = 0; i < 3; i++) in_point[i] = out_point[i];
339  for (i = 0; i < 3; i++) current_ijk[i] = next_ijk[i];
340  }
341  /* if((vd->empty_flag[current_voxel->cell_id-vd->nxyz[0]*vd->nxyz[1]*vd->nxyz[2]]==1)
342 && ((fabs(vd->var[current_voxel->cell_id])>EPSILON) ||
343  (fabs(vd->grad_var[current_voxel->cell_id*3])>EPSILON) ||
344  (fabs(vd->grad_var[current_voxel->cell_id*3+1])>EPSILON) ||
345  (fabs(vd->grad_var[current_voxel->cell_id*3+2])>EPSILON)))
346  */
347  /* if((vd->empty_flag[current_voxel->cell_id-vd->nxyz[0]*vd->nxyz[1]*vd->nxyz[2]]==1)
348  && (fabs(vd->var[current_voxel->cell_id])>EPSILON))
349  */
350  }
351  return;
352 }
void compute_color_vr(int current_ijk[3], int color_mapping_style, double *interval_point, int transfer_function_style, double opa_value, int num_of_features, double *fea_point, int interval_mapping_num, int color_system_type, double k_ads[3], int r_level[3], double orig_xyz[3], double r_dxyz[3], double *var, double accum_rgba[4], double mincolor, double maxcolor, double in_point[3], double out_point[3], double tav_length)
#define EPSILON
void HECMW_vis_print_exit(char *var)
void ray_trace(int color_mapping_style, double *interval_point, int transfer_function_style, double opa_value, int num_of_features, double *fea_point, int interval_mapping_num, int color_system_type, double k_ads[3], double orig_xyz[3], double r_dxyz[3], int r_level[3], int *empty_flag, double *var, double first_p[3], int first_ijk[3], double ray_direction[3], double mincolor, double maxcolor, double accum_rgba[4], double tav_length)
int find_first_inter(double view_point_d[3], int r_level[3], double orig_xyz[3], double dxyz[3], double r_dxyz[3], double ray_direction[3], double first_p[3], int ijk[3])