private void DrawSegmentMeshLines(SegmentMeshInfo transform, XNAColor color) { if (transform == null || transform.arap == null) { return; } List <PointF> vts = transform.arap.GetMeshVertexList(); vertexList.Clear(); for (int i = 0; i < vts.Count; i += 3) { XNAVector3 pos1 = new XNAVector3(vts[i].X, -vts[i].Y, 0); XNAVector3 pos2 = new XNAVector3(vts[i + 1].X, -vts[i + 1].Y, 0); XNAVector3 pos3 = new XNAVector3(vts[i + 2].X, -vts[i + 2].Y, 0); vertexList.Add(new VertexPositionColorTexture(pos1, color, XNAVector2.Zero)); vertexList.Add(new VertexPositionColorTexture(pos2, color, XNAVector2.Zero)); vertexList.Add(new VertexPositionColorTexture(pos2, color, XNAVector2.Zero)); vertexList.Add(new VertexPositionColorTexture(pos3, color, XNAVector2.Zero)); vertexList.Add(new VertexPositionColorTexture(pos3, color, XNAVector2.Zero)); vertexList.Add(new VertexPositionColorTexture(pos1, color, XNAVector2.Zero)); } bool succeed = FlushVertexList(vertexList, true, false, null); if (!succeed) { return; } DrawLineList(vts.Count / 2); }
private void DrawSegmentMeshes(Segment seg, SegmentMeshInfo transform, XNAColor color, bool textureEnabled) { if (seg == null || seg.bmp == null || transform == null || transform.arap == null) { return; } Texture2D texture = null; if (textureEnabled) { texture = ToTexture2D(seg.bmp); if (texture == null || texture.Width <= 0 || texture.Height <= 0) { return; } } if (saveRenderedSegment) { var bmp = transform.arap.ToBitmap(); if (bmp != null) { bmp.Save("./" + seg.name + ".bmp"); } } List <PointF> vts = transform.arap.GetMeshVertexList(); List <PointF> cds = !textureEnabled ? null : transform.arap.GetMeshCoordList(texture.Width, texture.Height); if (textureEnabled && (cds == null || cds.Count != vts.Count)) { return; } vertexList.Clear(); for (int i = 0; i < vts.Count; i += 3) { XNAVector3 pos1 = new XNAVector3(vts[i].X, -vts[i].Y, 0); XNAVector3 pos2 = new XNAVector3(vts[i + 1].X, -vts[i + 1].Y, 0); XNAVector3 pos3 = new XNAVector3(vts[i + 2].X, -vts[i + 2].Y, 0); XNAVector2 coord1 = !textureEnabled ? XNAVector2.Zero : new XNAVector2(cds[i].X, cds[i].Y); XNAVector2 coord2 = !textureEnabled ? XNAVector2.Zero : new XNAVector2(cds[i + 1].X, cds[i + 1].Y); XNAVector2 coord3 = !textureEnabled ? XNAVector2.Zero : new XNAVector2(cds[i + 2].X, cds[i + 2].Y); vertexList.Add(new VertexPositionColorTexture(pos1, color, coord1)); vertexList.Add(new VertexPositionColorTexture(pos2, color, coord2)); vertexList.Add(new VertexPositionColorTexture(pos3, color, coord3)); } bool succeed = FlushVertexList(vertexList, true, textureEnabled, texture); if (!succeed) { return; } DrawMeshes(vts.Count / 3); }
//-------------------------------------------------------- private void UpdateSegmentTransformView(SegmentMeshInfo transform) { posXTrackbar.Value = Math.Min(posXTrackbar.Maximum, Math.Max(posXTrackbar.Minimum, (int)transform.position.X)); posYTrackbar.Value = Math.Min(posYTrackbar.Maximum, Math.Max(posYTrackbar.Minimum, (int)transform.position.Y)); rotTrackbar.Value = Math.Min(rotTrackbar.Maximum, Math.Max(rotTrackbar.Minimum, (int)transform.angle)); scaleXTrackbar.Value = Math.Min(scaleXTrackbar.Maximum, Math.Max(scaleXTrackbar.Minimum, (int)transform.scale.X * 10)); scaleYTrackbar.Value = Math.Min(scaleYTrackbar.Maximum, Math.Max(scaleYTrackbar.Minimum, (int)transform.scale.Y * 10)); }
public ConnectPair(BoneAnnotation bone, SegmentMeshInfo meshInfo1, CharacterRange section1, SegmentMeshInfo meshInfo2, CharacterRange section2) { this.bone = bone; this.meshInfo1 = meshInfo1; this.sectionRange1 = section1; this.meshInfo2 = meshInfo2; this.sectionRange2 = section2; }
public Segment AssignSegment(string key, Segment seg) { if (seg == null) { return(null); } var newSeg = new Segment(seg, key); if (segments.Select(s => s.name).Contains(newSeg.name)) { return(null); } segments.Add(newSeg); transformDict[key] = new SegmentMeshInfo(seg, true); return(newSeg); }
public void SetMeshInfo(string name, SegmentMeshInfo m) { if (m == null) { return; } if (editingUnit == null) { return; } if (!editingUnit.transformDict.ContainsKey(name)) { return; } editingUnit.transformDict[name] = m; }
public SegmentMeshInfo(SegmentMeshInfo m) { position = m.position; angle = m.angle; scale = m.scale; reverse = m.reverse; arap = new ARAPDeformation(m.arap); sections = new List <CharacterRange>(m.sections); an = new SkeletonAnnotation(m.an, true); boneToControls = new Dictionary <BoneAnnotation, List <PointF> >(m.boneToControls); crossDict = new Dictionary <BoneAnnotation, CrossBoneSection>(m.crossDict); }
private void DrawCompositedUnit(ComposedUnit composedUnit, Composition composition) { if (composedUnit == null) { return; } if (composition == null) { return; } if (composedUnit.segments == null) { return; } for (int i = 0; i < composedUnit.segments.Count; i++) { Segment seg = composedUnit.segments[i]; if (seg == null) { continue; } if (!composedUnit.transformDict.ContainsKey(seg.name)) { continue; } SegmentMeshInfo transform = composedUnit.transformDict[seg.name]; DrawSegmentMeshes(seg, transform, XNAColor.White, true); if (seg == composition.editingSegment) { DrawSegmentMeshes(seg, transform, new XNAColor(0.2f, 0.2f, 0.2f, 0.1f), false); DrawSegmentMeshLines(transform, XNAColor.Blue); DrawSkeleton(transform.an, new Dictionary <JointAnnotation, XNAColor>(), XNAColor.Gray); Dictionary <PointF, XNAColor> ptColorDict = new Dictionary <PointF, XNAColor>(); if (composition.editingControlPoint != null) { ptColorDict[composition.editingControlPoint.Value] = XNAColor.Red; } if (composition.nearestControlPoint != null) { ptColorDict[composition.nearestControlPoint.Value] = XNAColor.Yellow; } DrawSegmentControlPoints(transform, ptColorDict, XNAColor.Black); } } }
private void DrawSegmentControlPoints(SegmentMeshInfo transform, Dictionary <PointF, XNAColor> ptColorDict, XNAColor defaultColor) { if (transform == null || transform.arap == null) { return; } foreach (var pt in transform.arap.controlPoints) { if (ptColorDict.ContainsKey(pt)) { DrawPoint((int)pt.X, (int)pt.Y, ptColorDict[pt], 10); } else { DrawPoint((int)pt.X, (int)pt.Y, defaultColor, 10); } } }
public static void Fitting(SegmentMeshInfo mesh, SkeletonAnnotation refSkeleton) { mesh.position = Point.Empty; mesh.angle = 0; if (mesh.arap == null) { return; } Dictionary <BoneAnnotation, List <PointF> > sCps = mesh.boneToControls; foreach (var kv in sCps) { BoneAnnotation b = kv.Key; List <PointF> orgPts = kv.Value; BoneAnnotation br = GetBoneAnnotation(refSkeleton, b); if (br == null) { continue; } if (orgPts.Count <= 1) { continue; } for (int i = 0; i < orgPts.Count; i++) { float t = (float)i / (orgPts.Count - 1); float x = br.src.position.X * (1 - t) + br.dst.position.X * t; float y = br.src.position.Y * (1 - t) + br.dst.position.Y * t; PointF?pt = mesh.arap.OrgToCurControlPoint(orgPts[i]); if (pt == null || !pt.HasValue) { continue; } mesh.arap.TranslateControlPoint(pt.Value, new PointF(x, y), false); } } mesh.arap.FlushDefomation(); }
private static void ExpandSegments(List <SegmentMeshInfo> meshes, SkeletonAnnotation an, List <ConnectPair> pairs) { int _cnt = 0; foreach (var m in meshes) { if (m.arap == null) { continue; } m.arap.controlPoints.Clear(); SegmentMeshInfo.SetPathControlPoints(m.arap); m.arap.BeginDeformation(); m.arap.ToBitmap().Save("../../../Test2/" + (_cnt++) + ".png"); } foreach (var b in an.bones) { foreach (var p in pairs) { if (p.bone != b) { continue; } var m1 = p.meshInfo1; var m2 = p.meshInfo2; if (m1.arap == null || m2.arap == null) { continue; } var path1 = m1.arap.GetPath(); var path2 = m2.arap.GetPath(); var ranges1 = SectionToCurves(path1, p.sectionRange1, 5, 30); var ranges2 = SectionToCurves(path2, p.sectionRange2, 5, 30); if (ranges1 == null || ranges2 == null) { continue; } var curves1 = GetSortedCurves(path1, ranges1, b); var curves2 = GetSortedCurves(path2, ranges2, b); if (curves1.Count != 2 || curves2.Count != 2) { continue; } List <Tuple <PointF, PointF> > move1 = new List <Tuple <PointF, PointF> >(); List <Tuple <PointF, PointF> > move2 = new List <Tuple <PointF, PointF> >(); for (int i = 0; i < 2; i++) { var p1 = curves1[i].First(); var v1 = new PointF(p1.X - curves1[i].Last().X, p1.Y - curves1[i].Last().Y); var p2 = curves2[i].First(); var v2 = new PointF(curves2[i].Last().X - p2.X, curves2[i].Last().Y - p2.Y); // 2点かぶらせる int cnt = curves1[i].Count + curves2[i].Count - 2; if (cnt <= 1) { continue; } for (int j = 0; j < curves1[i].Count; j++) { PointF to = FMath.HelmitteInterporate(p1, v1, p2, v2, (float)j / (cnt - 1)); if (j == curves1[i].Count - 1) { move1.Add(new Tuple <PointF, PointF>(curves1[i][j], to)); } m1.arap.TranslateControlPoint(curves1[i][j], to, false); } for (int j = 0; j < curves2[i].Count; j++) { PointF to = FMath.HelmitteInterporate(p1, v1, p2, v2, (float)(-j + cnt - 1) / (cnt - 1)); if (j == curves2[i].Count - 1) { move2.Add(new Tuple <PointF, PointF>(curves2[i][j], to)); } m2.arap.TranslateControlPoint(curves2[i][j], to, false); } } List <PointF> sections1 = new List <PointF>(); List <PointF> sections2 = new List <PointF>(); for (int i = p.sectionRange1.First + 1; i < p.sectionRange1.First + p.sectionRange1.Length - 1; i++) { sections1.Add(path1[Rem(i, path1.Count)]); } for (int i = p.sectionRange2.First + 1; i < p.sectionRange2.First + p.sectionRange2.Length - 1; i++) { sections2.Add(path2[Rem(i, path2.Count)]); } List <PointF> newSection1 = ARAPDeformation.Deform(sections1, move1); List <PointF> newSection2 = ARAPDeformation.Deform(sections2, move2); if (newSection1.Count == sections1.Count) { for (int i = 0; i < newSection1.Count; i++) { m1.arap.TranslateControlPoint(sections1[i], newSection1[i], false); } } if (newSection2.Count == sections2.Count) { for (int i = 0; i < newSection2.Count; i++) { m2.arap.TranslateControlPoint(sections2[i], newSection2[i], false); } } m1.arap.FlushDefomation(); m2.arap.FlushDefomation(); } } foreach (var m in meshes) { if (m.arap == null) { continue; } m.arap.EndDeformation(); } }