Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approximation helper sync android #4331

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

- (instancetype)initWithLocations:(NSArray<OALocationsHolder *> *)locations initialAppMode:(OAApplicationMode *)appMode initialThreshold:(float)threshold;

- (BOOL)calculateGpxApproximation:(BOOL)newCalculation;
- (void)calculateGpxApproximationAsync;
- (OASGpxFile *)approximateGpxSync:(OASGpxFile *)gpxFile params:(OAGpxApproximationParams *)params;
- (void)updateAppMode:(OAApplicationMode *)appMode;
- (void)updateDistanceThreshold:(float)threshold;
Expand Down
123 changes: 50 additions & 73 deletions Sources/Controllers/RoutePlanning/OAGpxApproximationHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ @interface OAGpxApproximationHelper () <OAGpxApproximationProgressDelegate>

@implementation OAGpxApproximationHelper
{
NSMutableDictionary<OALocationsHolder *, OAGpxRouteApproximation *> *_resultMap;
NSArray<OALocationsHolder *> *_locationsHolders;
OAGpxApproximator *_gpxApproximator;
OAGpxApproximator *_currentApproximator;
OAApplicationMode *_appMode;
float _distanceThreshold;
}
Expand All @@ -36,7 +35,6 @@ - (instancetype)initWithLocations:(NSArray<OALocationsHolder *> *)locations init
if (self)
{
_locationsHolders = [locations copy];
_resultMap = [[NSMutableDictionary alloc] init];
_appMode = appMode;
_distanceThreshold = threshold;
}
Expand All @@ -54,91 +52,71 @@ - (void)updateDistanceThreshold:(float)threshold
_distanceThreshold = threshold;
}

- (BOOL)calculateGpxApproximation:(BOOL)newCalculation
- (void)calculateGpxApproximationAsync
{
// UI Thread+
if (newCalculation)
if (_currentApproximator != nil)
{
if (_gpxApproximator != nil)
{
[_gpxApproximator cancelApproximation];
_gpxApproximator = nil;
}

[_resultMap removeAllObjects];
if (self.delegate)
[self.delegate didStartProgress];
[_currentApproximator cancelApproximation];
_currentApproximator = nil;
}

OAGpxApproximator *gpxApproximator = nil;
if (self.delegate)
[self.delegate didStartProgress];

NSMutableArray<OAGpxApproximator *> *approximateList = [NSMutableArray array];
for (OALocationsHolder *locationsHolder in _locationsHolders)
{
if (!_resultMap[locationsHolder])
{
gpxApproximator = [self getNewGpxApproximator:locationsHolder];
break;
}
OAGpxApproximator *approximate = [self getNewGpxApproximator:locationsHolder];
if (approximate != nil)
[approximateList addObject:approximate];
}

if (gpxApproximator != nil)
{
_gpxApproximator = gpxApproximator;
_gpxApproximator.mode = _appMode;
_gpxApproximator.pointApproximation = _distanceThreshold;
[self approximateGpx:_gpxApproximator];
return YES;
}
NSMutableDictionary<OALocationsHolder *, OAGpxRouteApproximation *> *approximateResult = [[NSMutableDictionary alloc] init];
if (self.delegate)
[self.delegate didApproximationStarted];

return NO;
@try {
[self approximateMultipleGpxAsync:approximateList withResult:approximateResult];
} @catch (NSException *exception) {
NSLog(@"Error: %@, %@", exception.name, exception.reason);
}
}

- (OAGpxApproximator *)getNewGpxApproximator:(OALocationsHolder *)locationsHolder
{
OAGpxApproximator *gpxApproximator = [[OAGpxApproximator alloc] initWithApplicationMode:_appMode pointApproximation:_distanceThreshold locationsHolder:locationsHolder];
gpxApproximator.progressDelegate = self;
[gpxApproximator setMode:_appMode];
[gpxApproximator setPointApproximation:_distanceThreshold];
return gpxApproximator;
}

- (void) approximateGpx:(OAGpxApproximator *)gpxApproximator
{
// UI Thread+
if (self.delegate)
[self.delegate didApproximationStarted];

[gpxApproximator calculateGpxApproximation:[[OAResultMatcher alloc] initWithPublishFunc:^BOOL(OAGpxRouteApproximation *__autoreleasing *object) {
// wait for first result as final
if (!gpxApproximator.isCancelled)
{
if (*object)
_resultMap[gpxApproximator.locationsHolder] = *object;
if (![self calculateGpxApproximation:NO])
[self onApproximationFinished];
}
return YES;
} cancelledFunc:^BOOL {
return NO;
}]];
}

