[binoculars-ng] refactor with hspec-attoparsec
[hkl.git] / hkl / hkl-source.c
blobd873d1584b4c9a345720ef4d5c31a0939e840d4c
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-2019, 2022 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 <math.h> // for fabs, sqrt
23 #include <stdio.h> // for fprintf, FILE
24 #include <stdlib.h> // for free
25 #include "hkl-macros-private.h" // for HKL_MALLOC
26 #include "hkl-source-private.h" // for HklSource
27 #include "hkl-vector-private.h" // for hkl_vector_div_double, etc
28 #include "hkl.h" // for HKL_EPSILON, FALSE, etc
30 /**
31 * hkl_source_dup:
32 * @self: the #Hklsource to copy
34 * copy constructor
35 * TODO test
37 * Returns:
38 **/
39 HklSource *hkl_source_dup(const HklSource *self)
41 HklSource *dup = g_new(HklSource, 1);
43 *dup = *self;
45 return dup;
48 /**
49 * hkl_source_free:
50 * @self: the #Hklsource to delete
52 * destructor
53 * TODO: test
54 **/
55 void hkl_source_free(HklSource *self)
57 if (self)
58 free(self);
61 /**
62 * hkl_source_init:
63 * @self: the #Hklsource to initialize
64 * @wave_length: the wave length to set
65 * @x: x coordinates of the ki vector
66 * @y: y coordinates of the ki vector
67 * @z: z coordinates of the ki vector
69 * initialize the #HklSource
71 * Returns: HKL_SUCCESS if everythongs goes fine, HKL_FAIL otherwise
72 **/
73 int hkl_source_init(HklSource *self,
74 double wave_length, double x, double y, double z)
76 if (wave_length > HKL_EPSILON &&
77 ( x > HKL_EPSILON || y > HKL_EPSILON || z > HKL_EPSILON)) {
78 double norm;
80 norm = sqrt(x*x + y*y + z*z);
82 self->wave_length = wave_length;
83 hkl_vector_init(&self->direction, x, y, z);
84 hkl_vector_div_double(&self->direction, norm);
85 return TRUE;
86 } else
87 return FALSE;
90 /**
91 * hkl_source_cmp: (skip)
92 * @self: 1st #Hklsource
93 * @s: 2nd #Hklsource
95 * compare two sources
97 * Returns:
98 **/
99 int hkl_source_cmp(HklSource const *self, HklSource const *s)
101 return ( (fabs(self->wave_length - s->wave_length) < HKL_EPSILON)
102 && hkl_vector_is_colinear(&self->direction,
103 &s->direction));
107 * hkl_source_compute_ki: (skip)
108 * @self:
109 * @ki: (out caller-allocates):
111 * compute the ki hkl_vector
113 void hkl_source_compute_ki(HklSource const *self, HklVector *ki)
115 *ki = self->direction;
116 hkl_vector_times_double(ki, HKL_TAU / self->wave_length);
120 * hkl_source_get_wavelength: (skip)
121 * @self:
123 * get the wave_length
125 * Returns: the wave_length
127 double hkl_source_get_wavelength(HklSource const *self)
129 return self->wave_length;
133 * hkl_source_fprintf: (skip)
134 * @f:
135 * @self:
137 * printf the source
139 void hkl_source_fprintf(FILE *f, HklSource const *self)
141 fprintf(f, "%f", self->wave_length);
142 hkl_vector_fprintf(f, &self->direction);