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) 2003-2013 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>
25 #include <math.h> // for M_PI
26 #include <stddef.h> // for size_t
27 #include <stdio.h> // for FILE
28 #include <hkl/ccan/darray/darray.h> // for darray
30 /* Guard C code in headers, while including them from C++ */
32 # define HKL_BEGIN_DECLS extern "C" {
33 # define HKL_END_DECLS }
35 # define HKL_BEGIN_DECLS
36 # define HKL_END_DECLS
40 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
41 # define HKL_DEPRECATED __attribute__((__deprecated__))
42 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
43 # define HKL_DEPRECATED __declspec(deprecated)
45 # define HKL_DEPRECATED
48 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
49 # define HKL_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
50 #elif defined(_MSC_FULL_VER) && (_MSC_FULL_VER > 140050320)
51 # define HKL_DEPRECATED_FOR(f) __declspec(deprecated("is deprecated. Use '" #f "' instead"))
53 # define HKL_DEPRECATED_FOR(f) HKL_DEPRECATED
60 #define HKL_EPSILON 1e-6
61 #define HKL_DEGTORAD (M_PI/180.)
62 #define HKL_RADTODEG (180./M_PI)
65 #define HKL_TAU (2. * M_PI)
66 /* #define HKL_TAU 1 */
74 # define HKLAPI __attribute__ ((visibility("default")))
81 * @brief Used to export functions(by changing visibility).
86 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
87 # define HKL_ARG_NONNULL(...) __attribute__ ((__nonnull__(__VA_ARGS__)))
89 # define HKL_ARG_NONNULL(...)
92 #define _darray(type) type *item; size_t size; size_t alloc
100 typedef struct _HklMatrix HklMatrix
;
102 HKLAPI HklMatrix
*hkl_matrix_new(void);
104 HKLAPI HklMatrix
*hkl_matrix_new_full(double m11
, double m12
, double m13
,
105 double m21
, double m22
, double m23
,
106 double m31
, double m32
, double m33
);
108 HKLAPI HklMatrix
*hkl_matrix_new_euler(double euler_x
, double euler_y
, double euler_z
);
110 HKLAPI
double hkl_matrix_get(const HklMatrix
*self
, unsigned int i
, unsigned int j
) HKL_ARG_NONNULL(1);
112 HKLAPI
void hkl_matrix_free(HklMatrix
*self
) HKL_ARG_NONNULL(1);
114 HKLAPI
void hkl_matrix_init(HklMatrix
*self
,
115 double m11
, double m12
, double m13
,
116 double m21
, double m22
, double m23
,
117 double m31
, double m32
, double m33
) HKL_ARG_NONNULL(1);
119 HKLAPI
int hkl_matrix_cmp(const HklMatrix
*self
, const HklMatrix
*m
) HKL_ARG_NONNULL(1, 2);
121 HKLAPI
void hkl_matrix_times_matrix(HklMatrix
*self
, const HklMatrix
*m
) HKL_ARG_NONNULL(1, 2);
127 typedef struct _HklError HklError
;
129 HKLAPI
const char *hkl_error_message_get(const HklError
*self
) HKL_ARG_NONNULL(1);
130 HKLAPI
void hkl_error_clear (HklError
**err
) HKL_ARG_NONNULL(1);
136 typedef struct _HklParameter HklParameter
;
137 typedef struct _HklParameterList HklParameterList
;
138 typedef HklParameterList darray_parameter
;
142 HKLAPI HklParameter
*hkl_parameter_new_copy(const HklParameter
*self
) HKL_ARG_NONNULL(1);
144 HKLAPI
void hkl_parameter_free(HklParameter
*self
) HKL_ARG_NONNULL(1);
146 HKLAPI
const char *hkl_parameter_name_get(const HklParameter
*self
) HKL_ARG_NONNULL(1);
148 HKLAPI
double hkl_parameter_value_get(const HklParameter
*self
) HKL_ARG_NONNULL(1);
150 HKLAPI
int hkl_parameter_value_set(HklParameter
*self
, double value
,
151 HklError
**error
) HKL_ARG_NONNULL(1);
153 HKLAPI
double hkl_parameter_value_unit_get(const HklParameter
*self
) HKL_ARG_NONNULL(1);
155 HKLAPI
int hkl_parameter_value_unit_set(HklParameter
*self
, double value
,
156 HklError
**error
) HKL_ARG_NONNULL(1);
158 HKLAPI
void hkl_parameter_min_max_get(const HklParameter
*self
, double *min
, double *max
) HKL_ARG_NONNULL(1, 2, 3);
160 HKLAPI
void hkl_parameter_min_max_set(HklParameter
*self
, double min
, double max
) HKL_ARG_NONNULL(1);
162 HKLAPI
void hkl_parameter_min_max_unit_get(const HklParameter
*self
, double *min
, double *max
) HKL_ARG_NONNULL(1, 2, 3);
164 HKLAPI
void hkl_parameter_min_max_unit_set(HklParameter
*self
, double min
, double max
) HKL_ARG_NONNULL(1);
166 HKLAPI
int hkl_parameter_fit_get(const HklParameter
*self
) HKL_ARG_NONNULL(1);
168 HKLAPI
void hkl_parameter_fit_set(HklParameter
*self
, int fit
) HKL_ARG_NONNULL(1);
170 HKLAPI
void hkl_parameter_randomize(HklParameter
*self
) HKL_ARG_NONNULL(1);
172 /* HklParameterList */
174 struct _HklParameterList
{
175 _darray(HklParameter
*);
178 HKLAPI
unsigned int hkl_parameter_list_values_set(HklParameterList
*self
,
179 double values
[], unsigned int len
,
180 HklError
**error
) HKL_ARG_NONNULL(1);
182 HKLAPI
double *hkl_parameter_list_values_unit_get(const HklParameterList
*self
,
183 unsigned int *len
) HKL_ARG_NONNULL(1, 2);
185 /* only use in the test method for now */
186 HKLAPI
void hkl_parameter_list_randomize(HklParameterList
*self
) HKL_ARG_NONNULL(1);
192 typedef struct _HklDetector HklDetector
;
193 typedef enum _HklDetectorType
198 HKLAPI
void hkl_detector_free(HklDetector
*self
) HKL_ARG_NONNULL(1);
200 HKLAPI
void hkl_detector_idx_set(HklDetector
*self
, int idx
) HKL_ARG_NONNULL(1);
202 HKLAPI
void hkl_detector_fprintf(FILE *f
, const HklDetector
*self
) HKL_ARG_NONNULL(1, 2);
204 HKLAPI HklDetector
*hkl_detector_factory_new(HklDetectorType type
);
210 typedef struct _HklGeometry HklGeometry
;
211 typedef struct _HklGeometryList HklGeometryList
;
212 typedef struct _HklGeometryListItem HklGeometryListItem
;
214 typedef darray(HklGeometryListItem
*) darray_item
;
218 HKLAPI
void hkl_geometry_free(HklGeometry
*self
) HKL_ARG_NONNULL(1);
220 HKLAPI
void hkl_geometry_set(HklGeometry
*self
, const HklGeometry
*src
) HKL_ARG_NONNULL(1, 2);
222 HKLAPI
const darray_parameter
*hkl_geometry_axes_get(const HklGeometry
*self
) HKL_ARG_NONNULL(1);
224 HKLAPI
const HklParameter
*hkl_geometry_axis_get(const HklGeometry
*self
, const char *name
) HKL_ARG_NONNULL(1, 2);
226 HKLAPI
void hkl_geometry_axis_set(HklGeometry
*self
, const HklParameter
*axis
) HKL_ARG_NONNULL(1, 2);
228 HKLAPI
const char *hkl_geometry_name_get(const HklGeometry
*self
) HKL_ARG_NONNULL(1);
230 HKLAPI
double hkl_geometry_wavelength_get(const HklGeometry
*self
) HKL_ARG_NONNULL(1);
232 HKLAPI
void hkl_geometry_wavelength_set(HklGeometry
*self
, double wavelength
) HKL_ARG_NONNULL(1);
234 HKLAPI
void hkl_geometry_randomize(HklGeometry
*self
) HKL_ARG_NONNULL(1);
236 HKLAPI
int hkl_geometry_set_values_unit_v(HklGeometry
*self
, ...) HKL_ARG_NONNULL(1);
238 HKLAPI
void hkl_geometry_fprintf(FILE *file
, const HklGeometry
*self
) HKL_ARG_NONNULL(1, 2);
240 /* HklGeometryList */
242 HKLAPI
const darray_item
*hkl_geometry_list_items_get(const HklGeometryList
*self
) HKL_ARG_NONNULL(1);
244 /* HklGeometryListItem */
246 HKLAPI
const HklGeometry
*hkl_geometry_list_item_geometry_get(const HklGeometryListItem
*self
) HKL_ARG_NONNULL(1);
252 typedef struct _HklLattice HklLattice
;
253 typedef struct _HklSample HklSample
;
254 typedef struct _HklSampleReflection HklSampleReflection
;
258 HKLAPI HklLattice
*hkl_lattice_new(double a
, double b
, double c
,
259 double alpha
, double beta
, double gamma
);
261 HKLAPI HklLattice
*hkl_lattice_new_copy(const HklLattice
*self
) HKL_ARG_NONNULL(1);
263 HKLAPI HklLattice
*hkl_lattice_new_default(void);
265 HKLAPI
void hkl_lattice_free(HklLattice
*self
) HKL_ARG_NONNULL(1);
267 HKLAPI
const HklParameter
*hkl_lattice_a_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
269 HKLAPI
void hkl_lattice_a_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
271 HKLAPI
const HklParameter
*hkl_lattice_b_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
273 HKLAPI
void hkl_lattice_b_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
275 HKLAPI
const HklParameter
*hkl_lattice_c_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
277 HKLAPI
void hkl_lattice_c_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
279 HKLAPI
const HklParameter
*hkl_lattice_alpha_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
281 HKLAPI
void hkl_lattice_alpha_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
283 HKLAPI
const HklParameter
*hkl_lattice_beta_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
285 HKLAPI
void hkl_lattice_beta_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
287 HKLAPI
const HklParameter
*hkl_lattice_gamma_get(const HklLattice
*self
) HKL_ARG_NONNULL(1);
289 HKLAPI
void hkl_lattice_gamma_set(HklLattice
*self
, const HklParameter
*parameter
) HKL_ARG_NONNULL(1, 2);
291 HKLAPI
int hkl_lattice_set(HklLattice
*self
,
292 double a
, double b
, double c
,
293 double alpha
, double beta
, double gamma
) HKL_ARG_NONNULL(1);
295 HKLAPI
void hkl_lattice_get(const HklLattice
*self
,
296 double *a
, double *b
, double *c
,
297 double *alpha
, double *beta
, double *gamma
) HKL_ARG_NONNULL(1, 2, 3, 4, 5, 6, 7);
299 HKLAPI
int hkl_lattice_get_B(const HklLattice
*self
, HklMatrix
*B
) HKL_ARG_NONNULL(1, 2);
301 HKLAPI
int hkl_lattice_get_1_B(const HklLattice
*self
, HklMatrix
*B
) HKL_ARG_NONNULL(1, 2);
303 HKLAPI
int hkl_lattice_reciprocal(const HklLattice
*self
, HklLattice
*reciprocal
) HKL_ARG_NONNULL(1, 2);
307 HKLAPI HklSample
*hkl_sample_new(const char *name
) HKL_ARG_NONNULL(1);
309 HKLAPI HklSample
*hkl_sample_new_copy(const HklSample
*self
) HKL_ARG_NONNULL(1);
311 HKLAPI
void hkl_sample_free(HklSample
*self
) HKL_ARG_NONNULL(1);
313 HKLAPI
const char *hkl_sample_name_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
315 HKLAPI
void hkl_sample_name_set(HklSample
*self
, const char *name
) HKL_ARG_NONNULL(1, 2);
317 HKLAPI
const HklLattice
*hkl_sample_lattice_get(HklSample
*self
) HKL_ARG_NONNULL(1);
319 HKLAPI
void hkl_sample_lattice_set(HklSample
*self
, const HklLattice
*lattice
) HKL_ARG_NONNULL(1, 2);
321 HKLAPI
const HklParameter
*hkl_sample_ux_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
323 HKLAPI
void hkl_sample_ux_set(HklSample
*self
, const HklParameter
*ux
) HKL_ARG_NONNULL(1, 2);
325 HKLAPI
const HklParameter
*hkl_sample_uy_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
327 HKLAPI
void hkl_sample_uy_set(HklSample
*self
, const HklParameter
*uy
) HKL_ARG_NONNULL(1, 2);
329 HKLAPI
const HklParameter
*hkl_sample_uz_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
331 HKLAPI
void hkl_sample_uz_set(HklSample
*self
, const HklParameter
*uz
) HKL_ARG_NONNULL(1, 2);
333 HKLAPI
const HklMatrix
*hkl_sample_U_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
335 HKLAPI
void hkl_sample_U_set(HklSample
*self
, const HklMatrix
*U
) HKL_ARG_NONNULL(1);
337 HKLAPI
const HklMatrix
*hkl_sample_UB_get(const HklSample
*self
) HKL_ARG_NONNULL(1);
339 HKLAPI
double hkl_sample_UB_set(HklSample
*self
, const HklMatrix
*UB
) HKL_ARG_NONNULL(1, 2);
341 HKLAPI HklSampleReflection
*hkl_sample_first_reflection_get(HklSample
*self
) HKL_ARG_NONNULL(1);
343 HKLAPI HklSampleReflection
*hkl_sample_next_reflection_get(HklSample
*self
,
344 HklSampleReflection
*reflection
) HKL_ARG_NONNULL(1, 2);
346 HKLAPI
void hkl_sample_del_reflection(HklSample
*self
,
347 HklSampleReflection
*reflection
) HKL_ARG_NONNULL(1, 2);
349 HKLAPI
void hkl_sample_add_reflection(HklSample
*self
,
350 HklSampleReflection
*reflection
) HKL_ARG_NONNULL(1, 2);
352 HKLAPI
int hkl_sample_compute_UB_busing_levy(HklSample
*self
,
353 const HklSampleReflection
*r1
,
354 const HklSampleReflection
*r2
) HKL_ARG_NONNULL(1, 2, 3);
356 HKLAPI
double hkl_sample_get_reflection_mesured_angle(const HklSample
*self
,
357 const HklSampleReflection
*r1
,
358 const HklSampleReflection
*r2
) HKL_ARG_NONNULL(1, 2, 3);
360 HKLAPI
double hkl_sample_get_reflection_theoretical_angle(const HklSample
*self
,
361 const HklSampleReflection
*r1
,
362 const HklSampleReflection
*r2
) HKL_ARG_NONNULL(1, 2, 3);
364 HKLAPI
double hkl_sample_affine(HklSample
*self
) HKL_ARG_NONNULL(1);
366 /* hklSampleReflection */
368 HKLAPI HklSampleReflection
*hkl_sample_reflection_new(const HklGeometry
*geometry
,
369 const HklDetector
*detector
,
370 double h
, double k
, double l
) HKL_ARG_NONNULL(1, 2);
372 HKLAPI
void hkl_sample_reflection_hkl_get(const HklSampleReflection
*self
,
373 double *h
, double *k
, double *l
) HKL_ARG_NONNULL(1, 2, 3, 4);
375 HKLAPI
void hkl_sample_reflection_hkl_set(HklSampleReflection
*self
,
376 double h
, double k
, double l
) HKL_ARG_NONNULL(1);
378 HKLAPI
int hkl_sample_reflection_flag_get(const HklSampleReflection
*self
) HKL_ARG_NONNULL(1);
380 HKLAPI
void hkl_sample_reflection_flag_set(HklSampleReflection
*self
, int flag
) HKL_ARG_NONNULL(1);
382 HKLAPI
const HklGeometry
*hkl_sample_reflection_geometry_get(HklSampleReflection
*self
) HKL_ARG_NONNULL(1);
384 HKLAPI
void hkl_sample_reflection_geometry_set(HklSampleReflection
*self
,
385 const HklGeometry
*geometry
) HKL_ARG_NONNULL(1, 2);
391 typedef struct _HklPseudoAxis HklPseudoAxis
;
392 typedef struct _HklMode HklMode
;
393 typedef struct _HklEngine HklEngine
;
394 typedef struct _HklEngineList HklEngineList
;
396 typedef darray(HklMode
*) darray_mode
;
397 typedef darray(HklEngine
*) darray_engine
;
401 HKLAPI
const char * hkl_mode_name(const HklMode
*self
) HKL_ARG_NONNULL(1);
403 HKLAPI HklParameterList
*hkl_mode_parameters(HklMode
*self
) HKL_ARG_NONNULL(1);
405 HKLAPI
void hkl_mode_fprintf(FILE *f
, const HklMode
*self
) HKL_ARG_NONNULL(1, 2);
409 HKLAPI
const char *hkl_engine_name(const HklEngine
*self
) HKL_ARG_NONNULL(1);
411 HKLAPI
unsigned int hkl_engine_len(const HklEngine
*self
) HKL_ARG_NONNULL(1);
413 HKLAPI HklParameterList
*hkl_engine_pseudo_axes(HklEngine
*self
) HKL_ARG_NONNULL(1);
415 HKLAPI HklMode
*hkl_engine_mode(HklEngine
*self
) HKL_ARG_NONNULL(1);
417 HKLAPI darray_mode
*hkl_engine_modes(HklEngine
*self
) HKL_ARG_NONNULL(1);
419 HKLAPI HklEngineList
*hkl_engine_engines(HklEngine
*self
) HKL_ARG_NONNULL(1);
421 HKLAPI
void hkl_engine_select_mode(HklEngine
*self
,
422 HklMode
*mode
) HKL_ARG_NONNULL(1, 2);
424 HKLAPI
void hkl_engine_select_mode_by_name(HklEngine
*self
,
425 const char *name
) HKL_ARG_NONNULL(1, 2);
427 HKLAPI
int hkl_engine_initialize(HklEngine
*self
, HklError
**error
) HKL_ARG_NONNULL(1);
429 HKLAPI
int hkl_engine_set(HklEngine
*self
, HklError
**error
) HKL_ARG_NONNULL(1);
431 HKLAPI
int hkl_engine_get(HklEngine
*self
, HklError
**error
) HKL_ARG_NONNULL(1);
433 HKLAPI
void hkl_engine_fprintf(FILE *f
, const HklEngine
*self
) HKL_ARG_NONNULL(1, 2);
437 HKLAPI
void hkl_engine_list_free(HklEngineList
*self
) HKL_ARG_NONNULL(1);
439 HKLAPI darray_engine
*hkl_engine_list_engines(HklEngineList
*self
) HKL_ARG_NONNULL(1);
441 HKLAPI
void hkl_engine_list_geometry_set(HklEngineList
*self
, const HklGeometry
*geometry
) HKL_ARG_NONNULL(1, 2);
443 HKLAPI
const HklGeometryList
*hkl_engine_list_geometries(const HklEngineList
*self
) HKL_ARG_NONNULL(1);
445 HKLAPI HklGeometry
*hkl_engine_list_get_geometry(HklEngineList
*self
) HKL_ARG_NONNULL(1);
447 HKLAPI
void hkl_engine_list_select_solution(HklEngineList
*self
, unsigned int idx
) HKL_ARG_NONNULL(1);
449 HKLAPI HklEngine
*hkl_engine_list_get_by_name(HklEngineList
*self
,
450 const char *name
) HKL_ARG_NONNULL(1, 2);
452 HKLAPI HklParameter
*hkl_engine_list_get_pseudo_axis_by_name(const HklEngineList
*self
,
453 const char *name
) HKL_ARG_NONNULL(1, 2);
455 HKLAPI
void hkl_engine_list_init(HklEngineList
*self
,
456 HklGeometry
*geometry
,
457 HklDetector
*detector
,
458 HklSample
*sample
) HKL_ARG_NONNULL(1, 2, 3, 4);
460 HKLAPI
int hkl_engine_list_get(HklEngineList
*self
) HKL_ARG_NONNULL(1);
462 HKLAPI
void hkl_engine_list_fprintf(FILE *f
,
463 const HklEngineList
*self
) HKL_ARG_NONNULL(1, 2);
469 typedef struct _HklFactory HklFactory
;
471 HKLAPI HklFactory
**hkl_factory_get_all(unsigned int *n
) HKL_ARG_NONNULL(1);
473 HKLAPI HklFactory
*hkl_factory_get_by_name(const char *name
) HKL_ARG_NONNULL(1);
475 HKLAPI
const char *hkl_factory_name(const HklFactory
*self
) HKL_ARG_NONNULL(1);
477 HKLAPI HklGeometry
*hkl_factory_create_new_geometry(const HklFactory
*self
) HKL_ARG_NONNULL(1);
479 HKLAPI HklEngineList
*hkl_factory_create_new_engine_list(const HklFactory
*self
) HKL_ARG_NONNULL(1);