Skip to content

Commit

Permalink
Merge pull request #814 from compas-dev/area
Browse files Browse the repository at this point in the history
fixes #812
  • Loading branch information
tomvanmele authored Apr 20, 2021
2 parents 819de11 + c678168 commit 78b5b0b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed base object of `compas.datastructures.Datastructure` to `compas.data.Data`.
* Changed base object of `compas.geometry.Primitive` to `compas.data.Data`.
* Renamed `Base` to `Data` for all data based classes.
* Fixed calculation of triangle normals.
* Fixed calculation of triangle areas.

### Removed

Expand Down
10 changes: 4 additions & 6 deletions src/compas/geometry/_core/normals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from compas.geometry._core import cross_vectors
from compas.geometry._core import cross_vectors_xy
from compas.geometry._core import length_vector
from compas.geometry._core import length_vector_xy
from compas.geometry._core import normalize_vector

from compas.geometry._core import centroid_points
Expand Down Expand Up @@ -67,7 +66,7 @@ def normal_polygon(polygon, unitized=True):
nz += n[2]

if not unitized:
return [0.5 * nx, 0.5 * ny, 0.5 * nz]
return [nx, ny, nz]

return normalize_vector([nx, ny, nz])

Expand Down Expand Up @@ -97,7 +96,7 @@ def normal_triangle(triangle, unitized=True):
ac = subtract_vectors(c, a)
n = cross_vectors(ab, ac)
if not unitized:
return [0.5 * n[0], 0.5 * n[1], 0.5 * n[2]]
return n
lvec = 1 / length_vector(n)
return [lvec * n[0], lvec * n[1], lvec * n[2]]

Expand Down Expand Up @@ -127,6 +126,5 @@ def normal_triangle_xy(triangle, unitized=True):
ac = subtract_vectors_xy(c, a)
n = cross_vectors_xy(ab, ac)
if not unitized:
return [0.5 * n[0], 0.5 * n[1], 0]
lvec = 1 / length_vector_xy(n)
return [lvec * n[0], lvec * n[1], 0]
return n
return [0, 0, n[2] / length_vector(n)]
3 changes: 1 addition & 2 deletions src/compas/geometry/_core/size.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from compas.geometry._core import subtract_vectors
from compas.geometry._core import subtract_vectors_xy
from compas.geometry._core import length_vector
from compas.geometry._core import length_vector_xy
from compas.geometry._core import cross_vectors
from compas.geometry._core import cross_vectors_xy
from compas.geometry._core import dot_vectors
Expand Down Expand Up @@ -128,7 +127,7 @@ def area_triangle_xy(triangle):
The area of the triangle.
"""
return 0.5 * length_vector_xy(normal_triangle_xy(triangle, False))
return 0.5 * length_vector(normal_triangle_xy(triangle, False))


def volume_polyhedron(polyhedron):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from math import pi
from math import pi, radians, sqrt

import pytest

from compas.geometry import Polyhedron
from compas.geometry import Polygon
from compas.geometry import Rotation
from compas.geometry import allclose
from compas.geometry import angle_vectors
from compas.geometry import angle_planes
Expand All @@ -13,6 +15,26 @@
from compas.geometry import length_vector
from compas.geometry import subtract_vectors
from compas.geometry import volume_polyhedron
from compas.geometry import area_polygon
from compas.geometry import area_polygon_xy
from compas.geometry import area_triangle
from compas.geometry import area_triangle_xy


@pytest.fixture
def R():
return Rotation.from_axis_and_angle([0, 1, 0], radians(-90))


@pytest.fixture
def square():
return Polygon.from_sides_and_radius_xy(4, sqrt(0.5 ** 2 + 0.5 ** 2))


@pytest.fixture
def triangle():
return Polygon([[0, 0, 0], [1, 0, 0], [0, 1, 0]])


# ==============================================================================
# angles
Expand Down Expand Up @@ -156,3 +178,27 @@ def test_volume_polyhedron(polyhedron, volume):
volume = L * L * L
V = volume_polyhedron(polyhedron)
assert close(V, volume)


def test_area_square(square, R):
assert close(area_polygon(square.points), 1)
assert close(area_polygon_xy(square.points), 1)
assert close(square.area, 1)
square.transform(R)
assert close(area_polygon(square.points), 1)
assert close(area_polygon_xy(square.points), 0)
assert close(square.area, 1)


def test_area_triangle(triangle, R):
assert close(area_polygon(triangle.points), 0.5)
assert close(area_polygon_xy(triangle.points), 0.5)
assert close(area_triangle(triangle.points), 0.5)
assert close(area_triangle_xy(triangle.points), 0.5)
assert close(triangle.area, 0.5)
triangle.transform(R)
assert close(area_polygon(triangle.points), 0.5)
assert close(area_polygon_xy(triangle.points), 0.0)
assert close(area_triangle(triangle.points), 0.5)
assert close(area_triangle_xy(triangle.points), 0.0)
assert close(triangle.area, 0.5)

0 comments on commit 78b5b0b

Please sign in to comment.