WCSketchAlignmentSuggestion Evaluate(WCSketchWorkbench *wb, WCGeometricObject *object, WPFloat &x, WPFloat &y) { WPFloat dist = 10.0; WCVector4 start, end; //Look through all lines std::map<WCGeometricLine*,WCEventController*>::iterator lineIter; for (lineIter = wb->Sketch()->LineMap().begin(); lineIter != wb->Sketch()->LineMap().end(); lineIter++) { //See if matches object - if not evaluate for match if ((*lineIter).first != object) { //Inverse project line onto plane start = (*lineIter).first->Begin(); end = (*lineIter).first->End(); start = wb->Sketch()->ReferencePlane()->InverseTransformMatrix() * start; end = wb->Sketch()->ReferencePlane()->InverseTransformMatrix() * end; //Determine distance to line dist = PointToLineDistance2(start.I(), start.J(), end.I(), end.J(), x, y); //If close enough, project to closest point on line if (dist < (0.6 * wb->Grid()->XSpacing())) { if (IsWithinLine(start.I(), start.J(), end.I(), end.J(), x, y)) { //Move x, y onto the line ProjectPointToLine2(start.I(), start.J(), end.I(), end.J(), x, y); //Return the suggestion return WCSketchAlignmentSuggestion::CoincidentToLine((*lineIter).first); } } } } //Otherwie, no suggestion at this time return WCSketchAlignmentSuggestion::None(); }
float PointToLineDistance1( float x, float y, float slope, float intercept ) { float xa, ya; if ( slope == NaN ) { return ABS(x-intercept); } else if ( slope == 0.0 ) { return ABS(intercept-y); } else { ya = x*slope + intercept; xa = ( y - intercept ) / slope; } return PointToLineDistance2(x,ya,xa,y,x,y); }
Polygon PolygonLineReduction( Polygon poly, int minr, int maxr ) { int size = poly->numberOfVertices; FVec hist1 = FVecNew(0,size-1); int range, i, r, l; float x1, y1, x2, y2, x3, y3; for (range=minr;range<=maxr;range++) { for (i=0;i<size;i++) { x2 = poly->vertices[i].x; y2 = poly->vertices[i].y; r = ( i + range ) % size; l = ( i + size - range ) % size; x1 = poly->vertices[l].x; y1 = poly->vertices[l].y; x3 = poly->vertices[r].x; y3 = poly->vertices[r].y; float dist = PointToLineDistance2(x1,y1,x3,y3,x2,y2); hist1->values[i] += dist; } } WrapGaussianBlur1D(hist1->values,hist1->l,hist1->r,5); FVecDivideBy(hist1,FVecMax(hist1)); Polygon peaks = NewPolygon(10); for ( i = 0; i < size ; i++ ) { float peak = hist1->values[i]; if ( peak < 0.3 ) continue; l = ( i + size - 1 ) % size; r = ( i + 1 ) % size; if ( hist1->values[r] > peak ) continue; if ( hist1->values[l] > peak ) continue; x1 = poly->vertices[i].x; y1 = poly->vertices[i].y; AddPolygonVertex(peaks,x1,y1); } size = peaks->numberOfVertices; for (i=0;i<size;i++) { r = ( i + 1 ) % size; x1 = peaks->vertices[i].x; y1 = peaks->vertices[i].y; x2 = peaks->vertices[r].x - x1; y2 = peaks->vertices[r].y - y1; float dist1 = sqrt( x2*x2 + y2*y2 ); peaks->vertices[i].z = dist1; } FVecFree(hist1); return peaks; }