summaryrefslogtreecommitdiff
path: root/mv_3d/3d/src/mv_3d.cpp
blob: 282a4b5ad1bea9568aec7d840cbd20dca95d1261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstddef>
#include <exception>
#include <new>

#include "Mv3d.h"
#include "mv_3d.h"
#include "mv_common.h"
#include "mv_feature_key.h"
#include "mv_private.h"

using namespace mediavision::mv3d;

static const char *main_feature_keys[] = { "http://tizen.org/feature/vision.3d" };
static const char *sub_feature_keys[] = { "http://tizen.org/feature/vision.3d.depth",
										  "http://tizen.org/feature/vision.3d.pointcloud" };

static inline bool mv_3d_all_check_system_info_feature_supported()
{
	return mv_check_feature_key(main_feature_keys, 1, false) && mv_check_feature_key(sub_feature_keys, 2, true);
}

int mv_3d_create(mv_3d_h *mv3d)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_NULL_ARG_CHECK(mv3d);
	MEDIA_VISION_FUNCTION_ENTER();

	try {
		(*mv3d) = static_cast<mv_3d_h>(new Mv3d());
	} catch (const std::exception &e) {
		LOGE("Failed to create mv3d handle with %s", e.what());
		return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return MEDIA_VISION_ERROR_NONE;
}

int mv_3d_destroy(mv_3d_h mv3d)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_FUNCTION_ENTER();

	delete static_cast<Mv3d *>(mv3d);

	MEDIA_VISION_FUNCTION_LEAVE();
	return MEDIA_VISION_ERROR_NONE;
}

int mv3dSetDepthParameters(mv_3d_h mv3d, mv_engine_config_h engine_config)
{
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);

	pMv3d->SetParameters(127.5, // threshold
						 3, // aggregation window width
						 3, // aggregation window height
						 0 // speckleSize
	);

	MEDIA_VISION_FUNCTION_LEAVE();
	return MEDIA_VISION_ERROR_NONE;
}

int mv_3d_configure(mv_3d_h mv3d, mv_engine_config_h engine_config)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_INSTANCE_CHECK(engine_config);
	MEDIA_VISION_FUNCTION_ENTER();

	int mode = MV_3D_DEPTH_MODE_NONE;

	int ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_DEPTH_MODE, &mode);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get depth mode");
		return ret;
	}

	ret = mv3dSetDepthParameters(mv3d, engine_config);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to set dfs parameters");
		return ret;
	}

	int depthWidth, depthHeight, minDisp, maxDisp;
	ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_DEPTH_WIDTH, &depthWidth);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get depth width");
		return ret;
	}

	ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_DEPTH_HEIGHT, &depthHeight);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get depth height");
		return ret;
	}

	if (depthWidth <= 0 || depthHeight <= 0) {
		LOGE("Invalid depth resolution %d x %d", depthWidth, depthHeight);
		return MEDIA_VISION_ERROR_INVALID_PARAMETER;
	}

	ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_DEPTH_MIN_DISPARITY, &minDisp);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get min disparity");
		return ret;
	}

	ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_DEPTH_MAX_DISPARITY, &maxDisp);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get max disparity");
		return ret;
	}

	double samplingRatio;
	ret = mv_engine_config_get_double_attribute(engine_config, MV_3D_POINTCLOUD_SAMPLING_RATIO, &samplingRatio);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get stereo configuration file path");
		return ret;
	}

	int outlierRemovalPoints;
	ret = mv_engine_config_get_int_attribute(engine_config, MV_3D_POINTCLOUD_OUTLIER_REMOVAL_POINTS,
											 &outlierRemovalPoints);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get stereo configuration file path");
		return ret;
	}

	double outlierRemovalRadius;
	ret = mv_engine_config_get_double_attribute(engine_config, MV_3D_POINTCLOUD_OUTLIER_REMOVAL_RADIUS,
												&outlierRemovalRadius);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get stereo configuration file path");
		return ret;
	}

	char *stereoConfigFilePath = NULL;
	ret = mv_engine_config_get_string_attribute(engine_config, MV_3D_DEPTH_STEREO_CONFIG_FILE_PATH,
												&stereoConfigFilePath);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get stereo configuration file path");
		return ret;
	}

	char *pointcloudOutputFilePath = NULL;
	ret = mv_engine_config_get_string_attribute(engine_config, MV_3D_POINTCLOUD_OUTPUT_FILE_PATH,
												&pointcloudOutputFilePath);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to get stereo configuration file path");
		free(stereoConfigFilePath);
		return ret;
	}

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	ret = pMv3d->Configure(mode, static_cast<unsigned int>(depthWidth), static_cast<unsigned int>(depthHeight), minDisp,
						   maxDisp, samplingRatio, outlierRemovalPoints, outlierRemovalRadius, stereoConfigFilePath,
						   pointcloudOutputFilePath);

	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to configure Depth");
	}

	free(pointcloudOutputFilePath);
	pointcloudOutputFilePath = NULL;

	free(stereoConfigFilePath);
	stereoConfigFilePath = NULL;

	MEDIA_VISION_FUNCTION_LEAVE();
	return ret;
}

