File: box.cpp

package info (click to toggle)
ghemical 1.01-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,984 kB
  • ctags: 19,443
  • sloc: ansic: 69,073; cpp: 60,583; fortran: 35,324; sh: 5,419; makefile: 506; perl: 91
file content (129 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download
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
/**********************************************************************
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

#include "mol.h"

using namespace std;

namespace OpenBabel 
{

bool ReadBox(istream &ifs, OBMol &mol,char *title)
{
  char buffer[BUFF_SIZE];
  vector<string> vs;
  vector<string>::iterator i;
  OBAtom atom;

  mol.BeginModify();
  
  while (ifs.getline(buffer,BUFF_SIZE) && !EQn(buffer,"END",3))
    {
      if (EQn(buffer,"ATOM",4))
	{
	  string sbuf = &buffer[6]; 
	  /* X, Y, Z */
	  string x = sbuf.substr(24,8);
	  string y = sbuf.substr(32,8);
	  string z = sbuf.substr(40,8);
	  Vector v(atof(x.c_str()),atof(y.c_str()),atof(z.c_str()));
	  atom.SetVector(v);
	  if (!mol.AddAtom(atom)) return(false);
	}
    
      if (EQn(buffer,"CONECT",6))
	{
	  tokenize(vs,buffer);
	  if (!vs.empty() && vs.size() > 2)
	    for (i = vs.begin(),i+=2;i != vs.end();i++)
	      mol.AddBond(atoi(vs[1].c_str()),atoi((*i).c_str()),1);
	}
    }

  mol.EndModify();
  mol.SetTitle(title);
  return(true);
}

bool WriteBox(ostream &ofs,OBMol &mol,float margin)
{
  char buffer[BUFF_SIZE];
  Vector vcenter,vmin,vmax,vmid,vdim;
  
  OBAtom *atom;
  vector<OBNodeBase*>::iterator i;
  vmax.Set(-10E10,-10E10,-10E10);
  vmin.Set( 10E10, 10E10, 10E10);

  for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
    {
      vcenter += atom->GetVector();
      if (atom->x() < vmin.x()) vmin.SetX(atom->x());
      if (atom->y() < vmin.y()) vmin.SetY(atom->y());
      if (atom->z() < vmin.z()) vmin.SetZ(atom->z());

      if (atom->x() > vmax.x()) vmax.SetX(atom->x());
      if (atom->y() > vmax.y()) vmax.SetY(atom->y());
      if (atom->z() > vmax.z()) vmax.SetZ(atom->z());
    }
  vcenter /= (float)mol.NumAtoms();

  Vector vmarg(margin,margin,margin);
  vmin -= vmarg; vmax += vmarg;
  vdim = vmax - vmin;
  vmid = vmin+vmax; vmid /= 2.0f;

  ofs << "HEADER    CORNERS OF BOX" << endl;
  sprintf(buffer,"REMARK    CENTER (X Y Z)      %10.3f %10.3f %10.3f",
	  vmid.x(),vmid.y(),vmid.z()); ofs << buffer << endl;
  sprintf(buffer,"REMARK    DIMENSIONS (X Y Z)  %10.3f %10.3f %10.3f",
	  vdim.x(),vdim.y(),vdim.z()); ofs << buffer << endl;
  vdim /= 2.0f;

  Vector vtmp;
  int j;
  for (j = 1;j <= 8;j++)
    {
      switch(j)
	{
	case 1: vtmp = vmid-vdim; break;
	case 2: vtmp.SetX(vmid.x()+vdim.x()); break;
	case 3: vtmp.SetZ(vmid.z()+vdim.z()); break;
	case 4: vtmp.SetX(vmid.x()-vdim.x()); break;
	case 5: 
	  vtmp = vmid-vdim; vtmp.SetY(vmid.y()+vdim.y());
	  break;
	case 6: 
	  vtmp = vmid+vdim; vtmp.SetZ(vmid.z()-vdim.z());
	  break;
	case 7: vtmp = vmid+vdim; break;
	case 8: vtmp.SetX(vmid.x()-vdim.x()); break;
	}
      sprintf(buffer,"ATOM      %d  DUA BOX     1    %8.3f%8.3f%8.3f",
	      j,vtmp.x(),vtmp.y(),vtmp.z());
      ofs << buffer << endl;
    }

  ofs << "CONECT    1    2    4    5" << endl;
  ofs << "CONECT    2    1    3    6" << endl;
  ofs << "CONECT    3    2    4    7" << endl;
  ofs << "CONECT    4    1    3    8" << endl;
  ofs << "CONECT    5    1    6    8" << endl;
  ofs << "CONECT    6    2    5    7" << endl;
  ofs << "CONECT    7    3    6    8" << endl;
  ofs << "CONECT    8    4    5    7" << endl;
  
  return(true);
}

}