00001 /* 00002 File: Examiner.cc 00003 00004 Function: 00005 00006 Author: Andrew Willmott 00007 00008 Notes: 00009 */ 00010 00011 #include "Examiner.h" 00012 #include "gcl/SceneLang.h" 00013 #include "gcl/MesaRenderer.h" 00014 00015 #include <fstream.h> 00016 00017 00018 // --- Useful prims ----------------------------------------------------------- 00019 00020 Void CentredArrow(GCLReal height, GCLReal width) 00021 { 00022 const GCLReal kWidth = 0.2; 00023 00024 slBeginObject("cArrow"); 00025 slTransform(Scalef(width)); 00026 00027 slObject("cone"); 00028 slApply(Shift(Vector(0, (height - width) / width, 0))); 00029 00030 slObject("cylinder"); 00031 slApply(Shift(0.0, 0.5, 0.0)); 00032 slApply(Scale(Vector(0.3, (height - width) / width, 0.3))); 00033 slEndObject(); 00034 } 00035 00036 Void Examiner::SampleDiff() 00037 // subtract samples from the currently selected patch. 00038 { 00039 Int i, j; 00040 RadElem *leaf; 00041 Coord place; 00042 Int density = plot->samples.Rows(); 00043 00044 if (radPane->selPatch[0]) 00045 for (i = 0; i < density; i++) 00046 for (j = 0; j < density; j++) 00047 { 00048 place[0] = GCLReal(i) / GCLReal(density - 1); 00049 place[1] = GCLReal(j) / GCLReal(density - 1); 00050 00051 plot->samples[i][j] -= len((radPane->selPatch[0])-> 00052 Sample(place)); 00053 } 00054 } 00055 00056 Void Examiner::SampleAdd() 00057 // Add samples from the currently selected patch. 00058 { 00059 Int i, j; 00060 RadElem *leaf; 00061 Coord place; 00062 Int density = plot->samples.Rows(); 00063 00064 if (radPane->selPatch[0]) 00065 for (i = 0; i < density; i++) 00066 for (j = 0; j < density; j++) 00067 { 00068 place[0] = GCLReal(i) / GCLReal(density - 1); 00069 place[1] = GCLReal(j) / GCLReal(density - 1); 00070 00071 plot->samples[i][j] += len((radPane->selPatch[0])-> 00072 Sample(place)); 00073 } 00074 } 00075 00076 Void Examiner::Dump(ostream &s) 00077 { 00078 Int i, j; 00079 GCLReal x; 00080 00081 s << 0.0; 00082 for (j = 0; j < plot->samples.Cols(); j++) 00083 { 00084 x = j / GCLReal(plot->samples.Cols() - 1); 00085 s << " " << x; 00086 } 00087 s << endl; 00088 00089 for (i = 0; i < plot->samples.Rows(); i++) 00090 { 00091 x = i / GCLReal(plot->samples.Rows() - 1); 00092 s << x << " "; 00093 for (j = 0; j < plot->samples.Cols(); j++) 00094 s << plot->samples[i][j] << ' '; 00095 s << endl; 00096 } 00097 } 00098 00099 Void Examiner::BuildPlot() 00100 { 00101 slBeginObject("plot"); 00102 00103 // set up plot colour 00104 slColour(HSVCol(260, 1, 0.5)); 00105 plotColour = (scColour *) slCurrent(); 00106 00107 // set up scale transform 00108 slTransform(Scale(Vector(vl_1))); 00109 scaler = (scTransform *) slCurrent(); 00110 00111 // the plot! 00112 slObject(plot); 00113 00114 slEndObject(); 00115 00116 // axes 00117 slColour(cWhite); 00118 CentredArrow(1.3, 0.1); 00119 slApply(Rotation(vl_x, M_PI / 2.0)); 00120 CentredArrow(1.3, 0.1); 00121 slApply(Rotation(vl_z, -M_PI / 2.0)); 00122 CentredArrow(1.3, 0.1); 00123 00124 // base 00125 slPointList(); 00126 slPoint(Point(-1, 0, 1)); 00127 slPoint(Point( 1, 0, 1)); 00128 slPoint(Point( 1, 0, -1)); 00129 slPoint(Point(-1, 0, -1)); 00130 00131 slColour(0.5 * cWhite); 00132 slPoly(); 00133 00134 slApply(Scale(Vector(1.1, 1, 1.1))); 00135 slApply(Shift(Vector(0, 1, 0))); 00136 } 00137 00138 static double pslider_map(double val) 00139 { 00140 // double d = floor(val - 0.05); 00141 // 00142 // return(pow(10.0, d + 1.0) * (val - d)); 00143 return(pow(10.0, -val)); 00144 } 00145 00146 static char *psliderFilt(FL_OBJECT *, double val, int prec) 00147 { 00148 static char result[32]; 00149 00150 val = pslider_map(val); 00151 sprintf(result, "%.*g", prec, val); 00152 00153 return(result); 00154 } 00155 00156 Void Examiner::Init(FormsGraphicsSystem &gs, RadScenePane *mainPane) 00157 { 00158 plot = new scPlot2D; 00159 radPane = mainPane; 00160 00161 fl_set_slider_filter(scaleSlider, psliderFilt); 00162 fl_set_slider_bounds(scaleSlider, -2.0, 2.0); 00163 fl_set_slider_value(scaleSlider, 0.0); 00164 fl_set_slider_step(scaleSlider, 0.01); 00165 00166 gs.CreateFormsPane(&plotPane, pane); 00167 00168 plotScene = slBeginObject("plot"); 00169 BuildPlot(); 00170 slEndObject(); 00171 00172 plot->plotType = plotVertex; 00173 00174 // set up camera rather than tweaking ScenePane after the 00175 // SetScene so that we don't get a visual jump 00176 00177 scCamera *cam = new scCamera; 00178 cam->SetModelCentric(Coord(0.25, 0.25), vl_0); 00179 plotScene->Set(cam); 00180 plotPane.SetScene(plotScene); 00181 plotPane.SetHeadlight(true); 00182 } 00183 00184 Void Examiner::Event(FL_OBJECT *object) 00185 { 00186 if (object == dismiss) 00187 { 00188 plot->samples.SetSize(1, 1); 00189 Hide(); 00190 } 00191 else if (object == scaleSlider) 00192 { 00193 GCLReal amount = pslider_map(fl_get_slider_value(scaleSlider)); 00194 00195 *scaler = Scale(Vector(1, amount, 1)); 00196 plotPane.Redraw(); 00197 } 00198 else if (object == diff) 00199 { 00200 SampleDiff(); 00201 *plotColour = HSVCol(hsvRed, 0.5, 0.5); 00202 plotPane.Redraw(); 00203 } 00204 else if (object == add) 00205 { 00206 SampleAdd(); 00207 *plotColour = HSVCol(hsvGreen, 0.5, 0.5); 00208 plotPane.Redraw(); 00209 } 00210 else if (object == dump) 00211 { 00212 StrConst result; 00213 RGBAImage image; 00214 FILE *file; 00215 00216 result = fl_show_fselector("Saving plot data file...", 0, 00217 "*.tif,*.tiff", "plot.tif"); 00218 00219 if (result) 00220 { 00221 cout << "*** dumping plot image to " << result << endl; 00222 #if defined(GCL_MESA) && defined(RAD_MINE) 00223 MesaRenderer mr; 00224 RGBAImage hqImage; 00225 00226 image.SetSize(2 * plotPane.width, 2 * plotPane.height); 00227 hqImage.SetSize(plotPane.width, plotPane.height); 00228 mr.Init(image); 00229 mr.SetHeadlight(true); 00230 mr.SetDoubleSided(true); 00231 mr.SetBgColour(plotPane.GetBgColour()); 00232 mr.Clear().Draw(plotPane.ItsScene()).Show(); 00233 image.DownSample(hqImage); 00234 00235 hqImage.Save(FileName().SetPath(result)); 00236 #else 00237 plotPane.MakeCurrent(); 00238 plotPane.GetImage(image); 00239 image.Save(FileName().SetPath(result)); 00240 #endif 00241 } 00242 } 00243 else if (object == dataDump) 00244 { 00245 StrConst result; 00246 ofstream fout; 00247 00248 result = fl_show_fselector("Saving plot data file...", 0, "*.data", 00249 "plot.data"); 00250 00251 if (result) 00252 { 00253 cout << "*** dumping plot data to " << result << endl; 00254 fout.open(result); 00255 if (fout) 00256 { 00257 Dump(fout); 00258 fout.close(); 00259 } 00260 else 00261 perror("Couldn't open output file"); 00262 } 00263 } 00264 } 00265