1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import trigo
import math
import numpy as np
class Plane(object):
def __init__(self, a, b, c=None, d=None):
if c !=None :
if d != None :
self._planeFromABCD(a,b,c,d)
else :
self._planeFrom3Points(a,b,c)
else :
self._planeFromVectorPoint(a, b)
def _planeFromABCD(self, a, b,c,d):
self._plane=[a, b, c, d]
def _planeFrom3Points(self, a, b, c):
u=[a[i]- b[i] for i in range(len(a))]
v=[c[i]- b[i] for i in range(len(c))]
w=trigo.vdot(u,v)
return self.__planeFromVectorPoint(w, a)
def _planeFromVectorPoint(self, v, p):
self._plane=v+[-trigo.dot(v,p)]
def __str__(self):
plane=self._plane
return "<{:8.3f},{:8.3f},{:8.3f},{:8.3f}>".format(
plane[0], plane[1], plane[2], plane[3])
def normalize(self):
nn=self.norm()
return [self._plane[i]/nn for i in range(4)]
def norm(self):
from math import sqrt
return sqrt(sum([self._plane[i]*self._plane[i] for i in range(3)]))
def perpendicular(self):
return self._plane[:3]
def distance(self, point):
plane=self._plane
return plane[0]*point[0]+plane[1]*point[1]+plane[2]*point[2]+plane[3]
def check(self, points):
print "check distance to plane"
for v in points:
dist=self.distance(v)
print v, dist
if __name__ == "__main__":
p1=Plane([1,0,0], [1,1,1])
print "p1", p1
p=Plane(1,2,0,2)
print "p:", p
print "norme de p:", p.norm()
print "perpendiculaire a p:", p.perpendicular()
print "p normalise",p.normalize() |
Partager