Skip to content

Commit db51f51

Browse files
committed
fix bug in normals calc
1 parent 682ced2 commit db51f51

File tree

4 files changed

+11
-18
lines changed

4 files changed

+11
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* Fixed box scaling.
2323
* Fixed a bug in `Polyline.divide_polyline_by_length` related to a floating point rounding error.
2424
* Fixed bug in `RobotModel.zero_configuration`.
25+
* Fixed bug in `compas.geometry.normals`.
2526

2627
### Removed
2728

src/compas/geometry/_core/normals.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def normal_polygon(polygon, unitized=True):
6767
nz += n[2]
6868

6969
if not unitized:
70-
return nx, ny, nz
70+
return [0.5 * nx, 0.5 * ny, 0.5 * nz]
7171

7272
return normalize_vector([nx, ny, nz])
7373

@@ -97,9 +97,9 @@ def normal_triangle(triangle, unitized=True):
9797
ac = subtract_vectors(c, a)
9898
n = cross_vectors(ab, ac)
9999
if not unitized:
100-
return n
101-
lvec = length_vector(n)
102-
return n[0] / lvec, n[1] / lvec, n[2] / lvec
100+
return [0.5 * n[0], 0.5 * n[1], 0.5 * n[2]]
101+
lvec = 1 / length_vector(n)
102+
return [lvec * n[0], lvec * n[1], lvec * n[2]]
103103

104104

105105
def normal_triangle_xy(triangle, unitized=True):
@@ -127,15 +127,6 @@ def normal_triangle_xy(triangle, unitized=True):
127127
ac = subtract_vectors_xy(c, a)
128128
n = cross_vectors_xy(ab, ac)
129129
if not unitized:
130-
return n
131-
lvec = length_vector_xy(n)
132-
return n[0] / lvec, n[1] / lvec, n[2] / lvec
133-
134-
135-
# ==============================================================================
136-
# Main
137-
# ==============================================================================
138-
139-
if __name__ == "__main__":
140-
141-
pass
130+
return [0.5 * n[0], 0.5 * n[1], 0]
131+
lvec = 1 / length_vector_xy(n)
132+
return [lvec * n[0], lvec * n[1], 0]

tests/compas/datastructures/test_mesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from compas.geometry import Box
1010
from compas.geometry import Polygon
1111
from compas.geometry import Translation
12+
from compas.geometry import allclose
1213

1314

1415
@pytest.fixture
@@ -585,7 +586,7 @@ def test_face_coordinates():
585586

586587
def test_face_normal():
587588
mesh = Mesh.from_obj(compas.get('quadmesh.obj'))
588-
assert mesh.face_normal(0) == [0.5435358481001584, -0.16248515023849733, 0.8235091728584537]
589+
assert allclose(mesh.face_normal(0), [0.5435358481001584, -0.16248515023849733, 0.8235091728584537], tol=1e-6)
589590

590591

591592
def test_face_centroid():

tests/compas/geometry/test_polygon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ def test___setitem__():
5252
polygon[4] = point
5353
assert polygon[4] == point
5454
assert isinstance(polygon[4], Point)
55-
assert polygon.lines[-2].end == point
55+
assert polygon.lines[-2].end == point

0 commit comments

Comments
 (0)