This repository was archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPoint.java
More file actions
155 lines (130 loc) · 4.14 KB
/
Point.java
File metadata and controls
155 lines (130 loc) · 4.14 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
/*-
*******************************************************************************
* Copyright (c) 2011, 2016 Diamond Light Source Ltd.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthew Gerring - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.scanning.api.points;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.scanning.api.annotation.UiHidden;
/**
*
* This class represents an x,y position for a mapping scan.
*
* By default Points are 2D values used in things like GridScans. If used in a
* LineScan or a Spiral scan where one dimension has two motors, the
* constructor with is2D=false should be used.
*
* The Point location is immutable: you may not change the values of x and y after it
* is created.
*
* @authors James Mudd, Matthew Gerring
*/
public final class Point extends AbstractPosition {
/**
*
*/
private static final long serialVersionUID = 2946649777289185552L;
private final Double x;
private final Double y;
private final Integer xIndex;
private final Integer yIndex;
private final String xName;
private final String yName;
public Point(int xIndex, double xPosition, int yIndex, double yPosition) {
this(xIndex, xPosition, yIndex, yPosition, true);
}
public Point(int xIndex, double xPosition, int yIndex, double yPosition, boolean is2D) {
this("x", xIndex, xPosition, "y", yIndex, yPosition, is2D);
}
public Point(String xName, int xIndex, double xPosition, String yName, int yIndex, double yPosition) {
this(xName, xIndex, xPosition, yName, yIndex, yPosition, true);
}
public Point(String xName, int xIndex, double xPosition, String yName, int yIndex, double yPosition, boolean is2D) {
this.xName = xName;
this.xIndex = xIndex;
this.x = xPosition;
this.yName = yName;
this.yIndex = yIndex;
this.y = yPosition;
this.dimensionNames = is2D
? Arrays.asList(Arrays.asList(yName), Arrays.asList(xName))
: Arrays.asList(Arrays.asList(yName, xName));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
@Override
public int size() {
return 2;
}
private List<String> names;
@UiHidden
@Override
public List<String> getNames() {
if (names==null) names = Arrays.asList(yName, xName);
return names;
}
@Override
public Double get(String name) {
if (xName.equalsIgnoreCase(name)) return getX();
if (yName.equalsIgnoreCase(name)) return getY();
return null;
}
@Override
public int getIndex(String name) {
if (xName.equalsIgnoreCase(name)) return xIndex;
if (yName.equalsIgnoreCase(name)) return yIndex;
return -1;
}
private Map<String, Object> values;
@UiHidden
@Override
public Map<String, Object> getValues() {
if (values == null) {
values = new LinkedHashMap<>(2);
values.put(yName, y);
values.put(xName, x);
}
return values;
}
private Map<String, Integer> indices;
@UiHidden
@Override
public Map<String, Integer> getIndices() {
if (indices == null) {
indices = new LinkedHashMap<>(2);
indices.put(yName, yIndex);
indices.put(xName, xIndex);
}
return indices;
}
private static final String VERTEX = "([a-zA-Z0-9_])+\\((\\d+)\\)=([-+]?[0-9]*\\.?[0-9]+)";
private static final Pattern POSITION = Pattern.compile(VERTEX + ", " + VERTEX);
/**
* Parse a point from the toString() method into an instance of Point
* @param asString
* @return
*/
public static Point parse(String asString) {
Matcher m = POSITION.matcher(asString);
if (m.matches()) {
return new Point(m.group(4), Integer.parseInt(m.group(5)), Double.parseDouble(m.group(6)),
m.group(1), Integer.parseInt(m.group(2)), Double.parseDouble(m.group(3)));
}
throw new RuntimeException("Unparsable string" + asString);
}
}