int mv_3d_set_depth_cb(mv_3d_h mv3d, mv_3d_depth_cb depth_cb, void *user_data)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_check_feature_key(main_feature_keys, 1, false) &&
							   mv_check_feature_key(sub_feature_keys, 1, false));
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_NULL_ARG_CHECK(depth_cb);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	pMv3d->SetDepthCallback(depth_cb, user_data);

	MEDIA_VISION_FUNCTION_LEAVE();
	return MEDIA_VISION_ERROR_NONE;
}

// LCOV_EXCL_START
int mv_3d_set_pointcloud_cb(mv_3d_h mv3d, mv_3d_pointcloud_cb pointcloud_cb, void *user_data)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_check_feature_key(main_feature_keys, 1, false) &&
							   mv_check_feature_key(&sub_feature_keys[1], 1, false));
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_NULL_ARG_CHECK(pointcloud_cb);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	int ret = pMv3d->SetPointcloudCallback(pointcloud_cb, user_data);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to run depth");
		return ret;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return MEDIA_VISION_ERROR_NONE;
}
// LCOV_EXCL_STOP

int mv_3d_prepare(mv_3d_h mv3d)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	int ret = pMv3d->Prepare();
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to prepare depth");
		return ret;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return ret;
}

int mv_3d_run(mv_3d_h mv3d, mv_source_h source, mv_source_h source_extra, mv_source_h color)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_INSTANCE_CHECK(source);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);

	int ret = pMv3d->Run(source, source_extra);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to run depth");
		return ret;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return ret;
}

int mv_3d_run_async(mv_3d_h mv3d, mv_source_h source, mv_source_h source_extra, mv_source_h color)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_3d_all_check_system_info_feature_supported());
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_INSTANCE_CHECK(source);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	int ret = pMv3d->RunAsync(source, source_extra);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to run depth");
		return ret;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return ret;
}

// LCOV_EXCL_START
int mv_3d_pointcloud_write_file(mv_3d_h mv3d, mv_3d_pointcloud_h pointcloud, mv_3d_pointcloud_type_e type,
								char *filename)
{
	MEDIA_VISION_SUPPORT_CHECK(mv_check_feature_key(main_feature_keys, 1, false) &&
							   mv_check_feature_key(&sub_feature_keys[1], 1, false));
	MEDIA_VISION_INSTANCE_CHECK(mv3d);
	MEDIA_VISION_INSTANCE_CHECK(pointcloud);
	MEDIA_VISION_FUNCTION_ENTER();

	auto pMv3d = static_cast<Mv3d *>(mv3d);
	int ret = pMv3d->WritePointcloudFile(pointcloud, type, filename);
	if (ret != MEDIA_VISION_ERROR_NONE) {
		LOGE("Failed to write pointcloud file");
		return ret;
	}

	MEDIA_VISION_FUNCTION_LEAVE();
	return ret;
}
// LCOV_EXCL_STOP