PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/primitives.py

https://bitbucket.org/dexteris/forma
Python | 158 lines | 89 code | 12 blank | 57 comment | 8 complexity | ebade3ead8c69b74bec9da22260a51a0 MD5 | raw file
  1. # Copyright (c) 2010 Galen Clark Haynes
  2. # 2010 Dexteris Robotics, LLC
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of Dexteris Robotics, LLC nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. from math import pi
  29. from forma.common import Forma
  30. from forma.transforms import Translation, Rotation
  31. class Polygon(Forma):
  32. """
  33. Generic list of points generating polygon on X-Y plane, provided as a 2xN array
  34. """
  35. def __init__(self,**kwargs):
  36. import numpy
  37. self.normal = [0.0,0.0,1.0];
  38. super(type(self),self).__init__(kwargs)
  39. if not hasattr(self,'points'):
  40. self.points = numpy.zeros([3,4])
  41. self.points[:,0] = [-1,1,0]
  42. self.points[:,1] = [-1,-1,0]
  43. self.points[:,2] = [1,-1,0]
  44. self.points[:,3] = [1,1,0]
  45. class Circle(Forma):
  46. """
  47. Circle on the X-Y plane.
  48. """
  49. def __init__(self,**kwargs):
  50. self.radius = 1.0
  51. self.arc = 2.0*pi
  52. super(type(self),self).__init__(kwargs)
  53. class Sphere(Forma):
  54. """
  55. Sphere centered at origin, poles along Z.
  56. """
  57. def __init__(self,**kwargs):
  58. self.radius = 1.0
  59. super(type(self),self).__init__(kwargs)
  60. class Cone(Forma):
  61. """
  62. Cone with base on the X-Y plane, height along positive Z.
  63. """
  64. def __init__(self,**kwargs):
  65. self.radius = 1.0
  66. self.height = 1.0
  67. super(type(self),self).__init__(kwargs)
  68. self.c = Circle(radius=self.radius)
  69. if hasattr(self,'color'):
  70. self.c.color = self.color
  71. self.attach(self.c)
  72. class Box(Forma):
  73. """
  74. Box with X,Y,Z correspond to width, length, and height.
  75. """
  76. def __init__(self,**kwargs):
  77. self.width = 1.0
  78. self.length = 1.0
  79. self.height = 1.0
  80. super(type(self),self).__init__(kwargs)
  81. class Cube(Forma):
  82. """
  83. A Box with equal sizes for each side.
  84. """
  85. def __init__(self,**kwargs):
  86. self.size = 1.0
  87. super(type(self),self).__init__(kwargs)
  88. b = Box(width=self.size,length=self.size,height=self.size)
  89. if hasattr(self,'color'):
  90. b.color = self.color
  91. self.attach(b)
  92. class Cylinder(Forma):
  93. """
  94. Cylinder on X-Y plane, with Z as height.
  95. """
  96. def __init__(self,**kwargs):
  97. self.radius = 1.0
  98. self.height = 1.0
  99. self.arc = 2.0*pi
  100. super(type(self),self).__init__(kwargs)
  101. class SolidCylinder(Forma):
  102. """
  103. Cylinder with capped ends.
  104. """
  105. def __init__(self,**kwargs):
  106. self.radius = 1.0
  107. self.height = 1.0
  108. self.arc = 2.0*pi
  109. super(type(self),self).__init__(kwargs)
  110. c = Cylinder(radius=self.radius,height=self.height,arc=self.arc)
  111. s = Circle(radius=self.radius,arc=self.arc)
  112. self.attach(c)
  113. self.attach(Translation(z=self.height/2).attach(s))
  114. self.attach(Translation(z=-self.height/2).\
  115. attach(Rotation(y=pi).\
  116. attach(s)))
  117. if hasattr(self,'color'):
  118. c.color = self.color
  119. s.color = self.color
  120. class LineTrace(Forma):
  121. """
  122. An object allow easy addition of data points to be traced
  123. """
  124. def __init__(self,**kwargs):
  125. self.t_trace = []
  126. self.x_trace = []
  127. self.y_trace = []
  128. self.z_trace = []
  129. super(type(self),self).__init__(kwargs)
  130. def add(self,x,y,z):
  131. if len(self.x_trace) == 0 or (self.x_trace[-1] != x and self.y_trace[-1] != y and self.z_trace[-1] != z):
  132. self.x_trace.append(x)
  133. self.y_trace.append(y)
  134. self.z_trace.append(z)
  135. class Label(Forma):
  136. """
  137. Text label at current origin or specified point given as (x,y,z) parameter set.
  138. """
  139. def __init__(self,**kwargs):
  140. self.text = ''
  141. self.x = 0.0
  142. self.y = 0.0
  143. self.z = 0.0
  144. self.color = [0,0,0]
  145. super(type(self),self).__init__(kwargs)