Commit bde8fc53 authored by lode_leroy's avatar lode_leroy

cleanup

git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@75 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 2b46eaa4
...@@ -17,6 +17,13 @@ class Model: ...@@ -17,6 +17,13 @@ class Model:
Model.id += 1 Model.id += 1
self._triangles = [] self._triangles = []
self.name = "model%d" % self.id self.name = "model%d" % self.id
self.minx = None
self.miny = None
self.minz = None
self.maxx = None
self.maxy = None
self.maxz = None
self._maxsize = None
def to_OpenGL(self): def to_OpenGL(self):
if not GL_enabled: if not GL_enabled:
...@@ -32,41 +39,28 @@ class Model: ...@@ -32,41 +39,28 @@ class Model:
for t in self._triangles: for t in self._triangles:
t.to_OpenGL() t.to_OpenGL()
def append(self, t): def _update_limits(self, t):
if not hasattr(self,"minx"): if self.minx is None:
self.minx = t.minx() self.minx = t.minx()
else:
self.minx = min(self.minx, t.minx())
if not hasattr(self,"miny"):
self.miny = t.miny() self.miny = t.miny()
else:
self.miny = min(self.miny, t.miny())
if not hasattr(self,"minz"):
self.minz = t.minz() self.minz = t.minz()
else:
self.minz = min(self.minz, t.minz())
if not hasattr(self,"maxx"):
self.maxx = t.maxx() self.maxx = t.maxx()
else:
self.maxx = max(self.maxx, t.maxx())
if not hasattr(self,"maxy"):
self.maxy = t.maxy() self.maxy = t.maxy()
else:
self.maxy = max(self.maxy, t.maxy())
if not hasattr(self,"maxz"):
self.maxz = t.maxz() self.maxz = t.maxz()
else: else:
self.minx = min(self.minx, t.minx())
self.miny = min(self.miny, t.miny())
self.minz = min(self.minz, t.minz())
self.maxx = max(self.maxx, t.maxx())
self.maxy = max(self.maxy, t.maxy())
self.maxz = max(self.maxz, t.maxz()) self.maxz = max(self.maxz, t.maxz())
def append(self, t):
self._update_limits(t)
self._triangles.append(t) self._triangles.append(t)
def maxsize(self): def maxsize(self):
if not hasattr(self,"_maxsize"): if self._maxsize is None:
self._maxsize = max3(max(abs(self.maxx),abs(self.minx)),max(abs(self.maxy),abs(self.miny)),max(abs(self.maxz),abs(self.minz))) self._maxsize = max3(max(abs(self.maxx),abs(self.minx)),max(abs(self.maxy),abs(self.miny)),max(abs(self.maxz),abs(self.minz)))
return self._maxsize return self._maxsize
...@@ -84,4 +78,29 @@ class Model: ...@@ -84,4 +78,29 @@ class Model:
model.append(s) model.append(s)
return model return model
def reset_cache(self):
self.minx = None
self.miny = None
self.minz = None
self.maxx = None
self.maxy = None
self.maxz = None
for t in self._triangles:
self._update_limits(t)
self._maxsize = None
def transform(self, matrix):
processed = []
for tr in self._triangles:
for point in (tr.p1, tr.p2, tr.p3):
if not point.id in processed:
processed.append(point.id)
x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3]
y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3]
point.x = x
point.y = y
point.z = z
tr.reset_cache()
self.reset_cache()
...@@ -32,6 +32,18 @@ class Triangle: ...@@ -32,6 +32,18 @@ class Triangle:
self.e3 = Line(p3,p1) self.e3 = Line(p3,p1)
else: else:
self.e3 = e3 self.e3 = e3
self._minx = None
self._miny = None
self._minz = None
self._maxx = None
self._maxy = None
self._maxz = None
self._center = None
self._middle = None
self._radius = None
self._radiussq = None
self._normal = None
self._plane = None
def __repr__(self): def __repr__(self):
return "Triangle%d<%s,%s,%s>" % (self.id,self.p1,self.p2,self.p3) return "Triangle%d<%s,%s,%s>" % (self.id,self.p1,self.p2,self.p3)
...@@ -111,7 +123,7 @@ class Triangle: ...@@ -111,7 +123,7 @@ class Triangle:
GL.glColor4f(cc[0],cc[1],cc[2],cc[3]) GL.glColor4f(cc[0],cc[1],cc[2],cc[3])
def normal(self): def normal(self):
if not hasattr(self, '_normal'): if self._normal is None:
# calculate normal, if p1-p2-pe are in clockwise order # calculate normal, if p1-p2-pe are in clockwise order
self._normal = self.p3.sub(self.p1).cross(self.p2.sub(self.p1)) self._normal = self.p3.sub(self.p1).cross(self.p2.sub(self.p1))
denom = self._normal.norm() denom = self._normal.norm()
...@@ -119,7 +131,7 @@ class Triangle: ...@@ -119,7 +131,7 @@ class Triangle:
return self._normal return self._normal
def plane(self): def plane(self):
if not hasattr(self, '_plane'): if self._plane is None:
self._plane=Plane(self.center(), self.normal()) self._plane=Plane(self.center(), self.normal())
return self._plane return self._plane
...@@ -149,64 +161,62 @@ class Triangle: ...@@ -149,64 +161,62 @@ class Triangle:
return ((u * denom) >= 0) and ((v * denom) >= 0) and (u + v <= denom) return ((u * denom) >= 0) and ((v * denom) >= 0) and (u + v <= denom)
def minx(self): def minx(self):
if not hasattr(self, "_minx"): if self._minx is None:
self._minx = min3(self.p1.x, self.p2.x, self.p3.x) self._minx = min3(self.p1.x, self.p2.x, self.p3.x)
return self._minx return self._minx
def miny(self): def miny(self):
if not hasattr(self, "_miny"): if self._miny is None:
self._miny = min3(self.p1.y, self.p2.y, self.p3.y) self._miny = min3(self.p1.y, self.p2.y, self.p3.y)
return self._miny return self._miny
def minz(self): def minz(self):
if not hasattr(self, "_minz"): if self._minz is None:
self._minz = min3(self.p1.z, self.p2.z, self.p3.z) self._minz = min3(self.p1.z, self.p2.z, self.p3.z)
return self._minz return self._minz
def maxx(self): def maxx(self):
if not hasattr(self, "_maxx"): if self._maxx is None:
self._maxx = max3(self.p1.x, self.p2.x, self.p3.x) self._maxx = max3(self.p1.x, self.p2.x, self.p3.x)
return self._maxx return self._maxx
def maxy(self): def maxy(self):
if not hasattr(self, "_maxy"): if self._maxy is None:
self._maxy = max3(self.p1.y, self.p2.y, self.p3.y) self._maxy = max3(self.p1.y, self.p2.y, self.p3.y)
return self._maxy return self._maxy
def maxz(self): def maxz(self):
if not hasattr(self, "_maxz"): if self._maxz is None:
self._maxz = max3(self.p1.z, self.p2.z, self.p3.z) self._maxz = max3(self.p1.z, self.p2.z, self.p3.z)
return self._maxz return self._maxz
def center(self): def center(self):
if not hasattr(self, "_center"): if self._center is None:
self._center = self.p1.add(self.p2).add(self.p3).mul(1.0/3) self._center = self.p1.add(self.p2).add(self.p3).mul(1.0/3)
return self._center return self._center
def middle(self): def middle(self):
if not hasattr(self, "_middle"): if self._middle is None:
self.calc_circumcircle() self.calc_circumcircle()
return self._middle return self._middle
def radius(self): def radius(self):
if not hasattr(self, "_radius"): if self._radius is None:
self.calc_circumcircle() self.calc_circumcircle()
return self._radius return self._radius
def radiussq(self): def radiussq(self):
if not hasattr(self, "_radiussq"): if self._radiussq is None:
self.calc_circumcircle() self.calc_circumcircle()
return self._radiussq return self._radiussq
def calc_circumcircle(self): def calc_circumcircle(self):
normal = self.p2.sub(self.p1).cross(self.p3.sub(self.p2)) normal = self.normal()
denom = normal.norm() self._radius = (self.p2.sub(self.p1).norm()*self.p3.sub(self.p2).norm()*self.p3.sub(self.p1).norm())/(2)
self._radius = (self.p2.sub(self.p1).norm()*self.p3.sub(self.p2).norm()*self.p3.sub(self.p1).norm())/(2*denom)
self._radiussq = self._radius*self._radius self._radiussq = self._radius*self._radius
denom2 = 2*denom*denom; alpha = self.p3.sub(self.p2).normsq()*(self.p1.sub(self.p2).dot(self.p1.sub(self.p3))) / 2
alpha = self.p3.sub(self.p2).normsq()*(self.p1.sub(self.p2).dot(self.p1.sub(self.p3))) / (denom2) beta = self.p1.sub(self.p3).normsq()*(self.p2.sub(self.p1).dot(self.p2.sub(self.p3))) / 2
beta = self.p1.sub(self.p3).normsq()*(self.p2.sub(self.p1).dot(self.p2.sub(self.p3))) / (denom2) gamma = self.p1.sub(self.p2).normsq()*(self.p3.sub(self.p1).dot(self.p3.sub(self.p2))) / 2
gamma = self.p1.sub(self.p2).normsq()*(self.p3.sub(self.p1).dot(self.p3.sub(self.p2))) / (denom2)
self._middle = Point(self.p1.x*alpha+self.p2.x*beta+self.p3.x*gamma, self._middle = Point(self.p1.x*alpha+self.p2.x*beta+self.p3.x*gamma,
self.p1.y*alpha+self.p2.y*beta+self.p3.y*gamma, self.p1.y*alpha+self.p2.y*beta+self.p3.y*gamma,
self.p1.z*alpha+self.p2.z*beta+self.p3.z*gamma) self.p1.z*alpha+self.p2.z*beta+self.p3.z*gamma)
...@@ -225,3 +235,16 @@ class Triangle: ...@@ -225,3 +235,16 @@ class Triangle:
sub += Triangle(p4,self.p2,p5).subdivide(depth-1) sub += Triangle(p4,self.p2,p5).subdivide(depth-1)
return sub return sub
def reset_cache(self):
self._minx = None
self._miny = None
self._minz = None
self._maxx = None
self._maxy = None
self._maxz = None
self._center = None
self._middle = None
self._radius = None
self._radiussq = None
self._normal = None
self._plane = None
Markdown is supported
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