File: Export.c

package info (click to toggle)
pymol 1.7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 42,668 kB
  • ctags: 25,775
  • sloc: ansic: 494,779; python: 75,446; cpp: 20,088; makefile: 351; sh: 172; csh: 21
file content (230 lines) | stat: -rw-r--r-- 6,313 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
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

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

/* Example of embedding Python in another program */
#include"os_python.h"

#include"os_predef.h"

#include"os_std.h"

#include"MemoryDebug.h"
#include"Base.h"
#include"Err.h"
#include"Export.h"
#include"RepDot.h"
#include"Executive.h"
#include"ObjectMolecule.h"
#include"Scene.h"

void ExportDotsObjFree(PyMOLGlobals * G, ExportDotsObj * rec);


/*--------------------------------------------------------------------- */


/* routines for shuttling coordinates to and from molecular mechanics routines */

ExportCoords *ExportCoordsExport(PyMOLGlobals * G, char *name, int state, int order)
{
  ExportCoords *io = NULL;
  ObjectMolecule *obj;
  CoordSet *cs;
  int a, a0;
  float *crd0, *crd1;

  obj = ExecutiveFindObjectMoleculeByName(G, name);
  if(obj && (state >= 0) && (state < obj->NCSet) && (!obj->DiscreteFlag)
     && obj->CSet[state]) {
    cs = obj->CSet[state];
    io = (ExportCoords *) mmalloc(sizeof(ExportCoords));

    if(io) {
      io->nAtom = cs->NIndex;
      io->coord = Alloc(float, cs->NIndex * 3);

      if(io->coord) {
        crd0 = cs->Coord;
        crd1 = io->coord;
        if(order) {
          /* Coordinate Set Order */
          for(a = 0; a < cs->NIndex; a++) {
            *(crd1++) = *(crd0++);
            *(crd1++) = *(crd0++);
            *(crd1++) = *(crd0++);
          }
        } else {
          /* PyMOL Atom Order */
          for(a = 0; a < obj->NAtom; a++) {
            a0 = cs->AtmToIdx[a];
            if(a0 >= 0) {
              crd0 = cs->Coord + 3 * a0;
              *(crd1++) = *(crd0++);
              *(crd1++) = *(crd0++);
              *(crd1++) = *(crd0++);
            }
          }
        }
      }
    }
  }
  return io;
}

int ExportCoordsImport(PyMOLGlobals * G, char *name, int state, ExportCoords * io,
                       int order)
{
  int result = false;
  ObjectMolecule *obj;
  CoordSet *cs;
  int a, a0, cc;
  float *crd0, *crd1;

  obj = ExecutiveFindObjectMoleculeByName(G, name);
  if(io) {
    if(!obj) {
      result = ErrMessage(G, "ExportCoordsImport", "invalid object");
    } else {
      if((state < 0) || (state >= obj->NCSet) || (obj->DiscreteFlag)) {
        result = ErrMessage(G, "ExportCoordsImport", "invalid state for object.");
      } else {
        if(!obj->CSet[state]) {
          result = ErrMessage(G, "ExportCoordsImport", "empty state.");
        } else {
          cs = obj->CSet[state];
          if(cs->NIndex != io->nAtom) {
            result = ErrMessage(G, "ExportCoordsImport", "atom count mismatch.");
            PRINTF "ExportCoordsImport: cset %d != io %d \n", cs->NIndex,
              io->nAtom ENDF(G);
          } else {

            crd0 = cs->Coord;
            crd1 = io->coord;
            if(order) {
              /* Coordinate Set Ordering */
              for(a = 0; a < cs->NIndex; a++) {
                *(crd0++) = *(crd1++);
                *(crd0++) = *(crd1++);
                *(crd0++) = *(crd1++);
              }
            } else {
              cc = cs->NIndex;  /* array range safety */
              /* PyMOL Atom Ordering */
              for(a = 0; a < obj->NAtom; a++) {
                a0 = cs->AtmToIdx[a];
                if((a0 >= 0) && (cc--)) {
                  crd0 = cs->Coord + 3 * a0;
                  *(crd0++) = *(crd1++);
                  *(crd0++) = *(crd1++);
                  *(crd0++) = *(crd1++);
                }
              }
            }

            if(cs->fInvalidateRep)
              cs->fInvalidateRep(cs, cRepAll, cRepInvAll);
            SceneChanged(G);
            result = true;
          }
        }
      }
    }
  }
  return (result);
}

void ExportCoordsFree(ExportCoords * io)
{
  if(io) {
    FreeP(io->coord);
    FreeP(io);
  }
}

ExportDotsObj *ExportDots(PyMOLGlobals * G, char *name, int csIndex)
{
  CObject *obj;
  ObjectMolecule *objMol;
  RepDot *rep;
  CoordSet *cs = NULL;
  ExportDotsObj *result = NULL;
  int ok = true;

  obj = ExecutiveFindObjectByName(G, name);
  if(!obj)
    ok = ErrMessage(G, "ExportDots", "Not a valid object name.");
  else if(obj->type != cObjectMolecule)
    ok = ErrMessage(G, "ExportDots", "Not molecule object.");

  if(ok) {
    /*   ExecutiveSetRepVisib(G,name,cRepDot,1); */
    objMol = (ObjectMolecule *) obj;
    cs = ObjectMoleculeGetCoordSet(objMol, csIndex);
    if(!cs)
      ok = ErrMessage(G, "ExportDots", "Invalid coordinate set number.");
  }

  if(ok) {
    rep = (RepDot *) RepDotDoNew(cs, cRepDotAreaType, -1);
    if(!rep)
      ok = ErrMessage(G, "ExportDots", "Couldn't get dot representation.");
    else {
      result = Alloc(ExportDotsObj, 1);
      ErrChkPtr(G, result);
      result->export_.fFree = (void (*)(struct Export *)) ExportDotsObjFree;
      /* cannabilize the data structures */
      result->point = rep->V;
      rep->V = NULL;
      result->normal = rep->VN;
      rep->VN = NULL;
      result->type = rep->T;
      rep->T = NULL;
      result->flag = rep->F;
      rep->F = NULL;
      result->area = rep->A;
      rep->A = NULL;
      result->nPoint = rep->N;
      rep->R.fFree((Rep *) rep);        /* free the remaining structures */
    }
  }
  return (result);
}


/*---------------------------------------------------------------------*/

void ExportDotsObjFree(PyMOLGlobals * G, ExportDotsObj * obj)
{
  if(obj) {
    FreeP(obj->point);
    FreeP(obj->normal);
    FreeP(obj->type);
    FreeP(obj->flag);
    FreeP(obj->area);
  }
}

void ExportDeleteMDebug(PyMOLGlobals * G, Export * ex)
{
  if(ex)
    if(ex->fFree)
      ex->fFree(ex);
  FreeP(ex);
}


/*--------------------------------------------------------------------- */