/primitives.py
Python | 158 lines | 89 code | 12 blank | 57 comment | 8 complexity | ebade3ead8c69b74bec9da22260a51a0 MD5 | raw file
- # Copyright (c) 2010 Galen Clark Haynes
- # 2010 Dexteris Robotics, LLC
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions are met:
- #
- # * Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # * Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- # * Neither the name of Dexteris Robotics, LLC nor the names of its
- # contributors may be used to endorse or promote products derived from
- # this software without specific prior written permission.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- # POSSIBILITY OF SUCH DAMAGE.
- from math import pi
- from forma.common import Forma
- from forma.transforms import Translation, Rotation
- class Polygon(Forma):
- """
- Generic list of points generating polygon on X-Y plane, provided as a 2xN array
- """
- def __init__(self,**kwargs):
- import numpy
- self.normal = [0.0,0.0,1.0];
- super(type(self),self).__init__(kwargs)
- if not hasattr(self,'points'):
- self.points = numpy.zeros([3,4])
- self.points[:,0] = [-1,1,0]
- self.points[:,1] = [-1,-1,0]
- self.points[:,2] = [1,-1,0]
- self.points[:,3] = [1,1,0]
- class Circle(Forma):
- """
- Circle on the X-Y plane.
- """
- def __init__(self,**kwargs):
- self.radius = 1.0
- self.arc = 2.0*pi
- super(type(self),self).__init__(kwargs)
- class Sphere(Forma):
- """
- Sphere centered at origin, poles along Z.
- """
- def __init__(self,**kwargs):
- self.radius = 1.0
- super(type(self),self).__init__(kwargs)
-
- class Cone(Forma):
- """
- Cone with base on the X-Y plane, height along positive Z.
- """
- def __init__(self,**kwargs):
- self.radius = 1.0
- self.height = 1.0
- super(type(self),self).__init__(kwargs)
- self.c = Circle(radius=self.radius)
- if hasattr(self,'color'):
- self.c.color = self.color
- self.attach(self.c)
-
- class Box(Forma):
- """
- Box with X,Y,Z correspond to width, length, and height.
- """
- def __init__(self,**kwargs):
- self.width = 1.0
- self.length = 1.0
- self.height = 1.0
- super(type(self),self).__init__(kwargs)
- class Cube(Forma):
- """
- A Box with equal sizes for each side.
- """
- def __init__(self,**kwargs):
- self.size = 1.0
- super(type(self),self).__init__(kwargs)
- b = Box(width=self.size,length=self.size,height=self.size)
- if hasattr(self,'color'):
- b.color = self.color
- self.attach(b)
- class Cylinder(Forma):
- """
- Cylinder on X-Y plane, with Z as height.
- """
- def __init__(self,**kwargs):
- self.radius = 1.0
- self.height = 1.0
- self.arc = 2.0*pi
- super(type(self),self).__init__(kwargs)
- class SolidCylinder(Forma):
- """
- Cylinder with capped ends.
- """
- def __init__(self,**kwargs):
- self.radius = 1.0
- self.height = 1.0
- self.arc = 2.0*pi
- super(type(self),self).__init__(kwargs)
- c = Cylinder(radius=self.radius,height=self.height,arc=self.arc)
- s = Circle(radius=self.radius,arc=self.arc)
- self.attach(c)
- self.attach(Translation(z=self.height/2).attach(s))
- self.attach(Translation(z=-self.height/2).\
- attach(Rotation(y=pi).\
- attach(s)))
- if hasattr(self,'color'):
- c.color = self.color
- s.color = self.color
-
- class LineTrace(Forma):
- """
- An object allow easy addition of data points to be traced
- """
- def __init__(self,**kwargs):
- self.t_trace = []
- self.x_trace = []
- self.y_trace = []
- self.z_trace = []
- super(type(self),self).__init__(kwargs)
-
- def add(self,x,y,z):
- if len(self.x_trace) == 0 or (self.x_trace[-1] != x and self.y_trace[-1] != y and self.z_trace[-1] != z):
- self.x_trace.append(x)
- self.y_trace.append(y)
- self.z_trace.append(z)
-
- class Label(Forma):
- """
- Text label at current origin or specified point given as (x,y,z) parameter set.
- """
- def __init__(self,**kwargs):
- self.text = ''
- self.x = 0.0
- self.y = 0.0
- self.z = 0.0
- self.color = [0,0,0]
- super(type(self),self).__init__(kwargs)
-