I need to select objects next to selected object by same vertices position


#9

c’mon…I’m not a coder…but it doesn’t take that long to fumble your way around…the Doc has done 95% of the work for you…can you put in a bit of elbow grease to push thru to the last 5% ?

Here, I’ve bastardised the code…as said I’m not a coder so coding integrity has gone out the window, I’m dealing with whatever errors come up on the screen…

so all your object should be ā€˜meshes’, ask the Doc to provide you an all purpose solution…NICELY.

It’s not that hard with a bit of determination…

fn areMeshesNear source target threshold:5 = 
(

	s = source
	t = target	
	result = false
	for k=1 to s.numverts while result == false do
	(
		p = getvert s k
		for i=1 to t.numverts while result == false do
		(
			if distance p (getvert t i) <= threshold do
			(
				result = true
			)
		)
	)
	
	return result
)

s = $Teapot001 -- enter your source object name here !!!
clearSelection()

for t in (geometry) do
(
	if areMeshesNear s t then (selectmore t)
)

as I’m not a coder, the source name is hardcoded…


#10

thank you guys for putting an effort for me, I really appreciated.
But again I don’t understand how to use it — mean that I don’t know what parameters I should give to the function

All I want is to select one object of scene and then with some code select all other objects with ā€œsharedā€ vertices positions

as I understand the given FN could compare 2 objects by any vertex that share positions and return true/false.
now I want to use it for objects within a single scene. Do I need to have all objects as array or two arrays and then compare them? this is most hard part


#11

it’s not that hard actually

for i=1 to geometry.count do
(
    local obj1 = geometry[i]
    for j=i+1 to geometry.count where areMeshesNear obj1 geometry[j] do ... anything you need
)

#12

upload your scene, and tell what you want to select and what should be selected with it


#13

here’s an example of the scene:
https://drive.google.com/open?id=17DEGbh5wHWa5aQmnJ6KQYDCsA4i9d3zs

there’s two overlayed roads and other objects. I want to by selecting object ā€œMesh_3173ā€ all of these objects to be selected:

"Mesh_2444"
"Mesh_2455"
"Mesh_2463"
"Mesh_2750"
"Mesh_2773"
"Mesh_2778"
"Mesh_2996"
"Mesh_3001"
"Mesh_3008"
"Mesh_3178"
"Mesh_3184"
"Mesh_2349"
"Mesh_2988"
"Mesh_3165"
"Mesh_2343"
"Mesh_2347"
"Mesh_2396"
"Mesh_2977"
"Mesh_3011"
"Mesh_3187"
"Mesh_2335"
"Mesh_2774"
"Mesh_2995"
"Mesh_3172"
"Mesh_2454"
"Mesh_2777"
"Mesh_2989"
"Mesh_3166"
"Mesh_2348"

EDIT: I add mentioned object list to the layer and reupload scene


#14

:+1: as soon as i have a time i will take a look


#15

well … i looked into the file and its setup and requirements.

as i expected the straightforward position comparison is very slow because of pretty big meshes and pure algorithm

anyway here is the code:

fn getOpenEdgeVerts mesh = 
(
	ee = meshop.getopenedges mesh
	meshop.getvertsusingedge mesh ee
)
fn isMeshCloseEnough source target threshold:10 open:off = 
(
	near = off
	sv = if open then getOpenEdgeVerts source else source.verts as bitarray
	tv = if open then getOpenEdgeVerts target else target.verts as bitarray
	
	for s in sv while not near do
	(
		p = getvert source s
		for t in tv while not near do 
		(
			near = distance p (getvert target t) <= threshold
		)
	)
	near
)

fn findMeshesCloseEnough sources: targets: threshold:10 open:on = 
(
	if sources == unsupplied do sources = selection as array
	sources = for source in sources where iskindof source Editable_mesh collect source
	if targets == unsupplied do targets = for target in geometry where iskindof target Editable_mesh collect target
	targets = for target in targets where (finditem sources target == 0) collect target
	
	format ">> s:% t:%\n" sources.count targets.count
	
	_sources = deepcopy (sources as array)
	
	for source in _sources do
	(
		format "% >>" (finditem sources source)
		
		for i = targets.count to 1 by -1 while not keyboard.escpressed do
		(
			target = targets[i]
			if (isMeshCloseEnough source target threshold:threshold open:open) do
			(
				append sources target
				deleteitem targets i
			)
		)
		format " s:% t:%\n" sources.count targets.count
	)
	sources
)

