added video writing feature

This commit is contained in:
2025-10-27 23:13:33 -07:00
parent 7fb9d13781
commit 0621e0a07a
8 changed files with 59 additions and 17 deletions

3
.gitignore vendored
View File

@@ -38,3 +38,6 @@ codegen/
# SimBiology backup files # SimBiology backup files
*.sbproj.backup *.sbproj.backup
*.sbproj.bak *.sbproj.bak
# Sandbox contents
sandbox/*

61
miSim.m
View File

@@ -12,6 +12,12 @@ classdef miSim
adjacency = NaN; % Adjacency matrix representing communications network graph adjacency = NaN; % Adjacency matrix representing communications network graph
end end
properties (Access = private)
v = VideoWriter(fullfile('sandbox', strcat(string(datetime('now'), 'yyyy_MM_dd_HH_mm_ss'), '_miSimHist.mp4')));
connectionsPlot; % objects for lines connecting agents in spatial plots
graphPlot; % objects for abstract network graph plot
end
methods (Access = public) methods (Access = public)
function [obj, f] = initialize(obj, domain, objective, agents, timestep, maxIter, obstacles) function [obj, f] = initialize(obj, domain, objective, agents, timestep, maxIter, obstacles)
arguments (Input) arguments (Input)
@@ -71,10 +77,10 @@ classdef miSim
end end
% Plot communication links % Plot communication links
f = obj.plotNetwork(f); [obj, f] = obj.plotConnections(f);
% Plot abstract network graph % Plot abstract network graph
f = obj.plotGraph(f); [obj, f] = obj.plotGraph(f);
end end
function [obj, f] = run(obj, f) function [obj, f] = run(obj, f)
arguments (Input) arguments (Input)
@@ -92,6 +98,11 @@ classdef miSim
% Set up times to iterate over % Set up times to iterate over
times = linspace(0, obj.timestep * obj.maxIter, obj.maxIter+1)'; times = linspace(0, obj.timestep * obj.maxIter, obj.maxIter+1)';
% Start video writer
obj.v.FrameRate = 1/obj.timestep;
obj.v.Quality = 90;
obj.v.open();
for ii = 1:size(times, 1) for ii = 1:size(times, 1)
% Display current sim time % Display current sim time
t = times(ii); t = times(ii);
@@ -106,28 +117,44 @@ classdef miSim
obj = obj.updateAdjacency; obj = obj.updateAdjacency;
% Update plots % Update plots
f = obj.updatePlots(f); [obj, f] = obj.updatePlots(f);
% Write frame in to video
I = getframe(f);
obj.v.writeVideo(I);
end end
% Close video file
obj.v.close();
end end
function f = updatePlots(obj, f) function [obj, f] = updatePlots(obj, f)
arguments (Input) arguments (Input)
obj (1, 1) {mustBeA(obj, 'miSim')}; obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure;
end end
arguments (Output) arguments (Output)
obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')}; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')};
end end
% Update agent positions, collision geometries, connections % Update agent positions, collision geometries
for ii = 1:size(obj.agents, 1) for ii = 1:size(obj.agents, 1)
obj.agents{ii}.updatePlots(); obj.agents{ii}.updatePlots();
end end
% The remaining updates might be possible to do in a clever way
% that moves existing lines instead of clearing and
% re-plotting, which is much better for performance boost
% Update agent connections plot
delete(obj.connectionsPlot);
[obj, f] = obj.plotConnections(f);
% Update network graph plot % Update network graph plot
delete(obj.graphPlot);
[obj, f] = obj.plotGraph(f);
drawnow; drawnow;
end end
function obj = updateAdjacency(obj) function obj = updateAdjacency(obj)
arguments (Input) arguments (Input)
@@ -157,12 +184,13 @@ classdef miSim
obj.adjacency = A | A'; obj.adjacency = A | A';
end end
function f = plotNetwork(obj, f) function [obj, f] = plotConnections(obj, f)
arguments (Input) arguments (Input)
obj (1, 1) {mustBeA(obj, 'miSim')}; obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure;
end end
arguments (Output) arguments (Output)
obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')}; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')};
end end
@@ -188,29 +216,28 @@ classdef miSim
% Check if this is a tiled layout figure % Check if this is a tiled layout figure
if strcmp(f.Children(1).Type, 'tiledlayout') if strcmp(f.Children(1).Type, 'tiledlayout')
% Add to other plots % Add to other plots
copyobj(o, f.Children(1).Children(2)); o = [o, copyobj(o(:, 1), f.Children(1).Children(2))];
copyobj(o, f.Children(1).Children(3)); o = [o, copyobj(o(:, 1), f.Children(1).Children(3))];
copyobj(o, f.Children(1).Children(5)); o = [o, copyobj(o(:, 1), f.Children(1).Children(5))];
end end
obj.connectionsPlot = o;
end end
function f = plotGraph(obj, f) function [obj, f] = plotGraph(obj, f)
arguments (Input) arguments (Input)
obj (1, 1) {mustBeA(obj, 'miSim')}; obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')} = figure;
end end
arguments (Output) arguments (Output)
obj (1, 1) {mustBeA(obj, 'miSim')};
f (1, 1) {mustBeA(f, 'matlab.ui.Figure')}; f (1, 1) {mustBeA(f, 'matlab.ui.Figure')};
end end
% Form graph from adjacency matrix % Form graph from adjacency matrix
G = graph(obj.adjacency, 'omitselfloops'); G = graph(obj.adjacency, 'omitselfloops');
% Check if this is a tiled layout figure % Plot graph object
if strcmp(f.Children(1).Type, 'tiledlayout') obj.graphPlot = plot(f.Children(1).Children(4), G, 'LineStyle', '--', 'EdgeColor', 'g', 'NodeColor', 'k', 'LineWidth', 2);
o = plot(f.Children(1).Children(4), G, 'LineStyle', '--', 'EdgeColor', 'g', 'NodeColor', 'k', 'LineWidth', 2);
else
o = plot(f.CurrentAxes, G, 'LineStyle', '--', 'EdgeColor', 'g', 'NodeColor', 'k', 'LineWidth', 2);
end
end end
end end

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info Ref="sandbox" Type="Relative"/>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info location="ff13e617-a2ad-49b1-a9b5-668ac2cffc4a" type="Reference"/>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info/>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info location="1" type="DIR_SIGNIFIER"/>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info/>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info location="sandbox" type="File"/>