-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectionTriangle.java
More file actions
192 lines (159 loc) · 6.23 KB
/
ProjectionTriangle.java
File metadata and controls
192 lines (159 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Copyright (C) 2014,2015 Maximilian Diedrich
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.hu_berlin.informatik.spws2014.ImagePositionLocator;
import java.util.ArrayList;
import java.util.List;
/**
* Projects points from GPS to image coords.
* Triple of Marker.
*/
public class ProjectionTriangle {
private double MIN_TRIANGLE_ANGLE_SIZE;
private double FLAT_TRI_WEIGHT_PENALTY;
private double MAX_DISSIMILARITY_PERCENT;
private double weight = 1; //[0..1]
private GpsPoint pivot;
private Marker a;
private Marker b;
private Marker c;
private double common_divisor;
public List<ProjectionTriangle> projectionGroup;
private GpsPoint cachedInput;
private FPoint2D cachedResult;
public ProjectionTriangle(Marker a, Marker b) {
initTriangle(a, b, b.getOrthogonal(a));
pivot = new GpsPoint(a.realpoint, b.realpoint);
}
public ProjectionTriangle(Marker a, Marker b, Marker c) {
initTriangle(a,b,c);
pivot = new GpsPoint(a.realpoint, b.realpoint, c.realpoint);
}
public ProjectionTriangle(Marker a, Marker b, Marker c, double MaxDissim, double BadTriPanelty, double GoodTriMinAngle) {
MIN_TRIANGLE_ANGLE_SIZE = GoodTriMinAngle;
FLAT_TRI_WEIGHT_PENALTY = BadTriPanelty;
MAX_DISSIMILARITY_PERCENT = MaxDissim;
initTriangle(a,b,c);
pivot = new GpsPoint(a.realpoint, b.realpoint, c.realpoint);
}
private void initTriangle(Marker ma, Marker mb, Marker mc) {
a = ma;
b = mb;
c = mc;
common_divisor = ((b.realpoint.latitude - c.realpoint.latitude) * (a.realpoint.longitude - c.realpoint.longitude)
+(c.realpoint.longitude - b.realpoint.longitude) * (a.realpoint.latitude - c.realpoint.latitude));
if (!isValidTriangle(a.realpoint.getPlanarDistance(b.realpoint),
a.realpoint.getPlanarDistance(c.realpoint),
b.realpoint.getPlanarDistance(c.realpoint),
MIN_TRIANGLE_ANGLE_SIZE)
|| !isValidTriangle(a.imgpoint.getDistance(b.imgpoint),
a.imgpoint.getDistance(c.imgpoint),
b.imgpoint.getDistance(c.imgpoint),
MIN_TRIANGLE_ANGLE_SIZE)) {
weight *= FLAT_TRI_WEIGHT_PENALTY;
}
projectionGroup = new ArrayList<ProjectionTriangle>();
projectionGroup.add(this);
}
public GpsPoint getPivot() {
return this.pivot;
}
public Marker getMarker(int i) {
switch (i) {
case 0: return a;
case 1: return b;
case 2: return c;
default: return null;
}
}
public double getWeigth() {
return weight;
}
/**
* @param sideA Distance A
* @param sideB Distance B
* @param sideC Distance C
* @return If the triangle has no angle less than minAngle(in degrees).
*/
public boolean isValidTriangle(double sideA, double sideB, double sideC, double minAngle) {
if (Math.acos((sideA*sideA+sideB*sideB-sideC*sideC)/(2*sideA*sideB)) < Math.toRadians(minAngle)) return false;
if (Math.acos((sideC*sideC+sideB*sideB-sideA*sideA)/(2*sideC*sideB)) < Math.toRadians(minAngle)) return false;
if (Math.acos((sideA*sideA+sideC*sideC-sideB*sideB)/(2*sideA*sideC)) < Math.toRadians(minAngle)) return false;
return true;
}
/**
* Approximates the new image position by averaging
* the projectSingle() of every projectionGroup member.
* @return new image position
*/
public FPoint2D project(GpsPoint pos, int ownPriority) {
FPoint2D result = new FPoint2D();
for (ProjectionTriangle t : projectionGroup) {
//own projection is more important than the rest of the projGroup
if (t == this)
result.fma(t.projectSingle(pos), ownPriority);
else
result.fma(t.projectSingle(pos), 1);
}
result.div(projectionGroup.size() - 1 + ownPriority);
return result;
}
/**
* Approximates the new image position using a barycentric coordinate system.
* Formula taken from https://en.wikipedia.org/wiki/Barycentric_coordinate_system#Conversion_between_barycentric_and_Cartesian_coordinates
* @return new image position
*/
public FPoint2D projectSingle(GpsPoint pos) {
if (pos != cachedInput) {
double delta1 = ((b.realpoint.latitude - c.realpoint.latitude) * (pos.longitude - c.realpoint.longitude)
+(c.realpoint.longitude - b.realpoint.longitude) * (pos.latitude - c.realpoint.latitude))/ common_divisor;
double delta2 = ((c.realpoint.latitude - a.realpoint.latitude) * (pos.longitude - c.realpoint.longitude)
+(a.realpoint.longitude - c.realpoint.longitude) * (pos.latitude - c.realpoint.latitude))/ common_divisor;
double delta3 = 1 - delta1 - delta2;
double x = delta1 * a.imgpoint.x + delta2 * b.imgpoint.x + delta3 * c.imgpoint.x;
double y = delta1 * a.imgpoint.y + delta2 * b.imgpoint.y + delta3 * c.imgpoint.y;
cachedInput = pos;
cachedResult = new FPoint2D(x,y);
}
return cachedResult;
}
/**
* Checks if two triangles are similar and adds inputTri
* to the this ProjectionTriangles projection group.
* @param inputTri input triangle
* @return if triangles are similar
*/
public boolean tryAddToProjGroup(ProjectionTriangle inputTri) {
Point2D center = new Point2D((a.imgpoint.x+b.imgpoint.x+c.imgpoint.x)/3,
(a.imgpoint.y+b.imgpoint.y+c.imgpoint.y)/3);
for (int i = 0; i < 3; i++) {
double distToCenter = center.getDistance(getMarker(i).imgpoint);
double distToNew = getMarker(i).imgpoint.getDistance(inputTri.projectSingle(getMarker(i).realpoint));
if (distToNew > MAX_DISSIMILARITY_PERCENT * distToCenter) {
return false;
}
}
projectionGroup.add(inputTri);
return true;
}
public String toString() {
String sim = "";
for (ProjectionTriangle t : projectionGroup) {
if (t != this)
sim += Integer.toHexString(t.hashCode()) + ", ";
}
return Integer.toHexString(this.hashCode()) + " weight: " + this.getWeigth() + " sim: " + sim;
}
}