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 double min
, double value
, double max
,
39 const HklUnit
*unit
, const HklUnit
*punit
)
44 && hkl_unit_compatible(unit
, punit
)) {
46 self
->range
.min
= min
;
47 self
->range
.max
= max
;
52 self
->changed
= changed
;
53 self
->ops
= &hkl_parameter_operations_defaults
;
61 * hkl_parameter_new: (skip)
71 * create a new #HklParameter
75 HklParameter
*hkl_parameter_new(const char *name
,
76 double min
, double value
, double max
,
78 const HklUnit
*unit
, const HklUnit
*punit
)
82 self
= HKL_MALLOC(HklParameter
);
84 if (!hkl_parameter_init(self
,
85 name
, min
, value
, max
,
96 * hkl_parameter_new_copy: (skip)
99 * copy an #HklParameter
103 HklParameter
*hkl_parameter_new_copy(const HklParameter
*self
)
105 return self
->ops
->copy(self
);
109 * hkl_parameter_free: (skip)
112 * delete an #HklParameter
114 void hkl_parameter_free(HklParameter
*self
)
116 self
->ops
->free(self
);
120 * hkl_parameter_init_copy: (skip)
121 * @self: the this ptr
122 * @src: the parameter to copy from
123 * @error: return location for a GError, or NULL
125 * Returns: TRUE on success, FALSE if an error occurred
127 int hkl_parameter_init_copy(HklParameter
*self
, const HklParameter
*src
,
130 return self
->ops
->init_copy(self
, src
, error
);
133 const char *hkl_parameter_name_get(const HklParameter
*self
)
139 * hkl_parameter_value_get:
140 * @self: the this ptr
142 * Returns: the value of the #HklParameter
144 inline double hkl_parameter_value_get(const HklParameter
*self
)
150 * hkl_parameter_value_unit_get:
151 * @self: the this ptr
153 * Returns: the value of the #HklParameter expressed in the user unit
155 inline double hkl_parameter_value_unit_get(const HklParameter
*self
)
157 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
159 return self
->_value
* factor
;
164 * hkl_parameter_value_get_closest:
165 * @self: the this ptr
166 * @ref: the reference #HklParameter
170 * Returns: the closest value of the ref #HklParameter from the
171 * current self #HklParameter
173 inline double hkl_parameter_value_get_closest(const HklParameter
*self
,
174 const HklParameter
*ref
)
176 return self
->ops
->get_value_closest(self
, ref
);
180 * hkl_parameter_value_set: (skip)
182 * @value: the value to set
183 * @error: return location for a GError, or NULL
185 * set the value of an #HklParameter
187 * Returns: TRUE on success, FALSE if an error occurred
189 inline int hkl_parameter_value_set(HklParameter
*self
, double value
,
192 return self
->ops
->set_value(self
, value
, error
);
196 * hkl_parameter_value_unit_set:
197 * @self: the this ptr
198 * @value: the value to set
199 * @error: return location for a GError, or NULL
201 * set the value of the parameter express in the punit #HklUnit
204 * Returns: TRUE on success, FALSE if an error occurred
206 inline int hkl_parameter_value_unit_set(HklParameter
*self
, double value
,
209 return self
->ops
->set_value_unit(self
, value
, error
);
213 * hkl_parameter_value_set_smallest_in_range: (skip)
214 * @self: the this ptr
216 inline void hkl_parameter_value_set_smallest_in_range(HklParameter
*self
)
218 self
->ops
->set_value_smallest_in_range(self
);
222 * hkl_parameter_min_max_get: (skip)
225 * get the max value of the #HklParameter
228 void hkl_parameter_min_max_get(const HklParameter
*self
, double *min
, double *max
)
230 *min
= self
->range
.min
;
231 *max
= self
->range
.max
;
235 * hkl_parameter_min_max_unit_get: (skip)
240 * get the #HklParameter range, min, max
243 void hkl_parameter_min_max_unit_get(const HklParameter
*self
, double *min
, double *max
)
245 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
247 *min
= factor
* self
->range
.min
;
248 *max
= factor
* self
->range
.max
;
252 * hkl_parameter_min_max_set: (skip)
253 * @self: the this ptr
256 * @error: return location for a GError, or NULL
258 * set the #HklParameter range.
259 * @todo test and set the GError
261 * Returns: TRUE on success, FALSE if an error occurred
263 int hkl_parameter_min_max_set(HklParameter
*self
, double min
, double max
,
266 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
271 HKL_PARAMETER_ERROR_MIN_MAX_SET
,
272 "can not set this range min > max\n");
277 self
->range
.min
= min
;
278 self
->range
.max
= max
;
284 * hkl_parameter_min_max_unit_set:
285 * @self: the this ptr
286 * @min: the minimum value to set
287 * @max: the maximum value to set
288 * @error: return location for a GError, or NULL
290 * set the #HklParameter range.
291 * @todo test and set the GError
293 * Returns: TRUE on success, FALSE if an error occurred
295 int hkl_parameter_min_max_unit_set(HklParameter
*self
, double min
, double max
,
298 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
300 return hkl_parameter_min_max_set(self
, min
/ factor
, max
/ factor
, error
);
304 * hkl_parameter_fit_get:
305 * @self: the this ptr
307 * Retuen value: the #HklParameter fit value, True is the parameter can be fitted, not otherwise
310 int hkl_parameter_fit_get(const HklParameter
*self
)
316 * hkl_parameter_fit_set:
317 * @self: the this ptr
318 * @fit: the fit value to set
320 * set the #HklParameter fit value, True is the parameter can be fitted, not otherwise
323 void hkl_parameter_fit_set(HklParameter
*self
, int fit
)
329 * hkl_parameter_randomize: (skip)
332 * randomize the #HklParameter value into the min,max range
334 void hkl_parameter_randomize(HklParameter
*self
)
336 self
->ops
->randomize(self
);
340 * hkl_parameter_is_valid: (skip)
343 * check if the value of the #HklParameter is in the min,max range
347 int hkl_parameter_is_valid(const HklParameter
*self
)
349 return self
->ops
->is_valid(self
);
353 * hkl_parameter_fprintf: (skip)
357 * print into the #FILE f an #HklParameter
359 void hkl_parameter_fprintf(FILE *f
, HklParameter
*self
)
361 double factor
= hkl_unit_factor(self
->unit
, self
->punit
);
363 fprintf(f
, "\"%s\" : %.7f %s [%.7f : %.7f] (%d)",
365 self
->_value
* factor
,
367 self
->range
.min
* factor
,
368 self
->range
.max
* factor
,
371 fprintf(f
, "\"%s\" : %.7f [%.7f : %.7f] (%d)",
373 self
->_value
* factor
,
374 self
->range
.min
* factor
,
375 self
->range
.max
* factor
,