forked from dbt-ethz/mola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolab3D.py
309 lines (290 loc) · 12 KB
/
colab3D.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['Benjamin Dillenburger','Demetris Shammas','Mathias Bernhard']
__copyright__ = 'Copyright 2019 / Digital Building Technologies DBT / ETH Zurich'
__license__ = 'MIT License'
__email__ = ['<[email protected]>']
__code=""
'''display variables'''
__showAxis=True
__showEdges=False
__edgesWidth=1.0
__showWireframe=False
__showPointsCloud=False
__showPointsNumbers=False
__backgroundColor = (0,0,0)
__edgesColor = (1,1,1,1)
__pointColor = (1,1,1)
__pointSize = 10
__numberColor = (1,1,1)
__numberSize = 1
__canvasWidth = "100%"
__canvasHeight = "56.25vw"
__positionsWelded = []
def display_mesh(mesh,canvasWidth=None,canvasHeight=None,showAxis=True,showEdges=False,edgesWidth=1.0,showWireframe=False,showPointsCloud=False,showPointsNumbers=False,backgroundColor=(0,0,0),edgesColor=(1,1,1,1),pointColor=(1,1,1),pointSize=10,numberColor=(1,1,1),numberSize=1):
"""
Displays Mesh.
Arguments:
----------
mesh : mola.core.Mesh
The mesh to be displayed
Optional Arguments:
----------
canvasWidth : float
canvasHeight : float
showAxis : Boolean
showEdges : Boolean
showWireframe : Boolean
showPointsCloud : Boolean
showPointsNumbers : Boolean
edgesWidth : float
backgroundColor : tuple (r,g,b)
r,g,b values, 0.0 to 1.0
edgesColor : tuple (r,g,b,a)
r,g,b,a values, 0.0 to 1.0
pointColor : tuple (r,g,b)
r,g,b values, 0.0 to 1.0
pointSize : float
numberColor : tuple (r,g,b)
r,g,b values, 0.0 to 1.0
numberSize : float
"""
global __canvasWidth, __canvasHeight, __showAxis,__showEdges,__edgesWidth,__showWireframe,__showPointsCloud,__showPointsNumbers,__backgroundColor,__edgesColor,__pointColor,__pointSize,__numberColor,__numberSize
if(canvasWidth):
__canvasWidth = str(canvasWidth) + "px"
if(canvasHeight):
__canvasHeight = str(canvasHeight) + "px"
__showAxis=showAxis
__showEdges=showEdges
__edgesWidth=edgesWidth
__showWireframe=showWireframe
__showPointsCloud = showPointsCloud
__showPointsNumbers = showPointsNumbers
__backgroundColor = backgroundColor
__edgesColor = edgesColor
__pointColor = pointColor
__pointSize = pointSize
__numberColor = numberColor
__numberSize = numberSize
if(showPointsNumbers):
global __positionsWelded
__positionsWelded = []
for v in mesh.vertices:
__positionsWelded.extend((v.x,v.y,v.z))
return display_faces(mesh.faces)
def display_faces_welded(faces):
__begin3D()
verticesDict={}
positions=[]
indices=[]
colors=[]
cIndex=0
for face in faces:
col=face.color
# triangle
for i in range(3):
p=face.vertices[i]
ptuple = (p.x,p.y,p.z)
if ptuple in verticesDict:
indices.append(verticesDict[ptuple])
else:
verticesDict[ptuple]=cIndex
positions.extend(ptuple)
colors.extend(face.color)
indices.append(cIndex)
cIndex+=1
# quad
if len(face.vertices)>3:
p=face.vertices[3]
ptuple = (p.x,p.y,p.z)
i0=indices[-3]
i1=indices[-1]
indices.append(i0)
indices.append(i1)
if ptuple in verticesDict:
indices.append(verticesDict[ptuple])
else:
verticesDict[ptuple]=cIndex
positions.extend(ptuple)
colors.extend(face.color)
indices.append(cIndex)
cIndex+=1
__draw_mesh_with_colors(positions, indices, colors)
__end3D()
return __code
def display_faces(faces):
__begin3D()
positions=[]
indices=[]
colors=[]
cIndex=0
for face in faces:
for v in face.vertices:
positions.extend((v.x,v.y,v.z))
colors.extend(face.color)
indices.extend([cIndex, cIndex+1, cIndex+2])
if len(face.vertices)>3:
# indices.extend([cIndex+2, cIndex+3, cIndex])
for i in range(2,len(face.vertices)-1):
indices.extend([cIndex+i, cIndex+i+1, cIndex])
cIndex+=len(face.vertices)
__draw_mesh_with_colors(positions, indices, colors)
__end3D()
return __code
def __begin3D():
global __code
__code=""
__code+='''<canvas id="renderCanvas" touch-action="none"></canvas>
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<style>
html, body {
overflow: hidden;
width:''' + __canvasWidth + ''';
height: ''' + __canvasHeight + ''';
margin: 0;
padding: 0;
}
#renderCanvas {
width:''' + __canvasWidth + ''';
height: ''' + __canvasHeight + ''';
touch-action: none;
}
</style>
<script>
var canvas = document.getElementById("renderCanvas");'''
__code+='''var createScene = function () {var scene = new BABYLON.Scene(engine);'''
__code+='''scene.useRightHandedSystem = true;'''
__code+='''scene.clearColor = new BABYLON.Color3'''+str(__backgroundColor)+ ";"
__code+='''var light = new BABYLON.DirectionalLight("direct", new BABYLON.Vector3(1, 1, 1), scene);var light2 = new BABYLON.DirectionalLight("direct", new BABYLON.Vector3(-1, -1, -1), scene);'''
__code+='''var camera = new BABYLON.ArcRotateCamera("camera1", 0, 0, 0, new BABYLON.Vector3(0, 0, 0), scene);
camera.setPosition(new BABYLON.Vector3(0, 5, 30));
camera.attachControl(canvas, true);'''
__code+=''' var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.5;'''
def __draw_mesh_with_colors(vertices,faces,vertexColors):
global __code
__code+="var positions = "+str(vertices)+";"
__code+="var indices = "+str(faces)+";"
__code+="var colors = "+str(vertexColors)+";"
return __code
"""
def __draw_test_mesh():
global __code
__code+=''' var positions = [-5, 2, -3, -7, -2, -3, -3, -2, -3, 5, 2, 3, 7, -2, 3, 3, -2, 3];
var indices = [0, 1, 2, 3, 4, 5]; '''
"""
def __end3D():
global __code
__code+= '''
//Create a custom mesh
var customMesh = new BABYLON.Mesh("custom", scene);
//Empty array to contain calculated values
var normals = [];
var vertexData = new BABYLON.VertexData();
BABYLON.VertexData.ComputeNormals(positions, indices, normals);
//Assign positions, indices and normals to vertexData
vertexData.positions = positions;
vertexData.indices = indices;
vertexData.normals = normals;
vertexData.colors = colors;
//Apply vertexData to custom mesh
vertexData.applyToMesh(customMesh);
var mat = new BABYLON.StandardMaterial("mat", scene);
mat.backFaceCulling = false;'''
if __showWireframe:
__code+='''mat.wireframe=true;'''
if __showPointsCloud:
__code+='''mat.pointsCloud=true;'''
__code+='''mat.pointSize=''' + str(__pointSize) + ';'
__code+='''mat.diffuseColor = new BABYLON.Color3'''+str(__pointColor)+ ';'
__code+='''mat.emissiveColor = new BABYLON.Color3'''+str(__pointColor)+ ';'
__code+='''mat.disableLighting = true; '''
if __showEdges:
__code+= '''customMesh.enableEdgesRendering();'''
__code+= '''customMesh.edgesWidth = ''' + str(__edgesWidth * 10) +';'
__code+= '''customMesh.edgesColor = new BABYLON.Color4'''+str(__edgesColor)+ ';'
if __showPointsNumbers:
__code+='''
var drawNumber = function(scene, text, posVector){
//data reporter
var outputplane = BABYLON.Mesh.CreatePlane("outputplane", 1.5, scene, false);
outputplane.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_ALL;
outputplane.material = new BABYLON.StandardMaterial("outputplane", scene);
outputplane.position = posVector;
outputplane.scaling.x = '''+str(__numberSize)+ ''';
outputplane.scaling.y = '''+str(__numberSize)+ ''';
var outputplaneTexture = new BABYLON.DynamicTexture("dynamic texture", 512, scene, true);
outputplane.material.diffuseTexture = outputplaneTexture;
outputplane.material.diffuseColor = new BABYLON.Color3'''+str(__numberColor)+ ''';
outputplane.material.emissiveColor = new BABYLON.Color3'''+str(__numberColor)+ ''';
outputplane.material.backFaceCulling = false;
//outputplaneTexture.getContext().clearRect(0, 140, 512, 512);
var textColor = new BABYLON.Color3''' + str(__numberColor) + '''.toHexString();
outputplaneTexture.drawText(text, null, 300, "200px arial", textColor);
outputplaneTexture.hasAlpha = true;
};'''
__code+='''
//drawNumber(scene,"NT",new BABYLON.Vector3(0,0,0));
//var vPositions = customMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
//console.log("vplength " + vPositions.length);
var ind = 0;
var pWelded = ''' +str(__positionsWelded)+ ''';
for(var i=0;i<pWelded.length;i+=3){
var posX = (pWelded[i]);
var posY = (pWelded[i+1]);
var posZ = (pWelded[i+2]);
drawNumber(scene,ind.toString(),new BABYLON.Vector3(posX,posY+1,posZ));
ind++;
}
'''
if __showAxis:
__code+='''
var makeTextPlane = function(text, color, size) {
var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", 50, scene, true);
dynamicTexture.hasAlpha = true;
dynamicTexture.drawText(text, 5, 40, "bold 36px Arial", color , "transparent", true);
var plane = new BABYLON.Mesh.CreatePlane("TextPlane", size, scene, true);
plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", scene);
plane.material.backFaceCulling = false;
plane.material.specularColor = new BABYLON.Color3(0, 0, 0);
plane.material.diffuseTexture = dynamicTexture;
return plane;
};'''
__code+='''// show axis
var showAxis = function(size) {
var axisX = BABYLON.Mesh.CreateLines("axisX", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, 0.05 * size, 0),
new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, -0.05 * size, 0)
], scene);
axisX.color = new BABYLON.Color3(1, 0, 0);
var xChar = makeTextPlane("X", "red", size / 10);
xChar.position = new BABYLON.Vector3(0.9 * size, -0.05 * size, 0);
var axisY = BABYLON.Mesh.CreateLines("axisY", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3( -0.05 * size, size * 0.95, 0),
new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3( 0.05 * size, size * 0.95, 0)
], scene);
axisY.color = new BABYLON.Color3(0, 1, 0);
var yChar = makeTextPlane("Y", "green", size / 10);
yChar.position = new BABYLON.Vector3(0, 0.9 * size, -0.05 * size);
var axisZ = BABYLON.Mesh.CreateLines("axisZ", [
new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3( 0 , -0.05 * size, size * 0.95),
new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3( 0, 0.05 * size, size * 0.95)
], scene);
axisZ.color = new BABYLON.Color3(0, 0, 1);
var zChar = makeTextPlane("Z", "blue", size / 10);
zChar.position = new BABYLON.Vector3(0, 0.05 * size, 0.9 * size);
};
showAxis(10);'''
__code+='''customMesh.material = mat;'''
__code+='''return scene;
};
var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
var scene = createScene();
engine.runRenderLoop(function () {
if (scene) {scene.render();}
});
// Resize
window.addEventListener("resize", function () {
engine.resize();
});
</script>'''