Skip to content

Commit e6dd097

Browse files
committed
added simple examples for the current cpp implementations
1 parent 2b60f0d commit e6dd097

File tree

10 files changed

+295
-13
lines changed

10 files changed

+295
-13
lines changed
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

3.49 MB
Binary file not shown.
-126 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 random
7+
import compas
8+
import compas_rhino
9+
10+
from compas.datastructures import Network
11+
from compas.numerical import fd_cpp
12+
from compas.utilities import i_to_red
13+
14+
from compas_rhino.helpers import NetworkArtist
15+
16+
network = Network.from_obj(compas.get('saddle.obj'))
17+
18+
# for u, v, attr in network.edges(True):
19+
# attr['q'] = 1.0 * random.randint(1, 5)
20+
21+
vertices = network.get_vertices_attributes('xyz')
22+
loads = network.get_vertices_attributes(('px', 'py', 'pz'), (0.0, 0.0, 0.0))
23+
fixed = network.leaves()
24+
free = [i for i in range(len(vertices)) if i not in fixed]
25+
edges = list(network.edges())
26+
q = network.get_edges_attribute('q', 1.0)
27+
28+
29+
artist = NetworkArtist(network, layer='Network')
30+
31+
artist.clear_layer()
32+
artist.draw_edges(color='#cccccc')
33+
artist.redraw()
34+
35+
xyz = fd_cpp(vertices, edges, fixed, q, loads)
36+
37+
for key, attr in network.vertices(True):
38+
attr['x'] = xyz[key][0]
39+
attr['y'] = xyz[key][1]
40+
attr['z'] = xyz[key][2]
41+
42+
artist.draw_vertices(color={key: '#ff0000' for key in network.leaves()})
43+
artist.draw_edges(color='#000000')
44+
artist.redraw()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from __future__ import print_function
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
5+
import Rhino
6+
7+
from System.Drawing.Color import FromArgb
8+
9+
from Rhino.Geometry import Point3d
10+
from Rhino.Geometry import Line
11+
12+
import compas
13+
import compas_rhino
14+
15+
from compas.datastructures import Network
16+
from compas.numerical import fd_cpp
17+
18+
from compas_rhino.helpers import NetworkArtist
19+
from compas_rhino.helpers.selectors import VertexSelector
20+
from compas_rhino.conduits import LinesConduit
21+
22+
23+
network = Network.from_obj(compas.get('saddle.obj'))
24+
25+
26+
vertices = network.get_vertices_attributes('xyz')
27+
loads = network.get_vertices_attributes(('px', 'py', 'pz'), (0.0, 0.0, 0.0))
28+
fixed = network.leaves()
29+
free = [i for i in range(len(vertices)) if i not in fixed]
30+
edges = list(network.edges())
31+
q = network.get_edges_attribute('q', 1.0)
32+
33+
34+
artist = NetworkArtist(network, layer='Network')
35+
36+
artist.clear_layer()
37+
artist.draw_edges(color='#cccccc')
38+
artist.redraw()
39+
40+
xyz = fd_cpp(vertices, edges, fixed, q, loads)
41+
42+
for key, attr in network.vertices(True):
43+
attr['x'] = xyz[key][0]
44+
attr['y'] = xyz[key][1]
45+
attr['z'] = xyz[key][2]
46+
47+
artist.draw_vertices(color={key: '#ff0000' for key in network.leaves()})
48+
artist.draw_edges(color='#000000')
49+
artist.redraw()
50+
51+
# select and drag one of the anchors
52+
# update equilibrium using *OnDynamicDraw*
53+
54+
move = VertexSelector.select_vertex(network)
55+
move = int(move)
56+
57+
sp = network.vertex_coordinates(move)
58+
ep = sp[:]
59+
ep[2] += 1
60+
61+
vertical = Line(Point3d(*sp), Point3d(*ep))
62+
63+
color = FromArgb(255, 255, 255)
64+
65+
66+
def OnDynamicDraw(sender, e):
67+
global xyz
68+
xyz[move] = list(e.CurrentPoint)
69+
xyz = fd_cpp(xyz, edges, fixed, q, loads)
70+
for u, v in edges:
71+
e.Display.DrawLine(Point3d(* xyz[u]), Point3d(* xyz[v]), color)
72+
73+
74+
gp = Rhino.Input.Custom.GetPoint()
75+
gp.SetCommandPrompt('Point to move to?')
76+
gp.Constrain(vertical)
77+
gp.DynamicDraw += OnDynamicDraw
78+
gp.Get()
79+
80+
if gp.CommandResult() == Rhino.Commands.Result.Success:
81+
xyz[move] = list(gp.Point())
82+
xyz = fd_cpp(xyz, edges, fixed, q, loads)
83+
84+
for key, attr in network.vertices(True):
85+
attr['x'] = xyz[key][0]
86+
attr['y'] = xyz[key][1]
87+
attr['z'] = xyz[key][2]
88+
89+
artist.draw_vertices(color={key: '#ff0000' for key in network.leaves()})
90+
artist.draw_edges(color='#000000')
91+
artist.redraw()

