summaryrefslogtreecommitdiffstats
path: root/src/proj_pt.cpp
blob: db9827f63fa29b63dcf04545c23fd95b3fa483ac (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
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * 3x4 transformation matrix to map points from projective 3-space into the projective plane
 *
 * Authors:
 *   Maximilian Albert <Anhalter42@gmx.de>
 *
 * Copyright (C) 2007  Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glib.h>

#include "proj_pt.h"
#include "svg/stringstream.h"

namespace Proj {

Pt2::Pt2(const char *coord_str) {
    if (!coord_str) {
        pt[0] = 0.0;
        pt[1] = 0.0;
        pt[2] = 1.0;
        g_warning ("Coordinate string is empty. Creating default Pt2\n");
        return;
    }
    char **coords = g_strsplit(coord_str, ":", 0);
    if (coords[0] == nullptr || coords[1] == nullptr || coords[2] == nullptr) {
        g_strfreev (coords);
        g_warning ("Malformed coordinate string.\n");
        return;
    }

    pt[0] = g_ascii_strtod(coords[0], nullptr);
    pt[1] = g_ascii_strtod(coords[1], nullptr);
    pt[2] = g_ascii_strtod(coords[2], nullptr);
    g_strfreev (coords);
}

void
Pt2::normalize() {
    if (fabs(pt[2]) < 1E-6 || pt[2] == 1.0)
        return;
    pt[0] /= pt[2];
    pt[1] /= pt[2];
    pt[2] = 1.0;
}

Geom::Point
Pt2::affine() {
  if (fabs(pt[2]) < epsilon) {
    return Geom::Point (Geom::infinity(), Geom::infinity());
  }
  return Geom::Point (pt[0]/pt[2], pt[1]/pt[2]);
}

char *
Pt2::coord_string() {
    Inkscape::SVGOStringStream os;
    os << pt[0] << " : "
       << pt[1] << " : "
       << pt[2];
    return g_strdup(os.str().c_str());
}

Pt3::Pt3(const char *coord_str) {
    if (!coord_str) {
        pt[0] = 0.0;
        pt[1] = 0.0;
        pt[2] = 0.0;
        pt[3] = 1.0;
        g_warning ("Coordinate string is empty. Creating default Pt2\n");
        return;
    }
    char **coords = g_strsplit(coord_str, ":", 0);
    if (coords[0] == nullptr || coords[1] == nullptr ||
        coords[2] == nullptr || coords[3] == nullptr) {
        g_strfreev (coords);
        g_warning ("Malformed coordinate string.\n");
        return;
    }

    pt[0] = g_ascii_strtod(coords[0], nullptr);
    pt[1] = g_ascii_strtod(coords[1], nullptr);
    pt[2] = g_ascii_strtod(coords[2], nullptr);
    pt[3] = g_ascii_strtod(coords[3], nullptr);
}

void
Pt3::normalize() {
    if (fabs(pt[3]) < 1E-6 || pt[3] == 1.0)
        return;
    pt[0] /= pt[3];
    pt[1] /= pt[3];
    pt[2] /= pt[3];
    pt[3] = 1.0;
}

char *
Pt3::coord_string() {
    Inkscape::SVGOStringStream os;
    os << pt[0] << " : "
       << pt[1] << " : "
       << pt[2] << " : "
       << pt[3];
    return g_strdup(os.str().c_str());
}

} // namespace Proj

/*
  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 :