45 arInstallRequested_(
false),
47 uvs_initialized_(
false),
48 updateOcclusionImage_(
false),
49 depthFromMotion_(depthFromMotion)
73 std::vector<CameraConfig> & camera_configs,
76 if (camera_configs.empty()) {
80 int low_resolution_config_idx = 0;
81 int high_resolution_config_idx = 0;
82 int32_t smallest_height = camera_configs[0].height;
83 int32_t largest_height = camera_configs[0].height;
85 for (
int i = 1; i < camera_configs.size(); ++i) {
86 int32_t image_height = camera_configs[i].height;
87 if (image_height < smallest_height) {
88 smallest_height = image_height;
89 low_resolution_config_idx = i;
90 }
else if (image_height > largest_height) {
91 largest_height = image_height;
92 high_resolution_config_idx = i;
96 if (low_resolution_config_idx == high_resolution_config_idx) {
97 *lowest_resolution_config = &camera_configs[low_resolution_config_idx];
99 *lowest_resolution_config = &camera_configs[low_resolution_config_idx];
100 *highest_resolution_config = &camera_configs[high_resolution_config_idx];
105 const ArSession* ar_session,
const ArCameraConfigList* all_configs,
106 int index,
int num_configs,
CameraConfig* camera_config) {
107 if (camera_config !=
nullptr && index >= 0 && index < num_configs) {
108 ArCameraConfig_create(ar_session, &camera_config->
config);
109 ArCameraConfigList_getItem(ar_session, all_configs, index,
111 ArCameraConfig_getImageDimensions(ar_session, camera_config->
config,
112 &camera_config->
width,
121 for (
int i = 0; i < camera_configs.size(); ++i) {
122 if (camera_configs[i].
config !=
nullptr) {
123 ArCameraConfig_destroy(camera_configs[i].
config);
139 ArInstallStatus install_status;
149 ArCoreApk_requestInstall(
env_,
activity_, user_requested_install, &install_status);
151 switch (install_status)
153 case AR_INSTALL_STATUS_INSTALLED:
155 case AR_INSTALL_STATUS_INSTALL_REQUESTED:
167 int32_t is_depth_supported = 0;
168 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
173 if (is_depth_supported!=0) {
191 ArCameraConfigList* all_camera_configs =
nullptr;
193 ArCameraConfigList_create(
arSession_, &all_camera_configs);
195 ArCameraConfigFilter* camera_config_filter =
nullptr;
196 ArCameraConfigFilter_create(
arSession_, &camera_config_filter);
197 ArCameraConfigFilter_setTargetFps(
arSession_, camera_config_filter, AR_CAMERA_CONFIG_TARGET_FPS_30 | AR_CAMERA_CONFIG_TARGET_FPS_60);
198 ArSession_getSupportedCameraConfigsWithFilter(
arSession_, camera_config_filter, all_camera_configs);
199 ArCameraConfigList_getSize(
arSession_, all_camera_configs, &num_configs);
201 if (num_configs < 1) {
202 UERROR(
"No camera config found");
207 std::vector<CameraConfig> camera_configs;
208 CameraConfig* cpu_low_resolution_camera_config_ptr =
nullptr;
209 CameraConfig* cpu_high_resolution_camera_config_ptr =
nullptr;
210 camera_configs.resize(num_configs);
211 for (
int i = 0; i < num_configs; ++i) {
216 cpu_low_resolution_camera_config_ptr =
nullptr;
217 cpu_high_resolution_camera_config_ptr =
nullptr;
220 &cpu_low_resolution_camera_config_ptr,
221 &cpu_high_resolution_camera_config_ptr);
226 ArCameraConfigList_destroy(all_camera_configs);
227 ArSession_setCameraConfig(
arSession_, cpu_low_resolution_camera_config_ptr->
config);
235 if (ArSession_resume(
arSession_) != ArStatus::AR_SUCCESS)
237 UERROR(
"Cannot resume camera!");
286 const float * pointCloudData,
291 std::vector<cv::KeyPoint> * kpts,
292 std::vector<cv::Point3f> * kpts3D)
294 if(pointCloudData && points>0)
296 cv::Mat scanData(1, points, CV_32FC4);
297 float * ptr = scanData.ptr<
float>();
298 for(
unsigned int i=0;i<points; ++i)
300 cv::Point3f pt(pointCloudData[i*4], pointCloudData[i*4 + 1], pointCloudData[i*4 + 2]);
311 unsigned char r=255,g=255,b=255;
314 b=rgb.at<cv::Vec3b>(v,u).val[0];
315 g=rgb.at<cv::Vec3b>(v,u).val[1];
316 r=rgb.at<cv::Vec3b>(v,u).val[2];
318 kpts->push_back(cv::KeyPoint(u,v,3));
320 kpts3D->push_back(org);
322 *(
int*)&ptr[i*4 + 3] =
int(b) | (int(g) << 8) | (
int(r) << 16);
338 int ret =
static_cast<int>(colorCameraToDisplayRotation) + 1;
343 ArSession_setDisplayGeometry(
arSession_, ret, width, height);
361 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
textureId_);
362 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
363 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
364 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
365 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
371 LOGE(
"CameraARCore::captureImage() ArSession_update error");
380 ArFrame_transformCoordinates2d(
392 ArCamera_getProjectionMatrix(
arSession_, ar_camera,
396 ArTrackingState camera_tracking_state;
397 ArCamera_getTrackingState(
arSession_, ar_camera, &camera_tracking_state);
401 if(camera_tracking_state == AR_TRACKING_STATE_TRACKING)
407 pose =
Transform(pose_raw[4], pose_raw[5], pose_raw[6], pose_raw[0], pose_raw[1], pose_raw[2], pose_raw[3]);
418 LOGI(
"%f %f %f %f %d %d", fx, fy, cx, cy, width, height);
421 if(fx > 0 && fy > 0 && width > 0 && height > 0 && cx > 0 && cy > 0)
425 ArPointCloud * pointCloud =
nullptr;
428 int32_t is_depth_supported = 0;
429 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
431 ArImage * image =
nullptr;
433 if(status == AR_SUCCESS)
437 LOGD(
"Acquire depth image!");
438 ArImage * depthImage =
nullptr;
441 ArImageFormat format;
442 ArImage_getFormat(
arSession_, depthImage, &format);
443 if(format == AR_IMAGE_FORMAT_DEPTH16)
445 LOGD(
"Depth format detected!");
447 ArImage_getNumberOfPlanes(
arSession_, depthImage, &planeCount);
448 LOGD(
"planeCount=%d", planeCount);
449 UASSERT_MSG(planeCount == 1,
uFormat(
"Error: getNumberOfPlanes() planceCount = %d", planeCount).c_str());
455 ArImage_getWidth(
arSession_, depthImage, &depth_width);
456 ArImage_getHeight(
arSession_, depthImage, &depth_height);
457 ArImage_getPlaneRowStride(
arSession_, depthImage, 0, &stride);
458 ArImage_getPlaneData(
arSession_, depthImage, 0, &data, &len);
460 LOGD(
"width=%d, height=%d, bytes=%d stride=%d", depth_width, depth_height, len, stride);
462 occlusionImage_ = cv::Mat(depth_height, depth_width, CV_16UC1, (
void*)data).clone();
464 float scaleX = (float)depth_width / (
float)width;
465 float scaleY = (float)depth_height / (
float)height;
468 ArImage_release(depthImage);
472 ArImageFormat format;
473 ArImage_getTimestamp(
arSession_, image, ×tamp_ns);
474 ArImage_getFormat(
arSession_, image, &format);
475 if(format == AR_IMAGE_FORMAT_YUV_420_888)
479 ArImage_getNumberOfPlanes(
arSession_, image, &num_planes);
480 for(
int i=0;i<num_planes; ++i)
484 ArImage_getPlanePixelStride(
arSession_, image, i, &pixel_stride);
485 ArImage_getPlaneRowStride(
arSession_, image, i, &row_stride);
486 LOGI(
"Plane %d/%d: pixel stride=%d, row stride=%d", i+1, num_planes, pixel_stride, row_stride);
492 ArImage_getPlaneData(
arSession_, image, 0, &plane_data, &data_length);
494 ArImage_getPlaneData(
arSession_, image, 2, &plane_uv_data, &uv_data_length);
496 if(plane_data !=
nullptr && data_length == height*width)
498 double stamp = double(timestamp_ns)/10e8;
500 LOGI(
"data_length=%d stamp=%f", data_length, stamp);
503 if((
long)plane_uv_data-(
long)plane_data != data_length)
506 cv::Mat yuv(height+height/2, width, CV_8UC1);
507 memcpy(yuv.data, plane_data, data_length);
508 memcpy(yuv.data+data_length, plane_uv_data, height/2*width);
509 cv::cvtColor(yuv, rgb, CV_YUV2BGR_NV21);
513 cv::cvtColor(cv::Mat(height+height/2, width, CV_8UC1, (
void*)plane_data), rgb, CV_YUV2BGR_NV21);
516 std::vector<cv::KeyPoint> kpts;
517 std::vector<cv::Point3f> kpts3;
522 ArPointCloud_getNumberOfPoints(
arSession_, pointCloud, &points);
523 const float * pointCloudData = 0;
524 ArPointCloud_getData(
arSession_, pointCloud, &pointCloudData);
526 LOGI(
"pointCloudData=%d size=%d", pointCloudData?1:0, points);
528 if(pointCloudData && points>0)
535 LOGI(
"pointCloud empty");
544 LOGE(
"CameraARCore: cannot convert image format %d", format);
549 LOGE(
"CameraARCore: failed to get rgb image (status=%d)", (
int)status);
552 ArImage_release(image);
553 ArPointCloud_release(pointCloud);
557 ArCamera_release(ar_camera);
561 LOGE(
"CameraARCore: Pose is null");
585 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
textureId_);
586 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
587 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
588 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
589 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
595 LOGE(
"CameraARCore::capturePoseOnly() ArSession_update error");
604 ArFrame_transformCoordinates2d(
616 ArCamera_getProjectionMatrix(
arSession_, ar_camera,
620 ArTrackingState camera_tracking_state;
621 ArCamera_getTrackingState(
arSession_, ar_camera, &camera_tracking_state);
625 if(camera_tracking_state == AR_TRACKING_STATE_TRACKING)
631 pose =
Transform(pose_raw[4], pose_raw[5], pose_raw[6], pose_raw[0], pose_raw[1], pose_raw[2], pose_raw[3]);
638 int32_t is_depth_supported = 0;
639 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
643 LOGD(
"Acquire depth image!");
644 ArImage * depthImage =
nullptr;
647 ArImageFormat format;
648 ArImage_getFormat(
arSession_, depthImage, &format);
649 if(format == AR_IMAGE_FORMAT_DEPTH16)
651 LOGD(
"Depth format detected!");
653 ArImage_getNumberOfPlanes(
arSession_, depthImage, &planeCount);
654 LOGD(
"planeCount=%d", planeCount);
655 UASSERT_MSG(planeCount == 1,
uFormat(
"Error: getNumberOfPlanes() planceCount = %d", planeCount).c_str());
661 ArImage_getWidth(
arSession_, depthImage, &width);
662 ArImage_getHeight(
arSession_, depthImage, &height);
663 ArImage_getPlaneRowStride(
arSession_, depthImage, 0, &stride);
664 ArImage_getPlaneData(
arSession_, depthImage, 0, &data, &len);
666 LOGD(
"width=%d, height=%d, bytes=%d stride=%d", width, height, len, stride);
668 occlusionImage_ = cv::Mat(height, width, CV_16UC1, (
void*)data).clone();
677 float scaleX = (float)width / (
float)rgb_width;
678 float scaleY = (float)height / (
float)rgb_height;
681 ArImage_release(depthImage);
685 ArCamera_release(ar_camera);
ArCameraIntrinsics * arCameraIntrinsics_
bool updateOcclusionImage_
cv::Point3f RTABMAP_EXP transformPoint(const cv::Point3f &pt, const Transform &transform)
bool inFrame(int u, int v) const
virtual SensorData captureImage(CameraInfo *info=0)
void poseReceived(const Transform &pose)
static constexpr int kNumVertices
virtual void capturePoseOnly()
GLM_FUNC_DECL genType::value_type const * value_ptr(genType const &vec)
static const GLfloat BackgroundRenderer_kVertices[]
#define UASSERT(condition)
static const rtabmap::Transform opengl_world_T_rtabmap_world(0.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,-1.0f, 0.0f, 0.0f, 0.0f)
virtual std::string getSerial() const
float transformed_uvs_[BackgroundRenderer::kNumVertices *2]
Transform deviceTColorCamera_
#define UASSERT_MSG(condition, msg_str)
CameraModel occlusionModel_
static LaserScan scanFromPointCloudData(const float *pointCloudData, int points, const Transform &pose, const CameraModel &model, const cv::Mat &rgb, std::vector< cv::KeyPoint > *kpts=0, std::vector< cv::Point3f > *kpts3D=0)
void reproject(float x, float y, float z, float &u, float &v) const
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
virtual void setScreenRotationAndSize(ScreenRotation colorCameraToDisplayRotation, int width, int height)
void getCameraConfigLowestAndHighestResolutions(std::vector< CameraConfig > &camera_configs, CameraConfig **lowest_resolution_config, CameraConfig **highest_resolution_config)
static const rtabmap::Transform rtabmap_world_T_opengl_world(0.0f, 0.0f,-1.0f, 0.0f,-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f)
void destroyCameraConfigs(std::vector< CameraConfig > &camera_configs)
static const rtabmap::Transform opticalRotation
ULogger class and convenient macros.
glm::mat4 projectionMatrix_
void copyCameraConfig(const ArSession *ar_session, const ArCameraConfigList *all_configs, int index, int num_configs, CameraConfig *camera_config)
static const rtabmap::Transform opticalRotationInv
GLM_FUNC_DECL std::string to_string(genType const &x)
CameraARCore(void *env, void *context, void *activity, bool depthFromMotion=false, bool smoothing=false)
std::string UTILITE_EXP uFormat(const char *fmt,...)
static LaserScan backwardCompatibility(const cv::Mat &oldScanFormat, int maxPoints=0, int maxRange=0, const Transform &localTransform=Transform::getIdentity())
void setFeatures(const std::vector< cv::KeyPoint > &keypoints, const std::vector< cv::Point3f > &keypoints3D, const cv::Mat &descriptors)
virtual void setScreenRotationAndSize(ScreenRotation colorCameraToDisplayRotation, int width, int height)