ZEDCamera.cs 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace sl
  7. {
  8. /// <summary>
  9. /// Main interface between Unity and the ZED SDK. Primarily consists of extern calls to the ZED SDK wrapper .dll and
  10. /// low-level logic to process data sent to/received from it.
  11. /// </summary>
  12. /// <remarks>The ZEDManager component creates a ZEDCamera instance in Awake() and handles all initialization.
  13. /// Most ZED functionality can be handled simply in Unity via ZEDManager or other high-level manager components
  14. /// (ZEDSpatialMappingManager, ZEDPlaneDetectionManager, etc.)
  15. /// Textures created by ZEDCamera by CreateTextureImageType() and CreateTextureMeasureType()
  16. /// are updated automatically by the wrapper whenever a new frame is available. They represent a specific kind of
  17. /// output, like left RGB image, or depth relative to the right sensor. As such, you never need more than one texture
  18. /// of each kind, since it can simply be reused by all scripts that need it. Therefore, textures created in ZEDCamera
  19. /// are indexed by their type (Image or Measure) and then by the options of each type. If a script needs a certain kind
  20. /// of output, ZEDCamera will make a new one if it doesn't exist, but refer to the existing one otherwise.</remarks>
  21. ///
  22. public class ZEDCamera
  23. {
  24. /// <summary>
  25. /// Type of textures requested.
  26. /// </summary>
  27. public enum TYPE_VIEW
  28. {
  29. /// <summary>
  30. /// Image-type texture. Human-viewable but loses measurement accuracy.
  31. /// </summary>
  32. RETRIEVE_IMAGE,
  33. /// <summary>
  34. /// Measure-type texture. Preserves measurement accuracy but isn't human-viewable.
  35. /// </summary>
  36. RETRIEVE_MEASURE
  37. }
  38. /// <summary>
  39. /// Information for a requested texture. Stored in the texturesRequested list so DestroyTexture()
  40. /// can be called with the correct arguments in DestroyAllTexture().
  41. /// </summary>
  42. private struct TextureRequested
  43. {
  44. /// <summary>
  45. /// Texture type - 'image' or 'measure.' Assigned using ZEDCamera.TYPE_VIEW.
  46. /// </summary>
  47. public int type;
  48. /// <summary>
  49. /// View mode (left/right eye, depth, etc.) Assigned using ZEDCommon.VIEW.
  50. /// </summary>
  51. public int option;
  52. };
  53. /********* Camera members ********/
  54. /// <summary>
  55. /// DLL name, used for extern calls to the wrapper.
  56. /// </summary>
  57. public const string nameDll = sl.ZEDCommon.NameDLL;
  58. /// <summary>
  59. /// List of all created textures, representing SDK output. Indexed by ints corresponding to its ZEDCamera.TYPE_VIEW
  60. /// and its ZEDCommon.VIEW as you can't have more than one texture for each combination (nor would it be useful to).
  61. /// </summary>
  62. private Dictionary<int, Dictionary<int, Texture2D>> textures;
  63. /// <summary>
  64. /// List of all requested textures. Used for destroying all textures when the camera closes.
  65. /// </summary>
  66. private List<TextureRequested> texturesRequested;
  67. /// <summary>
  68. /// Width of the textures in pixels. Corresponds to the ZED's current resolution setting.
  69. /// </summary>
  70. private int imageWidth;
  71. /// <summary>
  72. /// Width of the images returned by the ZED in pixels. Corresponds to the ZED's current resolution setting.
  73. /// </summary>
  74. public int ImageWidth
  75. {
  76. get
  77. {
  78. return imageWidth;
  79. }
  80. }
  81. /// <summary>
  82. /// Height of the textures in pixels. Corresponds to the ZED's current resolution setting.
  83. /// </summary>
  84. private int imageHeight;
  85. /// <summary>
  86. /// Height of the images returned by the ZED in pixels. Corresponds to the ZED's current resolution setting.
  87. /// </summary>
  88. public int ImageHeight
  89. {
  90. get
  91. {
  92. return imageHeight;
  93. }
  94. }
  95. /// <summary>
  96. /// Projection matrix corresponding to the ZED's camera traits. Useful for lining up virtual cameras with the ZED image.
  97. /// </summary>
  98. private Matrix4x4 projection = new Matrix4x4();
  99. /// <summary>
  100. /// Projection matrix corresponding to the ZED's camera traits. Useful for lining up virtual cameras with the ZED image.
  101. /// </summary>
  102. public Matrix4x4 Projection
  103. {
  104. get
  105. {
  106. return projection;
  107. }
  108. }
  109. /// <summary>
  110. /// True if the ZED SDK is installed.
  111. /// </summary>
  112. private static bool pluginIsReady = true;
  113. /// <summary>
  114. /// Mutex for the image acquisition thread.
  115. /// </summary>
  116. public object grabLock = new object();
  117. /// <summary>
  118. /// Current ZED resolution setting. Set at initialization.
  119. /// </summary>
  120. private RESOLUTION currentResolution;
  121. /// <summary>
  122. /// Callback for c++ debugging. Should not be used in C#.
  123. /// </summary>
  124. private delegate void DebugCallback(string message);
  125. /// <summary>
  126. /// Desired FPS from the ZED camera. This is the maximum FPS for the ZED's current
  127. /// resolution unless a lower setting was specified in Init().
  128. /// Maximum values are bound by the ZED's output, not system performance.
  129. /// </summary>
  130. private uint fpsMax = 60; //Defaults to HD720 resolution's output.
  131. /// <summary>
  132. /// Desired FPS from the ZED camera. This is the maximum FPS for the ZED's current
  133. /// resolution unless a lower setting was specified in Init().
  134. /// Maximum values are bound by the ZED's output, not system performance.
  135. /// </summary>
  136. public float GetRequestedCameraFPS()
  137. {
  138. return fpsMax;
  139. }
  140. /// <summary>
  141. /// Holds camera settings like brightness/contrast, gain/exposure, etc.
  142. /// </summary>
  143. private ZEDCameraSettings cameraSettingsManager = new ZEDCameraSettings();
  144. /// <summary>
  145. /// Camera's stereo baseline (distance between the cameras). Extracted from calibration files.
  146. /// </summary>
  147. private float baseline = 0.0f;
  148. /// <summary>
  149. /// Camera's stereo baseline (distance between the cameras). Extracted from calibration files.
  150. /// </summary>
  151. public float Baseline
  152. {
  153. get { return baseline; }
  154. }
  155. /// <summary>
  156. /// ZED's current horizontal field of view in degrees.
  157. /// </summary>
  158. private float fov_H = 0.0f;
  159. /// <summary>
  160. /// ZED's current vertical field of view in degrees.
  161. /// </summary>
  162. private float fov_V = 0.0f;
  163. /// <summary>
  164. /// ZED's current horizontal field of view in degrees.
  165. /// </summary>
  166. public float HorizontalFieldOfView
  167. {
  168. get { return fov_H; }
  169. }
  170. /// <summary>
  171. /// ZED's current vertical field of view in degrees.
  172. /// </summary>
  173. public float VerticalFieldOfView
  174. {
  175. get { return fov_V; }
  176. }
  177. /// <summary>
  178. /// Structure containing information about all the sensors available in the current device
  179. /// </summary>
  180. private SensorsConfiguration sensorsConfiguration;
  181. /// <summary>
  182. /// Stereo parameters for current ZED camera prior to rectification (distorted).
  183. /// </summary>
  184. private CalibrationParameters calibrationParametersRaw;
  185. /// <summary>
  186. /// Stereo parameters for current ZED camera after rectification (undistorted).
  187. /// </summary>
  188. private CalibrationParameters calibrationParametersRectified;
  189. /// <summary>
  190. /// Camera model - ZED or ZED Mini.
  191. /// </summary>
  192. private sl.MODEL cameraModel;
  193. /// <summary>
  194. /// Whether the camera has been successfully initialized.
  195. /// </summary>
  196. private bool cameraReady = false;
  197. /// <summary>
  198. /// Structure containing information about all the sensors available in the current device
  199. /// </summary>
  200. public SensorsConfiguration SensorsConfiguration
  201. {
  202. get { return sensorsConfiguration; }
  203. }
  204. /// <summary>
  205. /// Stereo parameters for current ZED camera prior to rectification (distorted).
  206. /// </summary>
  207. public CalibrationParameters CalibrationParametersRaw
  208. {
  209. get { return calibrationParametersRaw; }
  210. }
  211. /// <summary>
  212. /// Stereo parameters for current ZED camera after rectification (undistorted).
  213. /// </summary>
  214. public CalibrationParameters CalibrationParametersRectified
  215. {
  216. get { return calibrationParametersRectified; }
  217. }
  218. /// <summary>
  219. /// Camera model - ZED or ZED Mini.
  220. /// </summary>
  221. public sl.MODEL CameraModel
  222. {
  223. get { return cameraModel; }
  224. }
  225. /// <summary>
  226. /// Whether the camera has been successfully initialized.
  227. /// </summary>
  228. public bool IsCameraReady
  229. {
  230. get { return cameraReady; }
  231. }
  232. /// <summary>
  233. /// Whether the current device (ZED or ZED Mini) should be used for pass-through AR.
  234. /// True if using ZED Mini, false if using ZED. </summary><remarks>Note: the plugin will allow using the original ZED
  235. /// for pass-through but it will feel quite uncomfortable due to the baseline.</remarks>
  236. public bool IsHmdCompatible
  237. {
  238. get { return cameraModel == sl.MODEL.ZED_M; }
  239. }
  240. /// <summary>
  241. /// Camera ID (for multiple cameras)
  242. /// </summary>
  243. public int CameraID = 0;
  244. /// <summary>
  245. /// Layer that the ZED can't see, but overlay cameras created by ZEDMeshRenderer and ZEDPlaneRenderer can.
  246. /// </summary>
  247. //int tagInvisibleToZED = 16;
  248. /// <summary>
  249. /// Layer that the ZED can't see, but overlay cameras created by ZEDMeshRenderer and ZEDPlaneRenderer can.
  250. /// </summary>
  251. public int TagInvisibleToZED
  252. {
  253. get { return ZEDLayers.tagInvisibleToZED; }
  254. }
  255. public const int brightnessDefault = 4;
  256. public const int contrastDefault = 4;
  257. public const int hueDefault = 0;
  258. public const int saturationDefault = 4;
  259. public const int sharpnessDefault = 3;
  260. public const int gammaDefault = 5;
  261. public const int whitebalanceDefault = 2600;
  262. #region DLL Calls
  263. /// <summary>
  264. /// Current Plugin Version.
  265. /// </summary>
  266. public static readonly System.Version PluginVersion = new System.Version(3, 7, 1);
  267. /******** DLL members ***********/
  268. [DllImport(nameDll, EntryPoint = "GetRenderEventFunc")]
  269. private static extern IntPtr GetRenderEventFunc();
  270. [DllImport(nameDll, EntryPoint = "sl_register_callback_debuger")]
  271. private static extern void dllz_register_callback_debuger(DebugCallback callback);
  272. /*
  273. * Utils function.
  274. */
  275. [DllImport(nameDll, EntryPoint = "sl_unload_all_instances")]
  276. private static extern void dllz_unload_all_instances();
  277. [DllImport(nameDll, EntryPoint = "sl_unload_instance")]
  278. private static extern void dllz_unload_instance(int id);
  279. [DllImport(nameDll, EntryPoint = "sl_find_usb_device")]
  280. private static extern bool dllz_find_usb_device(USB_DEVICE dev);
  281. [DllImport(nameDll, EntryPoint = "sl_generate_unique_id")]
  282. private static extern int dllz_generate_unique_id([In, Out] byte[] id);
  283. /*
  284. * Create functions
  285. */
  286. [DllImport(nameDll, EntryPoint = "sl_create_camera")]
  287. private static extern bool dllz_create_camera(int cameraID);
  288. /*
  289. * Opening function (Opens camera and creates textures).
  290. */
  291. [DllImport(nameDll, EntryPoint = "sl_open_camera")]
  292. private static extern int dllz_open(int cameraID, ref dll_initParameters parameters, System.Text.StringBuilder svoPath, System.Text.StringBuilder ipStream, int portStream, System.Text.StringBuilder output, System.Text.StringBuilder opt_settings_path, System.Text.StringBuilder opencv_calib_path);
  293. /*
  294. * Close function.
  295. */
  296. [DllImport(nameDll, EntryPoint = "sl_close_camera")]
  297. private static extern void dllz_close(int cameraID);
  298. /*
  299. * Grab function.
  300. */
  301. [DllImport(nameDll, EntryPoint = "sl_grab")]
  302. private static extern int dllz_grab(int cameraID, ref sl.RuntimeParameters runtimeParameters);
  303. /*
  304. * GetDeviceList function
  305. */
  306. [DllImport(nameDll, EntryPoint = "sl_get_device_list")]
  307. private static extern void dllz_get_device_list(sl.DeviceProperties[] deviceList, out int nbDevices);
  308. /*
  309. * Reboot function.
  310. */
  311. [DllImport(nameDll, EntryPoint = "sl_reboot")]
  312. private static extern int dllz_reboot(int serialNumber, bool fullReboot);
  313. /*
  314. * Recording functions.
  315. */
  316. [DllImport(nameDll, EntryPoint = "sl_enable_recording")]
  317. private static extern int dllz_enable_recording(int cameraID, System.Text.StringBuilder video_filename, int compresssionMode,int bitrate,int target_fps,bool transcode);
  318. [DllImport(nameDll, EntryPoint = "sl_disable_recording")]
  319. private static extern bool dllz_disable_recording(int cameraID);
  320. /*
  321. * Texturing functions.
  322. */
  323. [DllImport(nameDll, EntryPoint = "sl_retrieve_textures")]
  324. private static extern void dllz_retrieve_textures(int cameraID);
  325. [DllImport(nameDll, EntryPoint = "sl_get_updated_textures_timestamp")]
  326. private static extern ulong dllz_get_updated_textures_timestamp(int cameraID);
  327. [DllImport(nameDll, EntryPoint = "sl_swap_textures")]
  328. private static extern void dllz_swap_textures(int cameraID);
  329. [DllImport(nameDll, EntryPoint = "sl_register_texture_image_type")]
  330. private static extern int dllz_register_texture_image_type(int cameraID, int option, IntPtr id, int width, int height);
  331. [DllImport(nameDll, EntryPoint = "sl_register_texture_measure_type")]
  332. private static extern int dllz_register_texture_measure_type(int cameraID, int option, IntPtr id, int width, int height);
  333. [DllImport(nameDll, EntryPoint = "sl_unregister_texture_measure_type")]
  334. private static extern int dllz_unregister_texture_measure_type(int cameraID, int option);
  335. [DllImport(nameDll, EntryPoint = "sl_unregister_texture_image_type")]
  336. private static extern int dllz_unregister_texture_image_type(int cameraID, int option);
  337. [DllImport(nameDll, EntryPoint = "sl_get_copy_mat_texture_image_type")]
  338. private static extern IntPtr dllz_get_copy_mat_texture_image_type(int cameraID, int option);
  339. [DllImport(nameDll, EntryPoint = "sl_get_copy_mat_texture_measure_type")]
  340. private static extern IntPtr dllz_get_copy_mat_texture_measure_type(int cameraID, int option);
  341. /*
  342. * Camera control functions.
  343. */
  344. [DllImport(nameDll, EntryPoint = "sl_set_video_settings")]
  345. private static extern void dllz_set_video_settings(int id, int mode, int value);
  346. [DllImport(nameDll, EntryPoint = "sl_get_video_settings")]
  347. private static extern int dllz_get_video_settings(int id, int mode);
  348. [DllImport(nameDll, EntryPoint = "sl_set_roi_for_aec_agc")]
  349. private static extern int dllz_set_roi_for_aec_agc(int id, int side, iRect roi,bool reset);
  350. [DllImport(nameDll, EntryPoint = "sl_get_roi_for_aec_agc")]
  351. private static extern int dllz_get_roi_for_aec_agc(int id, int side, ref iRect roi);
  352. [DllImport(nameDll, EntryPoint = "sl_get_input_type")]
  353. private static extern int dllz_get_input_type(int cameraID);
  354. [DllImport(nameDll, EntryPoint = "sl_get_camera_fps")]
  355. private static extern float dllz_get_camera_fps(int cameraID);
  356. [DllImport(nameDll, EntryPoint = "sl_get_width")]
  357. private static extern int dllz_get_width(int cameraID);
  358. [DllImport(nameDll, EntryPoint = "sl_get_height")]
  359. private static extern int dllz_get_height(int cameraID);
  360. [DllImport(nameDll, EntryPoint = "sl_update_self_calibration")]
  361. private static extern void dllz_update_self_calibration(int cameraID);
  362. [DllImport(nameDll, EntryPoint = "sl_get_calibration_parameters")]
  363. private static extern IntPtr dllz_get_calibration_parameters(int cameraID, bool raw);
  364. [DllImport(nameDll, EntryPoint = "sl_get_sensors_configuration")]
  365. private static extern IntPtr dllz_get_sensors_configuration(int cameraID);
  366. [DllImport(nameDll, EntryPoint = "sl_get_camera_model")]
  367. private static extern int dllz_get_camera_model(int cameraID);
  368. [DllImport(nameDll, EntryPoint = "sl_get_camera_firmware")]
  369. private static extern int dllz_get_camera_firmware(int cameraID);
  370. [DllImport(nameDll, EntryPoint = "sl_get_sensors_firmware")]
  371. private static extern int dllz_get_sensors_firmware(int cameraID);
  372. [DllImport(nameDll, EntryPoint = "sl_get_zed_serial")]
  373. private static extern int dllz_get_zed_serial(int cameraID);
  374. [DllImport(nameDll, EntryPoint = "sl_get_camera_imu_transform")]
  375. private static extern void dllz_get_camera_imu_transform(int cameraID, out Vector3 translation, out Quaternion rotation);
  376. [DllImport(nameDll, EntryPoint = "sl_get_camera_timestamp")]
  377. private static extern ulong dllz_get_image_timestamp(int cameraID);
  378. [DllImport(nameDll, EntryPoint = "sl_get_current_Timestamp")]
  379. private static extern ulong dllz_get_current_timestamp(int cameraID);
  380. [DllImport(nameDll, EntryPoint = "sl_get_frame_dropped_count")]
  381. private static extern uint dllz_get_frame_dropped_count(int cameraID);
  382. [DllImport(nameDll, EntryPoint = "sl_get_frame_dropped_percent")]
  383. private static extern float dllz_get_frame_dropped_percent(int cameraID);
  384. /*
  385. * SVO control functions.
  386. */
  387. [DllImport(nameDll, EntryPoint = "sl_set_svo_position")]
  388. private static extern void dllz_set_svo_position(int cameraID, int frame);
  389. [DllImport(nameDll, EntryPoint = "sl_get_svo_number_of_frames")]
  390. private static extern int dllz_get_svo_number_of_frames(int cameraID);
  391. [DllImport(nameDll, EntryPoint = "sl_get_svo_position")]
  392. private static extern int dllz_get_svo_position(int cameraID);
  393. /*
  394. * Depth Sensing utils functions.
  395. */
  396. /* Removed as of ZED SDK v3.0.
  397. [DllImport(nameDll, EntryPoint = "set_confidence_threshold")]
  398. private static extern void dllz_set_confidence_threshold(int cameraID, int threshold);
  399. [DllImport(nameDll, EntryPoint = "set_depth_max_range_value")]
  400. private static extern void dllz_set_depth_max_range_value(int cameraID, float distanceMax);
  401. */
  402. [DllImport(nameDll, EntryPoint = "sl_get_confidence_threshold")]
  403. private static extern int dllz_get_confidence_threshold(int cameraID);
  404. [DllImport(nameDll, EntryPoint = "sl_get_depth_max_range_value")]
  405. private static extern float dllz_get_depth_max_range_value(int cameraID);
  406. [DllImport(nameDll, EntryPoint = "sl_get_depth_value")]
  407. private static extern float dllz_get_depth_value(int cameraID, uint x, uint y);
  408. [DllImport(nameDll, EntryPoint = "sl_get_distance_value")]
  409. private static extern float dllz_get_distance_value(int cameraID, uint x, uint y);
  410. [DllImport(nameDll, EntryPoint = "sl_get_normal_value")]
  411. private static extern bool dllz_get_normal_value(int cameraID, uint x, uint y, out Vector4 value);
  412. [DllImport(nameDll, EntryPoint = "sl_get_xyz_value")]
  413. private static extern bool dllz_get_xyz_value(int cameraID, uint x, uint y, out Vector4 value);
  414. [DllImport(nameDll, EntryPoint = "sl_get_depth_min_range_value")]
  415. private static extern float dllz_get_depth_min_range_value(int cameraID);
  416. /*
  417. * Motion Tracking functions.
  418. */
  419. [DllImport(nameDll, EntryPoint = "sl_enable_positional_tracking_unity")]
  420. private static extern int dllz_enable_tracking(int cameraID, ref Quaternion quat, ref Vector3 vec, bool enableSpatialMemory = false, bool enablePoseSmoothing = false, bool enableFloorAlignment = false,
  421. bool trackingIsStatic = false, bool enableIMUFusion = true, System.Text.StringBuilder areaFilePath = null);
  422. [DllImport(nameDll, EntryPoint = "sl_disable_positional_tracking")]
  423. private static extern void dllz_disable_tracking(int cameraID, System.Text.StringBuilder path);
  424. [DllImport(nameDll, EntryPoint = "sl_save_area_map")]
  425. private static extern int dllz_save_current_area(int cameraID, System.Text.StringBuilder path);
  426. [DllImport(nameDll, EntryPoint = "sl_get_position_data")]
  427. private static extern int dllz_get_position_data(int cameraID, ref Pose pose, int reference_frame);
  428. [DllImport(nameDll, EntryPoint = "sl_get_position")]
  429. private static extern int dllz_get_position(int cameraID, ref Quaternion quat, ref Vector3 vec, int reference_frame);
  430. [DllImport(nameDll, EntryPoint = "sl_get_position_at_target_frame")]
  431. private static extern int dllz_get_position_at_target_frame(int cameraID, ref Quaternion quaternion, ref Vector3 translation, ref Quaternion targetQuaternion, ref Vector3 targetTranslation, int reference_frame);
  432. [DllImport(nameDll, EntryPoint = "sl_transform_pose")]
  433. private static extern void dllz_transform_pose(ref Quaternion quaternion, ref Vector3 translation, ref Quaternion targetQuaternion, ref Vector3 targetTranslation);
  434. [DllImport(nameDll, EntryPoint = "sl_reset_positional_tracking")]
  435. private static extern int dllz_reset_tracking(int cameraID, Quaternion rotation, Vector3 translation);
  436. [DllImport(nameDll, EntryPoint = "sl_reset_tracking_with_offset")]
  437. private static extern int dllz_reset_tracking_with_offset(int cameraID, Quaternion rotation, Vector3 translation, Quaternion offsetQuaternion, Vector3 offsetTranslation);
  438. [DllImport(nameDll, EntryPoint = "sl_estimate_initial_position")]
  439. private static extern int dllz_estimate_initial_position(int cameraID, ref Quaternion quaternion, ref Vector3 translation, int countSuccess, int countTimeout);
  440. [DllImport(nameDll, EntryPoint = "sl_set_imu_prior_orientation")]
  441. private static extern int dllz_set_imu_prior_orientation(int cameraID, Quaternion rotation);
  442. [DllImport(nameDll, EntryPoint = "sl_get_internal_imu_orientation")]
  443. private static extern int dllz_get_internal_imu_orientation(int cameraID, ref Quaternion rotation, int reference_time);
  444. [DllImport(nameDll, EntryPoint = "sl_get_internal_sensors_data")]
  445. private static extern int dllz_get_internal_sensors_data(int cameraID, ref SensorsData imuData, int reference_time);
  446. [DllImport(nameDll, EntryPoint = "sl_get_area_export_state")]
  447. private static extern int dllz_get_area_export_state(int cameraID);
  448. /*
  449. * Spatial Mapping functions.
  450. */
  451. [DllImport(nameDll, EntryPoint = "sl_enable_spatial_mapping_unity")]
  452. private static extern int dllz_enable_spatial_mapping(int cameraID, int type, float resolution_meter, float max_range_meter, int saveTexture,int max_memory_usage);
  453. [DllImport(nameDll, EntryPoint = "sl_disable_spatial_mapping")]
  454. private static extern void dllz_disable_spatial_mapping(int cameraID);
  455. [DllImport(nameDll, EntryPoint = "sl_pause_spatial_mapping")]
  456. private static extern void dllz_pause_spatial_mapping(int cameraID, bool status);
  457. [DllImport(nameDll, EntryPoint = "sl_request_mesh_async")]
  458. private static extern void dllz_request_mesh_async(int cameraID);
  459. [DllImport(nameDll, EntryPoint = "sl_get_mesh_request_status_async")]
  460. private static extern int dllz_get_mesh_request_status_async(int cameraID);
  461. [DllImport(nameDll, EntryPoint = "sl_update_mesh")]
  462. private static extern int dllz_update_mesh(int cameraID, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);
  463. [DllImport(nameDll, EntryPoint = "sl_retrieve_mesh")]
  464. private static extern int dllz_retrieve_mesh(int cameraID, Vector3[] vertices, int[] triangles, int nbSubmesh, Vector2[] uvs, IntPtr textures);
  465. [DllImport(nameDll, EntryPoint = "sl_update_fused_point_cloud")]
  466. private static extern int dllz_update_fused_point_cloud(int cameraID, ref int pbPoints);
  467. [DllImport(nameDll, EntryPoint = "sl_retrieve_fused_point_cloud")]
  468. private static extern int dllz_retrieve_fused_point_cloud(int cameraID, Vector4[] points);
  469. [DllImport(nameDll, EntryPoint = "sl_save_mesh")]
  470. private static extern bool dllz_save_mesh(int cameraID, string filename, MESH_FILE_FORMAT format);
  471. [DllImport(nameDll, EntryPoint = "sl_save_point_cloud")]
  472. private static extern bool dllz_save_point_cloud(int cameraID, string filename, MESH_FILE_FORMAT format);
  473. [DllImport(nameDll, EntryPoint = "sl_load_mesh")]
  474. private static extern bool dllz_load_mesh(int cameraID, string filename, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbMaxSubmesh, int[] textureSize = null);
  475. [DllImport(nameDll, EntryPoint = "sl_apply_texture")]
  476. private static extern bool dllz_apply_texture(int cameraID, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int[] textureSize, int nbSubmesh);
  477. [DllImport(nameDll, EntryPoint = "sl_filter_mesh")]
  478. private static extern bool dllz_filter_mesh(int cameraID, FILTER meshFilter, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);
  479. [DllImport(nameDll, EntryPoint = "sl_get_spatial_mapping_state")]
  480. private static extern int dllz_get_spatial_mapping_state(int cameraID);
  481. [DllImport(nameDll, EntryPoint = "sl_spatial_mapping_merge_chunks")]
  482. private static extern void dllz_spatial_mapping_merge_chunks(int cameraID, int numberFaces, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh);
  483. [DllImport(nameDll, EntryPoint = "sl_spatial_mapping_get_gravity_estimation")]
  484. private static extern void dllz_spatial_mapping_get_gravity_estimation(int cameraID, ref Vector3 v);
  485. /*
  486. * Plane Detection functions (starting v2.4)
  487. */
  488. [DllImport(nameDll, EntryPoint = "sl_find_floor_plane")]
  489. private static extern IntPtr dllz_find_floor_plane(int cameraID, out Quaternion rotation, out Vector3 translation, Quaternion priorQuaternion, Vector3 priorTranslation);
  490. [DllImport(nameDll, EntryPoint = "sl_find_plane_at_hit")]
  491. private static extern IntPtr dllz_find_plane_at_hit(int cameraID, Vector2 HitPixel, bool refine);
  492. [DllImport(nameDll, EntryPoint = "sl_convert_floorplane_to_mesh")]
  493. private static extern int dllz_convert_floorplane_to_mesh(int cameraID, Vector3[] vertices, int[] triangles, out int numVertices, out int numTriangles);
  494. [DllImport(nameDll, EntryPoint = "sl_convert_hitplane_to_mesh")]
  495. private static extern int dllz_convert_hitplane_to_mesh(int cameraID, Vector3[] vertices, int[] triangles, out int numVertices, out int numTriangles);
  496. /*
  497. * Streaming Module functions (starting v2.8)
  498. */
  499. [DllImport(nameDll, EntryPoint = "sl_enable_streaming")]
  500. private static extern int dllz_enable_streaming(int cameraID, sl.STREAMING_CODEC codec, uint bitrate, ushort port, int gopSize, int adaptativeBitrate,int chunk_size,int target_fps);
  501. [DllImport(nameDll, EntryPoint = "sl_is_streaming_enabled")]
  502. private static extern int dllz_is_streaming_enabled(int cameraID);
  503. [DllImport(nameDll, EntryPoint = "sl_disable_streaming")]
  504. private static extern void dllz_disable_streaming(int cameraID);
  505. /*
  506. * Objects Detection functions (starting v3.0)
  507. */
  508. [DllImport(nameDll, EntryPoint = "sl_check_AI_model_status")]
  509. private static extern IntPtr dllz_check_AI_model_status(AI_MODELS model, int gpu_id);
  510. [DllImport(nameDll, EntryPoint = "sl_optimize_AI_model")]
  511. private static extern int dllz_optimize_AI_model(AI_MODELS model, int gpu_id);
  512. [DllImport(nameDll, EntryPoint = "sl_enable_objects_detection")]
  513. private static extern int dllz_enable_objects_detection(int cameraID, ref dll_ObjectDetectionParameters od_params);
  514. [DllImport(nameDll, EntryPoint = "sl_disable_objects_detection")]
  515. private static extern void dllz_disable_objects_detection(int cameraID);
  516. [DllImport(nameDll, EntryPoint = "sl_pause_objects_detection")]
  517. private static extern void dllz_pause_objects_detection(int cameraID, bool status);
  518. [DllImport(nameDll, EntryPoint = "sl_ingest_custom_box_objects")]
  519. private static extern int dllz_ingest_custom_box_objects(int cameraID, int nb_objects, CustomBoxObjectData[] objects_in);
  520. [DllImport(nameDll, EntryPoint = "sl_retrieve_objects")]
  521. private static extern int dllz_retrieve_objects_data(int cameraID, ref dll_ObjectDetectionRuntimeParameters od_params, ref ObjectsFrameSDK objFrame);
  522. [DllImport(nameDll, EntryPoint = "sl_update_objects_batch")]
  523. private static extern int dllz_update_objects_batch(int cameraID, out int nbBatches);
  524. [DllImport(nameDll, EntryPoint = "sl_get_objects_batch")]
  525. private static extern int dllz_get_objects_batch_data(int cameraID, int batch_index, ref int numData, ref int id, ref OBJECT_CLASS label, ref OBJECT_SUBCLASS sublabel, ref TRACKING_STATE trackingState,
  526. [In, Out] Vector3[] position, [In, Out] float[,] positionCovariances, [In, Out] Vector3[] velocities, [In, Out] ulong[] timestamps, [In, Out] Vector2[,] boundingBoxes2D, [In, Out] Vector3[,] boundingBoxes,
  527. [In, Out] float[] confidences, [In, Out] OBJECT_ACTION_STATE[] actionStates, [In, Out] Vector2[,] keypoints2D, [In, Out] Vector3[,] keypoints, [In, Out] Vector2[,] headBoundingBoxes2D, [In, Out] Vector3[,] headBoundingBoxes, [In, Out] Vector3[] headPositions,
  528. [In, Out] float[,] keypointsConfidences);
  529. /*
  530. * Save utils function
  531. */
  532. [DllImport(nameDll, EntryPoint = "sl_save_current_image")]
  533. private static extern int dllz_save_current_image(int cameraID, VIEW view,string filename);
  534. [DllImport(nameDll, EntryPoint = "sl_save_current_depth")]
  535. private static extern int dllz_save_current_depth(int cameraID, int side, string filename);
  536. [DllImport(nameDll, EntryPoint = "sl_save_current_point_cloud")]
  537. private static extern int dllz_save_current_point_cloud(int cameraID, int side, string filename);
  538. /*
  539. * Specific plugin functions
  540. */
  541. [DllImport(nameDll, EntryPoint = "sl_check_plugin")]
  542. private static extern int dllz_check_plugin(int major, int minor);
  543. [DllImport(nameDll, EntryPoint = "sl_get_sdk_version")]
  544. private static extern IntPtr dllz_get_sdk_version();
  545. [DllImport(nameDll, EntryPoint = "sl_compute_offset")]
  546. private static extern void dllz_compute_offset(float[] A, float[] B, int nbVectors, float[] C);
  547. [DllImport(nameDll, EntryPoint = "sl_compute_optical_center_offsets")]
  548. private static extern System.IntPtr dllz_compute_optical_center_offsets(ref Vector4 calibLeft, ref Vector4 calibRight, int width, int height, float planeDistance);
  549. /*
  550. * Retreieves used by mat
  551. */
  552. [DllImport(nameDll, EntryPoint = "sl_retrieve_measure")]
  553. private static extern int dllz_retrieve_measure(int cameraID, System.IntPtr ptr, int type, int mem, int width, int height);
  554. [DllImport(nameDll, EntryPoint = "sl_retrieve_image")]
  555. private static extern int dllz_retrieve_image(int cameraID, System.IntPtr ptr, int type, int mem, int width, int height);
  556. #endregion
  557. public static void UnloadPlugin()
  558. {
  559. dllz_unload_all_instances();
  560. }
  561. public static void UnloadInstance(int id)
  562. {
  563. dllz_unload_instance(id);
  564. }
  565. public static void ComputeOffset(float[] A, float[] B, int nbVectors, ref Quaternion rotation, ref Vector3 translation)
  566. {
  567. float[] C = new float[16];
  568. if (A.Length != 4 * nbVectors || B.Length != 4 * nbVectors || C.Length != 16) return;
  569. dllz_compute_offset(A, B, nbVectors, C);
  570. Matrix4x4 m = Matrix4x4.identity;
  571. Float2Matrix(ref m, C);
  572. rotation = Matrix4ToQuaternion(m);
  573. Vector4 t = m.GetColumn(3);
  574. translation.x = t.x;
  575. translation.y = t.y;
  576. translation.z = t.z;
  577. }
  578. /// <summary>
  579. /// Return a string from a pointer to a char. Used in GetSDKVersion().
  580. /// </summary>
  581. /// <param name="ptr">Pointer to a char.</param>
  582. /// <returns>The char as a string.</returns>
  583. private static string PtrToStringUtf8(IntPtr ptr)
  584. {
  585. if (ptr == IntPtr.Zero)
  586. {
  587. return "";
  588. }
  589. int len = 0;
  590. while (Marshal.ReadByte(ptr, len) != 0)
  591. len++;
  592. if (len == 0)
  593. {
  594. return "";
  595. }
  596. byte[] array = new byte[len];
  597. Marshal.Copy(ptr, array, 0, len);
  598. return System.Text.Encoding.ASCII.GetString(array);
  599. }
  600. /// <summary>
  601. /// Displays a console message. Used to display C++ SDK messages in Unity's console.
  602. /// </summary>
  603. /// <param name="message"></param>
  604. private static void DebugMethod(string message)
  605. {
  606. Debug.Log("[ZED plugin]: " + message);
  607. }
  608. /// <summary>
  609. /// Convert a pointer to a char into an array of bytes. Used to send file names to SDK for SVO recording.
  610. /// </summary>
  611. /// <param name="ptr">Pointer to a char.</param>
  612. /// <returns>The array.</returns>
  613. private static byte[] StringUtf8ToByte(string str)
  614. {
  615. byte[] array = System.Text.Encoding.ASCII.GetBytes(str);
  616. return array;
  617. }
  618. /// <summary>
  619. /// Gets the max FPS for each resolution setting. Higher FPS will cause lower GPU performance.
  620. /// </summary>
  621. /// <param name="reso"></param>
  622. /// <returns>The resolution</returns>
  623. static private uint GetFpsForResolution(RESOLUTION reso)
  624. {
  625. if (reso == RESOLUTION.HD1080) return 30;
  626. else if (reso == RESOLUTION.HD2K) return 15;
  627. else if (reso == RESOLUTION.HD720) return 60;
  628. else if (reso == RESOLUTION.VGA) return 100;
  629. return 30;
  630. }
  631. /// <summary>
  632. /// Get a quaternion from a matrix with a minimum size of 3x3.
  633. /// </summary>
  634. /// <param name="m">The matrix.</param>
  635. /// <returns>A quaternion which contains the matrix's rotation.</returns>
  636. public static Quaternion Matrix4ToQuaternion(Matrix4x4 m)
  637. {
  638. Quaternion q = new Quaternion();
  639. q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
  640. q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
  641. q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
  642. q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
  643. q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
  644. q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
  645. q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
  646. return q;
  647. }
  648. /// <summary>
  649. /// Performs a rigid transform.
  650. /// </summary>
  651. /// <param name="quaternion"></param>
  652. /// <param name="translation"></param>
  653. /// <param name="targetQuaternion"></param>
  654. /// <param name="targetTranslation"></param>
  655. public static void TransformPose(ref Quaternion quaternion, ref Vector3 translation, ref Quaternion targetQuaternion, ref Vector3 targetTranslation)
  656. {
  657. dllz_transform_pose(ref quaternion, ref translation, ref targetQuaternion, ref targetTranslation);
  658. }
  659. public static string GenerateUniqueID()
  660. {
  661. byte[] array = new byte[37];
  662. int size = dllz_generate_unique_id(array);
  663. return new string(System.Text.Encoding.ASCII.GetChars(array));
  664. }
  665. /// <summary>
  666. /// Checks that the ZED plugin's dependencies are installed.
  667. /// </summary>
  668. public static bool CheckPlugin()
  669. {
  670. try
  671. {
  672. int res = dllz_check_plugin(PluginVersion.Major, PluginVersion.Minor);
  673. if (res!= 0)
  674. {
  675. //0 = installed SDK is compatible with plugin. 1 otherwise.
  676. Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE));
  677. return false;
  678. }
  679. }
  680. catch (DllNotFoundException) //In case could not resolve the dll/.so
  681. {
  682. Debug.Log("DllNotFoundException");
  683. Debug.LogError(ZEDLogMessage.Error2Str(ZEDLogMessage.ERROR.SDK_DEPENDENCIES_ISSUE));
  684. return false;
  685. }
  686. pluginIsReady = true;
  687. return true;
  688. }
  689. /// <summary>
  690. /// Checks if the USB device of a 'brand' type is connected. Used to check if a VR headset are connected
  691. /// for display in ZEDManager's Inspector.
  692. /// </summary>
  693. /// <returns><c>True</c>, if USB device connected was found, <c>false</c> otherwise.</returns>
  694. /// <param name="Device brand.">Type.</param>
  695. public static bool CheckUSBDeviceConnected(USB_DEVICE Type)
  696. {
  697. if (dllz_find_usb_device(Type))
  698. return true;
  699. else
  700. return false;
  701. }
  702. /// <summary>
  703. /// Private constructor. Initializes lists to hold references to textures and texture requests.
  704. /// </summary>
  705. public ZEDCamera()
  706. {
  707. //Create the textures
  708. textures = new Dictionary<int, Dictionary<int, Texture2D>>();
  709. texturesRequested = new List<TextureRequested>();
  710. }
  711. /// <summary>
  712. /// Create a camera in Live mode (input comes from a connected ZED device, not SVO playback).
  713. /// </summary>
  714. /// <param name="verbose">True to create detailed log file of SDK calls at the cost of performance.</param>
  715. public bool CreateCamera(int cameraID, bool verbose)
  716. {
  717. string infoSystem = SystemInfo.graphicsDeviceType.ToString().ToUpper();
  718. if (!infoSystem.Equals("DIRECT3D11") && !infoSystem.Equals("OPENGLCORE"))
  719. {
  720. throw new Exception("The graphic library [" + infoSystem + "] is not supported");
  721. }
  722. CameraID = cameraID;
  723. //tagOneObject += cameraID;
  724. return dllz_create_camera(cameraID);
  725. }
  726. /// <summary>
  727. /// Closes the camera and deletes all textures.
  728. /// Once destroyed, you need to recreate a camera instance to restart again.
  729. /// </summary>
  730. public void Destroy()
  731. {
  732. cameraReady = false;
  733. dllz_close(CameraID);
  734. DestroyAllTexture();
  735. }
  736. /// <summary>
  737. /// DLL-friendly version of InitParameters (found in ZEDCommon.cs).
  738. /// </summary>
  739. [StructLayout(LayoutKind.Sequential)]
  740. public struct dll_initParameters
  741. {
  742. public sl.INPUT_TYPE inputType;
  743. /// <summary>
  744. /// Resolution the ZED will be set to.
  745. /// </summary>
  746. public sl.RESOLUTION resolution;
  747. /// <summary>
  748. /// Desired camera FPS. Max is set by resolution.
  749. /// </summary>
  750. public int cameraFps;
  751. /// <summary>
  752. /// ID for identifying which of multiple connected ZEDs to use.
  753. /// </summary>
  754. public int cameraDeviceID;
  755. /// <summary>
  756. /// True to flip images horizontally.
  757. /// </summary>
  758. public int cameraImageFlip;
  759. /// <summary>
  760. /// True to disable self-calibration, using unoptimized optional calibration parameters.
  761. /// False is recommended for optimized calibration.
  762. /// </summary>
  763. [MarshalAs(UnmanagedType.U1)]
  764. public bool cameraDisableSelfCalib;
  765. /// <summary>
  766. /// True if depth relative to the right sensor should be computed.
  767. /// </summary>
  768. [MarshalAs(UnmanagedType.U1)]
  769. public bool enableRightSideMeasure;
  770. /// <summary>
  771. /// True to skip dropped frames during SVO playback.
  772. /// </summary>
  773. [MarshalAs(UnmanagedType.U1)]
  774. public bool svoRealTimeMode;
  775. /// <summary>
  776. /// Quality level of depth calculations. Higher settings improve accuracy but cost performance.
  777. /// </summary>
  778. public sl.DEPTH_MODE depthMode;
  779. /// <summary>
  780. /// True to stabilize the depth map. Recommended.
  781. /// </summary>
  782. [MarshalAs(UnmanagedType.U1)]
  783. public bool depthStabilization;
  784. /// <summary>
  785. /// Minimum distance from the camera from which depth will be computed, in the defined coordinateUnit.
  786. /// </summary>
  787. public float depthMinimumDistance;
  788. /// <summary>
  789. /// Maximum distance that can be computed.
  790. /// </summary>
  791. public float depthMaximumDistance;
  792. /// <summary>
  793. /// Coordinate unit for all measurements (depth, tracking, etc.). Meters are recommended for Unity.
  794. /// </summary>
  795. public UNIT coordinateUnit;
  796. /// <summary>
  797. /// Defines order and direction of coordinate system axes. Unity uses left-handed, Y up, so this setting is recommended.
  798. /// </summary>
  799. public COORDINATE_SYSTEM coordinateSystem;
  800. /// <summary>
  801. /// ID of the graphics card on which the ZED's computations will be performed.
  802. /// </summary>
  803. public int sdkGPUId;
  804. /// <summary>
  805. /// True for the SDK to provide text feedback.
  806. /// </summary>
  807. public int sdkVerbose;
  808. /// <summary>
  809. /// True if sensors are required, false will not trigger an error if sensors are missing.
  810. /// </summary>
  811. [MarshalAs(UnmanagedType.U1)]
  812. public bool sensorsRequired;
  813. /// <summary>
  814. /// Whether to enable improved color/gamma curves added in ZED SDK 3.0.
  815. /// </summary>
  816. [MarshalAs(UnmanagedType.U1)]
  817. public bool enableImageEnhancement;
  818. /// <summary>
  819. /// Set an optional file path where the SDK can find a file containing the calibration information of the camera computed by OpenCV.
  820. /// <remarks> Using this will disable the factory calibration of the camera. </remarks>
  821. /// <warning> Erroneous calibration values can lead to poor SDK modules accuracy. </warning>
  822. /// </summary>
  823. public string optionalOpencvCalibrationFile;
  824. /// <summary>
  825. /// Define a timeout in seconds after which an error is reported if the \ref open() command fails.
  826. /// Set to '-1' to try to open the camera endlessly without returning error in case of failure.
  827. /// Set to '0' to return error in case of failure at the first attempt.
  828. /// This parameter only impacts the LIVE mode.
  829. /// </summary>
  830. public float openTimeoutSec;
  831. /// <summary>
  832. /// Copy constructor. Takes values from Unity-suited InitParameters class.
  833. /// </summary>
  834. /// <param name="init"></param>
  835. public dll_initParameters(InitParameters init)
  836. {
  837. inputType = init.inputType;
  838. resolution = init.resolution;
  839. cameraFps = init.cameraFPS;
  840. svoRealTimeMode = init.svoRealTimeMode;
  841. coordinateUnit = init.coordinateUnit;
  842. depthMode = init.depthMode;
  843. depthMinimumDistance = init.depthMinimumDistance;
  844. depthMaximumDistance = init.depthMaximumDistance;
  845. cameraImageFlip = init.cameraImageFlip;
  846. enableRightSideMeasure = init.enableRightSideMeasure;
  847. cameraDisableSelfCalib = init.cameraDisableSelfCalib;
  848. sdkVerbose = init.sdkVerbose;
  849. sdkGPUId = init.sdkGPUId;
  850. cameraDeviceID = init.cameraDeviceID;
  851. coordinateSystem = init.coordinateSystem;
  852. depthStabilization = init.depthStabilization;
  853. sensorsRequired = init.sensorsRequired;
  854. enableImageEnhancement = init.enableImageEnhancement;
  855. optionalOpencvCalibrationFile = init.optionalOpencvCalibrationFile;
  856. openTimeoutSec = init.openTimeoutSec;
  857. }
  858. }
  859. /// <summary>
  860. /// Checks if the ZED camera is plugged in, opens it, and initializes the projection matix and command buffers for updating textures.
  861. /// </summary>
  862. /// <param name="initParameters">Class with all initialization settings.
  863. /// A newly-instantiated InitParameters will have recommended default values.</param>
  864. /// <returns>ERROR_CODE: The error code gives information about the internal connection process.
  865. /// If SUCCESS is returned, the camera is ready to use. Every other code indicates an error.</returns>
  866. public ERROR_CODE Init(ref InitParameters initParameters)
  867. {
  868. //Update values with what we're about to pass to the camera.
  869. currentResolution = initParameters.resolution;
  870. fpsMax = GetFpsForResolution(currentResolution);
  871. if (initParameters.cameraFPS == 0)
  872. {
  873. initParameters.cameraFPS = (int)fpsMax;
  874. }
  875. dll_initParameters initP = new dll_initParameters(initParameters); //DLL-friendly version of InitParameters.
  876. initP.coordinateSystem = COORDINATE_SYSTEM.LEFT_HANDED_Y_UP; //Left-hand, Y-up is Unity's coordinate system, so we match that.
  877. int v = dllz_open(CameraID, ref initP,
  878. new System.Text.StringBuilder(initParameters.pathSVO, initParameters.pathSVO.Length),
  879. new System.Text.StringBuilder(initParameters.ipStream, initParameters.ipStream.Length),
  880. initParameters.portStream,
  881. new System.Text.StringBuilder(initParameters.sdkVerboseLogFile, initParameters.sdkVerboseLogFile.Length),
  882. new System.Text.StringBuilder(initParameters.optionalSettingsPath, initParameters.optionalSettingsPath.Length),
  883. new System.Text.StringBuilder(initParameters.optionalOpencvCalibrationFile, initParameters.optionalOpencvCalibrationFile.Length));
  884. if ((ERROR_CODE)v != ERROR_CODE.SUCCESS)
  885. {
  886. cameraReady = false;
  887. return (ERROR_CODE)v;
  888. }
  889. //Set more values if the initialization was successful.
  890. imageWidth = dllz_get_width(CameraID);
  891. imageHeight = dllz_get_height(CameraID);
  892. if (imageWidth > 0 && imageHeight > 0)
  893. {
  894. GetCalibrationParameters(false);
  895. FillProjectionMatrix();
  896. baseline = calibrationParametersRectified.Trans[0];
  897. fov_H = calibrationParametersRectified.leftCam.hFOV * Mathf.Deg2Rad;
  898. fov_V = calibrationParametersRectified.leftCam.vFOV * Mathf.Deg2Rad;
  899. cameraModel = GetCameraModel();
  900. cameraReady = true;
  901. return (ERROR_CODE)v;
  902. }
  903. else
  904. return sl.ERROR_CODE.CAMERA_NOT_INITIALIZED;
  905. }
  906. /// <summary>
  907. /// Fills the projection matrix with the parameters of the ZED. Needs to be called only once.
  908. /// This projection matrix is off-center.
  909. /// </summary>
  910. /// <param name="zFar"></param>
  911. /// <param name="zNear"></param>
  912. public void FillProjectionMatrix(float zFar = 500, float zNear = 0.1f)
  913. {
  914. CalibrationParameters parameters = GetCalibrationParameters(false);
  915. float fovx = parameters.leftCam.hFOV * Mathf.Deg2Rad;
  916. float fovy = parameters.leftCam.vFOV * Mathf.Deg2Rad;
  917. float f_imageWidth = (float)ImageWidth;
  918. float f_imageHeight = (float)ImageHeight;
  919. //Manually construct the matrix based on initialization/calibration values.
  920. projection[0, 0] = 1.0f / Mathf.Tan(fovx * 0.5f); //Horizontal FoV.
  921. projection[0, 1] = 0;
  922. projection[0, 2] = 2.0f * ((f_imageWidth - 1.0f * parameters.leftCam.cx) / f_imageWidth) - 1.0f; //Horizontal offset.
  923. projection[0, 3] = 0;
  924. projection[1, 0] = 0;
  925. projection[1, 1] = 1.0f / Mathf.Tan(fovy * 0.5f); //Vertical FoV.
  926. projection[1, 2] = -(2.0f * ((f_imageHeight - 1.0f * parameters.leftCam.cy) / f_imageHeight) - 1.0f); //Vertical offset.
  927. projection[1, 3] = 0;
  928. projection[2, 0] = 0;
  929. projection[2, 1] = 0;
  930. projection[2, 2] = -(zFar + zNear) / (zFar - zNear); //Near and far planes.
  931. projection[2, 3] = -(2.0f * zFar * zNear) / (zFar - zNear); //Near and far planes.
  932. projection[3, 0] = 0;
  933. projection[3, 1] = 0;
  934. projection[3, 2] = -1;
  935. projection[3, 3] = 0.0f;
  936. }
  937. /// <summary>
  938. /// Grabs a new image, rectifies it, and computes the disparity map and (optionally) the depth map.
  939. /// The grabbing function is typically called in the main loop in a separate thread.
  940. /// </summary><remarks>For more info, read about the SDK function it calls:
  941. /// https://www.stereolabs.com/developers/documentation/API/v2.5.1/classsl_1_1Camera.html#afa3678a18dd574e162977e97d7cbf67b </remarks>
  942. /// <param name="runtimeParameters">Struct holding all grab parameters. </param>
  943. /// <returns>the function returns false if no problem was encountered,
  944. /// true otherwise.</returns>
  945. public sl.ERROR_CODE Grab(ref sl.RuntimeParameters runtimeParameters)
  946. {
  947. return (sl.ERROR_CODE)dllz_grab(CameraID, ref runtimeParameters);
  948. }
  949. /// <summary>
  950. /// Return the INPUT_TYPE currently used
  951. /// </summary>
  952. /// <returns></returns>
  953. public sl.INPUT_TYPE GetInputType()
  954. {
  955. return (sl.INPUT_TYPE)dllz_get_input_type(CameraID);
  956. }
  957. /// <summary>
  958. /// Creates a file for recording the ZED's output into a .SVO or .AVI video.
  959. /// </summary><remarks>An SVO is Stereolabs' own format designed for the ZED. It holds the video feed with timestamps
  960. /// as well as info about the camera used to record it.</remarks>
  961. /// <param name="videoFileName">Filename. Whether it ends with .svo or .avi defines its file type.</param>
  962. /// <param name="compressionMode">How much compression to use</param>
  963. /// <returns>An ERROR_CODE that defines if the file was successfully created and can be filled with images.</returns>
  964. public ERROR_CODE EnableRecording(string videoFileName, SVO_COMPRESSION_MODE compressionMode = SVO_COMPRESSION_MODE.H264_BASED, int bitrate = 0, int target_fps = 0,bool transcode = false)
  965. {
  966. return (ERROR_CODE)dllz_enable_recording(CameraID, new System.Text.StringBuilder(videoFileName, videoFileName.Length), (int)compressionMode,bitrate,target_fps,transcode);
  967. }
  968. /// <summary>
  969. /// Stops recording to an SVO/AVI, if applicable, and closes the file.
  970. /// </summary>
  971. public bool DisableRecording()
  972. {
  973. return dllz_disable_recording(CameraID);
  974. }
  975. /// <summary>
  976. /// Sets the position of the SVO file currently being read to a desired frame.
  977. /// </summary>
  978. /// <param name="frame">Index of the desired frame to be decoded.</param>
  979. public void SetSVOPosition(int frame)
  980. {
  981. dllz_set_svo_position(CameraID, frame);
  982. }
  983. /// <summary>
  984. /// Gets the current confidence threshold value for the disparity map (and by extension the depth map).
  985. /// Values below the given threshold are removed from the depth map.
  986. /// </summary>
  987. /// <returns>Filtering value between 0 and 100.</returns>
  988. public int GetConfidenceThreshold()
  989. {
  990. return dllz_get_confidence_threshold(CameraID);
  991. }
  992. /// <summary>
  993. /// Gets the timestamp at the time the latest grabbed frame was extracted from the USB stream.
  994. /// This is the closest timestamp you can get from when the image was taken. Must be called after calling grab().
  995. /// </summary>
  996. /// <returns>Current timestamp in nanoseconds. -1 means it's is not available, such as with an .SVO file without compression.</returns>
  997. public ulong GetCameraTimeStamp()
  998. {
  999. return dllz_get_image_timestamp(CameraID);
  1000. }
  1001. /// <summary>
  1002. /// Gets the current timestamp at the time the function is called. Can be compared to the camera timestamp
  1003. /// for synchronization, since they have the same reference (the computer's start time).
  1004. /// </summary>
  1005. /// <returns>The timestamp in nanoseconds.</returns>
  1006. public ulong GetCurrentTimeStamp()
  1007. {
  1008. return dllz_get_current_timestamp(CameraID);
  1009. }
  1010. /// <summary>
  1011. /// Get the current position of the SVO being recorded to.
  1012. /// </summary>
  1013. /// <returns>Index of the frame being recorded to.</returns>
  1014. public int GetSVOPosition()
  1015. {
  1016. return dllz_get_svo_position(CameraID);
  1017. }
  1018. /// <summary>
  1019. /// Gets the total number of frames in the loaded SVO file.
  1020. /// </summary>
  1021. /// <returns>Total frames in the SVO file. Returns -1 if the SDK is not reading an SVO.</returns>
  1022. public int GetSVONumberOfFrames()
  1023. {
  1024. return dllz_get_svo_number_of_frames(CameraID);
  1025. }
  1026. /// <summary>
  1027. /// Gets the closest measurable distance by the camera, according to the camera type and depth map parameters.
  1028. /// </summary>
  1029. /// <returns>The nearest possible depth value.</returns>
  1030. public float GetDepthMinRangeValue()
  1031. {
  1032. return dllz_get_depth_min_range_value(CameraID);
  1033. }
  1034. /// <summary>
  1035. /// Returns the current maximum distance of depth/disparity estimation.
  1036. /// </summary>
  1037. /// <returns>The closest depth</returns>
  1038. public float GetDepthMaxRangeValue()
  1039. {
  1040. return dllz_get_depth_max_range_value(CameraID);
  1041. }
  1042. /// <summary>
  1043. /// Initialize and Start the tracking functions
  1044. /// </summary>
  1045. /// <param name="quat"> rotation used as initial world transform. By default it should be identity.</param>
  1046. /// <param name="vec"> translation used as initial world transform. By default it should be identity.</param>
  1047. /// <param name="enableSpatialMemory"> (optional) define if spatial memory is enable or not.</param>
  1048. /// <param name="areaFilePath"> (optional) file of spatial memory file that has to be loaded to relocate in the scene.</param>
  1049. /// <returns></returns>
  1050. public sl.ERROR_CODE EnableTracking(ref Quaternion quat, ref Vector3 vec, bool enableSpatialMemory = true, bool enablePoseSmoothing = false, bool enableFloorAlignment = false, bool trackingIsStatic = false,
  1051. bool enableIMUFusion = true, string areaFilePath = "")
  1052. {
  1053. sl.ERROR_CODE trackingStatus = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1054. trackingStatus = (sl.ERROR_CODE)dllz_enable_tracking(CameraID, ref quat, ref vec, enableSpatialMemory, enablePoseSmoothing, enableFloorAlignment,
  1055. trackingIsStatic, enableIMUFusion, new System.Text.StringBuilder(areaFilePath, areaFilePath.Length));
  1056. return trackingStatus;
  1057. }
  1058. /// <summary>
  1059. /// Reset tracking
  1060. /// </summary>
  1061. /// <param name="rotation"></param>
  1062. /// <param name="translation"></param>
  1063. /// <returns></returns>
  1064. public sl.ERROR_CODE ResetTracking(Quaternion rotation, Vector3 translation)
  1065. {
  1066. sl.ERROR_CODE trackingStatus = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1067. trackingStatus = (sl.ERROR_CODE)dllz_reset_tracking(CameraID, rotation, translation);
  1068. return trackingStatus;
  1069. }
  1070. public sl.ERROR_CODE ResetTrackingWithOffset(Quaternion rotation, Vector3 translation, Quaternion rotationOffset, Vector3 translationOffset)
  1071. {
  1072. sl.ERROR_CODE trackingStatus = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1073. trackingStatus = (sl.ERROR_CODE)dllz_reset_tracking_with_offset(CameraID, rotation, translation, rotationOffset, translationOffset);
  1074. return trackingStatus;
  1075. }
  1076. public sl.ERROR_CODE EstimateInitialPosition(ref Quaternion rotation, ref Vector3 translation)
  1077. {
  1078. sl.ERROR_CODE status = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1079. status = (sl.ERROR_CODE)dllz_estimate_initial_position(CameraID, ref rotation, ref translation, 2, 100);
  1080. return status;
  1081. }
  1082. /// <summary>
  1083. /// Stop the motion tracking, if you want to restart, call enableTracking().
  1084. /// </summary>
  1085. /// <param name="path">The path to save the area file</param>
  1086. public void DisableTracking(string path = "")
  1087. {
  1088. dllz_disable_tracking(CameraID, new System.Text.StringBuilder(path, path.Length));
  1089. }
  1090. public sl.ERROR_CODE SaveCurrentArea(string path)
  1091. {
  1092. return (sl.ERROR_CODE)dllz_save_current_area(CameraID, new System.Text.StringBuilder(path, path.Length));
  1093. }
  1094. /// <summary>
  1095. /// Returns the current state of the area learning saving
  1096. /// </summary>
  1097. /// <returns></returns>
  1098. public sl.AREA_EXPORT_STATE GetAreaExportState()
  1099. {
  1100. return (sl.AREA_EXPORT_STATE)dllz_get_area_export_state(CameraID);
  1101. }
  1102. /// <summary>
  1103. /// Register a texture to the base
  1104. /// </summary>
  1105. private void RegisterTexture(Texture2D m_Texture, int type, int mode)
  1106. {
  1107. TextureRequested t = new TextureRequested();
  1108. t.type = type;
  1109. t.option = mode;
  1110. texturesRequested.Add(t);
  1111. textures[type].Add(mode, m_Texture);
  1112. }
  1113. /// <summary>
  1114. /// Creates or retrieves a texture of type Image. Will be updated each frame automatically.
  1115. /// <para>Image type textures are human-viewable, but have less accuracy than measure types.</para>
  1116. /// </summary>
  1117. /// <remarks>
  1118. /// Note that the new texture will exist on the GPU, so accessing from the CPU will result in an empty image. To get images
  1119. /// with the CPU, use RetrieveImage() instead and specify CPU memory in the arguments.
  1120. /// </remarks>
  1121. /// <param name="mode">What the image shows (left RGB image, right depth image, normal map, etc.)</param>
  1122. /// /// <param name="resolution">Resolution of the image. Should correspond to ZED's current resolution.</param>
  1123. /// <returns>Texture2D that will update each frame with the ZED SDK's output.</returns>
  1124. public Texture2D CreateTextureImageType(VIEW mode, Resolution resolution = new Resolution())
  1125. {
  1126. if (HasTexture((int)TYPE_VIEW.RETRIEVE_IMAGE, (int)mode))
  1127. {
  1128. return textures[(int)TYPE_VIEW.RETRIEVE_IMAGE][(int)mode];
  1129. }
  1130. if (!cameraReady)
  1131. return null;
  1132. int width = ImageWidth;
  1133. int height = imageHeight;
  1134. if (!((uint)resolution.width == 0 && (uint)resolution.height == 0)) //Handles if Resolution arg was empty, as in the default.
  1135. {
  1136. width = (int)resolution.width;
  1137. height = (int)resolution.height;
  1138. }
  1139. Texture2D m_Texture;
  1140. if (mode == VIEW.LEFT_GREY || mode == VIEW.RIGHT_GREY || mode == VIEW.LEFT_UNRECTIFIED_GREY || mode == VIEW.RIGHT_UNRECTIFIED_GREY)
  1141. {
  1142. m_Texture = new Texture2D(width, height, TextureFormat.Alpha8, false);
  1143. }
  1144. else if (mode == VIEW.SIDE_BY_SIDE)
  1145. {
  1146. m_Texture = new Texture2D(width * 2, height, TextureFormat.RGBA32, false); //Needs to be twice as wide for SBS because there are two images.
  1147. }
  1148. else
  1149. {
  1150. m_Texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
  1151. }
  1152. m_Texture.filterMode = FilterMode.Point;
  1153. m_Texture.wrapMode = TextureWrapMode.Clamp;
  1154. m_Texture.Apply();
  1155. IntPtr idTexture = m_Texture.GetNativeTexturePtr();
  1156. int error = dllz_register_texture_image_type(CameraID, (int)mode, idTexture, (int)resolution.width, (int)resolution.height);
  1157. if (error != 0)
  1158. {
  1159. throw new Exception("CUDA error:" + error + " if the problem appears again, please contact Stereolabs support.");
  1160. }
  1161. if (!textures.ContainsKey((int)TYPE_VIEW.RETRIEVE_IMAGE))
  1162. {
  1163. textures.Add((int)TYPE_VIEW.RETRIEVE_IMAGE, new Dictionary<int, Texture2D>());
  1164. }
  1165. RegisterTexture(m_Texture, (int)TYPE_VIEW.RETRIEVE_IMAGE, (int)mode); //Save so you don't make a duplicate if another script needs the texture.
  1166. return m_Texture;
  1167. }
  1168. /// <summary>
  1169. /// Creates or retrievse a texture of type Measure. Will be updated each frame automatically.
  1170. /// Measure types are not human-viewable, but don't lose any accuracy.
  1171. /// </summary>
  1172. /// <remarks>
  1173. /// Note that the new texture will exist on the GPU, so accessing from the CPU will result in an empty image. To get images
  1174. /// with the CPU, use RetrieveMeasure() instead and specify CPU memory in the arguments.
  1175. /// </remarks>
  1176. /// <param name="mode">What the image shows (disparity, depth, confidence, etc.)</param>
  1177. /// <param name="resolution">Resolution of the image. Should correspond to ZED's current resolution.</param>
  1178. /// <returns>Texture2D that will update each frame with the ZED SDK's output.</returns>
  1179. public Texture2D CreateTextureMeasureType(MEASURE mode, Resolution resolution = new Resolution())
  1180. {
  1181. if (HasTexture((int)TYPE_VIEW.RETRIEVE_MEASURE, (int)mode))
  1182. {
  1183. return textures[(int)TYPE_VIEW.RETRIEVE_MEASURE][(int)mode];
  1184. }
  1185. if (!cameraReady)
  1186. return null;
  1187. Texture2D m_Texture;
  1188. int width = ImageWidth;
  1189. int height = imageHeight;
  1190. if (!((uint)resolution.width == 0 && (uint)resolution.height == 0))
  1191. {
  1192. width = (int)resolution.width;
  1193. height = (int)resolution.height;
  1194. }
  1195. //Handle the mode options.
  1196. if (mode == MEASURE.XYZ || mode == MEASURE.XYZABGR || mode == MEASURE.XYZARGB || mode == MEASURE.XYZBGRA || mode == MEASURE.XYZRGBA || mode == MEASURE.NORMALS
  1197. || mode == MEASURE.XYZ_RIGHT || mode == MEASURE.XYZABGR_RIGHT || mode == MEASURE.XYZARGB_RIGHT || mode == MEASURE.XYZBGRA_RIGHT || mode == MEASURE.XYZRGBA_RIGHT || mode == MEASURE.NORMALS_RIGHT)
  1198. {
  1199. m_Texture = new Texture2D(width, height, TextureFormat.RGBAFloat, false, true);
  1200. }
  1201. else if (mode == MEASURE.DEPTH || mode == MEASURE.CONFIDENCE || mode == MEASURE.DISPARITY || mode == MEASURE.DEPTH_RIGHT || mode == MEASURE.DISPARITY_RIGHT)
  1202. {
  1203. m_Texture = new Texture2D(width, height, TextureFormat.RFloat, false, true);
  1204. }
  1205. else
  1206. {
  1207. m_Texture = new Texture2D(width, height, TextureFormat.RGBA32, false, true);
  1208. }
  1209. if (!((uint)resolution.width == 0 && (uint)resolution.height == 0))
  1210. {
  1211. m_Texture.filterMode = FilterMode.Bilinear;
  1212. }
  1213. else
  1214. {
  1215. m_Texture.filterMode = FilterMode.Point;
  1216. }
  1217. m_Texture.wrapMode = TextureWrapMode.Clamp;
  1218. m_Texture.Apply();
  1219. IntPtr idTexture = m_Texture.GetNativeTexturePtr();
  1220. int error = dllz_register_texture_measure_type(CameraID, (int)mode, idTexture, (int)resolution.width, (int)resolution.height);
  1221. if (error != 0)
  1222. {
  1223. throw new Exception("CUDA error:" + error + " if the problem appears again, please contact Stereolabs support.");
  1224. }
  1225. if (!textures.ContainsKey((int)TYPE_VIEW.RETRIEVE_MEASURE))
  1226. {
  1227. textures.Add((int)TYPE_VIEW.RETRIEVE_MEASURE, new Dictionary<int, Texture2D>());
  1228. }
  1229. RegisterTexture(m_Texture, (int)TYPE_VIEW.RETRIEVE_MEASURE, (int)mode); //Save to avoid duplicates if texture type is needed elsewhere.
  1230. return m_Texture;
  1231. }
  1232. /// <summary>
  1233. /// Unregisters a texture of type Image. The texture will be destroyed and will no longer be updated each frame.
  1234. /// </summary>
  1235. /// <param name="view">What the image was showing (left RGB image, right depth image, normal map, etc.)</param>
  1236. public bool UnregisterTextureImageType(sl.VIEW view)
  1237. {
  1238. DestroyTextureImageType((int)view);
  1239. return dllz_unregister_texture_image_type(CameraID, (int)view) != 0;
  1240. }
  1241. /// <summary>
  1242. /// Unregisters a texture of type Measure, The texture will be destroyed and will no longer be updated each frame.
  1243. /// </summary>
  1244. /// <param name="measure">What the measure was showing (disparity, depth, confidence, etc.)</param>
  1245. public bool UnregisterTextureMeasureType(sl.MEASURE measure)
  1246. {
  1247. DestroyTextureMeasureType((int)measure);
  1248. return dllz_unregister_texture_measure_type(CameraID, (int)measure) != 0;
  1249. }
  1250. /// <summary>
  1251. /// Copies a Texture of type Image into a ZEDMat. This function should be called after a Grab() and an UpdateTextures().
  1252. /// </summary>
  1253. /// <param name="view">View type (left rgb, right depth, etc.)</param>
  1254. /// <returns>New ZEDMat for an image texture of the selected view type.</returns>
  1255. public ZEDMat RequestCopyMatFromTextureImageType(sl.VIEW view)
  1256. {
  1257. return new ZEDMat(dllz_get_copy_mat_texture_image_type(CameraID, (int)view));
  1258. }
  1259. /// <summary>
  1260. /// Copies a texture of type Measure into a ZEDMat. This function should be called after a Grab() and an UpdateTextures().
  1261. /// </summary>
  1262. /// <param name="measure">Measure type (depth, disparity, confidence, etc.)</param>
  1263. /// <returns>New ZEDMat for a measure texture of the selected measure type.</returns>
  1264. public ZEDMat RequestCopyMatFromTextureMeasureType(sl.MEASURE measure)
  1265. {
  1266. return new ZEDMat(dllz_get_copy_mat_texture_measure_type(CameraID, (int)measure));
  1267. }
  1268. /// <summary>
  1269. /// Destroys a texture and removes its reference in the textures list.
  1270. /// </summary>
  1271. /// <param name="type">Type of texture as an int (0 for Image, 1 for Measure).</param>
  1272. /// <param name="option">Corresponding options enum (sl.VIEW if Image type, sl.MEASURE if Measure type) as an integer.</param>
  1273. private void DestroyTexture(int type, int option)
  1274. {
  1275. if (textures.ContainsKey(type) && textures[type].ContainsKey(option))
  1276. {
  1277. textures[type][option] = null;
  1278. textures[type].Remove(option);
  1279. if (textures[type].Count == 0)
  1280. {
  1281. textures.Remove(type);
  1282. }
  1283. }
  1284. }
  1285. /// <summary>
  1286. /// Destroy all textures that were ever requested.
  1287. /// </summary>
  1288. private void DestroyAllTexture()
  1289. {
  1290. if (cameraReady)
  1291. {
  1292. foreach (TextureRequested t in texturesRequested)
  1293. {
  1294. DestroyTexture(t.type, t.option);
  1295. }
  1296. texturesRequested.Clear();
  1297. }
  1298. }
  1299. /// <summary>
  1300. /// Destroy a texture created with CreateTextureImageType().
  1301. /// </summary>
  1302. /// <param name="type">View type (left RGB, right depth image, etc.) as an integer.</param>
  1303. private void DestroyTextureImageType(int option)
  1304. {
  1305. DestroyTexture((int)TYPE_VIEW.RETRIEVE_IMAGE, option);
  1306. }
  1307. /// <summary>
  1308. /// Destroy a texture created with CreateTextureMeasureType().
  1309. /// </summary>
  1310. /// <param name="type">Measure type (depth, confidence, etc.) as an integer.</param>
  1311. private void DestroyTextureMeasureType(int option)
  1312. {
  1313. DestroyTexture((int)TYPE_VIEW.RETRIEVE_MEASURE, option);
  1314. }
  1315. /// <summary>
  1316. /// Retrieves a texture that was already created.
  1317. /// </summary>
  1318. /// <param name="type">Type of texture as an integer (0 for Image, 1 for Measure).</param>
  1319. /// <param name="mode">Corresponding options enum (sl.VIEW if Image type, sl.MEASURE if Measure type) as an integer.</param>
  1320. /// <returns>Existing texture of the given type/mode.</returns>
  1321. public Texture2D GetTexture(TYPE_VIEW type, int mode)
  1322. {
  1323. if (HasTexture((int)type, mode))
  1324. {
  1325. return textures[(int)type][mode];
  1326. }
  1327. return null;
  1328. }
  1329. /// <summary>
  1330. /// Checks if a texture of a given type has already been created.
  1331. /// </summary>
  1332. /// <param name="type">Type of texture as an integer (0 for Image, 1 for Measure).</param>
  1333. /// <param name="mode">Corresponding options enum (sl.VIEW if Image type, sl.MEASURE if Measure type) as an integer.</param>
  1334. /// <returns>True if the texture is available.</returns>
  1335. private bool HasTexture(int type, int mode)
  1336. {
  1337. if (cameraReady) //Texture can't exist if the ZED hasn't been initialized yet.
  1338. {
  1339. return textures.ContainsKey((int)type) && textures[type].ContainsKey((int)mode);
  1340. }
  1341. return false;
  1342. }
  1343. /// <summary>
  1344. /// Returns the current camera FPS. This is limited primarily by resolution but can also be lower due to
  1345. /// setting a lower desired resolution in Init() or from USB connection/bandwidth issues.
  1346. /// </summary>
  1347. /// <returns>The current fps</returns>
  1348. public float GetCameraFPS()
  1349. {
  1350. return dllz_get_camera_fps(CameraID);
  1351. }
  1352. public CalibrationParameters GetCalibrationParameters(bool raw = false)
  1353. {
  1354. IntPtr p = dllz_get_calibration_parameters(CameraID, raw);
  1355. if (p == IntPtr.Zero)
  1356. {
  1357. return new CalibrationParameters();
  1358. }
  1359. CalibrationParameters parameters = (CalibrationParameters)Marshal.PtrToStructure(p, typeof(CalibrationParameters));
  1360. if (raw)
  1361. calibrationParametersRaw = parameters;
  1362. else
  1363. calibrationParametersRectified = parameters;
  1364. return parameters;
  1365. }
  1366. public SensorsConfiguration GetInternalSensorsConfiguration()
  1367. {
  1368. IntPtr p = dllz_get_sensors_configuration(CameraID);
  1369. if (p == IntPtr.Zero)
  1370. {
  1371. return new SensorsConfiguration();
  1372. }
  1373. SensorsConfiguration configuration = (SensorsConfiguration)Marshal.PtrToStructure(p, typeof(SensorsConfiguration));
  1374. return configuration;
  1375. }
  1376. /// <summary>
  1377. /// Gets the ZED camera model (ZED or ZED Mini).
  1378. /// </summary>
  1379. /// <returns>Model of the ZED as sl.MODEL.</returns>
  1380. public sl.MODEL GetCameraModel()
  1381. {
  1382. return (sl.MODEL)dllz_get_camera_model(CameraID);
  1383. }
  1384. /// <summary>
  1385. /// Gets the ZED's camera firmware version.
  1386. /// </summary>
  1387. /// <returns>Firmware version.</returns>
  1388. public int GetCameraFirmwareVersion()
  1389. {
  1390. return dllz_get_camera_firmware(CameraID);
  1391. }
  1392. /// <summary>
  1393. /// Gets the ZED's sensors firmware version.
  1394. /// </summary>
  1395. /// <returns>Firmware version.</returns>
  1396. public int GetSensorsFirmwareVersion()
  1397. {
  1398. return dllz_get_sensors_firmware(CameraID);
  1399. }
  1400. /// <summary>
  1401. /// Gets the ZED's serial number.
  1402. /// </summary>
  1403. /// <returns>Serial number</returns>
  1404. public int GetZEDSerialNumber()
  1405. {
  1406. return dllz_get_zed_serial(CameraID);
  1407. }
  1408. /// <summary>
  1409. /// Returns the ZED's vertical field of view in radians.
  1410. /// </summary>
  1411. /// <returns>Vertical field of view.</returns>
  1412. public float GetFOV()
  1413. {
  1414. return GetCalibrationParameters(false).leftCam.vFOV * Mathf.Deg2Rad;
  1415. }
  1416. /// <summary>
  1417. /// Computes textures from the ZED. The new textures will not be displayed until an event is sent to the render thread.
  1418. /// This event is called from UpdateTextures().
  1419. /// </summary>
  1420. public void RetrieveTextures()
  1421. {
  1422. dllz_retrieve_textures(CameraID);
  1423. }
  1424. /// <summary>
  1425. /// Swaps textures safely between the acquisition and rendering threads.
  1426. /// </summary>
  1427. public void SwapTextures()
  1428. {
  1429. dllz_swap_textures(CameraID);
  1430. }
  1431. /// <summary>
  1432. /// Timestamp of the images used the last time the ZED wrapper updated textures.
  1433. /// </summary>
  1434. /// <returns></returns>
  1435. public ulong GetImagesTimeStamp()
  1436. {
  1437. return dllz_get_updated_textures_timestamp(CameraID);
  1438. }
  1439. /// <summary>
  1440. /// Perform a new self calibration process.
  1441. /// In some cases, due to temperature changes or strong vibrations, the stereo calibration becomes less accurate.
  1442. /// Use this function to update the self-calibration data and get more reliable depth values.
  1443. /// <remarks>The self calibration will occur at the next \ref grab() call.</remarks>
  1444. /// New values will then be available in \ref getCameraInformation(), be sure to get them to still have consistent 2D <-> 3D conversion.
  1445. /// </summary>
  1446. /// <param name="cameraID"></param>
  1447. /// <returns></returns>
  1448. public void UpdateSelfCalibration()
  1449. {
  1450. dllz_update_self_calibration(CameraID);
  1451. }
  1452. /// <summary>
  1453. /// Gets the number of frames dropped since Grab() was called for the first time.
  1454. /// Based on camera timestamps and an FPS comparison.
  1455. /// </summary><remarks>Similar to the Frame Drop display in the ZED Explorer app.</remarks>
  1456. /// <returns>Frames dropped since first Grab() call.</returns>
  1457. public uint GetFrameDroppedCount()
  1458. {
  1459. return dllz_get_frame_dropped_count(CameraID);
  1460. }
  1461. /// <summary>
  1462. /// Gets the percentage of frames dropped since Grab() was called for the first time.
  1463. /// </summary>
  1464. /// <returns>Percentage of frames dropped.</returns>
  1465. public float GetFrameDroppedPercent()
  1466. {
  1467. return dllz_get_frame_dropped_percent(CameraID);
  1468. }
  1469. /// <summary>
  1470. /// Gets the position of the camera and the current state of the ZED Tracking.
  1471. /// </summary>
  1472. /// <param name="rotation">Quaternion filled with the current rotation of the camera depending on its reference frame.</param>
  1473. /// <param name="position">Vector filled with the current position of the camera depending on its reference frame.</param>
  1474. /// <param name="referenceType">Reference frame for setting the rotation/position. CAMERA gives movement relative to the last pose.
  1475. /// WORLD gives cumulative movements since tracking started.</param>
  1476. /// <returns>State of ZED's Tracking system (off, searching, ok).</returns>
  1477. public TRACKING_STATE GetPosition(ref Quaternion rotation, ref Vector3 position, REFERENCE_FRAME referenceType = REFERENCE_FRAME.WORLD)
  1478. {
  1479. return (TRACKING_STATE)dllz_get_position(CameraID, ref rotation, ref position, (int)referenceType);
  1480. }
  1481. /// <summary>
  1482. /// Gets the current position of the camera and state of the tracking, with an optional offset to the tracking frame.
  1483. /// </summary>
  1484. /// <param name="rotation">Quaternion filled with the current rotation of the camera depending on its reference frame.</param>
  1485. /// <param name="position">Vector filled with the current position of the camera depending on its reference frame.</param>
  1486. /// <param name="targetQuaternion">Rotational offset applied to the tracking frame.</param>
  1487. /// <param name="targetTranslation">Positional offset applied to the tracking frame.</param>
  1488. /// <param name="referenceFrame">Reference frame for setting the rotation/position. CAMERA gives movement relative to the last pose.
  1489. /// WORLD gives cumulative movements since tracking started.</param>
  1490. /// <returns>State of ZED's Tracking system (off, searching, ok).</returns>
  1491. public TRACKING_STATE GetPosition(ref Quaternion rotation, ref Vector3 translation, ref Quaternion targetQuaternion, ref Vector3 targetTranslation, REFERENCE_FRAME referenceFrame = REFERENCE_FRAME.WORLD)
  1492. {
  1493. return (TRACKING_STATE)dllz_get_position_at_target_frame(CameraID, ref rotation, ref translation, ref targetQuaternion, ref targetTranslation, (int)referenceFrame);
  1494. }
  1495. /// <summary>
  1496. /// Gets the current position of the camera and state of the tracking, with a defined tracking frame.
  1497. /// A tracking frame defines what part of the ZED is its center for tracking purposes. See ZEDCommon.TRACKING_FRAME.
  1498. /// </summary>
  1499. /// <param name="rotation">Quaternion filled with the current rotation of the camera depending on its reference frame.</param>
  1500. /// <param name="position">Vector filled with the current position of the camera depending on its reference frame.</param>
  1501. /// <param name="trackingFrame">Center of the ZED for tracking purposes (left eye, center, right eye).</param>
  1502. /// <param name="referenceFrame">Reference frame for setting the rotation/position. CAMERA gives movement relative to the last pose.
  1503. /// WORLD gives cumulative movements since tracking started.</param>
  1504. /// <returns>State of ZED's Tracking system (off, searching, ok).</returns>
  1505. public TRACKING_STATE GetPosition(ref Quaternion rotation, ref Vector3 translation, TRACKING_FRAME trackingFrame, REFERENCE_FRAME referenceFrame = REFERENCE_FRAME.WORLD)
  1506. {
  1507. Quaternion rotationOffset = Quaternion.identity;
  1508. Vector3 positionOffset = Vector3.zero;
  1509. switch (trackingFrame) //Add offsets to account for different tracking frames.
  1510. {
  1511. case sl.TRACKING_FRAME.LEFT_EYE:
  1512. positionOffset = new Vector3(0, 0, 0);
  1513. break;
  1514. case sl.TRACKING_FRAME.RIGHT_EYE:
  1515. positionOffset = new Vector3(Baseline, 0, 0);
  1516. break;
  1517. case sl.TRACKING_FRAME.CENTER_EYE:
  1518. positionOffset = new Vector3(Baseline / 2.0f, 0, 0);
  1519. break;
  1520. }
  1521. return (TRACKING_STATE)dllz_get_position_at_target_frame(CameraID, ref rotation, ref translation, ref rotationOffset, ref positionOffset, (int)referenceFrame);
  1522. }
  1523. /// <summary>
  1524. /// Gets the current position of the camera and state of the tracking, filling a Pose struct useful for AR pass-through.
  1525. /// </summary>
  1526. /// <param name="pose">Current pose.</param>
  1527. /// <param name="referenceType">Reference frame for setting the rotation/position. CAMERA gives movement relative to the last pose.
  1528. /// WORLD gives cumulative movements since tracking started.</param>
  1529. /// <returns>State of ZED's Tracking system (off, searching, ok).</returns>
  1530. public TRACKING_STATE GetPosition(ref Pose pose, REFERENCE_FRAME referenceType = REFERENCE_FRAME.WORLD)
  1531. {
  1532. return (TRACKING_STATE)dllz_get_position_data(CameraID, ref pose, (int)referenceType);
  1533. }
  1534. /// <summary>
  1535. /// Sets a prior to the IMU orientation (only for ZED-M).
  1536. /// Prior must come from a external IMU, such as the HMD orientation and should be in a time frame
  1537. /// that's as close as possible to the camera.
  1538. /// </summary>
  1539. /// <returns>Error code status.</returns>
  1540. /// <param name="rotation">Prior rotation.</param>
  1541. public ERROR_CODE SetIMUOrientationPrior(ref Quaternion rotation)
  1542. {
  1543. sl.ERROR_CODE trackingStatus = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1544. trackingStatus = (sl.ERROR_CODE)dllz_set_imu_prior_orientation(CameraID, rotation);
  1545. return trackingStatus;
  1546. }
  1547. /// <summary>
  1548. /// Gets the rotation given by the ZED-M/ZED2 IMU. Return an error if using ZED (v1) which does not contains internal sensors
  1549. /// </summary>
  1550. /// <param name="rotation">Rotation from the IMU.</param>
  1551. /// <param name="referenceTime">time reference.</param>
  1552. /// <returns>Error code status.</returns>
  1553. public ERROR_CODE GetInternalIMUOrientation(ref Quaternion rotation, TIME_REFERENCE referenceTime = TIME_REFERENCE.IMAGE)
  1554. {
  1555. sl.ERROR_CODE err = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1556. err = (sl.ERROR_CODE)dllz_get_internal_imu_orientation(CameraID, ref rotation, (int)referenceTime);
  1557. return err;
  1558. }
  1559. /// <summary>
  1560. /// Gets the full Sensor data from the ZED-M or ZED2 . Return an error if using ZED (v1) which does not contains internal sensors
  1561. /// </summary>
  1562. /// <param name="data">Sensor Data.</param>
  1563. /// <param name="referenceTime">Time reference.</param>
  1564. /// <returns>Error code status.</returns>
  1565. public ERROR_CODE GetInternalSensorsData(ref SensorsData data, TIME_REFERENCE referenceTime = TIME_REFERENCE.IMAGE)
  1566. {
  1567. sl.ERROR_CODE err = sl.ERROR_CODE.CAMERA_NOT_DETECTED;
  1568. err = (sl.ERROR_CODE)dllz_get_internal_sensors_data(CameraID, ref data, (int)referenceTime);
  1569. return err;
  1570. }
  1571. /// <summary>
  1572. /// Converts a float array to a matrix.
  1573. /// </summary>
  1574. /// <param name="m">Matrix to be filled.</param>
  1575. /// <param name="f">Float array to be turned into a matrix.</param>
  1576. static public void Float2Matrix(ref Matrix4x4 m, float[] f)
  1577. {
  1578. if (f == null) return;
  1579. if (f.Length != 16) return;
  1580. for (int i = 0; i < 4; ++i)
  1581. {
  1582. for (int j = 0; j < 4; ++j)
  1583. {
  1584. m[i, j] = f[i * 4 + j];
  1585. }
  1586. }
  1587. }
  1588. /// <summary>
  1589. /// Sets a value in the ZED's camera settings.
  1590. /// </summary>
  1591. /// <param name="settings">Setting to be changed (brightness, contrast, gain, exposure, etc.)</param>
  1592. /// <param name="value">New value.</param>
  1593. /// <param name="usedefault">True to set the settings to their default values.</param>
  1594. public void SetCameraSettings(CAMERA_SETTINGS settings, int value)
  1595. {
  1596. AssertCameraIsReady();
  1597. //cameraSettingsManager.SetCameraSettings(CameraID, settings, value);
  1598. dllz_set_video_settings(CameraID, (int)settings, value);
  1599. }
  1600. /// <summary>
  1601. /// Gets the value of a given setting from the ZED camera.
  1602. /// </summary>
  1603. /// <param name="settings">Setting to be retrieved (brightness, contrast, gain, exposure, etc.)</param>
  1604. public int GetCameraSettings(CAMERA_SETTINGS settings)
  1605. {
  1606. AssertCameraIsReady();
  1607. return dllz_get_video_settings(CameraID, (int)settings);
  1608. //return cameraSettingsManager.GetCameraSettings(CameraID, settings);
  1609. }
  1610. /// <summary>
  1611. /// Overloaded function for CAMERA_SETTINGS.AEC_AGC_ROI (requires iRect as input)
  1612. /// </summary>
  1613. /// <param name="settings"> Must be set to CAMERA_SETTINGS.AEC_AGC_ROI. Otherwise will return -1.</param>
  1614. /// <param name="side"> defines left=0 or right=1 or both=2 sensor target</param>
  1615. /// <param name="roi">the roi defined as a sl.Rect</param>
  1616. /// <param name="reset">Defines if the target must be reset to full sensor</param>
  1617. /// <returns></returns>
  1618. public int SetCameraSettings(CAMERA_SETTINGS settings, int side, iRect roi,bool reset)
  1619. {
  1620. AssertCameraIsReady();
  1621. if (settings == CAMERA_SETTINGS.AEC_AGC_ROI)
  1622. return dllz_set_roi_for_aec_agc(CameraID, side, roi, reset);
  1623. else
  1624. return -1;
  1625. }
  1626. /// <summary>
  1627. /// Overloaded function for CAMERA_SETTINGS.AEC_AGC_ROI (requires iRect as input)
  1628. /// </summary>
  1629. /// <param name="settings"> Must be set to CAMERA_SETTINGS.AEC_AGC_ROI. Otherwise will return -1.</param>
  1630. /// <param name="side"> defines left=0 or right=1 or both=2 sensor target.</param>
  1631. /// <param name="roi"> Roi that will be filled.</param>
  1632. /// <returns></returns>
  1633. public int GetCameraSettings(CAMERA_SETTINGS settings, int side,ref iRect roi)
  1634. {
  1635. AssertCameraIsReady();
  1636. if (settings == CAMERA_SETTINGS.AEC_AGC_ROI)
  1637. return dllz_get_roi_for_aec_agc(CameraID, side, ref roi);
  1638. else
  1639. return -1;
  1640. }
  1641. /// <summary>
  1642. /// Reset camera settings to default
  1643. /// </summary>
  1644. public void ResetCameraSettings()
  1645. {
  1646. AssertCameraIsReady();
  1647. //cameraSettingsManager.ResetCameraSettings(this);
  1648. SetCameraSettings(sl.CAMERA_SETTINGS.BRIGHTNESS, sl.ZEDCamera.brightnessDefault);
  1649. SetCameraSettings(sl.CAMERA_SETTINGS.CONTRAST, sl.ZEDCamera.contrastDefault);
  1650. SetCameraSettings(sl.CAMERA_SETTINGS.HUE, sl.ZEDCamera.hueDefault);
  1651. SetCameraSettings(sl.CAMERA_SETTINGS.SATURATION, sl.ZEDCamera.saturationDefault);
  1652. SetCameraSettings(sl.CAMERA_SETTINGS.SHARPNESS, sl.ZEDCamera.sharpnessDefault);
  1653. SetCameraSettings(sl.CAMERA_SETTINGS.GAMMA, sl.ZEDCamera.gammaDefault);
  1654. SetCameraSettings(sl.CAMERA_SETTINGS.AUTO_WHITEBALANCE, 1);
  1655. SetCameraSettings(sl.CAMERA_SETTINGS.AEC_AGC, 1);
  1656. SetCameraSettings(sl.CAMERA_SETTINGS.LED_STATUS, 1);
  1657. SetCameraSettings(sl.CAMERA_SETTINGS.AEC_AGC_ROI,2, new sl.iRect(), true);
  1658. }
  1659. /// <summary>
  1660. /// Loads camera settings (brightness, contrast, hue, saturation, gain, exposure) from a file in the
  1661. /// project's root directory.
  1662. /// </summary>
  1663. /// <param name="path">Filename.</param>
  1664. public void LoadCameraSettings(string path)
  1665. {
  1666. cameraSettingsManager.LoadCameraSettings(this, path);
  1667. }
  1668. /// <summary>
  1669. /// Save the camera settings (brightness, contrast, hue, saturation, gain, exposure) to a file
  1670. /// relative to the project's root directory.
  1671. /// </summary>
  1672. /// <param name="path">Filename.</param>
  1673. public void SaveCameraSettings(string path)
  1674. {
  1675. cameraSettingsManager.SaveCameraSettings(path);
  1676. }
  1677. /// <summary>
  1678. /// Retrieves camera settings from the ZED camera and loads them into a CameraSettings instance
  1679. /// handled by ZEDCameraSettingsManager.
  1680. /// </summary>
  1681. public void RetrieveCameraSettings()
  1682. {
  1683. cameraSettingsManager.RetrieveSettingsCamera(this);
  1684. }
  1685. /// <summary>
  1686. /// Returns if the camera's exposure mode is set to automatic.
  1687. /// </summary>
  1688. /// <returns><c>True</c> if automatic, <c>false</c> if manual.</returns>
  1689. public bool GetExposureUpdateType()
  1690. {
  1691. return cameraSettingsManager.auto;
  1692. }
  1693. /// <summary>
  1694. /// Returns if the camera's white balance is set to automatic.
  1695. /// </summary>
  1696. /// <returns><c>True</c> if automatic, <c>false</c> if manual.</returns>
  1697. public bool GetWhiteBalanceUpdateType()
  1698. {
  1699. return cameraSettingsManager.whiteBalanceAuto;
  1700. }
  1701. /// <summary>
  1702. /// Applies all the settings registered in the ZEDCameraSettingsManager instance to the actual ZED camera.
  1703. /// </summary>
  1704. public void SetCameraSettings()
  1705. {
  1706. cameraSettingsManager.SetSettings(this);
  1707. }
  1708. /// <summary>
  1709. /// Gets the version of the currently installed ZED SDK.
  1710. /// </summary>
  1711. /// <returns>ZED SDK version as a string in the format MAJOR.MINOR.PATCH.</returns>
  1712. public static string GetSDKVersion()
  1713. {
  1714. return PtrToStringUtf8(dllz_get_sdk_version());
  1715. }
  1716. /// <summary>
  1717. /// List all the connected devices with their associated information.
  1718. /// This function lists all the cameras available and provides their serial number, models and other information.
  1719. /// </summary>
  1720. /// <returns>The device properties for each connected camera</returns>
  1721. public static sl.DeviceProperties[] GetDeviceList(out int nbDevices)
  1722. {
  1723. sl.DeviceProperties[] deviceList = new sl.DeviceProperties[(int)Constant.MAX_CAMERA_PLUGIN];
  1724. dllz_get_device_list(deviceList, out nbDevices);
  1725. return deviceList;
  1726. }
  1727. /// <summary>
  1728. /// Performs an hardware reset of the ZED 2/ZED 2i.
  1729. /// </summary>
  1730. /// <param name="serialNumber">Serial number of the camera</param>
  1731. /// <param name="fullReboot"> Perform a full reboot (Sensors and Video modules)</param>
  1732. /// <returns>ZED SDK version as a string in the format MAJOR.MINOR.PATCH.</returns>
  1733. public static sl.ERROR_CODE Reboot(int serialNumber, bool fullReboot = true)
  1734. {
  1735. return (sl.ERROR_CODE)dllz_reboot(serialNumber, fullReboot);
  1736. }
  1737. /// <summary>
  1738. /// Checks if the camera has been initialized and the plugin has been loaded. Throws exceptions otherwise.
  1739. /// </summary>
  1740. private void AssertCameraIsReady()
  1741. {
  1742. if (!cameraReady)
  1743. throw new Exception("ZED camera is not connected or Init() was not called.");
  1744. if (!pluginIsReady)
  1745. throw new Exception("Could not resolve ZED plugin dependencies.");
  1746. }
  1747. /// <summary>
  1748. /// Deploys an event that causes the textures to be updated with images received from the ZED.
  1749. /// Should be called after RetrieveTextures() so there are new images available.
  1750. /// </summary>
  1751. public void UpdateTextures()
  1752. {
  1753. GL.IssuePluginEvent(GetRenderEventFunc(), 1);
  1754. }
  1755. ///////////////////////////// SINGLE PIXEL UTILITY FUNCTIONS ////////////////////////////////
  1756. /// <summary>
  1757. /// Gets the current depth value of a pixel in the UNITS specified when the camera was started with Init().
  1758. /// May result in errors if the ZED image does not fill the whole screen.
  1759. /// <param name="position">The pixel's screen space coordinates as a Vector3.
  1760. /// The Z component is unused - designed to take input from Input.mousePosition.</param>
  1761. /// <returns>Depth value as a float.</returns>
  1762. /// </summary>
  1763. public float GetDepthValue(Vector3 pixel)
  1764. {
  1765. if (!cameraReady)
  1766. {
  1767. return -1;
  1768. }
  1769. float posX = (float)ImageWidth * (float)((float)pixel.x / (float)Screen.width);
  1770. float posY = ImageHeight * (1 - (float)pixel.y / (float)Screen.height);
  1771. posX = Mathf.Clamp(posX, 0, ImageWidth);
  1772. posY = Mathf.Clamp(posY, 0, ImageHeight);
  1773. float d = dllz_get_depth_value(CameraID, (uint)posX, (uint)posY);
  1774. return d;
  1775. }
  1776. /// <summary>
  1777. /// Gets the current Euclidean distance (sqrt(x²+y²+z²)) of the targeted pixel of the screen to the camera.
  1778. /// May result in errors if the ZED image does not fill the whole screen.
  1779. /// <param name="pixel">The pixel's screen space coordinates as a Vector3.
  1780. /// The Z component is unused - designed to take input from Input.mousePosition.</param>
  1781. /// <returns>Distance as a float.</returns>
  1782. /// </summary>
  1783. public float GetDistanceValue(Vector3 pixel)
  1784. {
  1785. if (!cameraReady) //Do nothing if the ZED isn't initialized.
  1786. {
  1787. return -1;
  1788. }
  1789. float posX = ImageWidth * (float)pixel.x / (float)Screen.width;
  1790. float posY = ImageHeight * (1 - (float)pixel.y / (float)Screen.height);
  1791. posX = Mathf.Clamp(posX, 0, ImageWidth);
  1792. posY = Mathf.Clamp(posY, 0, ImageHeight);
  1793. return dllz_get_distance_value(CameraID, (uint)posX, (uint)posY);
  1794. }
  1795. /// <summary>
  1796. /// Gets the position of a camera-space pixel relative to the camera frame.
  1797. /// <param name="pixel">The pixel's screen space coordinates as a Vector3.
  1798. /// The Z component is unused - designed to take input from Input.mousePosition.</param>
  1799. /// <param name="xyz">Position relative to the camera.</param>
  1800. /// <returns>True if successful.</returns>
  1801. /// </summary>
  1802. public bool GetXYZValue(Vector3 pixel, out Vector4 xyz)
  1803. {
  1804. if (!cameraReady) //Do nothing if the ZED isn't initialized.
  1805. {
  1806. xyz = Vector3.zero;
  1807. return false;
  1808. }
  1809. float posX = (float)ImageWidth * (float)((float)pixel.x / (float)Screen.width);
  1810. float posY = ImageHeight * (1 - (float)pixel.y / (float)Screen.height);
  1811. posX = Mathf.Clamp(posX, 0, ImageWidth);
  1812. posY = Mathf.Clamp(posY, 0, ImageHeight);
  1813. bool r = dllz_get_xyz_value(CameraID, (uint)posX, (uint)posY, out xyz);
  1814. return r;
  1815. }
  1816. /// <summary>
  1817. /// Gets the normal of a camera-space pixel. The normal is relative to the camera.
  1818. /// Use cam.worldToCameraMatrix.inverse to transform it to world space.
  1819. /// Note that ZEDSupportFunctions contains high-level versions of this function that are easier to use.
  1820. /// <param name="pixel">The pixel's screen space coordinates as a Vector3.
  1821. /// The Z component is unused - designed to take input from Input.mousePosition.</param>
  1822. /// <param name="normal">Normal value of the pixel as a Vector4.</param>
  1823. /// <returns>True if successful.</returns>
  1824. /// </summary>
  1825. public bool GetNormalValue(Vector3 pixel, out Vector4 normal)
  1826. {
  1827. if (!cameraReady) //Do nothing if the ZED isn't initialized.
  1828. {
  1829. normal = Vector3.zero;
  1830. return false;
  1831. }
  1832. float posX = (float)ImageWidth * (float)((float)pixel.x / (float)Screen.width);
  1833. float posY = ImageHeight * (1 - (float)pixel.y / (float)Screen.height);
  1834. posX = Mathf.Clamp(posX, 0, ImageWidth);
  1835. posY = Mathf.Clamp(posY, 0, ImageHeight);
  1836. bool r = dllz_get_normal_value(CameraID, (uint)posX, (uint)posY, out normal);
  1837. return r;
  1838. }
  1839. /// <summary>
  1840. /// Initializes and begins the spatial mapping processes.
  1841. /// </summary>
  1842. /// <param name="resolution_meter">Spatial mapping resolution in meters.</param>
  1843. /// <param name="max_range_meter">Maximum scanning range in meters.</param>
  1844. /// <param name="saveTexture">True to scan surface textures in addition to geometry.</param>
  1845. /// <returns></returns>
  1846. public sl.ERROR_CODE EnableSpatialMapping(SPATIAL_MAP_TYPE type, float resolution_meter, float max_range_meter, bool saveTexture = false)
  1847. {
  1848. sl.ERROR_CODE spatialMappingStatus = ERROR_CODE.FAILURE;
  1849. //lock (grabLock)
  1850. {
  1851. spatialMappingStatus = (sl.ERROR_CODE)dllz_enable_spatial_mapping(CameraID, (int)type,resolution_meter, max_range_meter, System.Convert.ToInt32(saveTexture), 4096);
  1852. }
  1853. return spatialMappingStatus;
  1854. }
  1855. /// <summary>
  1856. /// Disables the Spatial Mapping process.
  1857. /// </summary>
  1858. public void DisableSpatialMapping()
  1859. {
  1860. lock (grabLock)
  1861. {
  1862. dllz_disable_spatial_mapping(CameraID);
  1863. }
  1864. }
  1865. /// <summary>
  1866. /// Updates the internal version of the mesh and returns the sizes of the meshes.
  1867. /// </summary>
  1868. /// <param name="nbVerticesInSubmeshes">Array of the number of vertices in each submesh.</param>
  1869. /// <param name="nbTrianglesInSubmeshes">Array of the number of triangles in each submesh.</param>
  1870. /// <param name="nbSubmeshes">Number of submeshes.</param>
  1871. /// <param name="updatedIndices">List of all submeshes updated since the last update.</param>
  1872. /// <param name="nbVertices">Total number of updated vertices in all submeshes.</param>
  1873. /// <param name="nbTriangles">Total number of updated triangles in all submeshes.</param>
  1874. /// <param name="nbSubmeshMax">Maximum number of submeshes that can be handled.</param>
  1875. /// <returns>Error code indicating if the update was successful, and why it wasn't otherwise.</returns>
  1876. public sl.ERROR_CODE UpdateMesh(int[] nbVerticesInSubmeshes, int[] nbTrianglesInSubmeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmeshMax)
  1877. {
  1878. sl.ERROR_CODE err = sl.ERROR_CODE.FAILURE;
  1879. err = (sl.ERROR_CODE)dllz_update_mesh(CameraID, nbVerticesInSubmeshes, nbTrianglesInSubmeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmeshMax);
  1880. return err;
  1881. }
  1882. /// <summary>
  1883. /// Retrieves all chunks of the generated mesh. Call UpdateMesh() before calling this.
  1884. /// Vertex and triangle arrays must be at least of the sizes returned by UpdateMesh (nbVertices and nbTriangles).
  1885. /// </summary>
  1886. /// <param name="vertices">Vertices of the mesh.</param>
  1887. /// <param name="triangles">Triangles, formatted as the index of each triangle's three vertices in the vertices array.</param>
  1888. /// <param name="nbSubmeshMax">Maximum number of submeshes that can be handled.</param>
  1889. /// <returns>Error code indicating if the retrieval was successful, and why it wasn't otherwise.</returns>
  1890. public sl.ERROR_CODE RetrieveMesh(Vector3[] vertices, int[] triangles, int nbSubmeshMax, Vector2[] uvs, IntPtr textures)
  1891. {
  1892. return (sl.ERROR_CODE)dllz_retrieve_mesh(CameraID, vertices, triangles, nbSubmeshMax, uvs, textures);
  1893. }
  1894. /// <summary>
  1895. /// Updates the fused point cloud (if spatial map type was FUSED_POINT_CLOUD)
  1896. /// </summary>
  1897. /// <returns>Error code indicating if the update was successful, and why it wasn't otherwise.</returns>
  1898. public sl.ERROR_CODE UpdateFusedPointCloud(ref int nbVertices)
  1899. {
  1900. sl.ERROR_CODE err = sl.ERROR_CODE.FAILURE;
  1901. err = (sl.ERROR_CODE)dllz_update_fused_point_cloud(CameraID, ref nbVertices);
  1902. return err;
  1903. }
  1904. /// <summary>
  1905. /// Retrieves all points of the fused point cloud. Call UpdateFusedPointCloud() before calling this.
  1906. /// Vertex arrays must be at least of the sizes returned by UpdateFusedPointCloud
  1907. /// </summary>
  1908. /// <param name="vertices">Points of the fused point cloud.</param>
  1909. /// <returns>Error code indicating if the retrieval was successful, and why it wasn't otherwise.</returns>
  1910. public sl.ERROR_CODE RetrieveFusedPointCloud(Vector4[] vertices)
  1911. {
  1912. return (sl.ERROR_CODE)dllz_retrieve_fused_point_cloud(CameraID, vertices);
  1913. }
  1914. /// <summary>
  1915. /// Starts the mesh generation process in a thread that doesn't block the spatial mapping process.
  1916. /// ZEDSpatialMappingHelper calls this each time it has finished applying the last mesh update.
  1917. /// </summary>
  1918. public void RequestMesh()
  1919. {
  1920. dllz_request_mesh_async(CameraID);
  1921. }
  1922. /// <summary>
  1923. /// Sets the pause state of the data integration mechanism for the ZED's spatial mapping.
  1924. /// </summary>
  1925. /// <param name="status">If true, the integration is paused. If false, the spatial mapping is resumed.</param>
  1926. public void PauseSpatialMapping(bool status)
  1927. {
  1928. dllz_pause_spatial_mapping(CameraID, status);
  1929. }
  1930. /// <summary>
  1931. /// Returns the mesh generation status. Useful for knowing when to update and retrieve the mesh.
  1932. /// </summary>
  1933. public sl.ERROR_CODE GetMeshRequestStatus()
  1934. {
  1935. return (sl.ERROR_CODE)dllz_get_mesh_request_status_async(CameraID);
  1936. }
  1937. /// <summary>
  1938. /// Saves the scanned mesh in a specific file format.
  1939. /// </summary>
  1940. /// <param name="filename">Path and filename of the mesh.</param>
  1941. /// <param name="format">File format (extension). Can be .obj, .ply or .bin.</param>
  1942. public bool SaveMesh(string filename, MESH_FILE_FORMAT format)
  1943. {
  1944. return dllz_save_mesh(CameraID, filename, format);
  1945. }
  1946. /// <summary>
  1947. /// Saves the scanned point cloud in a specific file format.
  1948. /// </summary>
  1949. /// <param name="filename">Path and filename of the point cloud.</param>
  1950. /// <param name="format">File format (extension). Can be .obj, .ply or .bin.</param>
  1951. public bool SavePointCloud(string filename, MESH_FILE_FORMAT format)
  1952. {
  1953. return dllz_save_point_cloud(CameraID, filename, format);
  1954. }
  1955. /// <summary>
  1956. /// Loads a saved mesh file. ZEDSpatialMapping then configures itself as if the loaded mesh was just scanned.
  1957. /// </summary>
  1958. /// <param name="filename">Path and filename of the mesh. Should include the extension (.obj, .ply or .bin).</param>
  1959. /// <param name="nbVerticesInSubmeshes">Array of the number of vertices in each submesh.</param>
  1960. /// <param name="nbTrianglesInSubmeshes">Array of the number of triangles in each submesh.</param>
  1961. /// <param name="nbSubmeshes">Number of submeshes.</param>
  1962. /// <param name="updatedIndices">List of all submeshes updated since the last update.</param>
  1963. /// <param name="nbVertices">Total number of updated vertices in all submeshes.</param>
  1964. /// <param name="nbTriangles">Total number of updated triangles in all submeshes.</param>
  1965. /// <param name="nbSubmeshMax">Maximum number of submeshes that can be handled.</param>
  1966. /// <param name="textureSize">Array containing the sizes of all the textures (width, height) if applicable.</param>
  1967. public bool LoadMesh(string filename, int[] nbVerticesInSubmeshes, int[] nbTrianglesInSubmeshes, ref int nbSubmeshes, int[] updatedIndices,
  1968. ref int nbVertices, ref int nbTriangles, int nbSubmeshMax, int[] textureSize = null)
  1969. {
  1970. return dllz_load_mesh(CameraID, filename, nbVerticesInSubmeshes, nbTrianglesInSubmeshes, ref nbSubmeshes, updatedIndices, ref nbVertices,
  1971. ref nbTriangles, nbSubmeshMax, textureSize);
  1972. }
  1973. /// <summary>
  1974. /// Filters a mesh to remove triangles while still preserving its overall shape (though less accurate).
  1975. /// </summary>
  1976. /// <param name="filterParameters">Filter level. Higher settings remove more triangles.</param>
  1977. /// <param name="nbVerticesInSubmeshes">Array of the number of vertices in each submesh.</param>
  1978. /// <param name="nbTrianglesInSubmeshes">Array of the number of triangles in each submesh.</param>
  1979. /// <param name="nbSubmeshes">Number of submeshes.</param>
  1980. /// <param name="updatedIndices">List of all submeshes updated since the last update.</param>
  1981. /// <param name="nbVertices">Total number of updated vertices in all submeshes.</param>
  1982. /// <param name="nbTriangles">Total number of updated triangles in all submeshes.</param>
  1983. /// <param name="nbSubmeshMax">Maximum number of submeshes that can be handled.</param>
  1984. public bool FilterMesh(FILTER filterParameters, int[] nbVerticesInSubemeshes, int[] nbTrianglesInSubemeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmeshMax)
  1985. {
  1986. return dllz_filter_mesh(CameraID, filterParameters, nbVerticesInSubemeshes, nbTrianglesInSubemeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmeshMax);
  1987. }
  1988. /// <summary>
  1989. /// Applies the scanned texture onto the internal scanned mesh.
  1990. /// You will need to call RetrieveMesh() with uvs and textures to get the result into Unity.
  1991. /// </summary>
  1992. /// <param name="nbVerticesInSubmeshes">Array of the number of vertices in each submesh.</param>
  1993. /// <param name="nbTrianglesInSubmeshes">Array of the number of triangles in each submesh.</param>
  1994. /// <param name="nbSubmeshes">Number of submeshes.</param>
  1995. /// <param name="updatedIndices">List of all submeshes updated since the last update.</param>
  1996. /// <param name="nbVertices">Total number of updated vertices in all submeshes.</param>
  1997. /// <param name="nbTriangles">Total number of updated triangles in all submeshes.</param>
  1998. /// <param name="textureSize"> Vector containing the size of all the texture (width, height). </param>
  1999. /// <param name="nbSubmeshMax">Maximum number of submeshes that can be handled.</param>
  2000. /// <returns></returns>
  2001. public bool ApplyTexture(int[] nbVerticesInSubmeshes, int[] nbTrianglesInSubmeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int[] textureSize, int nbSubmeshMax)
  2002. {
  2003. return dllz_apply_texture(CameraID, nbVerticesInSubmeshes, nbTrianglesInSubmeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, textureSize, nbSubmeshMax);
  2004. }
  2005. /// <summary>
  2006. /// Gets the current state of spatial mapping.
  2007. /// </summary>
  2008. /// <returns></returns>
  2009. public SPATIAL_MAPPING_STATE GetSpatialMappingState()
  2010. {
  2011. return (sl.SPATIAL_MAPPING_STATE)dllz_get_spatial_mapping_state(CameraID);
  2012. }
  2013. /// <summary>
  2014. /// Gets a vector pointing toward the direction of gravity. This is estimated from a 3D scan of the environment,
  2015. /// and as such, a scan must be started/finished for this value to be calculated.
  2016. /// If using the ZED Mini / ZED2, this isn't required thanks to its IMU.
  2017. /// </summary>
  2018. /// <returns>Vector3 pointing downward.</returns>
  2019. public Vector3 GetGravityEstimate()
  2020. {
  2021. Vector3 v = Vector3.zero;
  2022. dllz_spatial_mapping_get_gravity_estimation(CameraID, ref v);
  2023. return v;
  2024. }
  2025. /// <summary>
  2026. /// Consolidates the chunks from a scan. This is used to turn lots of small meshes (which are efficient for
  2027. /// the scanning process) into several large meshes (which are more convenient to work with).
  2028. /// </summary>
  2029. /// <param name="numberFaces"></param>
  2030. /// <param name="nbVerticesInSubmeshes">Array of the number of vertices in each submesh.</param>
  2031. /// <param name="nbTrianglesInSubmeshes">Array of the number of triangles in each submesh.</param>
  2032. /// <param name="nbSubmeshes">Number of submeshes.</param>
  2033. /// <param name="updatedIndices">List of all submeshes updated since the last update.</param>
  2034. /// <param name="nbVertices">Total number of updated vertices in all submeshes.</param>
  2035. /// <param name="nbTriangles">Total number of updated triangles in all submeshes.</param>
  2036. public void MergeChunks(int numberFaces, int[] nbVerticesInSubmeshes, int[] nbTrianglesInSubmeshes, ref int nbSubmeshes, int[] updatedIndices, ref int nbVertices, ref int nbTriangles, int nbSubmesh)
  2037. {
  2038. dllz_spatial_mapping_merge_chunks(CameraID, numberFaces, nbVerticesInSubmeshes, nbTrianglesInSubmeshes, ref nbSubmeshes, updatedIndices, ref nbVertices, ref nbTriangles, nbSubmesh);
  2039. }
  2040. /// <summary>
  2041. /// Retrieves a measure texture from the ZED SDK and loads it into a ZEDMat. Use this to get an individual
  2042. /// texture from the last grabbed frame with measurements in every pixel - such as a depth map, confidence map, etc.
  2043. /// Measure textures are not human-viewable but don't lose accuracy, unlike image textures.
  2044. /// </summary><remarks>
  2045. /// If you want to access the texture via script, you'll usually want to specify CPU memory. Then you can use
  2046. /// Marshal.Copy to move them into a new byte array, which you can load into a Texture2D.
  2047. /// RetrieveMeasure() calls Camera::retrieveMeasure() in the C++ SDK. For more info, read:
  2048. /// https://www.stereolabs.com/developers/documentation/API/v2.5.1/classsl_1_1Camera.html#af799d12342a7b884242fffdef5588a7f
  2049. /// </remarks>
  2050. /// <param name="mat">ZEDMat to fill with the new texture.</param>
  2051. /// <param name="measure">Measure type (depth, confidence, xyz, etc.)</param>
  2052. /// <param name="mem">Whether the image should be on CPU or GPU memory.</param>
  2053. /// <param name="resolution">Resolution of the texture.</param>
  2054. /// <returns>Error code indicating if the retrieval was successful, and why it wasn't otherwise.</returns>
  2055. public sl.ERROR_CODE RetrieveMeasure(sl.ZEDMat mat, sl.MEASURE measure, sl.ZEDMat.MEM mem = sl.ZEDMat.MEM.MEM_CPU, sl.Resolution resolution = new sl.Resolution())
  2056. {
  2057. return (sl.ERROR_CODE)(dllz_retrieve_measure(CameraID, mat.MatPtr, (int)measure, (int)mem, (int)resolution.width, (int)resolution.height));
  2058. }
  2059. /// <summary>
  2060. /// Retrieves an image texture from the ZED SDK and loads it into a ZEDMat. Use this to get an individual
  2061. /// texture from the last grabbed frame in a human-viewable format. Image textures work for when you want the result to be visible,
  2062. /// such as the direct RGB image from the camera, or a greyscale image of the depth. However it will lose accuracy if used
  2063. /// to show measurements like depth or confidence, unlike measure textures.
  2064. /// </summary><remarks>
  2065. /// If you want to access the texture via script, you'll usually want to specify CPU memory. Then you can use
  2066. /// Marshal.Copy to move them into a new byte array, which you can load into a Texture2D. Note that you may need to
  2067. /// change the color space and/or flip the image.
  2068. /// RetrieveMeasure() calls Camera::retrieveMeasure() in the C++ SDK. For more info, read:
  2069. /// https://www.stereolabs.com/developers/documentation/API/v2.5.1/classsl_1_1Camera.html#ac40f337ccc76cacd3412b93f7f4638e2
  2070. /// </remarks>
  2071. /// <param name="mat">ZEDMat to fill with the new texture.</param>
  2072. /// <param name="view">Image type (left RGB, right depth map, etc.)</param>
  2073. /// <param name="mem">Whether the image should be on CPU or GPU memory.</param>
  2074. /// <param name="resolution">Resolution of the texture.</param>
  2075. /// <returns>Error code indicating if the retrieval was successful, and why it wasn't otherwise.</returns>
  2076. public sl.ERROR_CODE RetrieveImage(sl.ZEDMat mat, sl.VIEW view, sl.ZEDMat.MEM mem = sl.ZEDMat.MEM.MEM_CPU, sl.Resolution resolution = new sl.Resolution())
  2077. {
  2078. return (sl.ERROR_CODE)(dllz_retrieve_image(CameraID, mat.MatPtr, (int)view, (int)mem, (int)resolution.width, (int)resolution.height));
  2079. }
  2080. /// <summary>
  2081. /// Computes offsets of the optical centers used to line up the ZED's images properly with Unity cameras.
  2082. /// Called in ZEDRenderingPlane after the ZED finished initializing.
  2083. /// </summary>
  2084. /// <param name="planeDistance">Distance from a camera in the ZED rig to the quad/Canvas object holding the ZED image.</param>
  2085. /// <returns></returns>
  2086. public Vector4 ComputeOpticalCenterOffsets(float planeDistance)
  2087. {
  2088. IntPtr p = IntPtr.Zero;
  2089. sl.CalibrationParameters calib = GetCalibrationParameters(false);
  2090. Vector4 calibLeft = new Vector4(calib.leftCam.fx, calib.leftCam.fy, calib.leftCam.cx, calib.leftCam.cy);
  2091. Vector4 calibRight = new Vector4(calib.rightCam.fx, calib.rightCam.fy, calib.rightCam.cx, calib.rightCam.cy);
  2092. p = dllz_compute_optical_center_offsets(ref calibLeft, ref calibRight, this.ImageWidth, this.ImageHeight, planeDistance);
  2093. if (p == IntPtr.Zero)
  2094. {
  2095. return new Vector4();
  2096. }
  2097. Vector4 parameters = (Vector4)Marshal.PtrToStructure(p, typeof(Vector4));
  2098. return parameters;
  2099. }
  2100. ////////////////////////
  2101. /// Plane Detection ///
  2102. ////////////////////////
  2103. /// <summary>
  2104. /// Looks for a plane in the visible area that is likely to represent the floor.
  2105. /// Use ZEDPlaneDetectionManager.DetectFloorPlane for a higher-level version that turns planes into GameObjects.
  2106. /// </summary>
  2107. /// <param name="plane">Data on the detected plane.</param>
  2108. /// <param name="playerHeight">Height of the camera from the newly-detected floor.</param>
  2109. /// <param name="priorQuat">Prior rotation.</param>
  2110. /// <param name="priorTrans">Prior position.</param>
  2111. /// <returns></returns>
  2112. public sl.ERROR_CODE findFloorPlane(ref ZEDPlaneGameObject.PlaneData plane, out float playerHeight, Quaternion priorQuat, Vector3 priorTrans)
  2113. {
  2114. IntPtr p = IntPtr.Zero;
  2115. Quaternion out_quat = Quaternion.identity;
  2116. Vector3 out_trans = Vector3.zero;
  2117. p = dllz_find_floor_plane(CameraID, out out_quat, out out_trans, priorQuat, priorTrans);
  2118. plane.Bounds = new Vector3[256];
  2119. playerHeight = 0;
  2120. if (p != IntPtr.Zero)
  2121. {
  2122. plane = (ZEDPlaneGameObject.PlaneData)Marshal.PtrToStructure(p, typeof(ZEDPlaneGameObject.PlaneData));
  2123. playerHeight = out_trans.y;
  2124. return (sl.ERROR_CODE)plane.ErrorCode;
  2125. }
  2126. else
  2127. return sl.ERROR_CODE.FAILURE;
  2128. }
  2129. /// <summary>
  2130. /// Using data from a detected floor plane, updates supplied vertex and triangle arrays with
  2131. /// data needed to make a mesh that represents it. These arrays are updated directly from the wrapper.
  2132. /// </summary>
  2133. /// <param name="vertices">Array to be filled with mesh vertices.</param>
  2134. /// <param name="triangles">Array to be filled with mesh triangles, stored as indexes of each triangle's points.</param>
  2135. /// <param name="numVertices">Total vertices in the mesh.</param>
  2136. /// <param name="numTriangles">Total triangle indexes (3x number of triangles).</param>
  2137. /// <returns></returns>
  2138. public int convertFloorPlaneToMesh(Vector3[] vertices, int[] triangles, out int numVertices, out int numTriangles)
  2139. {
  2140. return dllz_convert_floorplane_to_mesh(CameraID, vertices, triangles, out numVertices, out numTriangles);
  2141. }
  2142. /// <summary>
  2143. /// Checks for a plane in the real world at given screen-space coordinates.
  2144. /// Use ZEDPlaneDetectionManager.DetectPlaneAtHit() for a higher-level version that turns planes into GameObjects.
  2145. /// </summary>
  2146. /// <param name="plane">Data on the detected plane.</param>
  2147. /// <param name="screenPos">Point on the ZED image to check for a plane.</param>
  2148. /// <returns></returns>
  2149. public sl.ERROR_CODE findPlaneAtHit(ref ZEDPlaneGameObject.PlaneData plane, Vector2 screenPos)
  2150. {
  2151. IntPtr p = IntPtr.Zero;
  2152. Quaternion out_quat = Quaternion.identity;
  2153. Vector3 out_trans = Vector3.zero;
  2154. float posX = (float)ImageWidth * (float)((float)screenPos.x / (float)Screen.width);
  2155. float posY = ImageHeight * (1 - (float)screenPos.y / (float)Screen.height);
  2156. posX = Mathf.Clamp(posX, 0, ImageWidth);
  2157. posY = Mathf.Clamp(posY, 0, ImageHeight);
  2158. p = dllz_find_plane_at_hit(CameraID, new Vector2(posX, posY), true);
  2159. plane.Bounds = new Vector3[256];
  2160. if (p != IntPtr.Zero)
  2161. {
  2162. plane = (ZEDPlaneGameObject.PlaneData)Marshal.PtrToStructure(p, typeof(ZEDPlaneGameObject.PlaneData));
  2163. return (sl.ERROR_CODE)plane.ErrorCode;
  2164. }
  2165. else
  2166. return sl.ERROR_CODE.FAILURE;
  2167. }
  2168. /// <summary>
  2169. /// Using data from a detected hit plane, updates supplied vertex and triangle arrays with
  2170. /// data needed to make a mesh that represents it. These arrays are updated directly from the wrapper.
  2171. /// </summary>
  2172. /// <param name="vertices">Array to be filled with mesh vertices.</param>
  2173. /// <param name="triangles">Array to be filled with mesh triangles, stored as indexes of each triangle's points.</param>
  2174. /// <param name="numVertices">Total vertices in the mesh.</param>
  2175. /// <param name="numTriangles">Total triangle indexes (3x number of triangles).</param>
  2176. /// <returns></returns>
  2177. public int convertHitPlaneToMesh(Vector3[] vertices, int[] triangles, out int numVertices, out int numTriangles)
  2178. {
  2179. return dllz_convert_hitplane_to_mesh(CameraID, vertices, triangles, out numVertices, out numTriangles);
  2180. }
  2181. ////////////////////////
  2182. /// Streaming Module ///
  2183. ////////////////////////
  2184. /// <summary>
  2185. /// Creates an streaming pipeline.
  2186. /// </summary>
  2187. /// <params>
  2188. /// Streaming parameters: See sl::StreamingParameters of ZED SDK. See ZED SDK API doc for more informations
  2189. /// </params>
  2190. /// <returns>An ERROR_CODE that defines if the streaming pipe was successfully created</returns>
  2191. public ERROR_CODE EnableStreaming(STREAMING_CODEC codec = STREAMING_CODEC.AVCHD_BASED, uint bitrate = 8000, ushort port = 30000, int gopSize = -1, bool adaptativeBitrate = false,int chunk_size = 8096,int target_fps = 0)
  2192. {
  2193. int doAdaptBitrate = adaptativeBitrate ? 1 : 0;
  2194. return (ERROR_CODE)dllz_enable_streaming(CameraID, codec, bitrate, port, gopSize, doAdaptBitrate, chunk_size,target_fps);
  2195. }
  2196. /// <summary>
  2197. /// Tells if streaming is running or not.
  2198. /// </summary>
  2199. /// <returns> false if streaming is not enabled, true if streaming is on</returns>
  2200. public bool IsStreamingEnabled()
  2201. {
  2202. int res = dllz_is_streaming_enabled(CameraID);
  2203. if (res == 1)
  2204. return true;
  2205. else
  2206. return false;
  2207. }
  2208. /// <summary>
  2209. /// Stops the streaming pipeline.
  2210. /// </summary>
  2211. public void DisableStreaming()
  2212. {
  2213. dllz_disable_streaming(CameraID);
  2214. }
  2215. ////////////////////////
  2216. /// Save utils fct ///
  2217. ////////////////////////
  2218. /// <summary>
  2219. /// Save current image (specified by view) in a file defined by filename
  2220. /// Supported formats are jpeg and png. Filename must end with either .jpg or .png
  2221. /// </summary>
  2222. public sl.ERROR_CODE SaveCurrentImageInFile(sl.VIEW view, String filename)
  2223. {
  2224. sl.ERROR_CODE err = (sl.ERROR_CODE)dllz_save_current_image(CameraID, view, filename);
  2225. return err;
  2226. }
  2227. /// <summary>
  2228. /// Save the current depth in a file defined by filename.
  2229. /// Supported formats are PNG,PFM and PGM
  2230. /// </summary>
  2231. /// <param name="side"> defines left (0) or right (1) depth</param>
  2232. /// <param name="filename"> filename must end with .png, .pfm or .pgm</param>
  2233. /// <returns></returns>
  2234. public sl.ERROR_CODE SaveCurrentDepthInFile(int side, String filename)
  2235. {
  2236. sl.ERROR_CODE err = (sl.ERROR_CODE)dllz_save_current_depth(CameraID, side, filename);
  2237. return err;
  2238. }
  2239. /// <summary>
  2240. /// Save the current point cloud in a file defined by filename.
  2241. /// Supported formats are PLY,VTK,XYZ and PCD
  2242. /// </summary>
  2243. /// <param name="side">defines left (0) or right (1) point cloud</param>
  2244. /// <param name="filename"> filename must end with .ply, .xyz , .vtk or .pcd </param>
  2245. /// <returns></returns>
  2246. public sl.ERROR_CODE SaveCurrentPointCloudInFile(int side, String filename)
  2247. {
  2248. sl.ERROR_CODE err = (sl.ERROR_CODE)dllz_save_current_point_cloud(CameraID, side, filename);
  2249. return err;
  2250. }
  2251. ////////////////////////
  2252. /// Object detection ///
  2253. ////////////////////////
  2254. public static sl.AI_MODELS cvtDetection(sl.DETECTION_MODEL m_in)
  2255. {
  2256. sl.AI_MODELS m_out = sl.AI_MODELS.LAST;
  2257. switch (m_in)
  2258. {
  2259. case sl.DETECTION_MODEL.HUMAN_BODY_ACCURATE: m_out = sl.AI_MODELS.HUMAN_BODY_ACCURATE_DETECTION; break;
  2260. case sl.DETECTION_MODEL.HUMAN_BODY_MEDIUM: m_out = sl.AI_MODELS.HUMAN_BODY_MEDIUM_DETECTION; break;
  2261. case sl.DETECTION_MODEL.HUMAN_BODY_FAST: m_out = sl.AI_MODELS.HUMAN_BODY_FAST_DETECTION; break;
  2262. case sl.DETECTION_MODEL.MULTI_CLASS_BOX_ACCURATE: m_out = sl.AI_MODELS.MULTI_CLASS_ACCURATE_DETECTION; break;
  2263. case sl.DETECTION_MODEL.MULTI_CLASS_BOX_MEDIUM: m_out = sl.AI_MODELS.MULTI_CLASS_MEDIUM_DETECTION; break;
  2264. case sl.DETECTION_MODEL.MULTI_CLASS_BOX: m_out = sl.AI_MODELS.MULTI_CLASS_DETECTION; break;
  2265. case sl.DETECTION_MODEL.PERSON_HEAD_BOX: m_out = sl.AI_MODELS.PERSON_HEAD_DETECTION; break;
  2266. }
  2267. return m_out;
  2268. }
  2269. public static sl.DETECTION_MODEL cvtDetection(sl.AI_MODELS m_in)
  2270. {
  2271. sl.DETECTION_MODEL m_out = sl.DETECTION_MODEL.LAST;
  2272. switch (m_in)
  2273. {
  2274. case sl.AI_MODELS.HUMAN_BODY_ACCURATE_DETECTION: m_out = sl.DETECTION_MODEL.HUMAN_BODY_ACCURATE; break;
  2275. case sl.AI_MODELS.HUMAN_BODY_MEDIUM_DETECTION: m_out = sl.DETECTION_MODEL.HUMAN_BODY_MEDIUM; break;
  2276. case sl.AI_MODELS.HUMAN_BODY_FAST_DETECTION: m_out = sl.DETECTION_MODEL.HUMAN_BODY_FAST; break;
  2277. case sl.AI_MODELS.MULTI_CLASS_ACCURATE_DETECTION: m_out = sl.DETECTION_MODEL.MULTI_CLASS_BOX_ACCURATE; break;
  2278. case sl.AI_MODELS.MULTI_CLASS_MEDIUM_DETECTION: m_out = sl.DETECTION_MODEL.MULTI_CLASS_BOX_MEDIUM; break;
  2279. case sl.AI_MODELS.MULTI_CLASS_DETECTION: m_out = sl.DETECTION_MODEL.MULTI_CLASS_BOX; break;
  2280. case sl.AI_MODELS.PERSON_HEAD_DETECTION: m_out = sl.DETECTION_MODEL.PERSON_HEAD_BOX; break;
  2281. }
  2282. return m_out;
  2283. }
  2284. /// <summary>
  2285. /// Check if a corresponding optimized engine is found for the requested Model based on your rig configuration.
  2286. /// </summary>
  2287. /// <param name="model"> AI model to check.</param>
  2288. /// <param name="gpu_id">ID of the gpu.</param>
  2289. /// <returns></returns>
  2290. public static AI_Model_status CheckAIModelStatus(AI_MODELS model, int gpu_id = 0)
  2291. {
  2292. IntPtr p = dllz_check_AI_model_status(model, gpu_id);
  2293. if (p == IntPtr.Zero)
  2294. {
  2295. return new AI_Model_status();
  2296. }
  2297. AI_Model_status status = (AI_Model_status)Marshal.PtrToStructure(p, typeof(AI_Model_status));
  2298. return status;
  2299. }
  2300. /// <summary>
  2301. /// Optimize the requested model, possible download if the model is not present on the host.
  2302. /// </summary>
  2303. /// <param name="model">AI model to optimize.</param>
  2304. /// <param name="gpu_id">ID of the gpu to optimize on.</param>
  2305. /// <returns></returns>
  2306. public static sl.ERROR_CODE OptimizeAIModel(AI_MODELS model, int gpu_id = 0)
  2307. {
  2308. return (sl.ERROR_CODE)dllz_optimize_AI_model(model, gpu_id);
  2309. }
  2310. /// <summary>
  2311. /// Enable object detection module
  2312. /// </summary>
  2313. public sl.ERROR_CODE EnableObjectsDetection(ref dll_ObjectDetectionParameters od_params)
  2314. {
  2315. sl.ERROR_CODE objDetectStatus = ERROR_CODE.FAILURE;
  2316. lock (grabLock)
  2317. {
  2318. objDetectStatus = (sl.ERROR_CODE)dllz_enable_objects_detection(CameraID, ref od_params);
  2319. }
  2320. return objDetectStatus;
  2321. }
  2322. /// <summary>
  2323. /// Disable object detection module and release the resources.
  2324. /// </summary>
  2325. public void DisableObjectsDetection()
  2326. {
  2327. lock (grabLock)
  2328. {
  2329. dllz_disable_objects_detection(CameraID);
  2330. }
  2331. }
  2332. /// <summary>
  2333. /// Pause or Unpause the object detection
  2334. /// </summary>
  2335. /// <param name="status"></param>
  2336. public void PauseObjectsDetection(bool status)
  2337. {
  2338. lock (grabLock)
  2339. {
  2340. dllz_pause_objects_detection(CameraID, status);
  2341. }
  2342. }
  2343. public sl.ERROR_CODE IngestCustomBoxObjects(List<CustomBoxObjectData> objects_in)
  2344. {
  2345. return (sl.ERROR_CODE)dllz_ingest_custom_box_objects(CameraID, objects_in.Count, objects_in.ToArray());
  2346. }
  2347. /// <summary>
  2348. /// Retrieve object detection data
  2349. /// </summary>
  2350. /// <param name="od_params"> Object detection runtime parameters</param>
  2351. /// <param name="objFrame"> ObjectsFrameSDK that contains all the detection data</param>
  2352. /// <returns></returns>
  2353. public sl.ERROR_CODE RetrieveObjectsDetectionData(ref dll_ObjectDetectionRuntimeParameters od_params, ref ObjectsFrameSDK objFrame)
  2354. {
  2355. return (sl.ERROR_CODE)dllz_retrieve_objects_data(CameraID, ref od_params, ref objFrame);
  2356. }
  2357. /// <summary>
  2358. /// Update the batch trajectories and retrieve the number of batches.
  2359. /// </summary>
  2360. /// <param name="nbBatches"> numbers of batches
  2361. /// <returns> returns an ERROR_CODE that indicates the type of error </returns>
  2362. public sl.ERROR_CODE UpdateObjectsBatch(out int nbBatches)
  2363. {
  2364. return (sl.ERROR_CODE)dllz_update_objects_batch(CameraID, out nbBatches);
  2365. }
  2366. /// <summary>
  2367. /// Retrieve a batch of objects.
  2368. /// This function need to be called after RetrieveObjects, otherwise trajectories will be empty.
  2369. /// If also needs to be called after UpdateOBjectsBatch in order to retrieve the number of batch trajectories.
  2370. /// </summary>
  2371. /// <remarks> To retrieve all the objectsbatches, you need to iterate from 0 to nbBatches (retrieved from UpdateObjectBatches) </remarks>
  2372. /// <param name="batch_index"> index of the batch retrieved.
  2373. /// <param name="objectsBatch"> trajectory that will be filled by the batching queue process</param>
  2374. /// <returns> returns an ERROR_CODE that indicates the type of error </returns>
  2375. public sl.ERROR_CODE GetObjectsBatch(int batch_index, ref ObjectsBatch objectsBatch)
  2376. {
  2377. return (sl.ERROR_CODE)dllz_get_objects_batch_data(CameraID, batch_index, ref objectsBatch.numData, ref objectsBatch.id, ref objectsBatch.label, ref objectsBatch.sublabel,
  2378. ref objectsBatch.trackingState, objectsBatch.positions, objectsBatch.positionCovariances, objectsBatch.velocities, objectsBatch.timestamps, objectsBatch.boundingBoxes2D,
  2379. objectsBatch.boundingBoxes, objectsBatch.confidences, objectsBatch.actionStates, objectsBatch.keypoints2D, objectsBatch.keypoints, objectsBatch.headBoundingBoxes2D,
  2380. objectsBatch.headBoundingBoxes, objectsBatch.headPositions, objectsBatch.keypointConfidences);
  2381. }
  2382. }//Zed Camera class
  2383. } // namespace sl