Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 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 #ifndef ANDROID_FILTERFW_FILTERPACKS_BASE_GEOMETRY_H
     18 #define ANDROID_FILTERFW_FILTERPACKS_BASE_GEOMETRY_H
     19 
     20 #include <vector>
     21 
     22 namespace android {
     23 namespace filterfw {
     24 
     25 // This is an initial implementation of some geometrical structures. This is
     26 // likely to grow and become more sophisticated in the future.
     27 
     28 class Point {
     29   public:
     30     Point() : x_(0.0f), y_(0.0f) {}
     31     Point(float x, float y) : x_(x), y_(y) {}
     32 
     33     float x() const { return x_; }
     34     float y() const { return y_; }
     35 
     36     float Length() const;
     37     bool ScaleTo(float new_length);
     38     static float Distance(const Point& p0, const Point& p1);
     39 
     40     // Add more of these as needed:
     41     Point operator+(const Point& other) const;
     42     Point operator-(const Point& other) const;
     43     Point operator*(float factor) const;
     44 
     45     void Rotate90Clockwise();
     46 
     47   private:
     48     float x_, y_;
     49 };
     50 
     51 class Quad {
     52   public:
     53     Quad() : points_(4) {}
     54     virtual ~Quad() {}
     55 
     56     Quad(const Point& p0, const Point& p1, const Point& p2, const Point& p3)
     57         : points_(4) {
     58       points_[0] = p0;
     59       points_[1] = p1;
     60       points_[2] = p2;
     61       points_[3] = p3;
     62     }
     63 
     64     const std::vector<Point>& points() const { return points_; }
     65     const Point& point(int ix) const;
     66 
     67   protected:
     68     std::vector<Point> points_;
     69 };
     70 
     71 class SlantedRect : public Quad {
     72   public:
     73     SlantedRect() : width_(0.0f), height_(0.0f) {}
     74     virtual ~SlantedRect() {}
     75 
     76     bool FromCenterAxisAndLengths(const Point& center,
     77                                   const Point& vert_axis,
     78                                   const Point& lenghts);
     79 
     80     float width() const { return width_; }
     81     float height() const { return height_; }
     82 
     83   private:
     84     float width_;
     85     float height_;
     86 };
     87 
     88 struct Rect {
     89   float x, y, width, height;
     90 
     91   Rect() {
     92     x = y = 0.0f;
     93     width = height = 1.0f;
     94   }
     95 
     96   Rect(float x, float y, float width, float height) {
     97     this->x = x;
     98     this->y = y;
     99     this->width = width;
    100     this->height = height;
    101   }
    102 
    103   bool ExpandToAspectRatio(float ratio);
    104   bool ExpandToMinLength(float length);
    105   bool ScaleWithLengthLimit(float factor, float max_length);
    106 };
    107 
    108 } // namespace filterfw
    109 } // namespace android
    110 
    111 #endif // ANDROID_FILTERFW_FILTERPACKS_BASE_GEOMETRY_H
    112