Skip to content

Commit 7ab4341

Browse files
author
andrewtliew
committed
Merge remote-tracking branch 'origin/master'
2 parents b53cf34 + e6dd097 commit 7ab4341

File tree

18 files changed

+575
-171
lines changed

18 files changed

+575
-171
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ __pycache__/
44
*$py.class
55

66
# C extensions
7-
*.so
7+
# *.so
88

99
# Distribution / packaging
1010
.Python
35.8 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import compas
2+
from compas.datastructures import Mesh
3+
from compas.plotters import MeshPlotter
4+
from compas.geometry import smooth_centroid_cpp
5+
6+
kmax = 50
7+
8+
# make a mesh
9+
# and set the default vertex and edge attributes
10+
11+
mesh = Mesh.from_obj(compas.get('faces.obj'))
12+
13+
# extract numerical data from the datastructure
14+
15+
vertices = mesh.get_vertices_attributes(('x', 'y', 'z'))
16+
adjacency = [mesh.vertex_neighbours(key) for key in mesh.vertices()]
17+
fixed = [int(mesh.vertex_degree(key) == 2) for key in mesh.vertices()]
18+
19+
slider = list(mesh.vertices_where({'x': (-0.1, 0.1), 'y': (9.9, 10.1)}))[0]
20+
21+
# make a plotter for (dynamic) visualization
22+
# and define a callback function
23+
# for plotting the intermediate configurations
24+
25+
plotter = MeshPlotter(mesh, figsize=(10, 7))
26+
27+
def callback(k, xyz):
28+
print(k)
29+
30+
if k < kmax - 1:
31+
xyz[slider][0] = 0.1 * (k + 1)
32+
33+
plotter.update_vertices()
34+
plotter.update_edges()
35+
plotter.update(pause=0.001)
36+
37+
for key, attr in mesh.vertices(True):
38+
attr['x'] = xyz[key][0]
39+
attr['y'] = xyz[key][1]
40+
attr['z'] = xyz[key][2]
41+
42+
# plot the lines of the original configuration of the mesh
43+
# as a reference
44+
45+
lines = []
46+
for u, v in mesh.edges():
47+
lines.append({
48+
'start': mesh.vertex_coordinates(u, 'xy'),
49+
'end' : mesh.vertex_coordinates(v, 'xy'),
50+
'color': '#cccccc',
51+
'width': 0.5
52+
})
53+
54+
plotter.draw_lines(lines)
55+
56+
# draw the vertices and edges in the starting configuration
57+
# and pause for a second before starting the dynamic visualization
58+
59+
plotter.draw_vertices(facecolor={key: '#000000' for key in mesh.vertices() if mesh.vertex_degree(key) == 2})
60+
plotter.draw_edges()
61+
62+
plotter.update(pause=0.5)
63+
64+
# run the smoother
65+
66+
xyz = smooth_centroid_cpp(vertices, adjacency, fixed, kmax=kmax, callback=callback)
67+
68+
# keep the plot alive
69+
70+
plotter.show()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
5+
from ctypes import c_double
6+
import compas
7+
import compas_rhino
8+
9+
from compas.datastructures import Mesh
10+
from compas.geometry import smooth_centroid_cpp
11+
12+
from compas_rhino.helpers import MeshArtist
13+
from compas_rhino.conduits import PointsConduit
14+
from compas_rhino.conduits import LinesConduit
15+
16+
kmax = 50
17+
18+
# make a mesh
19+
# and set the default vertex and edge attributes
20+
21+
mesh = Mesh.from_obj(compas.get('faces.obj'))
22+
23+
edges = list(mesh.edges())
24+
25+
# extract numerical data from the datastructure
26+
27+
vertices = mesh.get_vertices_attributes(('x', 'y', 'z'))
28+
adjacency = [mesh.vertex_neighbours(key) for key in mesh.vertices()]
29+
fixed = [int(mesh.vertex_degree(key) == 2) for key in mesh.vertices()]
30+
31+
# make a artist for (dynamic) visualization
32+
# and define a callback function
33+
# for plotting the intermediate configurations
34+
35+
slider = list(mesh.vertices_where({'x': (-0.1, 0.1), 'y': (9.9, 10.1)}))[0]
36+
37+
artist = MeshArtist(mesh, layer='SmoothMesh')
38+
artist.clear_layer()
39+
40+
41+
def callback(k, xyz):
42+
compas_rhino.wait()
43+
44+
print(k)
45+
46+
if k < kmax - 1:
47+
xyz[slider][0] = c_double(0.1 * (k + 1))
48+
49+
pointsconduit.points = [mesh.vertex_coordinates(key) for key in mesh.vertices()]
50+
linesconduit.lines = [mesh.edge_coordinates(u, v) for u, v in edges]
51+
52+
pointsconduit.redraw(k)
53+
linesconduit.redraw(k)
54+
55+
for key, attr in mesh.vertices(True):
56+
attr['x'] = float(xyz[key][0])
57+
attr['y'] = float(xyz[key][1])
58+
attr['z'] = float(xyz[key][2])
59+
60+
61+
pointsconduit = PointsConduit(radius=10, refreshrate=5)
62+
linesconduit = LinesConduit(refreshrate=5)
63+
64+
65+
with pointsconduit.enabled():
66+
with linesconduit.enabled():
67+
xyz = smooth_centroid_cpp(vertices, adjacency, fixed, kmax=kmax, callback=callback)
68+
69+
70+
for key, attr in mesh.vertices(True):
71+
attr['x'] = xyz[key][0]
72+
attr['y'] = xyz[key][1]
73+
attr['z'] = xyz[key][2]
74+
75+
artist.clear_edges()
76+
artist.draw_vertices()
77+
artist.draw_edges()
78+
artist.redraw()

