File: harp-geometry-sphere-euler.c

package info (click to toggle)
harp 1.5%2Bdata-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 54,032 kB
  • sloc: xml: 286,510; ansic: 143,710; yacc: 1,910; python: 913; makefile: 600; lex: 574; sh: 69
file content (298 lines) | stat: -rw-r--r-- 10,875 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2015-2018 S[&]T, The Netherlands.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "harp-geometry.h"

#include <math.h>
#include <stdlib.h>
#include <string.h>

/* Check if two Euler transformation are equal */
int harp_euler_transformation_equal(const harp_euler_transformation *euler1, const harp_euler_transformation *euler2)
{
    harp_spherical_point pointin[2], point[4];

    pointin[0].lat = 0.0;
    pointin[0].lon = 0.0;

    pointin[1].lat = 0.0;
    pointin[1].lon = M_PI_2;

    harp_spherical_point_apply_euler_transformation(&point[0], &pointin[0], euler1);
    harp_spherical_point_apply_euler_transformation(&point[1], &pointin[1], euler1);

    harp_spherical_point_apply_euler_transformation(&point[2], &pointin[0], euler2);
    harp_spherical_point_apply_euler_transformation(&point[3], &pointin[1], euler2);

    return (harp_spherical_point_equal(&point[0], &point[2]) && harp_spherical_point_equal(&point[1], &point[3]));
}

/* This transforms an Euler transformation into an ZXZ-axis Euler transformation */
void harp_euler_transformation_transform_to_zxz_euler_transformation(harp_euler_transformation *transformationout,
                                                                     const harp_euler_transformation *transformationin,
                                                                     const harp_euler_transformation *transformation)
{
    harp_spherical_point point[4];

    point[0].lat = 0.0;
    point[0].lon = 0.0;

    point[1].lat = 0.0;
    point[1].lon = M_PI_2;

    harp_spherical_point_apply_euler_transformation(&point[2], &point[0], transformationin);
    harp_spherical_point_apply_euler_transformation(&point[3], &point[1], transformationin);

    harp_spherical_point_apply_euler_transformation(&point[0], &point[2], transformation);
    harp_spherical_point_apply_euler_transformation(&point[1], &point[3], transformation);

    /* Determine output transformation */
    harp_euler_transformation_from_spherical_vector(transformationout, &point[0], &point[1]);
}

/* Invert an Euler transformation
 *
 * Parameter:
 *    pointer to input transformation
 *    Replace input transformation with output transformation
 */
void harp_euler_transformation_invert(harp_euler_transformation *transformation)
{
    harp_spherical_point point[3];

    const unsigned char c = transformation->phi_axis;

    point[2].lat = 0.0;
    point[1].lat = 0.0;
    point[0].lat = 0.0;

    point[2].lon = -transformation->phi;
    point[1].lon = -transformation->theta;
    point[0].lon = -transformation->psi;

    /* Check spherical points */
    harp_spherical_point_check(&point[0]);
    harp_spherical_point_check(&point[1]);
    harp_spherical_point_check(&point[2]);

    transformation->phi = point[0].lon;
    transformation->theta = point[1].lon;
    transformation->psi = point[2].lon;

    /* Swap phi and psi-axis */
    transformation->phi_axis = transformation->psi_axis;
    transformation->psi_axis = c;
}

/* Sets axes of rotation to ZXZ */
void harp_euler_transformation_set_to_zxz(harp_euler_transformation *transformation)
{
    transformation->phi_axis = 'Z';
    transformation->theta_axis = 'X';
    transformation->psi_axis = 'Z';
}

/* Transform a spherical vector to an
 * inverse Euler transformation
 *
 * Parameters:
 *   inverse_transformation = pointer to
 *       inverse Euler transformation
 *   pointbegin = pointer to begin of spherical vector
 *   pointend = pointer to end of spherical vector
 */
