PDA

View Full Version : TraverseLoop



faulknermano
03-04-2008, 12:11 AM
havent seen anything like this yet, so i wrote this. it's a script that selects by "crawling", point by point, or poly by poly, along a loop that's defined by the initial selection, and the subsequent last and "second-to-last" selections. it's like loop-constrained version of the Expand Selection command. the behavior is more apparent when you use the script. both point and polygon selection modes are supported.

a second script was also made to roll back the selection made.

tested with lw931 only.

*note: took off a isSymmetry() func because it was using VMap_Dump, a plugin that allows detection of symmetry mode. because i have forgotten the author's name, i'm not sure if i'm allowed to post it here. if anyone has any idea who wrote VMap_Dump, please tell so that i could insert the function here and make the whole thing more functional.



// written by Lernie Ang, faulknermano
@warnings 0


findCommonPolys: p1, p2
{
k = 0;
a = p1.polygon();

if(p2 == nil)
return(a);

b = p2.polygon();

for(i=1;i<=a.size();i++)
{
for(j=1;j<=b.size();j++)
{
if(a[i] == b[j])
cp[++k] = a[i];
}

}
if(cp)
return(cp);
else
return(nil);
}
findCommonEdge: poly1, poly2
{

commonPoints = nil;
if(!poly1 || !poly2)
return(commonPoints);
// get point ids of each poly
for(i=1;i<=polypointcount(poly1);i++)
{
for(j=1;j<=polypointcount(poly2);j++)
{
if(poly1.points[i] == poly2.points[j])
commonPoints[commonPoints.size()+1] = poly1.points[i];
}
}
return(commonPoints);
}

main
{
// determine selection type;
selmode(USER);
pc = pointcount();
(polyc) = polycount();

if(polyc)
main_poly();
else
main_point();
}
main_point
{
// 1st point must share poly with 2nd
// 3rd point must share poly with 2nd point but must not be 1st point

selmode(USER);
i = 0;
//sym = isSymmetryActive();
sym = false;
pc = editbegin();

if((pc < 2 && sym == false) || pc < 4 && sym == true)
error("Select at least two points.");

if(sym == false)
{
lastPoint = points[pc];
firstPoint = points[pc - 1];
}
else if(sym == true)
{
lastPoint = points[pc - 1];
firstPoint = points[pc - 3];
}

commonPolys = findCommonPolys(lastPoint,firstPoint);
if(commonPolys == nil)
error("Must select adjacent points.");
lastPointCommonPolys = lastPoint.polygon();

foreach(x,points)
pnts[++i] = x;

// look for polygons which lastPoint and firstPoint dont share

for(i=1;i<=lastPointCommonPolys.size();i++)
{
for(j=1;j<=commonPolys.size();j++)
{
if(lastPointCommonPolys[i] == commonPolys[j])
{
lastPointCommonPolys[i] = nil;
break;
}
}
}
lastPointCommonPolys.pack();
lastPointCommonPolys.trunc();
poly1 = lastPointCommonPolys[1];
if(lastPointCommonPolys.size() > 1)
{
poly2 = lastPointCommonPolys[2];
commonEdgePoints = findCommonEdge(poly1,poly2);
if(commonEdgePoints == nil)
return;
if(commonEdgePoints[1] != lastPoint)
pnts[pnts.size()+1] = commonEdgePoints[1];
if(commonEdgePoints[2] != lastPoint)
pnts[pnts.size()+1] = commonEdgePoints[2];
}
else
{
// scan single poly and locate point which is:
// one point away from lastpoint
// does not share poly with first point
if(!poly1)
return;
for(i=1;i<=poly1.pointCount;i++)
{
//info(poly1.points[i]," ",lastPoint);
if(sharesPolyWith(poly1.points[i],firstPoint) == true)
continue;
if(isOnePointAway(poly1.points[i],lastPoint) == true)
pnts[pnts.size()+1] = poly1.points[i];
}
}

editend();
selpoint(SET,POINTID,pnts);
//selpoint(CLEAR);
//selpoint(SET,POINTID,poly1.points[4]);


//selpoint(SET,POINTID,lastPoint);

}
sharesPolyWith: pnt1, pnt2
{
if(findCommonPolys(pnt1,pnt2))
return(true);
else
return(false);

}
isOnePointAway: pnt1, pnt2
{

// find their common polys
commonPolys = findCommonPolys(pnt1,pnt2);
// only need one common poly to check
cpoly = commonPolys[1];

// scan cpoly points
for(i=1;i<=cpoly.pointCount;i++)
{
p1 = cpoly.points[i];
if(i == cpoly.pointCount)
p2 = cpoly.points[1];
else
p2 = cpoly.points[i+1];

if(p1 == pnt1 && p2 == pnt2)
return(true);
if(p2 == pnt1 && p1 == pnt2)
return(true);
}
return(false);
}
main_poly
{

selmode(USER);
i = 0;
//sym = isSymmetryActive();
sym = false;
pc = editbegin();

// for the last two polys selected:


i = 0;
(polyc) = polycount();
poly1 = polygons[polyc - 1];
poly2 = polygons[polyc];

// look for the two points of poly2 that is not shared by poly1
for(i=1;i<=poly2.pointCount;i++)
{
myPnt = poly2.points[i];
for(j=1;j<=poly1.pointCount;j++)
{
if(poly2.points[i] == poly1.points[j])
{
myPnt = nil;
continue;
}
}
if(myPnt)
poly2pnts[poly2pnts.size()+1] = myPnt;
}

// determine what other polygon surrounding poly2 is sharing these two points.
pnt[1] = poly2pnts[1];
pnt[2] = poly2pnts[2];

pnt1polys = poly2pnts[1].polygon();
pnt2polys = poly2pnts[2].polygon();


// find common poly

commonPolys = findCommonPolys(pnt[1],pnt[2]);
editend();
selpolygon(SET,POLYID,commonPolys);
//selpoint(SET,POINTID,poly2pnts);




}





// written by Lernie Ang, faulknermano
@warnings 0
main
{
selmode(USER);
pc = pointcount();
(polyc) = polycount();

if(pc < 2)
return;

if(polyc)
main_p("polygon");
else
main_p("point");


}
main_p: mode
{

selmode(USER);
pc = editbegin();

if(mode == "point")
pnt = points[pc];
if(mode == "polygon")
{
(polyc) = polycount();
poly = polygons[polyc];


}


editend();
if(mode == "point")
selpoint(CLEAR,POINTID,pnt);
if(mode == "polygon")
selpolygon(CLEAR,POLYID,poly);

}

papou
03-06-2008, 04:02 PM
Hi learnie,
I have a script like this one from Glenn Tester, named "Select more"
Don't know where i get it from. i can't find it on his site: http://www.ghostoutpost.com

happy to see you are cooking again some stuff.

tais

faulknermano
03-06-2008, 07:14 PM
hmm, and i cant locate it in flay.com either. maybe tester's script is faster. the problem with the script above is that it has a slight lag. i'm trying to find out if there's a way i could make it a bit faster.