123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using sl;
- using System;
- public class DetectedObject
- {
- private ObjectDataSDK objectData;
-
-
-
-
- public ObjectDataSDK rawObjectData
- {
- get
- {
- return objectData;
- }
- }
-
-
-
-
- public int id
- {
- get
- {
- return objectData.id;
- }
- }
-
-
-
- public OBJECT_CLASS objectClass
- {
- get
- {
- return objectData.objectClass;
- }
- }
-
-
-
- public OBJECT_SUBCLASS objectSubClass
- {
- get
- {
- return objectData.objectSubClass;
- }
- }
-
-
-
-
-
- public OBJECT_TRACK_STATE trackingState
- {
- get
- {
- return objectData.objectTrackingState;
- }
- }
-
-
-
- public OBJECT_ACTION_STATE actionState
- {
- get
- {
- return objectData.actionState;
- }
- }
-
-
-
-
-
- public float confidence
- {
- get
- {
- return objectData.confidence;
- }
- }
-
-
-
- public ZEDManager detectingZEDManager;
- private Vector3 camPositionAtDetection;
- private Quaternion camRotationAtDetection;
-
-
-
-
- public sl.ZEDMat maskMat;
-
-
-
-
- private Texture2D maskTexture = null;
-
-
-
-
-
- private Texture2D maskTextureFlipped = null;
-
-
-
-
-
-
-
-
-
- public DetectedObject(ObjectDataSDK odata, ZEDManager viewingmanager, Vector3 campos, Quaternion camrot)
- {
- objectData = odata;
- detectingZEDManager = viewingmanager;
- camPositionAtDetection = campos;
- camRotationAtDetection = camrot;
- maskMat = new ZEDMat(odata.mask);
-
- }
-
-
-
-
-
-
-
-
-
-
- public Vector2[] Get2DBoundingBoxCorners_Image(float scaleForCanvasUnityError = 1)
- {
-
- float zedimagewidth = detectingZEDManager.zedCamera.ImageWidth;
- float zedimageheight = detectingZEDManager.zedCamera.ImageHeight;
-
- CalibrationParameters calib = detectingZEDManager.zedCamera.GetCalibrationParameters();
- float cxoffset = zedimagewidth * scaleForCanvasUnityError / 2f - calib.leftCam.cx * scaleForCanvasUnityError;
- float cyoffset = 0 - (zedimageheight / 2f - calib.leftCam.cy);
- Vector2[] flippedYimagecoords = new Vector2[4];
- for (int i = 0; i < 4; i++)
- {
- Vector2 rawcoord;
- rawcoord.x = objectData.imageBoundingBox[i].x * scaleForCanvasUnityError + cxoffset;
- rawcoord.y = detectingZEDManager.zedCamera.ImageHeight - objectData.imageBoundingBox[i].y + cyoffset;
-
- #if UNITY_2018_1_OR_NEWER
-
- rawcoord.y = (rawcoord.y - (zedimageheight / 2f)) * scaleForCanvasUnityError + (zedimageheight / 2f);
- #endif
- flippedYimagecoords[i] = rawcoord;
- }
- return flippedYimagecoords;
- }
-
-
-
-
-
-
-
-
-
-
- public Vector2[] Get2DBoundingBoxCorners_Viewport(float scaleForCanvasUnityError = 1)
- {
- Vector2[] imagecoords = Get2DBoundingBoxCorners_Image(scaleForCanvasUnityError);
- Vector2[] viewportcorners = new Vector2[4];
- for (int i = 0; i < 4; i++)
- {
- viewportcorners[i] = detectingZEDManager.GetLeftCamera().ScreenToViewportPoint(imagecoords[i]);
- }
- return viewportcorners;
- }
-
-
-
-
-
-
-
- public Rect Get2DBoundingBoxRect(float scaleForCanvasUnityError = 1f)
- {
- Vector2[] imagecoords = Get2DBoundingBoxCorners_Image(scaleForCanvasUnityError);
- float width = (imagecoords[1].x - imagecoords[0].x);
- float height = (imagecoords[0].y - imagecoords[3].y);
- Vector2 bottomleftcorner = imagecoords[3];
- return new Rect(bottomleftcorner, new Vector2(width, height));
- }
-
-
-
- public Vector3 Get3DWorldPosition()
- {
-
- float ypos = (localToWorld(objectData.worldBoundingBox[0]).y - localToWorld(objectData.worldBoundingBox[4]).y) / 2f + localToWorld(objectData.worldBoundingBox[4]).y;
- Vector3 transformedroot = localToWorld(objectData.rootWorldPosition);
- return new Vector3(transformedroot.x, ypos, transformedroot.z);
- }
-
-
-
-
-
- public Quaternion Get3DWorldRotation(bool boxesfacecamera)
- {
- Vector3 normal;
- if (boxesfacecamera)
- {
- normal = Get3DWorldPosition() - detectingZEDManager.GetLeftCameraTransform().position;
- }
- else
- {
- normal = camRotationAtDetection * Vector3.forward;
- }
- normal.y = 0;
- return Quaternion.LookRotation(normal, Vector3.up);
- }
-
-
-
-
- public Bounds Get3DWorldBounds()
- {
- Vector3[] worldcorners = objectData.worldBoundingBox;
- Quaternion pitchrot = GetRotationWithoutYaw();
- Vector3 leftbottomback = pitchrot * worldcorners[5];
- Vector3 righttopfront = pitchrot * worldcorners[3];
- float xsize = Mathf.Abs(righttopfront.x - leftbottomback.x);
- float ysize = Mathf.Abs(righttopfront.y - leftbottomback.y);
- float zsize = Mathf.Abs(righttopfront.z - leftbottomback.z);
- return new Bounds(Vector3.zero, new Vector3(xsize, ysize, zsize));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Vector3[] Get3DWorldCorners()
- {
- Vector3[] worldspacecorners = new Vector3[8];
- for (int i = 0; i < 8; i++)
- {
- worldspacecorners[i] = localToWorld(objectData.worldBoundingBox[i]);
- }
- return worldspacecorners;
- }
-
-
-
-
-
-
-
-
- public bool GetMaskTexture(out Texture2D masktex, bool fliponYaxis)
- {
- if (!fliponYaxis)
- {
- if (maskTexture == null)
- {
- IntPtr maskpointer = maskMat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);
- if (maskpointer != IntPtr.Zero)
- {
- maskTexture = ZEDMatToTexture_CPU(maskMat, false);
- }
- }
- }
- else
- {
- if (maskTextureFlipped == null)
- {
- IntPtr maskpointer = maskMat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);
- if (maskpointer != IntPtr.Zero)
- {
- maskTextureFlipped = ZEDMatToTexture_CPU(maskMat, true);
- }
- }
- }
- masktex = fliponYaxis ? maskTextureFlipped : maskTexture;
- return masktex != null;
- }
-
-
-
-
- public void CleanUpTextures()
- {
- if (maskTexture != null)
- {
- GameObject.Destroy(maskTexture);
- }
- if (maskTextureFlipped != null)
- {
- GameObject.Destroy(maskTextureFlipped);
- }
- }
-
-
-
-
-
- private Vector3 localToWorld(Vector3 localPos)
- {
- return camRotationAtDetection * localPos + camPositionAtDetection;
- }
-
-
-
-
- private Quaternion GetRotationWithoutYaw()
- {
- return Quaternion.Euler(camRotationAtDetection.eulerAngles.x, 0f, camRotationAtDetection.eulerAngles.z);
- }
-
-
-
-
- private static Texture2D ZEDMatToTexture_CPU(sl.ZEDMat zedmat, bool flipYcoords = false)
- {
- int width = zedmat.GetWidth();
- int height = zedmat.GetHeight();
-
- IntPtr maskpointer = zedmat.GetPtr(sl.ZEDMat.MEM.MEM_CPU);
- if (maskpointer != IntPtr.Zero && zedmat.IsInit() && width > 0 && height > 0)
- {
- byte[] texbytes = new byte[zedmat.GetStepBytes() * height];
- System.Runtime.InteropServices.Marshal.Copy(maskpointer, texbytes, 0, texbytes.Length);
- if (flipYcoords)
- {
- byte[] flippedbytes = new byte[texbytes.Length];
- int steplength = zedmat.GetWidthBytes();
- for (int i = 0; i < texbytes.Length; i += steplength)
- {
- Array.Copy(texbytes, i, flippedbytes, flippedbytes.Length - i - steplength, steplength);
- }
- texbytes = flippedbytes;
- }
- Texture2D zedtex = new Texture2D(width, height, TextureFormat.Alpha8, false, false);
- zedtex.anisoLevel = 0;
- zedtex.LoadRawTextureData(texbytes);
- zedtex.Apply();
- return zedtex;
- }
- else
- {
- Debug.LogError("Pointer to texture was null - returning null.");
- return null;
- }
- }
- }
|