欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

直线投影到三角网曲面上

civil 3d中要素线及放坡相关的.net api基本没有开放,

com api也只有要素线有有限的一些功能,

有时需要要素线到曲面的投影线,

这把我卡了很长时间。

 

今天想到了曲面有获取点到曲面投影的功能,

所以利用这个功能,

实现了求取直线到曲面投影的方法。

基本的思路是:

直线起点投影到曲面,

通过投影点获取到相应的三角形,

之后直线投影到三角形所在的平面,

再求投影线与三角形的交点,

通过交线找到三角形的边(有待优化),

通过三角形的边找到下一个三角形,

循环操作,

找出每一交点,

连成三维多段线。

主要的代码如下,

还不完善,

有需要的朋友可以借鉴一下,

拿去继续优化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/// <summary>
/// 2020年10月31日17:00:18
/// 直线投影到三角网曲面
/// </summary>
/// <param name="l">直线</param>
/// <param name="sur">曲面</param>
/// <param name="direction">投影方向</param>
/// <returns>投影三维多段线</returns>
public static Polyline3d ProjectToTinsurface(this Line l, TinSurface sur, Vector3d direction)
{
    Point3dCollection pts = new Point3dCollection();
    Point3d projectedPt01 = sur.GetIntersectionPoint(l.StartPoint, direction);
    Point3d projectedPt02 = sur.GetIntersectionPoint(l.EndPoint, direction);
 
    pts.Add(projectedPt01);
 
    var seedTriangle = sur.FindTriangleAtXY(projectedPt01.X, projectedPt01.Y);
    var projectedLine = l.GetProjectedCurve(seedTriangle.Plane(), direction) as Line;
    var intersectPts = projectedLine.GetIntersectPoints(seedTriangle);
    Point3d seedPt = new Point3d();
    // 如果三角形很大,直线很短,两个点投影后落到一个三角形内时,
    // 交点数为0,需要直接跳过
    // 交点数大于0时,说明与三角形的边有交点
    if (intersectPts.Count > 0)
    {
        foreach (Point3d pt in intersectPts)
        {
            if (!pts.Contains(pt))
            {
                seedPt = pt;
                pts.Add(pt);
            }
        }
        // 执行do while循环,依次选择相邻的三角形,依次查找交点
        do
        {
            // 交点位置的边
            // 这个应该有优化的空间,在求交点是记录边,
            // 之后传递到这里,而不是再用点查找一遍
            var edge = sur.FindEdgeAtXY(seedPt.X, seedPt.Y);
            // 选择下一个三角形
            if (edge.Triangle1.GetHashCode() != seedTriangle.GetHashCode())
            {
                seedTriangle = edge.Triangle1;
            }
            else
            {
                seedTriangle = edge.Triangle2;
            }
            // 投影直线到三角形上
            projectedLine = l.GetProjectedCurve(seedTriangle.Plane(), direction) as Line;
            // 获取投影线与三角形的交点
            intersectPts = projectedLine.GetIntersectPoints(seedTriangle);
            // 如果交点不在交点集合中,添加到集合
            foreach (Point3d pt in intersectPts)
            {
                if (!pts.Contains(pt))
                {
                    seedPt = pt;
                    pts.Add(pt);
                }
            }
        } while (intersectPts.Count > 1);
    }
    // 添加终点投影点到集合中
    pts.Add(projectedPt02);
    // 生成三维多段线
    return new Polyline3d(Poly3dType.SimplePoly, pts, false);
}
/// <summary>
/// 2020年10月31日17:07:38
/// 三角形平面
/// 获取曲面三角形所在的平面
/// </summary>
/// <param name="triangle">三角形</param>
/// <returns>平面</returns>
public static Plane Plane(this TinSurfaceTriangle triangle)
{
    Point3d point1 = triangle.Vertex1.Location;
    Point3d point2 = triangle.Vertex2.Location;
    Point3d point3 = triangle.Vertex3.Location;
 
    return new Plane(
       point1,
       point1.GetVectorTo(point2),
       point1.GetVectorTo(point3));
}
/// <summary>
/// 2020年10月31日17:08:49
/// 获取直线与三角形的交点集
/// </summary>
/// <param name="l"></param>
/// <param name="triangle"></param>
/// <returns></returns>
public static Point3dCollection GetIntersectPoints(this Line l, TinSurfaceTriangle triangle)
{
    Point3d point1 = triangle.Vertex1.Location;
    Point3d point2 = triangle.Vertex2.Location;
    Point3d point3 = triangle.Vertex3.Location;
 
    Point3dCollection result = new Point3dCollection();
 
    l.IntersectWith(new Line(point1, point2), Intersect.OnBothOperands, result, new IntPtr(0), new IntPtr(0));
    l.IntersectWith(new Line(point1, point3), Intersect.OnBothOperands, result, new IntPtr(0), new IntPtr(0));
    l.IntersectWith(new Line(point3, point2), Intersect.OnBothOperands, result, new IntPtr(0), new IntPtr(0));
 
 
    return result;
}

 

posted @   david96007  阅读(969)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示