Skip to content

Commit

Permalink
Remove use of set() to get knots from knotvector
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-adir committed Oct 1, 2023
1 parent b77d001 commit fa30f72
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/compmec/nurbs/heavy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@


class ImmutableKnotVector(tuple):
@staticmethod
def __get_unique(vector: Tuple[float]):
unique = []
for node in vector:
for knot in unique:
if abs(node - knot) < 1e-6:
break
else:
unique.append(node)
unique.sort()
return tuple(unique)

@staticmethod
def __is_valid(vector: Tuple[float], degree: Union[int, None]):
try:
Expand All @@ -35,7 +47,7 @@ def __is_valid(vector: Tuple[float], degree: Union[int, None]):
npts = lenght - degree - 1
if not degree < npts:
return False
knots = tuple(set(vector[degree : npts + 1]))
knots = ImmutableKnotVector.__get_unique(vector[degree : npts + 1])
for knot in knots:
mult = vector.count(knot)
if mult > degree + 1:
Expand Down Expand Up @@ -75,7 +87,8 @@ def __or__(self, other: ImmutableKnotVector) -> ImmutableKnotVector:
other = ImmutableKnotVector(other)
if self.limits != other.limits:
raise ValueError
all_knots = list(set(self.knots) | set(other.knots))
all_knots = list(self.knots) + list(other.knots)
all_knots = ImmutableKnotVector.__get_unique(all_knots)
all_mults = [0] * len(all_knots)
for vector in [self, other]:
for knot in vector:
Expand Down Expand Up @@ -118,7 +131,8 @@ def npts(self) -> int:

@property
def knots(self) -> Tuple[float]:
return tuple(sorted(set(self[self.degree : self.npts + 1])))
vector = self[self.degree : self.npts + 1]
return ImmutableKnotVector.__get_unique(vector)

@property
def limits(self) -> Tuple[float]:
Expand Down
7 changes: 7 additions & 0 deletions src/compmec/nurbs/knotspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def __new__(cls, vector: Tuple[float], degree: Optional[int] = None):
instance.internal = vector
return instance

def __str__(self) -> str:
return "(" + ", ".join(map(str, self)) + ")"

def __repr__(self) -> str:
return str(self)
return f"KV({self.degree}, {self.npts})"

def __iter__(self):
for item in self.internal:
yield item
Expand Down
5 changes: 4 additions & 1 deletion tests/test_customstruc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, number: float):
self.internal = float(number)

def __hash__(self):
return hash(self.internal)
return id(self)

def __str__(self) -> str:
return str(self.internal)
Expand Down Expand Up @@ -95,6 +95,9 @@ def __ne__(self, other):
def __ge__(self, other):
return self.__eq__(other) or self.__gt__(other)

def __abs__(self):
return self.__class__(self.internal if self.internal > 0 else -self.internal)


class CustomPoint:
def __init__(self, value: float):
Expand Down

0 comments on commit fa30f72

Please sign in to comment.