* add the axis source test
[hkl.git] / src / holder.c
blob4185975bbcff79d9383a7f5de9bc4cf1927c62ee
1 #include <string.h>
3 #include "config.h"
4 #include "holder.h"
5 #include "svector.h"
6 #include "smatrix.h"
7 #include "quaternion.h"
9 /* private hkl_holder part */
10 void hkl_holder_grow(struct hkl_holder * holder, size_t extra)
12 if (holder->len + extra <= holder->len)
13 die("you want to use way too much memory");
14 if (!holder->alloc)
15 holder->idx = NULL;
16 ALLOC_GROW(holder->idx, holder->len + extra, holder->alloc);
19 void hkl_holder_init(struct hkl_holder * holder, size_t hint)
21 holder->len = 0;
22 holder->alloc = 0;
23 if (hint)
24 hkl_holder_grow(holder, hint);
27 void hkl_holder_release(struct hkl_holder * holder)
29 if (holder->alloc) {
30 free(holder->idx);
31 hkl_holder_init(holder, 0);
35 void hkl_holder_add_idx(struct hkl_holder * holder, size_t const idx)
37 hkl_holder_grow(holder, 1);
38 holder->len++;
39 holder->idx[holder->len] = idx;
40 holder->dirty = HKL_TRUE;
43 /* private hkl_holders part */
44 void hkl_holders_grow(struct hkl_holders * holders, size_t extra)
46 if (holders->len + extra <= holders->len)
47 die("you want to use way too much memory");
48 if (!holders->alloc)
49 holders->holders = NULL;
50 ALLOC_GROW(holders->holders, holders->len + extra, holders->alloc);
53 void hkl_holders_init(struct hkl_holders * holders, size_t hint)
55 holders->len = 0;
56 holders->alloc = 0;
57 if (hint)
58 hkl_holders_grow(holders, hint);
61 void hkl_holders_release(struct hkl_holders * holders)
63 size_t i;
65 if (holders->alloc) {
66 for(i=0;i<holders->len;i++)
67 hkl_holder_release(&holders->holders[i]);
68 free(holders->holders);
69 hkl_holders_init(holders, 0);
73 /* public part */
74 void hkl_holder_add_rotation_axe(struct hkl_holder * holder, char const * name, struct hkl_svector const * rot_axis)
76 size_t i;
77 size_t idx;
78 struct hkl_axis * axis = NULL;
80 idx = hkl_axis_get_idx_by_name(holder->axes, name);
81 if (idx >= 0) { // found it in the axe list
82 // check that both have the same rotation axe.
83 if (hkl_svector_cmp(&holder->axes->axes[idx].axis, rot_axis)) {
84 // check that the axis is not already in the holder
85 for(i=0; i<holder->len; i++)
86 if (idx == holder->idx[i])
87 die("can not add two times the \"%s\" axis to an holder.", name);
89 // add 1 element to ther holder
90 hkl_holder_add_idx(holder, idx);
91 } else
92 die("can not add two axis with the same name \"%s\" but different axes <%f, %f, %f> != <%f, %f, %f> into an holder",
93 name,
94 axis->axis.data[X], axis->axis.data[Y], axis->axis.data[Z],
95 rot_axis->data[X], rot_axis->data[Y], rot_axis->data[Z]);
96 } else {// not already in the axe list
97 // add a rotation axe to the holder->axes list
98 hkl_axes_add_rotation(holder->axes, name, rot_axis);
99 // set the right index in the holder idx list
100 hkl_holder_add_idx(holder, holder->axes->len);
104 struct hkl_holder * hkl_holders_add(struct hkl_holders * holders)
106 struct hkl_holder * holder;
108 hkl_holders_grow(holders, 1);
109 holder = &holders->holders[holders->len++];
111 return holder;