Skip to content

Commit 73e25ba

Browse files
committed
Merge branch 'release/2.13.0' into master
2 parents e91d2e1 + e24efee commit 73e25ba

File tree

5 files changed

+108
-36
lines changed

5 files changed

+108
-36
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ addopts =
1717
--ignore=build
1818

1919
flake8-ignore =
20-
*.py W391
20+
*.py W391 W504
2121
docs/*.py ALL
2222

2323
looponfailroots =

stl/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__package_name__ = 'numpy-stl'
22
__import_name__ = 'stl'
3-
__version__ = '2.12.0'
3+
__version__ = '2.13.0'
44
__author__ = 'Rick van Hattem'
55
__author_email__ = '[email protected]'
66
__description__ = ' '.join('''

stl/_speedups.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# cython: language_level=2
12
from libc.stdio cimport *
23
from libc.string cimport memcpy, strcmp, strstr, strcpy
34

@@ -33,7 +34,7 @@ cdef packed struct Facet:
3334
dtype = np.dtype([
3435
('normals', np.float32, 3),
3536
('vectors', np.float32, (3, 3)),
36-
('attr', np.uint16, 1),
37+
('attr', np.uint16, (1,)),
3738
])
3839

3940
DEF ALLOC_SIZE = 200000

stl/base.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,60 @@ def __iter__(self):
589589
for point in self.points:
590590
yield point
591591

592+
def get_mass_properties_with_density(self, density):
593+
# add density for mesh,density unit kg/m3 when mesh is unit is m
594+
self.check()
595+
596+
def subexpression(x):
597+
w0, w1, w2 = x[:, 0], x[:, 1], x[:, 2]
598+
temp0 = w0 + w1
599+
f1 = temp0 + w2
600+
temp1 = w0 * w0
601+
temp2 = temp1 + w1 * temp0
602+
f2 = temp2 + w2 * f1
603+
f3 = w0 * temp1 + w1 * temp2 + w2 * f2
604+
g0 = f2 + w0 * (f1 + w0)
605+
g1 = f2 + w1 * (f1 + w1)
606+
g2 = f2 + w2 * (f1 + w2)
607+
return f1, f2, f3, g0, g1, g2
608+
609+
x0, x1, x2 = self.x[:, 0], self.x[:, 1], self.x[:, 2]
610+
y0, y1, y2 = self.y[:, 0], self.y[:, 1], self.y[:, 2]
611+
z0, z1, z2 = self.z[:, 0], self.z[:, 1], self.z[:, 2]
612+
a1, b1, c1 = x1 - x0, y1 - y0, z1 - z0
613+
a2, b2, c2 = x2 - x0, y2 - y0, z2 - z0
614+
d0, d1, d2 = b1 * c2 - b2 * c1, a2 * c1 - a1 * c2, a1 * b2 - a2 * b1
615+
616+
f1x, f2x, f3x, g0x, g1x, g2x = subexpression(self.x)
617+
f1y, f2y, f3y, g0y, g1y, g2y = subexpression(self.y)
618+
f1z, f2z, f3z, g0z, g1z, g2z = subexpression(self.z)
619+
620+
intg = numpy.zeros((10))
621+
intg[0] = sum(d0 * f1x)
622+
intg[1:4] = sum(d0 * f2x), sum(d1 * f2y), sum(d2 * f2z)
623+
intg[4:7] = sum(d0 * f3x), sum(d1 * f3y), sum(d2 * f3z)
624+
intg[7] = sum(d0 * (y0 * g0x + y1 * g1x + y2 * g2x))
625+
intg[8] = sum(d1 * (z0 * g0y + z1 * g1y + z2 * g2y))
626+
intg[9] = sum(d2 * (x0 * g0z + x1 * g1z + x2 * g2z))
627+
intg /= numpy.array([6, 24, 24, 24, 60, 60, 60, 120, 120, 120])
628+
volume = intg[0]
629+
cog = intg[1:4] / volume
630+
cogsq = cog ** 2
631+
vmass = volume * density
632+
inertia = numpy.zeros((3, 3))
633+
634+
inertia[0, 0] = (intg[5] + intg[6]) * density - vmass * (
635+
cogsq[1] + cogsq[2])
636+
inertia[1, 1] = (intg[4] + intg[6]) * density - vmass * (
637+
cogsq[2] + cogsq[0])
638+
inertia[2, 2] = (intg[4] + intg[5]) * density - vmass * (
639+
cogsq[0] + cogsq[1])
640+
inertia[0, 1] = inertia[1, 0] = -(
641+
intg[7] * density - vmass * cog[0] * cog[1])
642+
inertia[1, 2] = inertia[2, 1] = -(
643+
intg[8] * density - vmass * cog[1] * cog[2])
644+
inertia[0, 2] = inertia[2, 0] = -(
645+
intg[9] * density - vmass * cog[2] * cog[0])
646+
647+
return volume, vmass, cog, inertia
592648

tests/test_meshProperties.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,83 @@
77
tolerance = 1e-6
88

99

10+
def close(a, b):
11+
return numpy.allclose(a, b, atol=tolerance)
12+
13+
1014
def test_mass_properties_for_half_donut(binary_ascii_path, speedups):
11-
"""
15+
'''
1216
Checks the results of method get_mass_properties() on
1317
STL ASCII and binary files HalfDonut.stl
1418
One checks the results obtained with stl
1519
with the ones obtained with meshlab
16-
"""
20+
'''
1721
filename = binary_ascii_path.join('HalfDonut.stl')
1822
mesh = stl.StlMesh(str(filename), speedups=speedups)
1923
volume, cog, inertia = mesh.get_mass_properties()
20-
assert(abs(volume - 2.343149) < tolerance)
21-
assert(numpy.allclose(cog,
22-
numpy.array([1.500001, 0.209472, 1.500001]),
23-
atol=tolerance))
24-
assert(numpy.allclose(inertia,
25-
numpy.array([[+1.390429, +0.000000, +0.000000],
26-
[+0.000000, +2.701025, +0.000000],
27-
[+0.000000, +0.000000, +1.390429]]),
28-
atol=tolerance))
24+
assert close([volume], [2.343149])
25+
assert close(cog, [1.500001, 0.209472, 1.500001])
26+
assert close(inertia, [[+1.390429, +0.000000, +0.000000],
27+
[+0.000000, +2.701025, +0.000000],
28+
[+0.000000, +0.000000, +1.390429]])
2929

3030

3131
def test_mass_properties_for_moon(binary_ascii_path, speedups):
32-
"""
32+
'''
3333
Checks the results of method get_mass_properties() on
3434
STL ASCII and binary files Moon.stl
3535
One checks the results obtained with stl
3636
with the ones obtained with meshlab
37-
"""
37+
'''
3838
filename = binary_ascii_path.join('Moon.stl')
3939
mesh = stl.StlMesh(str(filename), speedups=speedups)
4040
volume, cog, inertia = mesh.get_mass_properties()
41-
assert(abs(volume - 0.888723) < tolerance)
42-
assert(numpy.allclose(cog,
43-
numpy.array([0.906913, 0.170731, 1.500001]),
44-
atol=tolerance))
45-
assert(numpy.allclose(inertia,
46-
numpy.array([[+0.562097, -0.000457, +0.000000],
47-
[-0.000457, +0.656851, +0.000000],
48-
[+0.000000, +0.000000, +0.112465]]),
49-
atol=tolerance))
41+
assert close([volume], [0.888723])
42+
assert close(cog, [0.906913, 0.170731, 1.500001])
43+
assert close(inertia, [[+0.562097, -0.000457, +0.000000],
44+
[-0.000457, +0.656851, +0.000000],
45+
[+0.000000, +0.000000, +0.112465]])
5046

5147

5248
@pytest.mark.parametrize('filename', ('Star.stl', 'StarWithEmptyHeader.stl'))
5349
def test_mass_properties_for_star(binary_ascii_path, filename, speedups):
54-
"""
50+
'''
5551
Checks the results of method get_mass_properties() on
5652
STL ASCII and binary files Star.stl and
5753
STL binary file StarWithEmptyHeader.stl (with no header)
5854
One checks the results obtained with stl
5955
with the ones obtained with meshlab
60-
"""
56+
'''
6157
filename = binary_ascii_path.join(filename)
6258
if not filename.exists():
6359
pytest.skip('STL file does not exist')
6460
mesh = stl.StlMesh(str(filename), speedups=speedups)
6561
volume, cog, inertia = mesh.get_mass_properties()
66-
assert(abs(volume - 1.416599) < tolerance)
67-
assert(numpy.allclose(cog,
68-
numpy.array([1.299040, 0.170197, 1.499999]),
69-
atol=tolerance))
70-
assert(numpy.allclose(inertia,
71-
numpy.array([[+0.509549, +0.000000, -0.000000],
72-
[+0.000000, +0.991236, +0.000000],
73-
[-0.000000, +0.000000, +0.509550]]),
74-
atol=tolerance))
62+
assert close([volume], [1.416599])
63+
assert close(cog, [1.299040, 0.170197, 1.499999])
64+
assert close(inertia, [[+0.509549, +0.000000, -0.000000],
65+
[+0.000000, +0.991236, +0.000000],
66+
[-0.000000, +0.000000, +0.509550]])
67+
68+
69+
def test_mass_properties_for_half_donut_with_density(
70+
binary_ascii_path, speedups):
71+
'''
72+
Checks the results of method get_mass_properties_with_density() on
73+
STL ASCII and binary files HalfDonut.stl
74+
One checks the results obtained with stl
75+
with the ones obtained with meshlab
76+
'''
77+
filename = binary_ascii_path.join('HalfDonut.stl')
78+
mesh = stl.StlMesh(str(filename), speedups=speedups)
79+
volume, mass, cog, inertia = mesh.get_mass_properties_with_density(1.23)
80+
81+
assert close([mass], [2.882083302268982])
82+
assert close([volume], [2.343149026234945])
83+
assert close(cog, [1.500001, 0.209472, 1.500001])
84+
print('inertia')
85+
numpy.set_printoptions(suppress=True)
86+
print(inertia)
87+
assert close(inertia, [[+1.71022851, +0.00000001, -0.00000011],
88+
[+0.00000001, +3.32226227, +0.00000002],
89+
[-0.00000011, +0.00000002, +1.71022859]])

0 commit comments

Comments
 (0)