Using Vuforia provided Projection Matrix and Marker Pose in SceneKit - scenekit

Currently I'm trying to troubleshoot the use of the projection matrix and framemarker pose when rendering in SceneKit. The model in the scene and the camera image background appear without problems. However, once I change the projection matrix and framemarker pose matrix to match Vuforia everything is pushed offscreen.
func didUpdateProjectionMatrix(projectionMatrix: matrix_float4x4)
{
let extrinsic = SCNMatrix4FromMat4(projectionMatrix)
let camera = self.cameraNode?.camera
camera?.setProjectionTransform(extrinsic)
}
func didUpdateFramemarkers(framemarkers: [Framemarker]?)
{
guard let framemarkers = framemarkers else {
return
}
for framemarker in framemarkers {
let pose = SCNMatrix4Invert(SCNMatrix4FromMat4(framemarker.pose))
self.objectNode?.transform = pose
}
}
Is this a correct way to setup the SCNCamera and object node and is there anything else required to setup Vuforia framemarkers to work with SceneKit?

It just works!
The hard part is determining what pieces of SceneKit are necessary to make this work. Originally I read the article Making Augmented Reality app easily with Scenekit + Vuforia which outlined how to rejigger the sample app for user-defined targets. The downsides to that article include that it isn't always clear what the author changed, no sample project is provided, and it is based upon an older version of Vuforia. Ultimately, I found it unnecessary to invert the pose matrix.
Draw camera image and set projection matrix and update marker pose
override func viewDidLoad()
{
super.viewDidLoad()
let scene = SmartScanScene()
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
scene.rootNode.addChildNode(cameraNode)
_cameraNode = cameraNode
let view = self.view as! SCNView
view.backgroundColor = UIColor.blackColor()
view.showsStatistics = true
// view.debugOptions = SCNDebugOptions.ShowBoundingBoxes.union(.ShowWireframe)
view.autoenablesDefaultLighting = true
view.allowsCameraControl = false
}
func didUpdateProjectionMatrix(projectionMatrix: matrix_float4x4)
{
let extrinsic = SCNMatrix4FromMat4(projectionMatrix)
_cameraNode?.camera?.setProjectionTransform(extrinsic)
}
func didUpdateFramemarkers(framemarkers: [Framemarker]?)
{
guard let framemarkers = framemarkers else {
return
}
for framemarker in framemarkers {
let pose = SCNMatrix4FromMat4(framemarker.pose)
self.objectNode?.transform = pose
}
}
func didUpdateCameraImage(image: UIImage?)
{
if let image = image {
_scene?.background.contents = image
}
}

Related

ImageView inside of ScrollView showing wrong picture

I have a scroll view that has an image view embedded into it. The user clicks the "Take Photo" button, takes a picture, and then those photos are stored in an array and then displayed on the scrollable imageView. My issue is that after the didFinishSelectingMedia is called and the camera closes, the imageView always shows the first image in the array. I want the imageView to show the most recent image added to the array and scroll backward through the images until the user comes to the first image in the array. How do I make this happen?
Btw: Idk if this is even plausible, but I tried reversing the for loop and that didn't work.
I'm a new programmer, so please explain your answers.
Here's my code:
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
#IBOutlet weak var scrollImageView: UIScrollView!
var imageTaken: [UIImage] = []
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
scrollImageView.frame = scrollImageView.frame
}
#IBAction func takePhotoButton(_ sender: Any) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
imageTaken.append((info[.originalImage] as? UIImage)!)
imagePicker.dismiss(animated: true, completion: nil)
for i in 0..<imageTaken.count {
let imageView = UIImageView()
imageView.image = imageTaken[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.scrollImageView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.scrollImageView.frame.width, height: self.scrollImageView.frame.height)
scrollImageView.contentSize.width = scrollImageView.frame.width * CGFloat(i + 1)
scrollImageView.addSubview(imageView)
}
}
}
Two options I guess you can take which ever one is seems easier.
OOP — Create an array of objects example:
struct TakePhotos {
var image : UIImage
var timestamp : Date
}
Then sort the array by the timestamp.
Use PHAsset
import Photos
Using PHAsset you should be able to retrieve the images taken from the user's album. Set a count when the user takes a new image.
Then you can run a query and sort the images in reverse order, they will already have the timestamp.
let photoOptions = PHFetchOptions()
photoOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
This is what I did in my application.
// FIXME: - Query only happens once this needs to be refactored so it happens when the album is updated on the device.
// Query only happens one time.
if imageArray.isEmpty {
let photoOptions = PHFetchOptions()
photoOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let allPhotos = PHAsset.fetchAssets(with: photoOptions)
let fetchOptions = PHImageRequestOptions()
fetchOptions.deliveryMode = .highQualityFormat
fetchOptions.resizeMode = .exact
fetchOptions.normalizedCropRect = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.width)
// Must be a synchronous call otherwise the view loads before images are retrieved (result is an empty view).
fetchOptions.isSynchronous = true
let imageSize : CGSize = CGSize(width: view.frame.width, height: view.frame.height)
//Retrieves Images using assets.
allPhotos.enumerateObjects { (assets, index, pointer) in
DispatchQueue.global(qos: .background).sync {
PHImageManager.default().requestImage(for: assets, targetSize: imageSize, contentMode: .aspectFit, options: fetchOptions) { (image, hashDictionary) in
guard let image = image else {return}
self.imageArray.append(image)
self.albumImages.append(ImageAlbum(images: image))
}
}
}
selectedImage.image = imageArray.first
selectedImage.contentMode = .scaleAspectFill
}

