Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector primitives #18

Merged
merged 34 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bf0b6a7
Add 3D vector
Revathyvenugopal162 Aug 31, 2022
b90a6c8
Add properties for vector 3D
Revathyvenugopal162 Aug 31, 2022
2750a9d
Add typecasting in vector
Revathyvenugopal162 Aug 31, 2022
e205560
Add numpy as dependency
Revathyvenugopal162 Aug 31, 2022
3cfb019
Add vectorUV module
Revathyvenugopal162 Aug 31, 2022
eae4d68
Modify the 2D vector
Revathyvenugopal162 Aug 31, 2022
b1fae83
Add docstrings
Revathyvenugopal162 Aug 31, 2022
b41c470
Apply suggestions from code review
Revathyvenugopal162 Aug 31, 2022
0efd2a9
Update src/ansys/geometry/core/primitives/vector3D.py
Revathyvenugopal162 Aug 31, 2022
6dd967b
Add single vector module
Revathyvenugopal162 Aug 31, 2022
15129f5
Merge branch 'main' into feat/add-vector-primitives
Revathyvenugopal162 Aug 31, 2022
4173348
Add single vector module
Revathyvenugopal162 Aug 31, 2022
5eb9c5a
Add single vector module
Revathyvenugopal162 Aug 31, 2022
97b1c43
Merge branch 'main' into feat/add-vector-primitives
Revathyvenugopal162 Aug 31, 2022
c3db9f9
Add type error
Revathyvenugopal162 Aug 31, 2022
9028af7
Add docstrings
Revathyvenugopal162 Aug 31, 2022
b7770b1
Add basic test
Revathyvenugopal162 Aug 31, 2022
226c2ca
Modify basic test
Revathyvenugopal162 Aug 31, 2022
999ef85
Add test for normalize
Revathyvenugopal162 Aug 31, 2022
562d93a
Apply suggestions from code review
Revathyvenugopal162 Aug 31, 2022
c25e10b
Update src/ansys/geometry/core/primitives/vector.py
RobPasMue Aug 31, 2022
274c52b
UnitVectorXD proper implementation
RobPasMue Aug 31, 2022
1dcb3c8
Pre-commit check
RobPasMue Aug 31, 2022
1a1928d
Removing FirectionXD objects
RobPasMue Aug 31, 2022
0b84555
Pre-commit checks
RobPasMue Aug 31, 2022
0b0fe5d
Flexible metadata test
RobPasMue Aug 31, 2022
b5d431c
Add unitvector test
Revathyvenugopal162 Aug 31, 2022
aefb6a3
Pre-commit checks
RobPasMue Aug 31, 2022
daf62ce
Typo
RobPasMue Aug 31, 2022
9d434ff
Reordering and typo
RobPasMue Aug 31, 2022
1c34620
Pre-commit
RobPasMue Aug 31, 2022
4278806
Add error test for vector
Revathyvenugopal162 Aug 31, 2022
5ca8c59
Change test order
RobPasMue Aug 31, 2022
a1273bd
Check norm error
RobPasMue Aug 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ packages = [
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
importlib-metadata = {version = "^4.0", python = "<3.8"}
numpy = ">=1.20.3"

RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
# Optional dependencies
ansys-sphinx-theme = {version = "==0.5.2", optional = true}
Expand Down
4 changes: 3 additions & 1 deletion src/ansys/geometry/core/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

from ansys.geometry.core.primitives.direction import Direction
from ansys.geometry.core.primitives.point3D import Point3D
from ansys.geometry.core.primitives.vector3D import Vector3D
from ansys.geometry.core.primitives.vectorUV import VectorUV

__all__ = ["Direction", "Point3D"]
__all__ = ["Direction", "Point3D", "Vector3D", "VectorUV"]
69 changes: 69 additions & 0 deletions src/ansys/geometry/core/primitives/vector3D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""``Vector3D`` class module"""
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
import numpy as np


class Vector3D(np.ndarray):
"""Provides 3D vector class.

Parameters
----------
array_input : np.ndarray
One dimensional numpy.ndarray with shape(3,)
"""

def __new__(cls, array_input):
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
"""Constructor for Vector3D"""
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved

vector = np.asarray(array_input).view(cls)

if len(vector) != 3:
raise ValueError("Vector3D must have three coordinates.")

if not np.issubdtype(vector.dtype, np.number) or not all(
isinstance(data, (int, float)) for data in vector.data
):
raise ValueError("The parameters of 'array_inputs' should be integer or float.")

return vector

@property
def x(self) -> float:
"""X coordinate of Vector3D"""
return self[0]

@x.setter
def x(self, value: float) -> None:
self[0] = value
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

@property
def y(self) -> float:
"""Y coordinate of Vector3D"""
return self[1]

@y.setter
def y(self, value: float) -> None:
self[1] = value

@property
def z(self) -> float:
"""Z coordinate of Vector3D"""
return self[2]

@z.setter
def z(self, value: float) -> None:
self[2] = value

@property
def norm(self):
norm = np.linalg.norm(self)
if norm == 0:
norm = np.finfo(self.dtype).eps
return norm

def normalize(self):
"""Return a normalized version of the vector"""
return self / self.norm
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved

def cross(self, v: "Vector3D") -> "Vector3D":
"""Return cross product of vector"""
return Vector3D(np.cross(self, v))
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
54 changes: 54 additions & 0 deletions src/ansys/geometry/core/primitives/vectorUV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np


class VectorUV(np.ndarray):
Revathyvenugopal162 marked this conversation as resolved.
Show resolved Hide resolved
"""A two-dimensional vector with Cartesian coordinates.

Parameters
----------
array_input : np.ndarray
One dimensional numpy.ndarray with shape(2,)
"""

def __new__(cls, array_input):

vector = np.asarray(array_input).view(cls)

if len(vector) != 2:
raise ValueError("VectorUV must have two coordinates.")

if not np.issubdtype(vector.dtype, np.number) or not all(
isinstance(data, (int, float)) for data in vector.data
):
raise ValueError("The parameters of 'array_input' should be integer or float.")

return vector

@property
def x(self) -> float:
"""Returns X coordinate of VectorUV"""
return self[0]

@x.setter
def x(self, value: float) -> None:
self[0] = value

@property
def y(self) -> float:
"""Returns Y coordinate of VectorUV"""
return self[1]

@y.setter
def y(self, value: float) -> None:
self[1] = value

@property
def norm(self):
norm = np.linalg.norm(self)
if norm == 0:
norm = np.finfo(self.dtype).eps
return norm

def normalize(self):
"""Return a normalized version of the vector"""
return self / self.norm