Пример #1
0
void Slice::ConnectSegmentNeighbors()
{
	unsigned int s;
    unsigned int potential;
    unsigned int s2;
	double potentialangle;
	double potentialDist;
	double minDist = 10000.0;

	Segment* thisSeg = NULL;
	Segment* thatSeg = NULL;
    QVector2D* thisPoint = NULL;

    QVector2D* thatPoint = NULL;

    std::vector<Segment*> sameXStripSegs;
    std::vector<Segment*> potentialLeadSegs;

    Segment* finalLeadSeg = NULL;

	for(s = 0; s < segmentList.size(); s++)//compare from every segment
	{
		thisSeg = segmentList[s];
		thisPoint = &thisSeg->p2;//compare from the 2nd point on "this" segment

		if(thisSeg->leadingSeg)//no need to add a connection if there already is one!
			continue;
		
		potentialLeadSegs.clear();//clear out the potentials list
		

        GetSegmentsAroundX(sameXStripSegs, thisPoint->x());


        //////////////////////////////////////
        for(s2 = 0; s2 < sameXStripSegs.size(); s2++)//to every other segment in ring
		{
			//make sure its not the same segment
			if(s == s2)
			{continue;}
			
            thatSeg = sameXStripSegs[s2];
			if(thatSeg->trailingSeg)//already connected to a trailing segment...
				continue;
			
			thatPoint = &thatSeg->p1;//to the first point of "that" segment
            if(IsZero(Distance2D(*thisPoint, *thatPoint),0.03))//they are close enough to each other
            {
				potentialLeadSegs.push_back(thatSeg);
            }
		}
        //////////////////////////////////////

		//sort through and pick from the potential pile
		//we want to pick a segment with the sharpest change in direction!
		//
		//
		//1>>>>>>>>>A>>>>>>>>2 1>>>>>>>>B>>>>>>>>>2
		//                    ^
		//             large delta angle/ Right Wieghted
		minDist = 100000.0;
		finalLeadSeg = NULL;
		potentialangle = 0.0;
		for(potential = 0; potential < potentialLeadSegs.size(); potential++)
		{
			thatSeg = potentialLeadSegs[potential];
			//potentialDot = (thisSeg->normal.x() * thatSeg->normal.x()) + (thisSeg->normal.y() * thatSeg->normal.y()); //gives a number indicating how sharp the angle is, -1 is the sharpest (-1,1)
			//potentialCross = (thisSeg->normal.x() * thatSeg->normal.y()) - (thisSeg->normal.y() * thatSeg->normal.x());//gives a number indicating a right or left turn. (uphill is positive), (downhill is negative) (-1,1)
			potentialDist = Distance2D(thisSeg->p2, thatSeg->p1);
			
			if(potentialDist < minDist)
			{
				minDist = potentialDist;
				finalLeadSeg = potentialLeadSegs[potential];
			}
			
		}
		if(finalLeadSeg)
		{
			thisSeg->leadingSeg = finalLeadSeg;
			finalLeadSeg->trailingSeg = thisSeg;

			thisSeg->p2 = finalLeadSeg->p1;
			thisSeg->FormNormal();
		}
	}
}
Пример #2
0
int Loop::CorrectDoubleBacks()
{
    int numDoubleBacks = 0;
    unsigned int s;

    double normalsDot;

    std::vector<Segment*> keepList;
    Segment* thisSeg;
    Segment* thatSeg;
    if(segListp.size() <= 3)
    {
        return 0;
    }

    //first we should find some double backs....
    for(s = 0; s < segListp.size(); s++)
    {
        if(segListp[s]->chucked)
        {
            continue;//if segments already have been thrown
        }

        thisSeg = segListp[s];
        thatSeg = thisSeg->leadingSeg;

        //get dot product
        normalsDot = (thisSeg->normal.x() * thatSeg->normal.x()) + (thisSeg->normal.y() * thatSeg->normal.y());

        if(normalsDot < -0.999)//doubleback (1-Dot)*2*180 = Degrees freedom,  current(-0.999) = 0.36 degrees
        {
            numDoubleBacks++;
            //compute length of thisSeg and thatSeg
            //thislength = Distance2D(thisSeg->p1, thisSeg->p2);
            //thatlength = Distance2D(thatSeg->p1, thatSeg->p2);


            thisSeg->p2 = thatSeg->p2;
            thisSeg->FormNormal();
            thisSeg->leadingSeg = thatSeg->leadingSeg;
            thatSeg->leadingSeg->trailingSeg = thisSeg;
            thatSeg->leadingSeg = NULL;
            thatSeg->trailingSeg = NULL;
            thatSeg->chucked = true;

            s = 0;//start from beggining of list again
        }
    }

    if(numDoubleBacks > 0)
    {
        for(s = 0; s < segListp.size(); s++)
        {
            if(segListp[s]->chucked)
            {
                segListp[s]->chucked = false;
            }
            else
            {
                keepList.push_back(segListp[s]);
            }
        }
        segListp.clear();
        segListp = keepList;
        numSegs = segListp.size();
    }

    return numDoubleBacks;
}