-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from alphaville/feature/337-codegen-sphere2
Code generation support for Sphere2
- Loading branch information
Showing
7 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.7.1 | ||
0.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import casadi.casadi as cs | ||
import numpy as np | ||
from .constraint import Constraint | ||
import opengen.functions as fn | ||
|
||
|
||
class Sphere2(Constraint): | ||
"""A Euclidean sphere constraint | ||
A constraint of the form :math:`\|u-u_0\| = r`, where :math:`u_0` is the center | ||
of the ball and `r` is its radius | ||
""" | ||
|
||
def __init__(self, center=None, radius: float = 1.0): | ||
"""Constructor for a Euclidean sphere constraint | ||
:param center: center of the sphere; if this is equal to Null, the | ||
sphere is centered at the origin | ||
:param radius: radius of the sphere | ||
:return: New instance of Sphere2 with given center and radius | ||
""" | ||
if radius <= 0: | ||
raise Exception("The radius must be a positive number") | ||
|
||
if center is not None and not isinstance(center, (list, np.ndarray)): | ||
raise Exception("center is neither None nor a list nor np.ndarray") | ||
|
||
self.__center = None if center is None else np.array( | ||
[float(i) for i in center]) | ||
self.__radius = float(radius) | ||
|
||
@property | ||
def center(self): | ||
"""Returns the center of the ball""" | ||
return self.__center | ||
|
||
@property | ||
def radius(self): | ||
"""Returns the radius of the sphere""" | ||
return self.__radius | ||
|
||
def distance_squared(self, u): | ||
"""Computes the squared distance between a given point `u` and this sphere | ||
:param u: given point; can be a list of float, a numpy | ||
n-dim array (`ndarray`) or a CasADi SX/MX symbol | ||
:return: distance from set as a float or a CasADi symbol | ||
""" | ||
if fn.is_symbolic(u): | ||
# Case I: `u` is a CasADi SX symbol | ||
v = u if self.__center is None else u - self.__center | ||
elif (isinstance(u, list) and all(isinstance(x, (int, float)) for x in u))\ | ||
or isinstance(u, np.ndarray): | ||
if self.__center is None: | ||
v = u | ||
else: | ||
# Note: self.__center is np.ndarray (`u` might be a list) | ||
z = self.__center.reshape(len(u)) | ||
u = np.array(u).reshape(len(u)) | ||
v = np.subtract(u, z) | ||
else: | ||
raise Exception("u is of invalid type") | ||
|
||
return (self.__radius - fn.norm2(v))**2 | ||
|
||
def project(self, u): | ||
raise NotImplementedError() | ||
|
||
def is_convex(self): | ||
return False | ||
|
||
def is_compact(self): | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters