@@ -12,37 +12,55 @@ def flip(plane):
12
12
13
13
def normalize (pdata ):
14
14
''' Return the normalized plane.'''
15
- vec = vector .Vector (3 , data = [ pdata [ 0 ], pdata [ 1 ], pdata [ 2 ]] )
15
+ vec = vector .Vector (3 , data = pdata )
16
16
vecN = vec .normalize ()
17
17
18
18
length = vecN .magnitude ()
19
19
20
20
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
22
22
else :
23
23
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
25
25
26
26
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. '''
41
29
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 )
42
60
43
61
def i_flip (self ):
44
62
''' Flip the plane in its place. '''
45
- data = flip (self )
63
+ self . data = flip (self )
46
64
self .a = data [0 ]
47
65
self .b = data [1 ]
48
66
self .c = data [2 ]
@@ -52,20 +70,31 @@ def i_flip(self):
52
70
53
71
def flip (self ):
54
72
''' 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
56
81
57
82
def dot (self , vec ):
58
83
''' 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 ]
60
85
61
86
def i_normalize (self ):
62
87
''' 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 )
64
90
return self
65
91
66
92
def normalize (self ):
67
93
''' 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
69
98
70
99
def bestFitNormal (self , vecList ):
71
100
''' Pass in a list of vectors to find the best fit normal. '''
@@ -84,11 +113,11 @@ def bestFitD(self, vecList, bestFitNormal):
84
113
return val / len (vecList )
85
114
86
115
def point_location (self , plane , point ):
87
- ''' Returns the location of the point. '''
116
+ ''' Returns the location of the point. Point is a tuple. '''
88
117
# If s > 0 then the point is on the same side as the normal. (front)
89
118
# If s < 0 then the point is on the opposide side of the normal. (back)
90
119
# 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
92
121
93
122
if s > 0 :
94
123
return 1
0 commit comments