public static GTAData DumpData(string imageName, List <Weather> capturedWeathers) { var ret = new GTAData(); ret.Version = 3; ret.ImageName = imageName; ret.CurrentWeather = World.Weather; ret.CapturedWeathers = capturedWeathers; ret.Timestamp = DateTime.UtcNow; ret.LocalTime = World.CurrentDayTime; ret.Pos = new GTAVector(GameplayCamera.Position); ret.CamDirection = new GTAVector(GameplayCamera.Direction); ret.CamFOV = GameplayCamera.FieldOfView; ret.ImageWidth = Game.ScreenResolution.Width; ret.ImageHeight = Game.ScreenResolution.Height; //ret.Pos = new GTAVector(Game.Player.Character.Position); var peds = World.GetNearbyPeds(Game.Player.Character, 150.0f); var cars = World.GetNearbyVehicles(Game.Player.Character, 150.0f); //var props = World.GetNearbyProps(Game.Player.Character.Position, 300.0f); var constants = VisionNative.GetConstants(); if (!constants.HasValue) { return(null); } var W = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, constants.Value.world.ToArray()).ToDouble(); var WV = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, constants.Value.worldView.ToArray()).ToDouble(); var WVP = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, constants.Value.worldViewProjection.ToArray()).ToDouble(); var V = WV * W.Inverse(); var P = WVP * WV.Inverse(); ret.ProjectionMatrix = P as DenseMatrix; ret.ViewMatrix = V as DenseMatrix; var pedList = from ped in peds where ped.IsHuman && ped.IsOnFoot select new GTADetection(ped); var cycles = from ped in peds where ped.IsOnBike select new GTADetection(ped, DetectionType.bicycle); var vehicleList = from car in cars select new GTADetection(car); ret.Detections = new List <GTADetection>(); ret.Detections.AddRange(pedList); ret.Detections.AddRange(vehicleList); //ret.Detections.AddRange(cycles); return(ret); }
public GTADetection(Entity e, DetectionType type) { Type = type; Pos = new GTAVector(e.Position); Distance = Game.Player.Character.Position.DistanceTo(e.Position); BBox = GTAData.ComputeBoundingBox(e, e.Position); Handle = e.Handle; Rot = new GTAVector(e.Rotation); cls = DetectionClass.Unknown; Vector3 gmin; Vector3 gmax; e.Model.GetDimensions(out gmin, out gmax); BBox3D = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax)); }
public GTAPed(Ped p) { Pos = new GTAVector(p.Position); BBox = GTAData.ComputeBoundingBox(p, p.Position); }
public GTAVehicle(Vehicle v) { Pos = new GTAVector(v.Position); BBox = GTAData.ComputeBoundingBox(v, v.Position); }
public static void SaveSnapshotImpl(GTAData data, Guid runId) { var conn = OpenConnection(); var trans = conn.BeginTransaction(); using (NpgsqlCommand cmd = new NpgsqlCommand()) { cmd.Connection = conn; cmd.Transaction = trans; cmd.CommandText = "INSERT INTO snapshots (run_id, version, imagepath, timestamp, timeofday, currentweather, camera_pos, camera_direction, camera_fov, view_matrix, proj_matrix, width, height) " + "VALUES ( (SELECT run_id FROM runs WHERE runguid=@guid), " + "@Version, @Imagepath, @Timestamp, @Timeofday, @currentweather, ST_MakePoint(@x, @y, @z), ST_MakePoint(@dirx, @diry, @dirz), @fov, @view_matrix, @proj_matrix, @width, @height ) " + "RETURNING snapshot_id;"; cmd.Parameters.Add(new NpgsqlParameter("@version", data.Version)); cmd.Parameters.Add(new NpgsqlParameter("@imagepath", data.ImageName)); cmd.Parameters.Add(new NpgsqlParameter("@timestamp", data.Timestamp)); cmd.Parameters.Add(new NpgsqlParameter("@timeofday", data.LocalTime)); cmd.Parameters.Add(new NpgsqlParameter("@currentweather", data.CurrentWeather)); cmd.Parameters.Add(new NpgsqlParameter("@x", data.Pos.X)); cmd.Parameters.Add(new NpgsqlParameter("@y", data.Pos.Y)); cmd.Parameters.Add(new NpgsqlParameter("@z", data.Pos.Z)); cmd.Parameters.AddWithValue("@dirx", data.CamDirection.X); cmd.Parameters.AddWithValue("@diry", data.CamDirection.Y); cmd.Parameters.AddWithValue("@dirz", data.CamDirection.Z); cmd.Parameters.AddWithValue("@fov", data.CamFOV); cmd.Parameters.AddWithValue("@view_matrix", data.ViewMatrix.ToArray()); cmd.Parameters.AddWithValue("@proj_matrix", data.ProjectionMatrix.ToArray()); cmd.Parameters.AddWithValue("@width", data.ImageWidth); cmd.Parameters.AddWithValue("@height", data.ImageHeight); cmd.Parameters.Add(new NpgsqlParameter("@guid", runId)); int snapshotid = (int)cmd.ExecuteScalar(); cmd.Parameters.Clear(); cmd.CommandText = "INSERT INTO snapshot_weathers (snapshot_id, weather_type, snapshot_page) VALUES (@snapshot, @weather, @page);"; cmd.Parameters.AddWithValue("@snapshot", NpgsqlDbType.Integer, snapshotid); cmd.Parameters.AddWithValue("@weather", NpgsqlDbType.Enum, Weather.Unknown); cmd.Parameters.Add("@page", NpgsqlDbType.Integer); cmd.Prepare(); for (int i = 0; i < data.CapturedWeathers.Count; ++i) { cmd.Parameters["@weather"].Value = data.CapturedWeathers[i]; cmd.Parameters["@page"].Value = i; cmd.ExecuteNonQuery(); } cmd.Parameters.Clear(); cmd.Parameters.Add("@snapshot", NpgsqlDbType.Integer); cmd.Parameters.AddWithValue("@type", NpgsqlDbType.Enum, DetectionType.background); cmd.Parameters.Add("@x", NpgsqlDbType.Real); cmd.Parameters.Add("@y", NpgsqlDbType.Real); cmd.Parameters.Add("@z", NpgsqlDbType.Real); cmd.Parameters.Add("@xrot", NpgsqlDbType.Real); cmd.Parameters.Add("@yrot", NpgsqlDbType.Real); cmd.Parameters.Add("@zrot", NpgsqlDbType.Real); cmd.Parameters.Add("@bbox", NpgsqlDbType.Box); cmd.Parameters.Add("@minx", NpgsqlDbType.Real); cmd.Parameters.Add("@miny", NpgsqlDbType.Real); cmd.Parameters.Add("@minz", NpgsqlDbType.Real); cmd.Parameters.Add("@maxx", NpgsqlDbType.Real); cmd.Parameters.Add("@maxy", NpgsqlDbType.Real); cmd.Parameters.Add("@maxz", NpgsqlDbType.Real); cmd.Parameters.AddWithValue("@class", NpgsqlDbType.Enum, DetectionClass.Unknown); cmd.Parameters.Add("@handle", NpgsqlDbType.Integer); cmd.CommandText = "INSERT INTO detections (snapshot_id, type, pos, rot, bbox, class, handle, bbox3d) VALUES " + "(@snapshot, @type, ST_MakePoint(@x,@y,@z), ST_MakePoint(@xrot, @yrot, @zrot), @bbox, @class, @handle," + "ST_3DMakeBox(ST_MakePoint(@minx,@miny,@minz), ST_MakePoint(@maxx, @maxy, @maxz)));"; cmd.Prepare(); foreach (var detection in data.Detections) { cmd.Parameters["@snapshot"].Value = snapshotid; cmd.Parameters["@type"].Value = detection.Type; cmd.Parameters["@x"].Value = detection.Pos.X; cmd.Parameters["@y"].Value = detection.Pos.Y; cmd.Parameters["@z"].Value = detection.Pos.Z; cmd.Parameters["@xrot"].Value = detection.Rot.X; cmd.Parameters["@yrot"].Value = detection.Rot.Y; cmd.Parameters["@zrot"].Value = detection.Rot.Z; cmd.Parameters["@bbox"].Value = new NpgsqlBox(detection.BBox.Max.Y, detection.BBox.Max.X, detection.BBox.Min.Y, detection.BBox.Min.X); cmd.Parameters["@class"].Value = detection.cls; cmd.Parameters["@handle"].Value = detection.Handle; cmd.Parameters["@minx"].Value = detection.BBox3D.Minimum.X; cmd.Parameters["@miny"].Value = detection.BBox3D.Minimum.Y; cmd.Parameters["@minz"].Value = detection.BBox3D.Minimum.Z; cmd.Parameters["@maxx"].Value = detection.BBox3D.Maximum.X; cmd.Parameters["@maxy"].Value = detection.BBox3D.Maximum.Y; cmd.Parameters["@maxz"].Value = detection.BBox3D.Maximum.Z; cmd.ExecuteNonQuery(); } } trans.Commit(); conn.Close(); }
public static async void SaveSnapshot(GTAData data, Guid runId) { await Task.Run(() => SaveSnapshotImpl(data, runId)); }
public static void SaveSnapshotImpl(GTAData data, Guid runId) { var conn = OpenConnection(); var trans = conn.BeginTransaction(); using (NpgsqlCommand cmd = new NpgsqlCommand()) { var camRelativeRotString = "NULL"; if (data.CamRelativeRot != null) { camRelativeRotString = "ST_MakePoint(@relative_rot_x, @relative_rot_y, @relative_rot_z)"; cmd.Parameters.AddWithValue("@relative_rot_x", data.CamRelativeRot.X); cmd.Parameters.AddWithValue("@relative_rot_y", data.CamRelativeRot.Y); cmd.Parameters.AddWithValue("@relative_rot_z", data.CamRelativeRot.Z); } var camRelativePosString = "NULL"; if (data.CamRelativePos != null) { camRelativePosString = "ST_MakePoint(@relative_pos_x, @relative_pos_y, @relative_pos_z)"; cmd.Parameters.AddWithValue("@relative_pos_x", data.CamRelativePos.X); cmd.Parameters.AddWithValue("@relative_pos_y", data.CamRelativePos.Y); cmd.Parameters.AddWithValue("@relative_pos_z", data.CamRelativePos.Z); } var carModelBoxString = "NULL"; if (data.CarModelBox != null) { carModelBoxString = "ST_3DMakeBox(ST_MakePoint(@cam_box_min_x, @cam_box_min_y, @cam_box_min_z), " + "ST_MakePoint(@cam_box_max_x, @cam_box_max_y, @cam_box_max_z))"; cmd.Parameters.AddWithValue("@cam_box_min_x", data.CarModelBox.Minimum.X); cmd.Parameters.AddWithValue("@cam_box_min_y", data.CarModelBox.Minimum.Y); cmd.Parameters.AddWithValue("@cam_box_min_z", data.CarModelBox.Minimum.Z); cmd.Parameters.AddWithValue("@cam_box_max_x", data.CarModelBox.Maximum.X); cmd.Parameters.AddWithValue("@cam_box_max_y", data.CarModelBox.Maximum.Y); cmd.Parameters.AddWithValue("@cam_box_max_z", data.CarModelBox.Maximum.Z); } var currentTarget = "NULL"; if (data.CurrentTarget != null) { currentTarget = "ST_MakePoint(@target_x, @target_y)"; cmd.Parameters.AddWithValue("@target_x", data.CurrentTarget.X); cmd.Parameters.AddWithValue("@target_y", data.CurrentTarget.Y); } cmd.Connection = conn; cmd.Transaction = trans; cmd.CommandText = $"INSERT INTO snapshots (run_id, version, imagepath, timestamp, timeofday, currentweather, camera_pos, " + $"camera_rot, camera_direction, camera_fov, view_matrix, proj_matrix, width, height, ui_width, ui_height, " + $"player_pos, cam_near_clip, cam_far_clip, velocity, scene_id, camera_relative_rotation, " + $"camera_relative_position, car_model_box, world_matrix, current_target) VALUES ( (SELECT run_id FROM runs WHERE " + $"runguid=@guid), @Version, @Imagepath, @Timestamp, @Timeofday, @currentweather, ST_MakePoint(@x, @y, @z), " + $"ST_MakePoint(@rotx, @roty, @rotz), ST_MakePoint(@dirx, @diry, @dirz), @fov, @view_matrix, @proj_matrix, " + $"@width, @height, @ui_width, @ui_height, ST_MakePoint(@player_x, @player_y, @player_z), @cam_near_clip, " + $"@cam_far_clip, ST_MakePoint(@vel_x, @vel_y, @vel_z), @scene_id, {camRelativeRotString}, " + $"{camRelativePosString}, {carModelBoxString}, @world_matrix, {currentTarget}) RETURNING snapshot_id;"; cmd.Parameters.Add(new NpgsqlParameter("@version", data.Version)); cmd.Parameters.Add(new NpgsqlParameter("@imagepath", data.ImageName)); cmd.Parameters.Add(new NpgsqlParameter("@timestamp", data.Timestamp)); cmd.Parameters.Add(new NpgsqlParameter("@timeofday", data.LocalTime)); cmd.Parameters.Add(new NpgsqlParameter("@currentweather", data.CurrentWeather)); cmd.Parameters.Add(new NpgsqlParameter("@x", data.CamPos.X)); cmd.Parameters.Add(new NpgsqlParameter("@y", data.CamPos.Y)); cmd.Parameters.Add(new NpgsqlParameter("@z", data.CamPos.Z)); cmd.Parameters.Add(new NpgsqlParameter("@rotx", data.CamRot.X)); cmd.Parameters.Add(new NpgsqlParameter("@roty", data.CamRot.Y)); cmd.Parameters.Add(new NpgsqlParameter("@rotz", data.CamRot.Z)); cmd.Parameters.AddWithValue("@dirx", data.CamDirection.X); cmd.Parameters.AddWithValue("@diry", data.CamDirection.Y); cmd.Parameters.AddWithValue("@dirz", data.CamDirection.Z); cmd.Parameters.AddWithValue("@fov", data.CamFOV); cmd.Parameters.AddWithValue("@view_matrix", data.ViewMatrix.ToArray()); cmd.Parameters.AddWithValue("@proj_matrix", data.ProjectionMatrix.ToArray()); cmd.Parameters.AddWithValue("@world_matrix", data.WorldMatrix.ToArray()); cmd.Parameters.AddWithValue("@width", data.ImageWidth); cmd.Parameters.AddWithValue("@height", data.ImageHeight); // @ui_width, @ui_height, @player_pos, @cam_near_clip, @cam_far_clip cmd.Parameters.AddWithValue("@ui_width", data.UIWidth); cmd.Parameters.AddWithValue("@ui_height", data.UIHeight); cmd.Parameters.AddWithValue("@player_x", data.playerPos.X); cmd.Parameters.AddWithValue("@player_y", data.playerPos.Y); cmd.Parameters.AddWithValue("@player_z", data.playerPos.Z); cmd.Parameters.AddWithValue("@vel_x", data.velocity.X); cmd.Parameters.AddWithValue("@vel_y", data.velocity.Y); cmd.Parameters.AddWithValue("@vel_z", data.velocity.Z); cmd.Parameters.AddWithValue("@cam_near_clip", data.CamNearClip); cmd.Parameters.AddWithValue("@cam_far_clip", data.CamFarClip); cmd.Parameters.AddWithValue("@scene_id", data.sceneGuid); cmd.Parameters.Add(new NpgsqlParameter("@guid", runId)); int snapshotid = (int)cmd.ExecuteScalar(); cmd.Parameters.Clear(); cmd.CommandText = "INSERT INTO snapshot_weathers (snapshot_id, weather_type, snapshot_page) VALUES (@snapshot, @weather, @page);"; cmd.Parameters.AddWithValue("@snapshot", NpgsqlDbType.Integer, snapshotid); cmd.Parameters.AddWithValue("@weather", NpgsqlDbType.Enum, Weather.Unknown); cmd.Parameters.Add("@page", NpgsqlDbType.Integer); cmd.Prepare(); for (int i = 0; i < data.CapturedWeathers.Count; ++i) { cmd.Parameters["@weather"].Value = data.CapturedWeathers[i]; cmd.Parameters["@page"].Value = i; cmd.ExecuteNonQuery(); } cmd.Parameters.Clear(); cmd.Parameters.Add("@snapshot", NpgsqlDbType.Integer); cmd.Parameters.AddWithValue("@type", NpgsqlDbType.Enum, DetectionType.background); cmd.Parameters.Add("@x", NpgsqlDbType.Real); cmd.Parameters.Add("@y", NpgsqlDbType.Real); cmd.Parameters.Add("@z", NpgsqlDbType.Real); cmd.Parameters.Add("@xrot", NpgsqlDbType.Real); cmd.Parameters.Add("@yrot", NpgsqlDbType.Real); cmd.Parameters.Add("@zrot", NpgsqlDbType.Real); cmd.Parameters.Add("@bbox", NpgsqlDbType.Box); cmd.Parameters.Add("@minx", NpgsqlDbType.Real); cmd.Parameters.Add("@miny", NpgsqlDbType.Real); cmd.Parameters.Add("@minz", NpgsqlDbType.Real); cmd.Parameters.Add("@maxx", NpgsqlDbType.Real); cmd.Parameters.Add("@maxy", NpgsqlDbType.Real); cmd.Parameters.Add("@maxz", NpgsqlDbType.Real); cmd.Parameters.Add("@vel_x", NpgsqlDbType.Real); cmd.Parameters.Add("@vel_y", NpgsqlDbType.Real); cmd.Parameters.Add("@vel_z", NpgsqlDbType.Real); cmd.Parameters.AddWithValue("@class", NpgsqlDbType.Enum, DetectionClass.Unknown); cmd.Parameters.Add("@handle", NpgsqlDbType.Integer); cmd.CommandText = "INSERT INTO detections (snapshot_id, type, pos, rot, bbox, class, handle, bbox3d, velocity) VALUES " + "(@snapshot, @type, ST_MakePoint(@x,@y,@z), ST_MakePoint(@xrot, @yrot, @zrot), @bbox, @class, @handle," + "ST_3DMakeBox(ST_MakePoint(@minx,@miny,@minz), ST_MakePoint(@maxx, @maxy, @maxz)), ST_MakePoint(@vel_x, @vel_y, @vel_z))"; cmd.Prepare(); foreach (var detection in data.Detections) { cmd.Parameters["@snapshot"].Value = snapshotid; cmd.Parameters["@type"].Value = detection.Type; cmd.Parameters["@x"].Value = detection.Pos.X; cmd.Parameters["@y"].Value = detection.Pos.Y; cmd.Parameters["@z"].Value = detection.Pos.Z; cmd.Parameters["@xrot"].Value = detection.Rot.X; cmd.Parameters["@yrot"].Value = detection.Rot.Y; cmd.Parameters["@zrot"].Value = detection.Rot.Z; cmd.Parameters["@bbox"].Value = new NpgsqlBox(detection.BBox.Max.Y, detection.BBox.Max.X, detection.BBox.Min.Y, detection.BBox.Min.X); cmd.Parameters["@class"].Value = detection.cls; cmd.Parameters["@handle"].Value = detection.Handle; cmd.Parameters["@minx"].Value = detection.BBox3D.Minimum.X; cmd.Parameters["@miny"].Value = detection.BBox3D.Minimum.Y; cmd.Parameters["@minz"].Value = detection.BBox3D.Minimum.Z; cmd.Parameters["@maxx"].Value = detection.BBox3D.Maximum.X; cmd.Parameters["@maxy"].Value = detection.BBox3D.Maximum.Y; cmd.Parameters["@maxz"].Value = detection.BBox3D.Maximum.Z; cmd.Parameters["@vel_x"].Value = detection.velocity.X; cmd.Parameters["@vel_y"].Value = detection.velocity.Y; cmd.Parameters["@vel_z"].Value = detection.velocity.Z; cmd.ExecuteNonQuery(); } } trans.Commit(); conn.Close(); }
public static GTAData DumpData(string imageName, List <Weather> capturedWeathers) { var ret = new GTAData(); ret.Version = 3; ret.ImageName = imageName; ret.CurrentWeather = World.Weather; ret.CapturedWeathers = capturedWeathers; ret.Timestamp = DateTime.UtcNow; ret.LocalTime = World.CurrentDayTime; ret.CamPos = new GTAVector(World.RenderingCamera.Position); ret.CamRot = new GTAVector(World.RenderingCamera.Rotation); //getting information about currently driving vehicle model size Vector3 gmin; Vector3 gmax; Game.Player.Character.CurrentVehicle.Model.GetDimensions(out gmin, out gmax); ret.CarModelBox = new BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax)); ret.CamDirection = new GTAVector(World.RenderingCamera.Direction); ret.CamFOV = World.RenderingCamera.FieldOfView; ret.ImageWidth = Game.ScreenResolution.Width; ret.ImageHeight = Game.ScreenResolution.Height; ret.UIWidth = UI.WIDTH; ret.UIHeight = UI.HEIGHT; ret.playerPos = new GTAVector(Game.Player.Character.Position); ret.velocity = new GTAVector(Game.Player.Character.Velocity); ret.CamNearClip = World.RenderingCamera.NearClip; ret.CamFarClip = World.RenderingCamera.FarClip; var peds = World.GetNearbyPeds(Game.Player.Character, 500.0f); var cars = World.GetNearbyVehicles(Game.Player.Character, 500.0f); //var props = World.GetNearbyProps(Game.Player.Character.Position, 300.0f); var constants = VisionNative.GetConstants(); if (!constants.HasValue) { return(null); } var W = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix .OfColumnMajor(4, 4, constants.Value.world.ToArray()).ToDouble(); var WV = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, constants.Value.worldView.ToArray()).ToDouble(); var WVP = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, constants.Value.worldViewProjection.ToArray()).ToDouble(); var V = WV * W.Inverse(); var P = WVP * WV.Inverse(); ret.ProjectionMatrix = P as DenseMatrix; ret.ViewMatrix = V as DenseMatrix; ret.WorldMatrix = W as DenseMatrix; var pedList = from ped in peds where ped.IsHuman && ped.IsOnFoot // where ped.IsHuman && ped.IsOnFoot && CheckVisible(ped) select new GTADetection(ped); var cycles = from ped in peds where ped.IsOnBike // where ped.IsOnBike && CheckVisible(ped) select new GTADetection(ped, DetectionType.bicycle); var vehicleList = from car in cars // where CheckVisible(car) select new GTADetection(car); ret.Detections = new List <GTADetection>(); ret.Detections.AddRange(pedList); ret.Detections.AddRange(vehicleList); //ret.Detections.AddRange(cycles); return(ret); }