1 /***********************************************************************
2 CalibrateProjector - Utility to calculate the calibration transformation
3 of a projector into a Kinect-captured 3D space.
4 Copyright (c) 2012-2018 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 CALIBRATEPROJECTOR_INCLUDED
24 #define CALIBRATEPROJECTOR_INCLUDED
27 #include <Threads/TripleBuffer.h>
28 #include <Math/Matrix.h>
29 #include <Geometry/Point.h>
30 #include <Geometry/Vector.h>
31 #include <Geometry/Plane.h>
32 #include <Geometry/Box.h>
33 #include <Geometry/OrthonormalTransformation.h>
34 #include <Vrui/Application.h>
35 #include <Vrui/Tool.h>
36 #include <Vrui/GenericToolFactory.h>
37 #include <Kinect/Config.h>
38 #include <Kinect/ProjectorType.h>
39 #include <Kinect/ProjectorHeader.h>
40 #include <Kinect/DiskExtractor.h>
42 /* Forward declarations: */
46 class DirectFrameSource
;
49 class CalibrateProjector
: public Vrui::Application
{
50 /* Embedded classes: */
52 typedef Kinect::DiskExtractor::Scalar Scalar
; // Scalar type
53 typedef Kinect::DiskExtractor::Point OPoint
; // Type for 3D points in object (camera) space
54 typedef Kinect::DiskExtractor::Vector Vector
; // Type for 3D vectors
55 typedef Geometry::Point
<Scalar
, 2> PPoint
; // Type for 2D points in projection space
56 typedef Geometry::Plane
<Scalar
, 3> OPlane
; // Type for planes
57 typedef Geometry::Box
<Scalar
, 3> Box
; // Type for bounding boxes
58 typedef Geometry::OrthonormalTransformation
<Scalar
, 3>
59 ONTransform
; // Type for rigid body transformations
61 struct TiePoint
{ // Tie point between 3D object space and 2D projector space
64 PPoint p
; // Projection-space point
65 OPoint o
; // Object-space point
69 typedef Vrui::GenericToolFactory
<CaptureTool
>
70 CaptureToolFactory
; // Tool class uses the generic factory class
72 class CaptureTool
: public Vrui::Tool
, public Vrui::Application::Tool
<CalibrateProjector
> {
73 friend class Vrui::GenericToolFactory
<CaptureTool
>;
77 static CaptureToolFactory
* factory
; // Pointer to the factory object for this class
79 /* Constructors and destructors: */
81 CaptureTool(const Vrui::ToolFactory
* factory
, const Vrui::ToolInputAssignment
& inputAssignment
);
82 virtual ~CaptureTool(void);
84 /* Methods from class Vrui::Tool: */
85 virtual const Vrui::ToolFactory
* getFactory(void) const;
86 virtual void buttonCallback(int buttonSlotIndex
, Vrui::InputDevice::ButtonCallbackData
* cbData
);
91 int imageSize
[2]; // Size of projector image
92 int numTiePoints
[2]; // Number of tie points in x and y
93 OPlane basePlane
; // Base plane of the configured sandbox area
94 OPoint basePlaneCorners
[4]; // Corners of the configured sandbox area
96 boxTransform
; // Transformation from camera space to sandbox space (x along long sandbox axis, z up)
97 Box bbox
; // Bounding box around the sandbox area
98 unsigned int numTiePointFrames
; // Number of frames to capture per tie point
99 unsigned int numBackgroundFrames
; // Number of frames to capture for background removal
101 Kinect::FrameSource
* camera
; // 3D video source to calibrate
102 Kinect::DiskExtractor
* diskExtractor
; // Object to extract disk shapes from a 3D video stream
103 Kinect::ProjectorType
* projector
; // A projector to render the 3D video stream
104 bool capturingBackground
; // Flag if the 3D camera is currently capturing a background frame
105 bool capturingTiePoint
; // Flag whether the main thread is currently capturing a tie point
106 unsigned int numCaptureFrames
; // Number of background or tie point frames still to capture
108 Threads::TripleBuffer
<Kinect::DiskExtractor::DiskList
>
109 diskList
; // Triple buffer of lists of extracted disks
110 std::vector
<TiePoint
> tiePoints
; // List of collected calibration tie points
111 int tiePointIndex
; // Index of the next tie point to be collected
112 bool haveProjection
; // Flag if a projection matrix has been computed
113 Math::Matrix projection
; // The current projection matrix
115 std::string projectionMatrixFileName
; // Name of the file to which the projection matrix is saved
117 /* Private methods: */
118 void depthStreamingCallback(const Kinect::FrameBuffer
&
119 frameBuffer
); // Callback receiving depth frames from the 3D camera
120 #if !KINECT_CONFIG_USE_SHADERPROJECTOR
121 void meshStreamingCallback(const Kinect::MeshBuffer
&
122 meshBuffer
); // Callback receiving projected meshes from the 3D video projector
124 void backgroundCaptureCompleteCallback(Kinect::DirectFrameSource
&
125 camera
); // Callback when the 3D camera is done capturing a background image
126 void diskExtractionCallback(const Kinect::DiskExtractor::DiskList
&
127 disks
); // Called when a new list of disks has been extracted
129 /* Constructors and destructors: */
131 CalibrateProjector(int& argc
, char**& argv
);
132 virtual ~CalibrateProjector(void);
134 /* Methods from Vrui::Application: */
135 virtual void frame(void);
136 virtual void display(GLContextData
& contextData
) const;
139 void startBackgroundCapture(void); // Starts capturing a background frame
140 void startTiePointCapture(void); // Starts capturing an averaged depth frame
141 void calcCalibration(
142 void); // Calculates the calibration transformation after all tie points have been collected