[binding] add hkl_quaternion_to_matrix in order to convert quaternion to matrix
[hkl.git] / hkl / hkl-binding.c
blobdcfda40f8a20fea3ce599ebce37127f918a6ec88
1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2012-2016 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #include <stdlib.h> // for malloc
24 #include <string.h> // for NULL, strdup
25 #include <sys/types.h> // for uint
26 #include "hkl-factory-private.h" // for __start_xautodata_factories, etc
27 #include "hkl-geometry-private.h" // for _HklGeometry, etc
28 #include "hkl-parameter-private.h"
29 #include "hkl-pseudoaxis-private.h" // for _HklEngine, HklEngineInfo
30 #include "hkl-sample-private.h" // for _HklSampleReflection, etc
31 #include "hkl.h" // for HklGeometry, HklEngine, etc
32 #include "hkl/ccan/autodata/autodata.h" // for autodata_get
33 #include "hkl/ccan/darray/darray.h" // for darray_foreach, darray_size
34 #include "hkl/ccan/list/list.h" // for list_for_each, list_head
36 /*****************/
37 /* HklQuaternion */
38 /*****************/
40 /**
41 * hkl_quaternion_to_matrix_binding:
42 * @self: the #HklQuaternion use to compute the #HklMatrix
44 * Rename to: hkl_quaternion_to_matrix
46 * Returns: (transfer full): the @HklQuaternion@ as a @HklMatrix@.
48 HklMatrix *hkl_quaternion_to_matrix_binding(const HklQuaternion *self)
50 HklMatrix *m = hkl_matrix_new();
52 hkl_quaternion_to_matrix(self, m);
54 return m;
58 /**************/
59 /* HklFactory */
60 /**************/
62 /**
63 * hkl_factories:
65 * return all the Hkl factories objects as a dictionnary
67 * Returns: (element-type utf8 Hkl.Factory) (transfer container):
68 **/
69 GHashTable *hkl_factories(void)
71 GHashTable *table = NULL;
72 size_t i, n;
73 HklFactory **factories;
75 table = g_hash_table_new(g_str_hash, g_str_equal);
76 factories = autodata_get(factories, &n);
77 for(i=0; i<n; ++i){
78 g_hash_table_insert(table,
79 (gpointer)hkl_factory_name_get(factories[i]),
80 factories[i]);
83 return table;
86 /************/
87 /* Geometry */
88 /************/
90 /**
91 * hkl_geometry_axis_names_get_binding:
92 * @self: the this ptr
93 * @length: (out caller-allocates): the length of the returned array
95 * get all the axes of the given geometry.
97 * Rename to: hkl_geometry_axis_names_get
99 * Returns: (array length=length) (transfer none): array of the axes names.
101 const char **hkl_geometry_axis_names_get_binding(const HklGeometry *self,
102 size_t *length)
104 const darray_string *axes = hkl_geometry_axis_names_get(self);
106 *length = darray_size(*axes);
108 return &darray_item(*axes, 0);
112 * hkl_geometry_axis_values_get_binding:
113 * @self: the this ptr
114 * @len: (out caller-allocates): the length of the returned array
115 * @unit_type: the unit type (default or user) of the returned value
117 * Rename to: hkl_geometry_axis_values_get
119 * Return value: (array length=len) (transfer container): list of axes values,
120 * free the list with free when done.
122 double *hkl_geometry_axis_values_get_binding(const HklGeometry *self, guint *len,
123 HklUnitEnum unit_type)
125 double *values;
126 uint i = 0;
127 HklParameter **axis;
129 if(!self || !len || darray_size(self->axes) == 0)
130 return NULL;
132 *len = darray_size(self->axes);
133 values = malloc(darray_size(self->axes) * sizeof(*values));
135 darray_foreach(axis, self->axes){
136 values[i++] = hkl_parameter_value_get(*axis, unit_type);
139 return values;
143 * hkl_geometry_sample_rotation_get_binding:
144 * @self: the self @HklGeometry@
145 * @sample: the rotated sample.
147 * return the rotation part of the given sample in the laboratory basis.
149 * Rename to: hkl_geometry_sample_rotation_get
151 * Returns: (transfer full): the rotation express as a quaternion.
153 HklQuaternion *hkl_geometry_sample_rotation_get_binding(const HklGeometry *self,
154 const HklSample *sample)
156 HklQuaternion q = hkl_geometry_sample_rotation_get(self, sample);
158 return hkl_quaternion_dup(&q);
162 * hkl_geometry_detector_rotation_get_binding:
163 * @self: the self @HklGeometry@
165 * return the rotation part of the given detector in the laboratory
166 * basis.
168 * Rename to: hkl_geometry_detector_rotation_get
170 * Returns: (transfer full): the rotation express as a quaternion.
172 HklQuaternion *hkl_geometry_detector_rotation_get_binding(const HklGeometry *self,
173 const HklDetector *detector)
175 HklQuaternion q = hkl_geometry_detector_rotation_get(self, detector);
177 return hkl_quaternion_dup(&q);
180 /*******************/
181 /* HklGeometryList */
182 /*******************/
185 * hkl_geometry_list_items:
186 * @self: the #HklGeometryList
188 * Return value: (element-type HklGeometryListItem) (transfer container): list of items,
189 * free the list with g_slist_free when done.
191 GSList* hkl_geometry_list_items(HklGeometryList *self)
193 GSList *list = NULL;
194 HklGeometryListItem *item;
196 list_for_each(&self->items, item, list)
197 list = g_slist_append(list, item);
199 return list;
202 /*************/
203 /* HklEngine */
204 /*************/
207 * hkl_engine_modes_names_get_binding:
208 * @self: the this ptr
209 * @length: (out caller-allocates): return the length of the returned array.
211 * Rename to: hkl_engine_modes_names_get
213 * Return value: (array length=length) (transfer none): All the modes supported by the #HklEngine
215 const char **hkl_engine_modes_names_get_binding(const HklEngine *self, size_t *length)
217 *length = darray_size(self->mode_names);
218 return &darray_item(self->mode_names, 0);
222 * hkl_engine_pseudo_axis_names_get_binding:
223 * @self: the this ptr
224 * @length: (out caller-allocates): return the length of the returned array.
226 * Rename to: hkl_engine_pseudo_axis_names_get
228 * Return value: (array length=length) (transfer none): All the pseudo_axes names of the #HklEngine
231 const char **hkl_engine_pseudo_axis_names_get_binding(HklEngine *self, size_t *length)
233 *length = darray_size(self->pseudo_axis_names);
234 return &darray_item(self->pseudo_axis_names, 0);
238 * hkl_engine_parameters_names_get_binding:
239 * @self: the this ptr
240 * @length: (out caller-allocates): return the length of the returned array.
242 * Rename to: hkl_engine_parameters_names_get
244 * Return value: (array length=length) (transfer none): All the parameters of #HklEngine.
246 const char **hkl_engine_parameters_names_get_binding(const HklEngine *self, size_t *length)
248 *length = darray_size(self->mode->parameters_names);
249 return &darray_item(self->mode->parameters_names, 0);
253 * hkl_engine_axis_names_get_binding:
254 * @self: the this ptr
255 * @mode: the #HklEngineAxesNamesGet
256 * @length: (out caller-allocates): return the length of the returned array.
258 * Rename to: hkl_engine_axis_names_get
260 * Return value: (array length=length) (transfer none): axes of the #HklEngine for the given mode.
262 const char **hkl_engine_axis_names_get_binding(const HklEngine *self,
263 HklEngineAxisNamesGet mode,
264 size_t *length)
266 const darray_string *axes = hkl_engine_axis_names_get(self, mode);
268 *length = darray_size(*axes);
270 return &darray_item(*axes, 0);
274 * hkl_engine_parameters_values_get_binding:
275 * @self: the this ptr
276 * @len: (out caller-allocates): the length of the returned array
277 * @unit_type: the unit type (default or user) of the returned value
279 * Rename to: hkl_engine_parameters_values_get
281 * Return value: (array length=len) (transfer container): list of parameters values,
282 * free the list with free when done.
284 double *hkl_engine_parameters_values_get_binding(const HklEngine *self, guint *len,
285 HklUnitEnum unit_type)
287 double *values;
288 uint i = 0;
289 HklParameter **parameter;
291 if(!self || !len || !self->mode || darray_size(self->mode->parameters) == 0)
292 return NULL;
294 *len = darray_size(self->mode->parameters);
295 values = malloc(*len * sizeof(*values));
297 darray_foreach(parameter, self->mode->parameters){
298 values[i++] = hkl_parameter_value_get(*parameter, unit_type);
301 return values;
305 * hkl_engine_pseudo_axis_values_get_binding:
306 * @self: the this ptr
307 * @len: (out caller-allocates): the length of the returned array
308 * @unit_type: the unit type (default or user) of the returned value
310 * Rename to: hkl_engine_pseudo_axis_values_get
312 * Return value: (array length=len) (transfer container): list of pseudo axes values,
313 * free the list with free when done.
315 double *hkl_engine_pseudo_axis_values_get_binding(const HklEngine *self, guint *len,
316 HklUnitEnum unit_type)
318 double *values;
319 uint i = 0;
320 HklParameter **axis;
322 if(!self || !len || darray_size(self->pseudo_axes) == 0)
323 return NULL;
325 *len = darray_size(self->pseudo_axes);
326 values = malloc(darray_size(self->pseudo_axes) * sizeof(*values));
328 darray_foreach(axis, self->pseudo_axes){
329 values[i++] = hkl_parameter_value_get(*axis, unit_type);
332 return values;
336 * hkl_engine_list_engines_get_as_gslist:
337 * @self: the this ptr
339 * Return value: (element-type HklEngine) (transfer container): list of engines,
340 * free the list with g_slist_free when done.
342 * Rename to: hkl_engine_list_engines_get
344 GSList* hkl_engine_list_engines_get_as_gslist(HklEngineList *self)
346 GSList *list = NULL;
347 HklEngine **engine;
349 darray_foreach(engine, *self){
350 list = g_slist_append(list, *engine);
353 return list;
356 /*************/
357 /* HklSample */
358 /*************/
361 * hkl_sample_reflections_get:
362 * @self: the this ptr
364 * Return value: (element-type HklSampleReflection) (transfer container): list of reflections,
365 * free the list with g_slist_free when done.
367 GSList *hkl_sample_reflections_get(const HklSample *self)
369 GSList *list = NULL;
370 HklSampleReflection *reflection;
372 list_for_each(&self->reflections, reflection, list){
373 list = g_slist_append(list, reflection);
376 return list;
380 * hkl_sample_add_reflection_binding:
381 * @self: the this ptr
382 * @geometry: the geometry of the HklSampleReflection
383 * @detector: the detector of the HklSampleReflection
384 * @h: the h coordinate
385 * @k: the k coordinate
386 * @l: the l coordinate
387 * @error: return location for a GError, or NULL
389 * Return value: (transfer none): the newly created HklSampleReflection
391 * Rename to: hkl_sample_add_reflection
393 HklSampleReflection *hkl_sample_add_reflection_binding(HklSample *self,
394 const HklGeometry *geometry,
395 const HklDetector *detector,
396 double h, double k, double l,
397 GError **error)
399 HklSampleReflection *reflection;
401 hkl_error (error == NULL || *error == NULL);
403 reflection = hkl_sample_reflection_new(geometry, detector,
404 h, k, l, error);
405 if(!reflection){
406 g_assert (error == NULL || *error != NULL);
407 return NULL;
409 g_assert (error == NULL || *error == NULL);
411 hkl_sample_add_reflection(self, reflection);
413 return reflection;