-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathView.java
More file actions
283 lines (262 loc) · 9.58 KB
/
View.java
File metadata and controls
283 lines (262 loc) · 9.58 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.Arrays;
public class View{
public static final Color darkBlue = new Color(0,0,128,255);
public static final Color darkRed = new Color(128,0,0,255);
public static final Color darkGreen = new Color(0,128,0,255);
public static final Color blue = new Color(0,0,255,255);
public static final Color red = new Color(255,0,0,255);
public static final Color green = new Color(0,255,0,255);
public static final Color gold = new Color(255,193,37,255);
public static final Color yellow = new Color(255,255,0,255);
public static final Color steel = new Color(35,107,142,255);
public static final Color transBlue = new Color(0,0,128,128);
public static final Color transRed = new Color(128,0,0,128);
public static final Color transGreen = new Color(0,128,0,128);
public static final Color transLightBlue = new Color(0,0,255,128);
public static final Color transLightRed = new Color(255,0,0,128);
public static final Color transLightGreen = new Color(0,255,0,128);
public static final Color transYellow = new Color(255,255,0,128);
public static final Color black = new Color(0,0,0,255);
public static final Color darkGray = new Color(63,63,63,255);
public static final Color gray = new Color(127,127,127,255);
public static final Color lightGray = new Color(191,191,191,255);
public static final Color white = new Color(255,255,255,255);
public static final Color transWhite = new Color(255,255,255,128);
public static final Color transBlack = new Color(0,0,0,128);
public static final Color transGold = new Color(245,195,50,150);
public static Color bg = new Color(128,128,128,255);
public static Color picked = new Color(0,0,0,255);
public static Color unpicked = new Color(50,50,50,100);
public static Color hiddenPicked = new Color(0,0,0,42);
public static Color hiddenUnpicked = new Color(50,50,50,6);
public static Color transparent = new Color(0,0,0,0);
public static int STROKE_WIDTH = 4;
public static double displayRadiusMultiplier = 0.5;
private Projection[] projections;
private Vector originProjection;
private Vector[] cellProjections;
private Vector[] axisProjections;
private int visibleAtoms;
private boolean isReady;
private int width;
private int height;
public View(Structure geo, ViewPoint look, int width, int height){
isReady = false;
this.width = width;
this.height = height;
try{
calculateProjections(geo,look);
} catch(Exception error){
axisProjections = null;
cellProjections = null;
projections = null;
visibleAtoms = 0;
}
}
public boolean isReady(){
return this.isReady;
}
public void setReady(boolean yesno){
this.isReady = yesno;
}
public Shape getAtomicShape(int index){
return projections[index].getAtomicShape(width,height);
}
public boolean isVisible(int index){
return projections[index].isVisible();
}
public Line2D getCellAxisLine(int index){
if(originProjection.element(0) > 0 && cellProjections[index].element(0) > 0){
double originx = originProjection.element(1)*100+width/2;
double originy = -originProjection.element(2)*100+height/2;
double vecx = cellProjections[index].element(1)*100+width/2;
double vecy = -cellProjections[index].element(2)*100+height/2;
return new Line2D.Double(originx,originy,vecx,vecy);
} else {
return new Line2D.Double(-100,-100,-100,-100);
}
}
public Line2D getUnitAxisLine(int index){
if(originProjection.element(0) > 0 && cellProjections[index].element(0) > 0){
double originx = originProjection.element(1)*100+width/2;
double originy = -originProjection.element(2)*100+height/2;
double vecx = axisProjections[index].element(1)*100+width/2;
double vecy = -axisProjections[index].element(2)*100+height/2;
return new Line2D.Double(originx,originy,vecx,vecy);
} else {
return new Line2D.Double(-100,-100,-100,-100);
}
}
public void updateProjections(Structure geo, ViewPoint look)
throws Exception{
Atom[] atoms = geo.getAllAtoms();
visibleAtoms = 0;
if(atoms.length != projections.length){
throw new Exception();
}
for(int i=0; i<atoms.length; i++){
Vector pVec = projection(atoms[i],geo,look);
projections[i].setX(pVec.element(1));
projections[i].setY(pVec.element(2));
projections[i].setDepth(pVec.element(3));
projections[i].setRadius(pVec.element(0));
if(atoms[i].isHidden()){
projections[i].setColor(transparent);
if(atoms[i].isPicked()){
projections[i].setLineColor(hiddenPicked);
} else {
projections[i].setLineColor(hiddenUnpicked);
}
} else {
projections[i].setColor(atoms[i].getColor());
if(atoms[i].isPicked()){
projections[i].setLineColor(picked);
} else {
projections[i].setLineColor(unpicked);
}
}
if(projections[i].isVisible()){
visibleAtoms ++;
}
}
Arrays.sort(projections);
originProjection = projection(new Vector(0,0,0),geo,look);
for(int i=0; i<3; i++){
cellProjections[i] = projection(geo.getCell()[i],geo,look);
}
axisProjections[0] = projection(new Vector(1,0,0),geo,look);
axisProjections[1] = projection(new Vector(0,1,0),geo,look);
axisProjections[2] = projection(new Vector(0,0,1),geo,look);
}
private void calculateProjections(Structure geo, ViewPoint look){
Atom[] atoms = geo.getAllAtoms();
projections = new Projection[atoms.length];
visibleAtoms = 0;
for(int i=0; i<atoms.length; i++){
Vector pVec = projection(atoms[i],geo,look);
projections[i] = new Projection(i);
projections[i].setX(pVec.element(1));
projections[i].setY(pVec.element(2));
projections[i].setDepth(pVec.element(3));
projections[i].setRadius(pVec.element(0));
projections[i].setColor(atoms[i].getColor());
if(atoms[i].isHidden()){
projections[i].setColor(transparent);
if(atoms[i].isPicked()){
projections[i].setLineColor(hiddenPicked);
} else {
projections[i].setLineColor(hiddenUnpicked);
}
} else {
projections[i].setColor(atoms[i].getColor());
if(atoms[i].isPicked()){
projections[i].setLineColor(picked);
} else {
projections[i].setLineColor(unpicked);
}
}
if(projections[i].isVisible()){
visibleAtoms ++;
}
}
Arrays.sort(projections);
cellProjections = new Vector[3];
axisProjections = new Vector[3];
originProjection = projection(new Vector(0,0,0),geo,look);
for(int i=0; i<3; i++){
cellProjections[i] = projection(geo.getCell()[i],geo,look);
}
axisProjections[0] = projection(new Vector(1,0,0),geo,look);
axisProjections[1] = projection(new Vector(0,1,0),geo,look);
axisProjections[2] = projection(new Vector(0,0,1),geo,look);
}
public static Vector projection(Atom atom, Structure geo, ViewPoint look){
Vector proj = projection(atom.getCoordinates(),geo,look);
proj.setElement(0,proj.element(0)*atom.getRadius());
return proj;
}
public static Vector projection(Vector coordinates, Structure geo, ViewPoint look){
// perspective projection
Vector shifted = coordinates.minus(look.getPosition());
double[] projCoord = new double[4];
projCoord[3] = shifted.norm();
if(look.getProjectionType() == ViewPoint.PERSPECTIVE){
double totalDist = shifted.projectionOnVector(look.getDirection()).norm();
if(shifted.dot(look.getDirection()) > 0){
projCoord[0] = look.getZoom()/totalDist;
} else {
projCoord[0] = -1;
}
shifted = shifted.times(projCoord[0]).minus(look.getDirection());
projCoord[1] = shifted.dot(look.getSide());
projCoord[2] = shifted.dot(look.getUp());
return new Vector(projCoord);
} else if(look.getProjectionType() == ViewPoint.ISOMETRIC){
double totalDist = (geo.getCenter().minus(look.getPosition())).norm();
projCoord[3] = shifted.dot(look.getDirection());
if(shifted.dot(look.getDirection()) > 0){
projCoord[0] = look.getZoom()/totalDist;
} else {
projCoord[0] = -1;
}
shifted = shifted.times(projCoord[0]).projectionOnPlane(look.getDirection());
projCoord[1] = shifted.dot(look.getSide());
projCoord[2] = shifted.dot(look.getUp());
return new Vector(projCoord);
} else if(look.getProjectionType() == ViewPoint.LENS){
double totalDist = shifted.norm();
if(shifted.dot(look.getDirection()) > 0){
projCoord[0] = look.getZoom()/totalDist;
} else {
projCoord[0] = -1;
}
shifted = shifted.unit();
Vector unitDir = look.getDirection().unit();
Vector planeProj = shifted.projectionOnPlane(unitDir).unit();
double theta = Math.acos(shifted.dot(unitDir));
double phiS = 0.0;
double phiU = 0.0;
if(theta != 0){
phiS = (planeProj.dot(look.getSide()));
phiU = (planeProj.dot(look.getUp()));
}
projCoord[1] = phiS*theta*look.getZoom();
projCoord[2] = phiU*theta*look.getZoom();
return new Vector(projCoord);
}
return null;
}
public Projection[] getProjections(){
return this.projections;
}
public Vector[] getCellProjections(){
return this.cellProjections;
}
public int getNumberOfVisibleAtoms(){
return visibleAtoms;
}
public int[] coversScreenPoint(int x, int y){
boolean[] covered = new boolean[this.projections.length];
int hits = 0;
for(int i=0; i<covered.length; i++){
covered[i] = projections[i].coversScreenPoint(x,y,width,height);
if(covered[i]){
hits++;
}
}
int[] list = new int[hits];
int index = 0;
for(int i=0; i<covered.length; i++){
if(covered[i]){
list[index] = projections[i].getIndex();
index++;
}
}
//Arrays.sort(list);
return list;
}
}