Skip to content

Commit 6017ef9

Browse files
committed
Updated plane class. v0.1.8
1 parent 28941ed commit 6017ef9

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

gem/plane.py

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,55 @@ def flip(plane):
1212

1313
def normalize(pdata):
1414
''' Return the normalized plane.'''
15-
vec = vector.Vector(3, data=[pdata[0], pdata[1], pdata[2]])
15+
vec = vector.Vector(3, data=pdata)
1616
vecN = vec.normalize()
1717

1818
length = vecN.magnitude()
1919

2020
if length is not 0:
21-
return [vecN.vector[0], vecN.vector[1], vecN.vector[2], pdata[3] / length]
21+
return vecN.vector[0], vecN.vector[1], vecN.vector[2], pdata[3] / length
2222
else:
2323
print("Plane fail to normalize due to zero division.")
24-
return [0.0, 0.0, 0.0, 0.0]
24+
return 0.0, 0.0, 0.0, 0.0
2525

2626
class Plane(object):
27-
def __init__(self, *args):
28-
''' Define a plane using 3 vectors, return a 4 item 1D array. '''
29-
if isinstance(args[0], list):
30-
self.data = args[0]
31-
elif isinstance(args[0], vector.Vector):
32-
# Define using 3 vectors
33-
v1 = args[1] - args[0]
34-
v2 = args[2] - args[0]
35-
n = v1.cross(v2)
36-
n.i_normalize()
37-
self.data = [n.vector[0], n.vector[1], n.vector[2], (n * -1.0).dot(args[0])]
38-
else:
39-
self.data = [0.0, 0.0, 0.0, 0.0]
40-
27+
def __init__(self):
28+
''' Plane class constructor. '''
4129
self.normal = vector.Vector(3, data=[0.0, 0.0, 0.0])
30+
self.a = 0
31+
self.b = 0
32+
self.c = 0
33+
self.d = 0
34+
35+
def clone(self):
36+
'''Create a new Plane with similar propertise.'''
37+
nPlane = Plane()
38+
nPlane.normal = self.normal.clone()
39+
nPlane.a = self.a
40+
nPlane.b = self.b
41+
nPlane.c = self.c
42+
nPLane.d = self.d
43+
return nPlane
44+
45+
def fromCoeffs(self, a, b, c, d):
46+
''' Create the plane from A,B,C,D. '''
47+
self.a = a
48+
self.b = b
49+
self.c = c
50+
self.d = d
51+
self.normal = vector.cross(b - a, c - a).normalize()
52+
53+
def fromPoints(self, a, b, c):
54+
'''Calculate the plane from A,B,C.'''
55+
self.a = a
56+
self.b = b
57+
self.c = c
58+
self.normal = vector.cross(b - a, c - a).normalize()
59+
self.d = self.normal.dot(self.a)
4260

4361
def i_flip(self):
4462
''' Flip the plane in its place. '''
45-
data = flip(self)
63+
self.data = flip(self)
4664
self.a = data[0]
4765
self.b = data[1]
4866
self.c = data[2]
@@ -52,20 +70,31 @@ def i_flip(self):
5270

5371
def flip(self):
5472
''' Return a flipped plane. '''
55-
return Plane(flip(self))
73+
nPlane = Plane()
74+
data = flip(self)
75+
nPlane.a = data[0]
76+
nPlane.b = data[1]
77+
nPlane.c = data[2]
78+
nPlane.d = data[3]
79+
nPlane.normal = data[4]
80+
return nPlane
5681

5782
def dot(self, vec):
5883
''' Return the dot product between a plane and 4D vector. '''
59-
return self.data[0] * vec.vector[0] + self.data[1] * vec.vector[1] + self.data[2] * vec.vector[2] + self.data[3] * vec.vector[3]
84+
return self.a * vec.vector[0] + self.b * vec.vector[1] + self.c * vec.vector[2] + self.d * vec.vector[3]
6085

6186
def i_normalize(self):
6287
''' Normalize the vector in place. '''
63-
self.data = normalize(self.data)
88+
pdata = [self.a, self.b, self.c, self.d]
89+
self.a, self.b, self.c, self.d = normalize(pdata)
6490
return self
6591

6692
def normalize(self):
6793
''' Return the normalized plane.'''
68-
return Plane(normalize(self.data))
94+
nPlane = Plane().clone()
95+
pdata = [self.a, self.b, self.c, self.d]
96+
nPlane.a, nPlane.b, nPlane.c, nPlane.d = normalize(pdata)
97+
return nPlane
6998

7099
def bestFitNormal(self, vecList):
71100
''' Pass in a list of vectors to find the best fit normal. '''
@@ -84,11 +113,11 @@ def bestFitD(self, vecList, bestFitNormal):
84113
return val / len(vecList)
85114

86115
def point_location(self, plane, point):
87-
''' Returns the location of the point. '''
116+
''' Returns the location of the point. Point is a tuple. '''
88117
# If s > 0 then the point is on the same side as the normal. (front)
89118
# If s < 0 then the point is on the opposide side of the normal. (back)
90119
# If s = 0 then the point lies on the plane.
91-
s = plane.data[0] * point[0] + plane.data[1] * point[1] + plane.data[2] * point[2] + plane.data[3]
120+
s = plane.a * point[0] + plane.b * point[1] + plane.c * point[2] + plane.d
92121

93122
if s > 0:
94123
return 1

0 commit comments

Comments
 (0)