Skip to content

Commit

Permalink
wip output geometry as a float image stream
Browse files Browse the repository at this point in the history
  • Loading branch information
SoylentGraham committed Jul 6, 2021
1 parent 29cce01 commit 1a3b5a7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Source/ArkitCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Arkit
class TSessionCamera; // arkit session which reads a certain source
class TCaptureParams;

class TGeometryCache;

namespace ArFrameSource
{
enum Type
Expand Down Expand Up @@ -70,10 +72,24 @@ class Arkit::TCaptureParams
bool mOutputSceneDepthSmooth = false;
bool mVerboseDebug = false;
// todo: colour format
bool mOutputAnchorGeometryStream = false; // output anchor geometry triangles as a float-image stream
};



class Arkit::TGeometryCache
{
public:
std::string mUuid;
vec3f mBoundsCenter;
vec3f mBoundsSize;
size_t mPositionCount = 0;
SoyTime mTimestamp;
std::shared_ptr<SoyPixelsImpl> mTrianglePositionsPixels; // floats stored in a pixel buffer. done early to avoid a copy/alloc later
//Array<float> mTrianglePositions;
BufferArray<float,4*4> mLocalToWorld;
};

class Arkit::TFrameDevice : public PopCameraDevice::TDevice
{
public:
Expand All @@ -84,9 +100,15 @@ class Arkit::TFrameDevice : public PopCameraDevice::TDevice
void PushFrame(ARDepthData* DepthData,SoyTime Timestamp,json11::Json::object& Meta,const char* StreamName);
void PushFrame(ARFrame* Frame,ArFrameSource::Type Source);

void PushGeometryFrame(const TGeometryCache& Geometry);
void UpdateGeometry(const std::string& Uuid,std::function<bool(TGeometryCache&)> UpdateGeometry); // callback return true if data changed

SoyTime mPreviousDepthTime;
SoyTime mPreviousFrameTime;
TCaptureParams mParams;

// we keep old geometry, so we can detect is if it's changed
Array<std::shared_ptr<TGeometryCache>> mGeometryCache;
};


Expand Down
81 changes: 80 additions & 1 deletion Source/ArkitCapture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace Arkit
{
const char* GeometryStreamName = "Geometry";
const char* GetColourStreamName(ArFrameSource::Type Source);
};

Expand Down Expand Up @@ -646,9 +647,9 @@ void GetAnchorMeta(ARPlaneAnchor* Anchor, json11::Json::object& Meta,bool Includ
json11::Json::array GeometryPositions;
GetAnchorTriangles( Anchor.geometry, GeometryPositions );
Meta["Triangles"] = GeometryPositions;
}
}
}
}

// return false to not report this anchor
bool GetAnchorMeta(ARAnchor* Anchor, json11::Json::object& Meta,bool IncludeGeometry)
Expand Down Expand Up @@ -692,6 +693,21 @@ bool GetAnchorMeta(ARAnchor* Anchor, json11::Json::object& Meta,bool IncludeGeom
return true;
}

void EnumGeometryAnchors(ARFrame* Frame,std::function<void(ARPlaneAnchor* PlaneAnchor,ARMeshAnchor* MeshAnchor)> Enum)
{
auto EnumAnchor = [&](ARAnchor* Anchor)
{
if ( [Anchor isKindOfClass:[ARPlaneAnchor class]] )
{
Enum( (ARPlaneAnchor*)Anchor, nullptr );
}
if ( [Anchor isKindOfClass:[ARMeshAnchor class]] )
{
Enum( nullptr, (ARMeshAnchor*)Anchor );
}
};
Platform::NSArray_ForEach<ARAnchor*>(Frame.anchors,EnumAnchor);
}

void Avf::GetMeta(ARFrame* Frame,json11::Json::object& Meta,Arkit::TCaptureParams& Params)
{
Expand Down Expand Up @@ -842,9 +858,54 @@ bool GetAnchorMeta(ARAnchor* Anchor, json11::Json::object& Meta,bool IncludeGeom
// todo; get capabilities and reject params here
}

bool UpdateGeomtetry(ARPlaneAnchor* Anchor,Arkit::TGeometryCache& Geometry)
{
Geometry.mBoundsCenter = vec3f( Anchor.center.x, Anchor.center.y, Anchor.center.z );
Geometry.mBoundsSize = vec3f( Anchor.extent.x, Anchor.extent.y, Anchor.extent.z );

if ( Anchor.geometry )
{
auto TriangleCount = Anchor.geometry.triangleCount;
Geometry.mPositionCount = TriangleCount * 3;

}

}

bool UpdateGeomtetry(ARMeshAnchor* Anchor,Arkit::TGeometryCache& Geometry)
{
return false;
}

void Arkit::TFrameDevice::PushFrame(ARFrame* Frame,ArFrameSource::Type Source)
{
auto OnAnchor = [&](ARPlaneAnchor* PlaneAnchor,ARMeshAnchor* MeshAnchor)
{
auto* Anchor = PlaneAnchor ? PlaneAnchor : MeshAnchor;
if ( !Anchor )
return;

auto Uuid = Soy::NSStringToString(Anchor.identifier.UUIDString);
if ( PlaneAnchor )
{
auto Update = [&](TGeometryCache& Geometry)
{
return UpdateGeomtetry( PlaneAnchor, Geometry );
};
this->UpdateGeometry( Uuid, Update );
}

if ( MeshAnchor )
{
auto Update = [&](TGeometryCache& Geometry)
{
return UpdateGeomtetry( MeshAnchor, Geometry );
};
this->UpdateGeometry( Uuid, Update );
}
};
EnumGeometryAnchors(Frame);

auto* ColourStreamName = GetColourStreamName(Source);
auto FrameTime = Soy::Platform::GetTime( Frame.timestamp );
auto CapDepthTime = Soy::Platform::GetTime( Frame.capturedDepthDataTimestamp );
Expand Down Expand Up @@ -949,6 +1010,24 @@ bool GetAnchorMeta(ARAnchor* Anchor, json11::Json::object& Meta,bool IncludeGeom
}


void Arkit::TFrameDevice::PushGeometryFrame(const TGeometryCache& Geometry)
{
Soy::TScopeTimerPrint Timer(__PRETTY_FUNCTION__,5);

float3x3 Transform;
auto StreamName = GeometryStreamName;
std::shared_ptr<TPixelBuffer> Buffer( new TDumbSharedPixelBuffer( Geometry.mTrianglePositionsPixels, Transform ) );

json11::Json::object Meta;
Meta["StreamName"] = StreamName;
Meta["AnchorUuid"] = Geometry.mUuid;
Meta["PositionCount"] = Geometry.mPositionCount;
Meta["LocalToWorld"] = GetJsonArray(Geometry.mLocalToWorld);

PopCameraDevice::TDevice::PushFrame( Buffer, Timestamp, Meta );
}





Expand Down

0 comments on commit 1a3b5a7

Please sign in to comment.