summaryrefslogtreecommitdiffstats
path: root/src/display/nr-3dutils.cpp
blob: 3c0eb5eeb0be30fc4f3e124065fd8a985d13a5f0 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * 3D utils.
 *
 * Authors:
 *   Jean-Rene Reinhard <jr@komite.net>
 *
 * Copyright (C) 2007 authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glib.h>

#include "display/nr-3dutils.h"
#include <cmath>
#include <2geom/point.h>
#include <2geom/affine.h>

namespace NR {

void convert_coord(gdouble &x, gdouble &y, gdouble &z, Geom::Affine const &trans) {
    Geom::Point p = Geom::Point(x, y);
    p *= trans;
    x = p[Geom::X];
    y = p[Geom::Y];
    z *= trans[0];
}

gdouble norm(const Fvector &v) {
    return sqrt(v[X_3D]*v[X_3D] + v[Y_3D]*v[Y_3D] + v[Z_3D]*v[Z_3D]);
}

void normalize_vector(Fvector &v) {
    gdouble nv = norm(v);
    //TODO test nv == 0
    for (int j = 0; j < 3; j++) {
        v[j] /= nv;
    }
}

gdouble scalar_product(const Fvector &a, const Fvector &b) {
    return  a[X_3D] * b[X_3D] +
            a[Y_3D] * b[Y_3D] +
            a[Z_3D] * b[Z_3D];
}

void normalized_sum(Fvector &r, const Fvector &a, const Fvector &b) {
    r[X_3D] = a[X_3D] + b[X_3D];
    r[Y_3D] = a[Y_3D] + b[Y_3D];
    r[Z_3D] = a[Z_3D] + b[Z_3D];
    normalize_vector(r);
}

}/* namespace NR */

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :