View Javadoc
1   /*
2    * Copyright (C) 2015 Alberto Irurueta Carro (alberto@irurueta.com)
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  package com.irurueta.ar.calibration;
17  
18  import com.irurueta.geometry.InhomogeneousPoint2D;
19  import com.irurueta.geometry.Point2D;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Contains coordinates of ideal points for a circle pattern.
27   */
28  public class CirclesPattern2D extends Pattern2D implements Serializable {
29  
30      /**
31       * Default point separation in the circles pattern.
32       */
33      public static final double DEFAULT_POINT_SEPARATION = 4.25e-2; //4.25cm
34  
35      /**
36       * Default number of columns in the circles pattern.
37       */
38      public static final int DEFAULT_COLS = 4;
39  
40      /**
41       * Default number of rows in the circles pattern.
42       */
43      public static final int DEFAULT_ROWS = 11;
44  
45      /**
46       * Default number of points used by this 2D pattern.
47       */
48      public static final int DEFAULT_NUMBER_OF_POINTS = DEFAULT_COLS * DEFAULT_ROWS;
49  
50      /**
51       * Minimum number of columns.
52       */
53      public static final int MIN_COLS = 2;
54  
55      /**
56       * Minimum number of rows.
57       */
58      public static final int MIN_ROWS = 2;
59  
60  
61      /**
62       * Point separation in the circles pattern.
63       */
64      private double pointSeparation;
65  
66      /**
67       * Number of columns in the circles pattern.
68       */
69      private int cols;
70  
71      /**
72       * Number of rows in the circles pattern.
73       */
74      private int rows;
75  
76      /**
77       * Constructor.
78       */
79      public CirclesPattern2D() {
80          pointSeparation = DEFAULT_POINT_SEPARATION;
81          cols = DEFAULT_COLS;
82          rows = DEFAULT_ROWS;
83      }
84  
85      /**
86       * Returns point separation in the circles pattern.
87       *
88       * @return point separation in the circles pattern.
89       */
90      public double getPointSeparation() {
91          return pointSeparation;
92      }
93  
94      /**
95       * Sets point separation in the circles pattern.
96       *
97       * @param pointSeparation point separation in the circles pattern.
98       * @throws IllegalArgumentException if provided value is zero or negative.
99       */
100     public void setPointSeparation(final double pointSeparation) {
101         if (pointSeparation <= 0.0) {
102             throw new IllegalArgumentException();
103         }
104 
105         this.pointSeparation = pointSeparation;
106     }
107 
108     /**
109      * Returns number of columns in the circles pattern.
110      *
111      * @return number of columns in the circles pattern.
112      */
113     public int getCols() {
114         return cols;
115     }
116 
117     /**
118      * Sets number of columns in the circles pattern.
119      *
120      * @param cols number of columns in the circles pattern.
121      * @throws IllegalArgumentException if provided value is less than 2.
122      */
123     public void setCols(final int cols) {
124         if (cols < MIN_COLS) {
125             throw new IllegalArgumentException();
126         }
127 
128         this.cols = cols;
129     }
130 
131     /**
132      * Returns number of rows in the circles pattern.
133      *
134      * @return number of rows in the circles pattern.
135      */
136     public int getRows() {
137         return rows;
138     }
139 
140     /**
141      * Sets number of rows in the circles pattern.
142      *
143      * @param rows number of rows in the circles pattern.
144      * @throws IllegalArgumentException if provided value is less than 2.
145      */
146     public void setRows(final int rows) {
147         if (rows < MIN_ROWS) {
148             throw new IllegalArgumentException();
149         }
150 
151         this.rows = rows;
152     }
153 
154     /**
155      * Returns ideal points coordinates contained in a circles 2D pattern and
156      * expressed in meters. These values are used for calibration purposes.
157      *
158      * @return ideal points coordinates.
159      */
160     @Override
161     public List<Point2D> getIdealPoints() {
162         final var points = new ArrayList<Point2D>();
163 
164         double x;
165         double y;
166         for (var i = 0; i < rows; i++) {
167             for (var j = 0; j < cols; j++) {
168                 x = (2 * j + i % 2) * pointSeparation;
169                 y = i * pointSeparation;
170                 points.add(new InhomogeneousPoint2D(x, y));
171             }
172         }
173         return points;
174     }
175 
176     /**
177      * Returns number of 2D points used by this pattern.
178      *
179      * @return number of 2D points used by this pattern.
180      */
181     @Override
182     public int getNumberOfPoints() {
183         return rows * cols;
184     }
185 
186     /**
187      * Returns type of pattern.
188      *
189      * @return type of pattern.
190      */
191     @Override
192     public Pattern2DType getType() {
193         return Pattern2DType.CIRCLES;
194     }
195 }