RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
3D car markers on the Mapbox map; the OpenFreeMap map follows you again when it opens
CIS-C0ecedec31ed7c
diff --git a/app/src/main/java/com/rfsentinel/app/ui/Car3d.kt b/app/src/main/java/com/rfsentinel/app/ui/Car3d.ktnew file mode 100644index 0000000..e260e05--- /dev/null+++ b/app/src/main/java/com/rfsentinel/app/ui/Car3d.kt@@ -0,0 +1,456 @@+package com.rfsentinel.app.ui++import android.graphics.Color+import org.json.JSONArray+import org.json.JSONObject+import java.nio.ByteBuffer+import java.nio.ByteOrder+import kotlin.math.PI+import kotlin.math.abs+import kotlin.math.cos+import kotlin.math.max+import kotlin.math.min+import kotlin.math.pow+import kotlin.math.roundToInt+import kotlin.math.sin+import kotlin.math.sqrt++/**+ * Your car as a 3D model (as in Google Maps), for the Mapbox map, which draws a 3D position marker itself: it turns+ * and tilts with the map and shows the car's sides, roof and glass as a real one would. The models are built here from+ * the same measurements as the flat drawings in [MarkerArt] and saved as glTF files ([modelFile]). The OpenFreeMap+ * map has no 3D models and keeps the flat drawings.+ *+ * Units: the flat drawings' 100 x 200 sheet; x to the right, y forward, z up, the car's middle on the ground at 0.+ */+internal object Car3d {++ fun supports(style: MarkerArt.Style) = when (style) {+ MarkerArt.Style.NAVIGATION, MarkerArt.Style.DOT, MarkerArt.Style.ARROW, MarkerArt.Style.CHEVRON -> false+ else -> true+ }++ // ---- the model --------------------------------------------------------------------------------------++ private class V(val x: Float, val y: Float, val z: Float)++ /** One face: its corners, colour, shine and a point inside its part (its normal points away from it). */+ private class Tri(val a: V, val b: V, val c: V, val color: Int, val shine: Float, val inside: V, val paint: ((Float, Float) -> Int)? = null)++ private class Mesh(val tris: List<Tri>)++ private val meshes = object : LinkedHashMap<String, Mesh>(16, 0.75f, true) {+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Mesh>?) = size > 6+ }++ private fun mesh(style: MarkerArt.Style, color: Int): Mesh = synchronized(meshes) {+ meshes.getOrPut("${style.key}|$color") { build(style, color) }+ }++ private class Builder {+ val tris = ArrayList<Tri>()++ fun tri(a: V, b: V, c: V, color: Int, shine: Float, inside: V, paint: ((Float, Float) -> Int)? = null) {+ tris += Tri(a, b, c, color, shine, inside, paint)+ }++ fun quad(a: V, b: V, c: V, d: V, color: Int, shine: Float, inside: V, paint: ((Float, Float) -> Int)? = null) {+ tri(a, b, c, color, shine, inside, paint); tri(a, c, d, color, shine, inside, paint)+ }++ /** A box (no floor: never seen from above). */+ fun box(x0: Float, x1: Float, y0: Float, y1: Float, z0: Float, z1: Float, color: Int, shine: Float = 0.25f,+ top: Int = color, paint: ((Float, Float) -> Int)? = null) {+ val i = V((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2)+ val a = V(x0, y0, z0); val b = V(x1, y0, z0); val c = V(x1, y1, z0); val d = V(x0, y1, z0)+ val e = V(x0, y0, z1); val f = V(x1, y0, z1); val g = V(x1, y1, z1); val h = V(x0, y1, z1)+ quad(e, f, g, h, top, shine, i, paint)+ quad(a, b, f, e, color, shine, i); quad(b, c, g, f, color, shine, i)+ quad(c, d, h, g, color, shine, i); quad(d, a, e, h, color, shine, i)+ }++ /** Rings of the same number of points, joined band by band; the last ring closed with a lid. */+ fun loft(rings: List<List<V>>, bands: List<Int>, shine: Float, lid: Int, lidPaint: ((Float, Float) -> Int)? = null) {+ val all = rings.flatten()+ val inside = V(all.map { it.x }.average().toFloat(), all.map { it.y }.average().toFloat(), all.map { it.z }.average().toFloat())+ for (r in 0 until rings.size - 1) {+ val lo = rings[r]; val hi = rings[r + 1]+ for (i in lo.indices) {+ val j = (i + 1) % lo.size+ quad(lo[i], lo[j], hi[j], hi[i], bands[r], shine, inside)+ }+ }+ val top = rings.last()+ val mid = V(top.map { it.x }.average().toFloat(), top.map { it.y }.average().toFloat(), top.map { it.z }.average().toFloat())+ val under = V(mid.x, mid.y, mid.z - 50f)+ for (i in top.indices) tri(mid, top[i], top[(i + 1) % top.size], lid, shine, under, lidPaint)+ }++ /** A wheel on the ground: a tyre with a grey rim on its outer face. */+ fun wheel(x: Float, y: Float, r: Float, width: Float, outward: Float) {+ val n = 14+ val xi = x - outward * width / 2; val xo = x + outward * width / 2+ val inside = V(x, y, r)+ val inner = (0 until n).map { k -> val a = 2 * PI * k / n; V(xi, y + r * cos(a).toFloat(), r + r * sin(a).toFloat()) }+ val outer = (0 until n).map { k -> val a = 2 * PI * k / n; V(xo, y + r * cos(a).toFloat(), r + r * sin(a).toFloat()) }+ for (k in 0 until n) {+ val j = (k + 1) % n+ quad(inner[k], inner[j], outer[j], outer[k], MarkerArt.DARK, 0.08f, inside)+ }+ val hub = V(xo, y, r)+ val face = V(x, y, r)+ for (k in 0 until n) tri(hub, outer[k], outer[(k + 1) % n], MarkerArt.DARK, 0.15f, face) { py, pz ->+ if (sqrt((py - y) * (py - y) + (pz - r) * (pz - r)) < r * 0.62f) 0xFF9AA1AA.toInt() else MarkerArt.DARK+ }+ }+ }++ /** A rounded rectangle in plan, counter-clockwise: [l]..[r] wide, [front]..[rear] long, corners [rf] / [rr]. */+ private fun outline(l: Float, r: Float, front: Float, rear: Float, rf: Float, rr: Float): List<Pair<Float, Float>> {+ val pts = ArrayList<Pair<Float, Float>>()+ fun arc(cx: Float, cy: Float, rad: Float, from: Double) {+ val segs = 5+ for (k in 0..segs) {+ val a = Math.toRadians(from + 90.0 * k / segs)+ pts += (cx + rad * cos(a).toFloat()) to (cy + rad * sin(a).toFloat())+ }+ }+ arc(r - rf, front - rf, rf, 0.0)+ arc(l + rf, front - rf, rf, 90.0)+ arc(l + rr, rear + rr, rr, 180.0)+ arc(r - rr, rear + rr, rr, 270.0)+ return pts+ }++ /** The front (or rear) edge of that outline at [x]. */+ private fun edgeY(x: Float, half: Float, end: Float, rad: Float, front: Boolean): Float {+ val dx = abs(x) - (half - rad)+ val dy = if (dx <= 0f) 0f else rad - sqrt(max(0f, rad * rad - dx * dx))+ return if (front) end - dy else end + dy+ }++ private class Heights(val body: Float, val cabin: Float)++ private fun heights(style: MarkerArt.Style, w: Float) = when (style) {+ MarkerArt.Style.HATCHBACK -> Heights(0.42f * w, 0.36f * w)+ MarkerArt.Style.WAGON -> Heights(0.42f * w, 0.34f * w)+ MarkerArt.Style.SUV -> Heights(0.50f * w, 0.36f * w)+ MarkerArt.Style.PICKUP -> Heights(0.50f * w, 0.34f * w)+ MarkerArt.Style.SPORTS, MarkerArt.Style.RACER -> Heights(0.32f * w, 0.26f * w)+ MarkerArt.Style.CONVERTIBLE -> Heights(0.38f * w, 0.30f * w)+ MarkerArt.Style.MINI -> Heights(0.44f * w, 0.38f * w)+ MarkerArt.Style.VAN -> Heights(0.52f * w, 0.50f * w)+ else -> Heights(0.40f * w, 0.33f * w)+ }++ private fun build(style: MarkerArt.Style, color: Int): Mesh {+ val b = Builder()+ when (style) {+ MarkerArt.Style.TRUCK -> truck(b, color)+ MarkerArt.Style.MOTORCYCLE -> motorcycle(b, color)+ else -> car(b, style, MarkerArt.specOf(style) ?: MarkerArt.SEDAN, color)+ }+ return Mesh(b.tris)+ }++ private const val CLEAR = 6f++ /** A car body with its cabin, wheels, lights and extras, from its flat drawing's measurements. */+ private fun car(b: Builder, style: MarkerArt.Style, k: MarkerArt.Car, color: Int, heightsOverride: Heights? = null) {+ val hs = heightsOverride ?: heights(style, k.w)+ val half = k.w / 2+ // Sheet (y down, 0..200) to model (y forward, middle at 0).+ fun my(sheetY: Float) = 100f - sheetY+ val front = my(k.top); val rear = my(k.bottom)+ val yHood = my(k.hoodEnd); val yWs = my(k.wsEnd); val yRoof = my(k.roofEnd)+ val yRearGlass = if (k.rearEnd > k.roofEnd + 2f) my(k.rearEnd) else yRoof+ val deckTop = CLEAR + hs.body+ // The hood slopes down to the nose, the boot a little to the tail.+ fun deck(y: Float): Float {+ val hood = if (y > yHood) ((y - yHood) / max(1f, front - yHood)).coerceIn(0f, 1f) else 0f+ val boot = if (y < yRearGlass) ((yRearGlass - y) / max(1f, yRearGlass - rear)).coerceIn(0f, 1f) else 0f+ return deckTop - hs.body * (0.22f * hood + 0.10f * boot)+ }+ fun ring(inset: Float, frac: Float): List<V> =+ outline(-half + inset, half - inset, front - inset, rear + inset, max(0f, k.rf - inset), max(0f, k.rr - inset))+ .map { (x, y) -> V(x, y, CLEAR + frac * (deck(y) - CLEAR)) }+ val stripes = MarkerArt.Extra.STRIPES in k.extras+ val bodyPaint: ((Float, Float) -> Int)? = if (stripes) { x, _ -> if (abs(x) in 4f..11f) 0xFFF2F2F2.toInt() else color } else null+ val lower = MarkerArt.shade(color, 0.82f)+ b.loft(listOf(ring(3f, 0f), ring(0f, 0.55f), ring(1.2f, 0.86f), ring(5f, 1f)),+ listOf(lower, color, color), 0.4f, color, bodyPaint)++ // Wheels, a little out of the body.+ val wheelR = min(13f, CLEAR + hs.body * 0.5f)+ for (wy in k.wheelsY) for (side in listOf(-1f, 1f)) b.wheel(side * (half - 3f), my(wy), wheelR, 11f, side)++ // The cabin: windshield, side windows, rear window and the roof.+ val roofZ = deck((yWs + yRoof) / 2) + hs.cabin+ val glassBase = k.w * 0.42f; val glassRear = k.w * 0.40f; val roofHalf = k.w * 0.33f+ if (MarkerArt.Extra.OPEN_TOP in k.extras) {+ val z0 = deck(yHood)+ b.quad(V(-glassBase, yHood, z0), V(glassBase, yHood, z0), V(roofHalf, yWs, z0 + hs.cabin * 0.7f), V(-roofHalf, yWs, z0 + hs.cabin * 0.7f),+ MarkerArt.GLASS, 0.7f, V(0f, yHood - 10f, z0))+ val cockpit = 0xFF262B32.toInt()+ b.box(-k.w * 0.37f, k.w * 0.37f, yRoof, yWs, deck(yWs) - 0.5f, deck(yWs) + 0.3f, cockpit, 0.05f)+ val seat = 0xFF55616E.toInt()+ b.box(-k.w * 0.31f, -3f, yWs - 34f, yWs - 10f, deck(yWs), deck(yWs) + hs.cabin * 0.45f, seat, 0.1f)+ b.box(3f, k.w * 0.31f, yWs - 34f, yWs - 10f, deck(yWs), deck(yWs) + hs.cabin * 0.45f, seat, 0.1f)+ } else {+ val long = MarkerArt.Extra.LONG_ROOF in k.extras+ val roofColor = MarkerArt.lighten(color, 0.08f)+ val ribs = MarkerArt.Extra.RIBS in k.extras+ val roofPaint: ((Float, Float) -> Int)? = when {+ stripes -> { x, _ -> if (abs(x) in 4f..11f) 0xFFF2F2F2.toInt() else roofColor }+ ribs -> { _, y -> if (((yWs - y) % 18f) in 12f..14f) MarkerArt.shade(color, 0.7f) else roofColor }+ else -> null+ }+ val base = listOf(V(-glassBase, yHood, deck(yHood)), V(glassBase, yHood, deck(yHood)),+ V(glassRear, yRearGlass, deck(yRearGlass)), V(-glassRear, yRearGlass, deck(yRearGlass)))+ val sill = listOf(V(-glassBase, yHood + (yWs - yHood) * 0.06f, deck(yHood) + 1.5f), V(glassBase, yHood + (yWs - yHood) * 0.06f, deck(yHood) + 1.5f),+ V(glassRear, yRearGlass, deck(yRearGlass) + 1.5f), V(-glassRear, yRearGlass, deck(yRearGlass) + 1.5f))+ val r = if (long) roofHalf * 1.05f else roofHalf+ val top = listOf(V(-r, yWs, roofZ), V(r, yWs, roofZ), V(r, yRoof, roofZ), V(-r, yRoof, roofZ))+ // The lowest strip in the body's colour, then glass all round, then the roof.+ b.loft(listOf(base, sill, top), listOf(color, MarkerArt.GLASS), 0.65f, roofColor, roofPaint)+ if (MarkerArt.Extra.RAILS in k.extras) for (side in listOf(-1f, 1f)) {+ val x = side * r * 0.72f+ b.box(x - 1.3f, x + 1.3f, yRoof + 4f, yWs - 4f, roofZ, roofZ + 2.6f, 0xFF30363D.toInt(), 0.2f)+ }+ if (MarkerArt.Extra.ROOF_SIGN in k.extras) {+ val m = (yWs + yRoof) / 2+ b.box(-10f, 10f, m - 6f, m + 6f, roofZ, roofZ + 7f, 0xFFFFE082.toInt(), 0.3f)+ }+ }+ if (MarkerArt.Extra.BED in k.extras) {+ val z = deck(rear + 10f)+ b.box(-half + 5f, half - 5f, rear + 5f, yRearGlass - 2f, z - 0.4f, z + 0.3f, MarkerArt.shade(color, 0.38f), 0.05f)+ }+ if (MarkerArt.Extra.SPOILER in k.extras) {+ val y0 = rear + 9f; val y1 = rear + 16f; val z = deck(rear + 12f)+ for (side in listOf(-1f, 1f)) b.box(side * half * 0.6f - 1.5f, side * half * 0.6f + 1.5f, y0 + 2f, y1 - 2f, z, z + 6f, 0xFF1A1D21.toInt(), 0.2f)+ b.box(-half + 1f, half - 1f, y0, y1, z + 6f, z + 9f, 0xFF1A1D21.toInt(), 0.3f)+ }+ // Mirrors.+ for (side in listOf(-1f, 1f)) {+ val x = side * half+ b.box(min(x, x + side * 5f), max(x, x + side * 5f), yHood - 4f, yHood + 2f, deck(yHood) + 1f, deck(yHood) + 5f, MarkerArt.shade(color, 0.72f), 0.3f)+ }+ // Lights on the nose and the tail, following their curve.+ for (side in listOf(-1f, 1f)) {+ val hx = side * (half - 11f)+ val fy = edgeY(hx, half, front, k.rf, true)+ val zf = CLEAR + 0.62f * (deck(front) - CLEAR)+ b.box(hx - 6f, hx + 6f, fy - 4f, fy + 0.6f, zf, zf + 4f, MarkerArt.HEADLIGHT, 0.5f)+ val ty = edgeY(hx, half, rear, k.rr, false)+ val zr = CLEAR + 0.66f * (deck(rear) - CLEAR)+ b.box(hx - 5.5f, hx + 5.5f, ty - 0.6f, ty + 4f, zr, zr + 4f, MarkerArt.TAILLIGHT, 0.4f)+ }+ }++ private fun truck(b: Builder, color: Int) {+ // The cab: a short, tall car.+ car(b, MarkerArt.Style.TRUCK, MarkerArt.Car(62f, 12f, 72f, 18f, 4f, 36f, 50f, 70f, 70f, listOf(34f)), color, Heights(36f, 24f))+ // The box behind it, pale, with ribs across its roof.+ val boxColor = MarkerArt.lighten(color, 0.82f)+ val rib = MarkerArt.shade(color, 0.9f)+ b.box(-37f, 37f, 100f - 192f, 100f - 78f, CLEAR + 2f, 78f, MarkerArt.shade(boxColor, 0.92f), 0.2f, boxColor) { _, y ->+ if (((100f - 94f - y) % 18f).let { it < 0f || it > 16f }) rib else boxColor+ }+ b.box(-14f, 14f, 100f - 82f, 100f - 68f, CLEAR, CLEAR + 8f, MarkerArt.DARK, 0.1f)+ for (wy in listOf(134f, 168f)) for (side in listOf(-1f, 1f)) b.wheel(side * 34f, 100f - wy, 13f, 12f, side)+ for (side in listOf(-1f, 1f)) b.box(side * 30f - 5.5f, side * 30f + 5.5f, 100f - 192f - 0.6f, 100f - 188f, 14f, 18f, MarkerArt.TAILLIGHT, 0.4f)+ }++ private fun motorcycle(b: Builder, color: Int) {+ b.wheel(0f, 100f - 38f, 15f, 7f, 1f)+ b.wheel(0f, 100f - 164f, 16f, 8f, 1f)+ // Tank and engine, the seat, then the rider leaning to the bars.+ b.loft(listOf(+ outline(-12f, 12f, 54f, -50f, 11f, 11f).map { (x, y) -> V(x, y, 14f) },+ outline(-14f, 14f, 54f, -50f, 13f, 13f).map { (x, y) -> V(x, y, 30f) },+ outline(-11f, 11f, 50f, -46f, 10f, 10f).map { (x, y) -> V(x, y, 40f) }),+ listOf(0xFF3A3F47.toInt(), color), 0.45f, color)+ b.box(-9f, 9f, -46f, -4f, 38f, 45f, 0xFF2A2E34.toInt(), 0.1f)+ val suit = 0xFF3A3F47.toInt()+ b.box(-13f, 13f, -28f, -6f, 45f, 76f, suit, 0.15f)+ for (side in listOf(-1f, 1f)) b.box(min(side * 10f, side * 26f), max(side * 10f, side * 26f), -8f, 42f, 62f, 67f, suit, 0.15f)+ b.box(-28f, 28f, 40f, 44f, 54f, 58f, MarkerArt.DARK, 0.3f)+ // The helmet: a low ball in a lighter shade.+ val helmet = MarkerArt.lighten(color, 0.45f)+ val c = V(0f, -14f, 86f); val r = 11f+ val lat = 5; val lon = 10+ fun p(i: Int, j: Int): V {+ val t = PI * i / lat; val f = 2 * PI * j / lon+ return V(c.x + r * (sin(t) * cos(f)).toFloat(), c.y + r * (sin(t) * sin(f)).toFloat(), c.z + r * cos(t).toFloat())+ }+ for (i in 0 until lat) for (j in 0 until lon) {+ val a = p(i, j); val bb = p(i, j + 1); val cc = p(i + 1, j + 1); val d = p(i + 1, j)+ b.quad(a, bb, cc, d, helmet, 0.6f, c)+ }+ b.box(-5f, 5f, 100f - 16f, 100f - 12f, 26f, 32f, MarkerArt.HEADLIGHT, 0.5f)+ b.box(-5f, 5f, 100f - 190f, 100f - 186f, 28f, 32f, MarkerArt.TAILLIGHT, 0.4f)+ }++ // ---- glTF ------------------------------------------------------------------------------------------++ /** Metres per model unit: a sedan (172 units) is 4.3 m long. */+ private const val METRE = 1f / 40f+ /** Painted faces are cut into smaller triangles where their colour changes, this many times at most. */+ private const val PAINT_DEPTH = 4++ /**+ * The car as a glTF 2.0 binary (.glb), for a map that draws 3D models itself (Mapbox's 3D position marker):+ * flat-shaded triangles with their colours, in metres, y up and the front towards +z (glTF's own convention).+ * Glass, paint and the matte parts (tyres, trim) are separate materials, so the map's light plays on them+ * differently.+ */+ fun glb(style: MarkerArt.Style, color: Int): ByteArray {+ val mesh = mesh(style, color)+ // By material: matte, paint, glass.+ val groups = Array(3) { Prim() }+ for (t in mesh.tris) {+ val g = groups[if (t.shine >= 0.6f) 2 else if (t.shine >= 0.3f) 1 else 0]+ val n = normal(t) ?: continue+ if (t.paint == null) g.add(t.a, t.b, t.c, n, t.color) else paintInto(g, t, n, t.a, t.b, t.c, 0)+ }+ val materials = listOf(Material(0.85f, 0.0f), Material(0.35f, 0.25f), Material(0.12f, 0.0f))+ val used = groups.indices.filter { groups[it].count > 0 }+ return GlbWriter.write(used.map { groups[it] }, used.map { materials[it] })+ }++ /** The face's normal, away from the inside of its part (null for a degenerate face). */+ private fun normal(t: Tri): FloatArray? {+ val ux = t.b.x - t.a.x; val uy = t.b.y - t.a.y; val uz = t.b.z - t.a.z+ val vx = t.c.x - t.a.x; val vy = t.c.y - t.a.y; val vz = t.c.z - t.a.z+ var nx = uy * vz - uz * vy; var ny = uz * vx - ux * vz; var nz = ux * vy - uy * vx+ val len = sqrt(nx * nx + ny * ny + nz * nz)+ if (len < 1e-6f) return null+ val cx = (t.a.x + t.b.x + t.c.x) / 3 - t.inside.x+ val cy = (t.a.y + t.b.y + t.c.y) / 3 - t.inside.y+ val cz = (t.a.z + t.b.z + t.c.z) / 3 - t.inside.z+ if (nx * cx + ny * cy + nz * cz < 0) { nx = -nx; ny = -ny; nz = -nz }+ return floatArrayOf(nx / len, ny / len, nz / len)+ }++ /** A painted face (stripes, rims, ribs): cut into four while its corners and middle differ in colour. */+ private fun paintInto(g: Prim, t: Tri, n: FloatArray, a: V, b: V, c: V, depth: Int) {+ val paint = t.paint ?: return+ val wheel = abs(t.a.x - t.b.x) < 0.01f && abs(t.a.x - t.c.x) < 0.01f+ fun at(v: V) = if (wheel) paint(v.y, v.z) else paint(v.x, v.y)+ val m = at(V((a.x + b.x + c.x) / 3, (a.y + b.y + c.y) / 3, (a.z + b.z + c.z) / 3))+ if (depth >= PAINT_DEPTH || (at(a) == m && at(b) == m && at(c) == m)) { g.add(a, b, c, n, m); return }+ val ab = V((a.x + b.x) / 2, (a.y + b.y) / 2, (a.z + b.z) / 2)+ val bc = V((b.x + c.x) / 2, (b.y + c.y) / 2, (b.z + c.z) / 2)+ val ca = V((c.x + a.x) / 2, (c.y + a.y) / 2, (c.z + a.z) / 2)+ paintInto(g, t, n, a, ab, ca, depth + 1); paintInto(g, t, n, ab, b, bc, depth + 1)+ paintInto(g, t, n, ca, bc, c, depth + 1); paintInto(g, t, n, ab, bc, ca, depth + 1)+ }++ private class Material(val roughness: Float, val metallic: Float)++ /** One material's triangles, ready for glTF: positions and normals in glTF's axes, colours in linear light. */+ private class Prim {+ val pos = ArrayList<Float>()+ val nrm = ArrayList<Float>()+ val col = ArrayList<Byte>()+ val count get() = pos.size / 3++ fun add(a: V, b: V, c: V, n: FloatArray, color: Int) {+ for (v in listOf(a, b, c)) {+ // Model x right, y forward, z up -> glTF x to the car's left, y up, z forward.+ pos += -v.x * METRE; pos += v.z * METRE; pos += v.y * METRE+ nrm += -n[0]; nrm += n[2]; nrm += n[1]+ for (ch in listOf(Color.red(color), Color.green(color), Color.blue(color))) col += linear(ch).toByte()+ col += 255.toByte()+ }+ }++ private fun linear(c: Int) = (255.0 * (c / 255.0).pow(2.2)).roundToInt().coerceIn(0, 255)+ }++ /** The glTF binary container: a JSON chunk describing the triangles, then their data. */+ private object GlbWriter {+ fun write(prims: List<Prim>, materials: List<Material>): ByteArray {+ val bin = java.io.ByteArrayOutputStream()+ val views = JSONArray()+ val accessors = JSONArray()+ val primitives = JSONArray()+ fun view(bytes: ByteArray): Int {+ while (bin.size() % 4 != 0) bin.write(0)+ views.put(JSONObject().put("buffer", 0).put("byteOffset", bin.size()).put("byteLength", bytes.size))+ bin.write(bytes)+ return views.length() - 1+ }+ fun floats(list: List<Float>): ByteArray {+ val bb = ByteBuffer.allocate(list.size * 4).order(ByteOrder.LITTLE_ENDIAN)+ list.forEach { bb.putFloat(it) }+ return bb.array()+ }+ prims.forEachIndexed { i, p ->+ val min = FloatArray(3) { Float.MAX_VALUE }+ val max = FloatArray(3) { -Float.MAX_VALUE }+ for (k in p.pos.indices) { min[k % 3] = minOf(min[k % 3], p.pos[k]); max[k % 3] = maxOf(max[k % 3], p.pos[k]) }+ accessors.put(JSONObject().put("bufferView", view(floats(p.pos))).put("componentType", FLOAT).put("count", p.count)+ .put("type", "VEC3").put("min", JSONArray(min.map { it.toDouble() })).put("max", JSONArray(max.map { it.toDouble() })))+ accessors.put(JSONObject().put("bufferView", view(floats(p.nrm))).put("componentType", FLOAT).put("count", p.count).put("type", "VEC3"))+ accessors.put(JSONObject().put("bufferView", view(p.col.toByteArray())).put("componentType", UNSIGNED_BYTE)+ .put("normalized", true).put("count", p.count).put("type", "VEC4"))+ // An index list too (0, 1, 2...): glTF doesn't need one, but some loaders count on it.+ val ib = ByteBuffer.allocate(p.count * 4).order(ByteOrder.LITTLE_ENDIAN)+ for (k in 0 until p.count) ib.putInt(k)+ accessors.put(JSONObject().put("bufferView", view(ib.array())).put("componentType", UNSIGNED_INT).put("count", p.count).put("type", "SCALAR"))+ primitives.put(JSONObject()+ .put("attributes", JSONObject().put("POSITION", i * 4).put("NORMAL", i * 4 + 1).put("COLOR_0", i * 4 + 2))+ .put("indices", i * 4 + 3)+ .put("material", i).put("mode", TRIANGLES))+ }+ while (bin.size() % 4 != 0) bin.write(0)+ val json = JSONObject()+ .put("asset", JSONObject().put("version", "2.0").put("generator", "RF Sentinel"))+ .put("scene", 0)+ .put("scenes", JSONArray().put(JSONObject().put("nodes", JSONArray().put(0))))+ .put("nodes", JSONArray().put(JSONObject().put("mesh", 0)))+ .put("meshes", JSONArray().put(JSONObject().put("primitives", primitives)))+ .put("materials", JSONArray(materials.map { m ->+ JSONObject().put("doubleSided", true).put("pbrMetallicRoughness", JSONObject()+ .put("baseColorFactor", JSONArray(listOf(1.0, 1.0, 1.0, 1.0)))+ .put("metallicFactor", m.metallic.toDouble()).put("roughnessFactor", m.roughness.toDouble()))+ }))+ .put("buffers", JSONArray().put(JSONObject().put("byteLength", bin.size())))+ .put("bufferViews", views)+ .put("accessors", accessors)+ var text = json.toString().toByteArray(Charsets.UTF_8)+ // Chunks are padded to 4 bytes: the JSON with spaces, the data with zeros.+ if (text.size % 4 != 0) text += ByteArray(4 - text.size % 4) { 0x20 }+ val data = bin.toByteArray()+ val out = ByteBuffer.allocate(12 + 8 + text.size + 8 + data.size).order(ByteOrder.LITTLE_ENDIAN)+ out.putInt(MAGIC).putInt(2).putInt(out.capacity())+ out.putInt(text.size).putInt(CHUNK_JSON).put(text)+ out.putInt(data.size).putInt(CHUNK_BIN).put(data)+ return out.array()+ }++ private const val MAGIC = 0x46546C67 // "glTF"+ private const val CHUNK_JSON = 0x4E4F534A+ private const val CHUNK_BIN = 0x004E4942+ private const val FLOAT = 5126+ private const val UNSIGNED_BYTE = 5121+ private const val UNSIGNED_INT = 5125+ private const val TRIANGLES = 4+ }++ /** The model file for this car, written once into the app's own files (the map loads it by its path). */+ fun modelFile(context: android.content.Context, style: MarkerArt.Style, color: Int): java.io.File {+ val dir = java.io.File(context.filesDir, "cars3d").apply { mkdirs() }+ val f = java.io.File(dir, "v2-${style.key}-${Integer.toHexString(color)}.glb")+ if (!f.exists() || f.length() == 0L) {+ val tmp = java.io.File(dir, f.name + ".tmp")+ tmp.writeBytes(glb(style, color))+ tmp.renameTo(f)+ // Only the car in use is kept (a few hundred KB each; older versions and other colours go).+ dir.listFiles()?.forEach { if (it != f) it.delete() }+ }+ return f+ }+}diff --git a/app/src/main/java/com/rfsentinel/app/ui/MapEngine.kt b/app/src/main/java/com/rfsentinel/app/ui/MapEngine.ktindex 9cb01d9..823dfe9 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapEngine.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapEngine.kt@@ -99,6 +99,10 @@ internal interface MapEngine {/** Shows your arrow (once the style is in); false without location permission. *//** The picture of your position, turned with your heading (null: the engine's own arrow); applied now or when the puck starts. */fun setMarker(bitmap: Bitmap?)+ /** True when the map can show your position as a 3D model ([setMarker3d]): Mapbox. */+ val draws3dMarker: Boolean+ /** Your position as this 3D model (a .glb file), [size] times the standard size; null: the picture of [setMarker]. */+ fun setMarker3d(model: java.io.File?, size: Float)fun startPuck(): Booleanval puckOn: Booleanfun pushFix(fix: Location)diff --git a/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt b/app/src/main/java/com/rfsentinel/app/ui/MapFragment.ktindex 4df2e8c..27b530b 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt@@ -864,10 +864,16 @@ class MapFragment : Fragment() {/** The marker chosen in Settings > Map: given to the map engine whenever the choice has changed. */private fun applyMarker() {val m = map ?: return- val key = MarkerPicker.effective(act).key+ val choice = MarkerPicker.effective(act)+ val key = choice.keyif (key == markerKey) returnmarkerKey = key+ val style = MarkerArt.Style.of(choice.style)+ // A car on a map that draws 3D models (Mapbox) is a 3D model; everything else the flat picture.+ val model = if (m.draws3dMarker && Car3d.supports(style))+ runCatching { Car3d.modelFile(act, style, MarkerArt.colorOf(choice.color).argb) }.getOrNull() else nullm.setMarker(MarkerPicker.current(act))+ m.setMarker3d(model, choice.size)}private fun startLocation() {diff --git a/app/src/main/java/com/rfsentinel/app/ui/MapLibreEngine.kt b/app/src/main/java/com/rfsentinel/app/ui/MapLibreEngine.ktindex 107c7d1..44b4e18 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapLibreEngine.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapLibreEngine.kt@@ -339,6 +339,10 @@ internal class MapLibreEngine(private val context: Context) : MapEngine {marker?.let { b -> markerName?.let { style.addImage(it, b); style.addImage(BLANK, Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)) } }}+ // No 3D models on MapLibre: the flat picture.+ override val draws3dMarker = false+ override fun setMarker3d(model: java.io.File?, size: Float) {}+override fun setMarker(bitmap: Bitmap?) {marker = bitmapmarkerName = bitmap?.let { "rf-puck-${++markerVersion}" }@@ -389,7 +393,9 @@ internal class MapLibreEngine(private val context: Context) : MapEngine {zoom?.let { lc.zoomWhileTracking(it, duration) }}look(ms)- lc.setCameraMode(if (mode == MapEngine.Follow.HEADING) CameraMode.TRACKING_GPS else CameraMode.TRACKING, ms, null, null, null, null)+ // Never 0 ms: the component only stops "transitioning" when an animated move ends, and a 0 ms move+ // is not one, so it would never follow again (not even after the follow button: same mode, ignored).+ lc.setCameraMode(if (mode == MapEngine.Follow.HEADING) CameraMode.TRACKING_GPS else CameraMode.TRACKING, ms.coerceAtLeast(1), null, null, null, null)// Also after an instant switch (the map opening already in 3D): the angle set above was dropped.Runnable { if (component() != null) look(if (ms > 0) 400 else 0) }.also { reapply = it; view.postDelayed(it, ms + 250) }val m = mapdiff --git a/app/src/main/java/com/rfsentinel/app/ui/MapboxEngine.kt b/app/src/main/java/com/rfsentinel/app/ui/MapboxEngine.ktindex f04b49c..7220f3d 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapboxEngine.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapboxEngine.kt@@ -62,6 +62,13 @@ import java.util.concurrent.CopyOnWriteArraySet*/internal class MapboxEngine(private val context: Context) : MapEngine {+ private companion object {+ /** The car models' size on the map at the standard marker size (they keep it at every zoom). */+ const val MODEL_SCALE = 10f+ /** The models' front (glTF's +z) turned to the puck's forward. */+ val MODEL_ROTATION = listOf(0f, 0f, 180f)+ }+override val provider = MapEngine.Provider.MAPBOXoverride val credit = "© Mapbox © OpenStreetMap contributors"override val labelFont = listOf("Open Sans Regular", "Arial Unicode MS Regular")@@ -271,7 +278,27 @@ internal class MapboxEngine(private val context: Context) : MapEngine {private var marker: Bitmap? = null- private fun puck(): com.mapbox.maps.plugin.LocationPuck = marker?.let { b ->+ private var model: java.io.File? = null+ private var modelSize = 1f++ override val draws3dMarker = true++ override fun setMarker3d(model: java.io.File?, size: Float) {+ this.model = model; modelSize = size+ if (puck && !destroyed) view.location.locationPuck = puck()+ }++ private fun puck(): com.mapbox.maps.plugin.LocationPuck = model?.let { f ->+ // Mapbox draws the model itself, lit by the map's light, turned with your course and seen from the map's angle.+ val s = MODEL_SCALE * modelSize+ com.mapbox.maps.plugin.LocationPuck3D(+ modelUri = "file://" + f.absolutePath,+ modelScale = listOf(s, s, s),+ modelRotation = MODEL_ROTATION,+ modelCastShadows = true,+ modelReceiveShadows = false,+ modelEmissiveStrength = 0.3f)+ } ?: marker?.let { b ->val image = com.mapbox.maps.ImageHolder.from(b)// The picture is the bearing layer, which turns with your course; no fixed top picture (it would stay// north up behind it), no shadow, no zoom scaling.diff --git a/app/src/main/java/com/rfsentinel/app/ui/MarkerArt.kt b/app/src/main/java/com/rfsentinel/app/ui/MarkerArt.ktindex a1a5b88..4f9184e 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MarkerArt.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MarkerArt.kt@@ -87,17 +87,17 @@ object MarkerArt {// ---- colours ---------------------------------------------------------------------------------------- private fun shade(c: Int, f: Float) = Color.argb(Color.alpha(c),+ internal fun shade(c: Int, f: Float) = Color.argb(Color.alpha(c),(Color.red(c) * f).toInt().coerceIn(0, 255), (Color.green(c) * f).toInt().coerceIn(0, 255), (Color.blue(c) * f).toInt().coerceIn(0, 255))- private fun lighten(c: Int, t: Float) = Color.argb(Color.alpha(c),+ internal fun lighten(c: Int, t: Float) = Color.argb(Color.alpha(c),(Color.red(c) + (255 - Color.red(c)) * t).toInt(), (Color.green(c) + (255 - Color.green(c)) * t).toInt(), (Color.blue(c) + (255 - Color.blue(c)) * t).toInt())- private const val GLASS = 0xFF1E2B38.toInt()+ internal const val GLASS = 0xFF1E2B38.toInt()private const val GLASS_HI = 0xFF3E5C78.toInt()- private const val DARK = 0xFF14171B.toInt()- private const val HEADLIGHT = 0xFFFFF3B0.toInt()- private const val TAILLIGHT = 0xFFD32F2F.toInt()+ internal const val DARK = 0xFF14171B.toInt()+ internal const val HEADLIGHT = 0xFFFFF3B0.toInt()+ internal const val TAILLIGHT = 0xFFD32F2F.toInt()private const val CX = 50fprivate fun fill(color: Int) = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL; this.color = color }@@ -187,27 +187,35 @@ object MarkerArt {// ---- cars --------------------------------------------------------------------------------------------- private enum class Extra { RAILS, BED, SPOILER, STRIPES, ROOF_SIGN, OPEN_TOP, RIBS, LONG_ROOF }+ internal enum class Extra { RAILS, BED, SPOILER, STRIPES, ROOF_SIGN, OPEN_TOP, RIBS, LONG_ROOF }/** A car from above, in 100 x 200 units: its width, front and rear, how round those are, and where the glass runs. */- private class Car(+ internal class Car(val w: Float, val top: Float, val bottom: Float, val rf: Float, val rr: Float,/** Where the windshield starts, ends (the roof starts), the roof ends (the rear window starts) and the rear window ends. */val hoodEnd: Float, val wsEnd: Float, val roofEnd: Float, val rearEnd: Float,val wheelsY: List<Float>, val extras: Set<Extra> = emptySet())- private val SEDAN = Car(64f, 14f, 186f, 20f, 14f, 62f, 86f, 112f, 134f, listOf(42f, 152f))- private val HATCHBACK = Car(62f, 20f, 180f, 20f, 12f, 62f, 86f, 126f, 144f, listOf(44f, 148f))- private val WAGON = Car(62f, 12f, 188f, 18f, 8f, 58f, 82f, 146f, 162f, listOf(40f, 158f))- private val SUV = Car(72f, 14f, 186f, 14f, 10f, 58f, 84f, 146f, 160f, listOf(40f, 158f), setOf(Extra.RAILS))- private val PICKUP = Car(70f, 10f, 190f, 14f, 6f, 56f, 78f, 100f, 110f, listOf(38f, 156f), setOf(Extra.BED))- private val SPORTS = Car(70f, 8f, 184f, 26f, 16f, 78f, 98f, 118f, 134f, listOf(34f, 150f), setOf(Extra.SPOILER))- private val RACER = Car(70f, 8f, 184f, 26f, 16f, 78f, 98f, 118f, 134f, listOf(34f, 150f), setOf(Extra.SPOILER, Extra.STRIPES))- private val CONVERTIBLE = Car(66f, 14f, 184f, 22f, 14f, 70f, 84f, 128f, 128f, listOf(40f, 152f), setOf(Extra.OPEN_TOP))- private val MINI = Car(60f, 40f, 168f, 22f, 16f, 72f, 90f, 118f, 134f, listOf(62f, 146f))- private val VAN = Car(66f, 16f, 188f, 12f, 8f, 44f, 70f, 176f, 186f, listOf(38f, 162f), setOf(Extra.RIBS, Extra.LONG_ROOF))- private val TAXI = Car(64f, 14f, 186f, 20f, 14f, 62f, 86f, 112f, 134f, listOf(42f, 152f), setOf(Extra.ROOF_SIGN))+ /** The measurements of a car style (null: not a car, or the truck / motorcycle, drawn their own way). */+ internal fun specOf(style: Style): Car? = when (style) {+ Style.SEDAN -> SEDAN; Style.HATCHBACK -> HATCHBACK; Style.WAGON -> WAGON; Style.SUV -> SUV; Style.PICKUP -> PICKUP+ Style.SPORTS -> SPORTS; Style.RACER -> RACER; Style.CONVERTIBLE -> CONVERTIBLE; Style.MINI -> MINI; Style.VAN -> VAN+ Style.TAXI -> TAXI+ else -> null+ }++ internal val SEDAN = Car(64f, 14f, 186f, 20f, 14f, 62f, 86f, 112f, 134f, listOf(42f, 152f))+ internal val HATCHBACK = Car(62f, 20f, 180f, 20f, 12f, 62f, 86f, 126f, 144f, listOf(44f, 148f))+ internal val WAGON = Car(62f, 12f, 188f, 18f, 8f, 58f, 82f, 146f, 162f, listOf(40f, 158f))+ internal val SUV = Car(72f, 14f, 186f, 14f, 10f, 58f, 84f, 146f, 160f, listOf(40f, 158f), setOf(Extra.RAILS))+ internal val PICKUP = Car(70f, 10f, 190f, 14f, 6f, 56f, 78f, 100f, 110f, listOf(38f, 156f), setOf(Extra.BED))+ internal val SPORTS = Car(70f, 8f, 184f, 26f, 16f, 78f, 98f, 118f, 134f, listOf(34f, 150f), setOf(Extra.SPOILER))+ internal val RACER = Car(70f, 8f, 184f, 26f, 16f, 78f, 98f, 118f, 134f, listOf(34f, 150f), setOf(Extra.SPOILER, Extra.STRIPES))+ internal val CONVERTIBLE = Car(66f, 14f, 184f, 22f, 14f, 70f, 84f, 128f, 128f, listOf(40f, 152f), setOf(Extra.OPEN_TOP))+ internal val MINI = Car(60f, 40f, 168f, 22f, 16f, 72f, 90f, 118f, 134f, listOf(62f, 146f))+ internal val VAN = Car(66f, 16f, 188f, 12f, 8f, 44f, 70f, 176f, 186f, listOf(38f, 162f), setOf(Extra.RIBS, Extra.LONG_ROOF))+ internal val TAXI = Car(64f, 14f, 186f, 20f, 14f, 62f, 86f, 112f, 134f, listOf(42f, 152f), setOf(Extra.ROOF_SIGN))private fun car(c: Canvas, color: Int, k: Car) {val l = CX - k.w / 2; val r = CX + k.w / 2diff --git a/app/src/test/java/com/rfsentinel/app/ui/Car3dTest.kt b/app/src/test/java/com/rfsentinel/app/ui/Car3dTest.ktnew file mode 100644index 0000000..24836ee--- /dev/null+++ b/app/src/test/java/com/rfsentinel/app/ui/Car3dTest.kt@@ -0,0 +1,113 @@+package com.rfsentinel.app.ui++import org.json.JSONObject+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import java.nio.ByteBuffer+import java.nio.ByteOrder++/** The 3D car models for Mapbox: valid glTF binaries, car-sized, standing on the ground, front towards +z. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [35], application = android.app.Application::class)+class Car3dTest {++ private val cars = MarkerArt.Style.entries.filter { Car3d.supports(it) }++ private class Glb(val json: JSONObject, val bin: ByteBuffer)++ private fun parse(bytes: ByteArray): Glb {+ val b = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)+ assertEquals("glTF magic", 0x46546C67, b.getInt(0))+ assertEquals(2, b.getInt(4))+ assertEquals("length", bytes.size, b.getInt(8))+ val jsonLen = b.getInt(12)+ assertEquals("JSON chunk", 0x4E4F534A, b.getInt(16))+ assertEquals(0, jsonLen % 4)+ val json = JSONObject(String(bytes, 20, jsonLen, Charsets.UTF_8))+ val binLen = b.getInt(20 + jsonLen)+ assertEquals("BIN chunk", 0x004E4942, b.getInt(24 + jsonLen))+ val bin = ByteBuffer.wrap(bytes, 28 + jsonLen, binLen).slice().order(ByteOrder.LITTLE_ENDIAN)+ return Glb(json, bin)+ }++ /** Every position of every triangle, in glTF axes. */+ private fun positions(g: Glb): List<FloatArray> {+ val out = ArrayList<FloatArray>()+ val prims = g.json.getJSONArray("meshes").getJSONObject(0).getJSONArray("primitives")+ for (i in 0 until prims.length()) {+ val acc = g.json.getJSONArray("accessors").getJSONObject(prims.getJSONObject(i).getJSONObject("attributes").getInt("POSITION"))+ val view = g.json.getJSONArray("bufferViews").getJSONObject(acc.getInt("bufferView"))+ for (k in 0 until acc.getInt("count")) {+ val o = view.getInt("byteOffset") + k * 12+ out += floatArrayOf(g.bin.getFloat(o), g.bin.getFloat(o + 4), g.bin.getFloat(o + 8))+ }+ }+ return out+ }++ @Test+ fun theArrowsAndDotsHaveNoModel() {+ for (s in listOf(MarkerArt.Style.NAVIGATION, MarkerArt.Style.DOT, MarkerArt.Style.ARROW, MarkerArt.Style.CHEVRON)) assertFalse(Car3d.supports(s))+ assertEquals(13, cars.size)+ }++ @Test+ fun everyCarIsAValidModelOfACar() {+ for (s in cars) {+ val g = parse(Car3d.glb(s, MarkerArt.colorOf("red").argb))+ val prims = g.json.getJSONArray("meshes").getJSONObject(0).getJSONArray("primitives")+ assertTrue("${s.key}: materials for its parts", prims.length() in 2..3)+ for (i in 0 until prims.length()) {+ val attrs = prims.getJSONObject(i).getJSONObject("attributes")+ val n = g.json.getJSONArray("accessors").getJSONObject(attrs.getInt("POSITION")).getInt("count")+ assertEquals("${s.key}: whole triangles", 0, n % 3)+ for (a in listOf("NORMAL", "COLOR_0")) assertEquals(n, g.json.getJSONArray("accessors").getJSONObject(attrs.getInt(a)).getInt("count"))+ }+ val p = positions(g)+ val xs = p.map { it[0] }; val ys = p.map { it[1] }; val zs = p.map { it[2] }+ val length = zs.max() - zs.min(); val width = xs.max() - xs.min(); val height = ys.max() - ys.min()+ assertTrue("${s.key}: car length in metres ($length)", length in 3.0f..5.0f)+ assertTrue("${s.key}: longer than wide", length > width * 1.6f)+ assertTrue("${s.key}: on the ground", ys.min() in -0.01f..0.05f)+ assertTrue("${s.key}: a car's height ($height)", height in 0.8f..2.6f)+ assertTrue("${s.key}: centred", kotlin.math.abs(xs.max() + xs.min()) < 0.05f)+ }+ }++ @Test+ fun theHeadlightsAreAtThePlusZEnd() {+ val g = parse(Car3d.glb(MarkerArt.Style.SEDAN, MarkerArt.colorOf("blue").argb))+ // The pale yellow headlights' vertices: all towards +z (glTF's front).+ val prims = g.json.getJSONArray("meshes").getJSONObject(0).getJSONArray("primitives")+ var front = 0; var back = 0+ for (i in 0 until prims.length()) {+ val attrs = prims.getJSONObject(i).getJSONObject("attributes")+ val pa = g.json.getJSONArray("accessors").getJSONObject(attrs.getInt("POSITION"))+ val ca = g.json.getJSONArray("accessors").getJSONObject(attrs.getInt("COLOR_0"))+ val pv = g.json.getJSONArray("bufferViews").getJSONObject(pa.getInt("bufferView")).getInt("byteOffset")+ val cv = g.json.getJSONArray("bufferViews").getJSONObject(ca.getInt("bufferView")).getInt("byteOffset")+ for (k in 0 until pa.getInt("count")) {+ val r = g.bin.get(cv + k * 4).toInt() and 255; val gg = g.bin.get(cv + k * 4 + 1).toInt() and 255; val b = g.bin.get(cv + k * 4 + 2).toInt() and 255+ if (r > 240 && gg > 200 && b in 60..200) { if (g.bin.getFloat(pv + k * 12 + 8) > 0) front++ else back++ }+ }+ }+ assertTrue("headlights found at the front ($front)", front > 0)+ assertEquals("none at the back", 0, back)+ }++ @Test+ fun theModelFileIsWrittenOnceAndReused() {+ val ctx = androidx.test.core.app.ApplicationProvider.getApplicationContext<android.content.Context>()+ val f = Car3d.modelFile(ctx, MarkerArt.Style.SUV, MarkerArt.colorOf("green").argb)+ assertTrue(f.length() > 1000)+ val stamp = f.lastModified()+ assertEquals(f, Car3d.modelFile(ctx, MarkerArt.Style.SUV, MarkerArt.colorOf("green").argb))+ assertEquals(stamp, f.lastModified())+ assertTrue("another colour, another file", f != Car3d.modelFile(ctx, MarkerArt.Style.SUV, MarkerArt.colorOf("red").argb))+ }+}