Swift 4 array images show with food loop

This is my code in swift 4 xcode. i have problems with my code. I can't show my array, with images on the simulator what things I'm doing wrong?
im a beginner in swift. and i have tried too look up how you write an array with images with four loop but the simulator docent show
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let img : UIImage = UIImage(named: "owl")!
let imageView = UIImageView(image: img )
self.view.addSubview(imageView)
// var images = Array<UIImage>()
//images.append(UIImage(named: "lion")!,
let ImgArray = ["lion.png","wolf.png","snake.png"]
var images = [UIImage]()
for i in 0..<ImgArray.count
{
images.append(UIImage(named: ImgArray[i])!)
}
let imageView2 = UIImageView(image: img )
self.view.addSubview(imageView2)
}
}
As explained by #Robert, instead of using the index of the array, you can use a for loop this way :
let imagesNames = ["img1.png", "img2.png", "img3.png"]
for imageName in imagesNames {
let image = UIImage(named: imageName)
images.append(image)
}
You can also use map to directly transform your array of images names to an array of images :
let imagesNames = ["img1.png", "img2.png", "img3.png"]
let images = imagesNames.map { UIImage(named: $0) }
where 0$ is your imageName.

Realm Object returning nil (Swift)

I have a custom polygon object so I can save map overlays to Realm. I'm able to create this objects successfully, but when I want to retrieve the var polygon object it returns nil. When I print the polygon object, it prints it out fine, with all the data.
This is a sample of what it prints out.
CustomPolygon {
name = Polygon1;
id = p1;
polygon = Polygon {
coordinates = RLMArray <0x7f928ef36230> (
[0] Coordinate {
latitude = -36.914167;
longitude = 174.904722;
},
[1] Coordinate {
latitude = -36.9325;
longitude = 174.957222;
}, . . .
);
};
}
My Function for loading data from Realm
func loadOverlaysFromRealm(){
do {
let realm = try Realm()
let polygons = realm.objects(CustomPolygon)
for p in polygons {
var coordinates = [CLLocationCoordinate2D]()
print(p) // !!!!! prints out what is above
print(p.polygon) // !!!!! Returns nil.
if let coordinateList = p.polygon?.coordinates as? List<Coordinate> {
for coordinate in coordinateList {
coordinates.append(CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude))
}
}
print(coordinates) // prints "[]"
let polygon = MKPolygon(coordinates: &coordinates, count: coordinates.count)
self.map.addOverlay(polygon)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
My Classes
class CustomPolygon: Object {
var name:String = ""
var id:String = ""
var polygon:Polygon? = nil
}
class Polygon: Object {
var coordinates = List<Coordinate>()
}
class Coordinate: Object {
var latitude:Double = 0.0
var longitude:Double = 0.0
}
The String, Double and Object properties of your Object subclasses need to be declared with the dynamic modifier to allow Realm to override the getter and setter of the property. Without this the Swift compiler will access the object's instance variable directly, which doesn't provide any opportunity for Realm to read or write data from the Realm file. See Realm's model property cheatsheet for a quick overview of how to declare properties of each of the supported types.
If you are having issues with try Realm() returning nil, try removing the App from your simulator and rebuilding. Changing the Object properties as you develop can cause try Realm() to return nil.

Swift: How to handle wait time for parse queries

I am making a Tinder like application where I query an array with parse and display the data in a deck of cards. I have a method where I query an array, and in the same method I use the returned list. However I get an error because the back card is called before the data is finished downloading. "fatal error: unexpectedly found nil while unwrapping an Optional value"
How can I handle this so that the code waits for the query to be retrieved?
Thanks
import UIKit
import MDCSwipeToChoose
class ChoosePersonViewController: UIViewController, MDCSwipeToChooseDelegate {
var people:[Person] = []
let ChoosePersonButtonHorizontalPadding:CGFloat = 80.0
let ChoosePersonButtonVerticalPadding:CGFloat = 20.0
var currentPerson:Person!
var frontCardView:ChoosePersonView!
var backCardView:ChoosePersonView!
let nameData = ""
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let userObjectID = PFUser.currentUser()!.objectId!
let query = PFQuery(className:"Restaurants")
query.whereKey("Pointer", equalTo: userObjectID)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
var nameData = object.objectForKey("Name") as? String
var imageData = object.objectForKey("Image") as? PFFile
imageData!.getDataInBackgroundWithBlock { (imageData, error) -> Void in
if error == nil {
if let imageData = imageData
{
let image = UIImage(data: imageData)
self.people.append(Person(name: nameData, image: image, age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5))
print(self.people)
}
}
}
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Here you can init your properties
let imageData: PFFile
}
override func viewDidLoad(){
super.viewDidLoad()
// Display the first ChoosePersonView in front. Users can swipe to indicate
// whether they like or dislike the person displayed.
self.setMyFrontCardView(self.popPersonViewWithFrame(frontCardViewFrame())!)
self.view.addSubview(self.frontCardView)
// Display the second ChoosePersonView in back. This view controller uses
// the MDCSwipeToChooseDelegate protocol methods to update the front and
// back views after each user swipe.
self.backCardView = self.popPersonViewWithFrame(backCardViewFrame())!
self.view.insertSubview(self.backCardView, belowSubview: self.frontCardView)
// Add buttons to programmatically swipe the view left or right.
// See the `nopeFrontCardView` and `likeFrontCardView` methods.
constructNopeButton()
constructLikedButton()
}
func suportedInterfaceOrientations() -> UIInterfaceOrientationMask{
return UIInterfaceOrientationMask.Portrait
}
// This is called when a user didn't fully swipe left or right.
func viewDidCancelSwipe(view: UIView) -> Void{
print("You couldn't decide on \(self.currentPerson.Name)");
}
// This is called then a user swipes the view fully left or right.
func view(view: UIView, wasChosenWithDirection: MDCSwipeDirection) -> Void{
// MDCSwipeToChooseView shows "NOPE" on swipes to the left,
// and "LIKED" on swipes to the right.
if(wasChosenWithDirection == MDCSwipeDirection.Left){
print("You noped: \(self.currentPerson.Name)")
}
else{
print("You liked: \(self.currentPerson.Name)")
}
// MDCSwipeToChooseView removes the view from the view hierarchy
// after it is swiped (this behavior can be customized via the
// MDCSwipeOptions class). Since the front card view is gone, we
// move the back card to the front, and create a new back card.
if(self.backCardView != nil){
self.setMyFrontCardView(self.backCardView)
}
backCardView = self.popPersonViewWithFrame(self.backCardViewFrame())
//if(true){
// Fade the back card into view.
if(backCardView != nil){
self.backCardView.alpha = 0.0
self.view.insertSubview(self.backCardView, belowSubview: self.frontCardView)
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: {
self.backCardView.alpha = 1.0
},completion:nil)
}
}
func setMyFrontCardView(frontCardView:ChoosePersonView) -> Void{
// Keep track of the person currently being chosen.
// Quick and dirty, just for the purposes of this sample app.
self.frontCardView = frontCardView
self.currentPerson = frontCardView.person
}
func popPersonViewWithFrame(frame:CGRect) -> ChoosePersonView?{
if(self.people.count == 0){
return nil;
}
// UIView+MDCSwipeToChoose and MDCSwipeToChooseView are heavily customizable.
// Each take an "options" argument. Here, we specify the view controller as
// a delegate, and provide a custom callback that moves the back card view
// based on how far the user has panned the front card view.
let options:MDCSwipeToChooseViewOptions = MDCSwipeToChooseViewOptions()
options.delegate = self
//options.threshold = 160.0
options.onPan = { state -> Void in
if(self.backCardView != nil){
let frame:CGRect = self.frontCardViewFrame()
self.backCardView.frame = CGRectMake(frame.origin.x, frame.origin.y-(state.thresholdRatio * 10.0), CGRectGetWidth(frame), CGRectGetHeight(frame))
}
}
// Create a personView with the top person in the people array, then pop
// that person off the stack.
let personView:ChoosePersonView = ChoosePersonView(frame: frame, person: self.people[0], options: options)
self.people.removeAtIndex(0)
return personView
}
I would suggest that starting all the image downloads in a hard loop is ineffective and likely to lead to network flooding and multiple request timeouts.
Consider just storing all of the returned objects and requesting only the first (and perhaps second) images.
Add the views immediately after saving the returned items and deal with adding the image to the view when it becomes available.
It seems that your issue might actually be related to trying to remove a view which doesn't exist yet, though it isn't clear what that function does and it might be something more like it wants to access an object which hasn't been added to the array yet because the next image hasn't downloaded so it can't find / create a suitable item to return... Changing the way you use images and views will resolve either of these causes.

Get size of dae node

Suppose I have a collada file which has a box in it and I import the dae file into the scene. Now after importing in the scene I know the dae object is a box. How can I get the dimensions of the box in scenekit after adding it to the scene
If I import the node as a SCNBox i get errors saying that SNCBox is not a subtype of SCNNode.
floorNode = scene.rootNode.childNodeWithName("floor", recursively: false) as SCNBox
floorNode?.physicsBody = SCNPhysicsBody.staticBody()
floorNode?.physicsBody?.categoryBitMask = 2
let floorGeo: SCNBox = floorNode.geometry! as SCNBox
How do I get the dimensions if SCNNode is the only way to import nodes?
SCNBox is just helper subclass of SCNGeometry for box geometry creation. When you import Collada into a scene, you get scene graph of SCNNodes with SCNGeometries, not SCNBox'es or SCNCones etc doesn't matter how they look. If you want to get dimensions of any node you should use SCNBoundingVolume protocol, which is implemented by both SCNNode and SCNGeometry classes:
func getBoundingBoxMin(_ min: UnsafeMutablePointer,
max max: UnsafeMutablePointer) -> Bool
With this method you will get bounding box corners.
For box-shaped geometry, dimensions will match bounding box.
Example:
var v1 = SCNVector3(x:0, y:0, z:0)
var v2 = SCNVector3(x:0, y:0, z:0)
node.getBoundingBoxMin(&v1, max:&v2)
Where node is node you want to get bounding box of.
Results will be in v1 and v2.
Swift 3
Using Swift 3 you can simply use node.boundingBox.min and node.boundingBox.max respectively.
Example Swift code on how to use boundingBox:
var min = shipNode.boundingBox.min
var max = shipNode.boundingBox.max
let w = CGFloat(max.x - min.x)
let h = CGFloat(max.y - min.y)
let l = CGFloat(max.z - min.z)
let boxShape = SCNBox (width: w , height: h , length: l, chamferRadius: 0.0)
let shape = SCNPhysicsShape(geometry: boxShape, options: nil)
shipNode.physicsBody!.physicsShape = SCNPhysicsShape(geometry: boxShape, options: nil)
shipNode.physicsBody = SCNPhysicsBody.dynamic()
Swift 5.4.
import ARKit
extension SCNNode {
var height: CGFloat { CGFloat(self.boundingBox.max.y - self.boundingBox.min.y) }
var width: CGFloat { CGFloat(self.boundingBox.max.x - self.boundingBox.min.x) }
var length: CGFloat { CGFloat(self.boundingBox.max.z - self.boundingBox.min.z) }
var halfCGHeight: CGFloat { height / 2.0 }
var halfHeight: Float { Float(height / 2.0) }
var halfScaledHeight: Float { halfHeight * self.scale.y }
}
// Usage:
let node = SCNNode()
node.height
node.width
node.lenght

Resources