SMIL  1.0.4
DCommon.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _DCOMMON_H
31 #define _DCOMMON_H
32 
33 #include <cstring>
34 #include <memory>
35 #include <limits>
36 #include <vector>
37 #include <map>
38 #include <cmath>
39 #include <cstdarg>
40 #include <iostream>
41 
42 #include "private/DTypes.hpp"
43 
44 using namespace std;
45 
46 namespace smil
47 {
56 #define VERBOSE 1
57 
58 #if VERBOSE > 1
59 #define MESSAGE(msg) cout << msg << endl;
60 #else // VERBOSE
61 #define MESSAGE(msg)
62 #endif // VERBOSE
63 
64 #if defined __GNUC__ || defined __clang__
65 #define SMIL_UNUSED __attribute__((__unused__))
66 #else // MSVC et al.
67 #define SMIL_UNUSED
68 #endif
69 
70 #define INLINE inline
71 
72  // Generate template specializations (or instanciations?) for
73  // ImageHandler subclasses (in IO/DImageIO_{BMP,JPG,PBM,PNG,TIFF})
74 #define IMAGEFILEHANDLER_TEMP_SPEC(FORMAT, PIXELTYPE) \
75  template <> \
76  class FORMAT##ImageFileHandler<PIXELTYPE> \
77  : public ImageFileHandler<PIXELTYPE> \
78  { \
79  public: \
80  FORMAT##ImageFileHandler() : ImageFileHandler<PIXELTYPE>(#FORMAT) \
81  { \
82  } \
83  RES_T read(const char *filename, Image<PIXELTYPE> &image); \
84  RES_T write(const Image<PIXELTYPE> &image, const char *filename); \
85  };
86 
87 #define SMART_POINTER(T) boost::shared_ptr<T>
88 #define SMART_IMAGE(T) SMART_POINTER(D_Image<T>)
89 
90 #define D_DEFAULT_IMAGE_WIDTH 512
91 #define D_DEFAULT_IMAGE_HEIGHT 512
92 #define D_DEFAULT_IMAGE_DEPTH 1
93 
94 #define D_DEFAULT_OUT_PIXEL_VAL 0
95 
96 #ifndef PI
97 #define PI 3.141592653589793
98 #endif // PI
99 
100 #ifndef MIN
101 #define MIN(a, b) ((a) < (b) ? (a) : (b))
102 #endif // MIN
103 #ifndef MAX
104 #define MAX(a, b) ((a) > (b) ? (a) : (b))
105 #endif // MAX
106 #ifndef ABS
107 #define ABS(a) ((a) >= 0 ? (a) : -(a))
108 #endif // ABS
109 
110 #ifndef SWIG
112  template <typename Lhs, typename Rhs>
113  bool operator()(const Lhs &lhs, const Rhs &rhs) const
114  {
115  return lhs.second < rhs.second;
116  }
117  };
118 #endif // SWIG
119 
123  template <class T=int>
124  class Point
125  {
126  public:
127  T x;
128  T y;
129  T z;
130  public:
135  Point() : x(0), y(0), z(0)
136  {
137  }
138 
144  Point(const Point &pt) : x(pt.x), y(pt.y), z(pt.z)
145  {
146  }
147 
152  Point(T _x, T _y, T _z) : x(_x), y(_y), z(_z)
153  {
154  }
155 
160  Point(T _x, T _y) : x(_x), y(_y), z(0)
161  {
162  }
163 
168  bool operator==(const Point &p2)
169  {
170  return (x == p2.x && y == p2.y && z == p2.z);
171  }
172 
178  Point operator-(const Point &p2)
179  {
180  return Point(x - p2.x, y - p2.y, z - p2.z);
181  }
182 
188  Point operator+(const Point &p2)
189  {
190  return Point(x + p2.x, y + p2.y, z + p2.z);
191  }
192 
196  void printSelf(string indent = "")
197  {
198  cout << indent << " " << x << "\t" << y << "\t" << z << endl;
199  }
200  };
201 
207 
213 
219 
224  typedef vector<double> Vector_double;
225 
230  typedef vector<Vector_double> Matrix_double;
231 
236  typedef vector<UINT> Vector_UINT;
237 
242  typedef vector<size_t> Vector_size_t;
243 
248  typedef vector<off_t> Vector_off_t;
249 
253  struct Rectangle {
254  UINT x0, y0;
255  UINT xSize, ySize;
256  };
257 
267  class ImageBox {
268  public:
269  IntPoint pt;
270  off_t reference;
271  size_t width, height, depth;
272 
273  public:
283  ImageBox(size_t Size[3])
284  {
285  width = Size[0];
286  height = Size[1];
287  depth = Size[2];
288  pt.x = pt.y = pt.z = 0;
289  reference = 0;
290  }
291 
298  ImageBox(const ImageBox &box)
299  {
300  *this = box;
301  }
302 
307  ImageBox(size_t width, size_t height, size_t depth = 1)
308  {
309  this->width = width;
310  this->height = height;
311  this->depth = depth;
312  pt.x = pt.y = pt.z = 0;
313  reference = 0;
314  }
315 
320  void setReference(off_t x, off_t y, off_t z = 0)
321  {
322  this->pt.x = x;
323  this->pt.y = y;
324  this->pt.z = z;
325 
326  this->reference = x + y * width + z * width * height;
327  }
328 
334  {
335  this->pt = pt;
336  this->reference = pt.x + (pt.y + pt.z * width) * height;
337  }
338 
342  void setReference(off_t offset)
343  {
344  this->reference = offset;
345 
346  this->pt.x = offset % width;
347  offset = (offset - this->pt.x) / width;
348  this->pt.y = offset % height;
349  this->pt.z = (offset - this->pt.y) / height;
350  }
351 
356  {
357  return pt;
358  }
359 
363  off_t getOffset()
364  {
365  return reference;
366  }
367 
371  void shift(off_t dx, off_t dy, off_t dz = 0)
372  {
373  pt.x += dx;
374  pt.y += dy;
375  pt.z += dz;
376  this->reference = pt.x + (pt.y * width + pt.z * height) * width;
377  }
378 
382  void shift(IntPoint dp)
383  {
384  pt.x += dp.x;
385  pt.y += dp.y;
386  pt.z += dp.z;
387  this->reference = pt.x + (pt.y + pt.z * height) * width;
388  }
389 
390 
396  bool inImage()
397  {
398  return inImage(pt.x, pt.y, pt.z);
399  }
400 
407 #if 0
408  bool inImage(off_t x, off_t y, off_t z = 0)
409  {
410  return (x >= 0 && x < width && y >= 0 && y < height && z >= 0 &&
411  z < depth);
412  }
413 #else
414  bool inImage(size_t x, size_t y, size_t z = 0)
415  {
416  return (x < width && y < height && z < depth);
417  }
418 #endif
419 
427  {
428  return inImage(p.x, p.y, p.z);
429  }
430 
437  {
438  return p.x + width * (p.y + p.z * height);
439  }
440 
446  IntPoint getCoords(off_t off)
447  {
448  IntPoint p;
449  p.x = off % width;
450  off = (off - p.x) / width;
451  p.y = off % height;
452  p.z = (off - p.y) / height;
453 
454  return p;
455  }
456 
463  double getDistance(off_t pa, off_t pb)
464  {
465  IntPoint a = getCoords(pa);
466  IntPoint b = getCoords(pb);
467 
468  return getDistance(a, b);
469  }
470 
477  double getDistance(off_t p)
478  {
479  return getDistance(reference, p);
480  }
481 
489  {
490  return getDistance(pt, p);
491  }
492 
500  {
501  return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
502  }
503 
504  void printSelf()
505  {
506  cout << "ImageBox :" << endl;
507  cout << " Width :\t" << width << endl;
508  cout << " Height:\t" << height << endl;
509  cout << " Depth :\t" << depth << endl;
510  }
511  };
512 
513  // Misc Macros
515 #ifdef _MSC_VER
516 
517 #define __FUNC__ __FUNCTION__
518 
519 // Work-around to MSVC __VA_ARGS__ expanded as a single argument, instead of
520 // being broken down to multiple ones
521 #define EXPAND(...) __VA_ARGS__
522 
523 #define _GET_1ST_ARG(arg1, ...) arg1
524 #define _GET_2ND_ARG(arg1, arg2, ...) arg2
525 #define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
526 #define _GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
527 #define _GET_5TH_ARG(arg1, arg2, arg3, arg4, arg5, ...) arg5
528 #define _GET_6TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, ...) arg6
529 #define _GET_7TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, arg7, ...) arg7
530 #define _GET_8TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, ...) arg8
531 #define GET_1ST_ARG(...) EXPAND(_GET_1ST_ARG(__VA_ARGS__))
532 #define GET_2ND_ARG(...) EXPAND(_GET_2ND_ARG(__VA_ARGS__))
533 #define GET_3RD_ARG(...) EXPAND(_GET_3RD_ARG(__VA_ARGS__))
534 #define GET_4TH_ARG(...) EXPAND(_GET_4TH_ARG(__VA_ARGS__))
535 #define GET_5TH_ARG(...) EXPAND(_GET_5TH_ARG(__VA_ARGS__))
536 #define GET_6TH_ARG(...) EXPAND(_GET_6TH_ARG(__VA_ARGS__))
537 #define GET_7TH_ARG(...) EXPAND(_GET_7TH_ARG(__VA_ARGS__))
538 #define GET_8TH_ARG(...) EXPAND(_GET_8TH_ARG(__VA_ARGS__))
539 
540 #define _xPP_NARGS_IMPL(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
541  x13, x14, x15, N, ...) \
542  N
543 #define PP_NARGS(...) \
544  EXPAND(_xPP_NARGS_IMPL(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, \
545  4, 3, 2, 1, 0))
546 
547 #else // _MSC_VER
548 
549 #define __FUNC__ __func__
550 
551 #define GET_1ST_ARG(arg1, ...) arg1
552 #define GET_2ND_ARG(arg1, arg2, ...) arg2
553 #define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
554 #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
555 #define GET_5TH_ARG(arg1, arg2, arg3, arg4, arg5, ...) arg5
556 #define GET_6TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, ...) arg6
557 #define GET_7TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, arg7, ...) arg7
558 #define GET_8TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, ...) arg8
559 
560 #define _xPP_NARGS_IMPL(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
561  x13, x14, x15, N, ...) \
562  N
563 #define PP_NARGS(...) \
564  _xPP_NARGS_IMPL(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, \
565  1, 0)
566 
567 #endif // _MSC_VER
571 } // namespace smil
572 
573 #endif // _DCOMMON_H
ImageBox.
Definition: DCommon.h:267
void setReference(IntPoint pt)
setReference() - set reference point
Definition: DCommon.h:333
off_t getOffset(IntPoint p)
getOffset() - given the coordinates of a pixel in a image box, get its offset.
Definition: DCommon.h:436
void setReference(off_t x, off_t y, off_t z=0)
setReference() - set reference point
Definition: DCommon.h:320
ImageBox(size_t Size[3])
ImageBox - constructor.
Definition: DCommon.h:283
bool inImage(IntPoint p)
inImage() - given the coordinates of a pixel, as a point, check if its coordinates are inside the ima...
Definition: DCommon.h:426
off_t getOffset()
getOffset() - get coordinates as an reference
Definition: DCommon.h:363
IntPoint getCoords(off_t off)
getCoords() - given the offset of a pixel inside an image, returns its coordinates as a point
Definition: DCommon.h:446
ImageBox(size_t width, size_t height, size_t depth=1)
ImageBox - constructor.
Definition: DCommon.h:307
ImageBox(const ImageBox &box)
ImageBox - constructor.
Definition: DCommon.h:298
bool inImage(size_t x, size_t y, size_t z=0)
inImage() - given the coordinates of a pixel, check if it's inside image box
Definition: DCommon.h:414
bool inImage()
inImage() - check if the reference point is inside image bounds
Definition: DCommon.h:396
void shift(off_t dx, off_t dy, off_t dz=0)
shift() - move the point by some displacements
Definition: DCommon.h:371
double getDistance(off_t pa, off_t pb)
getDistance() - given the offset of two points inside the image box returns the Euclidean between the...
Definition: DCommon.h:463
double getDistance(off_t p)
getDistance() - given a point inside an image box returns the Euclidean between this point and the re...
Definition: DCommon.h:477
double getDistance(IntPoint a, IntPoint b)
getDistance() - given two points inside the image box returns the Euclidean between them.
Definition: DCommon.h:499
double getDistance(IntPoint p)
getDistance() - given a point inside an image box returns the Euclidean between this point and the re...
Definition: DCommon.h:488
void setReference(off_t offset)
setReference() - set reference point
Definition: DCommon.h:342
void shift(IntPoint dp)
shift() - move the point by some displacements given by a point
Definition: DCommon.h:382
IntPoint getPoint()
getPoint() - get coordinates as a point
Definition: DCommon.h:355
Struct Point.
Definition: DCommon.h:125
void printSelf(string indent="")
printSelf() - Print point coordinates
Definition: DCommon.h:196
Point(T _x, T _y, T _z)
3D Point Constructor
Definition: DCommon.h:152
Point(const Point &pt)
Constructor from another point.
Definition: DCommon.h:144
Point operator-(const Point &p2)
operator- - subtract two points
Definition: DCommon.h:178
bool operator==(const Point &p2)
operator== - comparison
Definition: DCommon.h:168
Point operator+(const Point &p2)
operator+ - add two points
Definition: DCommon.h:188
Point(T _x, T _y)
2D Point Constructor
Definition: DCommon.h:160
Point()
Contructor - an empty point.
Definition: DCommon.h:135
RES_T sqrt(const Image< T1 > &imIn, Image< T2 > &imOut)
sqrt() - square root of an image
Definition: DImageArith.hpp:926
RES_T pow(const Image< T1 > &imIn, Image< T2 > &imOut, double exponent=2)
pow() - power of an image
Definition: DImageArith.hpp:905
vector< size_t > Vector_size_t
Vector_size_t.
Definition: DCommon.h:242
Point< double > DoublePoint
DoublePoint.
Definition: DCommon.h:206
vector< UINT > Vector_UINT
Vector_UINT.
Definition: DCommon.h:236
vector< Vector_double > Matrix_double
Matrix_double.
Definition: DCommon.h:230
vector< off_t > Vector_off_t
Vector_off_t.
Definition: DCommon.h:248
vector< double > Vector_double
Vector_double.
Definition: DCommon.h:224
Point< UINT > UintPoint
UintPoint.
Definition: DCommon.h:218
Point< int > IntPoint
IntPoint.
Definition: DCommon.h:212
Rectangle.
Definition: DCommon.h:253
Definition: DCommon.h:111