static void inverse_euler_transformation_from_spherical_vector(harp_euler_transformation *inverse_transformation,
                                                               const harp_spherical_point *sphericalvectorbegin,
                                                               const harp_spherical_point *sphericalvectorend)
{
    if (harp_spherical_point_equal(sphericalvectorbegin, sphericalvectorend))
    {
        inverse_transformation->phi = 0.0;
        inverse_transformation->theta = 0.0;
        inverse_transformation->psi = 0.0;
    }
    else
    {
        harp_vector3d vectorbegin;
        harp_vector3d vectorend;
        harp_vector3d vectortemp;
        harp_spherical_point pointtemp[2];

        /* Convert (lat,lon) coordinates to Cartesian coordinates and
           calculate cross product of the two obtained vectors */
        harp_vector3d_from_spherical_point(&vectorbegin, sphericalvectorbegin);
        harp_vector3d_from_spherical_point(&vectorend, sphericalvectorend);
        harp_vector3d_crossproduct(&vectortemp, &vectorbegin, &vectorend);

        /* Convert (x,y,z) of obtained point (lat,lon) and store
           it in pointtemp[0] */
        harp_spherical_point_from_vector3d(&pointtemp[0], &vectortemp);

        inverse_transformation->phi = -pointtemp[0].lon - M_PI_2;
        inverse_transformation->theta = pointtemp[0].lat - M_PI_2;
        inverse_transformation->psi = 0.0;

        /* Use ZXZ as axes of transformation */
        harp_euler_transformation_set_to_zxz(inverse_transformation);

        /* Apply Euler transformation on the spherical point */
        harp_spherical_point_apply_euler_transformation(&pointtemp[1], sphericalvectorbegin, inverse_transformation);

        inverse_transformation->psi = -pointtemp[1].lon;
    }
}

/* Transform a spherical vector to a Euler transformation
 *
 * Parameters:
 *   transformation = pointer to Euler transformation
 *   pointbegin = pointer to begin of spherical vector
 *   pointend = pointer to end of spherical vector
 */
void harp_euler_transformation_from_spherical_vector(harp_euler_transformation *transformation,
                                                     const harp_spherical_point *sphericalvectorbegin,
                                                     const harp_spherical_point *sphericalvectorend)
{
    /* Determine inverse Euler transformation and save the inverse transformation in "transformation" */
    inverse_euler_transformation_from_spherical_vector(transformation, sphericalvectorbegin, sphericalvectorend);

    /* Invert the inverse Euler transformation */
    harp_euler_transformation_invert(transformation);
}

/* Apply Euler transformation of 3d vector.
 * This involves a transformation over three angles:
 *
 *   psi
 *   theta
 *   phi
 *
 * Here, the angles are in [rad]
 */
static int vector3d_apply_euler_transformation(harp_vector3d *vectorout, const harp_vector3d *vectorin,
                                               const harp_euler_transformation *transformation)
{
    double u[3], v[3], sin_angle, cos_angle;
    const double *angle;
    unsigned char axis;
    int i;

    /* Input vector */
    axis = 'X'; /* Assume X */
    angle = NULL;
    u[0] = vectorin->x;
    u[1] = vectorin->y;
    u[2] = vectorin->z;

    for (i = 0; i < 3; i++)
    {
        switch (i)
        {
            case 0:
                angle = &transformation->phi;
                axis = transformation->phi_axis;
                break;
            case 1:
                angle = &transformation->theta;
                axis = transformation->theta_axis;
                break;
            case 2:
                angle = &transformation->psi;
                axis = transformation->psi_axis;
                break;
        }

        if (HARP_GEOMETRY_FPzero(*angle))
        {
            continue;
        }

        sin_angle = sin(*angle);
        cos_angle = cos(*angle);

        switch (axis)
        {
            case 'X':
                /* transformation around X-axis */
                v[0] = u[0];
                v[1] = cos_angle * u[1] - sin_angle * u[2];
                v[2] = sin_angle * u[1] + cos_angle * u[2];

                break;

            case 'Y':
                /* transformation around Y-axis */
                v[0] = cos_angle * u[0] + sin_angle * u[2];
                v[1] = u[1];
                v[2] = -sin_angle * u[0] + cos_angle * u[2];
                break;

            case 'Z':
                /* transformation around Z-axis */
                v[0] = cos_angle * u[0] - sin_angle * u[1];
                v[1] = sin_angle * u[0] + cos_angle * u[1];
                v[2] = u[2];
                break;

            default:
                harp_set_error(HARP_ERROR_INVALID_ARGUMENT, "invalid Euler axis");
                return -1;
        }

        memcpy((void *)&u[0], (void *)&v[0], sizeof(u));
    }

    vectorout->x = u[0];
    vectorout->y = u[1];
    vectorout->z = u[2];

    return 0;
}

/* Apply Euler transformation of spherical point */
void harp_spherical_point_apply_euler_transformation(harp_spherical_point *pointout,
                                                     const harp_spherical_point *pointin,
                                                     const harp_euler_transformation *transformation)
{
    harp_vector3d vectorin;
    harp_vector3d vectorout;

    /* First, convert (lat,lon) to (x,y,z) coordinates */
    harp_vector3d_from_spherical_point(&vectorin, pointin);

    /* Rotate the vector around the 3 Euler axes to get the output vector */
    vector3d_apply_euler_transformation(&vectorout, &vectorin, transformation);

    /* Finally, convert the rotated vector (x,y,z) to (lat,lon) coordinates */
    harp_spherical_point_from_vector3d(pointout, &vectorout);

    harp_spherical_point_check(pointout);
}