// 점을 vertex list에 추가 public void addVertex(PointF coord, int number) { if (vertexList.Count <= number) { for (int i = vertexList.Count; i <= number; i++) vertexList.Add(new Vertex2D()); } vertexList[number] = new Vertex2D(coord, number); }
public static bool closestPoint(PointF currentLocation, List<Vertex2D> vertexList, ref float min, ref Vertex2D vertex) { bool changed = false; foreach (Vertex2D item in vertexList) { if (item.coord.X != -1 && item.coord.distance(currentLocation) <= min) { min = item.coord.distance(currentLocation); vertex = new Vertex2D(item); changed = true; } } return changed; }
// 점을 vertex list에서 제거 public void removeVertex(int number) { vertexList[number] = new Vertex2D(); }
public static bool closestPoint(PointF currentLocation, List<Vertex2D> vertexList, ref float min, ref Vertex2D vertex, ref bool find) { if (closestPoint(currentLocation, vertexList, ref min, ref vertex)) { find = true; return true; } else return false; }
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (!mouseDownCapture) return; else mouseDownCapture = false; // 현재 위치를 끝점으로 하는 선을 그림 if ((vanishingLineButton.Checked || guideLineButton.Checked) && lineDraw) { if (radioButtonX.Checked) currentScene.addVanishingLine(tempLine, Scene.X); else if (radioButtonY.Checked) currentScene.addVanishingLine(tempLine, Scene.Y); else if (radioButtonZ.Checked) currentScene.addVanishingLine(tempLine, Scene.Z); else currentScene.addGuideLine(tempLine); lineDraw = false; savedLine = null; changePictureBoxImage(currentScene.display()); } else if (pointButton.Checked) { if (e.Location.X < 0 || e.Location.X > pictureBox1.Size.Width || e.Location.Y < 0 || e.Location.Y > pictureBox1.Size.Height || mousePoint.X == -1) return; if (e.Button == MouseButtons.Left) { currentScene.addVertex(mousePoint, pointNumber); addDataGridView(pointNumber, currentSceneNumber); pointNumber = currentScene.firstEmptyPointNumber(pointNumber + 1); changePictureBoxImage(currentScene.display()); } else if (e.Button == MouseButtons.Right && pointMove) { currentScene.addVertex(mousePoint, savedVertex.number); changePictureBoxImage(currentScene.display()); pointMove = false; } } else if (faceButton.Checked && e.Button == MouseButtons.Left) { PointF currentLocation = currentScene.picture2Image(e.Location); Vertex2D closestVertex = new Vertex2D(); float min = currentScene.threshold; if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex)) { if (tempFace == null) tempFace = new Face(); tempFace.vertexList.Add(closestVertex.number); } pictureBox1.Invalidate(); } else if (faceButton.Checked && e.Button == MouseButtons.Right) { if (tempFace != null && tempFace.Count >= 2) { model.face.Add(tempFace); changePictureBoxImage(currentScene.display()); } tempFace = null; } else if (selectButton.Checked) { // 선택 영역 안에 있는 line, point들을 고름 if (selectDrag) { PointF location = new PointF(Math.Min(selectPoint1.X, selectPoint2.X), Math.Min(selectPoint1.Y, selectPoint2.Y)); SizeF size = new SizeF(Math.Abs(selectPoint1.X - selectPoint2.X), Math.Abs(selectPoint1.Y - selectPoint2.Y)); selectArea = new RectangleF(location, size); if (currentScene.displayVanishingLine) { foreach (Line item in currentScene.vanishingLineX) if (selectArea.isIntersect(item)) select.Add(new SelectedObject(item, currentScene.vanishingLineX)); foreach (Line item in currentScene.vanishingLineY) if (selectArea.isIntersect(item)) select.Add(new SelectedObject(item, currentScene.vanishingLineY)); foreach (Line item in currentScene.vanishingLineZ) if (selectArea.isIntersect(item)) select.Add(new SelectedObject(item, currentScene.vanishingLineZ)); } if (currentScene.displayPoint) { foreach (Vertex2D item in currentScene.vertexList) if (selectArea.intersect(item.coord)) select.Add(new SelectedObject(item.coord, item.number)); } if (currentScene.displayGuideLine) { foreach (Line item in currentScene.guideLine) if (selectArea.isIntersect(item)) select.Add(new SelectedObject(item, currentScene.guideLine)); } if (currentScene.displayFace) { foreach (Face item in currentScene.faceList) { bool intersect = false; if (currentScene.isFaceIncluded(item)) { PointF[] points = currentScene.face2PointArray(item); for (int i = 0; i < item.Count; i++) { if (selectArea.intersect(points[i])) intersect = true; Line line; if (i == item.Count - 1) line = new Line(points[i], points[0]); else line = new Line(points[i], points[i + 1]); if (selectArea.isIntersect(line)) intersect = true; if (intersect) { select.Add(new SelectedObject(item)); break; } } } } } selectDrag = false; pictureBox1.Invalidate(); } } }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (vanishingLineButton.Checked && lineDraw) { tempLine.P2 = currentScene.picture2Image(e.Location); this.pictureBox1.Invalidate(); } else if (guideLineButton.Checked) { PointF currentLocation = currentScene.picture2Image(e.Location); Vertex2D closestVertex = new Vertex2D(); Line closestLine = new Line(); float min = currentScene.threshold; bool find = false; highlightLinePoint = false; if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex, ref find)) highlightLinePoint = false; if (Scene.closestLinePoint(currentLocation, currentScene.guideLine, ref min, ref closestVertex.coord, ref find)) highlightLinePoint = true; min = currentScene.threshold; Scene.closestLine(currentLocation, currentScene.guideLine, ref min, ref closestLine, ref find); if (find) { // closestLinePoint만 찾은 경우 -> closestLine은 무조건 찾음 if (highlightLinePoint) { highlightLine = closestLine; highlightPoint = closestVertex.coord; } // closestPoint를 찾은 경우 else if (closestVertex.coord.X != -1) { highlightLine = null; highlightPoint = closestVertex.coord; } // closestLine을 찾은 경우 else { highlightLine = closestLine; highlightPoint = new PointF(-1, -1); } } // 전혀 못 찾은 경우 else { highlightLine = null; highlightPoint = new PointF(-1, -1); } if (lineDraw) { if (highlightPoint.X != -1) tempLine.P2 = highlightPoint; else if (highlightLine != null) tempLine.P2 = highlightLine.projection(currentLocation); else tempLine.P2 = currentLocation; } pictureBox1.Invalidate(); } else if (pointButton.Checked) { PointF currentLocation = currentScene.picture2Image(e.Location); Vertex2D closestVertex = new Vertex2D(); Line closestLine = new Line(); float min = currentScene.threshold; bool find = false; highlightLinePoint = false; if (displayList.GetItemChecked(3)) if (Scene.closestLinePoint(currentLocation, currentScene.guideLine, ref min, ref closestVertex.coord, ref find)) highlightLinePoint = true; if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex, ref find)) highlightLinePoint = false; if (displayList.GetItemChecked(3)) { min = currentScene.threshold; Scene.closestLine(currentLocation, currentScene.guideLine, ref min, ref closestLine, ref find); } if (find) { // closestLinePoint만 찾은 경우 -> closestLine은 무조건 찾음 if (highlightLinePoint) { mousePoint = closestVertex.coord; highlightLine = null; highlightPoint = new PointF(-1, -1); } // closestPoint를 찾은 경우 else if (closestVertex.coord.X != -1) { mousePoint = currentLocation; highlightLine = null; highlightPoint = closestVertex.coord; } // closestLine을 찾은 경우 else { mousePoint = closestLine.projection(currentLocation); highlightLine = null; highlightPoint = new PointF(-1, -1); } } // 전혀 못 찾은 경우 else { mousePoint = currentLocation; highlightLine = null; highlightPoint = new PointF(-1, -1); } pictureBox1.Invalidate(); } else if (faceButton.Checked) { PointF currentLocation = currentScene.picture2Image(e.Location); Vertex2D closestVertex = new Vertex2D(); float min = currentScene.threshold; if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex)) highlightPoint = closestVertex.coord; else highlightPoint = new PointF(-1, -1); pictureBox1.Invalidate(); } else if (selectButton.Checked) { if (selectDrag) { selectPoint2 = currentScene.picture2Image(e.Location); pictureBox1.Invalidate(); } else { float min = currentScene.threshold; Line closestLine = new Line(); Vertex2D closestVertex = new Vertex2D(); Face closestFace = new Face(); bool line = false; bool point = false; bool face = false; bool faceLine = false; PointF currentLocation = currentScene.picture2Image(e.Location); // vertex중에 가까운 것 찾음 if (displayList.GetItemChecked(1)) { if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex, ref point)) highlightPointNumber = closestVertex.number; } // face중에 가까운 것 찾음 if (displayList.GetItemChecked(2)) { face = currentScene.closestFace(currentLocation, ref closestFace); faceLine = currentScene.closestFaceLine(currentLocation, ref min, ref closestFace); } // guideLine중에 가까운 것 찾음 if (displayList.GetItemChecked(3)) { if (Scene.closestLine(currentLocation, currentScene.guideLine, ref min, ref closestLine, ref line)) highlightLineList = currentScene.guideLine; } // vanishingLine중에 가까운 것 찾음 if (displayList.GetItemChecked(0)) { if (Scene.closestLine(currentLocation, currentScene.vanishingLineX, ref min, ref closestLine, ref line)) highlightLineList = currentScene.vanishingLineX; if (Scene.closestLine(currentLocation, currentScene.vanishingLineY, ref min, ref closestLine, ref line)) highlightLineList = currentScene.vanishingLineY; if (Scene.closestLine(currentLocation, currentScene.vanishingLineZ, ref min, ref closestLine, ref line)) highlightLineList = currentScene.vanishingLineZ; } // 가까우면 highlightLine이나 highlightPoint로 지정 if (point) { highlightPoint = closestVertex.coord; highlightLine = null; highlightFace = null; } else if (faceLine) { highlightPoint = new PointF(-1, -1); highlightLine = null; highlightFace = closestFace; } else if (line) { highlightLine = closestLine; highlightPoint = new PointF(-1, -1); highlightFace = null; } else if (face) { highlightPoint = new PointF(-1, -1); highlightLine = null; highlightFace = closestFace; } else { highlightLine = null; highlightPoint = new PointF(-1, -1); highlightFace = null; } pictureBox1.Invalidate(); } } }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { mouseDownCapture = true; if (this.vanishingLineButton.Checked) { tempLine.P1 = currentScene.picture2Image(e.Location); tempLine.P2 = currentScene.picture2Image(e.Location); lineDraw = true; } else if (this.guideLineButton.Checked) { if (highlightPoint.X != -1 && e.Button == MouseButtons.Right) { if (highlightLine.P1.equal(highlightPoint) || highlightLine.P2.equal(highlightPoint)) { if (highlightLine.P1.equal(highlightPoint)) tempLine.P1 = highlightLine.P2; else if (highlightLine.P2.equal(highlightPoint)) tempLine.P1 = highlightLine.P1; tempLine.P2 = currentScene.picture2Image(e.Location); savedLine = highlightLine; lineDraw = true; currentScene.removeGuideLine(highlightLine); highlightPoint = new PointF(-1, -1); highlightLine = null; changePictureBoxImage(currentScene.display()); } } else if (e.Button == MouseButtons.Left) { // highlightPoint가 있으면 그 점을 선의 시작점으로 if (highlightPoint.X != -1) { tempLine.P1 = highlightPoint; tempLine.P2 = highlightPoint; } // highlightLine이 있으면 현재 위치에서 line에 내린 수선의 발을 시작점으로 else if (highlightLine != null) { tempLine.P1 = highlightLine.projection(currentScene.picture2Image(e.Location)); tempLine.P2 = highlightLine.projection(currentScene.picture2Image(e.Location)); } else { tempLine.P1 = currentScene.picture2Image(e.Location); tempLine.P2 = currentScene.picture2Image(e.Location); } lineDraw = true; } } else if (this.pointButton.Checked && e.Button == MouseButtons.Right) { PointF currentLocation = currentScene.picture2Image(e.Location); Vertex2D closestVertex = new Vertex2D(); float min = currentScene.threshold; if (Scene.closestPoint(currentLocation, currentScene.vertexList, ref min, ref closestVertex)) { currentScene.removeVertex(closestVertex.number); removeDataGridView(closestVertex.number, currentSceneNumber); savedVertex = closestVertex; pointMove = true; } changePictureBoxImage(currentScene.display()); } // highlightLine이나 Point가 있다면 drag로 선택영역을 지정할 수 없음 else if (selectButton.Checked) { select = new List<ISelect>(); if (highlightLine != null) { select.Add(new SelectedObject(highlightLine, highlightLineList)); } else if (highlightPoint.X != -1) { select.Add(new SelectedObject(highlightPoint, highlightPointNumber)); } else if (highlightFace != null) { select.Add(new SelectedObject(highlightFace)); } else { selectDrag = true; selectPoint1 = currentScene.picture2Image(e.Location); selectPoint2 = currentScene.picture2Image(e.Location); } pictureBox1.Refresh(); } }
private void openButton_Click(object sender, EventArgs e) { string line; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt files (*.txt)|*.txt"; openFileDialog.Title = "Open the File"; if (openFileDialog.ShowDialog() == DialogResult.OK) { System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog.FileName); InitComponents(); fileName = openFileDialog.FileName; Scene scene = null; bool success = true; int pointNumber = 0; int sceneNumber = 0; while ((line = file.ReadLine()) != null) { if (line == "End Image") break; else if (line.Substring(0, 3) == "Img") { model.scene.Add(new Scene(file.ReadLine(), this.pictureBox1.Size, model.face)); sceneNumber = int.Parse(line.Substring(3)); imageList.Items.Add("Img" + sceneNumber.ToString()); dataGridView1.Columns.Add("Img" + sceneNumber.ToString(), "Img" + sceneNumber.ToString()); dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width = 50; scene = model.scene[sceneNumber]; pointNumber = 0; } else if (line.Substring(0, 2) == "vx") { Line item = new Line(line.Substring(3), ref success); if (success) scene.vanishingLineX.Add(item); } else if (line.Substring(0, 2) == "vy") { Line item = new Line(line.Substring(3), ref success); if (success) scene.vanishingLineY.Add(item); } else if (line.Substring(0, 2) == "vz") { Line item = new Line(line.Substring(3), ref success); if (success) scene.vanishingLineZ.Add(item); } else if (line.Substring(0, 1) == "v") { Vertex2D item = new Vertex2D(line.Substring(2), pointNumber, ref success); scene.addVertex(item.coord, item.number); addDataGridView(item.number, sceneNumber); pointNumber++; } else if (line.Substring(0, 1) == "g") { Line item = new Line(line.Substring(2), ref success); if (success) scene.guideLine.Add(item); } } while ((line = file.ReadLine()) != null) { if (line.Substring(0, 1) == "f") { model.face.Add(new Face(line.Substring(2))); } else break; } imageList.SetItemChecked(0, true); displayList.SetItemChecked(0, true); displayList.SetItemChecked(1, true); displayList.SetItemChecked(2, true); displayList.SetItemChecked(3, true); displayList.SetItemChecked(4, true); file.Close(); } }
public Vertex2D(Vertex2D vertex) { this.coord = vertex.coord; this.number = vertex.number; }