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-2015 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>
22 #include <stdio.h> // for fprintf, FILE
23 #include <stdlib.h> // for free, malloc, NULL
24 #include <string.h> // for strcmp
25 #include "hkl-interval-private.h" // for HklInterval
26 #include "hkl-macros-private.h" // for HKL_MALLOC
27 #include "hkl-parameter-private.h" // for _HklParameter, etc
28 #include "hkl-unit-private.h" // for hkl_unit_factor, HklUnit, etc
29 #include "hkl.h" // for HklParameter, etc
30 #include "hkl/ccan/darray/darray.h" // for darray_size, darray_item, etc
36 static int hkl_parameter_init(HklParameter
*self
, const char *name
,
37 const char *description
,
38 double min
, double value
, double max
,
40 const HklUnit
*unit
, const HklUnit
*punit
)
45 && strcmp(description
, "")
46 && hkl_unit_compatible(unit
, punit
)) {
48 self
->description
= description
;
49 self
->range
.min
= min
;
50 self
->range
.max
= max
;
55 self
->changed
= changed
;
56 self
->ops
= &hkl_parameter_operations_defaults
;
64 * hkl_parameter_new: (skip)
74 * create a new #HklParameter
78 HklParameter
*hkl_parameter_new(const char *name
, const char *description
,
79 double min
, double value
, double max
,
81 const HklUnit
*unit
, const HklUnit
*punit
)
85 self
= HKL_MALLOC(HklParameter
);
87 if (!hkl_parameter_init(self
,
100 * hkl_parameter_new_copy: (skip)
103 * copy an #HklParameter
107 HklParameter
*hkl_parameter_new_copy(const HklParameter
*self
)
109 return self
->ops
->copy(self
);
113 * hkl_parameter_free: (skip)
116 * delete an #HklParameter
118 void hkl_parameter_free(HklParameter
*self
)
120 self
->ops
->free(self
);
124 * hkl_parameter_init_copy: (skip)
125 * @self: the this ptr
126 * @src: the parameter to copy from
127 * @error: return location for a GError, or NULL
129 * Returns: TRUE on success, FALSE if an error occurred
131 int hkl_parameter_init_copy(HklParameter
*self
, const HklParameter
*src
,
134 return self
->ops
->init_copy(self
, src
, error
);
138 * hkl_parameter_name_get:
139 * @self: the this ptr
141 * Returns: the name of the #HklParameter
143 const char *hkl_parameter_name_get(const HklParameter
*self
)
149 * hkl_parameter_default_unit_get:
150 * @self: the this ptr
152 * Returns: the default unit of the #HklParameter
154 const char *hkl_parameter_default_unit_get(const HklParameter
*self
)
156 return self
->unit
->name
;
160 * hkl_parameter_user_unit_get:
161 * @self: the this ptr
163 * Returns: the user unit of the #HklParameter
165 const char *hkl_parameter_user_unit_get(const HklParameter
*self
)
167 return self
->punit
->name
;
171 * hkl_parameter_value_get:
172 * @self: the this ptr
173 * @unit_type: the unit type (default or user) of the returned value
175 * Returns: the value of the #HklParameter
177 inline double hkl_parameter_value_get(const HklParameter
*self
,
178 HklUnitEnum unit_type
)
181 case HKL_UNIT_DEFAULT
:
185 return self
->_value
* hkl_unit_factor(self
->unit
, self
->punit
);
193 * hkl_parameter_value_get_closest:
194 * @self: the this ptr
195 * @ref: the reference #HklParameter
199 * Returns: the closest value of the ref #HklParameter from the
200 * current self #HklParameter
202 inline double hkl_parameter_value_get_closest(const HklParameter
*self
,
203 const HklParameter
*ref
)
205 return self
->ops
->get_value_closest(self
, ref
);
209 * hkl_parameter_value_set:
211 * @value: the value to set
212 * @unit_type: the unit type (default or user) of the returned value
213 * @error: return location for a GError, or NULL
215 * set the value of an #HklParameter
217 * Returns: TRUE on success, FALSE if an error occurred
219 inline int hkl_parameter_value_set(HklParameter
*self
, double value
,
220 HklUnitEnum unit_type
, GError
**error
)
222 return self
->ops
->set_value(self
, value
, unit_type
, error
);
226 * hkl_parameter_value_set_smallest_in_range: (skip)
227 * @self: the this ptr
229 inline void hkl_parameter_value_set_smallest_in_range(HklParameter
*self
)
231 self
->ops
->set_value_smallest_in_range(self
);
235 * hkl_parameter_min_max_get:
236 * @self: the this ptr
237 * @min: (out caller-allocates): the returned minimum value
238 * @max: (out caller-allocates): the returned maximum value
239 * @unit_type: the unit type (default or user) of the returned values
241 * get the min and max value of the #HklParameter
244 void hkl_parameter_min_max_get(const HklParameter
*self
, double *min
, double *max
,
245 HklUnitEnum unit_type
)
250 case HKL_UNIT_DEFAULT
:
251 *min
= self
->range
.min
;
252 *max
= self
->range
.max
;
255 factor
= hkl_unit_factor(self
->unit
, self
->punit
);
256 *min
= factor
* self
->range
.min
;
257 *max
= factor
* self
->range
.max
;
263 * hkl_parameter_min_max_set:
264 * @self: the this ptr
265 * @min: the minimum value to set
266 * @max: the maximum value to set
267 * @unit_type: the unit type (default or user) of the min, max
268 * @error: return location for a GError, or NULL
270 * set the #HklParameter range.
271 * @todo test and set the GError
273 * Returns: TRUE on success, FALSE if an error occurred
275 int hkl_parameter_min_max_set(HklParameter
*self
, double min
, double max
,
276 HklUnitEnum unit_type
, GError
**error
)
280 hkl_error (error
== NULL
|| *error
== NULL
);
285 HKL_PARAMETER_ERROR_MIN_MAX_SET
,
286 "can not set this range min > max\n");
292 case HKL_UNIT_DEFAULT
:
293 self
->range
.min
= min
;
294 self
->range
.max
= max
;
297 factor
= hkl_unit_factor(self
->unit
, self
->punit
);
298 self
->range
.min
= min
/ factor
;
299 self
->range
.max
= max
/ factor
;
307 * hkl_parameter_fit_get:
308 * @self: the this ptr
310 * Retuen value: the #HklParameter fit value, True is the parameter can be fitted, not otherwise
313 int hkl_parameter_fit_get(const HklParameter
*self
)
319 * hkl_parameter_fit_set:
320 * @self: the this ptr
321 * @fit: the fit value to set
323 * set the #HklParameter fit value, True is the parameter can be fitted, not otherwise
326 void hkl_parameter_fit_set(HklParameter
*self
, int fit
)
332 * hkl_parameter_randomize: (skip)
335 * randomize the #HklParameter value into the min,max range
337 void hkl_parameter_randomize(HklParameter
*self
)
339 self
->ops
->randomize(self
);
343 * hkl_parameter_is_valid: (skip)
346 * check if the value of the #HklParameter is in the min,max range
350 int hkl_parameter_is_valid(const HklParameter
*self
)
352 return self
->ops
->is_valid(self
);
356 * hkl_parameter_fprintf: (skip)
360 * print into the #FILE f an #HklParameter
362 void hkl_parameter_fprintf(FILE *f
, HklParameter
*self
)
364 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
366 fprintf(f
, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
368 self
->_value
* factor
,
370 self
->range
.min
* factor
,
371 self
->range
.max
* factor
,
374 fprintf(f
, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
376 self
->_value
* factor
,
377 self
->range
.min
* factor
,
378 self
->range
.max
* factor
,
383 * hkl_parameter_axis_v_get:
384 * @self: the this ptr
386 * Returns: (allow-none):
388 const HklVector
*hkl_parameter_axis_v_get(const HklParameter
*self
)
390 return self
->ops
->axis_v_get(self
);
394 * hkl_parameter_quaternion_get:
395 * @self: the this ptr
397 * Returns: (allow-none):
399 const HklQuaternion
*hkl_parameter_quaternion_get(const HklParameter
*self
)
401 return self
->ops
->quaternion_get(self
);
405 * hkl_parameter_description_get:
406 * @self: the this ptr
408 * Returns: the #HklParameter description
410 const char *hkl_parameter_description_get(const HklParameter
*self
)
412 return self
->description
;