GPU-Accelerated Coverage  0.1.0
Compute coverage tours for known environment with articulated objects on GPU
Image.h
1 /*
2  * Copyright (c) 2018, Stefan Osswald
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 notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef INCLUDE_ARTICULATION_IMAGE_H_
32 #define INCLUDE_ARTICULATION_IMAGE_H_
33 
34 #include <assimp/scene.h>
35 #include <stdexcept>
36 #include <cstdlib>
37 
38 #if HAS_OPENCV
39 #include <opencv2/core/core.hpp>
40 #else
41 // Define dummy classes for systems without OpenGL (e.g. Android)
43 #define CV_8UC3 16
44 #define CV_8UC4 24
45 #define GL_BGR GL_RGB
46 #define GL_BGRA GL_RGBA
47 namespace cv {
48  struct Mat {
49  int rows;
50  int cols;
51  int type;
52  int nchannels;
53  unsigned char *data;
54  Mat() : rows(0), cols(0), type(0), data(NULL) {}
55  Mat(const int rows, const int cols, const int type, const void * const data) : rows(rows), cols(cols), type(type), data(NULL), allocatedData(false) {
56  switch (type) {
57  case CV_8UC3: nchannels = 3; break;
58  case CV_8UC4: nchannels = 4; break;
59  default: throw std::invalid_argument("Unknown type");
60  }
61  this->data = (unsigned char *) data;
62  }
63  Mat(const int rows, const int cols, const int type) : rows(rows), cols(cols), type(type), data(NULL), allocatedData(true) {
64  switch (type) {
65  case CV_8UC3: nchannels = 3; break;
66  case CV_8UC4: nchannels = 4; break;
67  default: throw std::invalid_argument("Unknown type");
68  }
69  data = (unsigned char *) calloc(rows * cols * nchannels, sizeof(unsigned char));
70  }
71  Mat(const Mat& other) : data(NULL), allocatedData(true) {
72  rows = other.rows;
73  cols = other.cols;
74  type = other.type;
75  nchannels = other.nchannels;
76  data = (unsigned char *) calloc(rows * cols * nchannels, sizeof(unsigned char));
77  memcpy(data, other.data, rows * cols * nchannels);
78  }
79  ~Mat() {
80  if (allocatedData) {
81  //TODO free leads to SIGSEGV
82  //free(data);
83  }
84  data = NULL;
85  }
86  inline const int& channels() const {
87  return nchannels;
88  }
89  private:
90  bool allocatedData;
91  };
92 }
94 #endif
95 #include GL_INCLUDE
96 
97 namespace gpu_coverage {
98 
102 class Image {
103 public:
111  Image(const aiTexture * const texture, const size_t& id);
112 
116  virtual ~Image();
117 
132  static const Image * get(const std::string& path);
133 
143  static void release(const Image * image);
144 
150  inline bool inUse() const {
151  return usage > 0;
152  }
153 
158  void bindToUnit(const GLenum unit) const;
159 
164  const cv::Mat& image() const {
165  return mat;
166  }
167 
168 protected:
175  Image(const std::string& path);
176 
184  Image(const int rows, const int cols, const int type, const unsigned char * const data = NULL);
185 
186  const size_t id;
187  cv::Mat mat;
188  mutable size_t usage;
189  const std::string path;
190 };
191 
192 } /* namespace gpu_coverage */
193 
194 #endif /* INCLUDE_ARTICULATION_IMAGE_H_ */
Represents a 2D image.
Definition: Image.h:102
bool inUse() const
Returns true if the image is in use.
Definition: Image.h:150
const cv::Mat & image() const
Returns this image as an OpenCV image.
Definition: Image.h:164
const size_t id
Unique ID of this image.
Definition: Image.h:186
Definition: AbstractCamera.h:41
const std::string path
File path from where the image was loaded.
Definition: Image.h:189
size_t usage
Internal usage counter, see get(), release() and inUse().
Definition: Image.h:188
cv::Mat mat
OpenCV image.
Definition: Image.h:187