src/compas/numerical/algorithms/_fd_cpp/src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ extern "C"
66
{
77
typedef void callback(int k);
88

9-
void fd(int numv, int nume, int numfix, double **vertices, int **edges, double **loads, double *q, int *fixed, int *free);
9+
void fd(int numv, int nume, int numfix, double** vertices, int** edges, double** loads, double* q, int* fixed, int* free);
1010
}
1111

12-
void fd(int numv, int nume, int numfix, double **vertices, int **edges, double **loads, double *q, int *fixed, int *free)
12+
void fd(int numv, int nume, int numfix, double** vertices, int** edges, double** loads, double* q, int* fixed, int* free)
1313
{
1414
int i;
1515
int numfree = numv - numfix;
@@ -70,8 +70,8 @@ void fd(int numv, int nume, int numfix, double **vertices, int **edges, double *
7070

7171
Cit = Ci.transpose();
7272

73-
A = Ci.transpose() * Q * Ci;
74-
b = Pi - Cit * Q * Cf * Xf;
73+
A.noalias() = Cit * Q * Ci;
74+
b.noalias() = Pi - Cit * Q * Cf * Xf;
7575

7676
Xi = A.colPivHouseholderQr().solve(b);
7777

src/compas/numerical/algorithms/fd_cpp.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ def fd_cpp(vertices, edges, fixed, q, loads):
3737
print(DLL)
3838
raise
3939

40+
free = [i for i in range(len(vertices)) if i not in fixed]
41+
4042
cvertices = Array2D(vertices, 'double')
41-
cedges = Array2D(edges, 'int')
42-
cloads = Array2D(loads, 'double')
43-
cq = Array1D(q, 'double')
44-
cfixed = Array1D(fixed, 'int')
45-
cfree = Array1D(free, 'int')
43+
cedges = Array2D(edges, 'int')
44+
cloads = Array2D(loads, 'double')
45+
cq = Array1D(q, 'double')
46+
cfixed = Array1D(fixed, 'int')
47+
cfree = Array1D(free, 'int')
4648

4749
lib.fd.argtypes = [
4850
ctypes.c_int,
@@ -86,7 +88,6 @@ def fd_cpp(vertices, edges, fixed, q, loads):
8688
vertices = network.get_vertices_attributes('xyz')
8789
loads = network.get_vertices_attributes(('px', 'py', 'pz'), (0.0, 0.0, 0.0))
8890
fixed = network.leaves()
89-
free = [i for i in range(len(vertices)) if i not in fixed]
9091
edges = list(network.edges())
9192
q = network.get_edges_attribute('q', 1.0)
9293

src/compas_rhino/cameras/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)