/// <summary> /// adjust vertical fitting value /// </summary> void ReCalculateFittingValues() { //(1) //clear all prev adjust value for (int i = _contours.Count - 1; i >= 0; --i) { List <Vertex> pnts = _contours[i].flattenPoints; for (int m = pnts.Count - 1; m >= 0; --m) { pnts[m].ResetFitAdjustValues(); } } //adjust the value when we move to new pixel scale (pxscale) //if we known adjust values for that pxscale before( and cache it) //we can use that without recalculation //-------------------- //(2) //select Horizontal BoneGroups for Vertical fitting: //for veritical fitting, we apply fitting value to each group. //each group may not need the same value. //-------------------- List <BoneGroup> selectedHBoneGroups = _groupingHelper.SelectedHorizontalBoneGroups; for (int i = selectedHBoneGroups.Count - 1; i >= 0; --i) { BoneGroup boneGroup = selectedHBoneGroups[i]; if (boneGroup._lengKind == BoneGroupSumLengthKind.Short) { continue; } EdgeLine[] h_edges = boneGroup.edges; if (h_edges == null) { continue; } // int edgeCount = h_edges.Length; //we need to calculate the avg of the glyph point //and add a total summary to this FitDiffCollector y_fitDiffCollector = new FitDiffCollector(); float groupLen = boneGroup.approxLength; // for (int e = edgeCount - 1; e >= 0; --e) { EdgeLine ee = h_edges[e]; //p y_fitDiffCollector.Collect(MyMath.CalculateDiffToFit(ee.P.Y * _pxScale), groupLen); //q y_fitDiffCollector.Collect(MyMath.CalculateDiffToFit(ee.Q.Y * _pxScale), groupLen); } float avg_ydiff = y_fitDiffCollector.CalculateProperDiff(); for (int e = edgeCount - 1; e >= 0; --e) { //TODO: review here again EdgeLine ee = h_edges[e]; ee.P.FitAdjustY = avg_ydiff; //assign px scale specific fit value ee.Q.FitAdjustY = avg_ydiff; //assign px scale specific fit value } } //--------------------------------------------------------- //(3) //vertical group for horizontal fit: //this different from the vertical fitting. //we calculate the value as a whole. //and apply it as a whole in later state List <BoneGroup> verticalGroups = _groupingHelper.SelectedVerticalBoneGroups; FitDiffCollector x_fitDiffCollector = new FitDiffCollector(); int j = verticalGroups.Count; for (int i = 0; i < j; ++i) { //1. the verticalGroup list is sorted, left to right //2. analyze in order left-> right BoneGroup boneGroup = verticalGroups[i]; if (boneGroup._lengKind != BoneGroupSumLengthKind.Long) { //in this case we focus on long-length bone group only continue; } EdgeLine[] v_edges = boneGroup.edges; if (v_edges == null) { continue; } int edgeCount = v_edges.Length; //we need to calculate the avg of the glyph point //and add a total summary to this float groupLen = boneGroup.approxLength; for (int e = 0; e < edgeCount; ++e) { EdgeLine ee = v_edges[e]; //TODO: review this //if (ee.IsLeftSide) //{ //focus on leftside edge //p x_fitDiffCollector.Collect(MyMath.CalculateDiffToFit(ee.P.X * _pxScale), groupLen); //q x_fitDiffCollector.Collect(MyMath.CalculateDiffToFit(ee.Q.X * _pxScale), groupLen); //} } //TODO: review here *** break; //only left most first long group ? } //(4) _avg_x_fitOffset = x_fitDiffCollector.CalculateProperDiff(); }
static Vector2f FindCutPoint(Vector2f p0, Vector2f p1, Vector2f p2, float cutAngle) { //a line from p0 to p1 //p2 is any point //return p3 -> cutpoint on p0,p1 //from line equation //y = mx + b ... (1) //from (1) //b = y- mx ... (2) //---------------------------------- //line1: //y1 = (m1 * x1) + b1 ...(3) //line2: //y2 = (m2 * x2) + b2 ...(4) //---------------------------------- //from (3), //b1 = y1 - (m1 * x1) ...(5) //b2 = y2 - (m2 * x2) ...(6) //---------------------------------- //y1diff = p1.Y-p0.Y ...(7) //x1diff = p1.X-p0.X ...(8) // //m1 = (y1diff/x1diff) ...(9) //m2 = cutAngle of m1 ...(10) // //replace value (x1,y1) and (x2,y2) //we know b1 and b2 //---------------------------------- //at cutpoint of line1 and line2 => (x1,y1)== (x2,y2) //or find (x,y) where (3)==(4) //---------------------------------- //at cutpoint, find x // (m1 * x1) + b1 = (m2 * x1) + b2 ...(11), replace x2 with x1 // (m1 * x1) - (m2 * x1) = b2 - b1 ...(12) // x1 * (m1-m2) = b2 - b1 ...(13) // x1 = (b2-b1)/(m1-m2) ...(14), now we know x1 //---------------------------------- //at cutpoint, find y // y1 = (m1 * x1) + b1 ... (15), replace x1 with value from (14) //Ans: (x1,y1) //---------------------------------- double y1diff = p1.Y - p0.Y; double x1diff = p1.X - p0.X; if (x1diff == 0) { //90 or 180 degree return(new Vector2f(p1.X, p2.Y)); } if (y1diff == 0) { return(new Vector2f(p2.X, p1.Y)); } //------------------------------ // //find slope double m1 = y1diff / x1diff; //from (2) b = y-mx, and (5) //so ... double b1 = p0.Y - (m1 * p0.X); // //from (10) //double invert_m = -(1 / slope_m); //double m2 = -1 / m1; //rotate m1 //--------------------- double angle = System.Math.Atan2(y1diff, x1diff); //rad in degree //double m2 = -1 / m1; double m2 = cutAngle == 90 ? //short cut (-1 / m1) : //or System.Math.Tan( //radial_angle of original line + radial of cutAngle //return new line slope System.Math.Atan2(y1diff, x1diff) + MyMath.DegreesToRadians(cutAngle)); //new m //--------------------- //from (6) double b2 = p2.Y - (m2) * p2.X; //find cut point //check if (m1-m2 !=0) double cutx = (b2 - b1) / (m1 - m2); //from (14) double cuty = (m1 * cutx) + b1; //from (15) return(new Vector2f((float)cutx, (float)cuty)); }