Newer
Older
/**
* Created by aghassaei on 5/26/15.
*/
var cellMaterial = new THREE.MeshNormalMaterial();
var wireframeMaterial = new THREE.MeshBasicMaterial({color:0x000000, wireframe:true});
function DMACell(indices){
//object 3d is parent to all 3d elements related to cell, parts, beams, nodes, etc
this.object3D = this._buildObject3D();
this._addChildren(this._buildMesh(), this.object3D);//build cell meshes
if (!this.superCell) globals.three.sceneAdd(this.object3D, this._getSceneType());
DMACell.prototype._getSceneType = function(){//todo need this?
DMACell.prototype._buildObject3D = function(){
var object3D = this._translateCell(this._rotateCell(new THREE.Object3D()));
object3D.myParent = this;//reference to get mouse raycasting back
DMACell.prototype.getObject3D = function(){//only called by supercells
return this.object3D;
}
var geometry = this._getGeometry();
var meshes = [];
var mesh = new THREE.Mesh(geometry, cellMaterial);
mesh.name = "cell";
wireframe.name = "cell";
meshes.push(wireframe);
return meshes;
};
DMACell.prototype._buildWireframe = function(mesh, geometry){//abstract mesh representation of cell
return new THREE.Mesh(geometry, wireframeMaterial);
};
DMACell.prototype._initParts = function(){
return [];//override in subclasses
};
DMACell.prototype.setSuperCell = function(superCell, index){
this.superCell = superCell;
this.superCellIndex = index;
if (this.superCellIndex == this.superCell.getLength()) this.mesh.rotateZ(Math.PI);
if (globals.appState.get("cellMode")=="part") {
this.parts = this.__initParts();
this.draw();
}
};
DMACell.prototype.setMode = function(mode){
if (mode === undefined) mode = globals.appState.get("cellMode");
switch(mode) {
case "cell":
break;
case "part":
if (!this.parts) {
this.parts = this._initParts();
var self = this;
_.each(this.parts, function(part){
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
break;
case "beam":
if (!this.beams) this.beams = this._initBeams();
break;
case "node":
if (!this.nodes) this.nodes = this._initNodes();
break;
}
_.each(this.object3D.children, function(child){
child.visible = child.name == mode;
});
};
DMACell.prototype.xScale = function(){
return globals.lattice.xScale(0);
};
DMACell.prototype.yScale = function(){
return globals.lattice.yScale(0);
};
DMACell.prototype.zScale = function(){
return globals.lattice.zScale(0);
};
DMACell.prototype.destroy = function(){
if (this.destroyStarted) return;
this.destroyStarted = true;
if (this.object3D) {
globals.three.sceneRemove(this.object3D, this._getSceneType());
this.object3D.myParent = null;
// this.object3D.dispose();
// geometry.dispose();
// material.dispose();
this.object3D = null;
}
this.destroyParts();
this.nodes = null;
this.beams = null;
if (this.superCell) {
this.superCell.destroy();
this.superCell = null;
}
this.superCellIndex = null;
this.indices = null;
};
DMACell.prototype.destroyParts = function(){
_.each(this.parts, function(part){
if (part) part.destroy();
});
this.parts = null;
};
//DMACell.prototype.removePart = function(index){
// this.parts[index].destroy();
// this.parts[index] = null;
// var hasAnyParts = false;//check if all parts have been deleted
// _.each(this.parts, function(part){
// if (part) hasAnyParts = true;
// });
// if (!hasAnyParts) globals.lattice.removeCell(this);//if all parts are gone, remove cell
//};
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
DMACell.prototype.toJSON = function(){
var data = {
indices:this.indices//todo get rid of this and calculate from min and max
};
if (this.parts) data.parts = this.parts;
return data;
};
//DMACell.prototype.moveTo = function(position, axis){//used for stock simulations
// this.object3D.position[axis] = position;
// if (globals.appState.get("cellMode") == "part"){
// _.each(this.parts, function(part){
// if (part) part.moveTo(position, axis);
// });
// }
//};
//
//DMACell.prototype.getType = function(){
// return null;//only used in freeform layout
//};
//
//DMACell.prototype._initNodes = function(vertices){
// var position = this.getPosition();
// var orientation = this.getOrientation();
// var nodes = [];
// for (var i=0;i<vertices.length;i++){
// var vertex = vertices[i].clone();
// vertex.applyQuaternion(orientation);
// vertex.add(position);
// nodes.push(new DmaNode(vertex, i));
// }
// return nodes;
//};
//
//
//DMACell.prototype._initBeams = function(nodes, faces){
// var beams = [];
// var self = this;
// var addBeamFunc = function(index1, index2){
// var duplicate = false;
// _.each(beams, function(beam){
// var index = beam.getIndex();
// if (index[0] == index1 && index[1] == index2) duplicate = true;
// });
// if (duplicate) return;
// var diff = nodes[index1].getPosition();
// diff.sub(nodes[index2].getPosition());
// if (diff.length() > self.getScale()*1.01) return;
// if (index2>index1) {
// beams.push(new DmaBeam(nodes[index1], nodes[index2], self));
// }
// };
// for (var i=0;i<nodes.length;i++){
// _.each(faces, function(face){
// if (face.a == i) {
// addBeamFunc(i, face.b);
// addBeamFunc(i, face.c);
// } else if (face.b == i){
// addBeamFunc(i, face.a);
// addBeamFunc(i, face.c);
// } else if (face.c == i){
// addBeamFunc(i, face.a);
// addBeamFunc(i, face.b);
// }
// })
// }
// return beams;
//};