/// <summary> /// Create a 2D bounding box that contain all given points /// </summary> /// <param name="point_list"></param> /// <returns></returns> private static BoundingBox2D CreateBoundingBox2DFromPointList(Vector3 [] point_list) { BoundingBox2D bb_ret = new BoundingBox2D(); bb_ret.min = new Vector2(float.MaxValue, float.MaxValue); bb_ret.max = new Vector2(float.MinValue, float.MinValue); foreach (var point in point_list) { // get bb in 2d var screen_pos = GTAUtils.Convert3DPostoScreenPos(point); if (screen_pos.X == -1f || screen_pos.Y == -1f) { return(null); } // update bb bb_ret.min.X = Math.Min(bb_ret.min.X, screen_pos.X); bb_ret.min.Y = Math.Min(bb_ret.min.Y, screen_pos.Y); bb_ret.max.X = Math.Max(bb_ret.max.X, screen_pos.X); bb_ret.max.Y = Math.Max(bb_ret.max.Y, screen_pos.Y); } return(bb_ret); }
/// <summary> /// Perform detection data export (Toolchain method: call GTA5 native functions & utility functions) /// </summary> private void DoDetectionExport() { Wait(40); // find target objects around camera var camera_position = GameplayCamera.Position; List <Entity> found_entities = new List <Entity>(); // find pedestrians if (_save_settings.SaveObjectType == SaveObjectType.PEDESTRIANS || _save_settings.SaveObjectType == SaveObjectType.PED_AND_VEHICLES) { found_entities.AddRange(World.GetNearbyPeds(camera_position, _save_settings.DetectDistance).ToList()); } // find vehicles if (_save_settings.SaveObjectType == SaveObjectType.VEHICLES || _save_settings.SaveObjectType == SaveObjectType.PED_AND_VEHICLES) { found_entities.AddRange(World.GetNearbyVehicles(camera_position, _save_settings.DetectDistance).ToList()); } List <GTAUtils.BoundingBox2D> peds_bblist = new List <GTAUtils.BoundingBox2D>(); List <GTAUtils.BoundingBox2D> vehicle_bblist = new List <GTAUtils.BoundingBox2D>(); // detect 2D bounding box foreach (var entity in found_entities) { if (entity == null || entity.Model == null) { continue; } // check if is in screen if (!entity.IsVisible) { continue; } if (!entity.IsOnScreen) { continue; } if (entity.IsOccluded) { continue; } // check occlusion by ray-casting if (!GTAUtils.CheckOcculusionVisibility(entity, camera_position)) { continue; } // compute a bounding box var bb = GTAUtils.ComputeBoundingBox(entity, entity.Model.IsPed); // cannot get bounding box if (bb == null) { continue; } if (entity.Model.IsPed) { peds_bblist.Add(bb); } else { vehicle_bblist.Add(bb); } } var screenshot = Utils.GetPrimaryScreenImage(); // override to image var overdrawn_image = new Bitmap(screenshot); DrawBoundingBoxes(overdrawn_image, peds_bblist, Color.Red); DrawBoundingBoxes(overdrawn_image, vehicle_bblist, Color.Blue); // save image var filename_prefix = _script_started_time.ToString("yyyy-MM-dd_hh-mm-ss_"); var filepath_prefix = System.IO.Path.Combine(_save_settings.OutputDirectory, filename_prefix); var filenumber_str = string.Format("{0:D8}", _frame_count); Utils.SaveBitmap(overdrawn_image, filepath_prefix + "with_rect_" + filenumber_str + ".jpg"); Utils.SaveBitmap(screenshot, filepath_prefix + "original_" + filenumber_str + ".png"); }