Class: Geom::Polygon

Inherits:
TriangleMesh show all
Defined in:
lib/geom/polygon.rb

Constant Summary collapse

WINDING_CW =
0
WINDING_CCW =
1
AXIS_X =
1
AXIS_Y =
2
AXIS_Z =
3
CAP_TOP =
4
CAP_BASE =
5
CAP_BOTH =
6

Instance Attribute Summary

Attributes inherited from TriangleMesh

#data, #faces, #meshes, #tess, #texcoord, #vertices

Instance Method Summary collapse

Methods inherited from TriangleMesh

#<<, #bounding_box, #initialize, #merge, #reverse, #transform_vertices

Constructor Details

This class inherits a constructor from Geom::TriangleMesh

Instance Method Details

#areaObject



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
# File 'lib/geom/polygon.rb', line 65

def area
  # remove duplicates and invisibles
  return nil if @vertices.length < 3
  @vertices.each{|v| return 0 if @vertices.grep(v).length>1}

  result = 0
  points = @vertices.dup
  plane  = self.plane

  ax = plane.normal.x > 0 ? plane.normal.x : -plane.normal.x
  ay = plane.normal.y > 0 ? plane.normal.y : -plane.normal.y
  az = plane.normal.z > 0 ? plane.normal.z : -plane.normal.z

  coord = AXIS_Z

  if ax > ay
    if ax > az
      coord = AXIS_X
    end
  elsif ay > az
    coord = AXIS_Y
  end

  points.push(points[0],points[1])

  # compute area of the 2D projection
  points.each_with_index do |point,i|
    next if i.zero?
    j = (i+1) % points.length
    k = (i-1) % points.length

    case coord
    when AXIS_X
      result += (point.y * (points[j].z - points[k].z))
    when AXIS_Y
      result += (point.x * (points[j].z - points[k].z))
    else
      result += (point.x * (points[j].y - points[k].y))
    end
  end

  # scale to get area before projection
  an = Math.sqrt(ax**2 + ay**2 + az**2) # length of normal vector
  case coord
  when AXIS_X
    result *= (an / (2*ax))
  when AXIS_Y
    result *= (an / (2*ay))
  when AXIS_Z
    result *= (an / (2*az))
  end
  2.times {points.pop}
  result
end

#calc_uvObject



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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/geom/polygon.rb', line 206

def calc_uv
  result = []
  plane = self.plane
  up = Number3D.new( 0, 1, 0 )

  # get side vector
  side = Number3D.cross(up, plane.normal)
  side.normalize

  # adjust up vector
  up = Number3D.cross(self.plane.normal, side)
  up.normalize

  matrix  = Matrix3D[
          [side.x, up.x, plane.normal.x, 0],
          [side.y, up.y, plane.normal.y, 0],
          [side.z, up.z, plane.normal.z, 0],
          [0, 0, 0, 1]]

  v, n, t = nil, nil, nil
  min = Number3D.new(1000,1000,1000)
  max = Number3D.new(-min.x, -min.y, -min.z)
  pts = []

  @vertices.each do |v|
    n = v.position

    # Matrix3D.multiplyVector3x3( matrix, n );

    min.x = n.x if n.x < min.x
    min.y = n.y if n.y < min.y
    max.x = n.x if n.x > max.x
    max.y = n.y if n.y > max.y

    pts << n
    result << NumberUV.new
  end

  w = max.x - min.x
  h = max.y - min.y
  size = w < h ? h : w

  @vertices.each_with_index do |v,i|
    n = pts[i]
    t = result[i]

    t.u = ((n.x - min.x) / size) * size
    t.v = ((n.y - min.y) / size) * size
  end

  result
end

#cloneObject



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
# File 'lib/geom/polygon.rb', line 15

