temp commit
[SARndbox.git] / RainMaker.h
blobbf701349952a7cfdf830f6498fde50732cbcb221
1 /***********************************************************************
2 RainMaker - Class to detect objects moving through a given range of
3 depths in a depth image sequence to trigger rainfall on virtual terrain.
4 Copyright (c) 2012-2015 Oliver Kreylos
6 This file is part of the Augmented Reality Sandbox (SARndbox).
8 The Augmented Reality Sandbox is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The Augmented Reality Sandbox is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with the Augmented Reality Sandbox; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ***********************************************************************/
23 #ifndef RAINMAKER_INCLUDED
24 #define RAINMAKER_INCLUDED
26 #include <vector>
27 #include <Threads/Thread.h>
28 #include <Threads/MutexCond.h>
29 #include <Geometry/Point.h>
30 #include <Geometry/Matrix.h>
31 #include <Geometry/ProjectiveTransformation.h>
32 #include <Kinect/FrameBuffer.h>
34 /* Forward declarations: */
35 namespace Misc {
36 template <class ParameterParam>
37 class FunctionCall;
39 namespace Geometry {
40 template <class ScalarParam, int dimensionParam>
41 class Plane;
43 class ValidPixelProperty;
45 class RainMaker {
46 /* Embedded classes: */
47 public:
48 typedef unsigned short RawDepth; // Data type for raw depth values
49 typedef Geometry::Point<double, 3> Point;
50 typedef Geometry::Plane<double, 3> Plane;
51 typedef Geometry::ProjectiveTransformation<double, 3> PTransform;
53 struct Blob { // Structure to describe a detected object in camera space
54 /* Elements: */
55 public:
56 Point centroid; // Object's centroid in camera space
57 double radius; // Object's approximate radius in camera space
60 typedef std::vector<Blob> BlobList; // Type for lists of detected objects
61 typedef Misc::FunctionCall<const BlobList&>
62 OutputBlobsFunction; // Type for functions called when a new object list has been extracted
64 /* Elements: */
65 private:
66 unsigned int depthSize[2]; // Width and height of incoming depth frames
67 bool depthIsFloat; // Flag whether the incoming depth frames have float pixel values
68 unsigned int colorSize[2]; // Width and height of incoming color frames
69 PTransform depthProjection; // Projective transformation from depth image space to camera space
70 PTransform colorProjection; // Projective transformation from camera space to color image space
71 Geometry::Matrix<float, 3, 4>
72 colorDepthHomography; // Homography from 3D depth image space into 2D color image space
73 float minPlane[4]; // Plane equation of the lower bound of valid depth values in depth image space
74 float maxPlane[4]; // Plane equation of the upper bound of valid depth values in depth image space
75 int minBlobSize; // Minimum size of objects to be detected
76 Threads::MutexCond inputCond; // Condition variable to signal arrival of a new input frame
77 Kinect::FrameBuffer inputDepthFrame; // The most recent input depth frame
78 unsigned int inputDepthFrameVersion; // Version number of input depth frame
79 Kinect::FrameBuffer inputColorFrame; // The most recent input color frame
80 unsigned int inputColorFrameVersion; // Version number of input color frame
81 volatile bool runDetectionThread; // Flag to keep the background object detection thread running
82 Threads::Thread detectionThread; // The background object detection thread
83 OutputBlobsFunction*
84 outputBlobsFunction; // Function called when a new (potentially empty) object list has been extracted
86 /* Private methods: */
87 template <class DepthPixelParam>
88 void extractBlobs(const Kinect::FrameBuffer& depthFrame, const ValidPixelProperty& vpp,
89 BlobList& blobsCc);
90 void* detectionThreadMethod(void); // Method for the object detection thread
92 /* Constructors and destructors: */
93 public:
94 RainMaker(const unsigned int sDepthSize[2], const unsigned int sColorSize[2],
95 const PTransform& sDepthProjection, const PTransform& sColorProjection, const Plane& basePlane,
96 double minElevation, double maxElevation,
97 int sMinBlobSize); // Creates an object detector for frames of the given size and the given range of elevation values relative to the given base plane in camera space
98 ~RainMaker(void); // Destroys the object detector
100 /* Methods: */
101 void setDepthIsFloat(bool
102 newDepthIsFloat); // Sets whether incoming depth frames have float pixel values
103 void setOutputBlobsFunction(OutputBlobsFunction*
104 newOutputBlobsFunction); // Sets the output function; adopts given functor object
105 void receiveRawDepthFrame(const Kinect::FrameBuffer&
106 newDepthFrame); // Called to receive a new raw depth frame
107 void receiveRawColorFrame(const Kinect::FrameBuffer&
108 newColorFrame); // Called to receive a new raw color frame
111 #endif