File size: 9,266 Bytes
a3f3d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef FREESASA_COORD_H
#define FREESASA_COORD_H

#ifdef __GNUC__
#define __attrib_pure__ __attribute__((pure))
#else
#define __attrib_pure__
#endif

/**
    @file
    @author Simon Mitternacht

    This is only for interal use, error handling is done by asserts.
    The header provides functions to store, copy and modify
    coordinates through the type ::coord_t.
    
    The distance calculation functions (freesasa_dist(),
    freesasa_dist2() and freesasa_dist2_12()) are useful for code that
    is not performance critical. When efficieny is a priority it is
    better to use freesasa_coord_all() to obtain a pointer to the
    array of coordinates and calculate directly using this..
 */

/** Store a set of 3-dimensional coordinates in a contiguous array */
typedef struct coord_t {
    /** number of 3-vectors */
    int n; 

    /** If these coordinates are only a link to an externally stored
        array this is 1, else 0. If it it is set, the coordinates can
        not be changed and the array not freed. */
    int is_linked; 

    /** array of all coordinates, dimension 3*n,
        x_1,y_1,z_1,...,x_n,y_n,z_n. */
    double *xyz; 
} coord_t;

/**
    Initialize new ::coord_t object.

    Return value is dynamically allocated, should be freed with
    freesasa_coord_free().
    
    @return An empty ::coord_t object. Returns NULL if out of
    memory.
 */
coord_t * 
freesasa_coord_new(void);

/**
   Free resources allocated by ::coord_t object.

   Will not free the coordinate array itself if it was initialized by
   freesasa_coord_new_linked().
 */
void
freesasa_coord_free(coord_t *coord);

/**
   Copy coordinates.

   Creates a new ::coord_t object that is a copy of the
   argument `src`.

   Return value is dynamically allocated, should be freed with
   freesasa_coord_free().

   @param src Coordinates to be copied.
   @return Copy of coordinates. NULL if out of memory.
 */
coord_t *
freesasa_coord_copy(const coord_t *src);

/**
    Creates a `const` ::coord_t-object that is linked to an array of
    coordinates owned by callee.

    This allows repeated calculations on a changing object without
    reinitialization. The returned ::coord_t-pointer is not
    explicitly const, to allow it to be freed later, but objects
    initiated through this interface will not change their
    coordinates. It also allows ::coord_t to be used as a wrapper
    for an array controlled by the caller.

    Return value is dynamically allocated, should be freed with
    freesasa_coord_free().

    @param xyz Array of coordinates x1,y1,z1,x2,y2,z2,...
    @param n Number of coordinates (array has size 3*n).
    @return New linked ::coord_t object. NULL if out of memory.
 */
coord_t *
freesasa_coord_new_linked(const double *xyz,
                          int n);

/**
    Append coordinates to ::coord_t object from one array.

    @param coord A ::coord_t object
    @param xyz Array of coordinates x1,y1,z1,x2,y2,z2,...
    @param n Number of coordinates (array has size 3*n).
    @return FREESASA_SUCCESS if successful, FREESASA_FAIL if out of memory.
 */
int
freesasa_coord_append(coord_t *coord,
                      const double *xyz,
                      int n);

/**
    Append coordinates to ::coord_t object from three
    separate arrays.

    @param coord A ::coord_t object
    @param x Array of x-coordinates
    @param y Array of x-coordinates
    @param z Array of x-coordinates
    @param n Size of arrays x, y and z.
    @return FREESASA_SUCCESS if successful, FREESASA_FAIL if out of memory.
 */
int
freesasa_coord_append_xyz(coord_t *coord,
                          const double *x,
                          const double *y,
                          const double *z,
                          int n);

/**
    Set given coordinate.

    Bounds-checking of i is handled by asserts for efficiency,
    i.e. only done in debug-mode.

    @param coord A ::coord_t object
    @param i Index
    @param xyz Array with coordinates x,y,z.
 */
void
freesasa_coord_set_i(coord_t *coord,
                     int i,
                     const double* xyz);

/**
    Set given coordinate.

    Bounds-checking of i is handled by asserts for efficiency,
    i.e. only done in debug-mode.

    @param coord A ::coord_t object
    @param i Index
    @param x x-coordinate.
    @param y y-coordinate.
    @param z z-coordinate
 */
void
freesasa_coord_set_i_xyz(coord_t *coord,
                         int i,
                         double x,
                         double y,
                         double z);

