Skip to content

Commit

Permalink
Merge branch 'main' into fix-scene-consistency-and-serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Jun 7, 2024
2 parents 781f5a8 + 8d40e38 commit d4ad97e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added implementation of `compute_vertices`, `compute_edges`, `compute_faces` to `compas.geometry.Sphere`.
* Added implementation of `compute_vertices`, `compute_edges`, `compute_faces` to `compas.geometry.Torus`.
* Added `compas_blender.scene.ShapeObject`.
* Added `compas.geometry.vector.__radd__`.
* Added `compas.geometry.vector.__rsub__`.
* Added `compas.geometry.vector.__rmul__`.
* Added `compas.geometry.vector.__rtruediv__`.

### Changed

Expand All @@ -38,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed `draw_vertices`, `draw_edges` and `draw_faces` of `compas_blender.scene.MeshObject`, `compas_ghpython.scene.MeshObject`, and `compas_rhino.scene.MeshObject` to use only attributes instead of parameters.
* Changed `draw_vertices`, `draw_edges` and `draw_faces` of `compas_blender.scene.VolMeshObject`, `compas_ghpython.scene.VolMeshObject`, and `compas_rhino.scene.VolMeshObject` to use only attributes instead of parameters.
* Changed registration of `Capsule`, `Cone`, `Cylinder`, `Sphere`, `Torus` to `ShapeObject` in `compas_blender.scene`.
* Updated `compas.geometry.vector.__mul__` to allow element-wise multiplication with another vector.
* Updated `compas.geometry.vector.__truediv__` to allow element-wise division with another vector.

### Removed

Expand Down
44 changes: 39 additions & 5 deletions src/compas/geometry/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,25 @@ def __add__(self, other):
def __sub__(self, other):
return Vector(self.x - other[0], self.y - other[1], self.z - other[2])

def __mul__(self, n):
return Vector(self.x * n, self.y * n, self.z * n)

def __truediv__(self, n):
return Vector(self.x / n, self.y / n, self.z / n)
def __mul__(self, other):
if isinstance(other, (int, float)):
return Vector(self.x * other, self.y * other, self.z * other)

try:
other = Vector(*other)
return Vector(self.x * other.x, self.y * other.y, self.z * other.z)
except TypeError:
raise TypeError("Cannot cast {} {} to Vector".format(other, type(other)))

def __truediv__(self, other):
if isinstance(other, (int, float)):
return Vector(self.x / other, self.y / other, self.z / other)

try:
other = Vector(*other)
return Vector(self.x / other.x, self.y / other.y, self.z / other.z)
except TypeError:
raise TypeError("Cannot cast {} {} to Vector".format(other, type(other)))

def __pow__(self, n):
return Vector(self.x**n, self.y**n, self.z**n)
Expand Down Expand Up @@ -190,6 +204,26 @@ def __ipow__(self, n):
self.z **= n
return self

def __rmul__(self, n):
return self.__mul__(n)

def __radd__(self, other):
return self.__add__(other)

def __rsub__(self, other):
try:
other = Vector(*other)
return other - self
except TypeError:
raise TypeError("Cannot cast {} {} to Vector".format(other, type(other)))

def __rtruediv__(self, other):
try:
other = Vector(*other)
return other / self
except TypeError:
raise TypeError("Cannot cast {} {} to Vector".format(other, type(other)))

# ==========================================================================
# Properties
# ==========================================================================
Expand Down
35 changes: 33 additions & 2 deletions tests/compas/geometry/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,42 @@ def test_vector2(x, y):
def test_vector_operators():
a = Vector(random(), random(), random())
b = Vector(random(), random(), random())
assert a + b == [a.x + b.x, a.y + b.y, a.z + b.z]
assert a - b == [a.x - b.x, a.y - b.y, a.z - b.z]
c = [random(), random(), random()]

assert a * 2 == [a.x * 2, a.y * 2, a.z * 2]
assert a / 2 == [a.x / 2, a.y / 2, a.z / 2]
assert a**3 == [a.x**3, a.y**3, a.z**3]
assert 2 * a == [2 * a.x, 2 * a.y, 2 * a.z]

assert a + b == [a.x + b.x, a.y + b.y, a.z + b.z]
assert a - b == [a.x - b.x, a.y - b.y, a.z - b.z]
assert a * b == [a.x * b.x, a.y * b.y, a.z * b.z]
assert a / b == [a.x / b.x, a.y / b.y, a.z / b.z]

assert b + a == [a.x + b.x, a.y + b.y, a.z + b.z]
assert b - a == [b.x - a.x, b.y - a.y, b.z - a.z]
assert b * a == [a.x * b.x, a.y * b.y, a.z * b.z]
assert b / a == [b.x / a.x, b.y / a.y, b.z / a.z]

assert a * c == [a.x * c[0], a.y * c[1], a.z * c[2]]
assert c * a == [a.x * c[0], a.y * c[1], a.z * c[2]]
assert a + c == [a.x + c[0], a.y + c[1], a.z + c[2]]
assert a - c == [a.x - c[0], a.y - c[1], a.z - c[2]]

assert c * a == [a.x * c[0], a.y * c[1], a.z * c[2]]
assert c / a == [c[0] / a.x, c[1] / a.y, c[2] / a.z]
assert c + a == [a.x + c[0], a.y + c[1], a.z + c[2]]
assert c - a == [c[0] - a.x, c[1] - a.y, c[2] - a.z]

with pytest.raises(TypeError) as exc_info:
a / "wrong type"
if not compas.IPY:
assert str(exc_info.value) == "Cannot cast wrong type <class 'str'> to Vector"

with pytest.raises(TypeError) as exc_info:
a * "wrong type"
if not compas.IPY:
assert str(exc_info.value) == "Cannot cast wrong type <class 'str'> to Vector"


def test_vector_equality():
Expand Down

0 comments on commit d4ad97e

Please sign in to comment.