Commit 6963bcb2 authored by Dennis Gläser's avatar Dennis Gläser
Browse files

[python][vector] implement type deduction from args

parent 8d3ac4b2
from ._geometry import *
# Construct an n-dimensional point
############################################
# Argument-dependent n-d point construction
def Point(*args, **kwargs):
numArgs = len(args) + len(kwargs)
......@@ -22,3 +23,37 @@ def Point(*args, **kwargs):
# select point type from the number of arguments
if numArgs == 2: return Point_2(*args, **kwargs)
else: return Point_3(*args, **kwargs)
############################################
# Argument-dependent n-d vector construction
def Vector(*args, **kwargs):
numArgs = len(args) + len(kwargs)
if numArgs == 0:
raise Exception("Arguments needed for vector construction. For default-" + \
"constructible vector use the dimension-specific " + \
"implementations Vector_1, Vector_2 or Vector_3");
def makeVector(dim):
if dim == 1: return Vector_1(*args, **kwargs)
elif dim == 2: return Vector_2(*args, **kwargs)
else: return Vector_3(*args, **kwargs)
# single argument construction (either list or direction)
if numArgs == 1:
if isinstance(args[0], list):
dim = len(args[0])
else:
try: dim = args[0].worldDimension
except: raise Exception("Invalid argument provided for Vector construction")
return makeVector(dim)
# (maybe) construct from two points
if numArgs == 2:
try: dim = args[0].worldDimension
except: pass
else: return makeVector(dim)
# last option: construction from the raw coordinates
return makeVector(numArgs)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment