-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_manager.c
221 lines (182 loc) · 5.72 KB
/
object_manager.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* This file is part of vis-o-mex.
*
* Copyright (C) 2010-2011 Greg Horn <ghorn@stanford.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* object_manager.c
* Keeps track of all objects (aircraft only right now) and their properties,
* draws them all on command.
*/
#include <inttypes.h>
#include <vis_telemetry.h>
#include <vis_types.h>
#include "simple_aircraft_vis.h"
#define MAX_NUM_OBJECTS 128
typedef struct {
int allocated;
int id;
char name[80];
object_type_t type;
void * obj_ptr;
} vis_object_item_t;
vis_object_item_t list_of_objects[MAX_NUM_OBJECTS];
// returns the object's index if already in list, otherwise return -1
static int
index_from_object_id(int id)
{
for (int k=0; k<MAX_NUM_OBJECTS; k++)
if ((list_of_objects[k].allocated == 1) && (list_of_objects[k].id == id))
return k;
return -1;
}
// returns the first free index, returns -1 if no free indices
static int
get_first_free_index()
{
for (int k=0; k<MAX_NUM_OBJECTS; k++)
if (list_of_objects[k].allocated == 0)
return k;
return -1;
}
static void *
allocate_vis_object(const object_type_t type, const void * obj_params)
{
switch (type){
case simple_aircraft:
{
const simple_aircraft_params_t * params = (simple_aircraft_params_t *)obj_params;
return (void*) alloc_simple_aircraft(params);
break;
}
default:
{
printf("allocate_vis_object() received unrecognized type of object (%d)\n", type);
exit(1);
}
}
}
static void
free_vis_object(const object_type_t type, void * obj_ptr)
{
switch (type){
case simple_aircraft:
{
free_simple_aircraft( (simple_aircraft_t*)obj_ptr);
break;
}
default:
{
printf("free_vis_object() received unrecognized type of object (%d)\n", type);
exit(1);
}
}
}
static void
object_manager_init_handler(const lcm_recv_buf_t *rbuf __attribute__((unused)),
const char *channel __attribute__((unused)),
const vis_object_manager_init_t *msg __attribute__((unused)),
void *user __attribute__((unused)))
{
printf("received object_manager_init, "
"name: %s, id: %d, type: %d\n", msg->name, msg->id, msg->type);
int obj_index = index_from_object_id(msg->id);
// if not yet in list, then add to list
if (obj_index == -1){
obj_index = get_first_free_index();
if (obj_index == -1){
printf("object manager list of objects full :(\n");
exit(1);
}
printf("object allocating\n");
vis_object_item_t * vis_obj = &(list_of_objects[obj_index]);
memcpy( vis_obj->name, msg->name, 80*sizeof(char) );
vis_obj->id = msg->id;
vis_obj->type = (object_type_t)(msg->type);
vis_obj->obj_ptr = allocate_vis_object(vis_obj->type, msg->params);
vis_obj->allocated = 1;
} else { // if already in list, re-initialize
printf("object re-allocating\n");
vis_object_item_t * vis_obj = &(list_of_objects[obj_index]);
// first free
free_vis_object(vis_obj->type, vis_obj->obj_ptr );
// then allocate again
memcpy( vis_obj->name, msg->name, 80*sizeof(char) );
vis_obj->id = msg->id;
vis_obj->type = (object_type_t)(msg->type);
vis_obj->obj_ptr = allocate_vis_object(vis_obj->type, msg->params);
}
}
static void
pose_handler(const lcm_recv_buf_t *rbuf __attribute__((unused)),
const char *channel __attribute__((unused)),
const vis_pose_t *msg,
void *user __attribute__((unused)))
{
int obj_index = index_from_object_id(msg->id);
if (obj_index == -1)
printf("pose message recieved but object_manager_init not yet received for that ID :(\n");
else {
vis_object_item_t * vis_obj = &(list_of_objects[obj_index]);
switch (vis_obj->type){
case simple_aircraft:
{
update_aircraft_pose( (xyz_t*)&(msg->r_n2b_n),
(quat_t*)&(msg->q_n2b),
(simple_aircraft_t*)(vis_obj->obj_ptr));
break;
}
default:
{
printf("no pose update function defined for this type (%d)\n", vis_obj->type);
exit(1);
}
}
}
}
void
init_object_manager()
{
// initialize list of vis objects to unallocated
for (int k=0; k<MAX_NUM_OBJECTS; k++)
list_of_objects[k].allocated = 0;
// subscribe to the object_manager_object init channel
vis_lcm_subscribe_chan(object_manager_init_t, &object_manager_init_handler, NULL, "object_manager_init");
// subscribe to the pose channel
vis_lcm_subscribe_chan(pose_t, &pose_handler, NULL, "pose");
}
void
draw_object_manager_objects()
{
for (int k=0; k<MAX_NUM_OBJECTS; k++){
if (list_of_objects[k].allocated){
switch (list_of_objects[k].type){
case simple_aircraft:
{
const simple_aircraft_t * ac = (simple_aircraft_t*)(list_of_objects[k].obj_ptr);
draw_simple_aircraft( ac, 0 );
draw_simple_aircraft_trails( ac );
draw_simple_aircraft( ac, 1 );
break;
}
default:
printf("no draw function set up for this object type type (%d)\n", list_of_objects[k].type);
exit(1);
}
}
}
}