1 /*
2  * Copyright (C) 2015 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 com.android.camera.ui.motion;
18 
19 /**
20  * Represents a discrete linear scale function.
21  */
22 public final class LinearScale {
23     private final float mDomainA;
24     private final float mDomainB;
25     private final float mRangeA;
26     private final float mRangeB;
27 
28     private final float mScale;
29 
LinearScale(float domainA, float domainB, float rangeA, float rangeB)30     public LinearScale(float domainA, float domainB, float rangeA, float rangeB) {
31         mDomainA = domainA;
32         mDomainB = domainB;
33         mRangeA = rangeA;
34         mRangeB = rangeB;
35 
36         // Precomputed ratio between input domain and output range.
37         float scale = (mRangeB - mRangeA) / (mDomainB - mDomainA);
38         mScale = Float.isNaN(scale) ? 0.0f : scale;
39     }
40 
41     /**
42      * Clamp a given domain value to the given domain.
43      */
clamp(float domainValue)44     public float clamp(float domainValue) {
45         if (mDomainA > mDomainB) {
46             return Math.max(mDomainB, Math.min(mDomainA, domainValue));
47         }
48 
49         return Math.max(mDomainA, Math.min(mDomainB, domainValue));
50     }
51 
52     /**
53      * Returns true if the value is within the domain.
54      */
isInDomain(float domainValue)55     public boolean isInDomain(float domainValue) {
56         if (mDomainA > mDomainB) {
57             return domainValue <= mDomainA && domainValue >= mDomainB;
58         }
59         return domainValue >= mDomainA && domainValue <= mDomainB;
60     }
61 
62     /**
63      * Linearly scale a given domain value into the output range.
64      */
scale(float domainValue)65     public float scale(float domainValue) {
66         return mRangeA + (domainValue - mDomainA) * mScale;
67     }
68 
69     /**
70      * For the current domain and range parameters produce a new scale function
71      * that is the inverse of the current scale function.
72      */
inverse()73     public LinearScale inverse() {
74         return new LinearScale(mRangeA, mRangeB, mDomainA, mDomainB);
75     }
76 
77     @Override
toString()78     public String toString() {
79         return "LinearScale{" +
80               "mDomainA=" + mDomainA +
81               ", mDomainB=" + mDomainB +
82               ", mRangeA=" + mRangeA +
83               ", mRangeB=" + mRangeB + "}";
84     }
85 }