- Home /
Triangle Intersection using Geometry3Sharp library.
I'm using Geometry3Sharp and want some help regarding triangle intersection. I'm using Unity to test the triangle intersection of two Unity sphere meshes. I converted the Unity mesh data to built DMesh3 using Build method of DMesh3Builder and provided vertices and triangles from Unity's Mesh data structure. I intend to generate spheres on the intersection points. Here is a chink of my code to give you and idea.
DMesh3 meshA = DMesh3Builder.Build(verticesA, trianglesA, normalsA);
DMesh3 meshB = DMesh3Builder.Build(verticesB, trianglesB, normalsB);
DMeshAABBTree3 spatialA = new DMeshAABBTree3(meshA, true);
DMeshAABBTree3 spatialB = new DMeshAABBTree3(meshB, true);
DMeshAABBTree3.IntersectionsQueryResult iqr = spatialA.FindAllIntersections(spatialB);
List<DMeshAABBTree3.PointIntersection> pointIntersection = iqr.Points;
for (int i = 0; i < pointIntersection.Count; i++)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
sphere.transform.position = (Vector3) pointIntersection[i].point;
}
In the screenshot there is no apparent intersection but spheres are still being generated as if there is intersection. Need some help.
Answer by hassanyawar · Nov 28, 2018 at 09:39 AM
I found the solution. I contacted the author of this library and he was kind enough to guide me. This is the link my thread in case someone wants to get complete idea.
To answer my own question. First I needed to convert Unity's Mesh vertices and normals to world space and then assign to DMesh3. Then after using FindAllIntersections, instead of using Points intersections query result I used Segments intersection query result. I got all the intersecting triangle pairs. The Point intersections are where two triangles touch exactly at an edge or vertex. This hardly ever happens so segment intersections is the answer.
Here is my code to give an idea:
using System.Collections.Generic;
using UnityEngine;
using g3;
public class TriTriIntersection : MonoBehaviour
{
public MeshFilter mf1;
public MeshFilter mf2;
private Transform t1, t2;
private Mesh mesh1, mesh2;
private Vector3[] oldVerticesA, oldVerticesB;
private Vector3[] oldNormalsA, oldNormalsB;
private int[] trianglesA, trianglesB;
private Vector3f[] newVerticesA, newVerticesB;
private Vector3f[] newNormalsA, newNormalsB;
void Start ()
{
// Caching meshes
mesh1 = mf1.sharedMesh;
mesh2 = mf2.sharedMesh;
int totalVertex1 = mesh1.vertexCount;
int totalVertex2 = mesh2.vertexCount;
// Caching transforms
t1 = mf1.transform;
t2 = mf2.transform;
// Getting vertices, triangles and normals for mesh 1
oldVerticesA = mesh1.vertices;
oldNormalsA = mesh1.normals;
trianglesA = mesh1.triangles;
// Getting vertices, triangles and normals for mesh 2
oldVerticesB = mesh2.vertices;
oldNormalsB = mesh2.normals;
trianglesB = mesh2.triangles;
// Creating Vector3f lists for converted data
newVerticesA = new Vector3f[totalVertex1];
newNormalsA = new Vector3f[totalVertex1];
newVerticesB = new Vector3f[totalVertex2];
newNormalsB = new Vector3f[totalVertex2];
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
FindIntersectionTriangles();
}
}
private void FindIntersectionTriangles ()
{
// Converting Vector3 to Vector3f and local positions/directions to world positions/directions for mesh 1
for (int i = 0; i < oldVerticesA.Length; i++)
{
newVerticesA[i] = t1.TransformPoint(oldVerticesA[i]);
newNormalsA[i] = t1.TransformDirection(oldNormalsA[i]);
}
// Converting Vector3 to Vector3f and local positions/directions to world positions/directions for mesh 2
for (int i = 0; i < oldVerticesB.Length; i++)
{
newVerticesB[i] = t2.TransformPoint(oldVerticesB[i]);
newNormalsB[i] = t2.TransformDirection(oldNormalsB[i]);
}
// Building g3 meshes
DMesh3 meshA = DMesh3Builder.Build(newVerticesA, trianglesA, newNormalsA);
DMesh3 meshB = DMesh3Builder.Build(newVerticesB, trianglesB, newNormalsB);
// Building AABB trees
DMeshAABBTree3 treeA = new DMeshAABBTree3(meshA, true);
DMeshAABBTree3 treeB = new DMeshAABBTree3(meshB, true);
// Getting all intersection points
DMeshAABBTree3.IntersectionsQueryResult iqr = treeA.FindAllIntersections(treeB);
List<DMeshAABBTree3.SegmentIntersection> segmentIntersection = iqr.Segments;
for (int i = 0; i < segmentIntersection.Count; i++)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
sphere.transform.position = (Vector3) segmentIntersection[i].point0;
}
}
}