1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 /*
6   CHECDB_Visual Ver.1.0
7 */
8 
9 #include <stdarg.h>
10 #include <vector>
11 #include "CHECDB.h"
12 #include "CHECDB_Visual.h"
13 #include "CHECData.h"
14 #include "hecd_util.h"
15 
16 using namespace std;
17 using namespace hecd_util;
18 
19 //*****************************************************************************
20 // CVis_ViewParams
21 //*****************************************************************************
22 
CVis_ViewParams()23 CVis_ViewParams::CVis_ViewParams()
24     : position_of_lights(0), interval_mapping(0) {
25   Init();
26 }
27 
~CVis_ViewParams()28 CVis_ViewParams::~CVis_ViewParams() {
29   delete position_of_lights;
30   delete interval_mapping;
31 }
32 
Init()33 void CVis_ViewParams::Init() {
34   x_resolution  = 512;
35   y_resolution  = 512;
36   num_of_lights = 1;
37   delete position_of_lights;
38   position_of_lights = 0;
39   viewpoint[0] = viewpoint[1] = viewpoint[2] = 0;
40   fg_viewpoint                               = false;
41   look_at_point[0] = look_at_point[1] = look_at_point[2] = 0;
42   fg_look_at_point                                       = false;
43   up_direction[0]                                        = 0;
44   up_direction[1]                                        = 0;
45   up_direction[2]                                        = 1;
46   ambient_coef                                           = 0.3;
47   diffuse_coef                                           = 0.7;
48   specular_coef                                          = 0.6;
49   color_mapping_style  = color_mapping_style_linear;
50   interval_mapping_num = 0;
51   delete[] interval_mapping;
52   interval_mapping     = 0;
53   interval_mapping     = 0;
54   rotate_style         = rotate_style_none;
55   num_of_frame         = 8;
56   color_mapping_bar_on = 0;
57   scale_marking_on     = 0;
58   num_of_scales        = 3;
59   font_size            = 1;
60   font_color[0] = font_color[1] = font_color[2] = 1;
61   background_color[0] = background_color[1] = background_color[2] = 0;
62   color_system_type = color_system_type_blue_red;
63   fixed_range_on    = 0;
64   range_value[0] = range_value[1] = 0;
65   fg_range_value                  = false;
66 }
67 
WriteVisParam(class CHECData * hecd,const char * name,const char * fmt,...)68 void CVis_ViewParams::WriteVisParam(class CHECData *hecd, const char *name,
69                                     const char *fmt, ...) {
70   va_list va;
71   va_start(va, fmt);
72   char buff[256];
73   char ss[256];
74   sprintf(buff, "!%s", name);
75   int n = strlen(fmt);
76 
77   if (n > 1) {
78     strcat(buff, " =");
79   }
80 
81   for (int i = 0; i < n; i++) {
82     strcat(buff, " ");
83     char c = fmt[i];
84 
85     switch (c) {
86       case 'S':
87         strcat(buff, va_arg(va, char *));
88         break;
89 
90       case 'I':
91         sprintf(ss, "%d", va_arg(va, int));
92         strcat(buff, ss);
93         break;
94 
95       case 'F':
96         sprintf(ss, "%lg", va_arg(va, double));
97         strcat(buff, ss);
98         break;
99 
100       default:
101         assert(0);
102     }
103   }
104 
105   hecd->WriteLine(buff);
106   va_end(va);
107 }
108 
WriteVisPArry(class CHECData * hecd,const char * name,char type,int n,void * p)109 void CVis_ViewParams::WriteVisPArry(class CHECData *hecd, const char *name,
110                                     char type, int n, void *p) {
111   char buff[256];
112   char ss[256];
113   int *ip;
114   double *dp;
115   sprintf(buff, "!%s", name);
116 
117   if (n > 1) {
118     strcat(buff, " =");
119   }
120 
121   ip = (int *)p;
122   dp = (double *)p;
123 
124   for (int i = 0; i < n; i++) {
125     strcat(buff, ", ");
126 
127     switch (type) {
128       case 'I':
129         sprintf(ss, "%d", *ip);
130         strcat(buff, ss);
131         ip++;
132         break;
133 
134       case 'F':
135         sprintf(ss, "%lg", *dp);
136         strcat(buff, ss);
137         dp++;
138         break;
139 
140       default:
141         assert(0);
142     }
143   }
144 
145   hecd->WriteLine(buff);
146 }
147 
Write(class CHECData * hecd)148 void CVis_ViewParams::Write(class CHECData *hecd) {
149   WriteVisParam(hecd, "x_resolution", "I", x_resolution);
150   WriteVisParam(hecd, "y_resolution", "I", y_resolution);
151   WriteVisParam(hecd, "num_of_lights", "I", num_of_lights);
152 
153   if (num_of_lights > 0 && position_of_lights) {
154     WriteVisPArry(hecd, "position_of_lights", 'F', num_of_lights,
155                   position_of_lights);
156   }
157 
158   if (fg_viewpoint) WriteVisPArry(hecd, "viewpoint", 'F', 3, viewpoint);
159 
160   if (fg_look_at_point)
161     WriteVisPArry(hecd, "look_at_point", 'F', 3, look_at_point);
162 
163   WriteVisParam(hecd, "up_direction", "F", 3, up_direction);
164   WriteVisParam(hecd, "ambient_coef", "F", ambient_coef);
165   WriteVisParam(hecd, "diffuse_coef", "F", diffuse_coef);
166   WriteVisParam(hecd, "specular_coef", "F", specular_coef);
167   WriteVisParam(hecd, "color_mapping_style", "I", color_mapping_style);
168   WriteVisParam(hecd, "interval_mapping_num", "I", interval_mapping_num);
169 
170   if (interval_mapping_num > 0) {
171     if (!interval_mapping) assert(0);
172 
173     WriteVisPArry(hecd, "nterval_mapping", 'F', interval_mapping_num * 2,
174                   interval_mapping);
175   }
176 
177   if (rotate_style != rotate_style_none)
178     WriteVisParam(hecd, "rotate_style", "I", rotate_style);
179 
180   WriteVisParam(hecd, "num_of_frame", "I", num_of_frame);
181   WriteVisParam(hecd, "color_mapping_bar_on", "I", color_mapping_bar_on);
182   WriteVisParam(hecd, "scale_marking_on", "I", scale_marking_on);
183   WriteVisParam(hecd, "num_of_scales", "I", num_of_scales);
184   WriteVisParam(hecd, "font_size", "F", font_size);
185   WriteVisPArry(hecd, "font_color", 'F', 3, font_color);
186   WriteVisPArry(hecd, "font_color", 'F', 3, font_color);
187   WriteVisPArry(hecd, "background_color", 'F', 3, background_color);
188   WriteVisParam(hecd, "color_system_type", "I", color_system_type);
189   WriteVisParam(hecd, "fixed_range_on", "I", fixed_range_on);
190 
191   if (fg_range_value) WriteVisPArry(hecd, "range_value", 'F', 3, range_value);
192 }
193 
194 //-----------------------------------------------------------------------------
195 
196 template <class T>
ReadVisValueT(char * s,int n,T * value,const char * fmt)197 int CVis_ViewParams::ReadVisValueT(char *s, int n, T *value, const char *fmt) {
198   const char *delim = ", \t\r\n";
199   int i             = 0;
200   char *token       = strtok(s, delim);
201 
202   while (token && i < n) {
203     if (sscanf(token, fmt, &value[i]) != 1) return -(i + 1);
204 
205     token = strtok(0, delim);
206     i++;
207   }
208 
209   return i;
210 }
211 
ReadVisValue(char * s,int n,int * value)212 int CVis_ViewParams::ReadVisValue(char *s, int n, int *value) {
213   return ReadVisValueT<int>(s, n, value, "%d");
214 }
215 
ReadVisValue(char * s,int n,double * value)216 int CVis_ViewParams::ReadVisValue(char *s, int n, double *value) {
217   return ReadVisValueT<double>(s, n, value, "%lf");
218 }
219 
ReadVisValue(char * s,int n,char * value)220 int CVis_ViewParams::ReadVisValue(char *s, int n, char *value) {
221   cleanup_token(s);
222   int len = strlen(s);
223 
224   if (len > n) return 0;
225 
226   strcpy(value, s);
227   return len;
228 }
229 
230 template <class T>
ReadVisPArry(char * s,int & n,const char * fmt)231 T *CVis_ViewParams::ReadVisPArry(char *s, int &n, const char *fmt) {
232   const int max_data_n = 200;
233   T buff[max_data_n];
234   const char *delim = ", \t\r\n";
235   n                 = 0;
236   int i             = 0;
237   char *token       = strtok(s, delim);
238 
239   while (token) {
240     if (max_data_n >= i) assert(0);
241 
242     T x;
243 
244     if (sscanf(token, fmt, &x) != 1) {
245       return 0;
246     }
247 
248     buff[i] = x;
249     token   = strtok(0, delim);
250     i++;
251   }
252 
253   n       = i;
254   T *data = new T[n];
255 
256   for (i = 0; i < n; i++) {
257     data[i] = buff[i];
258   }
259 
260   return data;
261 }
262 
ReadData(const char * line)263 bool CVis_ViewParams::ReadData(const char *line) {
264   char buff[256];
265   strcpy(buff, line);
266   char *header = strtok(buff, " =,\r\n\t");
267 
268   if (header[0] == 0 || header[0] != '!') return false;
269 
270   tolower(header);
271 #define GENERATE_CODE(x, n, fg)       \
272   else if (strcmp(#x, header) == 0) { \
273     int r = ReadVisValue(0, n, &x);   \
274     fg    = (r > 0);                  \
275     return fg;                        \
276   }
277 #define GENERATE_CODEA(x, n, fg)      \
278   else if (strcmp(#x, header) == 0) { \
279     int r = ReadVisValue(0, n, x);    \
280     fg    = (r > 0);                  \
281     return fg;                        \
282   }
283   bool dummy;
284 
285   if (false)
286     ;  // dummy
287 
288   GENERATE_CODE(x_resolution, 1, dummy)
289   GENERATE_CODE(y_resolution, 1, dummy)
290   GENERATE_CODEA(viewpoint, 3, fg_viewpoint)
291   GENERATE_CODEA(look_at_point, 3, fg_look_at_point)
292   GENERATE_CODEA(up_direction, 3, dummy)
293   GENERATE_CODE(ambient_coef, 1, dummy)
294   GENERATE_CODE(diffuse_coef, 1, dummy)
295   GENERATE_CODE(specular_coef, 1, dummy)
296   GENERATE_CODE(color_mapping_style, 1, dummy)
297   GENERATE_CODE(rotate_style, 1, dummy)
298   GENERATE_CODE(num_of_frame, 1, dummy)
299   GENERATE_CODE(color_mapping_bar_on, 1, dummy)
300   GENERATE_CODE(scale_marking_on, 1, dummy)
301   GENERATE_CODE(num_of_scales, 1, dummy)
302   GENERATE_CODE(font_size, 1, dummy)
303   GENERATE_CODEA(font_color, 3, dummy)
304   GENERATE_CODEA(background_color, 3, dummy)
305   GENERATE_CODE(color_system_type, 1, dummy)
306   GENERATE_CODE(fixed_range_on, 1, dummy)
307   GENERATE_CODEA(range_value, 2, fg_range_value)
308 #undef GENERATE_CODE
309 #undef GENERATE_CODEA
310 #define GENERATE_CODE(x, n, type, fmt) \
311   else if (strcmp(#x, header) == 0) {  \
312     delete[] x;                        \
313     x = 0;                             \
314     x = ReadVisPArry<type>(0, n, fmt); \
315     return x != 0;                     \
316   }
317 
318   if (false)
319     ;
320 
321   GENERATE_CODE(position_of_lights, num_of_lights, double, "%lf")
322   GENERATE_CODE(interval_mapping, interval_mapping_num, double, "%lf")
323 #undef GENERATE_CODE
324   return false;
325 }
326 
Read(CHECData * hecd)327 bool CVis_ViewParams::Read(CHECData *hecd) {
328   char line[256];
329 
330   while (1) {
331     if (!hecd->ReadLine(line)) break;
332 
333     if (!ReadData(line)) {
334       hecd->PushReadLine(line);
335       break;
336     }
337   }
338 
339   return true;
340 }
341 
342 //*****************************************************************************
343 // CVis_PSR
344 //*****************************************************************************
345 
CVis_PSR()346 CVis_PSR::CVis_PSR() : CVis_ViewParams() { Init(); }
347 
~CVis_PSR()348 CVis_PSR::~CVis_PSR() {}
349 
Init()350 void CVis_PSR::Init() {
351   // common ------------------------------------------------
352   surface_style      = surface_style_boundary;
353   display_method     = display_method_color;
354   color_comp_name[0] = 0;
355   strcpy(color_subcomp_name, "x");
356   color_comp            = 0;
357   color_subcomp         = 1;
358   iso_number            = 5;
359   specified_color       = 0;
360   deform_display_on     = 0;
361   deform_comp_name[0]   = 0;
362   deform_comp           = 0;
363   deform_scale          = 0;
364   fg_deform_scale       = false;
365   initial_style         = initial_style_none;
366   deform_style          = deform_style_none;
367   initial_line_color[0] = 0;
368   initial_line_color[1] = 0;
369   initial_line_color[2] = 1;
370   deform_line_color[0]  = 0;
371   deform_line_color[1]  = 0;
372   deform_line_color[2]  = 1;
373   strcpy(output_type, "AVS");
374   // for surface_style == surface_style_equivalent ---------
375   data_comp_name[0] = 0;
376   strcpy(data_subcomp_name, "x");
377   data_comp    = 0;
378   data_subcomp = 1;
379   iso_value    = 0;
380   // for surface_style == surface_style_user ---------------
381   method   = method_quadric;
382   point[0] = point[1] = point[2] = 0;
383   radius                         = 1;
384   length                         = 1;
385 
386   for (int i = 0; i < 10; i++) coef[i] = 0;
387 
388   // rendering parameters(output_type == "BMP") ------------
389   CVis_ViewParams::Init();
390   isoline_color[0] = isoline_color[1] = isoline_color[2] = 0;
391   fg_isoline_color                                       = false;
392   boundary_line_on                                       = 0;
393 }
394 
Write(class CHECData * hecd)395 void CVis_PSR::Write(class CHECData *hecd) {
396   // common ------------------------------------------------
397   hecd->WriteLine("!!common of PSR ----------------------------------");
398   WriteVisParam(hecd, "surface_style", "I", surface_style);
399   WriteVisParam(hecd, "display_method", "I", display_method);
400 
401   if (color_comp_name[0] != 0)
402     WriteVisParam(hecd, "display_method", "S", color_comp_name);
403 
404   if (color_subcomp_name[0] != 0)
405     WriteVisParam(hecd, "color_subcomp_name", "S", color_subcomp_name);
406 
407   WriteVisParam(hecd, "color_comp", "I", color_comp);
408   WriteVisParam(hecd, "color_subcomp", "I", color_subcomp);
409   WriteVisParam(hecd, "iso_number", "I", iso_number);
410   WriteVisParam(hecd, "specified_color", "F", specified_color);
411   WriteVisParam(hecd, "deform_display_on", "I", deform_display_on);
412 
413   if (deform_comp_name[0] != 0)
414     WriteVisParam(hecd, "deform_comp_name", "S", deform_comp_name);
415 
416   WriteVisParam(hecd, "deform_comp", "I", deform_comp);
417 
418   if (fg_deform_scale) WriteVisParam(hecd, "deform_scale", "F", deform_scale);
419 
420   if (initial_style != initial_style_none)
421     WriteVisParam(hecd, "initial_style", "I", initial_style);
422 
423   if (deform_style != deform_style_none)
424     WriteVisParam(hecd, "deform_style", "I", deform_style);
425 
426   WriteVisPArry(hecd, "initial_line_color", 'F', 3, initial_line_color);
427   WriteVisPArry(hecd, "deform_line_color", 'F', 3, deform_line_color);
428   WriteVisParam(hecd, "output_type", "S", output_type);
429   // for surface_style == surface_style_equivalent ---------
430   hecd->WriteLine("!!for equivalent value surface -------------------");
431 
432   if (data_comp_name[0] != 0)
433     WriteVisParam(hecd, "data_comp_name", "S", data_comp_name);
434 
435   WriteVisParam(hecd, "data_subcomp_name", "S", data_subcomp_name);
436   WriteVisParam(hecd, "data_comp", "I", data_comp);
437   WriteVisParam(hecd, "data_subcomp", "I", data_subcomp);
438   WriteVisParam(hecd, "iso_value", "F", iso_value);
439   // for surface_style == surface_style_user ---------------
440   hecd->WriteLine("!!for surface of user defined equation -----------");
441   WriteVisParam(hecd, "method", "I", method);
442   WriteVisPArry(hecd, "point", 'F', 3, point);
443   WriteVisParam(hecd, "radius", "F", radius);
444   WriteVisParam(hecd, "length", "F", length);
445   WriteVisPArry(hecd, "coef", 'F', 10, coef);
446   // rendering parameters(output_type == "BMP") ------------
447   // parameters defined in CVis_ScreenParams and followings
448   hecd->WriteLine("!!for BMP output ---------------------------------");
449   CVis_ViewParams::Write(hecd);
450 
451   if (fg_isoline_color)
452     WriteVisPArry(hecd, "isoline_color", 'F', 3, isoline_color);
453 
454   WriteVisParam(hecd, "boundary_line_on", "I", boundary_line_on);
455 }
456 
ReadData(const char * line)457 bool CVis_PSR::ReadData(const char *line) {
458   if (CVis_ViewParams::ReadData(line)) return true;
459 
460   char buff[256];
461   strcpy(buff, line);
462   char *header = strtok(buff, " =,\r\n\t");
463 
464   if (header[0] == 0 || header[0] != '!') return false;
465 
466   tolower(header);
467 #define GENERATE_CODE(x, n, fg)       \
468   else if (strcmp(#x, header) == 0) { \
469     int r = ReadVisValue(0, n, &x);   \
470     fg    = (r > 0);                  \
471     return fg;                        \
472   }
473 #define GENERATE_CODEA(x, n, fg)      \
474   else if (strcmp(#x, header) == 0) { \
475     int r = ReadVisValue(0, n, x);    \
476     fg    = (r > 0);                  \
477     return fg;                        \
478   }
479   bool dummy;
480 
481   if (false)
482     ;  // dummy
483 
484   GENERATE_CODE(surface_style, 1, dummy)
485   GENERATE_CODE(display_method, 1, dummy)
486   GENERATE_CODEA(color_comp_name, 100, dummy)
487   GENERATE_CODEA(color_subcomp_name, 4, dummy)
488   GENERATE_CODE(color_comp, 1, dummy)
489   GENERATE_CODE(color_subcomp, 1, dummy)
490   GENERATE_CODE(iso_number, 1, dummy)
491   GENERATE_CODE(specified_color, 1, dummy)
492   GENERATE_CODE(deform_display_on, 1, dummy)
493   GENERATE_CODEA(deform_comp_name, 100, dummy)
494   GENERATE_CODE(deform_comp, 1, dummy)
495   GENERATE_CODE(deform_scale, 1, fg_deform_scale)
496   GENERATE_CODE(initial_style, 1, dummy)
497   GENERATE_CODE(deform_style, 1, dummy)
498   GENERATE_CODEA(initial_line_color, 3, dummy)
499   GENERATE_CODEA(deform_line_color, 3, dummy)
500   GENERATE_CODEA(output_type, 3, dummy)
501   // for surface_style == surface_style_equivalent ---------
502   GENERATE_CODEA(data_comp_name, 100, dummy)
503   GENERATE_CODEA(data_subcomp_name, 4, dummy)
504   GENERATE_CODE(data_comp, 1, dummy)
505   GENERATE_CODE(data_subcomp, 1, dummy)
506   GENERATE_CODE(iso_value, 1, dummy)
507   // for surface_style == surface_style_user ---------------
508   GENERATE_CODE(method, 1, dummy)
509   GENERATE_CODEA(point, 3, dummy)
510   GENERATE_CODE(radius, 1, dummy)
511   GENERATE_CODE(length, 1, dummy)
512   GENERATE_CODEA(coef, 10, dummy)
513   // rendering parameters(output_type == "BMP") ------------
514   // parameters defined in CVis_ScreenParams and followings
515   GENERATE_CODEA(isoline_color, 3, fg_isoline_color)
516   GENERATE_CODE(boundary_line_on, 1, dummy)
517 #undef GENERATE_CODE
518 #undef GENERATE_CODEA
519   return false;
520 }
521 
522 //*****************************************************************************
523 // CVis_PVR
524 //*****************************************************************************
525 
CVis_PVR()526 CVis_PVR::CVis_PVR() : CVis_ViewParams(), fea_point(0) { Init(); }
527 
~CVis_PVR()528 CVis_PVR::~CVis_PVR() { delete fea_point; }
529 
Init()530 void CVis_PVR::Init() {
531   // for dividing ------------------------------------------
532   maximum_refinement = 100;
533   n_voxel_x          = 0;
534   fg_n_voxel_x       = false;
535   n_voxel_y          = 0;
536   fg_n_voxel_y       = false;
537   n_voxel_z          = 0;
538   fg_n_voxel_z       = false;
539   voxel_filename[0]  = 0;
540   x_specified_level  = 100;
541   y_specified_level  = 100;
542   z_specified_level  = 100;
543   // for opacity
544   transfer_function_type = 1;
545   opa_value              = 0.02;
546   num_of_features        = 0;
547   delete fea_point;
548   fea_point      = 0;
549   name_lookup[0] = 0;
550   // parameters defined in CVis_ViewParams and followings
551   CVis_ViewParams::Init();
552   histogram_on = 0;
553 
554   for (int i = 0; i < 6; i++) display_range[i] = 0;
555 
556   fg_display_range = false;
557 }
558 
Write(class CHECData * hecd)559 void CVis_PVR::Write(class CHECData *hecd) {
560   // for dividing ------------------------------------------
561   hecd->WriteLine("!!for dividing -----------------------------------");
562   WriteVisParam(hecd, "maximum_refinement", "I", maximum_refinement);
563 
564   if (fg_n_voxel_x) WriteVisParam(hecd, "n_voxel_x", "I", n_voxel_x);
565 
566   if (fg_n_voxel_y) WriteVisParam(hecd, "n_voxel_y", "I", n_voxel_y);
567 
568   if (fg_n_voxel_z) WriteVisParam(hecd, "n_voxel_z", "I", n_voxel_z);
569 
570   if (voxel_filename[0] != 0)
571     WriteVisParam(hecd, "voxel_filename", "S", voxel_filename);
572 
573   WriteVisParam(hecd, "x_specified_level", "I", x_specified_level);
574   WriteVisParam(hecd, "y_specified_level", "I", y_specified_level);
575   WriteVisParam(hecd, "z_specified_level", "I", z_specified_level);
576   // for opacity -------------------------------------------
577   hecd->WriteLine("!!for opacity ------------------------------------");
578   WriteVisParam(hecd, "transfer_function_type", "I", transfer_function_type);
579   WriteVisParam(hecd, "opa_value", "F", opa_value);
580   WriteVisParam(hecd, "num_of_features", "I", num_of_features);
581 
582   if (num_of_features > 0 && fea_point)
583     WriteVisPArry(hecd, "fea_point", 'F', 3 * num_of_features, fea_point);
584 
585   if (name_lookup[0] != 0) WriteVisParam(hecd, "name_lookup", "S", name_lookup);
586 
587   // parameters defined in CVis_ViewParams and followings
588   hecd->WriteLine("!!view params ------------------------------------");
589   CVis_ViewParams::Write(hecd);
590   WriteVisParam(hecd, "histogram_on", "I", histogram_on);
591 
592   if (fg_display_range)
593     WriteVisPArry(hecd, "display_range", 'F', 6, display_range);
594 }
595 
ReadData(const char * line)596 bool CVis_PVR::ReadData(const char *line) {
597   if (CVis_ViewParams::ReadData(line)) return true;
598 
599   char buff[256];
600   strcpy(buff, line);
601   char *header = strtok(buff, " =,\r\n\t");
602 
603   if (header[0] == 0 || header[0] != '!') return false;
604 
605   tolower(header);
606 #define GENERATE_CODE(x, n, fg)       \
607   else if (strcmp(#x, header) == 0) { \
608     int r = ReadVisValue(0, n, &x);   \
609     fg    = (r > 0);                  \
610     return fg;                        \
611   }
612 #define GENERATE_CODEA(x, n, fg)      \
613   else if (strcmp(#x, header) == 0) { \
614     int r = ReadVisValue(0, n, x);    \
615     fg    = (r > 0);                  \
616     return fg;                        \
617   }
618   bool dummy;
619 
620   if (false)
621     ;  // dummy
622 
623   // for dividing ------------------------------------------
624   GENERATE_CODE(maximum_refinement, 1, dummy)
625   GENERATE_CODE(n_voxel_x, 1, fg_n_voxel_x)
626   GENERATE_CODE(n_voxel_y, 1, fg_n_voxel_y)
627   GENERATE_CODE(n_voxel_z, 1, fg_n_voxel_z)
628   GENERATE_CODEA(voxel_filename, 100, dummy)
629   GENERATE_CODE(x_specified_level, 1, dummy)
630   GENERATE_CODE(y_specified_level, 1, dummy)
631   GENERATE_CODE(z_specified_level, 1, dummy)
632   // for opacity
633   GENERATE_CODE(transfer_function_type, 1, dummy)
634   GENERATE_CODE(opa_value, 1, dummy)
635   GENERATE_CODE(num_of_features, 1, dummy)
636   GENERATE_CODEA(name_lookup, 100, dummy)
637   // parameters defined in CVis_ViewParams and followings
638   GENERATE_CODE(histogram_on, 1, dummy)
639   GENERATE_CODEA(display_range, 6, fg_display_range)
640 #undef GENERATE_CODE
641 #undef GENERATE_CODEA
642 #define GENERATE_CODE(x, n, type, fmt) \
643   else if (strcmp(#x, header) == 0) {  \
644     delete[] x;                        \
645     x = 0;                             \
646     x = ReadVisPArry<type>(0, n, fmt); \
647     return x != 0;                     \
648   }
649 
650   if (num_of_features <= 0) return false;
651 
652   int f_data_size = 3 * num_of_features;
653 
654   if (false)
655     ;
656 
657   GENERATE_CODE(fea_point, f_data_size, double, "%lf");
658 #undef GENERATE_CODE
659   return false;
660 }
661 
662 //-----------------------------------------------------------------------------
663 // CHECDB_Visual
664 //-----------------------------------------------------------------------------
665 
CHECDB_Visual()666 CHECDB_Visual::CHECDB_Visual() : CHECDataBlock(HECDB_VISUAL), psr(), pvr(0) {
667   visual_start_step    = -1;
668   visual_interval_step = 1;
669   visual_end_step      = -1;
670   surface_num          = 0;
671 }
672 
~CHECDB_Visual()673 CHECDB_Visual::~CHECDB_Visual() { Clear(); }
674 
Clear()675 void CHECDB_Visual::Clear() {
676   visual_start_step    = -1;
677   visual_interval_step = 1;
678   visual_end_step      = -1;
679   surface_num          = 0;
680   vector<CVis_PSR *>::iterator is;
681 
682   for (is = psr.begin(); is != psr.end(); is++) {
683     delete *is;
684   }
685 
686   psr.clear();
687   delete pvr;
688   pvr = 0;
689 }
690 
Write(class CHECData * hecd)691 void CHECDB_Visual::Write(class CHECData *hecd) {
692   surface_num = psr.size();
693 
694   if (surface_num > 0) {
695     hecd->WriteHeader("!VISUAL", "SIII", "method", "PSR", "visual_start_step",
696                       visual_start_step, "visual_interval_step",
697                       visual_interval_step, "visual_end_step", visual_end_step);
698     CVis_ViewParams::WriteVisParam(hecd, "surface_num", "I", surface_num);
699     vector<CVis_PSR *>::iterator is;
700 
701     for (is = psr.begin(); is != psr.end(); is++) {
702       hecd->WriteLine("!SURFACE");
703       (*is)->Write(hecd);
704     }
705   }
706 
707   if (pvr) {
708     hecd->WriteHeader("!VISUAL", "SIII", "method", "PVR", "visual_start_step",
709                       visual_start_step, "visual_interval_step",
710                       visual_interval_step, "visual_end_step", visual_end_step);
711     pvr->Write(hecd);
712   }
713 }
714 
Read(CHECData * hecd,char * header_line)715 bool CHECDB_Visual::Read(CHECData *hecd, char *header_line) {
716   char s[256];
717   char method_s[256];
718   int rcode[10];
719 
720   if (!hecd->ParseHeader(header_line, rcode, "SIII", "method", s,
721                          "visual_start_step", &visual_start_step,
722                          "visual_interval_step", &visual_interval_step,
723                          "visual_end_step", &visual_end_step))
724     return false;
725 
726   cleanup_token(s, method_s);
727   toupper(method_s);
728   bool fg_psr = (strcmp("PSR", method_s) == 0);
729 
730   if (fg_psr) {
731     if (!hecd->ReadParameter(rcode, "I", "surface_num", &surface_num))
732       return false;
733 
734     for (int i = 0; i < surface_num; i++) {
735       if (!hecd->ReadLine(s)) return false;
736 
737       cleanup_token(s);
738       toupper(s);
739 
740       if (strcmp("!SURFACE", s) != 0) return false;
741 
742       CVis_PSR *vis_psr = new CVis_PSR();
743 
744       if (!vis_psr->Read(hecd)) {
745         delete vis_psr;
746         return false;
747       }
748 
749       psr.push_back(vis_psr);
750     }
751 
752   } else {
753     delete pvr;
754     pvr = new CVis_PVR();
755 
756     if (!pvr->Read(hecd)) {
757       delete pvr;
758       pvr = 0;
759       return false;
760     }
761   }
762 
763   return true;
764 }
765