1 #define __PROJ_PT_C__
3 /*
4 * 3x4 transformation matrix to map points from projective 3-space into the projective plane
5 *
6 * Authors:
7 * Maximilian Albert <Anhalter42@gmx.de>
8 *
9 * Copyright (C) 2007 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "proj_pt.h"
15 #include "svg/stringstream.h"
17 namespace Proj {
19 Pt2::Pt2(const gchar *coord_str) {
20 if (!coord_str) {
21 pt[0] = 0.0;
22 pt[1] = 0.0;
23 pt[2] = 1.0;
24 g_warning ("Coordinate string is empty. Creating default Pt2\n");
25 return;
26 }
27 gchar **coords = g_strsplit(coord_str, ":", 0);
28 if (coords[0] == NULL || coords[1] == NULL || coords[2] == NULL) {
29 g_strfreev (coords);
30 g_warning ("Malformed coordinate string.\n");
31 return;
32 }
34 pt[0] = g_ascii_strtod(coords[0], NULL);
35 pt[1] = g_ascii_strtod(coords[1], NULL);
36 pt[2] = g_ascii_strtod(coords[2], NULL);
37 }
39 void
40 Pt2::normalize() {
41 if (fabs(pt[2]) < 1E-6 || pt[2] == 1.0)
42 return;
43 pt[0] /= pt[2];
44 pt[1] /= pt[2];
45 pt[2] = 1.0;
46 }
48 Geom::Point
49 Pt2::affine() {
50 if (fabs(pt[2]) < epsilon) {
51 return Geom::Point (NR_HUGE, NR_HUGE);
52 }
53 return Geom::Point (pt[0]/pt[2], pt[1]/pt[2]);
54 }
56 gchar *
57 Pt2::coord_string() {
58 Inkscape::SVGOStringStream os;
59 os << pt[0] << " : "
60 << pt[1] << " : "
61 << pt[2];
62 return g_strdup(os.str().c_str());
63 }
65 Pt3::Pt3(const gchar *coord_str) {
66 if (!coord_str) {
67 pt[0] = 0.0;
68 pt[1] = 0.0;
69 pt[2] = 0.0;
70 pt[3] = 1.0;
71 g_warning ("Coordinate string is empty. Creating default Pt2\n");
72 return;
73 }
74 gchar **coords = g_strsplit(coord_str, ":", 0);
75 if (coords[0] == NULL || coords[1] == NULL ||
76 coords[2] == NULL || coords[3] == NULL) {
77 g_strfreev (coords);
78 g_warning ("Malformed coordinate string.\n");
79 return;
80 }
82 pt[0] = g_ascii_strtod(coords[0], NULL);
83 pt[1] = g_ascii_strtod(coords[1], NULL);
84 pt[2] = g_ascii_strtod(coords[2], NULL);
85 pt[3] = g_ascii_strtod(coords[3], NULL);
86 }
88 void
89 Pt3::normalize() {
90 if (fabs(pt[3]) < 1E-6 || pt[3] == 1.0)
91 return;
92 pt[0] /= pt[3];
93 pt[1] /= pt[3];
94 pt[2] /= pt[3];
95 pt[3] = 1.0;
96 }
98 gchar *
99 Pt3::coord_string() {
100 Inkscape::SVGOStringStream os;
101 os << pt[0] << " : "
102 << pt[1] << " : "
103 << pt[2] << " : "
104 << pt[3];
105 return g_strdup(os.str().c_str());
106 }
108 } // namespace Proj
110 /*
111 Local Variables:
112 mode:c++
113 c-file-style:"stroustrup"
114 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
115 indent-tabs-mode:nil
116 fill-column:99
117 End:
118 */
119 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :