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-2014 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
);
191 * hkl_parameter_value_get_closest:
192 * @self: the this ptr
193 * @ref: the reference #HklParameter
197 * Returns: the closest value of the ref #HklParameter from the
198 * current self #HklParameter
200 inline double hkl_parameter_value_get_closest(const HklParameter
*self
,
201 const HklParameter
*ref
)
203 return self
->ops
->get_value_closest(self
, ref
);
207 * hkl_parameter_value_set:
209 * @value: the value to set
210 * @unit_type: the unit type (default or user) of the returned value
211 * @error: return location for a GError, or NULL
213 * set the value of an #HklParameter
215 * Returns: TRUE on success, FALSE if an error occurred
217 inline int hkl_parameter_value_set(HklParameter
*self
, double value
,
218 HklUnitEnum unit_type
, GError
**error
)
220 return self
->ops
->set_value(self
, value
, unit_type
, error
);
224 * hkl_parameter_value_set_smallest_in_range: (skip)
225 * @self: the this ptr
227 inline void hkl_parameter_value_set_smallest_in_range(HklParameter
*self
)
229 self
->ops
->set_value_smallest_in_range(self
);
233 * hkl_parameter_min_max_get:
234 * @self: the this ptr
235 * @min: (out caller-allocates): the returned minimum value
236 * @max: (out caller-allocates): the returned maximum value
237 * @unit_type: the unit type (default or user) of the returned values
239 * get the min and max value of the #HklParameter
242 void hkl_parameter_min_max_get(const HklParameter
*self
, double *min
, double *max
,
243 HklUnitEnum unit_type
)
248 case HKL_UNIT_DEFAULT
:
249 *min
= self
->range
.min
;
250 *max
= self
->range
.max
;
253 factor
= hkl_unit_factor(self
->unit
, self
->punit
);
254 *min
= factor
* self
->range
.min
;
255 *max
= factor
* self
->range
.max
;
261 * hkl_parameter_min_max_set:
262 * @self: the this ptr
263 * @min: the minimum value to set
264 * @max: the maximum value to set
265 * @unit_type: the unit type (default or user) of the min, max
266 * @error: return location for a GError, or NULL
268 * set the #HklParameter range.
269 * @todo test and set the GError
271 * Returns: TRUE on success, FALSE if an error occurred
273 int hkl_parameter_min_max_set(HklParameter
*self
, double min
, double max
,
274 HklUnitEnum unit_type
, GError
**error
)
278 hkl_error (error
== NULL
|| *error
== NULL
);
283 HKL_PARAMETER_ERROR_MIN_MAX_SET
,
284 "can not set this range min > max\n");
290 case HKL_UNIT_DEFAULT
:
291 self
->range
.min
= min
;
292 self
->range
.max
= max
;
295 factor
= hkl_unit_factor(self
->unit
, self
->punit
);
296 self
->range
.min
= min
/ factor
;
297 self
->range
.max
= max
/ factor
;
305 * hkl_parameter_fit_get:
306 * @self: the this ptr
308 * Retuen value: the #HklParameter fit value, True is the parameter can be fitted, not otherwise
311 int hkl_parameter_fit_get(const HklParameter
*self
)
317 * hkl_parameter_fit_set:
318 * @self: the this ptr
319 * @fit: the fit value to set
321 * set the #HklParameter fit value, True is the parameter can be fitted, not otherwise
324 void hkl_parameter_fit_set(HklParameter
*self
, int fit
)
330 * hkl_parameter_randomize: (skip)
333 * randomize the #HklParameter value into the min,max range
335 void hkl_parameter_randomize(HklParameter
*self
)
337 self
->ops
->randomize(self
);
341 * hkl_parameter_is_valid: (skip)
344 * check if the value of the #HklParameter is in the min,max range
348 int hkl_parameter_is_valid(const HklParameter
*self
)
350 return self
->ops
->is_valid(self
);
354 * hkl_parameter_fprintf: (skip)
358 * print into the #FILE f an #HklParameter
360 void hkl_parameter_fprintf(FILE *f
, HklParameter
*self
)
362 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
364 fprintf(f
, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
366 self
->_value
* factor
,
368 self
->range
.min
* factor
,
369 self
->range
.max
* factor
,
372 fprintf(f
, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
374 self
->_value
* factor
,
375 self
->range
.min
* factor
,
376 self
->range
.max
* factor
,
381 * hkl_parameter_axis_v_get:
382 * @self: the this ptr
384 * Returns: (allow-none):
386 const HklVector
*hkl_parameter_axis_v_get(const HklParameter
*self
)
388 return self
->ops
->axis_v_get(self
);
392 * hkl_parameter_quaternion_get:
393 * @self: the this ptr
395 * Returns: (allow-none):
397 const HklQuaternion
*hkl_parameter_quaternion_get(const HklParameter
*self
)
399 return self
->ops
->quaternion_get(self
);
403 * hkl_parameter_description_get:
404 * @self: the this ptr
406 * Returns: the #HklParameter description
408 const char *hkl_parameter_description_get(const HklParameter
*self
)
410 return self
->description
;