src/compas/geometry/algorithms/_smoothing_cpp/src/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// cd _smoothing_cpp
44
// g++ -shared -fPIC src/main.cpp -o smoothing.so
55

6-
using namespace std;
7-
86
extern "C"
97
{
108
typedef void callback(int k);
@@ -19,7 +17,7 @@ void smooth_centroid(int v, int *nbrs, int *fixed, double **vertices, int **neig
1917
int j, n;
2018
double cx, cy, cz;
2119

22-
vector< vector<double> > xyz(v, vector<double>(3, 0.0));
20+
std::vector< std::vector<double> > xyz(v, std::vector<double>(3, 0.0));
2321

2422
for (k = 0; k < kmax; k++) {
2523

8.24 KB
Binary file not shown.

src/compas/numerical/algorithms/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
from .dr_numpy import *
33
from .drx_numpy import *
44
from .fd_numpy import *
5+
from .fd_cpp import *
56
from .pca_numpy import *
67

78
from .dr import __all__ as a
89
from .dr_numpy import __all__ as b
910
from .drx_numpy import __all__ as c
1011
from .fd_numpy import __all__ as d
11-
from .pca_numpy import __all__ as e
12+
from .fd_cpp import __all__ as e
13+
from .pca_numpy import __all__ as f
1214

13-
__all__ = a + b + c + d + e
15+
__all__ = a + b + c + d + e + f
3.49 MB
Binary file not shown.
2.1 MB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
5+
import ctypes
6+
import compas
7+
8+
from compas.datastructures import Network
9+
from compas.plotters import NetworkPlotter
10+
from compas.utilities import i_to_red
11+
12+
from compas.interop.core.cpp.xdarray import Array2D
13+
from compas.interop.core.cpp.xdarray import Array1D
14+
15+
lib = ctypes.cdll.LoadLibrary('fd.so')
16+
17+
network = Network.from_obj(compas.get('saddle.obj'))
18+
19+
vertices = network.get_vertices_attributes('xyz')
20+
loads = network.get_vertices_attributes(('px', 'py', 'pz'), (0.0, 0.0, 0.0))
21+
edges = list(network.edges())
22+
fixed = network.leaves()
23+
free = [i for i in range(len(vertices)) if i not in fixed]
24+
q = network.get_edges_attribute('q', 1.0)
25+
26+
cvertices = Array2D(vertices, 'double')
27+
cedges = Array2D(edges, 'int')
28+
cloads = Array2D(loads, 'double')
29+
cq = Array1D(q, 'double')
30+
cfixed = Array1D(fixed, 'int')
31+
cfree = Array1D(free, 'int')
32+
33+
lib.fd.argtypes = [
34+
ctypes.c_int,
35+
ctypes.c_int,
36+
ctypes.c_int,
37+
cvertices.ctype,
38+
cedges.ctype,
39+
cloads.ctype,
40+
cq.ctype,
41+
cfixed.ctype,
42+
cfree.ctype
43+
]
44+
45+
lib.fd(
46+
ctypes.c_int(len(vertices)),
47+
ctypes.c_int(len(edges)),
48+
ctypes.c_int(len(fixed)),
49+
cvertices.cdata,
50+
cedges.cdata,
51+
cloads.cdata,
52+
cq.cdata,
53+
cfixed.cdata,
54+
cfree.cdata
55+
)
56+
57+
xyz = cvertices.pydata
58+
59+
for key, attr in network.vertices(True):
60+
attr['x'] = float(xyz[key][0])
61+
attr['y'] = float(xyz[key][1])
62+
attr['z'] = float(xyz[key][2])
63+
64+
65+
zmax = max(network.get_vertices_attribute('z'))
66+
67+
plotter = NetworkPlotter(network, figsize=(10, 7))
68+
plotter.draw_vertices(
69+
facecolor={key: i_to_red(attr['z'] / zmax) for key, attr in network.vertices(True)}
70+
)
71+
plotter.draw_edges()
72+
plotter.show()

0 commit comments

Comments
 (0)