45 arInstallRequested_(
false),
46 depthFromMotion_(depthFromMotion)
64 std::vector<CameraConfig> & camera_configs,
67 if (camera_configs.empty()) {
71 int low_resolution_config_idx = 0;
72 int high_resolution_config_idx = 0;
73 int32_t smallest_height = camera_configs[0].height;
74 int32_t largest_height = camera_configs[0].height;
76 for (
int i = 1; i < camera_configs.size(); ++i) {
77 int32_t image_height = camera_configs[i].height;
78 if (image_height < smallest_height) {
79 smallest_height = image_height;
80 low_resolution_config_idx = i;
81 }
else if (image_height > largest_height) {
82 largest_height = image_height;
83 high_resolution_config_idx = i;
87 if (low_resolution_config_idx == high_resolution_config_idx) {
88 *lowest_resolution_config = &camera_configs[low_resolution_config_idx];
90 *lowest_resolution_config = &camera_configs[low_resolution_config_idx];
91 *highest_resolution_config = &camera_configs[high_resolution_config_idx];
96 const ArSession* ar_session,
const ArCameraConfigList* all_configs,
97 int index,
int num_configs,
CameraConfig* camera_config) {
98 if (camera_config !=
nullptr && index >= 0 && index < num_configs) {
99 ArCameraConfig_create(ar_session, &camera_config->
config);
100 ArCameraConfigList_getItem(ar_session, all_configs, index,
102 ArCameraConfig_getImageDimensions(ar_session, camera_config->
config,
103 &camera_config->
width,
112 for (
int i = 0; i < camera_configs.size(); ++i) {
113 if (camera_configs[i].
config !=
nullptr) {
114 ArCameraConfig_destroy(camera_configs[i].
config);
130 ArInstallStatus install_status;
140 ArCoreApk_requestInstall(
env_,
activity_, user_requested_install, &install_status);
142 switch (install_status)
144 case AR_INSTALL_STATUS_INSTALLED:
146 case AR_INSTALL_STATUS_INSTALL_REQUESTED:
158 int32_t is_depth_supported = 0;
159 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
164 if (is_depth_supported!=0) {
182 ArCameraConfigList* all_camera_configs =
nullptr;
184 ArCameraConfigList_create(
arSession_, &all_camera_configs);
186 ArCameraConfigFilter* camera_config_filter =
nullptr;
187 ArCameraConfigFilter_create(
arSession_, &camera_config_filter);
188 ArCameraConfigFilter_setTargetFps(
arSession_, camera_config_filter, AR_CAMERA_CONFIG_TARGET_FPS_30 | AR_CAMERA_CONFIG_TARGET_FPS_60);
189 ArSession_getSupportedCameraConfigsWithFilter(
arSession_, camera_config_filter, all_camera_configs);
190 ArCameraConfigList_getSize(
arSession_, all_camera_configs, &num_configs);
192 if (num_configs < 1) {
193 UERROR(
"No camera config found");
198 std::vector<CameraConfig> camera_configs;
199 CameraConfig* cpu_low_resolution_camera_config_ptr =
nullptr;
200 CameraConfig* cpu_high_resolution_camera_config_ptr =
nullptr;
201 camera_configs.resize(num_configs);
202 for (
int i = 0; i < num_configs; ++i) {
207 cpu_low_resolution_camera_config_ptr =
nullptr;
208 cpu_high_resolution_camera_config_ptr =
nullptr;
211 &cpu_low_resolution_camera_config_ptr,
212 &cpu_high_resolution_camera_config_ptr);
217 ArCameraConfigList_destroy(all_camera_configs);
218 ArSession_setCameraConfig(
arSession_, cpu_low_resolution_camera_config_ptr->
config);
226 if (ArSession_resume(
arSession_) != ArStatus::AR_SUCCESS)
228 UERROR(
"Cannot resume camera!");
276 const float * pointCloudData,
281 std::vector<cv::KeyPoint> * kpts,
282 std::vector<cv::Point3f> * kpts3D)
284 if(pointCloudData && points>0)
286 cv::Mat scanData(1, points, CV_32FC4);
287 float * ptr = scanData.ptr<
float>();
288 for(
unsigned int i=0;i<points; ++i)
290 cv::Point3f pt(pointCloudData[i*4], pointCloudData[i*4 + 1], pointCloudData[i*4 + 2]);
301 unsigned char r=255,g=255,b=255;
304 b=rgb.at<cv::Vec3b>(v,u).val[0];
305 g=rgb.at<cv::Vec3b>(v,u).val[1];
306 r=rgb.at<cv::Vec3b>(v,u).val[2];
308 kpts->push_back(cv::KeyPoint(u,v,3));
310 kpts3D->push_back(org);
312 *(
int*)&ptr[i*4 + 3] =
int(b) | (int(g) << 8) | (
int(r) << 16);
328 int ret =
static_cast<int>(colorCameraToDisplayRotation) + 1;
333 ArSession_setDisplayGeometry(
arSession_, ret, width, height);
351 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
textureId_);
352 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
353 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
354 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
355 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
362 LOGE(
"CameraARCore::captureImage() ArSession_update error");
371 ArFrame_transformCoordinates2d(
383 ArCamera_getProjectionMatrix(
arSession_, ar_camera,
393 ArTrackingState camera_tracking_state;
394 ArCamera_getTrackingState(
arSession_, ar_camera, &camera_tracking_state);
398 if(camera_tracking_state == AR_TRACKING_STATE_TRACKING)
404 pose =
Transform(pose_raw[4], pose_raw[5], pose_raw[6], pose_raw[0], pose_raw[1], pose_raw[2], pose_raw[3]);
410 LOGE(
"CameraARCore: Pose is null");
431 LOGI(
"%f %f %f %f %d %d", fx, fy, cx, cy, width, height);
434 if(fx > 0 && fy > 0 && width > 0 && height > 0 && cx > 0 && cy > 0)
438 ArPointCloud * pointCloud =
nullptr;
441 int32_t is_depth_supported = 0;
442 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
444 ArImage * image =
nullptr;
446 if(status == AR_SUCCESS)
448 if(is_depth_supported)
450 LOGD(
"Acquire depth image!");
451 ArImage * depthImage =
nullptr;
454 ArImageFormat format;
455 ArImage_getFormat(
arSession_, depthImage, &format);
456 if(format == AR_IMAGE_FORMAT_DEPTH16)
458 LOGD(
"Depth format detected!");
460 ArImage_getNumberOfPlanes(
arSession_, depthImage, &planeCount);
461 LOGD(
"planeCount=%d", planeCount);
462 UASSERT_MSG(planeCount == 1,
uFormat(
"Error: getNumberOfPlanes() planceCount = %d", planeCount).c_str());
468 ArImage_getWidth(
arSession_, depthImage, &depth_width);
469 ArImage_getHeight(
arSession_, depthImage, &depth_height);
470 ArImage_getPlaneRowStride(
arSession_, depthImage, 0, &stride);
471 ArImage_getPlaneData(
arSession_, depthImage, 0, &data, &len);
473 LOGD(
"width=%d, height=%d, bytes=%d stride=%d", depth_width, depth_height, len, stride);
475 cv::Mat occlusionImage = cv::Mat(depth_height, depth_width, CV_16UC1, (
void*)data).clone();
477 float scaleX = (float)depth_width / (
float)width;
478 float scaleY = (float)depth_height / (
float)height;
482 ArImage_release(depthImage);
486 ArImageFormat format;
487 ArImage_getTimestamp(
arSession_, image, ×tamp_ns);
488 ArImage_getFormat(
arSession_, image, &format);
489 if(format == AR_IMAGE_FORMAT_YUV_420_888)
493 ArImage_getNumberOfPlanes(
arSession_, image, &num_planes);
494 for(
int i=0;i<num_planes; ++i)
498 ArImage_getPlanePixelStride(
arSession_, image, i, &pixel_stride);
499 ArImage_getPlaneRowStride(
arSession_, image, i, &row_stride);
500 LOGI(
"Plane %d/%d: pixel stride=%d, row stride=%d", i+1, num_planes, pixel_stride, row_stride);
506 ArImage_getPlaneData(
arSession_, image, 0, &plane_data, &data_length);
508 ArImage_getPlaneData(
arSession_, image, 2, &plane_uv_data, &uv_data_length);
510 if(plane_data !=
nullptr && data_length == height*width)
512 double stamp = double(timestamp_ns)/10e8;
514 LOGI(
"data_length=%d stamp=%f", data_length, stamp);
517 if((
long)plane_uv_data-(
long)plane_data != data_length)
520 cv::Mat yuv(height+height/2, width, CV_8UC1);
521 memcpy(yuv.data, plane_data, data_length);
522 memcpy(yuv.data+data_length, plane_uv_data, height/2*width);
523 cv::cvtColor(yuv, rgb, cv::COLOR_YUV2BGR_NV21);
527 cv::cvtColor(cv::Mat(height+height/2, width, CV_8UC1, (
void*)plane_data), rgb, cv::COLOR_YUV2BGR_NV21);
530 std::vector<cv::KeyPoint> kpts;
531 std::vector<cv::Point3f> kpts3;
536 ArPointCloud_getNumberOfPoints(
arSession_, pointCloud, &points);
537 const float * pointCloudData = 0;
538 ArPointCloud_getData(
arSession_, pointCloud, &pointCloudData);
540 LOGI(
"pointCloudData=%d size=%d", pointCloudData?1:0, points);
542 if(pointCloudData && points>0)
549 LOGI(
"pointCloud empty");
558 LOGE(
"CameraARCore: cannot convert image format %d", format);
563 LOGE(
"CameraARCore: failed to get rgb image (status=%d)", (
int)status);
566 ArImage_release(image);
567 ArPointCloud_release(pointCloud);
571 ArCamera_release(ar_camera);
589 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
textureId_);
590 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
591 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
592 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
593 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
599 LOGE(
"CameraARCore::capturePoseOnly() ArSession_update error");
608 ArFrame_transformCoordinates2d(
620 ArCamera_getProjectionMatrix(
arSession_, ar_camera,
630 ArTrackingState camera_tracking_state;
631 ArCamera_getTrackingState(
arSession_, ar_camera, &camera_tracking_state);
635 if(camera_tracking_state == AR_TRACKING_STATE_TRACKING)
641 pose =
Transform(pose_raw[4], pose_raw[5], pose_raw[6], pose_raw[0], pose_raw[1], pose_raw[2], pose_raw[3]);
653 int32_t is_depth_supported = 0;
654 ArSession_isDepthModeSupported(
arSession_, AR_DEPTH_MODE_AUTOMATIC, &is_depth_supported);
656 if(is_depth_supported)
658 LOGD(
"Acquire depth image!");
659 ArImage * depthImage =
nullptr;
662 ArImageFormat format;
663 ArImage_getFormat(
arSession_, depthImage, &format);
664 if(format == AR_IMAGE_FORMAT_DEPTH16)
666 LOGD(
"Depth format detected!");
668 ArImage_getNumberOfPlanes(
arSession_, depthImage, &planeCount);
669 LOGD(
"planeCount=%d", planeCount);
670 UASSERT_MSG(planeCount == 1,
uFormat(
"Error: getNumberOfPlanes() planceCount = %d", planeCount).c_str());
676 ArImage_getWidth(
arSession_, depthImage, &width);
677 ArImage_getHeight(
arSession_, depthImage, &height);
678 ArImage_getPlaneRowStride(
arSession_, depthImage, 0, &stride);
679 ArImage_getPlaneData(
arSession_, depthImage, 0, &data, &len);
681 LOGD(
"width=%d, height=%d, bytes=%d stride=%d", width, height, len, stride);
683 cv::Mat occlusionImage = cv::Mat(height, width, CV_16UC1, (
void*)data).clone();
692 float scaleX = (float)width / (
float)rgb_width;
693 float scaleY = (float)height / (
float)rgb_height;
697 ArImage_release(depthImage);
701 ArCamera_release(ar_camera);
glm::mat4 glmFromTransform(const rtabmap::Transform &transform)
ArCameraIntrinsics * arCameraIntrinsics_
const Transform & getOriginOffset() const
cv::Point3f RTABMAP_EXP transformPoint(const cv::Point3f &pt, const Transform &transform)
float transformed_uvs_[8]
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)
virtual std::string getSerial() const
virtual SensorData captureImage(CameraInfo *info=0)
void reproject(float x, float y, float z, float &u, float &v) const
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[]
GLM_FUNC_DECL bool isNull(detail::tmat2x2< T, P > const &m, T const &epsilon)
#define UASSERT(condition)
Transform deviceTColorCamera_
#define UASSERT_MSG(condition, msg_str)
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)
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 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)
const cv::Mat & getOcclusionImage(CameraModel *model=0) const
void destroyCameraConfigs(std::vector< CameraConfig > &camera_configs)
static const rtabmap::Transform opticalRotation
ULogger class and convenient macros.
void setOcclusionImage(const cv::Mat &image, const CameraModel &model)
void copyCameraConfig(const ArSession *ar_session, const ArCameraConfigList *all_configs, int index, int num_configs, CameraConfig *camera_config)
static const rtabmap::Transform opticalRotationInv
bool inFrame(int u, int v) const
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())
glm::mat4 projectionMatrix_
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)
GLM_FUNC_DECL matType< T, P > inverse(matType< T, P > const &m)