1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.geometry.core.partitioning.test;
18
19 import java.util.function.UnaryOperator;
20
21 import org.apache.commons.geometry.core.Transform;
22
23 /** Implementation class for 2D {@link Transform}s. This
24 * class should only be used for testing purposes.
25 */
26 public final class TestTransform2D implements Transform<TestPoint2D> {
27
28 /** Underlying transform function. */
29 private final UnaryOperator<TestPoint2D> fn;
30
31 /** True if the transform preserves the handedness of the space. */
32 private final boolean preservesHandedness;
33
34 /** Create a new instance using the given transform function.
35 * @param fn transform function
36 */
37 public TestTransform2D(final UnaryOperator<TestPoint2D> fn) {
38 this.fn = fn;
39
40 final TestPoint2D tx = fn.apply(TestPoint2D.PLUS_X);
41 final TestPoint2D ty = fn.apply(TestPoint2D.PLUS_Y);
42
43 final double signedArea = (tx.getX() * ty.getY()) -
44 (tx.getY() * ty.getX());
45
46 this.preservesHandedness = signedArea > 0;
47 }
48
49 /** {@inheritDoc} */
50 @Override
51 public TestPoint2D apply(final TestPoint2D pt) {
52 return fn.apply(pt);
53 }
54
55 /** {@inheritDoc} */
56 @Override
57 public boolean preservesOrientation() {
58 return preservesHandedness;
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public Transform<TestPoint2D> inverse() {
64 throw new UnsupportedOperationException();
65 }
66 }