upgrading copyright year from 2016 to 2017
[hkl.git] / hkl / hkl-unit.c
blob0943c12ed5fa18c87d3c1ed8070c86b4d0f6359d
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-2017 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 <gsl/gsl_nan.h> // for GSL_NAN
23 #include <gsl/gsl_sys.h> // for gsl_isnan
24 #include <stdlib.h> // for free, NULL
25 #include "hkl-macros-private.h" // for HKL_MALLOC
26 #include "hkl-unit-private.h" // for HklUnit, etc
27 #include "hkl.h" // for FALSE, HKL_DEGTORAD, etc
29 /**
30 * hkl_unit_dup: (skip)
31 * @self:
33 * copy an #Hklunit
35 * Returns: the copied #HklUnit (memory must be release with
36 * hkl_unit_free)
37 **/
38 HklUnit* hkl_unit_dup(const HklUnit *self)
40 if (!self)
41 return NULL;
43 HklUnit *dup = HKL_MALLOC(HklUnit);
44 *dup = *self;
46 return dup;
49 /**
50 * hkl_unit_free: (skip)
51 * @self:
53 * release the memory of an #HklUnit
54 **/
55 void hkl_unit_free(HklUnit *self)
57 if (self)
58 free(self);
61 /**
62 * hkl_unit_compatible: (skip)
63 * @self: the first @HklUnit
64 * @unit: the second @HklUnit to check
66 * check if two units are compatible.
68 * Returns: TRUE or FALSE
69 **/
70 int hkl_unit_compatible(const HklUnit *self, const HklUnit *unit)
72 int res = TRUE;
73 if (self && unit){
74 switch(self->type){
75 case HKL_UNIT_ANGLE_DEG:
76 switch(unit->type){
77 case HKL_UNIT_ANGLE_DEG:
78 case HKL_UNIT_ANGLE_RAD:
79 case HKL_UNIT_ANGLE_MRAD:
80 break;
81 default:
82 res = FALSE;
83 break;
85 break;
86 case HKL_UNIT_ANGLE_RAD:
87 switch(unit->type){
88 case HKL_UNIT_ANGLE_DEG:
89 case HKL_UNIT_ANGLE_RAD:
90 case HKL_UNIT_ANGLE_MRAD:
91 break;
92 default:
93 res = FALSE;
94 break;
96 break;
97 case HKL_UNIT_LENGTH_NM:
98 switch(unit->type){
99 case HKL_UNIT_LENGTH_NM:
100 break;
101 default:
102 res = FALSE;
103 break;
105 break;
106 case HKL_UNIT_ANGLE_MRAD:
107 switch(unit->type){
108 case HKL_UNIT_ANGLE_DEG:
109 case HKL_UNIT_ANGLE_RAD:
110 case HKL_UNIT_ANGLE_MRAD:
111 break;
112 default:
113 res = FALSE;
114 break;
116 break;
119 return res;
123 * hkl_unit_factor:
124 * @self:
125 * @unit:
127 * compute the factor to convert from one @Hklunit to another one.
128 * @self * factor = @unit
130 * Returns: the factor of the conversion.
132 double hkl_unit_factor(const HklUnit *self, const HklUnit *unit)
134 double factor = 1.;
136 if (self && unit){
137 switch(self->type){
138 case HKL_UNIT_ANGLE_DEG:
139 switch(unit->type){
140 case HKL_UNIT_ANGLE_DEG:
141 break;
142 case HKL_UNIT_ANGLE_RAD:
143 factor = HKL_DEGTORAD;
144 break;
145 case HKL_UNIT_ANGLE_MRAD:
146 factor = HKL_DEGTORAD * 1e3;
147 break;
148 default:
149 factor = GSL_NAN;
150 break;
152 break;
153 case HKL_UNIT_ANGLE_RAD:
154 switch(unit->type){
155 case HKL_UNIT_ANGLE_DEG:
156 factor = HKL_RADTODEG;
157 break;
158 case HKL_UNIT_ANGLE_RAD:
159 break;
160 case HKL_UNIT_ANGLE_MRAD:
161 factor = 1e3;
162 break;
163 default:
164 factor = GSL_NAN;
165 break;
167 break;
168 case HKL_UNIT_LENGTH_NM:
169 switch(unit->type){
170 case HKL_UNIT_LENGTH_NM:
171 break;
172 default:
173 factor = GSL_NAN;
174 break;
176 break;
177 case HKL_UNIT_ANGLE_MRAD:
178 switch(unit->type){
179 case HKL_UNIT_ANGLE_DEG:
180 factor = 1e-3 * HKL_RADTODEG;
181 break;
182 case HKL_UNIT_ANGLE_RAD:
183 factor = 1e-3;
184 break;
185 case HKL_UNIT_ANGLE_MRAD:
186 break;
187 default:
188 factor = GSL_NAN;
189 break;
191 break;
194 return factor;