/**
    Reset everything.

    Allocates memory to allow array of size n.
    
    @param coord A ::coord_t object
    @param xyz Array of coordinates x1,y1,z1,x2,y2,z2,...
    @param n Number of coordinates (array has size 3*n).
    @return FREESASA_SUCCESS if successful, FREESASA_FAIL if out of memory.
 */
int
freesasa_coord_set_all(coord_t *coord,
                       const double* xyz,
                       int n);

/**
    Reset everything.

    Allocates memory to allow array of size n.    

    @param coord A ::coord_t object
    @param x Array of x-coordinates
    @param y Array of x-coordinates
    @param z Array of x-coordinates
    @param n Size of arrays x, y and z.
    @return FREESASA_SUCCESS if successful, FREESASA_FAIL if out of memory.
 */
int
freesasa_coord_set_all_xyz(coord_t *coord,
                           const double* x,
                           const double *y,
                           const double *z,
                           int n);

/**
    Set length of a given coordinate vector. Useful for test-points in
    S&R.

    Bounds-checking of `i` and `length` is handled by asserts for
    efficiency, i.e. only done in debug-mode.

    @param coord A ::coord_t object
    @param i Index
    @param length Desired length (>= 0)
 */
void
freesasa_coord_set_length_i(coord_t *coord,
                            int i,
                            double length);

/**
    Set length of all coordinate vectors to the same length. 

    This means all coordinates are on the same sphere.

    Bounds-checking of length is handled by asserts for
    efficiency, i.e. only done in debug-mode.

    @param coord A ::coord_t object
    @param length Desired length (>= 0)
 */
void
freesasa_coord_set_length_all(coord_t *coord,
                              double length);

/**
    Coordinates for a given index.

    Bounds-checking of `i` is handled by asserts for efficiency,
    i.e. only done in debug-mode.

    @param coord A ::coord_t object
    @param i Index
    @return Array with coordinates x,y,z
 */
const double*
freesasa_coord_i(const coord_t *coord,
                 int i);

/**
    Calculate distance between two coordinate vectors. For speed,
    arguments aren't checked.
    
    @param coord A ::coord_t object
    @param i first index
    @param j second index
    @return Distance between coorsinate i and j.
*/
double freesasa_coord_dist(const coord_t *coord,
                           int i,
                           int j)
    __attrib_pure__;

/**
    Calculate square distance between two coordinate vectors. For
    speed, arguments aren't checked.
    
    @param coord A ::coord_t object
    @param i First index
    @param j Second index
    @return Square distance between coordinate i and j
*/
double
freesasa_coord_dist2(const coord_t *coord,
                     int i,
                     int j)
    __attrib_pure__;

/**
    Calculate square distance between two coordinate vectors in
    separate coordinate sets. For speed, arguments aren't checked.
    
    @param c1 First set of coordinates
    @param c2 Second set of coordinates
    @param i1 Index in first set
    @param i2 Index in second set
    @return Square distance between coordinates i1 and i2
*/
double freesasa_coord_dist2_12(const coord_t *c1,
                               const coord_t *c2,
                               int i1,
                               int i2)
    __attrib_pure__;

/**
    All coordinates as an array.

    @param coord A ::coord_t object
    @return Array of coordinates x1,y1,z1,x2,y2,z2,...
 */
const double*
freesasa_coord_all(const coord_t *coord) __attrib_pure__;

/**
    Number of coordinates.
    
    @param coord A ::coord_t object
    @return Number of coordinates
 */
int
freesasa_coord_n(const coord_t *coord) __attrib_pure__;

/**
    Translate all coordinates by same vector.

    @param coord A ::coord_t object
    @param xyz Array describing translation vector x,y,z.
 */
void
freesasa_coord_translate(coord_t *coord,
                         const double *xyz);

/**
    Translate all coordinates by same vector.

    @param coord A ::coord_t object
    @param x x-coordinate of translation vector
    @param y y-coordinate of translation vector
    @param z z-coordinate of translation vector
 */
void
freesasa_coord_translate_xyz(coord_t *coord,
                             double x,
                             double y,
                             double z);

/**
    Scale all coordinates by given factor.
    
    @param coord A ::coord_t object
    @param a Factor to scale by
 */
void
freesasa_coord_scale(coord_t *coord,
                     double a);

#undef __attrib_pure__

#endif