Skip to content

Commit a3c66e2

Browse files
authored
Merge pull request #363 from tpaviot/review/webgl-updates
Review/webgl updates
2 parents b617f4d + b9cd2ea commit a3c66e2

20 files changed

Lines changed: 1147 additions & 558 deletions

examples/core_geometry_utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
1717

1818
from functools import wraps
19+
from math import radians
1920

2021
from OCC.BRepBndLib import brepbndlib_Add
2122
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakePrism
@@ -29,7 +30,7 @@
2930
from OCC.GeomAbs import GeomAbs_C0
3031
from OCC.GeomAPI import GeomAPI_PointsToBSpline
3132
from OCC.TColgp import TColgp_Array1OfPnt
32-
from OCC.gp import gp_Vec, gp_Pnt, gp_Trsf
33+
from OCC.gp import gp_Vec, gp_Pnt, gp_Trsf, gp_OX, gp_OY, gp_OZ
3334

3435

3536
def make_edge(*args):
@@ -77,7 +78,6 @@ def make_face(*args):
7778
return result
7879

7980

80-
8181
class assert_isdone(object):
8282
'''
8383
raises an assertion error when IsDone() returns false, with the error
@@ -200,6 +200,29 @@ def translate_shp(shp, vec, copy=False):
200200
return brep_trns.Shape()
201201

202202

203+
def rotate_shp_3_axis(shape, rx, ry, rz, unity="deg"):
204+
""" Rotate a shape around (O,x), (O,y) and (O,z).
205+
206+
@param rx_degree : rotation around (O,x)
207+
@param ry_degree : rotation around (O,y)
208+
@param rz_degree : rotation around (O,z)
209+
210+
@return : the rotated shape.
211+
"""
212+
if unity == "deg": # convert angle to radians
213+
rx = radians(rx)
214+
ry = radians(ry)
215+
rz = radians(rz)
216+
alpha = gp_Trsf()
217+
alpha.SetRotation(gp_OX(), rx)
218+
beta = gp_Trsf()
219+
beta.SetRotation(gp_OY(), ry)
220+
gamma = gp_Trsf()
221+
gamma.SetRotation(gp_OZ(), rz)
222+
brep_trns = BRepBuilderAPI_Transform(shape, alpha*beta*gamma, False)
223+
shp = brep_trns.Shape()
224+
return shp
225+
203226
def make_extrusion(face, length, vector=gp_Vec(0., 0., 1.)):
204227
''' creates a extrusion from a face, along the vector vector.
205228
with a distance legnth. Note that the normal vector does not
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2016 Thomas Paviot (tpaviot@gmail.com)
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import print_function
21+
22+
import random
23+
24+
from OCC.Display.WebGl import threejs_renderer
25+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus
26+
from OCC.gp import gp_Vec
27+
28+
from core_geometry_utils import translate_shp
29+
30+
my_ren = threejs_renderer.ThreejsRenderer()
31+
32+
idx = 0
33+
torus_shp1 = BRepPrimAPI_MakeTorus(20, 5).Shape()
34+
torus_shp2 = translate_shp(torus_shp1, gp_Vec(60, 0, 0))
35+
torus_shp3 = translate_shp(torus_shp1, gp_Vec(-60, 0, 0))
36+
37+
# default quality
38+
print("Computing RED torus: default quality")
39+
my_ren.DisplayShape(torus_shp1, export_edges=True, color=(1,0,0)) # red
40+
41+
# better mesh quality, i.e. more triangles
42+
print("Computing GREEN torus: better quality, more time to compute")
43+
my_ren.DisplayShape(torus_shp2, export_edges=True, color=(0,1,0), # green
44+
mesh_quality = 0.2)
45+
46+
# worse quality, i.e. less triangles
47+
print("Computing BLUE torus: worse quality, faster to compute")
48+
my_ren.DisplayShape(torus_shp3, export_edges=True, color=(0,0,1), # blue
49+
mesh_quality= 10.)
50+
my_ren.render()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2016 Thomas Paviot (tpaviot@gmail.com)
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import print_function
21+
22+
import random
23+
24+
from OCC.Display.WebGl import threejs_renderer
25+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus
26+
from OCC.gp import gp_Vec
27+
28+
from core_geometry_utils import translate_shp, rotate_shp_3_axis
29+
30+
my_ren = threejs_renderer.ThreejsRenderer()
31+
n_toruses = 100
32+
33+
idx = 0
34+
for i in range(n_toruses):
35+
torus_shp = BRepPrimAPI_MakeTorus(10 + random.random()*10, random.random()*10).Shape()
36+
# random position and orientation and color
37+
angle_x = random.random()*360
38+
angle_y = random.random()*360
39+
angle_z = random.random()*360
40+
rotated_torus = rotate_shp_3_axis(torus_shp, angle_x, angle_y, angle_z, 'deg')
41+
tr_x = random.uniform(-70, 50)
42+
tr_y = random.uniform(-70, 50)
43+
tr_z = random.uniform(-50, 50)
44+
trans_torus = translate_shp(rotated_torus, gp_Vec(tr_x, tr_y, tr_z))
45+
rnd_color = (random.random(), random.random(), random.random())
46+
my_ren.DisplayShape(trans_torus, export_edges=True, color=rnd_color, transparency=random.random())
47+
print("%i%%" % (idx * 100 / n_toruses), end="")
48+
idx += 1
49+
my_ren.render()

