[orx-composition] Add test and demo for PR#360

This commit is contained in:
Abe Pazos
2025-07-13 11:59:44 +02:00
parent 37442f018e
commit e2fa39af08
2 changed files with 93 additions and 1 deletions

View File

@@ -1,7 +1,14 @@
package org.openrndr.extra.composition
import org.openrndr.math.Vector2
import org.openrndr.shape.Circle
import org.openrndr.shape.LineSegment
import org.openrndr.shape.Rectangle
import org.openrndr.shape.Shape
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
class TestComposition {
val composition = let { _ ->
@@ -35,4 +42,46 @@ class TestComposition {
assertNull(composition.findImage("outer"))
assertNull(composition.findImage("shape"))
}
}
class TestCompositionIntersections {
val bounds = Rectangle(Vector2.ZERO, 640.0, 480.0)
val outline = Shape(
listOf(
Circle(bounds.center, 70.0).contour.reversed,
Circle(bounds.center, 100.0).contour,
)
)
val radius = outline.bounds.dimensions.length / 2
val off = outline.bounds.center
val num = radius.toInt()
@Test
fun `use a shape as a mask for line segments`() {
// Make sure intersections do not fail randomly, which was fixed in
// https://github.com/openrndr/orx/commit/e8f50b3dd153ed82de121e9017cf42f6ea95ac8e
val svg = List(100) {
drawComposition {
lineSegments(List(num) { segNum ->
val yNorm = (segNum / (num - 1.0))
val x = ((segNum % 2) * 2.0 - 1.0) * radius
val y = (yNorm * 2.0 - 1.0) * radius
val start = Vector2(-x, y) + off
val end = Vector2(x, y) + off
LineSegment(start, end)
})
clipMode = ClipMode.INTERSECT
shape(outline)
}
}
val shapes = svg[50].findShapes()
val dimensions = svg[50].bounds.dimensions
assertTrue(shapes.isNotEmpty())
assertTrue(shapes.first().shape.contours.isNotEmpty())
assertTrue(dimensions.x > 0.0)
assertTrue(dimensions.y > 0.0)
}
}