fn findAllMeshesCloseEnough sources: targets: threshold:10 open:on = 
(
	if sources == unsupplied do sources = selection as array
	sources = for source in sources where iskindof source Editable_mesh collect source
		
	if targets == unsupplied do targets = for target in geometry where iskindof target Editable_mesh collect target
	targets = for target in targets where (finditem sources target == 0) collect target

	format ">> s:% t:%\n" sources.count targets.count
	
	data = [0,0]
	curr = [sources.count, targets.count]
	for source in sources while not keyboard.escpressed do
	(
		data = copy curr
		
		format "% >>" (finditem sources source)
				
		for i = targets.count to 1 by -1 while not keyboard.escpressed do
		(
			target = targets[i]
			if (isMeshCloseEnough source target threshold:threshold open:open) do
			(
				append sources target
				deleteitem targets i
			)
		)
		format " s:% t:%\n" sources.count targets.count
		curr = [sources.count, targets.count]
	)
	sources
)

you can select the first mesh and run (with a specified threshold):

all = findAllMeshesCloseEnough threshold:10
select all

this function recursively adds close enough meshes to the list and grows the search. it stops when no new target was added.

the function shows the trace (where you can see timing and algorithm working)

i found that most meshes are ā€˜connected’ by open edges, so there is no reason to search all verts.
the function has an option <open: true/false>, which controls what set of verts to use (open or all). so, see the difference

the possible optimization could be:
#1 check meshes bounding box intersection before verts searching to ensure that the searching is needed at all
#2 of course it’s better to use Kd-tree search algorithm … but it’s a subject of another topic
(with a smart Kd-tree full search takes about 0.1 sec on my machine)

good luck!


#16

… and of course any suggestions of possible optimization are welcome. i will be interested to see them too


#17

thanks Denis, just launch it on my machine - still processing this example file.

I just thought if selection of all scene meshes and then searching for the meshes with shared vertices positions and then deviding those groups somehowe (by putting into different layers maybe?) would speed up the process

btw the process is finished: in around 5-7 minutes it does exactly what needs

edit: I’ve read the k-dimensional-tree wiki and this is quite interesting technology, how did you applied to my example file?


#18

Kd-tree algorithms are included in my MXS extension


#19

it is private I suppose?


#20

it’s still private at the moment … but I think I can open it for free. It all depends on my time and ongoing interest in MXS as a whole… which I doubt very much lately


#21

could it be used out of the box or it’s need to be wraped in the code search algoritm for the topic task?


#22

maybe it can… but it’s more what i want to show in this topic


#23

Thanks again for the help, Denis!


#24

I have one question regarding the bounding box intersection. For not rotated Box primitive the corners of the boundingbox have the same position as the vertices of the mesh. The built-in intersects command do not offer threshold parameter, so if the two boxes are positioned as the yellow and red object in the image below, the intersects command will return false.

So, what is the real code behind the ā€œintersectsā€ command, and is the maxscript version of this code faster enough to be used with custom bboxes? For example, using the same image, the user will use the real bbox of the yellow object but as bbox of the red object will provide a ā€œbboxā€ 10 units larger in all directions, so the maxscript code to detect the intersection before to go to vertex by vertex comparison.


#25

sure we have to enlarge bboxes if we take some threshold into account


#26

And can this be done with built-in intersects command?


#27

correct… but for this specific scene it will not help. most of searching objects intersect by their bb

there is more complicated but more accurate solution - check intersection of ā€˜local’ bboxes in world space


#28

I am asking for general cases. :slight_smile:I have done a quick search in the internet but was unable to find the code for 3d bbox intersection. Tomorrow I will continue searching. :slight_smile:

there is more complicated but more accurate solution - check intersection of ā€˜local’ bboxes in world space
For non rotated Box primitives the local and world bboxs have the same corner points, right?