The potential of search()

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

The potential of search()

Ronaldo
I just found that the function search in OpenSCAD has a greater potential than the documentation may suggests. The examples in the manual are restricted to the search of characters and numerical values. However, it seems that any kind of object may be used in an appropriate call of search. For instance:

echo(search([[[0],1]], [[[1,2],0],[[0,1],1],[[[0],1],3],[[2,3],4]], 0, 0));
// ECHO: [[2]]

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The potential of search()

TLC123
I commonly search for vertices in list of vertices

vertices= [[5,46,83],[97,28,66],[92,98,72],[62,28,60],[62,28,60],
[36,5,37],[24,36,41],[97,28,66] ];
 
uniquevertices =[   for (i = [0:len(vertices)-1 ])    if
(search([vertices[i]],vertices ,1)==[i]) vertices[i]  ];

 
Here in a mesh welding routine

function glue(points,faces)=
    points==[]?[]:
     let( usedpoints=([for(i=faces,j=i)points[j]]),
    upoints=truepoints(unique(usedpoints)),
    nfaces= [for(i=faces)[for(j=i)
    search([points[j]],upoints,1)[0]]])
    [upoints,nfaces];

function truepoints(p)= [for(i=p)if(i!=undef)i];

function  uniquejoin(m,n) =  
    concat(m,[   for (i = n)    if (search([i],m,1)==[[]]) i  ]);
       
function  unique(m,first,last) =
    last==undef?unique(m,0,len(m)-1) :
    last-first>1? let(mid=(first+last)/2)
    uniquejoin(  unique(m,first,floor(mid)),  unique(m,ceil(mid),last) )
    :m[first]==undef? [m[last]]:
     m[last]==undef?[m[first]] :
    concat([m[first]],[if( m[last]!=m[first])m[last]] ) ;



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The potential of search()

Ronaldo
Nice, thank you.

I did not dig deeper into glue() but if its intention is to eliminate point duplicates in a structure [points, faces] for polyhedron call, remember we don't need to do it because OpenSCAD (or CGAL) unifies polyhedron vertices with the same coordinates. I use that a lot without gluing.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The potential of search()

Ronaldo
In reply to this post by TLC123
I have found a simpler version for the mesh welding task:

// clean pdata pd=[points, faces] by eliminating all repeated
// vertices of points and apdating faces accordingly

function cleanPdata(pd) =
  let( s     = search(pd[0],pd[0],1),
       nvrts = [for(i=[0:len(s)-1]) if(i==s[i]) pd[0][i] ],
       s2    = search(pd[0],nvrts,1),
       nfcs  = [for(f=pd[1])[for(v=f) s2[v] ] ] )
  [ nvrts, nfcs ];




--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: The potential of search()

TLC123
 I weld my meshes before doing secondary operations . like relaxation,
subdivision collision, or fitting the mesh to some constraints



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org