Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
plweegie committed May 3, 2020
1 parent 55cf324 commit 3b1da01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
30 changes: 24 additions & 6 deletions app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.ar.sceneform.AnchorNode
import com.google.ar.sceneform.Node
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.rendering.Material
import com.google.ar.sceneform.rendering.ShapeFactory
import com.google.ar.sceneform.ux.ArFragment
import com.google.ar.sceneform.ux.TransformableNode
import com.plweegie.magmolecular.R
import com.plweegie.magmolecular.rendering.AndroidModelBuilder3D
import com.plweegie.magmolecular.utils.ArMolecule
Expand Down Expand Up @@ -38,6 +40,7 @@ class MagMolActivity : AppCompatActivity() {
private lateinit var modelBuilder: AndroidModelBuilder3D
private lateinit var arFragment: ArFragment

private var transformableNode: TransformableNode? = null
private var smiles: String? = null

private val renderableJob = Job()
Expand Down Expand Up @@ -68,26 +71,41 @@ class MagMolActivity : AppCompatActivity() {
val atoms3d = get3DCoordinates(atomContainer)
val arMolecule = ArMolecule(atoms3d, this)

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

arMolecule.renderableAtoms.forEach { (atom, material) ->
val coords = Vector3(atom.xCoord, atom.yCoord, atom.zCoord)
addRenderable(coords, material.await())
renderAtom(coords, material.await())
}
} catch (e: InvalidSmilesException) {
Toast.makeText(this, "Invalid SMILES", Toast.LENGTH_SHORT).show()
}
}

private fun addRenderable(position: Vector3, material: Material) {
private fun addCenterOfMass(position: Vector3) {
val anchorNode = AnchorNode().apply {
worldPosition = position.adjustPosition()
}
transformableNode = TransformableNode(arFragment.transformationSystem).apply {
setParent(anchorNode)
}

arFragment.arSceneView.scene.addChild(anchorNode)
}

private fun renderAtom(position: Vector3, material: Material) {
loading_pb?.visibility = View.GONE

val node = Node().apply {
worldPosition = Vector3.subtract(position.scaled(0.05f), Vector3(0.0f, 0.2f, 0.5f))
Node().apply {
worldPosition = position.adjustPosition()
renderable = ShapeFactory.makeSphere(0.05f, Vector3.zero(), material)
setParent(transformableNode)
}

arFragment.arSceneView.scene.addChild(node)
}

private fun Vector3.adjustPosition(): Vector3 =
Vector3.subtract(this.scaled(0.05f), Vector3(0.0f, 0.2f, 0.5f))

private suspend fun get3DCoordinates(atomContainer: IAtomContainer): List<IAtom> {
val atomContainer3D = withContext(Dispatchers.Default) {
modelBuilder.generate3DCoordinates(atomContainer, false)
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,32 @@ class MagMolFragment : ArFragment() {
override fun onUpdated(gesture: DragGesture?) {
endPosition = gesture?.position
val angle = getRotationAngle(startPosition, endPosition)
gesture?.targetNode?.localRotation = Quaternion.multiply(
gesture?.targetNode?.localRotation,
Quaternion.axisAngle(Vector3.up(), angle)
val rotationAxis = gesture?.targetNode?.parent?.worldPosition
val adjustedRotationAxis = Vector3(rotationAxis!!.x, rotationAxis.z, rotationAxis.y)

gesture.targetNode?.parent?.localRotation = Quaternion.multiply(
gesture.targetNode?.parent?.localRotation,
Quaternion.axisAngle(rotationAxis, angle)
)
}

override fun onFinished(gesture: DragGesture?) {}
})
}
transformationSystem.addGestureRecognizer(dragGestureRecognizer)
transformationSystem.apply {
addGestureRecognizer(dragGestureRecognizer)
selectionVisualizer = NullVisualizer()
}
return transformationSystem
}

private fun getRotationAngle(startPosition: Vector3, endPosition: Vector3): Float {
val diff = endPosition.x - startPosition.x
return (10 * diff) / displayMetrics.widthPixels
}

class NullVisualizer : SelectionVisualizer {
override fun applySelectionVisual(node: BaseTransformableNode?) {}
override fun removeSelectionVisual(node: BaseTransformableNode?) {}
}
}

0 comments on commit 3b1da01

Please sign in to comment.