solidruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +3 -0
  4. data/COPYING +674 -0
  5. data/Gemfile +2 -0
  6. data/Guardfile +24 -0
  7. data/LICENSE.md +675 -0
  8. data/README.md +180 -0
  9. data/Rakefile +10 -0
  10. data/bin/solidruby +61 -0
  11. data/examples/chamfered_cube.rb +39 -0
  12. data/examples/filleted_cube.rb +39 -0
  13. data/examples/gear.rb +11 -0
  14. data/examples/knurls.rb +12 -0
  15. data/examples/nut_support.rb +17 -0
  16. data/examples/openscad_examples/old/example001.rb +42 -0
  17. data/examples/openscad_examples/old/example002.rb +45 -0
  18. data/examples/openscad_examples/old/example003.rb +37 -0
  19. data/examples/openscad_examples/old/example004.rb +22 -0
  20. data/examples/openscad_examples/old/example005.rb +45 -0
  21. data/examples/openscad_examples/old/example006.rb +85 -0
  22. data/examples/openscad_examples/old/example007.dxf +2410 -0
  23. data/examples/openscad_examples/old/example007.rb +107 -0
  24. data/examples/openscad_examples/old/example008.rb +61 -0
  25. data/examples/openscad_examples/old/example009.dxf +3894 -0
  26. data/examples/openscad_examples/old/example009.rb.old +48 -0
  27. data/examples/openscad_examples/old/example010.dat +51 -0
  28. data/examples/openscad_examples/old/example010.rb +23 -0
  29. data/examples/openscad_examples/old/example011.rb +42 -0
  30. data/examples/openscad_examples/old/example012.rb +22 -0
  31. data/examples/openscad_examples/old/example012.stl +450 -0
  32. data/examples/openscad_examples/old/example013.dxf +2276 -0
  33. data/examples/openscad_examples/old/example013.rb +30 -0
  34. data/examples/openscad_examples/old/example014.rb +30 -0
  35. data/examples/openscad_examples/old/example015.rb +46 -0
  36. data/examples/openscad_examples/old/example016.rb +75 -0
  37. data/examples/openscad_examples/old/example016.stl +0 -0
  38. data/examples/pipe.rb +38 -0
  39. data/examples/pipe_bug.rb +16 -0
  40. data/examples/printed_gear.rb +12 -0
  41. data/examples/printed_gear2.rb +16 -0
  42. data/examples/printed_thread.rb +9 -0
  43. data/examples/printed_thread2.rb +13 -0
  44. data/examples/stack.rb +21 -0
  45. data/examples/threads.rb +183 -0
  46. data/examples/threads2.rb +93 -0
  47. data/examples/threads3.rb +46 -0
  48. data/lib/solidruby.rb +60 -0
  49. data/lib/solidruby/assemblies/assembly.rb +176 -0
  50. data/lib/solidruby/assemblies/bolt.rb +158 -0
  51. data/lib/solidruby/assemblies/gear.rb +131 -0
  52. data/lib/solidruby/assemblies/linear_bearing.rb +84 -0
  53. data/lib/solidruby/assemblies/nut.rb +153 -0
  54. data/lib/solidruby/assemblies/pipe.rb +153 -0
  55. data/lib/solidruby/assemblies/ruler.rb +44 -0
  56. data/lib/solidruby/assemblies/tslot.rb +141 -0
  57. data/lib/solidruby/assemblies/tslot_machining.rb +62 -0
  58. data/lib/solidruby/assemblies/washer.rb +57 -0
  59. data/lib/solidruby/bill_of_material.rb +43 -0
  60. data/lib/solidruby/csg_modelling/csg_modelling.rb +43 -0
  61. data/lib/solidruby/csg_modelling/difference.rb +41 -0
  62. data/lib/solidruby/csg_modelling/hull.rb +23 -0
  63. data/lib/solidruby/csg_modelling/intersection.rb +24 -0
  64. data/lib/solidruby/csg_modelling/minkowski.rb +23 -0
  65. data/lib/solidruby/csg_modelling/union.rb +41 -0
  66. data/lib/solidruby/csg_modifiers/color.rb +41 -0
  67. data/lib/solidruby/csg_modifiers/csg_modifier.rb +50 -0
  68. data/lib/solidruby/csg_modifiers/linear_extrude.rb +34 -0
  69. data/lib/solidruby/csg_modifiers/projection.rb +29 -0
  70. data/lib/solidruby/csg_modifiers/rotate_extrude.rb +30 -0
  71. data/lib/solidruby/extra.rb +58 -0
  72. data/lib/solidruby/helpers/chamfer.rb +27 -0
  73. data/lib/solidruby/helpers/fillet.rb +28 -0
  74. data/lib/solidruby/helpers/position.rb +267 -0
  75. data/lib/solidruby/helpers/rounded_cube.rb +37 -0
  76. data/lib/solidruby/helpers/triangle.rb +167 -0
  77. data/lib/solidruby/primitives/circle.rb +26 -0
  78. data/lib/solidruby/primitives/cube.rb +123 -0
  79. data/lib/solidruby/primitives/cylinder.rb +72 -0
  80. data/lib/solidruby/primitives/import.rb +42 -0
  81. data/lib/solidruby/primitives/polygon.rb +26 -0
  82. data/lib/solidruby/primitives/polyhedron.rb +30 -0
  83. data/lib/solidruby/primitives/primitive.rb +19 -0
  84. data/lib/solidruby/primitives/render.rb +48 -0
  85. data/lib/solidruby/primitives/sphere.rb +49 -0
  86. data/lib/solidruby/primitives/square.rb +88 -0
  87. data/lib/solidruby/primitives/surface.rb +44 -0
  88. data/lib/solidruby/primitives/text.rb +43 -0
  89. data/lib/solidruby/printed_thread.rb +163 -0
  90. data/lib/solidruby/rubyscad_bridge.rb +376 -0
  91. data/lib/solidruby/screw_thread.rb +170 -0
  92. data/lib/solidruby/solidruby.rb +150 -0
  93. data/lib/solidruby/solidruby_object.rb +176 -0
  94. data/lib/solidruby/transformations/mirror.rb +22 -0
  95. data/lib/solidruby/transformations/rotate.rb +22 -0
  96. data/lib/solidruby/transformations/scale.rb +22 -0
  97. data/lib/solidruby/transformations/transformation.rb +29 -0
  98. data/lib/solidruby/transformations/translate.rb +32 -0
  99. data/lib/solidruby/version.rb +18 -0
  100. data/lib/templates/assembly.tt +13 -0
  101. data/lib/templates/gitignore.tt +3 -0
  102. data/lib/templates/guardfile.tt +4 -0
  103. data/lib/templates/main.tt +20 -0
  104. data/lib/templates/params.tt +3 -0
  105. data/lib/templates/printed.tt +9 -0
  106. data/manual/manual.html +417 -0
  107. data/solidruby.gemspec +35 -0
  108. metadata +277 -0
