Permalink
Browse files

Changed some variable names. Added an optional perspective rotation t…

…o views based on distance from center. Significantly reduced the number of times view coordinates are calculated.
  • Loading branch information...
1 parent 41ae2ef commit a77bc5c68eaacb13996dfdd9ce2d3e8ebcea5d63 Zac White committed Oct 12, 2009
@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
+ 1109BC311082F1B2006FFB4B /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1109BC301082F1B2006FFB4B /* QuartzCore.framework */; };
+ 1109BCBE1082F1D4006FFB4B /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1109BC301082F1B2006FFB4B /* QuartzCore.framework */; };
1118AA4A10257DCE00C94460 /* location.png in Resources */ = {isa = PBXBuildFile; fileRef = 1118AA4910257DCE00C94460 /* location.png */; };
111A141C1024C875002D39A1 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 111A141B1024C875002D39A1 /* CoreLocation.framework */; };
111A142C1024C8A6002D39A1 /* ARViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 111A142A1024C8A6002D39A1 /* ARViewController.m */; };
@@ -32,17 +34,8 @@
CE169F581025704E00556F8F /* ARGeoCoordinate.m in Sources */ = {isa = PBXBuildFile; fileRef = CE169F571025704E00556F8F /* ARGeoCoordinate.m */; };
/* End PBXBuildFile section */
-/* Begin PBXContainerItemProxy section */
- 11A884BD10730D5D006B9518 /* PBXContainerItemProxy */ = {
- isa = PBXContainerItemProxy;
- containerPortal = 29B97313FDCFA39411CA2CEA /* Project object */;
- proxyType = 1;
- remoteGlobalIDString = 11192949106E7C3D00D96627;
- remoteInfo = UnitTests;
- };
-/* End PBXContainerItemProxy section */
-
/* Begin PBXFileReference section */
+ 1109BC301082F1B2006FFB4B /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
1118AA4910257DCE00C94460 /* location.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = location.png; sourceTree = "<group>"; };
1119294A106E7C3D00D96627 /* UnitTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UnitTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
111A141B1024C875002D39A1 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
@@ -80,6 +73,7 @@
1171B295106EAD6500CC3630 /* CoreGraphics.framework in Frameworks */,
1171B2A2106EAD6B00CC3630 /* CoreLocation.framework in Frameworks */,
1171B2B0106EAD6F00CC3630 /* MapKit.framework in Frameworks */,
+ 1109BCBE1082F1D4006FFB4B /* QuartzCore.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -92,6 +86,7 @@
288765FD0DF74451002DB57D /* CoreGraphics.framework in Frameworks */,
111A141C1024C875002D39A1 /* CoreLocation.framework in Frameworks */,
116DC887102933A200EB5A0A /* MapKit.framework in Frameworks */,
+ 1109BC311082F1B2006FFB4B /* QuartzCore.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -177,6 +172,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
+ 1109BC301082F1B2006FFB4B /* QuartzCore.framework */,
116DC886102933A200EB5A0A /* MapKit.framework */,
111A141B1024C875002D39A1 /* CoreLocation.framework */,
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */,
@@ -218,7 +214,6 @@
buildRules = (
);
dependencies = (
- 11A884BE10730D5D006B9518 /* PBXTargetDependency */,
);
name = ARKitDemo;
productName = ARKitDemo;
@@ -309,14 +304,6 @@
};
/* End PBXSourcesBuildPhase section */
-/* Begin PBXTargetDependency section */
- 11A884BE10730D5D006B9518 /* PBXTargetDependency */ = {
- isa = PBXTargetDependency;
- target = 11192949106E7C3D00D96627 /* UnitTests */;
- targetProxy = 11A884BD10730D5D006B9518 /* PBXContainerItemProxy */;
- };
-/* End PBXTargetDependency section */
-
/* Begin XCBuildConfiguration section */
1119294C106E7C3E00D96627 /* Debug */ = {
isa = XCBuildConfiguration;
@@ -10,6 +10,9 @@
#import <MapKit/MapKit.h>
+#define degreesToRadians(x) (M_PI * x / 180.0)
+#define radiansToDegrees(x) (x * (180.0/M_PI))
+
@class ARCoordinate;
@protocol ARPersistentItem
@@ -58,7 +58,7 @@ - (void)dealloc {
}
- (NSString *)description {
- return [NSString stringWithFormat:@"%@ r: %.3fm φ: %.3f° θ: %.3f°", self.title, self.radialDistance, self.azimuth * (180.0/M_PI), self.inclination * (180.0/M_PI)];
+ return [NSString stringWithFormat:@"%@ r: %.3fm φ: %.3f° θ: %.3f°", self.title, self.radialDistance, radiansToDegrees(self.azimuth), radiansToDegrees(self.inclination)];
}
@end
@@ -22,8 +22,8 @@ - (void)setCenterLocation:(CLLocation *)newLocation {
if ([geoLocation isKindOfClass:[ARGeoCoordinate class]]) {
[geoLocation calibrateUsingOrigin:centerLocation];
- if (geoLocation.radialDistance > self.maximumDistance) {
- self.maximumDistance = geoLocation.radialDistance;
+ if (geoLocation.radialDistance > self.maximumScaleDistance) {
+ self.maximumScaleDistance = geoLocation.radialDistance;
}
}
}
@@ -22,9 +22,11 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {
viewController.delegate = self;
- viewController.scalesViewsBasedOnDistance = YES;
+ //viewController.scaleViewsBasedOnDistance = YES;
viewController.minimumScaleFactor = .5;
+ viewController.rotateViewsBasedOnPerspective = YES;
+
NSMutableArray *tempLocationArray = [[NSMutableArray alloc] initWithCapacity:10];
CLLocation *tempLocation;
@@ -31,13 +31,21 @@
NSObject<CLLocationManagerDelegate> *locationDelegate;
NSObject<UIAccelerometerDelegate> *accelerometerDelegate;
- BOOL scalesViewsBasedOnDistance;
- double maximumDistance;
+ BOOL scaleViewsBasedOnDistance;
+ double maximumScaleDistance;
double minimumScaleFactor;
+ //defaults to 20hz;
+ double updateFrequency;
+
+ BOOL rotateViewsBasedOnPerspective;
+ double maximumRotationAngle;
+
@private
BOOL ar_debugMode;
+ NSTimer *_updateTimer;
+
UIView *ar_overlayView;
UILabel *ar_debugView;
@@ -50,10 +58,15 @@
@property BOOL debugMode;
-@property BOOL scalesViewsBasedOnDistance;
-@property double maximumDistance;
+@property BOOL scaleViewsBasedOnDistance;
+@property double maximumScaleDistance;
@property double minimumScaleFactor;
+@property BOOL rotateViewsBasedOnPerspective;
+@property double maximumRotationAngle;
+
+@property double updateFrequency;
+
//adding coordinates to the underlying data model.
- (void)addCoordinate:(ARCoordinate *)coordinate;
- (void)addCoordinate:(ARCoordinate *)coordinate animated:(BOOL)animated;
@@ -70,7 +83,7 @@
- (id)initWithLocationManager:(CLLocationManager *)manager;
- (void)startListening;
-- (void)updateLocations;
+- (void)updateLocations:(NSTimer *)timer;
- (CGPoint)pointInView:(UIView *)realityView forCoordinate:(ARCoordinate *)coordinate;
@@ -8,6 +8,8 @@
#import "ARViewController.h"
+#import <QuartzCore/QuartzCore.h>
+
#define VIEWPORT_WIDTH_RADIANS .5
#define VIEWPORT_HEIGHT_RADIANS .7392
@@ -16,9 +18,11 @@ @implementation ARViewController
@synthesize locationManager, accelerometerManager;
@synthesize centerCoordinate;
-@synthesize scalesViewsBasedOnDistance;
-@synthesize maximumDistance;
-@synthesize minimumScaleFactor;
+@synthesize scaleViewsBasedOnDistance, rotateViewsBasedOnPerspective;
+@synthesize maximumScaleDistance;
+@synthesize minimumScaleFactor, maximumRotationAngle;
+
+@synthesize updateFrequency;
@synthesize debugMode = ar_debugMode;
@@ -39,6 +43,9 @@ - (id)init {
ar_coordinates = [[NSMutableArray alloc] init];
ar_coordinateViews = [[NSMutableArray alloc] init];
+ _updateTimer = nil;
+ self.updateFrequency = 1 / 20.0;
+
#if !TARGET_IPHONE_SIMULATOR
self.cameraController = [[[UIImagePickerController alloc] init] autorelease];
@@ -51,10 +58,13 @@ - (id)init {
self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
#endif
- self.scalesViewsBasedOnDistance = NO;
- self.maximumDistance = 0.0;
+ self.scaleViewsBasedOnDistance = NO;
+ self.maximumScaleDistance = 0.0;
self.minimumScaleFactor = 1.0;
+ self.rotateViewsBasedOnPerspective = NO;
+ self.maximumRotationAngle = M_PI / 6.0;
+
self.wantsFullScreenLayout = YES;
return self;
@@ -88,6 +98,22 @@ - (void)loadView {
self.view = ar_overlayView;
}
+- (void)setUpdateFrequency:(double)newUpdateFrequency {
+
+ updateFrequency = newUpdateFrequency;
+
+ if (!_updateTimer) return;
+
+ [_updateTimer invalidate];
+ [_updateTimer release];
+
+ _updateTimer = [[NSTimer scheduledTimerWithTimeInterval:self.updateFrequency
+ target:self
+ selector:@selector(updateLocations:)
+ userInfo:nil
+ repeats:YES] retain];
+}
+
- (void)setDebugMode:(BOOL)flag {
if (self.debugMode == flag) return;
@@ -226,8 +252,6 @@ - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAccelera
} else if (rollingX >= 0) {
self.centerCoordinate.inclination = 3 * M_PI/2.0;
}
-
- [self updateLocations];
if (self.accelerometerDelegate && [self.accelerometerDelegate respondsToSelector:@selector(accelerometer:didAccelerate:)]) {
//forward the acceleromter.
@@ -253,8 +277,8 @@ - (void)addCoordinate:(ARCoordinate *)coordinate animated:(BOOL)animated {
//do some kind of animation?
[ar_coordinates addObject:coordinate];
- if (coordinate.radialDistance > self.maximumDistance) {
- self.maximumDistance = coordinate.radialDistance;
+ if (coordinate.radialDistance > self.maximumScaleDistance) {
+ self.maximumScaleDistance = coordinate.radialDistance;
}
//message the delegate.
@@ -289,9 +313,9 @@ - (void)removeCoordinates:(NSArray *)coordinates {
}
}
-- (void)updateLocations {
+- (void)updateLocations:(NSTimer *)timer {
//update locations!
-
+
if (!ar_coordinateViews || ar_coordinateViews.count == 0) {
return;
}
@@ -308,24 +332,41 @@ - (void)updateLocations {
CGPoint loc = [self pointInView:ar_overlayView forCoordinate:item];
CGFloat scaleFactor = 1.0;
- if (self.scalesViewsBasedOnDistance) {
- scaleFactor = 1.0 - self.minimumScaleFactor * (item.radialDistance / self.maximumDistance);
+ if (self.scaleViewsBasedOnDistance) {
+ scaleFactor = 1.0 - self.minimumScaleFactor * (item.radialDistance / self.maximumScaleDistance);
}
float width = viewToDraw.bounds.size.width * scaleFactor;
float height = viewToDraw.bounds.size.height * scaleFactor;
viewToDraw.frame = CGRectMake(loc.x - width / 2.0, loc.y - height / 2.0, width, height);
+ CATransform3D transform = CATransform3DIdentity;
+
+ //set the scale if it needs it.
+ if (self.scaleViewsBasedOnDistance) {
+ //scale the perspective transform if we have one.
+ transform = CATransform3DScale(transform, scaleFactor, scaleFactor, scaleFactor);
+ }
+
+ if (self.rotateViewsBasedOnPerspective) {
+ transform.m34 = 1.0 / 300.0;
+
+ double itemAzimuth = item.azimuth;
+ double centerAzimuth = self.centerCoordinate.azimuth;
+ if (itemAzimuth - centerAzimuth > M_PI) centerAzimuth += 2*M_PI;
+ if (itemAzimuth - centerAzimuth < -M_PI) itemAzimuth += 2*M_PI;
+
+ double angleDifference = itemAzimuth - centerAzimuth;
+ transform = CATransform3DRotate(transform, self.maximumRotationAngle * angleDifference / (VIEWPORT_HEIGHT_RADIANS / 2.0) , 0, 1, 0);
+ }
+
+ viewToDraw.layer.transform = transform;
+
//if we don't have a superview, set it up.
if (!(viewToDraw.superview)) {
[ar_overlayView addSubview:viewToDraw];
[ar_overlayView sendSubviewToBack:viewToDraw];
-
- //set the scale if it needs it.
- if (self.scalesViewsBasedOnDistance) {
- viewToDraw.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
- }
}
} else {
@@ -339,7 +380,6 @@ - (void)updateLocations {
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
self.centerCoordinate.azimuth = fmod(newHeading.magneticHeading, 360.0) * (2 * (M_PI / 360.0));
- [self updateLocations];
if (self.locationDelegate && [self.locationDelegate respondsToSelector:@selector(locationManager:didUpdateHeading:)]) {
//forward the call.
@@ -378,19 +418,30 @@ - (void)viewDidAppear:(BOOL)animated {
[ar_overlayView setFrame:self.cameraController.view.bounds];
#endif
+
+ if (!_updateTimer) {
+ _updateTimer = [[NSTimer scheduledTimerWithTimeInterval:self.updateFrequency
+ target:self
+ selector:@selector(updateLocations:)
+ userInfo:nil
+ repeats:YES] retain];
+ }
+
[super viewDidAppear:animated];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
-
+
if (self.debugMode) {
[ar_debugView sizeToFit];
[ar_debugView setFrame:CGRectMake(0,
ar_overlayView.frame.size.height - ar_debugView.frame.size.height,
ar_overlayView.frame.size.width,
ar_debugView.frame.size.height)];
}
+
+
}
- (void)didReceiveMemoryWarning {
@@ -417,5 +468,4 @@ - (void)dealloc {
[super dealloc];
}
-
@end

0 comments on commit a77bc5c

Please sign in to comment.