Skip to content

Commit

Permalink
Able to create blacklisted projection areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Timocop committed Jun 4, 2022
1 parent 30307e6 commit c3ada3f
Show file tree
Hide file tree
Showing 11 changed files with 708 additions and 184 deletions.
374 changes: 270 additions & 104 deletions src/psmoveconfigtool/AppStage_ColorCalibration.cpp

Large diffs are not rendered by default.

36 changes: 32 additions & 4 deletions src/psmoveconfigtool/AppStage_ColorCalibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class AppStage_ColorCalibration : public AppStage
}

protected:
enum eCommonBlacklistProjection
{
MAX_BLACKLIST_PROJECTIONS = 5
};

enum eMenuState
{
inactive,
Expand Down Expand Up @@ -147,6 +152,24 @@ class AppStage_ColorCalibration : public AppStage
float value_range;
};

struct CommonDeviceBlacklistProjection
{
float x, y, w, h;

inline void clear()
{
x = y = w = h = 0.f;
}

inline void set(float _x, float _y, float _w, float _h)
{
x = _x;
y = _y;
w = _w;
h = _h;
}
};

void setState(eMenuState newState);

void request_start_controller_streams();
Expand Down Expand Up @@ -186,10 +209,12 @@ class AppStage_ColorCalibration : public AppStage
const PSMResponseMessage *response,
void *userdata);

void request_tracker_set_option(TrackerOption &option, int new_option_index);
static void handle_tracker_set_option_response(
const PSMResponseMessage *response,
void *userdata);
void request_tracker_set_option(TrackerOption &option, int new_option_index);
static void handle_tracker_set_option_response(
const PSMResponseMessage *response,
void *userdata);

void request_tracker_set_projectionblacklist(CommonDeviceBlacklistProjection projection_blacklisted[eCommonBlacklistProjection::MAX_BLACKLIST_PROJECTIONS]);

void request_tracker_set_color_preset(PSMTrackingColorType color_type, TrackerColorPreset &color_preset);
static void handle_tracker_set_color_preset_response(
Expand Down Expand Up @@ -252,6 +277,8 @@ class AppStage_ColorCalibration : public AppStage
double m_trackerGain;
std::vector<TrackerOption> m_trackerOptions;
TrackerColorPreset m_colorPresets[PSMTrackingColorType_MaxColorTypes];
CommonDeviceBlacklistProjection m_blacklisted_projection[eCommonBlacklistProjection::MAX_BLACKLIST_PROJECTIONS];

int tracker_count;
int tracker_index;

Expand All @@ -277,6 +304,7 @@ class AppStage_ColorCalibration : public AppStage
bool m_bColorCollisionPrevent;
bool m_bColorCollsionShow;
std::vector<std::vector<int>> m_mDetectedContures;
bool m_bProjectionBlacklistedShow;

// Auto detect color
bool m_bDetectingColors;
Expand Down
25 changes: 25 additions & 0 deletions src/psmoveprotocol/PSMoveProtocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ message Pose {
Position position= 2;
}

message ProjectionBlacklistList {
ProjectionBlacklist _1 = 1;
ProjectionBlacklist _2 = 2;
ProjectionBlacklist _3 = 3;
ProjectionBlacklist _4 = 4;
ProjectionBlacklist _5 = 5;
}

message ProjectionBlacklist {
float x = 1;
float y = 2;
float w = 3;
float h = 4;
}

enum TrackerType {
PS3EYE = 0;
}
Expand Down Expand Up @@ -185,6 +200,7 @@ message Request {

SET_CONTROLLER_OPTICAL_TRACKING = 48;
SET_CONTROLLER_PSMOVE_EMULATION = 49;
SET_TRACKER_PROJECTIONBLACKLIST = 50;
}
RequestType type = 2;

Expand Down Expand Up @@ -561,6 +577,12 @@ message Request {
}
RequestSetControllerPSmoveEmulation request_set_controller_psmove_emulation = 49;

// Parameters for SET_TRACKER_PROJECTIONBLACKLIST
message RequestSetTrackerProjectionBlacklist {
int32 tracker_id = 1;
ProjectionBlacklistList projection_blacklist = 2;
}
RequestSetTrackerProjectionBlacklist request_set_tracker_projection_blacklist = 50;
}

