summaryrefslogtreecommitdiffstats
path: root/src/common/ufo-math.c
blob: 78b7ddad62eb8126ab9bc22939ee1c9411342557 (plain)
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
/*
 * Copyright (C) 2015-2017 Karlsruhe Institute of Technology
 *
 * This file is part of Ufo.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>
#include <glib.h>
#include "ufo-math.h"

UfoPoint *
ufo_point_new (gdouble x, gdouble y, gdouble z)
{
    UfoPoint *point = g_new (UfoPoint, 1);
    point->x = x;
    point->y = y;
    point->z = z;

    return point;
}

void
ufo_point_free (UfoPoint *point)
{
    g_free (point);
}

void
ufo_point_mul_scalar (UfoPoint *point, gdouble value)
{
    point->x *= value;
    point->y *= value;
    point->z *= value;
}

void
ufo_point_add (UfoPoint *point, UfoPoint *other)
{
    point->x += other->x;
    point->y += other->y;
    point->z += other->z;
}

void
ufo_point_subtract (UfoPoint *point, UfoPoint *other)
{
    point->x -= other->x;
    point->y -= other->y;
    point->z -= other->z;
}

gdouble
ufo_point_dot_product (UfoPoint *point, UfoPoint *other)
{
    return point->x * other->x + point->y * other->y + point->z * other->z;
}

void
ufo_point_rotate_x (UfoPoint *point, gdouble angle)
{
    UfoPoint tmp;
    gdouble sin_angle = sin (angle);
    gdouble cos_angle = cos (angle);

    tmp.x = point->x;
    tmp.y = point->y * cos_angle - point->z * sin_angle;
    tmp.z = point->y * sin_angle + point->z * cos_angle;
    *point = tmp;
}

void
ufo_point_rotate_y (UfoPoint *point, gdouble angle)
{
    UfoPoint tmp;
    gdouble sin_angle = sin (angle);
    gdouble cos_angle = cos (angle);

    tmp.x = point->x * cos_angle + point->z * sin_angle;
    tmp.y = point->y;
    tmp.z = -point->x * sin_angle + point->z * cos_angle;
    *point = tmp;
}

void
ufo_point_rotate_z (UfoPoint *point, gdouble angle)
{

    UfoPoint tmp;
    gdouble sin_angle = sin (angle);
    gdouble cos_angle = cos (angle);

    tmp.x = point->x * cos_angle - point->y * sin_angle;
    tmp.y = point->x * sin_angle + point->y * cos_angle;
    tmp.z = point->z;
    *point = tmp;
}

gdouble
ufo_clip_value (gdouble value, gdouble minimum, gdouble maximum)
{
    return MAX (minimum, MIN (maximum, value));
}

static gdouble
find_extremum (gdouble *array, gint num_values, gint sgn)
{
    gint i;
    gdouble minimum = sgn * array[0];

    for (i = 1; i < num_values; i++) {
        if (sgn * array[i] < minimum) {
            minimum = sgn * array[i];
        }
    }

    return sgn * minimum;
}

gdouble
ufo_array_maximum (gdouble *array, gint num_values)
{
    return find_extremum (array, num_values, -1);
}

gdouble
ufo_array_minimum (gdouble *array, gint num_values)
{
    return find_extremum (array, num_values, 1);
}