File: main.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (366 lines) | stat: -rw-r--r-- 8,728 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

// order of includes is important: first qapplication, than BALL includes
#include <QtWidgets/QApplication>

#include <BALL/SYSTEM/path.h>
#include <BALL/SYSTEM/directory.h>
#include "mainframe.h"

#include <BALL/FORMAT/DCDFile.h>
#include <BALL/MOLMEC/COMMON/snapShot.h>
#include <BALL/MOLMEC/COMMON/snapShotManager.h>
#include <BALL/VIEW/RENDERING/POVRenderer.h>

#include <iostream>
#include <sys/wait.h>

using namespace BALL;
using namespace BALL::VIEW;

void showUsage()
{
 	Log.insert(std::cerr);
	Log.error() << (Sting)qApp->translate("BALL::VIEW::DCD2PNG", "DCD2PNG DCDFILE.dcd  file.[bvp|pdb|hin]  [DCD2PNG.ini]") << std::endl;
	Log.error() << (Sting)qApp->translate("BALL::VIEW::DCD2PNG", "Read a BALLView project or a molecular file format and create PNG images from it by using POVRay.") << std::endl;
 	Log.remove(std::cerr);
}


int main(int argc, char **argv)
{
	if (argc < 3)
	{
		showUsage();
		return 1;
	}

	// =============== testing if we can write in current directoy =====================
	String home_dir;
	try
	{
		BALL::String temp_file_name;
		BALL::File::createTemporaryFilename(temp_file_name);
		BALL::File out(temp_file_name, std::ios::out);
		out << "test" << std::endl;
		out.remove();
	}
	catch(...)
	{
		// oh, we have a problem, look for the users home dir
		home_dir = BALL::Directory::getUserHomeDir();

		if (home_dir == "")
		{
			Log.error() << (Sting)qApp->translate("BALL::VIEW::DCD2PNG", 
					"You dont have write access to the current working directory")<< "\n" <<
					(Sting)qApp->translate("BALL::VIEW::DCD2PNG", "and I can not find your home directory. This can cause") << "\n" <<
					(Sting)qApp->translate("BALL::VIEW::DCD2PNG", "unexpected behaviour. Please start DCD2PNG from your homedir with") << "\n" << 
					(Sting)qApp->translate("BALL::VIEW::DCD2PNG", "absolute path.") << "\n";
			return -1;
		}
	}

	// =============== initialize Mainframe ============================================
	BALL::Directory dir;
	QApplication application(argc, argv);
	BALL::Mainframe* mainframe = new Mainframe();
	mainframe->setIdentifier("MAIN");
	mainframe->registerThis();

 	mainframe->hide();

	// if we need to use the users homedir as working dir, do so
	if (home_dir != "")
	{
		mainframe->setWorkingDir(home_dir);
	}

	// =============== parsing command line arguments ==================================
	String dcd_file_name;
	String molecular_file_name;
	String working_dir = ".";
	String project_file = "";
	String inifile_name = dir.getPath() + BALL::FileSystem::PATH_SEPARATOR + "DCD2PNG.ini";
	bool error = false;
	System* system = 0;
	Position nr = 100000000;

	for (BALL::Index i = 1; i < argc && !error; ++i)
	{
		BALL::String argument(argv[i]);
		if (argument.hasSuffix(".bvp"))
		{
		 	mainframe->loadBALLViewProjectFile(argument);
			project_file = argument;
			continue;
		}

		if (argument.hasSuffix(".ini"))
		{
			inifile_name = argument;
			continue;
		}

		if (argument.hasSuffix(".pdb") ||
		    argument.hasSuffix(".hin"))
		{
			MolecularFileDialog* mfd = MolecularFileDialog::getInstance(0);
			if (mfd == 0) return 0;
			system = mfd->openMolecularFile(argument);
			if (system == 0)
			{
				std::cerr << (Sting)qApp->translate("BALL::VIEW::DCD2PNG", "Could not open file") << ": " << argument << std::endl;
				error = true;
				break;
			}

			molecular_file_name = argument;
			continue;
		}
		
		if (argument.hasSuffix(".dcd"))
		{
			dcd_file_name = argument;
			continue;
		}

		if (argument.hasPrefix("-s"))
		{
			argument.after("-s");
			try
			{
				nr = argument.toUnsignedInt();
			}
			catch(...)
			{
			}
			continue;
		}
			
		error = true;
	}

	if ((molecular_file_name == "" && project_file == "") ||
			dcd_file_name == "" ||
			error)
	{
		showUsage();
		application.quit();
		return 0;
	}

	if (system == 0)
	{
		if (mainframe->getCompositeManager().getNumberOfComposites() == 0)
		{
			std::cerr << "No molecular entities stored in project file and no molecular file given..." << std::endl;
			showUsage();
			return -1;
		}

		system = (System*) *mainframe->getCompositeManager().begin();
	}

	Size width = 720;
	Size height = 480;
	String povray_options = "-V +FN +QR +A0.3 -UV -D +I- ";
	String povray_exec;
	INIFile inifile(inifile_name);
	if (!inifile.read())
	{
		std::cerr << "Could not find DCD2PNG.ini file, please specify the absolute path." << std::endl;
		return -1;
	}

	if (inifile.hasEntry("DCD2PNG", "WORKINGDIR"))
	{
		String argument = inifile.getValue("DCD2PNG", "WORKINGDIR");
		Directory d(argument);
		if (d.isValid())
		{
			working_dir = argument;
		}
		else
		{
			d.set(argument);
			if (d.isValid())
			{
				working_dir = argument;
			}
			else
			{
				std::cerr << "Could not read directory for images from DCD2PNG.ini!" << std::endl;
				return -1;
			}
		}
	}

	try
	{
		if (inifile.hasEntry("DCD2PNG", "WIDTH"))
		{
			String argument = inifile.getValue("DCD2PNG", "WIDTH");
			width = argument.toUnsignedInt();
		}
		if (inifile.hasEntry("DCD2PNG", "HEIGHT"))
		{
			String argument = inifile.getValue("DCD2PNG", "HEIGHT");
			height = argument.toUnsignedInt();
		}
	}
	catch(...)
	{
		std::cerr << "Could not read values for width and height from INIFile!" << std::endl;
	}

	if (inifile.hasEntry("DCD2PNG", "POVRAY_OPTIONS"))
	{
//   		povray_options = inifile.getValue("DCD2PNG", "POVRAY_OPTIONS") + " +I- ";
		povray_options = inifile.getValue("DCD2PNG", "POVRAY_OPTIONS") + " +Iasd.pov ";
	}

	if (inifile.hasEntry("DCD2PNG", "POVRAY"))
	{
		povray_exec = inifile.getValue("DCD2PNG", "POVRAY");
	}

	if (!File::isAccessible(povray_exec))
	{
		std::cout << "Could not find POVRay at the given location: " << povray_exec << std::endl;
		std::cout << "Please modify the settings in DCD2PNG.ini" << std::endl;
		return -1;
	}

	Scene::getInstance(0)->resize(width, height);
	povray_options +=	"+W" + String(width) + " +H" + String(height) + " +O" + working_dir + FileSystem::PATH_SEPARATOR;
	POVRenderer pov;
	pov.setHumanReadable(false);

	Position nr2 = 0;

	DCDFile dcdfile(dcd_file_name);
	SnapShotManager sm(system, 0, &dcdfile);
	sm.applyFirstSnapShot();

	vector<String> strings;
	Size s = povray_exec.split(strings, String(FileSystem::PATH_SEPARATOR).c_str());
	if (s < 2)
	{
		std::cerr << "Error: Specify in DCD2PNG.ini the absolute path to povray." << std::endl;
		std::cerr << "e.g. POVRAY=/usr/bin/povray" << std::endl;
		return -1 ;
	}
	String pov_exec2 = strings[s - 1];

	error = false;
	while(sm.applyNextSnapShot() && ! error)
	{
		String pov_arg = povray_options + String(nr) + ".png" ;

		std::stringstream pov_renderer_output;
//   		pov.setOstream(pov_renderer_output);
 		pov.setFileName("asd.pov");
 		Scene::getInstance(0)->exportScene(pov);

		int writepipe[2];
		pid_t childpid;
	
		if (pipe(writepipe) < 0 ) 
		{
			std::cerr << "Could not pipe!" << std::endl;
			return -1;
		}
	
		// abort, if we can not fork!
		if ( (childpid = fork()) < 0) 
		{
			std::cerr << "Could not fork!" << std::endl;
			return -1;
		}
		else if (childpid != 0)
		{
			// we are in the parent process and wait for the child to finish
			
			// Parent closes it's read end of the pipe
 			close (writepipe[0]); 
			
			sleep(1);

			while (pov_renderer_output.good())
			{
				char buffer[1000];
				pov_renderer_output.getline(buffer, 1000);

				Size n = strlen(buffer);
				if(write(writepipe[1], buffer, n) != (int) n) 
				{ 
					std::cerr << "Could not write to pipe!" << std::endl;
					return -1;
				}

				write(writepipe[1], "\n", 1);	
			}

			// Parent closes it's write end of the pipe, this sends EOF to reader
			close (writepipe[1]); 

			int status;
			int result = waitpid( -1, &status, 0 );

			if (result <= 1) 
			{
				std::cerr << "No child process!" << std::endl;
			}
		}
		else
		{
			// we are in the child process and start povray
			
			// Child closes it's write end of the pipe
		 	close (writepipe[1]); 

			// handle stdin already closed
			if(writepipe[0] != STDIN_FILENO) 
			{
				if(dup2(writepipe[0], STDIN_FILENO) < 0) 
				{ 
 					std::cerr << "Could not dup2!" << std::endl;
 					return -1;
				} 

 				close(writepipe[0]);
			}

			std::cerr << "Calling " << povray_exec << " " << pov_arg << std::endl;
 	   	int result = execl (povray_exec.c_str(), pov_exec2.c_str(), pov_arg.c_str(), NULL);
			if (result == -1)
			{
				std::cout << "Could not exec POVRay! " << std::endl;
				error = true;
			}
			
			/*
			// for debugging:
 			std::cout << std::endl ;
  		execl ("/bin/cat", "cat", 0);
			error = true;
			*/
		}

		nr++;
		nr2++;
	}

	std::cout << "Written " + String(nr2) + " images." << std::endl;
	
	/*
	mainframe->show();
 	return application.exec();
	*/

	return 0;
}