Skip to content

Commit d3bef02

Browse files
committed
Merge branch 'master' into custom-maps
2 parents 9e701e8 + b64cd78 commit d3bef02

File tree

12 files changed

+548
-37
lines changed

12 files changed

+548
-37
lines changed

ARKit.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
// Copyright © 2017 HippoAR. All rights reserved.
66
//
77

8-
import PropTypes from 'prop-types';
9-
import React, { Component } from 'react';
10-
118
import {
129
StyleSheet,
1310
View,
1411
Text,
1512
NativeModules,
1613
requireNativeComponent,
1714
} from 'react-native';
15+
import PropTypes from 'prop-types';
16+
import React, { Component } from 'react';
1817

18+
import { pickColors, pickColorsFromFile } from './lib/pickColors';
1919
import generateId from './components/lib/generateId';
2020

2121
const ARKitManager = NativeModules.ARKitManager;
@@ -170,6 +170,8 @@ ARKit.exportModel = presetId => {
170170
return ARKitManager.exportModel(property).then(result => ({ ...result, id }));
171171
};
172172

173+
ARKit.pickColors = pickColors;
174+
ARKit.pickColorsFromFile = pickColorsFromFile;
173175
ARKit.propTypes = {
174176
debug: PropTypes.bool,
175177
planeDetection: PropTypes.bool,

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import DeviceMotion from './DeviceMotion';
2525
import startup from './startup';
2626
import withProjectedPosition from './hocs/withProjectedPosition';
2727

28+
import * as colorUtils from './lib/colorUtils';
29+
2830
ARKit.Box = ARBox;
2931
ARKit.Sphere = ARSphere;
3032
ARKit.Cylinder = ARCylinder;
@@ -44,6 +46,7 @@ ARKit.Light = ARLight;
4446
startup();
4547

4648
export {
49+
colorUtils,
4750
ARKit,
4851
DeviceMotion,
4952
ARBox,

ios/RCTARKit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error
6161
- (void)hitTestSceneObjects:(CGPoint)tapPoint resolve:(RCTARKitResolve) resolve reject:(RCTARKitReject)reject;
6262
- (SCNVector3)projectPoint:(SCNVector3)point;
6363
- (float)getCameraDistanceToPoint:(SCNVector3)point;
64-
- (UIImage *)getSnaphshot;
65-
- (UIImage *)getSnaphshotCamera;
64+
- (UIImage *)getSnapshot:(NSDictionary*)selection;
65+
- (UIImage *)getSnapshotCamera:(NSDictionary*)selection;
6666
- (void)focusScene;
6767
- (void)clearScene;
6868
- (NSDictionary *)readCameraPosition;

ios/RCTARKit.m

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,19 @@ - (void)hitTestSceneObjects:(const CGPoint)tapPoint resolve:(RCTARKitResolve)res
252252
}
253253

254254

255-
- (UIImage *)getSnaphshot {
255+
- (UIImage *)getSnapshot:(NSDictionary *)selection {
256256
UIImage *image = [self.arView snapshot];
257-
return image;
257+
258+
259+
return [self cropImage:image toSelection:selection];
260+
258261
}
259262

260263

261264

262265

263266

264-
- (UIImage *)getSnaphsotCamera {
267+
- (UIImage *)getSnapshotCamera:(NSDictionary *)selection {
265268
CVPixelBufferRef pixelBuffer = self.arView.session.currentFrame.capturedImage;
266269
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
267270

@@ -274,11 +277,102 @@ - (UIImage *)getSnaphsotCamera {
274277

275278
UIImage *image = [UIImage imageWithCGImage:videoImage scale: 1.0 orientation:UIImageOrientationRight];
276279
CGImageRelease(videoImage);
277-
return image;
280+
281+
UIImage *cropped = [self cropImage:image toSelection:selection];
282+
return cropped;
283+
278284
}
279285

280286

281287

288+
- (UIImage *)cropImage:(UIImage *)imageToCrop toRect:(CGRect)rect
289+
{
290+
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
291+
292+
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
293+
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
294+
CGImageRelease(imageRef);
295+
296+
return cropped;
297+
}
298+
299+
static inline double radians (double degrees) {return degrees * M_PI/180;}
300+
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
301+
{
302+
UIGraphicsBeginImageContext(src.size);
303+
304+
CGContextRef context = UIGraphicsGetCurrentContext();
305+
[src drawAtPoint:CGPointMake(0, 0)];
306+
if (orientation == UIImageOrientationRight) {
307+
CGContextRotateCTM (context, radians(90));
308+
} else if (orientation == UIImageOrientationLeft) {
309+
CGContextRotateCTM (context, radians(-90));
310+
} else if (orientation == UIImageOrientationDown) {
311+
// NOTHING
312+
} else if (orientation == UIImageOrientationUp) {
313+
CGContextRotateCTM (context, radians(90));
314+
}
315+
316+
317+
318+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
319+
UIGraphicsEndImageContext();
320+
return image;
321+
}
322+
- (UIImage *)cropImage:(UIImage *)imageToCrop toSelection:(NSDictionary *)selection
323+
{
324+
325+
// selection is in view-coordinate system
326+
// where as the image is a camera picture with arbitary size
327+
// also, the camera picture is cut of so that it "covers" the self.bounds
328+
// if selection is nil, crop to the viewport
329+
330+
UIImage * image = rotate(imageToCrop, imageToCrop.imageOrientation);
331+
332+
float arViewWidth = self.bounds.size.width;
333+
float arViewHeight = self.bounds.size.height;
334+
float imageWidth = image.size.width;
335+
float imageHeight = image.size.height;
336+
337+
float arViewRatio = arViewHeight/arViewWidth;
338+
float imageRatio = imageHeight/imageWidth;
339+
float imageToArWidth = imageWidth/arViewWidth;
340+
float imageToArHeight = imageHeight/arViewHeight;
341+
342+
float finalHeight;
343+
float finalWidth;
344+
345+
346+
if (arViewRatio > imageRatio)
347+
{
348+
finalHeight = arViewHeight*imageToArHeight;
349+
finalWidth = arViewHeight*imageToArHeight /arViewRatio;
350+
}
351+
else
352+
{
353+
finalWidth = arViewWidth*imageToArWidth;
354+
finalHeight = arViewWidth * imageToArWidth * arViewRatio;
355+
}
356+
357+
float topOffset = (image.size.height - finalHeight)/2;
358+
float leftOffset = (image.size.width - finalWidth)/2;
359+
360+
361+
float x = leftOffset;
362+
float y = topOffset;
363+
float width = finalWidth;
364+
float height = finalHeight;
365+
if(selection && selection != [NSNull null]) {
366+
x = leftOffset+ [selection[@"x"] floatValue]*imageToArWidth;
367+
y = topOffset+[selection[@"y"] floatValue]*imageToArHeight;
368+
width = [selection[@"width"] floatValue]*imageToArWidth;
369+
height = [selection[@"height"] floatValue]*imageToArHeight;
370+
}
371+
CGRect rect = CGRectMake(x, y, width, height);
372+
373+
UIImage *cropped = [self cropImage:image toRect:rect];
374+
return cropped;
375+
}
282376

283377

284378
#pragma mark - plane hit detection
@@ -408,7 +502,7 @@ - (void)renderer:(id <SCNSceneRenderer>)renderer willUpdateNode:(SCNNode *)node
408502

409503
- (void)renderer:(id <SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor {
410504
ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
411-
505+
412506
if (self.onPlaneUpdate) {
413507
self.onPlaneUpdate(@{
414508
@"id": planeAnchor.identifier.UUIDString,

ios/RCTARKit.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
10ED47A71F38BC01004DF043 /* DeviceMotion.m in Sources */ = {isa = PBXBuildFile; fileRef = 10ED47A61F38BC00004DF043 /* DeviceMotion.m */; };
1717
10FEF6141F774C9000EC21AE /* RCTARKitIO.m in Sources */ = {isa = PBXBuildFile; fileRef = 10FEF6101F774C8F00EC21AE /* RCTARKitIO.m */; };
1818
10FEF6151F774C9000EC21AE /* RCTARKitNodes.m in Sources */ = {isa = PBXBuildFile; fileRef = 10FEF6121F774C9000EC21AE /* RCTARKitNodes.m */; };
19+
B1990B221FCEEBD60001AE2F /* color-grabber.m in Sources */ = {isa = PBXBuildFile; fileRef = B1990B211FCEEBD60001AE2F /* color-grabber.m */; };
1920
B3E7B58A1CC2AC0600A0062D /* RCTARKit.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RCTARKit.m */; };
2021
/* End PBXBuildFile section */
2122

@@ -63,6 +64,8 @@
6364
134814201AA4EA6300B7C361 /* libRCTARKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTARKit.a; sourceTree = BUILT_PRODUCTS_DIR; };
6465
B14C36631F960C500047CB67 /* PocketSVG.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = PocketSVG.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6566
B14C36651F960C6E0047CB67 /* PocketSVG.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = PocketSVG.framework; sourceTree = BUILT_PRODUCTS_DIR; };
67+
B1990B201FCEEBD60001AE2F /* color-grabber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "color-grabber.h"; sourceTree = "<group>"; };
68+
B1990B211FCEEBD60001AE2F /* color-grabber.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "color-grabber.m"; sourceTree = "<group>"; };
6669
B3E7B5881CC2AC0600A0062D /* RCTARKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTARKit.h; sourceTree = "<group>"; };
6770
B3E7B5891CC2AC0600A0062D /* RCTARKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTARKit.m; sourceTree = "<group>"; };
6871
/* End PBXFileReference section */
@@ -102,6 +105,7 @@
102105
58B511D21A9E6C8500147676 = {
103106
isa = PBXGroup;
104107
children = (
108+
B1990B1F1FCEEBD60001AE2F /* color-grabber */,
105109
106999E21F3EC2FB00032829 /* components */,
106110
10ED47A51F38BC00004DF043 /* DeviceMotion.h */,
107111
10ED47A61F38BC00004DF043 /* DeviceMotion.m */,
@@ -132,6 +136,15 @@
132136
name = Frameworks;
133137
sourceTree = "<group>";
134138
};
139+
B1990B1F1FCEEBD60001AE2F /* color-grabber */ = {
140+
isa = PBXGroup;
141+
children = (
142+
B1990B201FCEEBD60001AE2F /* color-grabber.h */,
143+
B1990B211FCEEBD60001AE2F /* color-grabber.m */,
144+
);
145+
path = "color-grabber";
146+
sourceTree = "<group>";
147+
};
135148
/* End PBXGroup section */
136149

137150
/* Begin PBXNativeTarget section */
@@ -192,6 +205,7 @@
192205
10FEF6141F774C9000EC21AE /* RCTARKitIO.m in Sources */,
193206
10DCBC4B1F7CE836008C89E7 /* ARGeosManager.m in Sources */,
194207
10FEF6151F774C9000EC21AE /* RCTARKitNodes.m in Sources */,
208+
B1990B221FCEEBD60001AE2F /* color-grabber.m in Sources */,
195209
10E553291F1391350059B7EC /* Plane.m in Sources */,
196210
1021FE1C1F3EDB98000E7339 /* ARModelManager.m in Sources */,
197211
1021FE1D1F3EDB9B000E7339 /* ARTextManager.m in Sources */,

0 commit comments

Comments
 (0)