-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.hpp
201 lines (167 loc) · 5.2 KB
/
Point.hpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef BLIB_POINT_HPP
#define BLIB_POINT_HPP
#include <boost/geometry/geometries/geometries.hpp>
#include "TransformMatrix.hpp"
#include "CoordinateConfigure.hpp"
#include <cmath>
#include <array>
#include <boost/operators.hpp>
namespace blib {
namespace geometry {
namespace bgeom = ::boost::geometry;
typedef ::blib::math::Vector3f Vec3f;;
// 2D Point Type
class Point2D : public boost::field_operators < Point2D > {
public:
typedef CoordinateType ValueType;
typedef Point2D SelfType;
typedef boost::mpl::int_ < 2 > Dimensions;
private:
std::array<ValueType, Dimensions::value> _coordinates;
public:
Point2D( const ValueType aX = 0, const ValueType aY = 0 ) :
_coordinates( { { aX, aY } } ) {}
void x( const ValueType aX ) {
_coordinates[ 0 ] = aX;
}
void y( const ValueType aY ) {
_coordinates[ 1 ] = aY;
}
ValueType x( ) const {
return _coordinates[ 0 ];
}
ValueType y( ) const {
return _coordinates[ 1 ];
}
ValueType& x( ) {
return _coordinates[ 0 ];
}
ValueType& y( ) {
return _coordinates[ 1 ];
}
Vec3f vec3f( ) const {
const Vec3f ret( _coordinates[ 0 ], _coordinates[ 1 ], 1 );
return ret;
}
// Convert to a Vec3f
operator Vec3f( ) const {
return vec3f( );
}
SelfType& operator=( Vec3f& aOther ) {
_coordinates[ 0 ] = aOther( 0 );
_coordinates[ 1 ] = aOther( 1 );
return *this;
}
SelfType& apply( TransformMatrix& aMat ) {
const Vec3f v( _coordinates[ 0 ], _coordinates[ 1 ], 1 );
const auto& p = aMat.mat( ) * v;
_coordinates[ 0 ] = p( 0 );
_coordinates[ 1 ] = p( 1 );
return *this;
}
ValueType magnitude( ) const {
const auto ret = std::sqrt( squarMagnitude( ) );
return ret;
}
ValueType squarMagnitude( ) const {
const auto x = _coordinates[ 0 ];
const auto y = _coordinates[ 1 ];
const auto ret = x*x + y*y;
return ret;
}
SelfType& operator+=( SelfType const& aOther ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x + aOther._coordinates[ 0 ];
y = y + aOther._coordinates[ 1 ];
return *this;
}
SelfType& operator-=( SelfType const& aOther ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x - aOther._coordinates[ 0 ];
y = y - aOther._coordinates[ 1 ];
return *this;
}
SelfType& operator+=( const ValueType aFactor ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x + aFactor;
y = y + aFactor;
return *this;
}
SelfType& operator-=( const ValueType aFactor ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x - aFactor;
y = y - aFactor;
return *this;
}
SelfType& operator*=( const ValueType aFactor ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x * aFactor;
y = y * aFactor;
return *this;
}
// Factor should be greater than 0
SelfType& operator/=( const ValueType aFactor ) {
auto& x = _coordinates[ 0 ];
auto& y = _coordinates[ 1 ];
x = x / aFactor;
y = y / aFactor;
return *this;
}
};
}
}
// Adapting the custom Point2D Class
namespace boost {
namespace geometry {
namespace traits {
// Adapt Point2D to Boost.Geometry
template<>
struct tag < ::blib::geometry::Point2D > {
typedef point_tag type;
};
template<>
struct coordinate_type < ::blib::geometry::Point2D > {
typedef ::blib::geometry::Point2D::ValueType type;
};
template<>
struct coordinate_system < ::blib::geometry::Point2D > {
typedef cs::cartesian type;
};
template<>
struct dimension<::blib::geometry::Point2D >
: ::blib::geometry::Point2D::Dimensions
{};
template<>
struct access < ::blib::geometry::Point2D, 0 > {
static inline ::blib::geometry::CoordinateType get( ::blib::geometry::Point2D const& p ) {
return p.x( );
}
static inline void set( ::blib::geometry::Point2D & p,
::blib::geometry::CoordinateType const& value ) {
p.x( value );
}
};
template<>
struct access < ::blib::geometry::Point2D, 1 > {
static inline ::blib::geometry::CoordinateType get( ::blib::geometry::Point2D const& p ) {
return p.y( );
}
static inline void set( ::blib::geometry::Point2D & p,
::blib::geometry::CoordinateType const& value ) {
p.y( value );
}
};
}
}
} // namespace boost::geometry::traits
// Overloading the operators
blib::geometry::Point2D operator*( blib::geometry::TransformMatrix & aTransformMatrix, blib::geometry::Point2D& aPoint ) {
const auto p = aTransformMatrix.mat( ) * aPoint.vec3f( );
blib::geometry::Point2D point( p( 0 ), p( 1 ) );
}
#endif // BLIB_POINT_HPP