def clone
  vertices = @vertices.collect{|v| v.clone}
  texcoord = @texcoord ? @texcoord.collect{|uv| uv.clone} : nil
  faces    = @faces.collect do |f|
    if texcoord
      Triangle.new([
        vertices[ @vertices.index(f.vertices[0]) ],
        vertices[ @vertices.index(f.vertices[1]) ],
        vertices[ @vertices.index(f.vertices[2]) ]
      ],[
        texcoord[ @texcoord.index(f.texcoord[0]) ],
        texcoord[ @texcoord.index(f.texcoord[1]) ],
        texcoord[ @texcoord.index(f.texcoord[2]) ]
      ])
    else
      Triangle.new([
        vertices[ @vertices.index(f.vertices[0]) ],
        vertices[ @vertices.index(f.vertices[1]) ],
        vertices[ @vertices.index(f.vertices[2]) ]
      ])
    end
  end
  result = Polygon.new(vertices,faces,@data.dup)
  result.texcoord = texcoord
  result
end

#dominant_axisObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/geom/polygon.rb', line 187

def dominant_axis
  plane = self.plane
  return 0 unless plane

  ax = (plane.normal.x > 0 ? plane.normal.x : -plane.normal.x)
  ay = (plane.normal.y > 0 ? plane.normal.y : -plane.normal.y)
  az = (plane.normal.z > 0 ? plane.normal.z : -plane.normal.z)

  axis = AXIS_Z
  if ax > ay
    if ax > az
      axis = AXIS_X
    end
  elsif ay > az
    axis = AXIS_Y
  end
  axis
end

#extrude(distance, direction, cap = CAP_BOTH, update = true) ⇒ Object



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
# File 'lib/geom/polygon.rb', line 153

def extrude(distance,direction,cap=CAP_BOTH,update=true)
  direction.normalize
  top_cap = clone
  top_cap.vertices.each do |v|
    v.x += distance*direction.x
    v.y += distance*direction.y
    v.z += distance*direction.z
  end
  top_cap.faces.each {|f| f.normal = direction }
  num = @vertices.length

  sides = Array.new(@vertices.length).map!{ Polygon.new }
  sides.each_with_index do |side,i|
    j = (i+1) % num
    side.vertices.push(top_cap.vertices[i],top_cap.vertices[j])
    side.vertices.push(@vertices[j],@vertices[i])
    side.data[:side] = true
    side.update if update
  end

  case cap
  when CAP_BASE
    top_cap.faces.clear
  when CAP_TOP
    self.faces.clear
  when CAP_BOTH
    self.faces.each do |f|
      f.flip_normal
    end
  end

  sides + [top_cap]
end

#planeObject

TODO real plane, not just from first 3 vertices



121
122
123
124
# File 'lib/geom/polygon.rb', line 121

def plane
  return nil if @vertices.length < 3
  Plane.three_points(*@vertices[0..2])
end

#point_inside(pt) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/geom/polygon.rb', line 126

def point_inside(pt)
  x = pt.x;
  y = pt.y;
  c = false;
  length = @vertices.length
  
  length.times do |i|
    j = (i+1) % length

    a = @vertices[i]
    b = @vertices[j]

    xpi = a.x
    ypi = a.y
    xpj = b.x
    ypj = b.y
    if ((((ypi<=y) && (y<ypj)) || ((ypj<=y) && (y<ypi))) && (x < (xpj-xpi)*(y-ypi)/(ypj-ypi)+xpi))
      c = !c
    end
  end
  return c;      
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/geom/polygon.rb', line 42

def update
  return false if @vertices.length < 3
  triangles = @tess.triangulate(self)
  unless triangles
    @vertices.reverse!
    triangles = @tess.triangulate(self)
    return false unless triangles
  end

  @texcoord = calc_uv
  triangles.each do |t|
    v0 = @vertices[t[0]]
    v1 = @vertices[t[1]]
    v2 = @vertices[t[2]]

    t0 = @texcoord[t[0]]
    t1 = @texcoord[t[1]]
    t2 = @texcoord[t[2]]
    @faces.push(Triangle.new([v2,v1,v0],[t0,t1,t2]))
  end
  true
end

#windingObject



149
150
151
# File 'lib/geom/polygon.rb', line 149

def winding
  area < 0 ? WINDING_CW : WINDING_CCW
end