-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.rb
719 lines (623 loc) · 27.9 KB
/
tool.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
module Wave_Trace
class Wave_Trace_Raycast_Tool
attr_accessor :active
def initialize(page)
@page = page
@window = @page.window
@driver = nil
@state = STATE_IDLE
@active = false
@cursor = nil
@cursor2 = nil
@cursor3 = nil
@cursor4 = nil
@ph = nil
@p_face = nil
@p_best = nil
@barrier_pt_1 = nil
@barrier_pt_2 = nil
@barrier_pt_3 = nil
@sweet_pt_1 = nil
@sweet_pt_2 = nil
@sweet_pt_3 = nil
@sweet_pt_4 = nil
@tmp_pt_1 = nil
@tmp_pt_2 = nil
@tmp_pt_3 = nil
end
def locate_driver(driver)
self.set_state(STATE_LOCATE_DRIVER)
@driver = driver
Sketchup.active_model.active_view.invalidate
end
def set_state(state_to_be)
case state_to_be
when STATE_IDLE # GOING TO BE
case @state
when STATE_LOCATE_DRIVER # Coming From
@page.show
@window.set_size(800,800)
@page.place_driver_label.visible = false
@page.tool_button.visible = false
when STATE_MARK_IGNORE # Coming From
@page.show
if @page.speaker_list.empty?
# @page.realtime_label.visible = false
# @page.commit_label.visible = false
@page.delete_button.visible = false
end
@window.set_size(800,800)
@page.mark_ignore_label.visible = false
@page.mark_ignore_label2.visible = false
@page.mark_ignore_target_label.visible = false
@page.mark_ignore_target_label.caption = ''
@page.mark_ignore_target_highlight.visible = false
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(0,0,0,0)
@page.mark_ignore_update_label.visible = false
@page.mark_ignore_update_check.visible = false
@page.tool_button.visible = false
if @page.mark_ignore_update_check.checked? # Auto-update all rays
self.update_all_drivers
end
when STATE_DEFINE_BARRIER # Coming From
# Clear inference locking if we are locked...
Sketchup.active_model.active_view.lock_inference if Sketchup.active_model.active_view.inference_locked?
@tmp_pt_1 = nil
@tmp_pt_2 = nil
@tmp_pt_3 = nil
@cursor2.clear
@cursor3.clear
@cursor4.clear
end
@state = STATE_IDLE
when STATE_LOCATE_DRIVER # GOING TO BE
@page.hide
@window.set_size(240,140)
@page.place_driver_label.visible = true
@page.tool_button.caption = "Cancel"
@page.tool_button.position(78, 60)
@page.tool_button.visible = true
@state = STATE_LOCATE_DRIVER
when STATE_MARK_IGNORE # GOING TO BE
@page.hide
@window.set_size(280, 200)
@page.mark_ignore_label.visible = true
@page.mark_ignore_label2.visible = true
@page.mark_ignore_target_label.visible = true
@page.mark_ignore_target_highlight.visible = true
@page.mark_ignore_update_label.visible = true
@page.mark_ignore_update_check.visible = true
@page.tool_button.caption = "Done"
@page.tool_button.position(100, 140)
@page.tool_button.visible = true
@state = STATE_MARK_IGNORE
when STATE_DEFINE_BARRIER
# FIX - Yeah yeah... make a little tool window, duh.
@state = STATE_DEFINE_BARRIER
end
end
def active?
return @active
end
# We don't really need to do much here.
def activate
@active = true
@cursor = Sketchup::InputPoint.new
@cursor2 = Sketchup::InputPoint.new # cursor 2-4 for inferencing in STATE_DEFINE_BARRIER & STATE_DEFINE_SWEET
@cursor3 = Sketchup::InputPoint.new
@cursor4 = Sketchup::InputPoint.new
Sketchup.active_model.active_view.invalidate # If we've re-opened the tool this will be sure to update the screen immediately
end
def deactivate(view)
self.set_state(STATE_IDLE)
@active = false
Sketchup.active_model.selection.clear unless Sketchup.active_model.selection.empty?
view.invalidate
end
def onCancel(reason, view)
self.set_state(STATE_IDLE) if reason == 0 # Pressed escape
view.invalidate
end
def resume(view)
view.invalidate
end
def onMouseMove(flags, x, y, view)
case @state
when STATE_LOCATE_DRIVER
@ph = view.pick_helper
@ph.do_pick(x, y)
if @ph.picked_face != @p_face # Found a face different than the currently picked
@p_face = @ph.picked_face
end
@cursor.pick(view, x, y)
view.invalidate
when STATE_MARK_IGNORE
@ph = view.pick_helper
@ph.do_pick(x, y)
if @ph.best_picked != @p_best
@p_best = @ph.best_picked
if @p_best == nil # Nothing under cursor
Sketchup.active_model.selection.clear
@page.mark_ignore_target_label.caption = ''
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(0,0,0,0)
else
# Found something... check its ignore flag and update the gui accordingly
if (ignore_status = @p_best.get_attribute('Wave_Trace', 'ignore') )
@page.mark_ignore_target_label.caption = 'IGNORE'
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(128,0,0,200)
else
@page.mark_ignore_target_label.caption = 'Normal'
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(0, 128, 0, 200)
end
# Highlight the object under cursor
Sketchup.active_model.selection.clear
if (@p_best.is_a? Sketchup::Face) || (@p_best.is_a? Sketchup::Edge) || (@p_best.is_a? Sketchup::ComponentInstance) ||
(@p_best.is_a? Sketchup::Group)
Sketchup.active_model.selection.add(@p_best)
else
# We found some misc stuff we don't care about (dimlinear, image, etc)
@page.mark_ignore_target_label.caption = ''
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(0,0,0,0)
end
end
end
view.invalidate if @cursor.pick(view, x, y)
when STATE_DEFINE_BARRIER
if !@tmp_pt_1 # Picking first point...
view.invalidate if @cursor.pick(view, x, y)
elsif !@tmp_pt_2 # Picking second point...
view.invalidate if @cursor2.pick(view, x, y, @cursor) # cursor2 inferred from cursor
view.lock_inference(@cursor) if !view.inference_locked? # FIX - Probably need this...
else # Picking third and final point
view.invalidate if @cursor3.pick(view, x, y, @cursor2) # cursor3 inferred from cursor2
end
end
end
def update_all_drivers
@page.speaker_list.each do |speaker|
speaker.driver_list.each do |driver|
update_driver(driver, false) # Update driver, but don't refresh view
end
end
Sketchup.active_model.active_view.invalidate
end
def update_driver(driver, refresh = true)
return unless driver.origin && driver.vector
driver.ray_list.clear # FIX - Remove?
driver.ray_list = create_ray_list(driver.origin, driver.vector, driver.y_angle_low_drop.value.to_f.degrees,
driver.y_angle_high_drop.value.to_f.degrees, driver.x_angle_high_drop.value.to_f.degrees,
driver.x_angle_low_drop.value.to_f.degrees, driver.density_drop.value.to_f.degrees,
15.degrees, (@page.max_length_drop.value.to_i*12), @page.max_bounces_drop.value.to_i, 1, 3,
!@page.bounce_hidden_check.checked?, @page.bounce_filter_drop.value )
Sketchup.active_model.active_view.invalidate if refresh
end
# FIX - only activate the inference if we are picking the first point...?
# FIX - Add logic to lock the first cursor to objects too.
# def onKeyDown(key, repeat, flags, view)
# if key == CONSTRAIN_MODIFIER_KEY
# if view.inference_locked?
# view.lock_inference # We WERE locked... clear it.
# else
# view.lock_inference(@cursor) if @cursor.valid?
# end
# view.invalidate
# end
# end
def onLButtonUp(flags, x, y, view)
case @state
when STATE_LOCATE_DRIVER
if !@p_face # Clicked on blank space.
return
end
normal = @p_face.normal
## FIX - Probably have to move
@ph.count.times { |index|
if @p_face == @ph.leaf_at(index) # Found the face's pickhelper branch
normal.transform!(@ph.transformation_at(index)) # Apply the branches complete transform
break
end
}
# Make sure normal is facing camera, flip if not
eye = Sketchup.active_model.active_view.camera.eye
test_point = @cursor.position.offset(normal, 1)
if eye.distance(test_point) > eye.distance(@cursor.position)
# Test point is farther from camera... the normal isn't facing me.
normal.reverse!
end
# Set the driver object's origin, vector and ray_list... then store those as model attributes
@driver.origin = @cursor.position
@driver.vector = normal
@driver.ray_list = create_ray_list(@cursor.position, normal, @driver.y_angle_low_drop.value.to_f.degrees,
@driver.y_angle_high_drop.value.to_f.degrees, @driver.x_angle_high_drop.value.to_f.degrees,
@driver.x_angle_low_drop.value.to_f.degrees, @driver.density_drop.value.to_f.degrees,
15.degrees, (@page.max_length_drop.value.to_i*12), @page.max_bounces_drop.value.to_i, 1, 3,
!@page.bounce_hidden_check.checked?, @page.bounce_filter_drop.value )
@driver.locate_button.background_color = Sketchup::Color.new(0, 0, 0, 128) # Un-highlight locate_button
@driver.locate_button.caption = "Relocate"
@driver = nil
self.set_state(STATE_IDLE)
view.invalidate
when STATE_MARK_IGNORE
if @p_best && (@p_best.is_a? Sketchup::Face) || (@p_best.is_a? Sketchup::Edge) || (@p_best.is_a? Sketchup::ComponentInstance) ||
(@p_best.is_a? Sketchup::Group)
# Flip the ignore status...
Sketchup.active_model.start_operation("Wave_Trace: Mark Objects to Ignore", true, true)
if (ignore_status = @p_best.get_attribute('Wave_Trace', 'ignore') )
@p_best.delete_attribute('Wave_Trace', 'ignore')
@page.mark_ignore_target_label.caption = 'Normal'
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(0, 128, 0, 200)
else
@p_best.set_attribute('Wave_Trace', 'ignore', true)
@page.mark_ignore_target_label.caption = 'IGNORE'
@page.mark_ignore_target_highlight.background_color = Sketchup::Color.new(128,0,0,200)
end
Sketchup.active_model.commit_operation
end
when STATE_DEFINE_BARRIER
if !@tmp_pt_1 # First click / point selection
if @cursor.valid?
@tmp_pt_1 = @cursor.position
@cursor.clear
@cursor = Sketchup::InputPoint.new(@tmp_pt_1) # Move the cursor to the selected point
@cursor.pick(view, x, y) # Make a pick at the selected point to get a valid inference
end
elsif !@tmp_pt_2 # Second click / point selection
@tmp_pt_2 = @cursor2.position if @cursor2.valid?
@cursor2 = Sketchup::InputPoint.new(@tmp_pt_2) # Move the cursor2 inference to the selected point
#view.lock_inference(@cursor2) if !view.inference_locked? # FIX - Probably need this...
else # Third and final click / point selection
if @cursor3.valid?
@barrier_pt_1 = @tmp_pt_1
@barrier_pt_2 = @tmp_pt_2
@barrier_pt_3 = @cursor3.position
self.set_state(STATE_IDLE) # Clear all tmp points / cursors and reset window
view.invalidate
end
end
end
end
# More stringent test to make sure the cursor point is actually touching a face. For some reason it doesn't
# report back as many valid points (@p_face.classify_point returns alot of '32' (point not on plane)... but why?)
def point_on_face?(view)
return true if @cursor.face == @p_face
point = @cursor.position
@ph.count.times { |index|
if @p_face == @ph.leaf_at(index) # Its in a group... apply the transform in reverse to the point we are checking
point.transform!(@ph.transformation_at(index).inverse)
break
end
}
status = @p_face.classify_point(point)
#puts "\nCursor returns....\nEdge: #{@cursor.edge}\nFace: #{@cursor.face}\nVertex: #{@cursor.vertex}\nClassify_point status: #{status}\n"
#view.draw_points([point], 20, 4, 'orange')
#@cursor.draw(view)
if status == Sketchup::Face::PointUnknown || status == Sketchup::Face::PointOutside || status == Sketchup::Face::PointNotOnPlane
return false
else
return true
end
end
def draw(view)
case @state
when STATE_IDLE
if @page.win_open
if @page.draw_realtime_check.checked?
@page.speaker_list.each do |speaker|
next unless speaker.realtime_check.checked?
speaker.driver_list.each do |driver|
next unless driver.realtime_check.checked? && driver.origin
draw_ray_list(view, driver.origin, driver.ray_list)
end
end
end
end
when STATE_LOCATE_DRIVER
# Draw the cursor and a simplified ray_cast icon
if !@p_face.nil? && @p_face.valid? #&& point_on_face?(view) <--- REMOVE comments for stricter point testing
normal = @p_face.normal
# Transform normal if in a group
@ph.count.times { |index|
if @p_face == @ph.leaf_at(index)
normal.transform!(@ph.transformation_at(index)) # if transform??
break
end
}
# Make sure normal is facing camera, flip if not
eye = Sketchup.active_model.active_view.camera.eye
test_point = @cursor.position.offset(normal, 1)
if eye.distance(test_point) > eye.distance(@cursor.position)
# Test point is farther from camera... the normal isn't facing me.
normal.reverse!
end
# This is the "driver placement" tool icon, for all intents...
#FIX - create a list using offsets... extremely large rooms will take inordinate amounts of time to raycast... just for an icon
rays = create_ray_list(@cursor.position, normal) # The defaults create a very small ray_list
draw_ray_list(view, @cursor.position, rays)
view.draw_points(@cursor.position, 15, 5, 'red')
end
when STATE_MARK_IGNORE
# Just draw a custom cursor
view.draw_points(@cursor.position, 15, 3, 'red')
view.draw_points(@cursor.position, 16, 6, 'blue')
when STATE_DEFINE_BARRIER
if @tmp_pt_2 # Point 1 and 2 are defined, locating Point 3
@cursor3.draw(view) # The current cursor
view.draw_points(@cursor3.position, 16, 6, 'orange') # Triangle on 3rd cursor point
@cursor2.draw(view) # The last chosen point (we could use draw_points with tmp_pt2 instead?)
view.draw_points(@cursor2.position, 16, 6, 'blue') # Triangle on 2nd cursor point
view.set_color_from_line(@cursor2.position, @cursor3.position)
view.draw_line(@cursor2.position, @cursor3.position)
elsif @tmp_pt_1 # Point 1 is defined, locating Point 2
@cursor2.draw(view)
view.draw_points(@cursor2.position, 16, 6, 'blue') # Triangle on 2nd cursor point
view.set_color_from_line(@cursor.position, @cursor2.position)
view.draw_line(@cursor.position, @cursor2.position)
# FIX - Cache these as arrays of lines to draw in the mousemove callback
if (vec_to = @cursor.position.vector_to(@cursor2.position)).valid? # Cursor positions are separate. Draw proposed barrier.
view.drawing_color = Sketchup::Color.new("DarkSlateGray")
offset_pt = @cursor2.position.offset(vec_to, 1200)
view.draw_line(@cursor2.position, offset_pt)
offset_pt = @cursor.position.offset(vec_to.reverse, 1200)
view.draw_line(@cursor.position, offset_pt)
#face = @cursor.face
#offset_pt = @cursor.position.offset(vec_to, 1200)
#view.draw_line(@cursor.position, offset_pt)
end
end
@cursor.draw(view) # Always draw the first cursor
view.draw_points(@cursor.position, 16, 6, 'red')
view.draw_points(@tmp_pt_1, 16, 6, 'purple') if @tmp_pt_1
end
end
# Draw a raylist in realtime (no geometry creation)
def draw_ray_list(view, origin, ray_list)
return if !ray_list || ray_list.length < 1
ray_list.each do |point_list|
next if point_list.length < 6 # Malformed point_list. Must be at least: [0]R [1]G [2]B [3]A [4]Length [5]P1
view.drawing_color = Sketchup::Color.new(point_list[0],point_list[1], point_list[2], point_list[3])
view.line_width = point_list[4]
start_point = origin
# Start drawing point to point
for index in 5..(point_list.length-1)
end_point = point_list[index]
view.draw_line(start_point, end_point)
start_point = end_point
end
view.draw_points(end_point, 6, 5)
end
end
def create_ray(ray_origin, ray_vector, ray_color, max_length, max_bounce, line_width = 1, bounce_hidden = true, bounce_filter = nil)
ray_length = 0
bounce_count = 0
ray_bounced_180 = false # To filter out parallel reflections
# We will save all points in an array, with the first 4 values of the array being the ray's color, and the 5th being its line width.
# (this format will make it quick and easy to store all ray info in an attribute_dictionary)
point_list = ray_color.to_a
point_list.push(line_width)
begin
ray = Sketchup.active_model.raytest([ray_origin, ray_vector], bounce_hidden)
if ray
ray_target_point = ray[0]
ray_target_list = ray[1]
ray_target_entity = ray_target_list.last
ray_length += ray_origin.distance(ray_target_point)
# If there is a positive bounce filter and our length exceeds it without having bounced then return nil
if (bounce_filter && bounce_filter > 0 && bounce_count == 0 && ray_length > bounce_filter)
return nil
end
if ray_length >= max_length # End the ray at max_length
over_length = ray_length - max_length
point_in_space = ray_origin.offset(ray_vector, (ray_origin.distance(ray_target_point) - over_length))
point_list.push(point_in_space.to_a)
return point_list
end
point_list.push(ray_target_point.to_a) # FIX - Possibly execute this per target type, to avoid excessive lines from ignored geom
# Test for ignore flag and keep moving if found.
if (ignore_status = ray_target_entity.get_attribute('Wave_Trace', 'ignore') )
ray_origin = ray_target_point # Set origin to target_point and continue on with same vector.
next
end
if ray_target_entity.is_a? Sketchup::Face
# Test face for group/component containers, test those for ignore flags and (if not ignored) apply all transformations
target_normal = ray_target_entity.normal
if ray_target_list.length > 1
## Face is inside group(s) / component(s). Iterate through and apply transformations.
ray_target_list.reverse_each do |container| # Does it really need to be in reverse?
if (container.is_a? Sketchup::ComponentInstance) || (container.is_a? Sketchup::Group)
if ( ignore_status = container.get_attribute('Wave_Trace', 'ignore') )
# Found ignore flag. Break out of this list traversal.
ray_origin = ray_target_point # Vector stays the same
break
end
target_normal.transform!(container.transformation)
end
end
end
next if ignore_status # We already set the origin to be the next point when we found ignore_status above
# If there is a negative bounce filter and this is the first bounce, return nil if the ray has
# has hit something before the filter length
if (bounce_filter && bounce_filter < 0 && bounce_count == 0 && ray_length < bounce_filter.abs)
return nil
end
return point_list if bounce_count == max_bounce # No more bounces allowed... end ray at this last target
angleb = ray_vector.angle_between(target_normal)
crossp = ray_vector.cross(target_normal) # Axis to rotate on
if !crossp.valid? # Ray hit a perpendicular surface and is coming straight back
return point_list if ray_bounced_180 == true # Second time it has happened... surfaces are parallel (abort / finished)
ray_bounced_180 = true
### Flip the ray vector and move origin for next ray cast
ray_vector.reverse!
ray_origin = ray_target_point
bounce_count += 1
next
end
### Reflect the ray vector and move origin for next ray cast
trans=Geom::Transformation.rotation(ray_target_point, crossp, angleb-180.degrees)
ray_vector = target_normal.transform(trans)
ray_origin = ray_target_point
bounce_count += 1
elsif ray_target_entity.is_a? Sketchup::Edge # Hit an edge...
face_list = ray_target_entity.faces
if face_list.empty? # There are no attached faces... just an errant line. Keep going without bouncing.
ray_origin = ray_target_point
# (Vector just stays the same)
next
end
closest_face = nil
if face_list.length > 1 # Multiple faces... find closest
last_test_point = nil
face_list.each do |face|
# Create a test point that is in the direction of the face's center.
test_point = ray_target_point.offset(ray_target_point.vector_to(face.bounds.center), 1)
if !last_test_point # First face. Set it as the bar to beat and keep searching.
last_test_point = test_point
closest_face = face
next
end
# The meat and potatoes... find the closest:
if test_point.distance(ray_origin) > last_test_point.distance(ray_origin)
closest_face = face
end
last_test_point = test_point
end
else # Only one face
closest_face = face_list[0]
end
target_normal = closest_face.normal
if ray_target_list.length > 1
## Face is inside group(s) / component(s). Iterate through and apply transformations.
ray_target_list.reverse_each do |container| # Does it actually need to be in reverse?
if (container.is_a? Sketchup::ComponentInstance) || (container.is_a? Sketchup::Group)
if ( ignore_status = container.get_attribute('Wave_Trace', 'ignore') )
# Found ignore flag. Break out of this list traversal.
ray_origin = ray_target_point # Vector stays the same
break
end
target_normal.transform!(container.transformation)
end
end
end
next if ignore_status # We already set the origin to be the next point when we found ignore_status above
# If there is a negative bounce filter and this is the first bounce, return nil if the ray has
# has hit something before the filter length
if (bounce_filter && bounce_filter < 0 && bounce_count == 0 && ray_length < bounce_filter.abs)
return nil
end
return point_list if bounce_count == max_bounce # No more bounces allowed... end ray at this last target
angleb = ray_vector.angle_between(target_normal)
crossp = ray_vector.cross(target_normal) # Axis to rotate on
if !crossp.valid? # Ray hit a perpendicular surface and is coming straight back
return point_list if ray_bounced_180 == true # Second time it has happened... surfaces are parallel (abort / finished)
ray_bounced_180 = true
### Flip the ray vector and move origin for next ray cast
ray_vector.reverse!
ray_origin = ray_target_point
bounce_count += 1
next
end
### Reflect the ray vector and move origin for next ray cast
trans=Geom::Transformation.rotation(ray_target_point, crossp, angleb-180.degrees)
ray_vector = target_normal.transform(trans)
ray_origin = ray_target_point
bounce_count += 1
else
puts "This should never happen... but apparently the ray hit something other than a face or edge?"
return point_list
end
else
# Ray hit nothing... your ceiling or walls are likely hidden. Finish the ray at its max length or return nil
# if there is a positive bounce filter and we still have yet to bounce
if (bounce_filter && bounce_filter > 0 && bounce_count == 0)
return nil
end
point_in_space = ray_origin.offset(ray_vector, (max_length - ray_length))
if !Sketchup.active_model.bounds.contains?(point_in_space)
Sketchup.active_model.bounds.add(point_in_space)
end
point_list.push(point_in_space.to_a)
return point_list
end
end while ray
# We literally should NEVER wind up down here...
puts "We shouldn't be here...(bottom of create_ray)"
return point_list
end
def create_ray_gradient(x_paths, x_count, y_paths, y_count)
x_paths -= 1 if x_paths > 1 # Protect against zero-divide
y_paths -= 1 if y_paths > 1
gradient = Sketchup::Color.new()
gradient.red = ((255 / x_paths) * (x_count-1))
gradient.green = 25 + ((115 / x_paths) * (x_count-1)) + ((115 / y_paths) * (y_count-1))
gradient.blue = ((255 / y_paths) * (y_count-1))
gradient.alpha = 255 # Always 100% visible
return gradient
end
def create_ray_list(ray_origin, ray_vector, x_angle_low = 30.degrees, x_angle_high = 30.degrees, y_angle_low = 30.degrees,
y_angle_high = 30.degrees, angle_between_rays = 15.degrees, highlight_angle = 15.degrees, max_length = 30,
max_bounce = 0, base_line_width = 1, center_line_width = 2, bounce_hidden = true, bounce_filter = nil)
ray_list = []
ray_color = Sketchup::Color.new(50,50,50)
x_paths = ((x_angle_low.radians + x_angle_high.radians) / angle_between_rays.radians) + 1
y_paths = ((y_angle_low.radians + y_angle_high.radians) / angle_between_rays.radians) + 1
highlight_x = false
trans = Geom::Transformation.rotation(ray_origin, ray_vector.axes.x, x_angle_low) # Start at bottom of x angle
x_vec = ray_vector.transform(trans)
if bounce_filter && bounce_filter != "---"
bounce_filter = bounce_filter.to_i * 12
else
bounce_filter = nil
end
for x_count in 1..x_paths
# Start working our way back up the x angle (unless this is our first time though)
x_vec.transform!(Geom::Transformation.rotation(ray_origin, ray_vector.axes.x, -angle_between_rays)) unless x_count == 1
# Start at the bottom of the y angle
trans = Geom::Transformation.rotation(ray_origin, ray_vector.axes.y, y_angle_low)
y_vec = x_vec.transform(trans)
current_angle_between_x = x_vec.angle_between(ray_vector)
if current_angle_between_x.to_l == (highlight_angle.to_l) # tolerance for floating point inaccuracies
highlight_x = true
else
highlight_x = false
end
for y_count in 1..y_paths
# Start working our way back up the y angle (unless this is our first time through)
y_vec.transform!(Geom::Transformation.rotation(ray_origin, ray_vector.axes.y, -angle_between_rays)) unless y_count == 1
current_angle_between_y = y_vec.angle_between(x_vec)
# Calculate color and width
=begin if current_angle_between_y.to_l <= (highlight_angle.to_l) && highlight_x == true
else # ray is in the "highlight" zone. create purple gradient (sloppy, quick version)\
line_width = base_line_width
if highlight_angle > 0
ray_color.red = 100 + 50*(current_angle_between_y / highlight_angle)
ray_color.blue = 100 + 72 * (current_angle_between_x / highlight_angle )
else
ray_color.red = 128
ray_color.blue = 128
end
ray_color.green = 0
end
=end
if y_vec.parallel?(ray_vector) # ray is in absolute center, highlight red
ray_color.red = 255
ray_color.green = 0
ray_color.blue = 0
line_width = center_line_width
else
line_width = base_line_width
ray_color = create_ray_gradient(x_paths, x_count, y_paths, y_count) # ray is not in a special zone. standard gradient
end
### Check for 90 degree rays and offset them a hair to reduce hitting speaker edges
final_vector = y_vec.clone
if y_vec.angle_between(ray_vector).to_l == 90.degrees.to_l
crossp = final_vector.cross(ray_vector)
trans = Geom::Transformation.rotation(ray_origin, crossp, 0.25.degrees) # Offset the vector by .25 degrees back to center
final_vector.transform!(trans)
end
ray = create_ray(ray_origin.clone, final_vector, ray_color, max_length, max_bounce, line_width, bounce_hidden, bounce_filter) # Create a point_list
ray_list.push(ray) if ray # Add it to the ray_list unless nil
end
end
return ray_list
end
end
end