Skip to content

Commit

Permalink
2D AR clean up (#23)
Browse files Browse the repository at this point in the history
* Clean up unused refs in storyboard

* Remove usdz handling for now

* Remove duplicate file ref

* Clean up AR UI elements
  • Loading branch information
jocmp committed Mar 3, 2024
1 parent b0ce7ba commit b7fdb36
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 750 deletions.
2 changes: 0 additions & 2 deletions ArtAtGVSU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@
8AFEF16F263DDA0800835F6D /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Main.strings; sourceTree = "<group>"; };
8AFEF170263DDA0800835F6D /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = "<group>"; };
9E05669528AD4384009D0C8C /* ARAssetViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ARAssetViewController.swift; sourceTree = "<group>"; };
C44099F22911719F00718803 /* ARAsset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ARAsset.swift; sourceTree = "<group>"; };
C4C3141C28FD9E9800538B99 /* ARAsset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ARAsset.swift; sourceTree = "<group>"; };
C4CEC60F29019E43007440BA /* art.scnassets */ = {isa = PBXFileReference; lastKnownFileType = wrapper.scnassets; path = art.scnassets; sourceTree = "<group>"; };
D22803421C7A588B008B630A /* ArtAtGVSU.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ArtAtGVSU.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -558,7 +557,6 @@
D28A5D351C7BDB3000DDC170 /* Models */ = {
isa = PBXGroup;
children = (
C44099F22911719F00718803 /* ARAsset.swift */,
C4C3141C28FD9E9800538B99 /* ARAsset.swift */,
8A87BFCA265AA2EA00511B3B /* Artist.swift */,
8A9F2BC8265DDFA400C40F2F /* Artwork+FeaturedArtworks.swift */,
Expand Down
42 changes: 18 additions & 24 deletions ArtAtGVSU/ARAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,79 +23,75 @@ struct ARAsset: Equatable {
static func == (lhs: ARAsset, rhs: ARAsset) -> Bool {
lhs.id == rhs.id
}

var id: String = ""
var referenceImage: URL?
var video: URL?
var models: [Model] = []
}

extension ARAsset {

static func getARResources(artwork: Artwork) async throws -> ARAsset {
var arAsset = ARAsset(
id: artwork.id,
referenceImage: artwork.mediaMedium,
video: artwork.arDigitalAsset,
models: artwork.ar3dModels
)

let dirname = "ArtAtGvsu/\(artwork.id)"


arAsset.video = await downloadFile(url: artwork.arDigitalAsset!, dirname: dirname)

arAsset.referenceImage = await downloadFile(url: artwork.mediaMedium!, dirname: dirname)

var models: [Model] = []

for model in artwork.ar3dModels {
let n = await downloadFile(url: model.url, dirname: dirname)
models.append(Model(url: n, metadata: model.metadata))
}

arAsset.models = models

return arAsset
}

static func downloadFile(url: URL, dirname: String) async -> URL {
await withCheckedContinuation { continuation in
downloadFile(url: url, dirname: dirname) { url in
print("[DEBUG] [ARAsset::downloadFile] returned url: \(url)")
continuation.resume(returning: url)
}
}
}

static func downloadFile(url: URL, dirname: String, transport: Transport = URLSession.shared, _ completion: @escaping (URL) -> Void) {
let filename = url.lastPathComponent

guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}

if !createIntermediate(dirname: dirname) {
print("Unable to create intermediate directories for: \(dirname)")
completion(url)
return
}

let localPath = documentsDirectory.appendingPathComponent("\(dirname)/\(filename)")

let filePath = localPath.path

if FileManager.default.fileExists(atPath: filePath) {
print("[DEBUG] [ARAsset::downloadFile] Path to file already exists.")
completion(localPath)
} else {
print("[DEBUG] [ARAsset::downloadFile] Making request")
ArtGalleryClient.init(transport: transport).downloadFile(url, path: localPath, completion: { url, error in
completion(url)
})
}
}

static func createIntermediate(dirname: String) -> Bool{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
Expand All @@ -104,10 +100,8 @@ extension ARAsset {
if !FileManager.default.fileExists(atPath: dataPath.path) {
do {
try FileManager.default.createDirectory(atPath: dataPath.path, withIntermediateDirectories: true, attributes: nil)
print("Directories created: ")
return true
} catch {
print(error.localizedDescription)
return false
}
}
Expand Down
19 changes: 7 additions & 12 deletions ArtAtGVSU/ArtGalleryClient/ArtGalleryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import RSWeb
struct ArtGalleryClient {
let baseURL: String
let transport: Transport

init(transport: Transport = URLSession.shared, baseURL: String = Properties.shared.art_gallery_base_url) {
self.transport = transport
self.baseURL = baseURL
Expand All @@ -37,45 +37,40 @@ struct ArtGalleryClient {
func fetchCampuses(completion: @escaping (Result<CampusSearchResult?, Error>) -> Void) {
sendRequest(resource: "locationcampusSearch?q=*", completion: completion)
}

func fetchTours(completion: @escaping (Result<TourSearchResult?, Error>) -> Void) {
sendRequest(resource: "tourSearch?q=*", completion: completion)
}

func fetchTourStop(id: String, completion: @escaping (Result<TourStopDetail?, Error>) -> Void) {
sendRequest(resource: "tourstopsDetail?id=\(id)", completion: completion)
}

func searchEntities(term: String, completion: @escaping (Result<EntitySearchResult?, Error>) -> Void) {
sendRequest(resource: "entitySearch?q=\(sanitizeTerm(term))", completion: completion)
}

func searchObjects(term: String, completion: @escaping (Result<ObjectSearchResult?, Error>) -> Void) {
sendRequest(resource: "objectSearch?q=\(sanitizeTerm(term))", completion: completion)
}

func downloadFile(_ url: URL, path: URL, completion: @escaping (URL, Error?) -> Void) {

let downloadTask = URLSession.shared.downloadTask(with: url) {
urlOrNil, responseOrNil, errorOrNil in
// check for and handle errors:
// * errorOrNil should be nil
// * responseOrNil should be an HTTPURLResponse with statusCode in 200..<299

if errorOrNil != nil {
completion(url, errorOrNil)
return
}


guard let fileURL = urlOrNil else { return }
do {
let documentsURL = try
FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)

try FileManager.default.moveItem(at: fileURL, to: path)
completion(path, nil)
} catch {
Expand Down
16 changes: 7 additions & 9 deletions ArtAtGVSU/Artwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ extension Artwork {
ar3dModels: self.getModel(file: objectDetail.ar_3d_file_usdz, matrix: objectDetail.ar_coordinates)
)
}

static func getModel(file: String?, matrix: String?) -> [Model] {
if (file == nil) {
guard let file = file else {
return []
}

var m: [Float] = []

var t: SCNMatrix4? = nil

do {
m = try mapToARCoordinates(String(matrix ?? ""))
if m.count == 16 {
Expand All @@ -109,15 +109,13 @@ extension Artwork {
m41:m[12],m42:m[12],m43:m[14],m44:m[15]
)
}
print("That transformation matrix is good!")
} catch {
print("Invalid transformation matrix.")
return []
}


return [
Model(
url: URL(string: file!)!,
url: URL(string: file)!,
metadata: Metadata(
transform: t
)
Expand Down

0 comments on commit b7fdb36

Please sign in to comment.