Model.py 4.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2008-2010 Lode Leroy
Copyright 2010 Lars Kruse <devel@sumpfralle.de>

This file is part of PyCAM.

PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PyCAM.  If not, see <http://www.gnu.org/licenses/>.
"""

lode_leroy's avatar
lode_leroy committed
24 25 26 27 28 29
from utils import *
from Point import *
from Line import *
from Triangle import *

try:
lode_leroy's avatar
lode_leroy committed
30 31
    import OpenGL.GL as GL
    GL_enabled = True
lode_leroy's avatar
lode_leroy committed
32
except:
lode_leroy's avatar
lode_leroy committed
33
    GL_enabled = False
lode_leroy's avatar
lode_leroy committed
34 35 36 37 38 39 40 41 42

class Model:
    id = 0

    def __init__(self):
        self.id = Model.id
        Model.id += 1
        self._triangles = []
        self.name = "model%d" % self.id
lode_leroy's avatar
lode_leroy committed
43 44 45 46 47 48 49
        self.minx = None
        self.miny = None
        self.minz = None
        self.maxx = None
        self.maxy = None
        self.maxz = None
        self._maxsize = None
lode_leroy's avatar
lode_leroy committed
50 51

    def to_OpenGL(self):
lode_leroy's avatar
lode_leroy committed
52 53
        if not GL_enabled:
            return
lode_leroy's avatar
lode_leroy committed
54
        if True:
lode_leroy's avatar
lode_leroy committed
55
            GL.glBegin(GL.GL_TRIANGLES)
lode_leroy's avatar
lode_leroy committed
56
            for t in self._triangles:
lode_leroy's avatar
lode_leroy committed
57 58 59 60
                GL.glVertex3f(t.p1.x, t.p1.y, t.p1.z)
                GL.glVertex3f(t.p2.x, t.p2.y, t.p2.z)
                GL.glVertex3f(t.p3.x, t.p3.y, t.p3.z)
            GL.glEnd()
lode_leroy's avatar
lode_leroy committed
61 62 63 64
        else:
            for t in self._triangles:
                t.to_OpenGL()

lode_leroy's avatar
lode_leroy committed
65 66
    def _update_limits(self, t):
        if self.minx is None:
67 68 69 70 71 72 73
            self.minx = t.minx()
            self.miny = t.miny()
            self.minz = t.minz()
            self.maxx = t.maxx()
            self.maxy = t.maxy()
            self.maxz = t.maxz()
        else:
lode_leroy's avatar
lode_leroy committed
74 75 76 77 78
            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())
79 80
            self.maxz = max(self.maxz, t.maxz())

lode_leroy's avatar
lode_leroy committed
81 82
    def append(self, t):
        self._update_limits(t)
lode_leroy's avatar
lode_leroy committed
83 84
        self._triangles.append(t)

85
    def maxsize(self):
lode_leroy's avatar
lode_leroy committed
86
        if self._maxsize is None:
87 88 89
            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

lode_leroy's avatar
lode_leroy committed
90 91 92 93 94
    def triangles(self, minx=-INFINITE,miny=-INFINITE,minz=-INFINITE,maxx=+INFINITE,maxy=+INFINITE,maxz=+INFINITE):
        if minx==-INFINITE and miny==-INFINITE and minz==-INFINITE and maxx==+INFINITE and maxy==+INFINITE and maxz==+INFINITE:
            return self._triangles
        if hasattr(self, "t_kdtree"):
            return self.t_kdtree.Search(minx,maxx,miny,maxy)
lode_leroy's avatar
lode_leroy committed
95 96 97 98 99 100 101 102 103
        return self._triangles

    def subdivide(self, depth):
        model = Model()
        for t in self._triangles:
            for s in t.subdivide(depth):
                model.append(s)
        return model

104
    def reset_cache(self):
lode_leroy's avatar
lode_leroy committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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
127 128
            tr.reset_cache()
        self.reset_cache()
lode_leroy's avatar
lode_leroy committed
129