2 /* This file is part of the hkl library.
4 * The hkl library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * The hkl library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2003-2015 Synchrotron SOLEIL
18 * L'Orme des Merisiers Saint-Aubin
19 * BP 48 91192 GIF-sur-YVETTE CEDEX
21 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #ifndef __HKL_PARAMETER_PRIVATE_H__
24 #define __HKL_PARAMETER_PRIVATE_H__
26 #include <math.h> // for M_PI
27 #include <stdio.h> // for FILE, fprintf, NULL
28 #include <stdlib.h> // for free, rand, RAND_MAX
29 #include "hkl-interval-private.h" // for HklInterval
30 #include "hkl-macros-private.h" // for HKL_MALLOC
31 #include "hkl-unit-private.h" // for HklUnit, hkl_unit_factor
32 #include "hkl.h" // for HklParameter, TRUE, etc
36 typedef struct _HklParameterOperations HklParameterOperations
;
38 struct _HklParameter
{
40 const char *description
;
47 const HklParameterOperations
*ops
;
50 #define HKL_PARAMETER_DEFAULTS .name="dummy", .description="no description", .range={.min=0, .max=0}, ._value=0, .unit=NULL, .punit=NULL, .fit=TRUE, .changed=TRUE, .ops = &hkl_parameter_operations_defaults
52 #define HKL_PARAMETER_DEFAULTS_ANGLE HKL_PARAMETER_DEFAULTS, .range={.min=-M_PI, .max=M_PI}, .unit = &hkl_unit_angle_rad, .punit = &hkl_unit_angle_deg
54 #define HKL_PARAMETER_ERROR hkl_parameter_error_quark ()
56 static GQuark
hkl_parameter_error_quark (void)
58 return g_quark_from_static_string ("hkl-parameter-error-quark");
62 HKL_PARAMETER_ERROR_MIN_MAX_SET
, /* can not set the min max */
69 struct _HklParameterOperations
{
70 HklParameter
* (*copy
)(const HklParameter
*self
);
71 void (*free
)(HklParameter
*self
);
72 int (*init_copy
)(HklParameter
*self
, const HklParameter
*src
,
74 double (*get_value_closest
)(const HklParameter
*self
,
75 const HklParameter
*other
);
76 int (*set_value
)(HklParameter
*self
, double value
,
77 HklUnitEnum unit_type
, GError
**error
);
78 void (*set_value_smallest_in_range
)(HklParameter
*self
);
79 void (*randomize
)(HklParameter
*self
);
80 int (*is_valid
)(const HklParameter
*self
);
81 void (*fprintf
)(FILE *f
, const HklParameter
*self
);
82 const HklVector
* (*axis_v_get
)(const HklParameter
*self
);
83 const HklQuaternion
* (*quaternion_get
)(const HklParameter
*self
);
86 #define HKL_PARAMETER_OPERATIONS_DEFAULTS \
87 .copy = hkl_parameter_copy_real, \
88 .free = hkl_parameter_free_real, \
89 .init_copy = hkl_parameter_init_copy_real, \
90 .get_value_closest = hkl_parameter_value_get_closest_real, \
91 .set_value = hkl_parameter_value_set_real, \
92 .set_value_smallest_in_range = hkl_parameter_value_set_smallest_in_range_real, \
93 .randomize = hkl_parameter_randomize_real, \
94 .is_valid = hkl_parameter_is_valid_real, \
95 .fprintf = hkl_parameter_fprintf_real, \
96 .axis_v_get = hkl_parameter_axis_v_get_real, \
97 .quaternion_get = hkl_parameter_quaternion_get_real
99 static inline HklParameter
*hkl_parameter_copy_real(const HklParameter
*self
)
101 HklParameter
*dup
= HKL_MALLOC(HklParameter
);
108 static inline void hkl_parameter_free_real(HklParameter
*self
)
113 static inline int hkl_parameter_init_copy_real(HklParameter
*self
, const HklParameter
*src
,
116 hkl_error (error
== NULL
|| *error
== NULL
);
117 hkl_error (self
->name
== src
->name
|| strcmp(self
->name
, src
->name
) == 0);
120 self
->changed
= TRUE
;
125 static inline double hkl_parameter_value_get_closest_real(const HklParameter
*self
,
126 const HklParameter
*ref
)
131 static inline int hkl_parameter_value_set_real(HklParameter
*self
, double value
,
132 HklUnitEnum unit_type
, GError
**error
)
134 hkl_error (error
== NULL
|| *error
== NULL
);
137 case HKL_UNIT_DEFAULT
:
138 self
->_value
= value
;
141 self
->_value
= value
/ hkl_unit_factor(self
->unit
, self
->punit
);
144 self
->changed
= TRUE
;
149 static inline void hkl_parameter_value_set_smallest_in_range_real(HklParameter
*self
)
151 /* DOES NOTHING for a standard parameter */
154 static inline void hkl_parameter_randomize_real(HklParameter
*self
)
157 double alea
= (double)rand() / (RAND_MAX
+ 1.);
158 self
->_value
= self
->range
.min
159 + (self
->range
.max
- self
->range
.min
) * alea
;
160 self
->changed
= TRUE
;
164 static inline int hkl_parameter_is_valid_real(const HklParameter
*self
)
166 if(self
->_value
< (self
->range
.min
- HKL_EPSILON
)
167 || self
->_value
> (self
->range
.max
+ HKL_EPSILON
))
173 static inline void hkl_parameter_fprintf_real(FILE *f
, const HklParameter
*self
)
175 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
177 fprintf(f
, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
179 self
->_value
* factor
,
181 self
->range
.min
* factor
,
182 self
->range
.max
* factor
,
185 fprintf(f
, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
187 self
->_value
* factor
,
188 self
->range
.min
* factor
,
189 self
->range
.max
* factor
,
193 static inline const HklVector
*hkl_parameter_axis_v_get_real(const HklParameter
*self
)
198 static inline const HklQuaternion
*hkl_parameter_quaternion_get_real(const HklParameter
*self
)
203 static HklParameterOperations hkl_parameter_operations_defaults
= {
204 HKL_PARAMETER_OPERATIONS_DEFAULTS
,
208 extern HklParameter
*hkl_parameter_new(const char *name
, const char *description
,
209 double min
, double value
, double max
,
210 int fit
, int changed
,
212 const HklUnit
*punit
);
214 extern int hkl_parameter_init_copy(HklParameter
*self
, const HklParameter
*src
,
217 extern double hkl_parameter_value_get_closest(const HklParameter
*self
,
218 const HklParameter
*ref
);
220 extern void hkl_parameter_value_set_smallest_in_range(HklParameter
*self
);
222 extern int hkl_parameter_is_valid(const HklParameter
*self
);
224 extern void hkl_parameter_fprintf(FILE *f
, HklParameter
*self
);
226 /********************/
227 /* HklParameterList */
228 /********************/
230 typedef darray(HklParameter
*) darray_parameter
;
234 #endif /* __HKL_PARAMETER_PRIVATE_H__ */