@@ -0,0 +1,24 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModelling
17
+ class Intersection < CSGModelling
18
+ end
19
+
20
+ def *(args)
21
+ return args if nil?
22
+ Intersection.new(self, args)
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModelling
17
+ class Minkowski < CSGModelling
18
+ end
19
+
20
+ def minkowski(*parts)
21
+ Minkowski.new(*parts)
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModelling
17
+ class Union < CSGModelling
18
+ end
19
+
20
+ def +(args)
21
+ return args if nil?
22
+ if args.is_a? Array
23
+ r = self
24
+ args.each do |a|
25
+ r = Union.new(r, a)
26
+ end
27
+ r
28
+ else
29
+ optimize_union(self, args)
30
+ end
31
+ end
32
+
33
+ def optimize_union(top, child)
34
+ if top.is_a?(Union) && (!child.is_a? Union) && top.transformations.empty?
35
+ top.children << child
36
+ top
37
+ else
38
+ Union.new(top, child)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModifiers
17
+ class Color < CSGModifier
18
+ def initialize(object, attributes, opacity = nil)
19
+ @operation = 'color'
20
+ if attributes.is_a? String
21
+ attributes = "\"#{attributes}\""
22
+ attributes += ", #{opacity}" unless opacity.nil?
23
+ elsif attributes.is_a? Hash
24
+ attributes[:a] ||= 255
25
+
26
+ r = attributes[:r] / 255.0
27
+ g = attributes[:g] / 255.0
28
+ b = attributes[:b] / 255.0
29
+ a = attributes[:a] / 255.0
30
+ attributes = {c: [r, g, b], alpha: a}
31
+ end
32
+
33
+ super(object, attributes)
34
+ end
35
+
36
+ end
37
+
38
+ def color(args, opacity = nil)
39
+ Color.new(self, args, opacity)
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModifiers
17
+ class CSGModifier < SolidRuby::SolidRubyObject
18
+ def initialize(object, attributes)
19
+ super(attributes)
20
+ @transformations = []
21
+ @children = [object]
22
+ @attributes = attributes
23
+ end
24
+
25
+ def to_rubyscad
26
+ att = @attributes
27
+ if att.is_a? Hash
28
+ att = @attributes.select {|k, v| !v.nil? }.collect do |k, v|
29
+ sv = RubyScadBridge.new.format_value(v)
30
+ "#{k} = #{sv}"
31
+ end.sort_by{ |x| x[0] == 'f' ? ('z' + x) : x }
32
+ end
33
+
34
+ att = att.join(', ') if att.is_a? Array
35
+
36
+ # Apparently this doesn't work for CSGModifiers, like it does for other things in RubyScad?
37
+ # also this is a dirty, dirty hack.
38
+ att = att.gsub('fn', '$fn').gsub('$$', '$')
39
+ ret = "#{@operation}(#{att}){"
40
+ @children ||= []
41
+ @children.each do |child|
42
+ begin
43
+ ret += child.walk_tree
44
+ rescue NoMethodError
45
+ end
46
+ end
47
+ ret += '}'
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,34 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModifiers
17
+ class LinearExtrude < CSGModifier
18
+ alias_attr :height
19
+ alias_attr :convexity, :co
20
+ alias_attr :center, :ce
21
+ alias_attr :twist
22
+ alias_attr :slices, :sl
23
+ alias_attr :scale, :sc
24
+
25
+ def initialize(object, args)
26
+ @operation = 'linear_extrude'
27
+ super(object, args)
28
+ end
29
+ end
30
+
31
+ def linear_extrude(args)
32
+ LinearExtrude.new(self, args)
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModifiers
17
+ class Projection < CSGModifier
18
+ alias_attr :cut
19
+
20
+ def initialize(object, args)
21
+ @operation = 'projection'
22
+ super(object, args)
23
+ end
24
+ end
25
+
26
+ def projection(args = {})
27
+ Projection.new(self, args)
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::CSGModifiers
17
+ class RotateExtrude < CSGModifier
18
+ alias_attr :angle
19
+ alias_attr :convexity
20
+
21
+ def initialize(object, args)
22
+ @operation = 'rotate_extrude'
23
+ super(object, args)
24
+ end
25
+ end
26
+
27
+ def rotate_extrude(args = {})
28
+ RotateExtrude.new(self, args)
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ module SolidRuby::Extras
17
+
18
+ def knurl(y)
19
+ x = 1.5
20
+ height = 1.5
21
+ res = cube([x, y, height])
22
+ res -= cylinder(d: 0.9, h: height * 1.42, fn: 16).rotate(y: 45).translate(x: 0)
23
+ res -= cylinder(d: 0.9, h: height * 1.42, fn: 16).rotate(y: -45).translate(x: 1.5)
24
+ res
25
+ end
26
+
27
+ def knurled_cube(size)
28
+ x = size[0]
29
+ y = size[1]
30
+ z = size[2]
31
+ res = nil
32
+
33
+ (x / 1.5).ceil.times do |i|
34
+ (z / 1.5).ceil.times do |f|
35
+ res += knurl(y).translate(x: i * 1.5, z: f * 1.5)
36
+ end
37
+ end
38
+
39
+ res *= cube([x, y, z])
40
+
41
+ res
42
+ end
43
+
44
+ def knurled_cylinder(args = {})
45
+ res = cylinder(args)
46
+ height = args[:h]
47
+ r = args[:d] / 2.0
48
+
49
+ 24.times do |_i|
50
+ (height / 2).ceil.times do |f|
51
+ res -= cylinder(d: 0.9, h: height * 2).rotate(y: 45).translate(y: -r, z: f * 2)
52
+ res -= cylinder(d: 0.9, h: height * 2).rotate(y: -45).translate(y: -r, z: f * 2)
53
+ end
54
+ res.rotate(z: 15)
55
+ end
56
+ res
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Helpers
17
+ #Helper method for creating chamfers - basically just an extruded triangle
18
+ #that can be subtracted from an edge
19
+ def chamfer(args={})
20
+ height = args[:height] || args[:h] || 0
21
+ length = args[:length] || args[:l] || 0
22
+
23
+ t = triangle(a: height, alpha: 90, beta: 45)
24
+
25
+ return t.linear_extrude(height: length)
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Helpers
17
+ def fillet(args)
18
+ @radius = args[:r] || args[:radius]
19
+ @height = args[:h] || args[:height]
20
+ @fn = args[:fn] || 64
21
+
22
+ res = cube(@radius*2, @radius*2, @height+0.02).translate(z: -0.01) -
23
+ cylinder(r: @radius, h: @height + 0.04, fn: @fn)
24
+ .translate(z: -0.02)
25
+
26
+ res.translate(x: -@radius, y: -@radius)
27
+ end
28
+ end
@@ -0,0 +1,267 @@
1
+ # This file is part of SolidRuby.
2
+ #
3
+ # SolidRuby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # SolidRuby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with SolidRuby. If not, see <http://www.gnu.org/licenses/>.
15
+ #
16
+ module SolidRuby::Helpers
17
+
18
+ def corners(face = :top)
19
+ [
20
+ {face: face, edge: :top, corner: :left},
21
+ {face: face, edge: :top, corner: :right},
22
+ {face: face, edge: :bottom, corner: :left},
23
+ {face: face, edge: :bottom, corner: :right},
24
+ ]
25
+ end
26
+
27
+ def is_vertical?(face, edge)
28
+ if [:top, :bottom].include? face
29
+ false
30
+ elsif [:top, :bottom].include? edge
31
+ false
32
+ else
33
+ true
34
+ end
35
+ end
36
+
37
+ def is_horizontal?(face, edge)
38
+ !is_vertical?(face, edge)
39
+ end
40
+
41
+ def is_x_dir?(face, edge)
42
+ if is_vertical?(face, edge)
43
+ false
44
+ elsif [:front, :back, :top, :bottom].include?(face) &&
45
+ [:top, :bottom].include?(edge)
46
+ true
47
+ else
48
+ false
49
+ end
50
+ end
51
+
52
+ def is_y_dir?(face, edge)
53
+ return false if is_x_dir?(face, edge) || is_vertical?(face, edge)
54
+ true
55
+ end
56
+
57
+ def normalise_edges(args={})
58
+ faces = {}
59
+
60
+ args.each do |key, val|
61
+ if [:front, :back, :left, :right, :top, :bottom].include? key
62
+ val = [val] unless val.is_a? Array
63
+ faces[key] = val
64
+ next
65
+ elsif key == :edges
66
+ if val == :vertical
67
+ faces[:front] = [:left, :right]
68
+ faces[:back] = [:left, :right]
69
+ elsif val == :horizontal
70
+ faces[:front] = [:top, :bottom]
71
+ faces[:right] = [:top, :bottom]
72
+ faces[:left] = [:top, :bottom]
73
+ faces[:back] = [:top, :bottom]
74
+ elsif val == :all
75
+ faces[:front] = [:top, :bottom, :left, :right]
76
+ faces[:right] = [:top, :bottom]
77
+ faces[:left] = [:top, :bottom]
78
+ faces[:back] = [:top, :bottom, :left, :right]
79
+ end
80
+ end
81
+ end
82
+ faces
83
+ end
84
+
85
+ def translations_for_edge(args={})
86
+ tolerance = args[:tolerance] || 0.01
87
+ trans = []
88
+ args[:faces].each do |face, edges|
89
+ edges.each do |edge|
90
+ res = {}
91
+ res[:z_rot] = 0
92
+ res[:y_rot] = 0
93
+ res[:x_rot] = 0
94
+ res[:x_trans] = 0
95
+ res[:y_trans] = 0
96
+ res[:length] = args[:z]
97
+ res[:z_trans] = -res[:length]/2.0 - (tolerance*2)
98
+
99
+ #position on edge
100
+ if Helpers::is_horizontal?(face, edge)
101
+ res[:y_rot] = 90
102
+ res[:z_trans] = 0
103
+ end
104
+
105
+ if is_x_dir?(face, edge)
106
+ res[:length] = args[:x]
107
+ res[:x_trans] = -res[:length] / 2.0 - (tolerance*2)
108
+ elsif is_y_dir?(face, edge)
109
+ res[:length] = args[:y]
110
+ res[:y_trans] = res[:length] / 2.0
111
+ res[:x_rot] = 90
112
+ end
113
+
114
+ #rotate to match edge
115
+ rot_matrix = {
116
+ [:top, :top] => 90,
117
+ [:top, :left] => 180,
118
+ [:top, :right] => 90,
119
+ [:top, :bottom] => 180,
120
+
121
+ [:bottom, :top] => 270,
122
+ [:bottom, :left] => 270,
123
+ [:bottom, :right] => 0,
124
+ [:bottom, :bottom] => 0,
125
+
126
+ [:left, :top] => 180,
127
+ [:left, :left] => 90,
128
+ [:left, :right] => 180,
129
+ [:left, :bottom] => 270,
130
+
131
+ [:right, :top] => 90,
132
+ [:right, :left] => 270,
133
+ [:right, :right] => 0,
134
+ [:right, :bottom] => 0,
135
+
136
+ [:front, :top] => 180,
137
+ [:front, :left] => 180,
138
+ [:front, :right] => 270,
139
+ [:front, :bottom] => 270,
140
+
141
+ [:back, :top] => 90,
142
+ [:back, :left] => 0,
143
+ [:back, :right] => 90,
144
+ [:back, :bottom] => 0,
145
+ }
146
+
147
+ res[:z_rot] = rot_matrix[[face, edge]]
148
+
149
+ point = args[:onto].get_point_on(face: face, edge: edge)
150
+
151
+ if tolerance > 0
152
+ point[:x] = point[:x] > 0 ? point[:x] + tolerance : point[:x] - tolerance
153
+ point[:y] = point[:y] > 0 ? point[:y] + tolerance : point[:y] - tolerance
154
+ point[:z] = point[:z] > 0 ? point[:z] + tolerance : point[:z] - tolerance
155
+ end
156
+
157
+ res[:x_trans] += point[:x]
158
+ res[:y_trans] += point[:y]
159
+ res[:z_trans] += point[:z]
160
+
161
+ trans << res
162
+ end
163
+ end
164
+
165
+ trans
166
+ end
167
+
168
+ def calculate_point_on(args = {})
169
+ face = args[:face] || :top
170
+ edge = args[:edge] || :center
171
+ corner = args[:corner] || :center
172
+ face_offset = args[:face_offset] || 0
173
+ edge_offset = args[:edge_offset] || 0
174
+ corner_offset = args[:corner_offset] || 0
175
+
176
+ vert_axis = :z
177
+ horiz_axis = :x
178
+ vert_dir = 1
179
+ horiz_dir = 1
180
+
181
+ pos = case face
182
+ when :top
183
+ vert_axis = :y
184
+ horiz_dir = -1
185
+ {x: args[:x] / 2.0, y: args[:y] / 2.0, z: args[:z] + face_offset}
186
+ when :bottom
187
+ vert_axis = :y
188
+ horiz_dir = -1
189
+ vert_dir = -1
190
+ { x: args[:x] / 2.0, y: args[:y] / 2.0, z: -face_offset}
191
+ when :left
192
+ horiz_axis = :y
193
+ { x: face_offset, y: args[:y] / 2.0, z: args[:z] / 2.0 }
194
+ when :right
195
+ horiz_axis = :y
196
+ horiz_dir = -1
197
+ { x: args[:x] + face_offset, y: args[:y] / 2.0, z: args[:z] / 2.0 }
198
+ when :front
199
+ horiz_dir = -1
200
+ { x: args[:x] / 2.0, y: -face_offset, z: args[:z] / 2.0 }
201
+ when :back
202
+ { x: args[:x] / 2.0, y: args[:y] + face_offset, z: args[:z] / 2.0 }
203
+ when :center
204
+ horiz_dir = -1
205
+ { x: args[:x] / 2.0, y: args[:y] / 2.0, z: args[:z] / 2.0 }
206
+ end
207
+
208
+ return pos if pos.nil?
209
+
210
+ pos[:x] -= args[:x] / 2.0 if args[:centered]
211
+ pos[:y] -= args[:y] / 2.0 if args[:centered]
212
+ pos[:z] -= args[:z] / 2.0 if args[:centered_z]
213
+
214
+ args[:transformations].each do |t|
215
+ pos[:x] += t.x
216
+ pos[:y] += t.y
217
+ pos[:z] += t.z
218
+ end
219
+
220
+ #pos is now center of the given face, move to the given side
221
+ h_change = case horiz_axis
222
+ when :x
223
+ args[:x] / 2.0
224
+ when :y
225
+ args[:y] / 2.0
226
+ end
227
+
228
+ v_change = case vert_axis
229
+ when :y
230
+ args[:y] / 2.0
231
+ else
232
+ args[:z] / 2.0
233
+ end
234
+
235
+ case edge
236
+ when :top
237
+ pos[vert_axis] += (v_change * vert_dir)
238
+ pos[vert_axis] += (edge_offset * vert_dir)
239
+ when :bottom
240
+ pos[vert_axis] -= (v_change * vert_dir)
241
+ pos[vert_axis] -= (edge_offset * vert_dir)
242
+ when :left
243
+ pos[horiz_axis] += (h_change * horiz_dir)
244
+ pos[horiz_axis] += (edge_offset * horiz_dir)
245
+ when :right
246
+ pos[horiz_axis] -= (h_change * horiz_dir)
247
+ pos[horiz_axis] -= (edge_offset * horiz_dir)
248
+ end
249
+
250
+ case corner
251
+ when :top
252
+ pos[vert_axis] += (v_change * vert_dir)
253
+ pos[vert_axis] += (corner_offset * vert_dir)
254
+ when :bottom
255
+ pos[vert_axis] -= (v_change * vert_dir)
256
+ pos[vert_axis] -= (corner_offset * vert_dir)
257
+ when :left
258
+ pos[horiz_axis] += (h_change * horiz_dir)
259
+ pos[horiz_axis] += (corner_offset * horiz_dir)
260
+ when :right
261
+ pos[horiz_axis] -= (h_change * horiz_dir)
262
+ pos[horiz_axis] -= (corner_offset * horiz_dir)
263
+ end
264
+
265
+ pos
266
+ end
267
+ end