Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
proj_pt.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
13#ifndef SEEN_PROJ_PT_H
14#define SEEN_PROJ_PT_H
15
16#include <2geom/point.h>
17#include <cstdio>
18
19namespace Proj {
20
21const double epsilon = 1E-6;
22
23// TODO: Catch the case when the constructors are called with only zeros
24class Pt2 {
25public:
26 Pt2 () { pt[0] = 0; pt[1] = 0; pt[2] = 1.0; } // we default to (0 : 0 : 1)
27 Pt2 (double x, double y, double w) { pt[0] = x; pt[1] = y; pt[2] = w; }
28 Pt2 (Geom::Point const &point) { pt[0] = point[Geom::X]; pt[1] = point[Geom::Y]; pt[2] = 1; }
29 Pt2 (const char *coord_str);
30
31 inline double operator[] (unsigned int index) const {
32 if (index > 2) { return Geom::infinity(); }
33 return pt[index];
34 }
35 inline double &operator[] (unsigned int index) {
36 // FIXME: How should we handle wrong indices?
37 //if (index > 2) { return Geom::infinity(); }
38 return pt[index];
39 }
40 inline bool operator== (Pt2 &rhs) {
41 normalize();
42 rhs.normalize();
43 return (fabs(pt[0] - rhs.pt[0]) < epsilon &&
44 fabs(pt[1] - rhs.pt[1]) < epsilon &&
45 fabs(pt[2] - rhs.pt[2]) < epsilon);
46 }
47 inline bool operator!= (Pt2 &rhs) {
48 return !((*this) == rhs);
49 }
50
51 /*** For convenience, we define addition/subtraction etc. as "affine" operators (i.e.,
52 the result for finite points is the same as if the affine points were added ***/
53 inline Pt2 operator+(Pt2 &rhs) const {
54 Pt2 result (*this);
55 result.normalize();
56 rhs.normalize();
57 for ( unsigned i = 0 ; i < 2 ; ++i ) {
58 result.pt[i] += rhs.pt[i];
59 }
60 return result;
61 }
62
63 inline Pt2 operator-(Pt2 &rhs) const {
64 Pt2 result (*this);
65 result.normalize();
66 rhs.normalize();
67 for ( unsigned i = 0 ; i < 2 ; ++i ) {
68 result.pt[i] -= rhs.pt[i];
69 }
70 return result;
71 }
72
73 inline Pt2 operator*(double const s) const {
74 Pt2 result (*this);
75 result.normalize();
76 for ( unsigned i = 0 ; i < 2 ; ++i ) {
77 result.pt[i] *= s;
78 }
79 return result;
80 }
81
82 void normalize();
84 inline bool is_finite() { return pt[2] != 0; } // FIXME: Should we allow for some tolerance?
85 char *coord_string();
86 inline void print(char const *s) const { printf ("%s(%8.2f : %8.2f : %8.2f)\n", s, pt[0], pt[1], pt[2]); }
87
88private:
89 double pt[3];
90};
91
92
93class Pt3 {
94public:
95 Pt3 () { pt[0] = 0; pt[1] = 0; pt[2] = 0; pt[3] = 1.0; } // we default to (0 : 0 : 0 : 1)
96 Pt3 (double x, double y, double z, double w) { pt[0] = x; pt[1] = y; pt[2] = z; pt[3] = w; }
97 Pt3 (const char *coord_str);
98
99 inline bool operator== (Pt3 &rhs) {
100 normalize();
101 rhs.normalize();
102 return (fabs(pt[0] - rhs.pt[0]) < epsilon &&
103 fabs(pt[1] - rhs.pt[1]) < epsilon &&
104 fabs(pt[2] - rhs.pt[2]) < epsilon &&
105 fabs(pt[3] - rhs.pt[3]) < epsilon);
106 }
107
108 /*** For convenience, we define addition/subtraction etc. as "affine" operators (i.e.,
109 the result for finite points is the same as if the affine points were added ***/
110 inline Pt3 operator+(Pt3 &rhs) const {
111 Pt3 result(*this);
112 result.normalize();
113 rhs.normalize();
114 for ( unsigned i = 0 ; i < 3 ; ++i ) {
115 result.pt[i] += rhs.pt[i];
116 }
117 return result;
118 }
119
120 inline Pt3 operator-(Pt3 &rhs) const {
121 Pt3 result (*this);
122 result.normalize();
123 rhs.normalize();
124 for ( unsigned i = 0 ; i < 3 ; ++i ) {
125 result.pt[i] -= rhs.pt[i];
126 }
127 return result;
128 }
129
130 inline Pt3 operator*(double const s) const {
131 Pt3 result (*this);
132 result.normalize();
133 for ( unsigned i = 0 ; i < 3 ; ++i ) {
134 result.pt[i] *= s;
135 }
136 return result;
137 }
138
139 inline double operator[] (unsigned int index) const {
140 if (index > 3) { return Geom::infinity(); }
141 return pt[index];
142 }
143 inline double &operator[] (unsigned int index) {
144 // FIXME: How should we handle wrong indices?
145 //if (index > 3) { return Geom::infinity(); }
146 return pt[index];
147 }
148 void normalize();
149 inline bool is_finite() { return pt[3] != 0; } // FIXME: Should we allow for some tolerance?
150 char *coord_string();
151 inline void print(char const *s) const {
152 printf ("%s(%8.2f : %8.2f : %8.2f : %8.2f)\n", s, pt[0], pt[1], pt[2], pt[3]);
153 }
154
155private:
156 double pt[4];
157};
158
159} // namespace Proj
160
161#endif // !SEEN_PROJ_PT_H
162
163/*
164 Local Variables:
165 mode:c++
166 c-file-style:"stroustrup"
167 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168 indent-tabs-mode:nil
169 fill-column:99
170 End:
171*/
172// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :
Cartesian point / 2D vector and related operations.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Pt2 operator*(double const s) const
Definition proj_pt.h:73
Pt2 operator+(Pt2 &rhs) const
Definition proj_pt.h:53
double operator[](unsigned int index) const
Definition proj_pt.h:31
char * coord_string()
Definition proj_pt.cpp:59
Pt2 operator-(Pt2 &rhs) const
Definition proj_pt.h:63
bool operator!=(Pt2 &rhs)
Definition proj_pt.h:47
double pt[3]
Definition proj_pt.h:89
bool is_finite()
Definition proj_pt.h:84
void print(char const *s) const
Definition proj_pt.h:86
void normalize()
Definition proj_pt.cpp:42
Pt2(double x, double y, double w)
Definition proj_pt.h:27
Pt2(Geom::Point const &point)
Definition proj_pt.h:28
Geom::Point affine()
Definition proj_pt.cpp:51
char * coord_string()
Definition proj_pt.cpp:101
double pt[4]
Definition proj_pt.h:156
bool is_finite()
Definition proj_pt.h:149
Pt3 operator*(double const s) const
Definition proj_pt.h:130
Pt3(double x, double y, double z, double w)
Definition proj_pt.h:96
double operator[](unsigned int index) const
Definition proj_pt.h:139
Pt3 operator-(Pt3 &rhs) const
Definition proj_pt.h:120
Pt3 operator+(Pt3 &rhs) const
Definition proj_pt.h:110
void normalize()
Definition proj_pt.cpp:91
void print(char const *s) const
Definition proj_pt.h:151
const double w
Definition conic-4.cpp:19
Css & result
constexpr Coord infinity()
Get a value representing infinity.
Definition coord.h:88
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Generic auxiliary routines for 3D axes.
const double epsilon
Definition proj_pt.h:21
int index