1/*
2 * Copyright (C) 2012 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
17package com.android.camera.crop;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Matrix;
22import android.graphics.Paint;
23import android.graphics.Rect;
24import android.graphics.RectF;
25
26/*
27import com.android.gallery3d.filtershow.cache.BitmapCache;
28import com.android.gallery3d.filtershow.cache.ImageLoader;
29import com.android.gallery3d.filtershow.filters.FilterCropRepresentation;
30import com.android.gallery3d.filtershow.filters.FilterMirrorRepresentation;
31import com.android.gallery3d.filtershow.filters.FilterMirrorRepresentation.Mirror;
32import com.android.gallery3d.filtershow.filters.FilterRepresentation;
33import com.android.gallery3d.filtershow.filters.FilterRotateRepresentation;
34import com.android.gallery3d.filtershow.filters.FilterRotateRepresentation.Rotation;
35import com.android.gallery3d.filtershow.filters.FilterStraightenRepresentation;
36import com.android.gallery3d.filtershow.pipeline.ImagePreset;
37*/
38
39import java.util.Collection;
40import java.util.Iterator;
41
42public final class GeometryMathUtils {
43    private static final String TAG = "GeometryMathUtils";
44    public static final float SHOW_SCALE = .9f;
45
46    private GeometryMathUtils() {};
47
48    // Math operations for 2d vectors
49    public static float clamp(float i, float low, float high) {
50        return Math.max(Math.min(i, high), low);
51    }
52
53    public static float[] lineIntersect(float[] line1, float[] line2) {
54        float a0 = line1[0];
55        float a1 = line1[1];
56        float b0 = line1[2];
57        float b1 = line1[3];
58        float c0 = line2[0];
59        float c1 = line2[1];
60        float d0 = line2[2];
61        float d1 = line2[3];
62        float t0 = a0 - b0;
63        float t1 = a1 - b1;
64        float t2 = b0 - d0;
65        float t3 = d1 - b1;
66        float t4 = c0 - d0;
67        float t5 = c1 - d1;
68
69        float denom = t1 * t4 - t0 * t5;
70        if (denom == 0)
71            return null;
72        float u = (t3 * t4 + t5 * t2) / denom;
73        float[] intersect = {
74                b0 + u * t0, b1 + u * t1
75        };
76        return intersect;
77    }
78
79    public static float[] shortestVectorFromPointToLine(float[] point, float[] line) {
80        float x1 = line[0];
81        float x2 = line[2];
82        float y1 = line[1];
83        float y2 = line[3];
84        float xdelt = x2 - x1;
85        float ydelt = y2 - y1;
86        if (xdelt == 0 && ydelt == 0)
87            return null;
88        float u = ((point[0] - x1) * xdelt + (point[1] - y1) * ydelt)
89                / (xdelt * xdelt + ydelt * ydelt);
90        float[] ret = {
91                (x1 + u * (x2 - x1)), (y1 + u * (y2 - y1))
92        };
93        float[] vec = {
94                ret[0] - point[0], ret[1] - point[1]
95        };
96        return vec;
97    }
98
99    // A . B
100    public static float dotProduct(float[] a, float[] b) {
101        return a[0] * b[0] + a[1] * b[1];
102    }
103
104    public static float[] normalize(float[] a) {
105        float length = (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
106        float[] b = {
107                a[0] / length, a[1] / length
108        };
109        return b;
110    }
111
112    // A onto B
113    public static float scalarProjection(float[] a, float[] b) {
114        float length = (float) Math.sqrt(b[0] * b[0] + b[1] * b[1]);
115        return dotProduct(a, b) / length;
116    }
117
118    public static float[] getVectorFromPoints(float[] point1, float[] point2) {
119        float[] p = {
120                point2[0] - point1[0], point2[1] - point1[1]
121        };
122        return p;
123    }
124
125    public static float[] getUnitVectorFromPoints(float[] point1, float[] point2) {
126        float[] p = {
127                point2[0] - point1[0], point2[1] - point1[1]
128        };
129        float length = (float) Math.sqrt(p[0] * p[0] + p[1] * p[1]);
130        p[0] = p[0] / length;
131        p[1] = p[1] / length;
132        return p;
133    }
134
135    public static void scaleRect(RectF r, float scale) {
136        r.set(r.left * scale, r.top * scale, r.right * scale, r.bottom * scale);
137    }
138
139    // A - B
140    public static float[] vectorSubtract(float[] a, float[] b) {
141        int len = a.length;
142        if (len != b.length)
143            return null;
144        float[] ret = new float[len];
145        for (int i = 0; i < len; i++) {
146            ret[i] = a[i] - b[i];
147        }
148        return ret;
149    }
150
151    public static float vectorLength(float[] a) {
152        return (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
153    }
154
155    public static float scale(float oldWidth, float oldHeight, float newWidth, float newHeight) {
156        if (oldHeight == 0 || oldWidth == 0 || (oldWidth == newWidth && oldHeight == newHeight)) {
157            return 1;
158        }
159        return Math.min(newWidth / oldWidth, newHeight / oldHeight);
160    }
161
162    public static Rect roundNearest(RectF r) {
163        Rect q = new Rect(Math.round(r.left), Math.round(r.top), Math.round(r.right),
164                Math.round(r.bottom));
165        return q;
166    }
167
168    private static int getRotationForOrientation(int orientation) {
169        switch (orientation) {
170            case ImageLoader.ORI_ROTATE_90:
171                return 90;
172            case ImageLoader.ORI_ROTATE_180:
173                return 180;
174            case ImageLoader.ORI_ROTATE_270:
175                return 270;
176            default:
177                return 0;
178        }
179    }
180
181}
182