- (void)onApproximationFinished
- (void)approximateMultipleGpxAsync:(NSMutableArray<OAGpxApproximator *> *)approximationsToDo withResult:(NSMutableDictionary<OALocationsHolder *, OAGpxRouteApproximation *> *)approximateResult
{
NSMutableArray<OAGpxRouteApproximation *> *approximations = [NSMutableArray array];
NSMutableArray<NSArray<OASWptPt *> *> *points = [NSMutableArray array];
for (OALocationsHolder *locationsHolder in _locationsHolders)
if (approximationsToDo.count > 0)
{
OAGpxRouteApproximation *approximation = _resultMap[locationsHolder];
if (approximation != nil)
OAGpxApproximator *gpxApproximator = approximationsToDo.firstObject;
[approximationsToDo removeObjectAtIndex:0];
_currentApproximator = gpxApproximator;
[gpxApproximator calculateGpxApproximation:[[OAResultMatcher alloc] initWithPublishFunc:^BOOL(OAGpxRouteApproximation *__autoreleasing *approxPtr) {
OAGpxRouteApproximation *strongApprox = (approxPtr && *approxPtr) ? *approxPtr : nil;
dispatch_async(dispatch_get_main_queue(), ^{
if (!gpxApproximator.isCancelled && strongApprox)
approximateResult[gpxApproximator.locationsHolder] = strongApprox;
[self approximateMultipleGpxAsync:approximationsToDo withResult:approximateResult];
});
return YES;
} cancelledFunc:^BOOL {
return NO;
}]];
} else {
if (approximateResult.count > 0)
{
[approximations addObject:approximation];
[points addObject:locationsHolder.getWptPtList];
NSArray *pair = [self processApproximationResults:approximateResult];
if (self.delegate)
[self.delegate didFinishAllApproximationsWithResults:pair.firstObject points:pair.lastObject];
}
}

if (self.delegate) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate didFinishAllApproximationsWithResults:approximations points:points];
});
}
}

- (OASGpxFile *)approximateGpxSync:(OASGpxFile *)gpxFile params:(OAGpxApproximationParams *)params
Expand All @@ -154,31 +132,32 @@ - (OASGpxFile *)approximateGpxSync:(OASGpxFile *)gpxFile params:(OAGpxApproximat

- (NSArray *)calculateGpxApproximationSync
{
NSMutableDictionary<OALocationsHolder *, OAGpxRouteApproximation *> *approximateResult = [[NSMutableDictionary alloc] init];
for (OALocationsHolder *holder in _locationsHolders)
{
OAGpxApproximator *approximator = [self getNewGpxApproximator:holder];
if (approximator)
{
[approximator calculateGpxApproximationSync:[[OAResultMatcher alloc] initWithPublishFunc:^BOOL(OAGpxRouteApproximation *__autoreleasing *approximation) {
if (approximation && *approximation)
_resultMap[holder] = *approximation;
approximateResult[holder] = *approximation;
return YES;
} cancelledFunc:^BOOL {
return NO;
}]];
}
}

return [self processApproximationResults];
return [self processApproximationResults:approximateResult];
}

- (NSArray *)processApproximationResults
- (NSArray *)processApproximationResults:(NSDictionary<OALocationsHolder *, OAGpxRouteApproximation *> *)approximateResult
{
NSMutableArray<OAGpxRouteApproximation *> *approximations = [NSMutableArray array];
NSMutableArray<NSArray<OASWptPt *> *> *points = [NSMutableArray array];
for (OALocationsHolder *holder in _locationsHolders)
{
OAGpxRouteApproximation *approximation = _resultMap[holder];
OAGpxRouteApproximation *approximation = approximateResult[holder];
if (approximation)
{
[approximations addObject:approximation];
Expand Down Expand Up @@ -217,12 +196,10 @@ - (OAMeasurementEditingContext *)createEditingContext:(OASGpxFile *)gpxFile para
- (void)updateProgress:(OAGpxApproximator *)approximator progress:(NSInteger)progress
{
// UI Thread+
if (approximator == _gpxApproximator)
if (approximator == _currentApproximator)
{
float partSize = 100. / _locationsHolders.count;
float p = _resultMap.count * partSize + (progress / 100.) * partSize;
if (self.delegate)
[self.delegate didUpdateProgress:(int)p];
[self.delegate didUpdateProgress:progress];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ - (void)viewDidLoad

_approximationHelper = [[OAGpxApproximationHelper alloc] initWithLocations:_locationsHolders initialAppMode:_snapToRoadAppMode initialThreshold:_distanceThreshold];
_approximationHelper.delegate = self;
[_approximationHelper calculateGpxApproximation:YES];
[_approximationHelper calculateGpxApproximationAsync];

_progressBarView = [[UIProgressView alloc] init];
_progressBarView.hidden = YES;
Expand Down Expand Up @@ -186,7 +186,7 @@ - (void)sliderValueChanged:(id)sender
UISlider *slider = sender;
_distanceThreshold = slider.value;
[_approximationHelper updateDistanceThreshold:_distanceThreshold];
[_approximationHelper calculateGpxApproximation:YES];
[_approximationHelper calculateGpxApproximationAsync];
OATitleSliderRoundCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.valueLabel.text = [OAOsmAndFormatter getFormattedDistance:_distanceThreshold];
}
Expand Down Expand Up @@ -285,7 +285,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
_snapToRoadAppMode = item[@"profile"];
[_approximationHelper updateAppMode:_snapToRoadAppMode];
[tableView reloadData];
[_approximationHelper calculateGpxApproximation:YES];
[_approximationHelper calculateGpxApproximationAsync];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Router/OAGpxApproximator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ - (void) updateProgress:(SHARED_PTR<GpxRouteApproximation>)gctx
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// + UI Thread
const auto calculationProgress = _gctx->ctx->progress;
if (_approximationTask && _gctx == gctx)
const auto calculationProgress = gctx->ctx->progress;
if (!_approximationTask && _gctx == gctx)
[self finishProgress];

if (_approximationTask != nil && calculationProgress != nullptr && !calculationProgress->isCancelled())
{
float pr = calculationProgress->getLinearProgress();
float pr = calculationProgress->getApproximationProgress();
if ([self.progressDelegate respondsToSelector:@selector(updateProgress:progress:)])
[self.progressDelegate updateProgress:self progress:(int)pr];
if (_gctx == gctx)
Expand Down