完成找最短距离功能。

Signed-off-by: facat <facat@facat.com>
This commit is contained in:
facat 2021-06-10 00:25:39 +08:00
parent bde8c2f462
commit b9810325fa
5 changed files with 93 additions and 3 deletions

View File

@ -20,8 +20,18 @@ namespace TransmissionGraphic
Matrix<double> sag_points = sag_and_value.Item1;
double sag_value = sag_and_value.Item2;
canvas.draw(sag_points);
canvas.draw(line.swing(30 / 180 * Math.PI));
canvas.text(String.Format("sag:{0}",sag_value), new TGVector3D(sag_points.At(1,0),sag_points.At(1,1),sag_points.At(1,2)));
canvas.draw(line.swing(1.0*90 / 180 * Math.PI));
Line line111 =new Line(new TGVector3D(50, 30, 0), new TGVector3D(430, 90, 1), 0.35 * 1e-3, 800);
Matrix<double> line111swing=line111.swing(1.0*45/180*Math.PI);
canvas.draw(line111swing);
Ruler ruler = new Ruler(line.curve(), line111swing);
Tuple<double, TGVector3D, TGVector3D> closest_distance = ruler.closest_distance();
canvas.draw(closest_distance.Item2, closest_distance.Item3);
canvas.save("abc.dxf");
Console.WriteLine(closest_distance.Item1);
Console.ReadKey();
}
}

View File

@ -57,6 +57,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="src\Graphic\Canvas.cs" />
<Compile Include="src\Graphic\Line.cs" />
<Compile Include="src\Graphic\Ruler.cs" />
<Compile Include="src\Transformation\Rotation.cs" />
<Compile Include="src\Transformation\Transformation.cs" />
<Compile Include="src\Type3D\Vector3D.cs" />

View File

@ -6,6 +6,7 @@ using WW.Cad.Model;
using WW.Cad.Model.Entities;
using WW.Cad.IO;
using MathNet.Numerics.LinearAlgebra;
using TransmissionGraphic.Type3D;
namespace TransmissionGraphic.Graphic
{
@ -25,7 +26,7 @@ namespace TransmissionGraphic.Graphic
public void draw(Matrix<double> points)
{
DxfModel model=this._model;
DxfModel model = this._model;
DxfPolyline3D polyline = new DxfPolyline3D();
DxfVertex3D[] vertex = new DxfVertex3D[points.RowCount];
for (int foo = 0; foo < points.RowCount; foo++)
@ -37,11 +38,32 @@ namespace TransmissionGraphic.Graphic
model.Entities.Add(polyline);
}
public void draw(TGVector3D point1, TGVector3D point2)
{
DxfModel model = this._model;
DxfVertex3D[] vertex = new DxfVertex3D[]
{
new DxfVertex3D(point1.x,point1.y,point1.z),
new DxfVertex3D(point2.x,point2.y,point2.z)
};
DxfPolyline3D polyline = new DxfPolyline3D();
polyline.Closed = false;
polyline.Vertices.AddRange(vertex);
model.Entities.Add(polyline);
}
public void save(string filePath)
{
DxfModel model = this._model;
DxfWriter.Write(filePath, model);
}
public void text(string text, TGVector3D point)
{
DxfModel model = this._model;
model.Entities.Add(new DxfText(text, new WW.Math.Point3D(point.x, point.y, point.z), 1d));
}
}
}

49
src/Graphic/Ruler.cs Normal file
View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransmissionGraphic.Type3D;
using MathNet.Numerics.LinearAlgebra;
namespace TransmissionGraphic.Graphic
{
class Ruler
{
private Matrix<double> _curve_points1;
private Matrix<double> _curve_points2;
public Ruler(Matrix<double> curve_points1, Matrix<double> curve_points2)
{
this._curve_points1 = curve_points1;
this._curve_points2 = curve_points2;
}
public Tuple<double, TGVector3D, TGVector3D> closest_distance()
{
Matrix<double> curve_points1 = this._curve_points1;
Matrix<double> curve_points2 = this._curve_points2;
double closest = 999999999999;
Vector<double> closest_poc1 = null;
Vector<double> closest_poc2 = null;
for (int foo = 0; foo < curve_points1.RowCount; foo++)
{
Vector<double> poc1 = curve_points1.Row(foo);// # point of curve 1
for (int bar = 0; bar < curve_points2.RowCount; bar++)
{
Vector<double> poc2 = curve_points2.Row(bar);
double distance = (poc1 - poc2).L1Norm();
if (distance < closest)
{
closest_poc1 = poc1;
closest_poc2 = poc2;
closest = distance;
}
}
}
return new Tuple<double, TGVector3D, TGVector3D>(closest, new TGVector3D(closest_poc1), new TGVector3D(closest_poc2));
}
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Numerics.LinearAlgebra;
namespace TransmissionGraphic.Type3D
{
@ -16,6 +17,13 @@ namespace TransmissionGraphic.Type3D
this.y = y;
this.z = z;
}
public TGVector3D(Vector<double> point)
{
this.x = point.At(0);
this.y = point.At(1);
this.z = point.At(2);
}
public static TGVector3D operator- (TGVector3D me, TGVector3D other)