[contrib] use (*~~) and (/~~) instead of from/toAngles
[hkl.git] / hkl / hkl-parameter.c
blob94cd2aca6aeb27b9c9dd6af9d1967f14de037d80
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
32 /****************/
33 /* HklParameter */
34 /****************/
36 static int hkl_parameter_init(HklParameter *self, const char *name,
37 const char *description,
38 double min, double value, double max,
39 int fit, int changed,
40 const HklUnit *unit, const HklUnit *punit)
42 if (min <= value
43 && value <= max
44 && strcmp(name, "")
45 && strcmp(description, "")
46 && hkl_unit_compatible(unit, punit)) {
47 self->name = name;
48 self->description = description;
49 self->range.min = min;
50 self->range.max = max;
51 self->_value = value;
52 self->unit = unit;
53 self->punit = punit;
54 self->fit = fit;
55 self->changed = changed;
56 self->ops = &hkl_parameter_operations_defaults;
57 } else
58 return FALSE;
60 return TRUE;
63 /**
64 * hkl_parameter_new: (skip)
65 * @name:
66 * @min:
67 * @value:
68 * @max:
69 * @fit:
70 * @changed:
71 * @unit:
72 * @punit:
74 * create a new #HklParameter
76 * Returns:
77 **/
78 HklParameter *hkl_parameter_new(const char *name, const char *description,
79 double min, double value, double max,
80 int fit, int changed,
81 const HklUnit *unit, const HklUnit *punit)
83 HklParameter *self;
85 self = HKL_MALLOC(HklParameter);
87 if (!hkl_parameter_init(self,
88 name, description,
89 min, value, max,
90 fit, changed,
91 unit, punit)) {
92 free(self);
93 self = NULL;
96 return self;
99 /**
100 * hkl_parameter_new_copy: (skip)
101 * @self:
103 * copy an #HklParameter
105 * Returns:
107 HklParameter *hkl_parameter_new_copy(const HklParameter *self)
109 return self->ops->copy(self);
113 * hkl_parameter_free: (skip)
114 * @self:
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,
132 GError **error)
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)
145 return self->name;
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)
180 switch(unit_type){
181 case HKL_UNIT_DEFAULT:
182 return self->_value;
183 break;
184 case HKL_UNIT_USER:
185 return self->_value * hkl_unit_factor(self->unit, self->punit);
186 break;
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:
208 * @self: this ptr
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)
245 double factor;
247 switch (unit_type){
248 case HKL_UNIT_DEFAULT:
249 *min = self->range.min;
250 *max = self->range.max;
251 break;
252 case HKL_UNIT_USER:
253 factor = hkl_unit_factor(self->unit, self->punit);
254 *min = factor * self->range.min;
255 *max = factor * self->range.max;
256 break;
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)
276 double factor;
278 hkl_error (error == NULL || *error == NULL);
280 if (min > max){
281 g_set_error(error,
282 HKL_PARAMETER_ERROR,
283 HKL_PARAMETER_ERROR_MIN_MAX_SET,
284 "can not set this range min > max\n");
286 return FALSE;
289 switch (unit_type){
290 case HKL_UNIT_DEFAULT:
291 self->range.min = min;
292 self->range.max = max;
293 break;
294 case HKL_UNIT_USER:
295 factor = hkl_unit_factor(self->unit, self->punit);
296 self->range.min = min / factor;
297 self->range.max = max / factor;
298 break;
301 return TRUE;
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
309 * @todo test
311 int hkl_parameter_fit_get(const HklParameter *self)
313 return self->fit;
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
322 * @todo test
324 void hkl_parameter_fit_set(HklParameter *self, int fit)
326 self->fit = fit;
330 * hkl_parameter_randomize: (skip)
331 * @self:
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)
342 * @self:
344 * check if the value of the #HklParameter is in the min,max range
346 * Returns:
348 int hkl_parameter_is_valid(const HklParameter *self)
350 return self->ops->is_valid(self);
354 * hkl_parameter_fprintf: (skip)
355 * @f:
356 * @self:
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);
363 if (self->punit)
364 fprintf(f, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
365 self->name,
366 self->_value * factor,
367 self->punit->repr,
368 self->range.min * factor,
369 self->range.max * factor,
370 self->fit);
371 else
372 fprintf(f, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
373 self->name,
374 self->_value * factor,
375 self->range.min * factor,
376 self->range.max * factor,
377 self->fit);
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;