-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewPoint.java
More file actions
159 lines (135 loc) · 3.91 KB
/
ViewPoint.java
File metadata and controls
159 lines (135 loc) · 3.91 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
public class ViewPoint{
private Vector position;
private Vector direction;
private Vector up;
private Vector side;
private double zoom;
private int projectionType;
public static final int PERSPECTIVE = 0;
public static final int ISOMETRIC = 1;
public static final int LENS = 2;
public ViewPoint(Vector p, Vector d, Vector u){
position = p;
direction = d;
up = u;
zoom = d.norm();
updateAxes();
projectionType = PERSPECTIVE;
}
public ViewPoint(Vector p, Vector d, Vector u, int pr){
position = p;
direction = d;
up = u;
zoom = d.norm();
updateAxes();
if(pr == ISOMETRIC ||
pr == PERSPECTIVE ||
pr == LENS){
projectionType = pr;
} else {
projectionType = PERSPECTIVE;
}
}
public void setProjectionType(int projo){
this.projectionType = projo;
}
public int getProjectionType(){
return this.projectionType;
}
public void shiftViewPoint(double sideD, double upD){
Vector shifter = direction.unit().times(upD);
position = position.plus(shifter);
Quaternion rotation = new Quaternion(direction,-sideD);
//position = position.rotate(rotation);
direction = direction.rotate(rotation);
up = up.rotate(rotation);
side = side.rotate(rotation);
}
public void rotateViewPoint(double sideD, double upD, Vector center){
Vector axis = side.times(-upD).plus(up.times(sideD));
double angle = Math.sqrt(sideD*sideD + upD*upD);
Quaternion rotation = new Quaternion(axis,angle);
position = position.minus(center).rotate(rotation).plus(center);
direction = direction.rotate(rotation);
up = up.rotate(rotation);
side = side.rotate(rotation);
}
private void updateAxes(){
if(direction.cross(up).norm() > 0.01){
this.side = direction.cross(up).unit();
} else {
if(direction.cross(new Vector(0,0,1)).norm() > 0.01){
this.side = direction.cross(new Vector(0,0,1)).unit();
} else {
this.side = direction.cross(new Vector(0,1,0)).unit();
}
}
up = direction.cross(side).unit().times(-1);
//System.out.println(side.element(0)+" "+side.element(1)+" "+side.element(2));
//System.out.println(up.element(0)+" "+up.element(1)+" "+up.element(2));
}
public Vector getPosition(){
return this.position;
}
public void setPosition(Vector newp){
this.position = newp;
}
public Vector getDirection(){
return this.direction;
}
public void setDirection(Vector newd){
this.direction = newd;
zoom = direction.norm();
updateAxes();
}
public Vector getUp(){
return this.up;
}
public void setUp(Vector newu){
this.up = newu;
updateAxes();
}
public double getZoom(){
return this.zoom;
}
public Vector getSide(){
return this.side;
}
public void aimAtCenter(Structure geo){
Vector center = geo.getCenter();
if(center == null){
geo.calculateCenter();
center = geo.getCenter();
}
this.direction = (center.minus(position)).unit().times(getZoom());
updateAxes();
}
public static ViewPoint resetViewPoint(Structure geo)
throws Exception{
double stepback = 50;
if(geo.countAllAtoms() > 100){
stepback = 12.0*Math.pow(geo.countAllAtoms(),1.0/3.0);
}
Vector positi = new Vector(0.0,0.0,stepback);
Vector up = new Vector(0.0,1.0,0.0);
Vector center = geo.getCenter();
positi = positi.plus(center);
Vector direct = (center.minus(positi)).unit().times(10);
return new ViewPoint(positi, direct, up);
}
public static ViewPoint resetViewPoint(Structure geo, ViewPoint old)
throws Exception{
double stepback = 50;
if(geo.countAllAtoms() > 100){
stepback = 15.0*Math.pow(geo.countAllAtoms(),1.0/3.0);
}
Vector positi = new Vector(0.0,0.0,stepback);
Vector up = new Vector(0.0,1.0,0.0);
Vector center = geo.getCenter();
positi = positi.plus(center);
Vector direct = (center.minus(positi)).unit().times(10);
int projo = old.getProjectionType();
ViewPoint newview = new ViewPoint(positi, direct, up, projo);
return newview;
}
}