-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathMissionProxy.java
More file actions
625 lines (520 loc) · 18.8 KB
/
Copy pathMissionProxy.java
File metadata and controls
625 lines (520 loc) · 18.8 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package org.droidplanner.android.proxy.mission;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.droidplanner.android.maps.DPMap;
import org.droidplanner.android.maps.MarkerInfo;
import org.droidplanner.android.proxy.mission.item.MissionItemProxy;
import org.droidplanner.android.utils.analytics.GAUtils;
import org.droidplanner.core.helpers.coordinates.Coord2D;
import org.droidplanner.core.helpers.coordinates.Coord3D;
import org.droidplanner.core.helpers.geoTools.GeoTools;
import org.droidplanner.core.helpers.geoTools.spline.SplinePath;
import org.droidplanner.core.helpers.units.Altitude;
import org.droidplanner.core.helpers.units.Length;
import org.droidplanner.core.mission.Mission;
import org.droidplanner.core.mission.MissionItem;
import org.droidplanner.core.mission.commands.ReturnToHome;
import org.droidplanner.core.mission.commands.Takeoff;
import org.droidplanner.core.mission.survey.Survey;
import org.droidplanner.core.mission.survey.Survey2D;
import org.droidplanner.core.mission.survey.Survey3D;
import org.droidplanner.core.mission.waypoints.SpatialCoordItem;
import org.droidplanner.core.mission.waypoints.SplineWaypoint;
import org.droidplanner.core.mission.waypoints.Waypoint;
import org.droidplanner.core.util.Pair;
import com.google.android.gms.analytics.HitBuilders;
/**
* This class is used to render a {@link org.droidplanner.core.mission.Mission}
* object on the Android side.
*/
public class MissionProxy implements DPMap.PathSource {
private final Mission mMission;
/**
* Stores all the mission item renders for this mission render.
*/
private final List<MissionItemProxy> mMissionItems = new ArrayList<MissionItemProxy>();
public MissionSelection selection = new MissionSelection();
public MissionProxy(Mission mission) {
mMission = mission;
refresh();
}
/**
* Provides access to the class' mission instance.
*
* @return {@link org.droidplanner.core.mission.Mission} object
*/
public Mission getMission() {
return mMission;
}
public List<MissionItemProxy> getItems() {
return mMissionItems;
}
/**
* @return the map markers corresponding to this mission's command set.
*/
public List<MarkerInfo> getMarkersInfos() {
List<MarkerInfo> markerInfos = new ArrayList<MarkerInfo>();
for (MissionItemProxy itemProxy : mMissionItems) {
List<MarkerInfo> itemMarkerInfos = itemProxy.getMarkerInfos();
if (itemMarkerInfos != null && !itemMarkerInfos.isEmpty()) {
markerInfos.addAll(itemMarkerInfos);
}
}
return markerInfos;
}
/**
* Update the state for this object based on the state of the Mission
* object.
*/
public void refresh() {
selection.mSelectedItems.clear();
mMissionItems.clear();
for (MissionItem item : mMission.getItems()) {
mMissionItems.add(new MissionItemProxy(this, item));
}
selection.notifySelectionUpdate();
}
/**
* Checks if this mission render contains the passed argument.
*
* @param item
* mission item render object
* @return true if this mission render contains the passed argument
*/
public boolean contains(MissionItemProxy item) {
return mMissionItems.contains(item);
}
/**
* Removes a waypoint mission item from the set of mission items commands.
*
* @param item
* item to remove
*/
public void removeItem(MissionItemProxy item) {
mMissionItems.remove(item);
selection.mSelectedItems.remove(item);
mMission.removeWaypoint(item.getMissionItem());
selection.notifySelectionUpdate();
}
/**
* Removes a set of mission items from the mission' set.
*
* @param items
* list of items to remove
*/
public void removeItemList(List<MissionItemProxy> items) {
final List<MissionItem> toRemove = new ArrayList<MissionItem>(items.size());
for (MissionItemProxy item : items) {
toRemove.add(item.getMissionItem());
}
mMissionItems.removeAll(items);
selection.mSelectedItems.removeAll(items);
mMission.removeWaypoints(toRemove);
selection.notifySelectionUpdate();
}
/**
* Adds a survey mission item to the set.
*
* @param points
* 2D points making up the survey
*/
public void addSurveyPolygon(List<Coord2D> points) {
Survey3D survey = new Survey3D(mMission, points);
mMissionItems.add(new MissionItemProxy(this, survey));
mMission.addMissionItem(survey);
try {
//survey.build();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Add a set of waypoints generated around the passed 2D points. TODO:
* replace Coord2D with library's classes such as android.graphics.Point
*
* @param points
* list of points used to generate the mission waypoints
*/
public void addWaypoints(List<Coord2D> points) {
final Altitude alt = mMission.getLastAltitude();
final List<MissionItem> missionItemsToAdd = new ArrayList<MissionItem>(points.size());
for (Coord2D point : points) {
Waypoint waypoint = new Waypoint(mMission, new Coord3D(point, alt));
missionItemsToAdd.add(waypoint);
}
addMissionItems(missionItemsToAdd);
}
/**
* Add a set of spline waypoints generated around the passed 2D points.
*
* @param points
* list of points used as location for the spline waypoints
*/
public void addSplineWaypoints(List<Coord2D> points) {
final Altitude alt = mMission.getLastAltitude();
final List<MissionItem> missionItemsToAdd = new ArrayList<MissionItem>(points.size());
for (Coord2D point : points) {
SplineWaypoint splineWaypoint = new SplineWaypoint(mMission, new Coord3D(point, alt));
missionItemsToAdd.add(splineWaypoint);
}
addMissionItems(missionItemsToAdd);
}
private void addMissionItems(List<MissionItem> missionItems) {
for (MissionItem missionItem : missionItems) {
mMissionItems.add(new MissionItemProxy(this, missionItem));
}
mMission.addMissionItems(missionItems);
}
/**
* Add a waypoint generated around the passed 2D point. TODO: replace
* Coord2D with library's classes such as android.graphics.Point
*
* @param point
* point used to generate the mission waypoint
*/
public void addWaypoint(Coord2D point) {
final Altitude alt = mMission.getLastAltitude();
final Waypoint waypoint = new Waypoint(mMission, new Coord3D(point, alt));
addMissionItem(waypoint);
}
/**
* Add a spline waypoint generated around the passed 2D point.
*
* @param point
* point used as location for the spline waypoint.
*/
public void addSplineWaypoint(Coord2D point) {
final Altitude alt = mMission.getLastAltitude();
final SplineWaypoint splineWaypoint = new SplineWaypoint(mMission, new Coord3D(point, alt));
addMissionItem(splineWaypoint);
}
private void addMissionItem(MissionItem missionItem) {
mMissionItems.add(new MissionItemProxy(this, missionItem));
mMission.addMissionItem(missionItem);
}
public void addTakeoff() {
Takeoff takeoff = new Takeoff(mMission, new Altitude(10));
mMissionItems.add(new MissionItemProxy(this, takeoff));
mMission.addMissionItem(takeoff);
}
public void addTakeOffAndRTL(){
if(!mMission.isFirstItemTakeoff()){
Altitude defaultAlt = new Altitude(Takeoff.DEFAULT_TAKEOFF_ALTITUDE);
if(!mMissionItems.isEmpty()){
MissionItem firstItem = mMissionItems.get(0).getMissionItem();
if(firstItem instanceof SpatialCoordItem)
defaultAlt = new Altitude(((SpatialCoordItem)firstItem).getCoordinate()
.getAltitude().valueInMeters());
}
final Takeoff takeOff = new Takeoff(mMission, defaultAlt);
mMissionItems.add(0, new MissionItemProxy(this, takeOff));
mMission.addMissionItem(0, takeOff);
}
if(!mMission.isLastItemLandOrRTL()){
final ReturnToHome rtl = new ReturnToHome(mMission);
mMissionItems.add(new MissionItemProxy(this, rtl));
mMission.addMissionItem(rtl);
}
}
/**
* Returns the order for the given argument in the mission set.
*
* @param item
* @return order of the given argument
*/
public int getOrder(MissionItemProxy item) {
return mMission.getOrder(item.getMissionItem());
}
/**
* Updates a mission item render
*
* @param oldItem
* mission item render to update
* @param newItem
* new mission item render
*/
public void replace(MissionItemProxy oldItem, MissionItemProxy newItem) {
final int index = mMissionItems.indexOf(oldItem);
if (index == -1)
return;
mMissionItems.remove(index);
mMissionItems.add(index, newItem);
// Update the mission object
mMission.replace(oldItem.getMissionItem(), newItem.getMissionItem());
if (selection.selectionContains(oldItem)) {
selection.removeItemFromSelection(oldItem);
selection.addToSelection(newItem);
}
}
public void replaceAll(List<Pair<MissionItemProxy, MissionItemProxy>> oldNewList){
if(oldNewList == null){
return;
}
final int pairSize = oldNewList.size();
if(pairSize == 0){
return;
}
final List<Pair<MissionItem, MissionItem>> missionItemsToUpdate = new
ArrayList<Pair<MissionItem, MissionItem>>(pairSize);
final List<MissionItemProxy> selectionsToRemove = new ArrayList<MissionItemProxy>(pairSize);
final List<MissionItemProxy> itemsToSelect = new ArrayList<MissionItemProxy>(pairSize);
for(int i = 0; i < pairSize; i++){
final MissionItemProxy oldItem = oldNewList.get(i).first;
final int index = mMissionItems.indexOf(oldItem);
if(index == -1){
continue;
}
final MissionItemProxy newItem = oldNewList.get(i).second;
mMissionItems.remove(index);
mMissionItems.add(index, newItem);
missionItemsToUpdate.add(Pair.create(oldItem.getMissionItem(), newItem.getMissionItem()));
if(selection.selectionContains(oldItem)){
selectionsToRemove.add(oldItem);
itemsToSelect.add(newItem);
}
}
//Update the mission objects
mMission.replaceAll(missionItemsToUpdate);
//Update the selection list.
selection.removeItemsFromSelection(selectionsToRemove);
selection.addToSelection(itemsToSelect);
}
/**
* Reverse the order of the mission items renders.
*/
public void reverse() {
Collections.reverse(mMissionItems);
mMission.reverse();
}
public void clear() {
selection.clearSelection();
removeItemList(mMissionItems);
}
/**
* Moves the selected objects up or down into the mission listing
*
* Think of it as pushing the selected objects, while you can only move a
* single unselected object per turn.
*
* @param moveUp
* true to move up, but can be false to move down
*/
public void moveSelection(boolean moveUp) {
if (selection.mSelectedItems.size() > 0
|| selection.mSelectedItems.size() < mMissionItems.size()) {
Collections.sort(selection.mSelectedItems);
if (moveUp) {
Collections.rotate(getSubListToRotateUp(), 1);
} else {
Collections.rotate(getSubListToRotateDown(), -1);
}
selection.notifySelectionUpdate();
mMission.notifyMissionUpdate();
}
}
private List<MissionItemProxy> getSubListToRotateUp() {
final int from = mMissionItems.indexOf(selection.mSelectedItems.get(0));
int to = from;
do {
if (mMissionItems.size() < to + 2)
return mMissionItems.subList(0, 0);
} while (selection.mSelectedItems.contains(mMissionItems.get(++to)));
return mMissionItems.subList(from, to + 1); // includes one unselected
// item
}
private List<MissionItemProxy> getSubListToRotateDown() {
final int from = mMissionItems.indexOf(selection.mSelectedItems
.get(selection.mSelectedItems.size() - 1));
int to = from;
do {
if (to < 1)
return mMissionItems.subList(0, 0);
} while (selection.mSelectedItems.contains(mMissionItems.get(--to)));
return mMissionItems.subList(to, from + 1); // includes one unselected
// item.
}
public Length getAltitudeDiffFromPreviousItem(MissionItemProxy waypointRender)
throws IllegalArgumentException {
MissionItem waypoint = waypointRender.getMissionItem();
if (!(waypoint instanceof SpatialCoordItem))
throw new IllegalArgumentException("Invalid mission item type.");
return mMission.getAltitudeDiffFromPreviousItem((SpatialCoordItem) waypoint);
}
public Length getDistanceFromLastWaypoint(MissionItemProxy waypointRender)
throws IllegalArgumentException {
MissionItem waypoint = waypointRender.getMissionItem();
if (!(waypoint instanceof SpatialCoordItem))
throw new IllegalArgumentException("Invalid mission item type.");
return mMission.getDistanceFromLastWaypoint((SpatialCoordItem) waypoint);
}
@Override
public List<Coord2D> getPathPoints() {
if (mMissionItems.isEmpty()) {
return Collections.emptyList();
}
// Partition the mission items into spline/non-spline buckets.
final List<Pair<Boolean, List<MissionItemProxy>>> bucketsList = new ArrayList<Pair<Boolean, List<MissionItemProxy>>>();
boolean isSpline = false;
List<MissionItemProxy> currentBucket = new ArrayList<MissionItemProxy>();
for (MissionItemProxy missionItemProxy : mMissionItems) {
if (missionItemProxy.getMissionItem() instanceof SplineWaypoint) {
if (!isSpline) {
if (!currentBucket.isEmpty()) {
// Get the last item from the current bucket. It will
// become the first
// anchor point for the spline path.
final MissionItemProxy lastItem = currentBucket
.get(currentBucket.size() - 1);
// Store the previous item bucket.
bucketsList.add(new Pair<Boolean, List<MissionItemProxy>>(Boolean.FALSE,
currentBucket));
// Create a new bucket for this category and update
// 'isSpline'
currentBucket = new ArrayList<MissionItemProxy>();
currentBucket.add(lastItem);
}
isSpline = true;
}
// Add the current element into the bucket
currentBucket.add(missionItemProxy);
} else {
if (isSpline) {
// Add the current item to the spline bucket. It will act as
// the end anchor
// point for the spline path.
if (!currentBucket.isEmpty()) {
currentBucket.add(missionItemProxy);
// Store the previous item bucket.
bucketsList.add(new Pair<Boolean, List<MissionItemProxy>>(Boolean.TRUE,
currentBucket));
currentBucket = new ArrayList<MissionItemProxy>();
}
isSpline = false;
}
// Add the current element into the bucket
currentBucket.add(missionItemProxy);
}
}
bucketsList.add(new Pair<Boolean, List<MissionItemProxy>>(isSpline, currentBucket));
final List<Coord2D> pathPoints = new ArrayList<Coord2D>();
Coord2D lastPoint = null;
for (Pair<Boolean, List<MissionItemProxy>> bucketEntry : bucketsList) {
final List<MissionItemProxy> bucket = bucketEntry.second;
if (bucketEntry.first) {
final List<Coord2D> splinePoints = new ArrayList<Coord2D>();
for (MissionItemProxy missionItemProxy : bucket) {
splinePoints.addAll(missionItemProxy.getPath(lastPoint));
if (!splinePoints.isEmpty()) {
lastPoint = splinePoints.get(splinePoints.size() - 1);
}
}
pathPoints.addAll(SplinePath.process(splinePoints));
} else {
for (MissionItemProxy missionItemProxy : bucket) {
pathPoints.addAll(missionItemProxy.getPath(lastPoint));
if (!pathPoints.isEmpty()) {
lastPoint = pathPoints.get(pathPoints.size() - 1);
}
}
}
}
return pathPoints;
}
public void removeSelection(MissionSelection missionSelection) {
removeItemList(missionSelection.mSelectedItems);
}
public void move(MissionItemProxy item, Coord2D position) {
((SpatialCoordItem) item.getMissionItem()).setPosition(position);
mMission.notifyMissionUpdate();
}
public void movePolygonPoint(Survey survey, int index, Coord2D position) {
survey.polygon.movePoint(position, index);
try {
survey.build();
} catch (Exception e) {
e.printStackTrace();
}
mMission.notifyMissionUpdate();
}
public List<Coord2D> getVisibleCoords() {
final List<Coord2D> coords = new ArrayList<Coord2D>();
for (MissionItem item : mMission.getItems()) {
if (!(item instanceof SpatialCoordItem))
continue;
final Coord2D coordinate = ((SpatialCoordItem) item).getCoordinate();
if (coordinate.isEmpty())
continue;
coords.add(coordinate);
}
return coords;
}
public static List<Coord2D> getVisibleCoords(List<MissionItemProxy> mipList){
final List<Coord2D> coords = new ArrayList<Coord2D>();
if(mipList == null || mipList.isEmpty()){
return coords;
}
for(MissionItemProxy mip: mipList){
if(!(mip.getMissionItem() instanceof SpatialCoordItem))
continue;
final Coord2D coordinate = ((SpatialCoordItem) mip.getMissionItem()).getCoordinate();
if(coordinate.isEmpty())
continue;
coords.add(coordinate);
}
return coords;
}
public void sendMissionToAPM(){
mMission.sendMissionToAPM();
//Send an event for the created mission
HitBuilders.EventBuilder eventBuilder = new HitBuilders.EventBuilder()
.setCategory(GAUtils.Category.MISSION_PLANNING)
.setAction("Mission sent to drone")
.setLabel("Mission items count")
.setValue(mMissionItems.size());
GAUtils.sendEvent(eventBuilder);
String missionItemsList = "[";
if(!mMissionItems.isEmpty()){
boolean isFirst = true;
for(MissionItemProxy itemProxy: mMissionItems){
if(isFirst)
isFirst = false;
else
missionItemsList += ", ";
missionItemsList += itemProxy.getMissionItem().getType().getName();
}
}
missionItemsList += "]";
eventBuilder = new HitBuilders.EventBuilder()
.setCategory(GAUtils.Category.MISSION_PLANNING)
.setAction("Mission sent to drone")
.setLabel("Mission items: " + missionItemsList);
GAUtils.sendEvent(eventBuilder);
}
public Length getMissionLength() {
List<Coord2D> points = getPathPoints();
if (points.size()>1) {
double length = 0;
for (int i = 1; i < points.size(); i++) {
length += GeoTools.getDistance(points.get(i-1), points.get(i)).valueInMeters();
}
return new Length(length);
}else{
return new Length(0);
}
}
public float makeAndUploadDronie() {
final double bearing = mMission.makeAndUploadDronie();
refresh();
return (float) bearing;
}
public List<List<Coord2D>> getPolygonsPath() {
ArrayList<List<Coord2D>> polygonPaths = new ArrayList<List<Coord2D>>();
for (MissionItem item : mMission.getItems()) {
if (item instanceof Survey) {
polygonPaths.add(((Survey)item).polygon.getPoints());
}
}
return polygonPaths;
}
}