// Reliable (TCP) responses to requests
Expand Down Expand Up @@ -712,6 +734,9 @@ message Response {
float frame_rate= 5;
float frame_width= 6;
float frame_height= 7;

// Tracker Projection Blacklist
ProjectionBlacklistList projection_blacklist = 8;
}
ResultTrackerSettings result_tracker_settings = 25;

Expand Down
25 changes: 25 additions & 0 deletions src/psmoveservice/Device/Interface/DeviceInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace PSMoveProtocol
};

// -- constants -----
enum eCommonBlacklistProjection
{
MAX_BLACKLIST_PROJECTIONS = 5
};

enum eCommonTrackingColorID {
INVALID_COLOR= -1,

Expand Down Expand Up @@ -158,6 +163,24 @@ struct CommonDeviceScreenLocation
}
};

struct CommonDeviceBlacklistProjection
{
float x, y, w, h;

inline void clear()
{
x = y = w = h = 0.f;
}

inline void set(float _x, float _y, float _w, float _h)
{
x = _x;
y = _y;
w = _w;
h = _h;
}
};

struct CommonDeviceQuaternion
{
float x, y, z, w;
Expand Down Expand Up @@ -623,6 +646,8 @@ class ITrackerInterface : public IDeviceInterface
virtual void gatherTrackingColorPresets(const std::string &controller_serial, PSMoveProtocol::Response_ResultTrackerSettings* settings) const = 0;
virtual void setTrackingColorPreset(const std::string &controller_serial, eCommonTrackingColorID color, const CommonHSVColorRange *preset) = 0;
virtual void getTrackingColorPreset(const std::string &controller_serial, eCommonTrackingColorID color, CommonHSVColorRange *out_preset) const = 0;
virtual void setBlacklistProjection(const int index, const float x, const float y, const float w, const float h) = 0;
virtual bool getBlacklistProjection(const int index, float &x, float &y, float &w, float &h) const = 0;
};

/// Abstract class for HMD interface. Implemented HMD classes
Expand Down
71 changes: 56 additions & 15 deletions src/psmoveservice/Device/View/ServerControllerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ void ServerControllerView::updateOpticalPoseEstimation(TrackerManager* tracker_m
// Assume we're going to lose tracking this frame
bool bCurrentlyTracking = false;
bool bOccluded = false;
bool bBlacklisted = false;

if (tracker->getIsOpen())
{
Expand Down Expand Up @@ -559,6 +560,7 @@ void ServerControllerView::updateOpticalPoseEstimation(TrackerManager* tracker_m
}

bool bIsOccluded = false;
bool bIsBlacklisted = false;

//Create an occlusion area at the last seen valid tracked projection.
//If the projection center is near the occluded area it will not mark the projection as valid.
Expand Down Expand Up @@ -609,27 +611,65 @@ void ServerControllerView::updateOpticalPoseEstimation(TrackerManager* tracker_m
}
}

// Ignore projections that are occluded BUT always pass atleast 2 biggest projected trackers.
if (!bIsOccluded || projections_found < trackerMgrConfig.occluded_area_ignore_trackers)
if (bWasTracking || bIsVisibleThisUpdate)
{
bOccluded = false;

// If the projection isn't too old (or updated this tick),
// say we have a valid tracked location
if ((bWasTracking && !tracker->getHasUnpublishedState()) || bIsVisibleThisUpdate)
for (int i = 0; i < eCommonBlacklistProjection::MAX_BLACKLIST_PROJECTIONS; ++i)
{
// If this tracker has a valid projection for the controller
// add it to the tracker id list
valid_projection_tracker_ids[projections_found] = tracker_id;
++projections_found;
float x, y, w, h;
if (tracker->getBlacklistProjection(i, x, y, w, h))
{
const float tracker_x = trackerPoseEstimateRef.projection.shape.ellipse.center.x;
const float tracker_y = trackerPoseEstimateRef.projection.shape.ellipse.center.y;

bool bInArea = (tracker_x > x)
&& (tracker_y > y)
&& (tracker_x < x + w)
&& (tracker_y < y + h);

if (bInArea)
{
trackerPoseEstimateRef.blacklistedAreaRec.x = x;
trackerPoseEstimateRef.blacklistedAreaRec.y = y;
trackerPoseEstimateRef.blacklistedAreaRec.w = w;
trackerPoseEstimateRef.blacklistedAreaRec.h = h;

// Flag this pose estimate as invalid
bCurrentlyTracking = true;
bIsBlacklisted = true;
break;
}
}
}
}

// Ignore projections that are occluded BUT always pass atleast 2 biggest projected trackers.
if (bIsBlacklisted)
{
bBlacklisted = true;
}
else
{
bOccluded = true;
bBlacklisted = false;

if (!bIsOccluded || projections_found < trackerMgrConfig.occluded_area_ignore_trackers)
{
bOccluded = false;

// If the projection isn't too old (or updated this tick),
// say we have a valid tracked location
if ((bWasTracking && !tracker->getHasUnpublishedState()) || bIsVisibleThisUpdate)
{
// If this tracker has a valid projection for the controller
// add it to the tracker id list
valid_projection_tracker_ids[projections_found] = tracker_id;
++projections_found;

// Flag this pose estimate as invalid
bCurrentlyTracking = true;
}
}
else
{
bOccluded = true;
}
}
}
}
Expand All @@ -639,6 +679,7 @@ void ServerControllerView::updateOpticalPoseEstimation(TrackerManager* tracker_m
trackerPoseEstimateRef.bValidTimestamps = true;
trackerPoseEstimateRef.bCurrentlyTracking = bCurrentlyTracking;
trackerPoseEstimateRef.bIsOccluded = bOccluded;
trackerPoseEstimateRef.bIsBlacklisted = bBlacklisted;
}

