def left_hand(self, left_arm, colour, angle=0, part=Hand): # Displacement from left hand displacement = left_arm.position + left_arm.matrix * Vector(4, 17, -9) matrix = left_arm.matrix * Identity().rotate( 40, XAxis) * Identity().rotate(angle, ZAxis) piece = Piece(colour, displacement, matrix, part, self.group) self.pieces_info["left hand"] = piece return piece
def left_arm(self, colour, angle=0, part=ArmLeft): """ Displacement from torso """ displacement = self.matrix * Vector(15, 8, 0) piece = Piece( colour, self.position + displacement, self.matrix * Identity().rotate(-10, ZAxis) * Identity().rotate(angle, XAxis), part, self.group) self.pieces_info["left arm"] = piece return piece
def right_hand(self, right_arm, colour, angle=0, part=Hand): # Displacement from right arm displacement = right_arm.position + right_arm.matrix * Vector( -4, 17, -9) matrix = right_arm.matrix * Identity().rotate( 40, XAxis) * Identity().rotate(angle, ZAxis) piece = Piece(colour, displacement, matrix, part, self.group) self.pieces_info["right hand"] = piece return piece
def left_hand_item(self, left_hand, colour, displacement, angle=0, part=None): """ Displacement from left hand """ if not part: return None # Displacement from left hand displacement = left_hand.position + left_hand.matrix * displacement matrix = left_hand.matrix * Identity().rotate( 10, XAxis) * Identity().rotate(angle, YAxis) piece = Piece(colour, displacement, matrix, part, self.group) return piece
def right_hand_item(self, right_hand, colour, displacement, angle=0, part=None): """ Add a right hand item""" if not part: return None # Displacement from right hand displacement = right_hand.position + right_hand.matrix * displacement matrix = right_hand.matrix * Identity().rotate( 10, XAxis) * Identity().rotate(angle, YAxis) piece = Piece(colour, displacement, matrix, part, self.group) return piece
def write(self, model, current_matrix=Identity(), current_position=Vector(0, 0, 0), level=0): for obj in model.objects: if isinstance(obj, Piece): part = self.parts.part(code=obj.part) if part: matrix = obj.matrix self.write( part, current_matrix * matrix, current_position + current_matrix * obj.position, level + 1) else: sys.stderr.write("Part not found: %s\n" % obj.part) elif isinstance(obj, Triangle): p1 = current_matrix * obj.p1 + current_position p2 = current_matrix * obj.p2 + current_position p3 = current_matrix * obj.p3 + current_position if abs((p3 - p1).cross(p2 - p1)) != 0: self._write_triangle(p1, p2, p3) elif isinstance(obj, Quadrilateral): p1 = current_matrix * obj.p1 + current_position p2 = current_matrix * obj.p2 + current_position p3 = current_matrix * obj.p3 + current_position p4 = current_matrix * obj.p4 + current_position if abs((p3 - p1).cross(p2 - p1)) != 0: self._write_triangle(p1, p2, p3) if abs((p3 - p1).cross(p4 - p1)) != 0: self._write_triangle(p3, p4, p1)
def _polygons_from_objects(self, model, top_level_piece=None, current=Current(Identity(), White.code, Vector(0, 0, 0))): # Extract polygons from objects, filtering out those behind the camera. polygons = [] poly_handlers = { Piece: self._subpart_get_poly, Line: self._line_get_poly, Triangle: self._triangle_get_poly, Quadrilateral: self._quadrilateral_get_poly, } for obj in model.objects: if isinstance(obj, Piece) and obj.part == "LIGHT": continue try: args = (obj, top_level_piece or obj, current) poly = poly_handlers[type(obj)](*args) except KeyError: continue if poly: polygons.extend(poly) else: continue return polygons
def head(self, colour, angle=0, part=Head): """ Displacement from torso """ displacement = self.matrix * Vector(0, -24, 0) piece = Piece(colour, self.position + displacement, self.matrix * Identity().rotate(angle, YAxis), part, self.group) self.pieces_info["head"] = piece return piece
def __init__(self, position=Vector(0, 0, 0), matrix=Identity(), group=None): self.position = position self.matrix = matrix self.pieces_info = {} self.group = group
def right_leg(self, colour, angle=0, part=LegRight): """" Add a right leg""" displacement = self.matrix * Vector(0, 44, 0) piece = Piece(colour, self.position + displacement, self.matrix * Identity().rotate(angle, XAxis), part, self.group) self.pieces_info["right leg"] = piece return piece
def left_shoe(self, left_leg, colour, angle=0, part=None): """ Add a shoe on the left""" if not part: return None # Displacement from left leg displacement = left_leg.position + left_leg.matrix * Vector(10, 28, 0) matrix = left_leg.matrix * Identity().rotate(angle, YAxis) piece = Piece(colour, displacement, matrix, part, self.group) return piece
def test_add_piece(): group1 = Group() group2 = Group() piece = Piece(White, Vector(0, 0, 0), Identity(), Brick1X1, group=group1) assert piece in group1.pieces assert piece not in group2.pieces group2.add_piece(piece) assert piece in group2.pieces assert piece not in group1.pieces
from __future__ import print_function import random from ldraw.geometry import Identity, XAxis, YAxis, ZAxis, Vector from ldraw.figure import Person from ldraw.library.colours import Yellow, Light_Green, Blue, Red, Green from ldraw.library.parts.minifig.torsos import TorsoWithClassicSpacePattern random.seed(12345) for x in range(-100, 200, 100): for z in range(-100, 200, 100): orientation = Identity() orientation = orientation.rotate(random.randrange(0, 360), XAxis) orientation = orientation.rotate(random.randrange(0, 360), YAxis) orientation = orientation.rotate(random.randrange(0, 360), ZAxis) figure = Person(Vector(x, 0, z), orientation) print(figure.head(Yellow)) print(figure.torso(Light_Green, TorsoWithClassicSpacePattern)) print(figure.hips(Blue)) angle = random.randrange(-90, 60) print(figure.left_leg(Red, angle)) angle = random.randrange(-90, 60) print(figure.right_leg(Green, angle)) angle = random.randrange(-120, 60) print(figure.left_arm(Red, angle)) angle = random.randrange(-90, 90) print(figure.left_hand(Yellow, angle)) angle = random.randrange(-120, 60)
def test_matrix_rmul(): m = Identity().scale(1, 2, 3) v = Vector(3, 2, 1) mul = m * v assert mul == Vector(3, 4, 3)
def test_mulothers(): m = Identity() pytest.raises(MatrixError, lambda: m * 2) pytest.raises(MatrixError, lambda: 2 * m)
import sys from ldraw.colours import * from ldraw.figure import * from ldraw.geometry import Identity, XAxis from ldraw.parts import Parts from ldraw.pieces import Group, Piece if len(sys.argv) != 2: sys.stderr.write("Usage: %s <parts file>\n" % sys.argv[0]) sys.exit(1) parts = Parts(sys.argv[1]) group = Group(Vector(0, 0, -40), Identity()) figure = Person(group = group) figure.head(Yellow, part = parts.Heads["Head with Monocle, Scar, and Moustache Pattern"]) figure.hat(Black, parts.Hats["Top Hat"]) figure.torso(Black, parts.Torsos["Torso with Black Suit, Red Shirt, Gold Clasps Pattern"]) figure.left_arm(Black, 70) figure.left_hand(White, 0) figure.right_arm(Black, -30) figure.right_hand(White, 0) figure.right_hand_item(ChromeSilver, Vector(0, -58, -20), 0, parts.parts["Minifig Tool Magnifying Glass"]) figure.hips(Black) figure.left_leg(Black, 50) figure.right_leg(Black, -40) group.position = Vector(-100, 40, -120)
along with this program. If not, see <http://www.gnu.org/licenses/>. """ from __future__ import print_function from ldraw.figure import * from ldraw.geometry import Identity from ldraw.library.colours import * from ldraw.library.parts.arch import Arch1X6 from ldraw.library.parts.brick import Brick2X3, Brick1X1 from ldraw.library.parts.minifig.accessories import ToolMagnifyingGlass from ldraw.library.parts.minifig.hats import TopHat from ldraw.library.parts.minifig.heads import HeadWithMonocle_Scar_AndMoustachePattern from ldraw.library.parts.minifig.torsos import TorsoWithBlackSuit_RedShirt_GoldClaspsPattern from ldraw.library.parts.plate import Plate6X6 from ldraw.pieces import Group, Piece group = Group(Vector(0, 0, -40), Identity()) figure = Person(group=group) figure.head(Yellow, part=HeadWithMonocle_Scar_AndMoustachePattern) figure.hat(Black, TopHat) figure.torso(Black, TorsoWithBlackSuit_RedShirt_GoldClaspsPattern) figure.left_arm(Black, 70) figure.left_hand(White, 0) figure.right_arm(Black, -30) figure.right_hand(White, 0) figure.right_hand_item(Chrome_Silver, Vector(0, -58, -20), 0, ToolMagnifyingGlass) figure.hips(Black) figure.left_leg(Black, 50) figure.right_leg(Black, -40)
def _polygons_from_objects(self, model, top_level_piece = None, current_colour = 15, current_matrix = Identity(), current_position = Vector(0, 0, 0)): # Extract polygons from objects, filtering out those behind the camera. polygons = [] c = self.camera_position x, y, z = self.axes for obj in model.objects: if isinstance(obj, Piece): if obj.part == "LIGHT": continue colour = self._current_colour(obj.colour, current_colour) part = self.parts.part(code = obj.part) if part: matrix = obj.matrix polygons += self._polygons_from_objects( part, top_level_piece or obj, colour, current_matrix * matrix, current_position + current_matrix * obj.position) else: sys.stderr.write("Part not found: %s\n" % obj.part) elif isinstance(obj, Line): p1 = current_matrix * obj.p1 + current_position - c p2 = current_matrix * obj.p2 + current_position - c r1 = Vector(p1.dot(x), p1.dot(y), p1.dot(z)) r2 = Vector(p2.dot(x), p2.dot(y), p2.dot(z)) if r1.z >= 0 or r2.z >= 0: continue colour = self._current_colour(obj.colour, current_colour) polygons.append(Polygon(min(r1.z, r2.z), [r1, r2], colour, top_level_piece)) elif isinstance(obj, Triangle): p1 = current_matrix * obj.p1 + current_position - c p2 = current_matrix * obj.p2 + current_position - c p3 = current_matrix * obj.p3 + current_position - c if abs((p3 - p1).cross(p2 - p1)) == 0: continue r1 = Vector(p1.dot(x), p1.dot(y), p1.dot(z)) r2 = Vector(p2.dot(x), p2.dot(y), p2.dot(z)) r3 = Vector(p3.dot(x), p3.dot(y), p3.dot(z)) if r1.z >= 0 or r2.z >= 0 or r3.z >= 0: continue colour = self._current_colour(obj.colour, current_colour) polygons.append(Polygon(min(r1.z, r2.z, r3.z), [r1, r2, r3], colour, top_level_piece)) elif isinstance(obj, Quadrilateral): p1 = current_matrix * obj.p1 + current_position - c p2 = current_matrix * obj.p2 + current_position - c p3 = current_matrix * obj.p3 + current_position - c p4 = current_matrix * obj.p4 + current_position - c if abs((p3 - p1).cross(p2 - p1)) == 0: continue if abs((p3 - p1).cross(p4 - p1)) == 0: continue r1 = Vector(p1.dot(x), p1.dot(y), p1.dot(z)) r2 = Vector(p2.dot(x), p2.dot(y), p2.dot(z)) r3 = Vector(p3.dot(x), p3.dot(y), p3.dot(z)) r4 = Vector(p4.dot(x), p4.dot(y), p4.dot(z)) if r1.z >= 0 or r2.z >= 0 or r3.z >= 0 or r4.z >= 0: continue colour = self._current_colour(obj.colour, current_colour) polygons.append(Polygon(min(r1.z, r2.z, r3.z, r4.z), [r1, r2, r3, r4], colour, top_level_piece)) return polygons
def __init__(self, position=Vector(0, 0, 0), matrix=Identity()): self.position = position self.matrix = matrix self.pieces = []