-
Notifications
You must be signed in to change notification settings - Fork 0
/
blPoint2dOperations.hpp
322 lines (254 loc) · 9.96 KB
/
blPoint2dOperations.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#ifndef BL_POINT2DOPERATIONS_HPP
#define BL_POINT2DOPERATIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: Useful functions for handling 2d points
///
/// AUTHOR: Vincenzo Barbato
///
/// NOTE: All things in this library are defined within the
/// blMathAPI namespace
///
/// LISENSE: MIT-LICENCE
/// http://www.opensource.org/licenses/mit-license.php
///
///
///
///-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include "blNumericFunctions.hpp"
#include "blPoint2d.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blMathAPI namespace
//-------------------------------------------------------------------
namespace blMathAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the L1-norm of a point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType norm1(const blPoint2d<blNumberType>& p)
{
return ( blMathAPI::abs(p.x()) + blMathAPI::abs(p.y()) );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the L2-norm of a point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType norm2(const blPoint2d<blNumberType>& p)
{
return std::sqrt( p.x()*p.x() + p.y()*p.y() );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the Linf-norm of a point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType normInf(const blPoint2d<blNumberType>& p)
{
return std::max(p.x(),p.y());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the Lp-norm of a point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType normP(const blPoint2d<blNumberType>& point,const int& p)
{
return blMathAPI::power(blMathAPI::power(point.x(),p) + blMathAPI::power(point.y(),p),
1.0 / double(p));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to normalize a point or to get
// a normalized point from a given point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blPoint2d<blNumberType>& normalize(blPoint2d<blNumberType>& p)
{
p /= norm2(p);
return p;
}
template<typename blNumberType>
inline blPoint2d<blNumberType> getNormalized(const blPoint2d<blNumberType>& p)
{
return (p/norm2(p));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to get a point perpendicular
// to a given point such that the dot product
// would give zero
//-------------------------------------------------------------------
template<typename blNumberType>
inline blPoint2d<blNumberType> getPerpendicularUnitPoint(const blPoint2d<blNumberType>& p)
{
// Find a component of this
// Point that is non zero
// and if unsuccessfull,
// just return the zero Point
//
// P1*P2 = x1*x2 + y1*y2 = 0
if(p.x() != 0)
{
if(p.y() == 0)
return blPoint2d<blNumberType>(0,1);
else
{
blPoint2d<blNumberType> point(1,-p.x()/p.y());
normalize(point);
return point;
}
}
else if(p.y() != 0)
{
return blPoint2d<blNumberType>(1,0);
}
// The Point was a zero Point,
// therefore any Point would
// be perpendicular, thus we
// are just going to return
// the zero Point
return blPoint2d<blNumberType>(0,0);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to return a point
// whose components are fractional
// inverses of the original point
//-------------------------------------------------------------------
template<typename blNumberType>
inline blPoint2d<blNumberType> getPointWithFractionalInvertedElements(const blPoint2d<blNumberType>& p)
{
return blPoint2d<blNumberType>(blNumberType(1)/p.x(),blNumberType(1)/p.y());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// output a point to
// an output stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::ostream& operator<<(std::ostream& os,
const blPoint2d<blNumberType>& p)
{
// We simply output the x and y
// components with a space
// separating them
os << p.x() << " " << p.y();
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// input a point from
// an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::istream& operator>>(std::istream& is,
blPoint2d<blNumberType>& p)
{
if(!(is >> p.x()))
{
// Error -- Cound not read
// the x value
return is;
}
if(!(is >> p.y()))
{
// Error -- Cound not read
// the y value
return is;
}
return is;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to return a point
// with the minimum or maximum values
// of each component when comparing
// multiple points
//-------------------------------------------------------------------
template<typename blNumberType>
inline void min(const blPoint2d<blNumberType>& p1,
const blPoint2d<blNumberType>& p2,
blPoint2d<blNumberType>& minP)
{
minP.x = std::min(p1.x(),p2.x());
minP.y = std::min(p1.y(),p2.y());
}
template<typename blNumberType>
inline void max(const blPoint2d<blNumberType>& p1,
const blPoint2d<blNumberType>& p2,
blPoint2d<blNumberType>& maxP)
{
maxP.x = std::max(p1.x(),p2.x());
maxP.y = std::max(p1.y(),p2.y());
}
template<typename blNumberType>
inline blPoint2d<blNumberType> min(const blPoint2d<blNumberType>& p1,
const blPoint2d<blNumberType>& p2)
{
blPoint2d<blNumberType> minP;
blMathAPI::min(p1,p2,minP);
return minP;
}
template<typename blNumberType>
inline blPoint2d<blNumberType> max(const blPoint2d<blNumberType>& p1,
const blPoint2d<blNumberType>& p2)
{
blPoint2d<blNumberType> maxP;
blMathAPI::max(p1,p2,maxP);
return maxP;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// convert a generic
// string to a point
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType convertStringToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& decimalPointDelimiter,
blPoint2d<blNumberType>& convertedNumber,
const int& numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter)
{
// We expect the
// following format:
// P = x y
blStringIteratorType newStringPosition;
// First we try to
// get the x value
newStringPosition = convertStringToNumber(beginIter,
endIter,
decimalPointDelimiter,
convertedNumber.x(),
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the y value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber.y(),
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
return newStringPosition;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_POINT2DOPERATIONS_HPP