@@ -8,7 +8,9 @@ namespace g3
8
8
public static class MeshQueries
9
9
{
10
10
11
- // convenience function to construct a DistPoint3Triangle3 object for a mesh triangle
11
+ /// <summary>
12
+ /// construct a DistPoint3Triangle3 object for a mesh triangle
13
+ /// </summary>
12
14
public static DistPoint3Triangle3 TriangleDistance ( DMesh3 mesh , int ti , Vector3d point )
13
15
{
14
16
if ( ! mesh . IsTriangle ( ti ) )
@@ -20,9 +22,24 @@ public static DistPoint3Triangle3 TriangleDistance(DMesh3 mesh, int ti, Vector3d
20
22
return q ;
21
23
}
22
24
25
+ /// <summary>
26
+ /// Find point-normal frame at closest point to queryPoint on mesh.
27
+ /// Returns interpolated vertex-normal frame if available, otherwise tri-normal frame.
28
+ /// </summary>
29
+ public static Frame3f NearestPointFrame ( DMesh3 mesh , ISpatial spatial , Vector3d queryPoint )
30
+ {
31
+ int tid = spatial . FindNearestTriangle ( queryPoint ) ;
32
+ Vector3d surfPt = TriangleDistance ( mesh , tid , queryPoint ) . TriangleClosest ;
33
+ if ( mesh . HasVertexNormals )
34
+ return SurfaceFrame ( mesh , tid , surfPt ) ;
35
+ else
36
+ return new Frame3f ( surfPt , mesh . GetTriNormal ( tid ) ) ;
37
+ }
23
38
24
39
25
- // convenience function to construct a IntrRay3Triangle3 object for a mesh triangle
40
+ /// <summary>
41
+ /// convenience function to construct a IntrRay3Triangle3 object for a mesh triangle
42
+ /// </summary>
26
43
public static IntrRay3Triangle3 TriangleIntersection ( DMesh3 mesh , int ti , Ray3d ray )
27
44
{
28
45
if ( ! mesh . IsTriangle ( ti ) )
@@ -35,8 +52,54 @@ public static IntrRay3Triangle3 TriangleIntersection(DMesh3 mesh, int ti, Ray3d
35
52
}
36
53
37
54
38
- // compute distance from point to triangle ti in mesh, with minimal extra objects/etc
39
- // TODO: take in current-max-distance so we can early-out?
55
+ /// <summary>
56
+ /// Find point-normal frame at ray-intersection point on mesh, or return false if no hit.
57
+ /// Returns interpolated vertex-normal frame if available, otherwise tri-normal frame.
58
+ /// </summary>
59
+ public static bool RayHitPointFrame ( DMesh3 mesh , ISpatial spatial , Ray3d ray , out Frame3f hitPosFrame )
60
+ {
61
+ hitPosFrame = new Frame3f ( ) ;
62
+ int tid = spatial . FindNearestHitTriangle ( ray ) ;
63
+ if ( tid == DMesh3 . InvalidID )
64
+ return false ;
65
+ var isect = TriangleIntersection ( mesh , tid , ray ) ;
66
+ if ( isect . Result != IntersectionResult . Intersects )
67
+ return false ;
68
+ Vector3d surfPt = ray . PointAt ( isect . RayParameter ) ;
69
+ if ( mesh . HasVertexNormals )
70
+ hitPosFrame = SurfaceFrame ( mesh , tid , surfPt ) ;
71
+ else
72
+ hitPosFrame = new Frame3f ( surfPt , mesh . GetTriNormal ( tid ) ) ;
73
+ return true ;
74
+ }
75
+
76
+
77
+ /// <summary>
78
+ /// Get point-normal frame on surface of mesh. Assumption is that point lies in tID.
79
+ /// returns interpolated vertex-normal frame if available, otherwise tri-normal frame.
80
+ /// </summary>
81
+ public static Frame3f SurfaceFrame ( DMesh3 mesh , int tID , Vector3d point )
82
+ {
83
+ if ( ! mesh . IsTriangle ( tID ) )
84
+ throw new Exception ( "MeshQueries.SurfaceFrame: triangle " + tID + " does not exist!" ) ;
85
+ Triangle3d tri = new Triangle3d ( ) ;
86
+ mesh . GetTriVertices ( tID , ref tri . V0 , ref tri . V1 , ref tri . V2 ) ;
87
+ Vector3d bary = tri . BarycentricCoords ( point ) ;
88
+ point = tri . PointAt ( bary ) ;
89
+ if ( mesh . HasVertexNormals ) {
90
+ Vector3d normal = mesh . GetTriBaryNormal ( tID , bary . x , bary . y , bary . z ) ;
91
+ return new Frame3f ( point , normal ) ;
92
+ } else
93
+ return new Frame3f ( point , mesh . GetTriNormal ( tID ) ) ;
94
+ }
95
+
96
+
97
+
98
+
99
+
100
+ /// <summary>
101
+ /// Compute distance from point to triangle in mesh, with minimal extra objects/etc
102
+ /// </summary>
40
103
public static double TriDistanceSqr ( DMesh3 mesh , int ti , Vector3d point )
41
104
{
42
105
Vector3d V0 = Vector3d . Zero , V1 = Vector3d . Zero , V2 = Vector3d . Zero ;
0 commit comments