1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 // Authors:
41 //  * Ozan Tonkal, ozantonkal@gmail.com
42 //  * Anatoly Baksheev, Itseez Inc.  myname.mysurname <> mycompany.com
43 //
44 //M*/
45 
46 #ifndef OPENCV_VIZ_VIZ3D_HPP
47 #define OPENCV_VIZ_VIZ3D_HPP
48 
49 #if !defined YES_I_AGREE_THAT_VIZ_API_IS_NOT_STABLE_NOW_AND_BINARY_COMPARTIBILITY_WONT_BE_SUPPORTED && !defined CVAPI_EXPORTS
50     //#error "Viz is in beta state now. Please define macro above to use it"
51 #endif
52 
53 #include <opencv2/core.hpp>
54 #include <opencv2/viz/types.hpp>
55 #include <opencv2/viz/widgets.hpp>
56 
57 namespace cv
58 {
59     namespace viz
60     {
61 
62 //! @addtogroup viz
63 //! @{
64 
65         /** @brief The Viz3d class represents a 3D visualizer window. This class is implicitly shared.
66         */
67         class CV_EXPORTS Viz3d
68         {
69         public:
70             typedef cv::viz::Color Color;
71             typedef void (*KeyboardCallback)(const KeyboardEvent&, void*);
72             typedef void (*MouseCallback)(const MouseEvent&, void*);
73 
74             /** @brief The constructors.
75 
76             @param window_name Name of the window.
77              */
78             Viz3d(const String& window_name = String());
79             Viz3d(const Viz3d&);
80             Viz3d& operator=(const Viz3d&);
81             ~Viz3d();
82 
83             /** @brief Shows a widget in the window.
84 
85             @param id A unique id for the widget. @param widget The widget to be displayed in the window.
86             @param pose Pose of the widget.
87              */
88             void showWidget(const String &id, const Widget &widget, const Affine3d &pose = Affine3d::Identity());
89 
90             /** @brief Removes a widget from the window.
91 
92             @param id The id of the widget that will be removed.
93              */
94             void removeWidget(const String &id);
95 
96             /** @brief Retrieves a widget from the window.
97 
98             A widget is implicitly shared; that is, if the returned widget is modified, the changes
99             will be immediately visible in the window.
100 
101             @param id The id of the widget that will be returned.
102              */
103             Widget getWidget(const String &id) const;
104 
105             /** @brief Removes all widgets from the window.
106             */
107             void removeAllWidgets();
108 
109             /** @brief Removed all widgets and displays image scaled to whole window area.
110 
111             @param image Image to be displayed.
112             @param window_size Size of Viz3d window. Default value means no change.
113              */
114             void showImage(InputArray image, const Size& window_size = Size(-1, -1));
115 
116             /** @brief Sets pose of a widget in the window.
117 
118             @param id The id of the widget whose pose will be set. @param pose The new pose of the widget.
119              */
120             void setWidgetPose(const String &id, const Affine3d &pose);
121 
122             /** @brief Updates pose of a widget in the window by pre-multiplying its current pose.
123 
124             @param id The id of the widget whose pose will be updated. @param pose The pose that the current
125             pose of the widget will be pre-multiplied by.
126              */
127             void updateWidgetPose(const String &id, const Affine3d &pose);
128 
129             /** @brief Returns the current pose of a widget in the window.
130 
131             @param id The id of the widget whose pose will be returned.
132              */
133             Affine3d getWidgetPose(const String &id) const;
134 
135             /** @brief Sets the intrinsic parameters of the viewer using Camera.
136 
137             @param camera Camera object wrapping intrinsic parameters.
138              */
139             void setCamera(const Camera &camera);
140 
141             /** @brief Returns a camera object that contains intrinsic parameters of the current viewer.
142             */
143             Camera getCamera() const;
144 
145             /** @brief Returns the current pose of the viewer.
146             */
147             Affine3d getViewerPose() const;
148 
149             /** @brief Sets pose of the viewer.
150 
151             @param pose The new pose of the viewer.
152              */
153             void setViewerPose(const Affine3d &pose);
154 
155             /** @brief Resets camera viewpoint to a 3D widget in the scene.
156 
157             @param id Id of a 3D widget.
158              */
159             void resetCameraViewpoint(const String &id);
160 
161             /** @brief Resets camera.
162             */
163             void resetCamera();
164 
165             /** @brief Transforms a point in world coordinate system to window coordinate system.
166 
167             @param pt Point in world coordinate system.
168             @param window_coord Output point in window coordinate system.
169              */
170             void convertToWindowCoordinates(const Point3d &pt, Point3d &window_coord);
171 
172             /** @brief Transforms a point in window coordinate system to a 3D ray in world coordinate system.
173 
174             @param window_coord Point in window coordinate system. @param origin Output origin of the ray.
175             @param direction Output direction of the ray.
176              */
177             void converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction);
178 
179             /** @brief Returns the current size of the window.
180             */
181             Size getWindowSize() const;
182             /** @brief Sets the size of the window.
183 
184             @param window_size New size of the window.
185              */
186             void setWindowSize(const Size &window_size);
187 
188             /** @brief Returns the name of the window which has been set in the constructor.
189              *  `Viz - ` is prepended to the name if necessary.
190              */
191             String getWindowName() const;
192 
193             /** @brief Returns the Mat screenshot of the current scene.
194             */
195             cv::Mat getScreenshot() const;
196 
197             /** @brief Saves screenshot of the current scene.
198 
199             @param file Name of the file.
200              */
201             void saveScreenshot(const String &file);
202 
203             /** @brief Sets the position of the window in the screen.
204 
205             @param window_position coordinates of the window
206              */
207             void setWindowPosition(const Point& window_position);
208 
209             /** @brief Sets or unsets full-screen rendering mode.
210 
211             @param mode If true, window will use full-screen mode.
212              */
213             void setFullScreen(bool mode = true);
214 
215             /** @brief Sets background color.
216             */
217             void setBackgroundColor(const Color& color = Color::black(), const Color& color2 = Color::not_set());
218             void setBackgroundTexture(InputArray image = noArray());
219             void setBackgroundMeshLab();
220 
221             /** @brief The window renders and starts the event loop.
222             */
223             void spin();
224 
225             /** @brief Starts the event loop for a given time.
226 
227             @param time Amount of time in milliseconds for the event loop to keep running.
228             @param force_redraw If true, window renders.
229              */
230             void spinOnce(int time = 1, bool force_redraw = false);
231 
232             /** @brief Create a window in memory instead of on the screen.
233              */
234             void setOffScreenRendering();
235 
236             /** @brief Remove all lights from the current scene.
237             */
238             void removeAllLights();
239 
240             /** @brief Add a light in the scene.
241 
242             @param position The position of the light.
243             @param focalPoint The point at which the light is shining
244             @param color The color of the light
245             @param diffuseColor The diffuse color of the light
246             @param ambientColor The ambient color of the light
247             @param specularColor The specular color of the light
248              */
249             void addLight(const Vec3d &position, const Vec3d &focalPoint = Vec3d(0, 0, 0), const Color &color = Color::white(),
250                           const Color &diffuseColor = Color::white(), const Color &ambientColor = Color::black(),
251                           const Color &specularColor = Color::white());
252 
253             /** @brief Returns whether the event loop has been stopped.
254             */
255             bool wasStopped() const;
256             void close();
257 
258             /** @brief Sets keyboard handler.
259 
260             @param callback Keyboard callback (void (\*KeyboardCallbackFunction(const
261             KeyboardEvent&, void\*)).
262             @param cookie The optional parameter passed to the callback.
263              */
264             void registerKeyboardCallback(KeyboardCallback callback, void* cookie = 0);
265 
266             /** @brief Sets mouse handler.
267 
268             @param callback Mouse callback (void (\*MouseCallback)(const MouseEvent&, void\*)).
269             @param cookie The optional parameter passed to the callback.
270              */
271             void registerMouseCallback(MouseCallback callback, void* cookie = 0);
272 
273             /** @brief Sets rendering property of a widget.
274 
275             @param id Id of the widget.
276             @param property Property that will be modified.
277             @param value The new value of the property.
278 
279             Rendering property can be one of the following:
280             -   **POINT_SIZE**
281             -   **OPACITY**
282             -   **LINE_WIDTH**
283             -   **FONT_SIZE**
284 
285             REPRESENTATION: Expected values are
286             -   **REPRESENTATION_POINTS**
287             -   **REPRESENTATION_WIREFRAME**
288             -   **REPRESENTATION_SURFACE**
289 
290             IMMEDIATE_RENDERING:
291             -   Turn on immediate rendering by setting the value to 1.
292             -   Turn off immediate rendering by setting the value to 0.
293 
294             SHADING: Expected values are
295             -   **SHADING_FLAT**
296             -   **SHADING_GOURAUD**
297             -   **SHADING_PHONG**
298              */
299             void setRenderingProperty(const String &id, int property, double value);
300             /** @brief Returns rendering property of a widget.
301 
302             @param id Id of the widget.
303             @param property Property.
304 
305             Rendering property can be one of the following:
306             -   **POINT_SIZE**
307             -   **OPACITY**
308             -   **LINE_WIDTH**
309             -   **FONT_SIZE**
310 
311             REPRESENTATION: Expected values are
312             -   **REPRESENTATION_POINTS**
313             -   **REPRESENTATION_WIREFRAME**
314             -   **REPRESENTATION_SURFACE**
315 
316             IMMEDIATE_RENDERING:
317             -   Turn on immediate rendering by setting the value to 1.
318             -   Turn off immediate rendering by setting the value to 0.
319 
320             SHADING: Expected values are
321             -   **SHADING_FLAT**
322             -   **SHADING_GOURAUD**
323             -   **SHADING_PHONG**
324              */
325             double getRenderingProperty(const String &id, int property);
326 
327             /** @brief Sets geometry representation of the widgets to surface, wireframe or points.
328 
329             @param representation Geometry representation which can be one of the following:
330             -   **REPRESENTATION_POINTS**
331             -   **REPRESENTATION_WIREFRAME**
332             -   **REPRESENTATION_SURFACE**
333              */
334             void setRepresentation(int representation);
335 
336             void setGlobalWarnings(bool enabled = false);
337         private:
338 
339             struct VizImpl;
340             VizImpl* impl_;
341 
342             void create(const String &window_name);
343             void release();
344 
345             friend class VizStorage;
346         };
347 
348 //! @}
349 
350     } /* namespace viz */
351 } /* namespace cv */
352 
353 #endif
354