-
Notifications
You must be signed in to change notification settings - Fork 5
/
cell.js
160 lines (126 loc) · 3.98 KB
/
cell.js
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
'use strict';
/**
* @fileoverview Classes for lattice box primitives
*/
import _ from 'lodash';
import * as THREE from 'three';
import { Vector3 } from 'three';
import { cellMatrix3 } from '../utils.js';
// Cell box
class BoxMesh extends THREE.LineSegments {
/**
* Constructor for BoxMesh primitive
*
* @param {Array} lattice Lattice parameters for the unit cell
* @param {Object} parameters Options:
* color
*
*/
constructor(lattice, parameters = {}) {
const defaults = {
color: 0xffffff,
}
parameters = _.merge(defaults, parameters);
if (lattice instanceof Array) {
lattice = cellMatrix3(lattice);
}
var boxGeom = new THREE.BoxGeometry(1, 1, 1);
boxGeom = new THREE.EdgesGeometry(boxGeom); // Convert to wireframe
var boxMat = new THREE.LineBasicMaterial({
color: parameters.color,
});
super(boxGeom, boxMat);
// Set the actual lattice basis
this.matrix.setFromMatrix3(lattice);
this.matrix.setPosition( (new Vector3(0.5, 0.5, 0.5)).applyMatrix3(lattice) );
this.matrixAutoUpdate = false;
}
get color() {
return this.material.color.getHex();
}
set color(c) {
this.material.color = new THREE.Color(c);
this.needsUpdate = true;
}
}
// Arrows for axes
class AxesMesh extends THREE.Group {
/**
* Constructor for AxesMesh primitive
*
* @param {Array} lattice Lattice parameters for the unit cell
* @param {Object} parameters Options:
* linewidth
* xColor
* yColor
* zColor
*
*/
constructor(lattice, parameters = {}) {
var defaults = {
linewidth: 1.2,
xColor: 0xff0000,
yColor: 0x00ff00,
zColor: 0x0000ff
};
parameters = _.merge(defaults, parameters);
super();
if (lattice instanceof Array) {
var lc = lattice;
lattice = new THREE.Matrix3();
lattice.set(lc[0][0], lc[1][0], lc[2][0],
lc[0][1], lc[1][1], lc[2][1],
lc[0][2], lc[1][2], lc[2][2]);
}
var origin = new THREE.Vector3(0, 0, 0);
var elems = lattice.elements;
var colors = [
parameters.xColor,
parameters.yColor,
parameters.zColor
];
for (var i = 0; i < 3; ++i) {
var dir = new THREE.Vector3(elems[3 * i],
elems[3 * i + 1],
elems[3 * i + 2]);
var l = dir.length() / 3.0;
dir.normalize();
var arr = new THREE.ArrowHelper(dir, origin, l, colors[i]);
arr.line.material.linewidth = parameters.linewidth;
this.add(arr);
}
}
get xColor() {
return this.children[0].line.material.color.getHex();
}
set xColor(c) {
this.children[0].line.material.color = new THREE.Color(c);
this.children[0].cone.material.color = new THREE.Color(c);
}
get yColor() {
return this.children[1].line.material.color.getHex();
}
set yColor(c) {
this.children[1].line.material.color = new THREE.Color(c);
this.children[1].cone.material.color = new THREE.Color(c);
}
get zColor() {
return this.children[2].line.material.color.getHex();
}
set zColor(c) {
this.children[2].line.material.color = new THREE.Color(c);
this.children[2].cone.material.color = new THREE.Color(c);
}
get linewidth() {
return this.children[0].line.material.linewidth;
}
set linewidth(lw) {
for (var i = 0; i < 3; ++i) {
this.children[i].line.material.linewidth = lw;
}
}
}
export {
BoxMesh,
AxesMesh
};