Skip to content

Commit

Permalink
Constraint removal complete
Browse files Browse the repository at this point in the history
  • Loading branch information
mlauer154 committed Feb 9, 2024
1 parent 7057b79 commit ffed468
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
42 changes: 31 additions & 11 deletions pymead/core/gcs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def __init__(self):
self.points = {}
self.roots = []

def add_point(self, point: Point):
def _add_point(self, point: Point):
point.gcs = self
self.add_node(point)
self.points[point.name()] = point

def remove_point(self, point: Point):
def _remove_point(self, point: Point):
self.remove_node(point)
self.points.pop(point.name())

Expand All @@ -27,7 +27,7 @@ def _check_if_constraint_creates_new_cluster(self, constraint: GeoCon):
return False
return True

def _set_distance_constraint_as_root(self, constraint: DistanceConstraint):
def _set_constraint_as_root(self, constraint: DistanceConstraint or RelAngle3Constraint or AntiParallel3Constraint or Perp3Constraint):
constraint.child_nodes[0].root = True
constraint.child_nodes[1].rotation_handle = True
if constraint.child_nodes[0] not in [r[0] for r in self.roots]:
Expand Down Expand Up @@ -114,8 +114,6 @@ def _discover_root_from_node(self, source_node: Point):

def _merge_clusters_with_constraint(self, constraint: GeoCon, unique_roots: typing.List[Point]):

print(f"Performing cluster merge! {unique_roots = }")

def _add_ghost_edges_to_constraint():
if isinstance(constraint, DistanceConstraint):
self.add_edge(constraint.p1, constraint.p2)
Expand Down Expand Up @@ -361,11 +359,13 @@ def add_constraint(self, constraint: GeoCon):
# Check if this constraint creates a new cluster
first_constraint_in_cluster = self._check_if_constraint_creates_new_cluster(constraint)

# If it does, make sure it is a distance constraint and set it as the root constraint
# If it does, set it as the root constraint
if first_constraint_in_cluster:
if not isinstance(constraint, DistanceConstraint):
raise ValueError("The first constraint in a constraint cluster must be a distance constraint")
self._set_distance_constraint_as_root(constraint)
if not isinstance(constraint, DistanceConstraint) or isinstance(
constraint, RelAngle3Constraint) or isinstance(
constraint, Perp3Constraint) or isinstance(
constraint, AntiParallel3Constraint):
self._set_constraint_as_root(constraint)

# If the constraint has a Param associated with it, pass the GCS reference to this parameter
if constraint.param() is not None:
Expand Down Expand Up @@ -424,18 +424,38 @@ def _remove_distance_constraint_from_directed_edge(self, constraint: DistanceCon
else:
self.remove_edge(constraint.p2, constraint.p1)

def _remove_angle_constraint_from_directed_edge(self, constraint: RelAngle3Constraint or AntiParallel3Constraint or Perp3Constraint):
def _remove_angle_constraint_from_directed_edge(self, constraint: RelAngle3Constraint
or AntiParallel3Constraint
or Perp3Constraint):
edge_data_21 = self.get_edge_data(constraint.p2, constraint.p1)
if edge_data_21 is not None and "angle" in edge_data_21.keys():
if "distance" in edge_data_21.keys():
edge_data_21.pop("angle")
else:
self.remove_edge(constraint.p2, constraint.p1)
pass

# Remove the ghost edge if there is one
edge_data_32 = self.get_edge_data(constraint.p3, constraint.p2)
if edge_data_32 is not None and len(edge_data_32) == 0:
self.remove_edge(constraint.p3, constraint.p2)
edge_data_23 = self.get_edge_data(constraint.p2, constraint.p3)
if edge_data_23 is not None and "angle" in edge_data_23.keys():
if "distance" in edge_data_23.keys():
edge_data_23.pop("angle")
else:
self.remove_edge(constraint.p2, constraint.p3)

# Remove the ghost edge if there is one
edge_data_12 = self.get_edge_data(constraint.p1, constraint.p2)
if edge_data_12 is not None and len(edge_data_12) == 0:
self.remove_edge(constraint.p1, constraint.p2)

def remove_constraint(self, constraint: GeoCon):
if isinstance(constraint, DistanceConstraint):
self._remove_distance_constraint_from_directed_edge(constraint)
elif isinstance(constraint, RelAngle3Constraint) or isinstance(
constraint, AntiParallel3Constraint) or isinstance(constraint, Perp3Constraint):
self._remove_angle_constraint_from_directed_edge(constraint)
else:
raise NotImplementedError(f"Constraint removal not yet implemented for constraint type {type(constraint)}")

Expand Down
4 changes: 2 additions & 2 deletions pymead/core/geometry_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def remove_pymead_obj(self, pymead_obj: PymeadObj, promotion_demotion: bool = Fa
for geo_con in pymead_obj.geo_cons[::-1]:
self.remove_pymead_obj(geo_con)

self.gcs.remove_point(pymead_obj)
self.gcs._remove_point(pymead_obj)

elif isinstance(pymead_obj, Dimension):
# Remove the dimension references from the points
Expand Down Expand Up @@ -471,7 +471,7 @@ def add_point(self, x: float, y: float, name: str or None = None, fixed: bool =
point.x().geo_col = self
point.y().geo_col = self
self.add_pymead_obj_by_ref(point, assign_unique_name=assign_unique_name)
self.gcs.add_point(point)
self.gcs._add_point(point)

return point

Expand Down

0 comments on commit ffed468

Please sign in to comment.