1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include <iostream>
28 #include <thread>
29 #include <librealsense/rs.hpp>
30 #include <Core/Core.h>
31 #include <Visualization/Visualization.h>
32 
33 using namespace three;
34 
main(int argc,char ** args)35 int main(int argc, char **args)
36 {
37 	rs::context ctx;
38 	PrintInfo("There are %d connected RealSense devices.\n",
39 			ctx.get_device_count());
40 	if(ctx.get_device_count() == 0) {
41 		return 0;
42 	}
43 
44 	rs::device * dev = ctx.get_device(0);
45 	PrintInfo("Using device 0, an %s\n", dev->get_name());
46 	PrintInfo("    Serial number: %s\n", dev->get_serial());
47 	PrintInfo("    Firmware version: %s\n\n", dev->get_firmware_version());
48 
49 	dev->set_option(rs::option::color_enable_auto_exposure, 0.0);
50 	dev->set_option(rs::option::color_exposure, 625);
51 	dev->set_option(rs::option::color_gain, 128);
52 	dev->set_option(rs::option::color_enable_auto_white_balance, 0.0);
53 
54 	dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);
55 	dev->enable_stream(rs::stream::color, 1920, 1080, rs::format::rgb8, 30);
56 	dev->start();
57 	std::this_thread::sleep_for(std::chrono::milliseconds(50));
58 	dev->set_option(rs::option::color_white_balance, 2100.0);
59 
60 	auto depth_image_ptr = std::make_shared<Image>();
61 	depth_image_ptr->PrepareImage(640, 480, 1, 2);
62 	auto color_image_ptr = std::make_shared<Image>();
63 	color_image_ptr->PrepareImage(1920, 1080, 3, 1);
64 	FPSTimer timer("Realsense stream");
65 
66 	rs::extrinsics extrinsics = dev->get_extrinsics(rs::stream::depth,
67 			rs::stream::rectified_color);
68 	for (int i = 0; i < 9; i++) {
69 		PrintInfo("%.6f ", extrinsics.rotation[i]);
70 	}
71 	PrintInfo("\n");
72 	for (int i = 0; i < 3; i++) {
73 		PrintInfo("%.6f ", extrinsics.translation[i]);
74 	}
75 	PrintInfo("\n");
76 
77 	rs::intrinsics depth_intr = dev->get_stream_intrinsics(rs::stream::depth);
78 	PrintInfo("%d %d %.6f %.6f %.6f %.6f\n",
79 			depth_intr.width, depth_intr.height,
80 			depth_intr.fx, depth_intr.fy,
81 			depth_intr.ppx, depth_intr.ppy);
82 	for (int i = 0; i < 5; i++) {
83 		PrintInfo("%.6f ", depth_intr.coeffs[i]);
84 	}
85 	PrintInfo("\n\n");
86 
87 	rs::intrinsics color_intr = dev->get_stream_intrinsics(rs::stream::color);
88 	PrintInfo("%d %d %.6f %.6f %.6f %.6f\n",
89 			color_intr.width, color_intr.height,
90 			color_intr.fx, color_intr.fy,
91 			color_intr.ppx, color_intr.ppy);
92 	for (int i = 0; i < 5; i++) {
93 		PrintInfo("%.6f ", color_intr.coeffs[i]);
94 	}
95 	PrintInfo("\n\n");
96 
97 	rs::intrinsics rect_intr = dev->get_stream_intrinsics(
98 			rs::stream::rectified_color);
99 	PrintInfo("%d %d %.6f %.6f %.6f %.6f\n",
100 			rect_intr.width, rect_intr.height,
101 			rect_intr.fx, rect_intr.fy,
102 			rect_intr.ppx, rect_intr.ppy);
103 	for (int i = 0; i < 5; i++) {
104 		PrintInfo("%.6f ", rect_intr.coeffs[i]);
105 	}
106 	PrintInfo("\n\n");
107 
108 	Visualizer depth_vis, color_vis;
109 	if (depth_vis.CreateWindow("Depth", 640, 480, 15, 50) == false ||
110 			depth_vis.AddGeometry(depth_image_ptr) == false ||
111 			color_vis.CreateWindow("Color", 1920, 1080, 675, 50) == false ||
112 			color_vis.AddGeometry(color_image_ptr) == false) {
113 		return 0;
114 	}
115 
116 	while (depth_vis.PollEvents() && color_vis.PollEvents()) {
117 		timer.Signal();
118 		dev->wait_for_frames();
119 		memcpy(depth_image_ptr->data_.data(),
120 				dev->get_frame_data(rs::stream::depth), 640 * 480 * 2);
121 		memcpy(color_image_ptr->data_.data(),
122 				dev->get_frame_data(rs::stream::rectified_color),
123 				1920 * 1080 * 3);
124 		depth_vis.UpdateGeometry();
125 		color_vis.UpdateGeometry();
126 
127 		PrintInfo("%.2f\n", dev->get_option(rs::option::color_white_balance));
128 
129 		/*
130 		rs::option opts[10] = {
131 				rs::option::color_enable_auto_exposure,
132 				rs::option::color_exposure,
133 				rs::option::color_backlight_compensation,
134 				rs::option::color_brightness,
135 				rs::option::color_contrast,
136 				rs::option::color_gain,
137 				rs::option::color_gamma,
138 				rs::option::color_saturation,
139 				rs::option::color_sharpness,
140 				rs::option::color_hue
141 				};
142 		double value[10];
143 		dev->get_options((const rs::option *)opts, 10, (double *)value);
144 		PrintInfo("%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
145 				value[0], value[1], value[2], value[3], value[4],
146 				value[5], value[6], value[7], value[8], value[9]);
147 		*/
148 	}
149 
150 	//DrawGeometryWithAnimationCallback(depth_image_ptr,
151 	//		[&](Visualizer &vis) {
152 	//			timer.Signal();
153 	//			dev->wait_for_frames();
154 	//			memcpy(depth_image_ptr->data_.data(),
155 	//					dev->get_frame_data(rs::stream::depth), 640 * 480 * 2);
156 	//			return true;
157 	//		}, "Depth", 640, 480);
158 	return 1;
159 }