forked from andybarry/simflight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forest.m
117 lines (102 loc) · 4.07 KB
/
Forest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
classdef Forest
%Holds many Trees, and can operate on them, mainly useful for only
%Trees
%Analogous to ObstacleField in the 2-D case
properties
obstacles; % Cell array of Tree Instances (currently, unless expanded)
number_of_obstacles;
plane;
end
methods
function obj = Forest()
obj.number_of_obstacles = 0;
%if nargin>0
% obj.plane = plane;
%end
end
function obj = AddPlane(obj, p)
%adds a Plane to the forest
obj.plane = p;
end
function obj = TheTwoTowers(obj, width, x)
% Generates two trees directly ahead, at x = 5, y = +-1
%
% @param width The distance each tree is from the y = 0. A width of 1 puts the center of the trees 2 units apart.
% @param x The x-distance ahead to the center of the obstacles.
%
% @retval obj the new forest
if nargin<2
width = 1;
end
if nargin<3
x = 5;
end
obj = obj.AddTree(Tree(x,-width,.1));
obj = obj.AddTree(Tree(x,width,.1));
end
function draw(obj)
%Draws the Forest. Implemented by calling draw() on each Tree
%{
persistent hFig;
if (isempty(hFig))
hFig = sfigure(25);
set(hFig,'DoubleBuffer', 'on');
set(gca,'YDir', 'reverse');
set(gca,'ZDir', 'reverse');
end
hold on
for i = 1:obj.number_of_obstacles
currTree = obj.obstacles{i};
currTree.draw();
end
%}
lcmgl = drake.util.BotLCMGLClient(lcm.lcm.LCM.getSingleton(), 'obstacles');
options = struct();
options.lcmgl = lcmgl;
options.switch_buffers = false;
for i = 1:obj.number_of_obstacles
currTree = obj.obstacles{i};
currTree.draw(options);
end
lcmgl.switchBuffers();
end
function con = AddConstraints(obj,con,plane)
% Adds the con.x.c constraint for the Forest to the given
% constraint object.
%
% @param con input constraint object. Must be a structure.
% Other than that, we'll just overwrite con.x.c, so anything
% else will be returned unchanged.
% @param plane An object of Plane
%
% @retval con Constraint structure with modified con.x.c value.
if nargin>2
obj.plane = plane;
end
con.x.c = @(x)obj.obstacleConstraint(x);
end
function obj = AddTree(obj, tree, index, alignment)
%Adds a tree to the obstacle field
%also allows replacing a tree with a new one if an index is
%given
if nargin<3
index = obj.number_of_obstacles + 1;
obj.number_of_obstacles = obj.number_of_obstacles + 1;
elseif index<0
index = obj.number_of_obstacles + 1;
obj.number_of_obstacles = obj.number_of_obstacles + 1;
elseif index>(obj.number_of_obstacles + 1)
index = obj.number_of_obstacles + 1;
obj.number_of_obstacles = obj.number_of_obstacles + 1;
end
obj.obstacles{index} = tree;
end
function [c] = obstacleConstraint(obj,x)
c=repmat(x(1),obj.number_of_obstacles,1); % preallocate f (to overcome temporary limitation of TaylorVar)
dc=zeros(obj.number_of_obstacles,length(x));
for i=1:obj.number_of_obstacles
c(i) = obj.obstacles{i}.spaceconstraint(x, obj.plane);
end
end
end
end