-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/plweegie/magmolecular/utils/ArAtom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.plweegie.magmolecular.utils | ||
|
||
import android.graphics.Color | ||
|
||
|
||
data class ArAtom(val xCoord: Float, | ||
val yCoord: Float, | ||
val zCoord: Float, | ||
private val element: String) { | ||
|
||
companion object { | ||
private val COLOR_MAPPINGS: HashMap<String, Int> = hashMapOf( | ||
"C" to Color.BLACK, | ||
"O" to Color.RED, | ||
"N" to Color.BLUE, | ||
"Cl" to Color.GREEN, | ||
"S" to Color.YELLOW, | ||
"P" to Color.parseColor("#FFA500"), | ||
"Br" to Color.parseColor("maroon"), | ||
"I" to Color.parseColor("fuchsia"), | ||
"F" to Color.parseColor("#BFFF00") | ||
) | ||
} | ||
|
||
val color: Int = COLOR_MAPPINGS[element] ?: Color.BLACK | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/plweegie/magmolecular/utils/ArMolecule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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 org.openscience.cdk.interfaces.IAtom | ||
|
||
|
||
class ArMolecule(atoms: List<IAtom>, private val context: Context) { | ||
|
||
val centerCoordX: Float = (atoms.sumByDouble { it.point3d.x } / atoms.size).toFloat() | ||
val centerCoordY: Float = (atoms.sumByDouble { it.point3d.y } / atoms.size).toFloat() | ||
val centerCoordZ: Float = (atoms.sumByDouble { it.point3d.z } / atoms.size).toFloat() | ||
|
||
private val arAtoms: List<ArAtom> = atoms.map { atom -> | ||
with(atom.point3d) { | ||
ArAtom(x.toFloat(), y.toFloat(), z.toFloat(), atom.symbol) | ||
} | ||
} | ||
|
||
private val materials: List<Deferred<Material>> = arAtoms.map { | ||
getMaterialAsync(it.color) | ||
} | ||
|
||
val renderableAtoms: List<Pair<ArAtom, Deferred<Material>>> = arAtoms zip materials | ||
|
||
private fun getMaterialAsync(color: Int): Deferred<Material> = | ||
MaterialFactory.makeOpaqueWithColor(context, com.google.ar.sceneform.rendering.Color(color)).asDeferred() | ||
} |