00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef LOCATION_H
00011 #define LOCATION_H
00012
00013 #include <math.h>
00014 #include <utility>
00015 #include "AppAssert.h"
00016 #include "AppConfig.h"
00017
00018
00019
00020
00021 namespace Opticks
00022 {
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 template<typename T, int SZ>
00041 class Location
00042 {
00043 public:
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 Location(T x, T y) : mX(x), mY(y), mZ(0)
00054 {
00055 ENSURE(SZ == 2);
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template<typename U>
00067 Location(std::pair<U, U> point) : mX(point.first), mY(point.second), mZ(0)
00068 {
00069 ENSURE(SZ == 2);
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 Location(T x, T y, T z): mX(x), mY(y), mZ(z)
00084 {
00085 ENSURE(SZ == 3);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 Location(const Location<T,SZ> &point): mX(point.mX), mY(point.mY), mZ(point.mZ) {}
00098
00099
00100
00101
00102
00103
00104
00105 Location(): mX(0), mY(0), mZ(0) {}
00106
00107
00108
00109
00110 ~Location() {}
00111
00112
00113
00114
00115
00116
00117 static int dimensionality() { return SZ; }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 double length() const
00128 {
00129 if(SZ == 2)
00130 {
00131 return sqrt(static_cast<double>(mX * mX) + mY * mY);
00132 }
00133 else if(SZ == 3)
00134 {
00135 return sqrt(static_cast<double>(mX * mX) + mY * mY + mZ * mZ);
00136 }
00137 return 0.0;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 Location<T,SZ> operator+(const Location<T,SZ> &point) const
00151 {
00152 if(SZ == 2)
00153 {
00154 return Location<T,SZ>(mX + point.mX, mY + point.mY);
00155 }
00156 else if(SZ == 3)
00157 {
00158 return Location<T,SZ>(mX + point.mX, mY + point.mY, mZ + point.mZ);
00159 }
00160 return Location<T,SZ>();
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 Location<T,SZ> operator+(T scalar) const
00172 {
00173 if(SZ == 2)
00174 {
00175 return Location<T,SZ>(mX + scalar, mY + scalar);
00176 }
00177 else if(SZ == 3)
00178 {
00179 return Location<T,SZ>(mX + scalar, mY + scalar, mZ + scalar);
00180 }
00181 return Location<T,SZ>();
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 Location<T,SZ> &operator+=(const Location<T,SZ> &point)
00193 {
00194 mX += point.mX;
00195 mY += point.mY;
00196 if(SZ == 3)
00197 {
00198 mZ += point.mZ;
00199 }
00200
00201 return *this;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 Location<T,SZ> operator-(const Location<T,SZ> &point) const
00213 {
00214 if(SZ == 2)
00215 {
00216 return Location<T,SZ>(mX - point.mX, mY - point.mY);
00217 }
00218 else if(SZ == 3)
00219 {
00220 return Location<T,SZ>(mX - point.mX, mY - point.mY, mZ - point.mZ);
00221 }
00222 return Location<T,SZ>();
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 Location<T,SZ> operator-(T scalar) const
00234 {
00235 if(SZ == 2)
00236 {
00237 return Location<T,SZ>(mX - scalar, mY - scalar);
00238 }
00239 else if(SZ == 3)
00240 {
00241 return Location<T,SZ>(mX - scalar, mY - scalar, mZ - scalar);
00242 }
00243 return Location<T,SZ>();
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 Location<T,SZ> &operator-=(const Location<T,SZ> &point)
00255 {
00256 mX -= point.mX;
00257 mY -= point.mY;
00258 if(SZ == 3)
00259 {
00260 mZ -= point.mZ;
00261 }
00262
00263 return *this;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 Location<T,SZ> operator*(T scalar) const
00275 {
00276 if(SZ == 2)
00277 {
00278 return Location<T,SZ>(mX * scalar, mY * scalar);
00279 }
00280 if(SZ == 3)
00281 {
00282 return Location<T,SZ>(mX * scalar, mY * scalar, mZ * scalar);
00283 }
00284 return Location<T,SZ>();
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 BROKEN_INLINE_HINT Location<T,SZ> &operator=(const Location<T,SZ> &point)
00296 {
00297 if(this != &point)
00298 {
00299 mX = point.mX;
00300 mY = point.mY;
00301 mZ = point.mZ;
00302 }
00303
00304 return *this;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 bool operator==(const Location<T,SZ> &point) const
00316 {
00317 return (mX == point.mX) && (mY == point.mY) && (SZ == 2 || (mZ == point.mZ));
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 bool operator!=(const Location<T,SZ> &point) const
00329 {
00330 return !(*this == point);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 bool operator<(const Location<T,SZ> &point) const
00342 {
00343 return (mX < point.mX) && (mY < point.mY) && (SZ == 2 || mZ < point.mZ);
00344 }
00345
00346
00347
00348
00349
00350
00351
00352 void clampMinimum(const Location<T,SZ> &point)
00353 {
00354 mX = std::max(mX, point.mX);
00355 mY = std::max(mY, point.mY);
00356 if(SZ == 3)
00357 {
00358 mZ = std::max(mZ, point.mZ);
00359 }
00360 }
00361
00362
00363
00364
00365
00366
00367
00368 void clampMaximum(const Location<T,SZ> &point)
00369 {
00370 mX = std::min(mX, point.mX);
00371 mY = std::min(mY, point.mY);
00372 if(SZ == 3)
00373 {
00374 mZ = std::min(mZ, point.mZ);
00375 }
00376 }
00377
00378 T mX;
00379 T mY;
00380 T mZ;
00381 };
00382
00383
00384
00385
00386 typedef Location<int,2> PixelLocation;
00387
00388
00389
00390
00391 typedef Location<int,2> PixelOffset;
00392
00393 }
00394
00395 #endif