Skip to content

Commit 3dbdf51

Browse files
committed
modifications to make openrndr-math run as JavaScript
1 parent a426869 commit 3dbdf51

19 files changed

+133
-43
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.xemantic.test.openrndr.kotlinjs
2+
3+
import org.openrndr.math.Vector2
4+
5+
@JsExport
6+
fun main() {
7+
println(Vector2(2.0, 2.0) + Vector2(2.0, 2.0))
8+
}

src/main/kotlin/org/openrndr/math/Bezier.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fun roots(p: List<Double>): List<Double> {
5858
val c = p[2]
5959
val d = a - 2 * b + c
6060
if (d != 0.0) {
61-
val m1 = -Math.sqrt(b * b - a * c)
61+
val m1 = - Math.sqrt(b * b - a * c)
6262
val m2 = -a + b
6363
val v1 = -(m1 + m2) / d
6464
val v2 = -(-m1 + m2) / d

src/main/kotlin/org/openrndr/math/Equations.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.openrndr.math
22

33
private const val DISCRIMINANT_EPSILON = 1e-10
44
private const val SOLUTION_EPSILON = 1e-8
5-
private val MACHINE_EPSILON = Math.ulp(1.0)
5+
//private val MACHINE_EPSILON = math.ulp(1.0)
66
private const val EPSILON = 1e-14
77

88
// adapted from https://github.com/paperjs/paper.js/blob/develop/src/util/Numerical.js
@@ -13,9 +13,7 @@ private fun trim(acc: DoubleArray, len: Int): DoubleArray {
1313
} else if (len == 0) {
1414
DoubleArray(0)
1515
} else {
16-
val result = DoubleArray(len)
17-
System.arraycopy(acc, 0, result, 0, len)
18-
result
16+
acc.copyOf(len)
1917
}
2018
}
2119

@@ -57,6 +55,7 @@ fun solveLinear(a: Double, b: Double): DoubleArray {
5755
return trim(acc, solveLinear(a, b, acc))
5856
}
5957

58+
/*
6059
fun solveQuadratic(a: Double, b: Double, c: Double, acc: DoubleArray): Int {
6160
var a = a
6261
var b = b
@@ -100,7 +99,9 @@ fun solveQuadratic(a: Double, b: Double, c: Double): DoubleArray {
10099
val acc = DoubleArray(2)
101100
return trim(acc, solveQuadratic(a, b, c, acc))
102101
}
102+
*/
103103

104+
/*
104105
fun solveCubic(a: Double, b: Double, c: Double, d: Double, acc: DoubleArray): Int {
105106
var a = a
106107
var b = b
@@ -165,4 +166,5 @@ fun solveCubic(a: Double, b: Double, c: Double, d: Double, acc: DoubleArray): In
165166
fun solveCubic(a: Double, b: Double, c: Double, d: Double): DoubleArray {
166167
val acc = DoubleArray(3)
167168
return trim(acc, solveCubic(a, b, c, d, acc))
168-
}
169+
}
170+
*/

src/main/kotlin/org/openrndr/math/Functions.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ fun mod(a: Int, b: Int) = ((a % b) + b) % b
1010
fun mod(a: Float, b: Float) = ((a % b) + b) % b
1111
fun mod(a: Long, b: Long) = ((a % b) + b) % b
1212

13-
@JvmName("modDouble")
1413
fun Double.mod_(b: Double) = mod(this, b)
1514

16-
@JvmName("modInt")
1715
fun Int.mod_(b: Int) = mod(this, b)
1816

19-
@JvmName("modFloat")
2017
fun Float.mod_(b: Float) = mod(this, b)
2118

22-
@JvmName("modLong")
2319
fun Long.mod_(b: Long) = mod(this, b)
2420

2521
fun Vector2.mod(b: Vector2) =
@@ -55,10 +51,8 @@ fun IntVector4.mod(b: IntVector4) =
5551
fun clamp(value: Double, min: Double, max: Double) = max(min, min(max, value))
5652
fun clamp(value: Int, min: Int, max: Int) = max(min, min(max, value))
5753

58-
@JvmName("doubleClamp")
5954
fun Double.clamp(min: Double, max: Double) = clamp(this, min, max)
6055

61-
@JvmName("intClamp")
6256
fun Int.clamp(min: Int, max: Int) = clamp(this, min, max)
6357

6458
fun Vector2.clamp(min : Vector2, max : Vector2) =

src/main/kotlin/org/openrndr/math/IntVector2.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.openrndr.math
22

3-
import java.io.Serializable
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
45
import kotlin.math.sqrt
56

67
/**
78
* Integer vector 2
89
*/
910
@Suppress("unused")
10-
data class IntVector2(val x: Int, val y: Int) : Serializable {
11+
@Serializable
12+
data class IntVector2(val x: Int, val y: Int) {
1113
companion object {
1214
val ZERO = IntVector2(0, 0)
1315
val UNIT_X = IntVector2(1, 0)
@@ -17,13 +19,17 @@ data class IntVector2(val x: Int, val y: Int) : Serializable {
1719
val length get() = sqrt(1.0 * x * x + y * y)
1820
val squaredLength get() = x * x + y * y
1921
infix fun dot(right: IntVector2) = x * right.x + y * right.y
22+
@Transient
2023
val yx get() = IntVector2(y, x)
24+
@Transient
2125
val xx get() = IntVector2(x, x)
26+
@Transient
2227
val yy get() = IntVector2(y, y)
2328
operator fun plus(v: IntVector2) = IntVector2(x + v.x, y + v.y)
2429
operator fun minus(v: IntVector2) = IntVector2(x - v.x, y - v.y)
2530
operator fun times(d: Int) = IntVector2(x * d, y * d)
2631
operator fun div(d: Int) = IntVector2(x / d, y / d)
32+
@Transient
2733
val vector2 get() = Vector2(this.x.toDouble(), this.y.toDouble())
2834
}
2935

src/main/kotlin/org/openrndr/math/IntVector3.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.openrndr.math
22

3-
import java.io.Serializable
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
45
import kotlin.math.sqrt
56

67
/**
78
* Integer vector 3
89
*/
910
@Suppress("unused")
10-
data class IntVector3(val x: Int, val y: Int, val z: Int) : Serializable {
11+
@Serializable
12+
data class IntVector3(val x: Int, val y: Int, val z: Int) {
1113
companion object {
1214
val ZERO = IntVector3(0, 0, 0)
1315
val UNIT_X = IntVector3(1, 0, 0)
@@ -18,14 +20,19 @@ data class IntVector3(val x: Int, val y: Int, val z: Int) : Serializable {
1820
val length get() = sqrt(1.0 * x * x + y * y + z * z)
1921
val squaredLength get() = x * x + y * y + z * z
2022
infix fun dot(right: IntVector3) = x * right.x + y * right.y + z * right.z
23+
@Transient
2124
val xy get() = IntVector2(x, y)
25+
@Transient
2226
val yx get() = IntVector2(y, x)
27+
@Transient
2328
val xx get() = IntVector2(x, x)
29+
@Transient
2430
val yy get() = IntVector2(y, y)
2531
operator fun plus(v: IntVector3) = IntVector3(x + v.x, y + v.y, z + v.z)
2632
operator fun minus(v: IntVector3) = IntVector3(x - v.x, y - v.y, z - v.z)
2733
operator fun times(d: Int) = IntVector3(x * d, y * d, z * d)
2834
operator fun div(d: Int) = IntVector3(x / d, y / d, z / d)
35+
@Transient
2936
val vector3 get() = Vector3(this.x.toDouble(), this.y.toDouble(), this.z.toDouble())
3037
}
3138

src/main/kotlin/org/openrndr/math/IntVector4.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.openrndr.math
22

3-
import java.io.Serializable
3+
import kotlinx.serialization.Serializable
44
import kotlin.math.sqrt
55

66

77
/**
88
* Integer vector 4
99
*/
1010
@Suppress("unused")
11-
data class IntVector4(val x: Int, val y: Int, val z: Int, val w: Int) : Serializable {
11+
@Serializable
12+
data class IntVector4(val x: Int, val y: Int, val z: Int, val w: Int) {
1213
companion object {
1314
val ZERO = IntVector4(0, 0, 0, 0)
1415
val UNIT_X = IntVector4(1, 0, 0, 0)

src/main/kotlin/org/openrndr/math/Mapping.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ fun map(beforeLeft: Double, beforeRight: Double,
4141
* @param clamp constrain the result to [afterLeft, afterRight]
4242
* @return a value in the after domain
4343
*/
44-
@JvmName("doubleMap")
4544
fun Double.map(beforeLeft: Double, beforeRight: Double,
4645
afterLeft: Double, afterRight: Double,
4746
clamp: Boolean = false): Double {
@@ -94,7 +93,6 @@ fun smoothstep(edge0: Double, edge1: Double, x: Double): Double {
9493
return u * u * (3 - 2 * u)
9594
}
9695

97-
@JvmName("doubleSmoothstep")
9896
fun Double.smoothstep(edge0: Double, edge1: Double) = smoothstep(edge0, edge1, this)
9997

10098
fun Vector2.smoothstep(edge0: Vector2, edge1: Vector2): Vector2 =
@@ -148,7 +146,6 @@ fun smootherstep(edge0: Double, edge1: Double, x: Double): Double {
148146

149147
fun saturate(x: Double) = max(0.0, min(1.0, x))
150148

151-
@JvmName("doubleSaturate")
152149
fun Double.saturate() = saturate(this)
153150

154151
fun Vector2.saturate() = Vector2(x.saturate(), y.saturate())
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.openrndr.math
2+
3+
class Math {
4+
companion object {
5+
const val DEGREES_TO_RADIANS = 0.017453292519943295
6+
const val RADIANS_TO_DEGREES = 57.29577951308232
7+
fun sqrt(x: Double) = kotlin.math.sqrt(x)
8+
fun toRadians(angdeg: Double) = angdeg * DEGREES_TO_RADIANS
9+
fun toDegrees(angrad: Double) = angrad * RADIANS_TO_DEGREES
10+
fun tan(x: Double) = kotlin.math.tan(x)
11+
fun sin(x: Double) = kotlin.math.sin(x)
12+
fun cos(x: Double) = kotlin.math.cos(x)
13+
/*fun getExponent(d: Double): Int {
14+
/*
15+
* Bitwise convert d to long, mask out exponent bits, shift
16+
* to the right and then subtract out double's bias adjust to
17+
* get true exponent value.
18+
*/
19+
return ((java.lang.Double.doubleToRawLongBits(d) and jdk.internal.math.DoubleConsts.EXP_BIT_MASK shr
20+
jdk.internal.math.DoubleConsts.SIGNIFICAND_WIDTH - 1) - jdk.internal.math.DoubleConsts.EXP_BIAS)
21+
}
22+
fun ulp(d: Double): Double {
23+
var exp: Int = java.lang.Math.getExponent(d)
24+
return when (exp) {
25+
java.lang.Double.MAX_EXPONENT + 1 -> java.lang.Math.abs(d)
26+
java.lang.Double.MIN_EXPONENT - 1 -> Double.MIN_VALUE
27+
else -> {
28+
assert(exp <= java.lang.Double.MAX_EXPONENT && exp >= java.lang.Double.MIN_EXPONENT)
29+
30+
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
31+
exp = exp - (jdk.internal.math.DoubleConsts.SIGNIFICAND_WIDTH - 1)
32+
if (exp >= java.lang.Double.MIN_EXPONENT) {
33+
java.lang.Math.powerOfTwoD(exp)
34+
} else {
35+
// return a subnormal result; left shift integer
36+
// representation of Double.MIN_VALUE appropriate
37+
// number of positions
38+
java.lang.Double.longBitsToDouble(
39+
1L shl
40+
exp - (java.lang.Double.MIN_EXPONENT - (jdk.internal.math.DoubleConsts.SIGNIFICAND_WIDTH - 1))
41+
)
42+
}
43+
}
44+
}
45+
}*/
46+
}
47+
}

src/main/kotlin/org/openrndr/math/Matrix44.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package org.openrndr.math
22

3-
import java.io.Serializable
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
45

56
/**
67
* A 4x4 matrix with double precision
78
*/
9+
@Serializable
810
data class Matrix44(
911
val c0r0: Double = 0.0, val c1r0: Double = 0.0, val c2r0: Double = 0.0, val c3r0: Double = 0.0,
1012
val c0r1: Double = 0.0, val c1r1: Double = 0.0, val c2r1: Double = 0.0, val c3r1: Double = 0.0,
1113
val c0r2: Double = 0.0, val c1r2: Double = 0.0, val c2r2: Double = 0.0, val c3r2: Double = 0.0,
12-
val c0r3: Double = 0.0, val c1r3: Double = 0.0, val c2r3: Double = 0.0, val c3r3: Double = 0.0) : Serializable, LinearType<Matrix44> {
14+
val c0r3: Double = 0.0, val c1r3: Double = 0.0, val c2r3: Double = 0.0, val c3r3: Double = 0.0) : LinearType<Matrix44> {
1315

1416
companion object {
1517
/**
@@ -142,6 +144,7 @@ data class Matrix44(
142144
/**
143145
* Returns a transposed version of the matrix
144146
*/
147+
@Transient
145148
val transposed
146149
get() = Matrix44(
147150
c0r0, c0r1, c0r2, c0r3,
@@ -152,6 +155,7 @@ data class Matrix44(
152155
/**
153156
* The 3x3 top-left part of the 4x4 matrix
154157
*/
158+
@Transient
155159
val matrix33
156160
get() = Matrix33(c0r0, c1r0, c2r0,
157161
c0r1, c1r1, c2r1,

0 commit comments

Comments
 (0)