examples/core_webgl_threejs_shader_demo1.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/core_webgl_threejs_torus.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
##Copyright 2009-2014 Thomas Paviot (tpaviot@gmail.com)
3+
##Copyright 2009-2016 Thomas Paviot (tpaviot@gmail.com)
44
##
55
##This file is part of pythonOCC.
66
##
@@ -21,5 +21,6 @@
2121
from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus
2222

2323
torus_shp = BRepPrimAPI_MakeTorus(20., 10.).Shape()
24-
my_renderer = threejs_renderer.ThreejsRenderer(background_color="#123345")
24+
my_renderer = threejs_renderer.ThreejsRenderer()
2525
my_renderer.DisplayShape(torus_shp)
26+
my_renderer.render()

examples/core_webgl_x3dom_cylinderhead.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
# render cylinder head in x3dom
3131
my_renderer = x3dom_renderer.X3DomRenderer()
3232
my_renderer.DisplayShape(cylinder_head)
33+
my_renderer.render()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2016 Thomas Paviot (tpaviot@gmail.com)
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
import random
21+
22+
from OCC.Display.WebGl import x3dom_renderer
23+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
24+
from OCC.gp import gp_Vec
25+
26+
from core_geometry_utils import translate_shp, rotate_shp_3_axis
27+
28+
my_ren = x3dom_renderer.X3DomRenderer()
29+
30+
for i in range(100):
31+
box_shp = BRepPrimAPI_MakeBox(random.random()*20, random.random()*20, random.random()*20).Shape()
32+
# random position and orientation and color
33+
angle_x = random.random()*360
34+
angle_y = random.random()*360
35+
angle_z = random.random()*360
36+
rotated_box = rotate_shp_3_axis(box_shp, angle_x, angle_y, angle_z, 'deg')
37+
tr_x = random.uniform(-20, 20)
38+
tr_y = random.uniform(-20, 20)
39+
tr_z = random.uniform(-20, 20)
40+
trans_box = translate_shp(rotated_box, gp_Vec(tr_x, tr_y, tr_z))
41+
rnd_color = (random.random(), random.random(), random.random())
42+
my_ren.DisplayShape(trans_box, export_edges=True, color=rnd_color, transparency=random.random())
43+
my_ren.render()

examples/core_webgl_x3dom_shader_demo1.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

examples/core_webgl_x3dom_splitted_faces.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)