Skip to content

Commit

Permalink
Deduplicate egs_view file loading code
Browse files Browse the repository at this point in the history
And in the process, give it rudimentary command line help.
  • Loading branch information
M committed Feb 13, 2016
1 parent ac9661a commit 7df6720
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 94 deletions.
95 changes: 8 additions & 87 deletions HEN_HOUSE/egs++/view/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,108 +71,29 @@ void my_info_function(const char *msg,...) {
#endif

int main(int argc, char **argv) {
if (argc >= 2 && (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1],"--help") == 0)) {
egsFatal("Usage: %s [geometry_file] [tracks_file]\n", argv[0]);
return 1;
}

QApplication a(argc, argv);
QString input_file = argc >= 2 ? QString(argv[1]) :
QFileDialog::getOpenFileName(NULL,"Select geometry definition file");
QString tracks_file = argc >= 3 ? argv[2] : "";
//if( argc < 2 ) egsFatal("\nUsage: %s geometry_file\n\n",argv[0]);
//QFile file(argv[1]);

#ifdef VDEBUG
debug_output << "Using " << input_file.toLatin1().data() << "\n";
debug_output << "Using " << input_file.latin1() << "\n";
egsSetInfoFunction(Information,my_info_function);
egsSetInfoFunction(Warning,my_info_function);
egsSetInfoFunction(Fatal,my_fatal_function);
#endif

QFile file(input_file);
if (!file.exists()) {
egsFatal("\nFile %s does not exist\n\n",argv[1]);
}

#ifdef VDEBUG
debug_output << "About to construct EGS_Input object\n";
#endif
EGS_Input input;
#ifdef VDEBUG
debug_output << "OK, parsing input\n";
#endif

//input.setContentFromFile(argv[1]);
input.setContentFromFile(input_file.toUtf8().constData());
#ifdef VDEBUG
debug_output << "Finished parsing\n";
#endif
#ifdef VIEW_DEBUG
input.print(0,cerr);
#endif

EGS_BaseGeometry *g = EGS_BaseGeometry::createGeometry(&input);
#ifdef VDEBUG
debug_output << "Got geometry\n";
#endif
if (!g) egsFatal("\nThe input file %s seems to not define a valid"
" geometry\n\n",argv[1]);

EGS_Float xmin = -50, xmax = 50;
EGS_Float ymin = -50, ymax = 50;
EGS_Float zmin = -50, zmax = 50;
EGS_Input *vc = input.takeInputItem("view control");
std::vector<EGS_UserColor> user_colors;
if (vc) {
EGS_Float tmp;
if (!vc->getInput("xmin",tmp)) {
xmin = tmp;
}
if (!vc->getInput("xmax",tmp)) {
xmax = tmp;
}
if (!vc->getInput("ymin",tmp)) {
ymin = tmp;
}
if (!vc->getInput("ymax",tmp)) {
ymax = tmp;
}
if (!vc->getInput("zmin",tmp)) {
zmin = tmp;
}
if (!vc->getInput("zmax",tmp)) {
zmax = tmp;
}
EGS_Input *uc;
while ((uc = vc->takeInputItem("set color")) != 0) {
vector<string> inp;
int err = uc->getInput("set color",inp);
if (!err && (inp.size() == 4 || inp.size() == 5)) {
qDebug("found color input %s %s %s %s",inp[0].c_str(),inp[1].c_str(),inp[2].c_str(),inp[3].c_str());
EGS_UserColor ucolor;
ucolor.medname = inp[0];
sscanf(inp[1].c_str(),"%d",&ucolor.red);
sscanf(inp[2].c_str(),"%d",&ucolor.green);
sscanf(inp[3].c_str(),"%d",&ucolor.blue);
if (inp.size() == 5) {
sscanf(inp[4].c_str(),"%d",&ucolor.alpha);
}
else {
ucolor.alpha = 255;
}
qDebug("Using rgb=(%d,%d,%d %d) for medium %s",ucolor.red,ucolor.green,ucolor.blue,
ucolor.alpha,ucolor.medname.c_str());
user_colors.push_back(ucolor);
}
else {
qWarning("Wrong 'set color' input");
}
delete uc;
}
delete vc;
}

GeometryViewControl w;
w.show();
w.setFilename(input_file);
w.setTracksFilename(tracks_file);
if (w.setGeometry(g,user_colors,xmin,xmax,ymin,ymax,zmin,zmax,0)) {
if (!w.loadInput(false)) {
return 1;
}

Expand Down
17 changes: 11 additions & 6 deletions HEN_HOUSE/egs++/view/viewcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,12 @@ GeometryViewControl::GeometryViewControl(QWidget *parent, const char *name)
GeometryViewControl::~GeometryViewControl() {
}

void GeometryViewControl::reloadInput() {

bool GeometryViewControl::loadInput(bool reloading) {
// check that the file (still) exists
QFile file(filename);
if (!file.exists()) {
egsWarning("\nFile %s does not exist anymore!\n\n",filename.toUtf8().constData());
return;
return false;
}

// read the input file again
Expand Down Expand Up @@ -271,8 +270,14 @@ void GeometryViewControl::reloadInput() {
}
// Start loading process
gview->restartWorker();
setGeometry(newGeom,user_colors,xmin,xmax,ymin,ymax,zmin,zmax,1);
setGeometry(newGeom,user_colors,xmin,xmax,ymin,ymax,zmin,zmax,reloading);
reloadButton->blockSignals(false);
// check that the file (still) exists
return true;
}

void GeometryViewControl::reloadInput() {
loadInput(true);
}

void GeometryViewControl::setFilename(QString str) {
Expand Down Expand Up @@ -797,7 +802,7 @@ int GeometryViewControl::setGeometry(
EGS_BaseGeometry *geom,
const std::vector<EGS_UserColor> &ucolors,
EGS_Float xmin, EGS_Float xmax, EGS_Float ymin, EGS_Float ymax,
EGS_Float zmin, EGS_Float zmax, int justReloading) {
EGS_Float zmin, EGS_Float zmax, bool justReloading) {
if (!geom) {
egsWarning("setGeometry(): got null geometry\n");
return 1;
Expand Down Expand Up @@ -845,7 +850,7 @@ int GeometryViewControl::setGeometry(
materialCB->clear();
m_colors = new QRgb [nmed];
for (int j=0; j<nmed; j++) {
materialCB->insertItem(j, g->getMediumName(j));
materialCB->insertItem(j,g->getMediumName(j));
}
int nstandard = sizeof(standard_red)/sizeof(unsigned char);
int js = 0;
Expand Down
3 changes: 2 additions & 1 deletion HEN_HOUSE/egs++/view/viewcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class GeometryViewControl : public QDialog, public Ui::GeometryViewControl {
virtual void setLookAtLineEdit();
virtual void updateLookAtLineEdit();
virtual void setMaterialColor(int j);
virtual int setGeometry(EGS_BaseGeometry *geom, const std::vector<EGS_UserColor> &ucolors, EGS_Float xmin, EGS_Float xmax, EGS_Float ymin, EGS_Float ymax, EGS_Float zmin, EGS_Float zmax, int justReloading);
virtual int setGeometry(EGS_BaseGeometry *geom, const std::vector<EGS_UserColor> &ucolors, EGS_Float xmin, EGS_Float xmax, EGS_Float ymin, EGS_Float ymax, EGS_Float zmin, EGS_Float zmax, bool justReloading);
virtual void updateView(bool transform = false);
virtual bool loadInput(bool first_time);

public slots:

Expand Down

0 comments on commit 7df6720

Please sign in to comment.