-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.rb
executable file
·231 lines (183 loc) · 7.2 KB
/
Shape.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- ruby -*-
require_relative 'Draw'
require 'rmath3d/rmath3d_plain'
def urand(range)
range*rand() - range/2.0
end
module MPRAlgorithm
class Box
attr_accessor :orientation, :center, :half, :rgb
def initialize
@orientation = RMath3D::RQuat.new(0.0, 0.0, 0.0, 1.0)
@center = RMath3D::RVec3.new
@half = RMath3D::RVec3.new(0.5, 0.5, 0.5)
@rgb = [1.0, 1.0, 1.0]
end
# returns suppot point in world coorinate.
def get_support_point(dir_world) # dir_world : RMath3D::RVec3
orientation_conj = @orientation.getConjugated
dir_local = orientation_conj * RMath3D::RQuat.new(dir_world.x, dir_world.y, dir_world.z, 0.0) * @orientation
support_local = RMath3D::RVec3.new(@half)
support_local.x *= -1.0 if dir_local.x < 0.0
support_local.y *= -1.0 if dir_local.y < 0.0
support_local.z *= -1.0 if dir_local.z < 0.0
support_world = (@orientation * RMath3D::RQuat.new(support_local.x, support_local.y, support_local.z, 0.0) * orientation_conj).xyz
support_world.add! @center
return support_world
end
def reset
@center.setElements(urand(6.0), 2*rand, urand(6.0))
@half.setElements(0.5+rand, 1.0+rand, 0.5+rand)
@orientation.rotationAxis(RMath3D::RVec3.new(rand+0.1,0,urand(2.0)).normalize!, Math::PI*urand(2.0))
end
def draw
Draw.box(@center, @orientation.normalize!, @half)
end
end
class Cylinder
attr_accessor :orientation, :center, :radius, :half_height, :rgb
def initialize
@orientation = RMath3D::RQuat.new(0.0, 0.0, 0.0, 1.0)
@center = RMath3D::RVec3.new
@radius = 1.0
@half_height = 0.5
@rgb = [1.0, 1.0, 1.0]
end
# returns suppot point in world coorinate.
def get_support_point(dir_world) # dir_world : RMath3D::RVec3
orientation_conj = @orientation.getConjugated
dir_local = orientation_conj * RMath3D::RQuat.new(dir_world.x, dir_world.y, dir_world.z, 0.0) * @orientation
u = RMath3D::RVec3.new(0.0, 1.0, 0.0)
v = dir_local.xyz.getNormalized
w = v - RMath3D::RVec3.dot(u,v) * u
w_length = w.getLength
sign = RMath3D::RVec3.dot(u,v) >= 0.0 ? 1.0 : -1.0
support_local = nil
if w_length > RMath3D::TOLERANCE
support_local = sign * @half_height * u + @radius * (1.0/w_length) * w
else
support_local = sign * @half_height * u
end
support_world = (@orientation * RMath3D::RQuat.new(support_local.x, support_local.y, support_local.z, 0.0) * orientation_conj).xyz
support_world.add! @center
return support_world
end
def reset
@center.setElements(urand(6.0), 2*rand, urand(6.0))
@orientation.rotationAxis(RMath3D::RVec3.new(rand+0.1,0,urand(2.0)).normalize!, Math::PI*urand(2.0))
@radius = 1.0+rand()
@half_height = 1.0+rand()
end
def draw
Draw.cylinder(@center, @orientation.normalize!, @radius, @half_height)
end
end
class Cone
attr_accessor :orientation, :center, :radius, :height, :rgb
def initialize
@orientation = RMath3D::RQuat.new(0.0, 0.0, 0.0, 1.0)
@center = RMath3D::RVec3.new
@radius = 1.0
@height = 0.5
@rgb = [1.0, 1.0, 1.0]
end
# returns suppot point in world coorinate.
def get_support_point(dir_world) # dir_world : RMath3D::RVec3
orientation_conj = @orientation.getConjugated
dir_local = orientation_conj * RMath3D::RQuat.new(dir_world.x, dir_world.y, dir_world.z, 0.0) * @orientation
u = RMath3D::RVec3.new(0.0, 1.0, 0.0)
v = dir_local.xyz.getNormalized
w = v - RMath3D::RVec3.dot(u,v) * u
w_length = w.getLength
w.normalize!
sin_theta = @radius / Math.sqrt(@radius*@radius + @height*@height)
sin_phi = RMath3D::RVec3.dot(u,v)
support_local = nil
if sin_phi >= sin_theta
support_local = @height * u
else
if w_length > RMath3D::TOLERANCE
support_local = @radius * w
else
support_local = RMath3D::RVec3.new
end
end
support_world = (@orientation * RMath3D::RQuat.new(support_local.x, support_local.y, support_local.z, 0.0) * orientation_conj).xyz
support_world.add! @center
return support_world
end
def reset
@center.setElements(urand(6.0), 2*rand, urand(6.0))
@orientation.rotationAxis(RMath3D::RVec3.new(rand+0.1,0,urand(2.0)).normalize!, Math::PI*urand(2.0))
@radius = 0.5+2.0*rand()
@height = 2.0+rand()
end
def draw
Draw.cone(@center, @orientation.normalize!, @radius, @height)
end
end
class Sphere
attr_accessor :center, :radius, :rgb
def initialize
@center = RMath3D::RVec3.new
@radius = 1.0
@rgb = [1.0, 1.0, 1.0]
end
# returns suppot point in world coorinate.
def get_support_point(dir_world) # dir_world : RMath3D::RVec3
support_local = @radius * dir_world.getNormalized
support_world = support_local + @center
return support_world
end
def reset
@center.setElements(urand(6.0), 2*rand, urand(6.0))
@radius = 1.0+rand()
end
def draw
Draw.sphere(@center, @radius)
end
end
class Triangle
attr_accessor :orientation, :center, :vertex, :rgb
def initialize
@orientation = RMath3D::RQuat.new(0.0, 0.0, 0.0, 1.0)
@center = RMath3D::RVec3.new
@vertex = [RMath3D::RVec3.new, RMath3D::RVec3.new, RMath3D::RVec3.new]
@rgb = [1.0, 1.0, 1.0]
end
# returns suppot point in world coorinate.
def get_support_point(dir_world) # dir_world : RMath3D::RVec3
orientation_conj = @orientation.getConjugated
dir_local = orientation_conj * RMath3D::RQuat.new(dir_world.x, dir_world.y, dir_world.z, 0.0) * @orientation
farthest = 0
max_dot_product = RMath3D::RVec3.dot(@vertex[0],dir_local)
dot_product = RMath3D::RVec3.dot(@vertex[1],dir_local)
if max_dot_product < dot_product
max_dot_product = dot_product
farthest = 1
end
dot_product = RMath3D::RVec3.dot(@vertex[2],dir_local)
if max_dot_product < dot_product
max_dot_product = dot_product
farthest = 2
end
support_local = @vertex[farthest]
support_world = (@orientation * RMath3D::RQuat.new(support_local.x, support_local.y, support_local.z, 0.0) * orientation_conj).xyz
support_world.add! @center
return support_world
end
def reset
@center.setElements(urand(6.0), 2*rand, urand(6.0))
@vertex[0].setElements(1.5+rand, 0, 1.5+rand)
@vertex[1].setElements(-2.5+rand, 0, -2.5+rand)
@vertex[2].setElements(-2.5+rand, 0, 1.5+rand)
@orientation.rotationAxis(RMath3D::RVec3.new(rand+0.1,0,urand(2.0)).normalize!, Math::PI*urand(2.0))
end
def draw
mtxOrientation = RMath3D::RMtx3.new.rotationQuaternion(@orientation)
Draw.triangle(@vertex[0].transformRS(mtxOrientation) + @center,
@vertex[1].transformRS(mtxOrientation) + @center,
@vertex[2].transformRS(mtxOrientation) + @center)
end
end
end