Classify_point all points of an array

ruby

#1

allfaces is an array with 4 faces.
punten is an array with all ‘Geom::Point3d’ of a grid spread over the entire model.
I want an array (gridpunten) of all Geom::Point3d that are outside each face.
This is my code:

target = model.entities
   allfaces = target.grep(Sketchup::Face)
      gridpunten = punten.select { |point|
      result = allfaces[0].classify_point(point)
      result == Sketchup::Face::PointOutside 
   }

This code gives all Geom::Point3d except the points in face[0].

The code below does not work.

target = model.entities
   allfaces = target.grep(Sketchup::Face)
      gridpunten = punten.select { |point|
      result = allfaces.each {|q| q.classify_point(point)
      result == Sketchup::Face::PointOutside 
     }
   }

How should the .each loop be used here?


#3
gridpunten = punten.to_a
punten.each{|p|allfaces.each{|f|
  if f.classify_point(p)!=Sketchup::Face::PointOutside
    gridpunten.delete(p)
  end
}}

#4

@sdmitch, puten is already an array, so calling #to_a() upon it just returns itself. (Ie, it does not clone the array, and in your example gridputen would simply be just another reference pointing at puten. The code would modify the common array which might not be desirable. Worse it sets up the bad scenario of modifying an array at the same time it is being iterated.)

Here is another example using Enumerable#all?

target = model.entities
allfaces = target.grep(Sketchup::Face)
gridpunten = punten.select { |point|
  allfaces.all? do |face|
    face.classify_point(point) == Sketchup::Face::PointOutside 
  end
}

#5

I constantly struggle with this problem. Previously, I thought .to_a was suggested as a way to create a “clone”. Is that ever the case?


#6

Yes it is (ie, can create an Array clone) … IF the collection class is NOT an Array.

But if the class of the receiver object IS an Array, then the #to_a() method just returns self.

If the receiver object is a subclass of Array, then the #to_a() method returns an Array clone of the object (ie, it serves to create a new object converted to the Array superclass.)

If the receiver object class has Enumerable mixed in, then the #to_a() method returns an Array clone of the object. Sometimes custom API collection classes define their own custom #to_a() method that creates Array clones.