// How we compute the final world pose estimate varies based on
Expand Down Expand Up @@ -2926,7 +2967,7 @@ static void computeSpherePoseForControllerFromMultipleTrackers(
ServerTrackerView::triangulateWorldPosition(
tracker.get(), &screen_location,
other_tracker.get(), &other_screen_location);

// Check how much the trangulation deviates from other trackers.
// Ignore its position if it deviates too much and renew its ROI.
if (pair_count > 0 && cfg.max_tracker_position_deviation > 0.01f)
Expand Down
4 changes: 4 additions & 0 deletions src/psmoveservice/Device/View/ServerControllerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ struct ControllerOpticalPoseEstimation

bool bEnforceNewROI;
bool bIsOccluded;
bool bIsBlacklisted;
CommonDeviceScreenLocation occlusionAreaPos;
float occlusionAreaSize;
CommonDeviceBlacklistProjection blacklistedAreaRec;

inline void clear()
{
Expand All @@ -59,8 +61,10 @@ struct ControllerOpticalPoseEstimation

bEnforceNewROI = false;
bIsOccluded = false;
bIsBlacklisted = false;
occlusionAreaPos.clear();
occlusionAreaSize = 0.f;
blacklistedAreaRec.clear();

memset(&projection, 0, sizeof(CommonDeviceTrackingProjection));
projection.shape_type= eCommonTrackingProjectionType::INVALID_PROJECTION;
Expand Down
36 changes: 34 additions & 2 deletions src/psmoveservice/Device/View/ServerTrackerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,21 @@ class OpenCVBufferState
cv::line(*bgrShmemBuffer, cv::Point(rec.x, rec.y + rec.height - 1), cv::Point(rec.x + rec.width - 1, rec.y), cv::Scalar(0, 0, 255));
}

void
draw_pose_blacklist(CommonDeviceBlacklistProjection proj_rec)
{
cv::Rect rec;
rec.x = static_cast<int>(proj_rec.x);
rec.y = static_cast<int>(proj_rec.y);
rec.width = static_cast<int>(proj_rec.w);
rec.height = static_cast<int>(proj_rec.h);

//Draw occlusion rectangle on bgrShmemBuffer
cv::rectangle(*bgrShmemBuffer, rec, cv::Scalar(0, 0, 255));
cv::line(*bgrShmemBuffer, cv::Point(rec.x, rec.y), cv::Point(rec.x + rec.width - 1, rec.y + rec.height - 1), cv::Scalar(0, 0, 255));
cv::line(*bgrShmemBuffer, cv::Point(rec.x, rec.y + rec.height - 1), cv::Point(rec.x + rec.width - 1, rec.y), cv::Scalar(0, 0, 255));
}

int frameWidth;
int frameHeight;

Expand Down Expand Up @@ -1297,6 +1312,16 @@ void ServerTrackerView::getHMDTrackingColorPreset(
return m_device->getTrackingColorPreset(hmd_id, color, out_preset);
}

void ServerTrackerView::setBlacklistProjection(const int index, const float x, const float y, const float w, const float h)
{
m_device->setBlacklistProjection(index, x, y, w, h);
}

bool ServerTrackerView::getBlacklistProjection(const int index, float &x, float &y, float &w, float &h)
{
return m_device->getBlacklistProjection(index, x, y, w, h);
}

bool
ServerTrackerView::computeProjectionForController(
const ServerControllerView* tracked_controller,
Expand Down Expand Up @@ -1332,16 +1357,18 @@ ServerTrackerView::computeProjectionForController(
const bool bIsTracking = priorPoseEst->bCurrentlyTracking;
const bool bEnforceNewROI = priorPoseEst->bEnforceNewROI;
const bool bIsOccluded = priorPoseEst->bIsOccluded;
const bool bIsBlacklisted = priorPoseEst->bIsBlacklisted;
const CommonDeviceScreenLocation mOcclusionAreaPos = priorPoseEst->occlusionAreaPos;
const float fOcclusionAreaSize = priorPoseEst->occlusionAreaSize;
const CommonDeviceBlacklistProjection mBlacklistedAreaRec = priorPoseEst->blacklistedAreaRec;

cv::Rect2i ROI= computeTrackerROIForPoseProjection(
(bRoiOptimized) ? tracked_controller->getDeviceID() : -1,
bRoiDisabled,
iRoiEdgeOffset,
this,
((bIsTracking || bIsOccluded) && !bEnforceNewROI) ? (tracked_controller->getPoseFilter()) : (nullptr),
((bIsTracking || bIsOccluded) && !bEnforceNewROI) ? (&priorPoseEst->projection) : (nullptr),
((bIsTracking || bIsOccluded || bIsBlacklisted) && !bEnforceNewROI) ? (tracked_controller->getPoseFilter()) : (nullptr),
((bIsTracking || bIsOccluded || bIsBlacklisted) && !bEnforceNewROI) ? (&priorPoseEst->projection) : (nullptr),
tracking_shape);

m_opencv_buffer_state->applyROI(ROI);
Expand Down Expand Up @@ -1481,6 +1508,11 @@ ServerTrackerView::computeProjectionForController(
m_opencv_buffer_state->draw_pose_occlusion(mOcclusionAreaPos, fOcclusionAreaSize);
}

if (bIsBlacklisted)
{
m_opencv_buffer_state->draw_pose_blacklist(mBlacklistedAreaRec);
}


// Throw out the result if the contour we found was too small and
// we were using an ROI less that the size of the full screen
Expand Down
3 changes: 3 additions & 0 deletions src/psmoveservice/Device/View/ServerTrackerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class ServerTrackerView : public ServerDeviceView

void setHMDTrackingColorPreset(const class ServerHMDView *controller, eCommonTrackingColorID color, const CommonHSVColorRange *preset);
void getHMDTrackingColorPreset(const class ServerHMDView *controller, eCommonTrackingColorID color, CommonHSVColorRange *out_preset) const;

void setBlacklistProjection(const int index, const float x, const float y, const float w, const float h);
bool getBlacklistProjection(const int index, float &x, float &y, float &w, float &h);

protected:
bool allocate_device_interface(const class DeviceEnumerator *enumerator) override;
Expand Down
Loading

0 comments on commit c3ada3f

Please sign in to comment.