From 3b1da012bfccdf8982c331835a8746960e0b4404 Mon Sep 17 00:00:00 2001 From: Jszymanski Date: Sun, 3 May 2020 21:36:39 +0100 Subject: [PATCH] Work in progress --- .../magmolecular/ar/MagMolActivity.kt | 30 +++++++++++++++---- .../magmolecular/ar/MagMolFragment.kt | 19 +++++++++--- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt b/app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt index b2b0aea..675d330 100644 --- a/app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt +++ b/app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt @@ -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 @@ -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() @@ -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 { val atomContainer3D = withContext(Dispatchers.Default) { modelBuilder.generate3DCoordinates(atomContainer, false) diff --git a/app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt b/app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt index 3e7f152..ab7a830 100644 --- a/app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt +++ b/app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt @@ -53,16 +53,22 @@ 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 } @@ -70,4 +76,9 @@ class MagMolFragment : ArFragment() { val diff = endPosition.x - startPosition.x return (10 * diff) / displayMetrics.widthPixels } + + class NullVisualizer : SelectionVisualizer { + override fun applySelectionVisual(node: BaseTransformableNode?) {} + override fun removeSelectionVisual(node: BaseTransformableNode?) {} + } } \ No newline at end of file