1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.ink.geometry 18 19 import androidx.annotation.RestrictTo 20 21 /** 22 * Represents a mutable triangle, defined by its three corners [p0], [p1] and [p2] in order. See 23 * [ImmutableTriangle] for the immutable version. 24 * 25 * @constructor Create the [MutableTriangle] from three existing [MutableVec] instances. Note that 26 * these instances will become the internal state of this [MutableTriangle], so modifications made 27 * to them directly or through setters on this [MutableTriangle] will modify the input 28 * [MutableVec] instances too. This is to allow performance-critical code to avoid any unnecessary 29 * allocations. This can be tricky to manage, especially in multithreaded code, so when calling 30 * code is unable to guarantee ownership of the nested mutable data at a particular time, it may 31 * be safest to construct this with copies of the data to give this [MutableTriangle] exclusive 32 * ownership of those copies. 33 */ 34 public class MutableTriangle( 35 override var p0: MutableVec, 36 override var p1: MutableVec, 37 override var p2: MutableVec, 38 ) : Triangle() { 39 40 /** Constructs a degenerate [MutableTriangle] with [p0], [p1], and [p2] set to (0, 0). */ 41 public constructor() : this(MutableVec(0f, 0f), MutableVec(0f, 0f), MutableVec(0f, 0f)) 42 43 /** Copies the points from [input] to this [MutableTriangle] and returns `this`. */ populateFromnull44 public fun populateFrom(input: Triangle): MutableTriangle { 45 p0.x = input.p0.x 46 p0.y = input.p0.y 47 p1.x = input.p1.x 48 p1.y = input.p1.y 49 p2.x = input.p2.x 50 p2.y = input.p2.y 51 return this 52 } 53 54 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) asImmutablenull55 override fun asImmutable(): ImmutableTriangle = ImmutableTriangle(this.p0, this.p1, this.p2) 56 57 /** 58 * Equality for [MutableTriangle] is defined using the order in which [p0], [p1] and [p2] are 59 * defined. Rotated/flipped triangles with out-of-order vertices are not considered equal. 60 */ 61 override fun equals(other: Any?): Boolean = 62 other === this || (other is Triangle && areEquivalent(this, other)) 63 64 // NOMUTANTS -- not testing exact hashCode values, just that equality implies same hashCode 65 override fun hashCode(): Int = hash(this) 66 67 override fun toString(): String = "Mutable${string(this)}" 68 } 69