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

feat: geometry objects with new structure #373

Open
wants to merge 5 commits into
base: v3-dev
Choose a base branch
from

Conversation

dogukankaratas
Copy link

This PR splits the geometry objects to different files under geometry/ folder.

image

Addition to that:

  • Helper class for transform added.
  • ITransformable interface added.
  • Manual test files removed and unit tests for all geometric objects added.

Copy link

linear bot commented Jan 14, 2025

@KatKatKateryna
Copy link
Contributor

Thing that came to my mind first: splitting classes to different files will make it behave not like C# (for dev experience), because in Python we cannot assign namespaces. So every import will look slightly redundant: from...object.geometry.point import Point, while in C# it can be just used as Speckle.Geomerty.Point

Not necessarily an issue, but an observation


raise IndexError(f"Face index {face_index} out of range")

def transform_to(self, transform) -> Tuple[bool, "Mesh"]:
Copy link
Contributor

@KatKatKateryna KatKatKateryna Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type hints would be helpful (here and in the other methods and classes, including interfaces)

@JR-Morgan
Copy link
Member

Re-exporting a module could be an option here.
I normally seek input from @gjedlicska on these sorts of design choices, he has a better grasp than I on how to craft a good DX around importing module.

Copy link
Contributor

@KatKatKateryna KatKatKateryna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another general observation, currently we do not calculate bbox on the objects. Should we or should we not, up to you or Gergo.

Overall, great work, I hope I helped to find some small blind spots 🙌

return total_length

@property
def _domain(self) -> Interval:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was domain made an internal property? both here and in ICurve interface

domain: Interval = field(default_factory=Interval.unit_interval)

@property
def length(self) -> float:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome 🚀

transform this point using the given transform
"""
m = transform.matrix
tx = self.x * m[0] + self.y * m[1] + self.z * m[2] + m[3]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we convert matrix to the objects' units first? Same for other geometry classes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a known bug in the c# implementation that we do this incorrectly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using an array of 16 values instead of matrix in c# for transform now? @JR-Morgan

return cls(x=coords[0], y=coords[1], z=coords[2], units=units)

@classmethod
def from_coords(cls, x: float, y: float, z: float, units: str | Units) -> "Point":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in it's current state, "from_coords" acts exactly like the default constructor (because x, y, z, units are already enforced). Should we still keep this method?


@property
def radius(self) -> float:
return self.startPoint.distance_to(self.plane.origin)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlikely event, but if the StartPoint and Plane have different units (nothing prevents the user to construct it), we might need to convert the distance to the Arc's units
Same with "measure" property

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will need to assume that an object never has mixed units. There's too much opportunity for undefined behaviour, and performance overheads if we start mixing units.

This fact sucks for DX, but I would pitch us moving away from using a our Point class, and using a light weight System.DoubleNumerics. Vector3 struct (dunno what a Python equivalent would be)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the less hardcore way would be to validate the units in the constructor, and in case of inconsistency (or null) throw an error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd not prefer to have mix units but there will be no errors anyway.

plane = Plane(
    origin=Point(x=0, y=0, z=0, units="mm"),
    normal=Vector(x=0, y=0, z=1, units="mm"),
    xdir=Vector(x=1, y=0, z=0, units="mm"),
    ydir=Vector(x=0, y=1, z=0, units="mm"),
    units="mm"
)

arc = Arc(
    plane=plane,
    startPoint=Point(x=1, y=0, z=0, units="m"),  # Different units
    midPoint=Point(x=0.7071, y=0.7071, z=0, units="m"),
    endPoint=Point(x=0, y=1, z=0, units="m"),
    units="m"
)

# This will work - distance_to() handles the unit conversion
radius = arc.radius  # Will be in meters since startPoint is in meters

The conversion logic will effect the performance though.

transformed_vertices = []

for i in range(0, len(self.vertices), 3):
point = Point(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use .vertices_count and .get_point here?

])

current_area = self._area
current_volume = self._volume
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 2 will need to be recalculated. Let's add a @Property "area" and "volume", even if just placeholders for now and use it here.

convert the mesh vertices to different units
"""
if isinstance(to_units, Units):
to_units = to_units.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the contrary, we can check if it's string instead (we will convert to Unit anyway in the next line)

colors: List[int] = field(default_factory=list)
textureCoordinates: List[float] = field(default_factory=list)
_area: float = field(default=0.0, init=False, repr=False)
_volume: float = field(default=0.0, init=False, repr=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe area and volume should only be calculatable properties instead

self._area *= scale_factor * scale_factor
self._volume *= scale_factor * scale_factor * scale_factor

def is_closed(self) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants