Skip to content

Commit

Permalink
Convert MaterialFactory to coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
plweegie committed Jul 1, 2020
1 parent efe41c0 commit 22dd7d8
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MagMolActivity : AppCompatActivity() {
}
}

private suspend fun parseSmiles(smiles: String?) {
private suspend fun parseSmiles(smiles: String?) {
loading_pb?.visibility = View.VISIBLE

try {
Expand All @@ -73,9 +73,9 @@ class MagMolActivity : AppCompatActivity() {

addCenterOfMass(Vector3(arMolecule.centerCoordX, arMolecule.centerCoordY, arMolecule.centerCoordZ))

arMolecule.renderableAtoms.forEach { (atom, material) ->
arMolecule.getRenderableAtoms().forEach { (atom, material) ->
val coords = Vector3(atom.xCoord, atom.yCoord, atom.zCoord)
renderAtom(coords, material.await())
renderAtom(coords, material)
}
} catch (e: InvalidSmilesException) {
Toast.makeText(this, "Invalid SMILES", Toast.LENGTH_SHORT).show()
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/com/plweegie/magmolecular/utils/ArMolecule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.plweegie.magmolecular.utils
import android.content.Context
import com.google.ar.sceneform.rendering.Material
import com.google.ar.sceneform.rendering.MaterialFactory
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.future.asDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.openscience.cdk.interfaces.IAtom


Expand All @@ -20,12 +20,15 @@ class ArMolecule(atoms: List<IAtom>, private val context: Context) {
}
}

private val materials: List<Deferred<Material>> = arAtoms.map {
getMaterialAsync(it.color)
private suspend fun getMaterials(): List<Material> = coroutineScope {
arAtoms.map {
val material = async { getMaterialAsync(it.color) }
material.await()
}
}

val renderableAtoms: List<Pair<ArAtom, Deferred<Material>>> = arAtoms zip materials
suspend fun getRenderableAtoms(): List<Pair<ArAtom, Material>> = arAtoms zip getMaterials()

private fun getMaterialAsync(color: Int): Deferred<Material> =
MaterialFactory.makeOpaqueWithColor(context, com.google.ar.sceneform.rendering.Color(color)).asDeferred()
private suspend fun getMaterialAsync(color: Int): Material =
MaterialFactory.makeOpaqueWithColor(context, com.google.ar.sceneform.rendering.Color(color))
}
13 changes: 12 additions & 1 deletion sceneformsrc/sceneform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
Expand Down Expand Up @@ -43,7 +44,17 @@ dependencies {

api 'com.google.android.filament:filament-android:1.7.0'
api 'com.google.android.filament:gltfio-android:1.7.0'
api "com.google.ar:core:1.17.0"
api "com.google.ar:core:1.18.0"

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.3.7'
}
repositories {
mavenCentral()
}

This file was deleted.

Loading

0 comments on commit 22dd7d8

Please sign in to comment.