public void intersection() { Triangle vertical = new Triangle(Vector3.Zero, Vector3.UnitY, Vector3.UnitZ, Vector2.Zero, Vector2.UnitY, Vector2.UnitX); Vector3 intersectionPoint; float t = vertical.intersection(new Vector3(-0.5f, 0.2f, 0.2f), Vector3.UnitX, out intersectionPoint); Assert.AreEqual(0.5f, t); Assert.AreEqual(new Vector3(0, 0.2f, 0.2f), intersectionPoint); Triangle horizontal = new Triangle(Vector3.Zero, Vector3.UnitX, Vector3.UnitZ, Vector2.Zero, Vector2.UnitX, Vector2.UnitY); Vector3 intersectionPoint2; float u = horizontal.intersection(new Vector3(0.2f, -0.5f, 0.2f), Vector3.UnitY, out intersectionPoint2); Assert.AreEqual(0.5f, u); Assert.AreEqual(new Vector3(0.2f, 0, 0.2f), intersectionPoint2); Vector3 intersectionPoint3; float v = vertical.intersection(new Vector3(0.2f, -0.5f, 0.2f), Vector3.UnitY, out intersectionPoint3); Assert.AreEqual(-1.0f, v); Assert.AreEqual(Vector3.Zero, intersectionPoint3); Vector3 intersectionPoint4; float w = vertical.intersection(new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(1.1f, -1.0f, -1.0f), out intersectionPoint4); Assert.Greater(w, 0); Assert.AreEqual(0, intersectionPoint4.X); Assert.AreEqual(new Vector3(-1.0f, 1.0f, 1.0f) + new Vector3(1.1f, -1.0f, -1.0f) * w, intersectionPoint4); }
public HexSeg(float radius, int sub) { Radius = radius; Triangles = new Triangle[6]; float halfRad = Radius / 2f; float opp = Mathf.Sqrt(.75f) * Radius; Vector3 p0 = new Vector3(-halfRad, 0, opp); Vector3 p1 = new Vector3(halfRad, 0, opp); Vector3 p2 = new Vector3(Radius, 0, 0); Vector3 p3 = new Vector3(halfRad, 0, -opp); Vector3 p4 = new Vector3(-halfRad, 0, -opp); Vector3 p5 = new Vector3(-Radius, 0, 0); Vector3 c = new Vector3(0, 0, 0); Triangles[0] = new Triangle(c, p0, p1); Triangles[1] = new Triangle(c, p1, p2); Triangles[2] = new Triangle(c, p2, p3); Triangles[3] = new Triangle(c, p3, p4); Triangles[4] = new Triangle(c, p4, p5); Triangles[5] = new Triangle(c, p5, p0); foreach (Triangle triangle in Triangles) { triangle.Subdivide(sub); } }
public ActiveTriangle(IScene scene, Triangle tri) { this.tri = tri; this.scene = scene; controller = new TriangleControllerNeutral(this); view = new ActiveTriangleView (this); }
public void onUV() { Triangle t = new Triangle(Vector3.Zero, Vector3.UnitX, Vector3.UnitY, Vector2.Zero, Vector2.UnitX, Vector2.UnitY, Vector2.Zero, Vector2.UnitX, Vector2.UnitY); Assert.True(t.isOnUVPixel(new Vector2(-2.0f, 2.0f), new Vector2(3.0f, -1.0f))); //Surrounds triangle Assert.True(t.isOnUVPixel(new Vector2(0, 0.5f), new Vector2(0.5f, 0))); //Completely inside Assert.True(t.isOnUVPixel(new Vector2(0.5f, 1.0f), new Vector2(1.0f, 0.5f))); //On the edge Assert.False(t.isOnUVPixel(new Vector2(-2.0f, 2.0f), new Vector2(-1.0f, 0))); //Not inside }
static void Main() { Shape[] someShapes = new Shape[6]; someShapes[0] = new Rectangle(5, 6); someShapes[1] = new Triangle(5, 6); someShapes[2] = new Circle(4); someShapes[3] = new Triangle(11, 7); someShapes[4] = new Rectangle(5, 6); someShapes[5] = new Circle(15); foreach (Shape shape in someShapes) { Console.WriteLine("Shape type: {0} \nSurface: {1}", shape.GetType(), shape.CalculateSurface()); Console.WriteLine(); } }
public static int GetSharpestPoint(Triangle tri) { int sharpest = 0; double least = PointUtils.SquaredDistance (tri.b,tri.c); double temp = PointUtils.SquaredDistance (tri.a,tri.c); if (temp < least) { least = temp; sharpest = 1; } temp = PointUtils.SquaredDistance (tri.a,tri.b); if (temp < least) { sharpest = 2; } return sharpest; }
public Triangle(Triangle t, Vector3 offset, Quaternion rotation) { Matrix4 transform = new Matrix4(new Vector4(Vector3.Transform(Vector3.UnitX, rotation), 0), new Vector4(Vector3.Transform(Vector3.UnitY, rotation), 0), new Vector4(Vector3.Transform(Vector3.UnitZ, rotation), 0), new Vector4(offset, 1)); _mVerts = new Vector3[3] { Vector3.Transform(t.x, transform), Vector3.Transform(t.y, transform), Vector3.Transform(t.z, transform) }; calculateNormal(); _tVerts = new Vector2[3] { t.u, t.v, t.w}; lightMapInverseMatrix = new Vector2[2]; if (t.isLightmapped) { isLightmapped = true; _lVerts = new Vector2[3] { t.a, t.b, t.c }; calculateInverseMatrix(); degenerate = areColinear(c - a, b - a); } }
public float intersection(Vector3 start, Vector3 direction, out Triangle intersectedTriangle) { float leastDistance = float.MaxValue; intersectedTriangle = null; //If the line doesn't go through this octree then it is unobstructed if (bounds.lineIntersects(start, direction)) { if (children.Length == 0) { foreach (Triangle t in tris) { Vector3 throwAway; float nextDistance = t.intersection(start, direction, out throwAway); if (nextDistance > 0 && nextDistance < leastDistance) { leastDistance = nextDistance; intersectedTriangle = t; } } } else { foreach (Octree o in children) { Triangle nextIntersection; float nextDistance = o.intersection(start, direction, out nextIntersection); if (nextDistance > 0 && nextDistance < leastDistance) { leastDistance = nextDistance; intersectedTriangle = nextIntersection; } } } } return leastDistance != float.MaxValue ? leastDistance : -1.0f; }
// Ear clipping algorithm to separate a polygon into triangles // This should work on any list of triangles with no holes. public IEnumerable<Triangle> ToTriangles() { Plane p = Plane; if (vertices.Count == 3) { } while (vertices.Count >= 3) { int verts = vertices.Count; int i = 0; // Find an ear on the face, remove it for (i = 0; i < verts; i++) { Vector3 v1 = vertices[i]; Vector3 v2 = vertices[(i + 1) % verts]; Vector3 v3 = vertices[(i + 2) % verts]; var tri = new Triangle(v1, v2, v3); bool anyPointInPolygon = false; foreach (var otherPoint in vertices) { if (otherPoint != v1 && otherPoint != v2 && otherPoint != v3 && tri.IsPointInTriangle(otherPoint)) { anyPointInPolygon = true; break; } } // First check: see if any point in the original polygon is inside the new triangle if (anyPointInPolygon) { // Can't use this triangle, move onto the next one } else { // Make sure the triangle points the right way. if (Vector3.Dot(tri.Plane.Normal, p.Normal) > 0.9f) { yield return tri; this.vertices.RemoveAt((i + 1) % verts); break; } else { } } } if (i == verts) { // Got a bad face break; } } }
//Already made triangles constructor(terrain meshes) public MeshChunk(Triangle[] triangles) { //unused things name = "THIS MESH CHUNK HAS NO NAME"; id = "THIS MESH CHUNK HAS NO ID"; vertexSize = 0; positionOffset = 0; textureOffset = 0; texture2Offset = 0; normalOffset = 0; startIndex = 0; vertexOffset = 0; materialObjectName = "THIS MESH CHUNK HAS NO MATERIAL OBJECT"; chunkOffset = new Vector3(); ; tris = triangles; receives = triangles[0].isLightmapped; usesTwoTexCoords = triangles[0].isLightmapped; casts = true; verts = new float[triangles.Length * 3 * vertexElementCount]; vertexCount = (uint)triangles.Length * 3; indices = new uint[triangles.Length * 3]; indexCount = (uint)indices.Length; for (int i = 0; i < tris.Length; i++) { int vertexIndex = i * vertexElementCount * 3; int indexIndex = i * 3; indices[indexIndex + 0] = (uint)indexIndex + 0; indices[indexIndex + 1] = (uint)indexIndex + 1; indices[indexIndex + 2] = (uint)indexIndex + 2; verts[vertexIndex + 0] = tris[i].x.X; verts[vertexIndex + 1] = tris[i].x.Y; verts[vertexIndex + 2] = tris[i].x.Z; verts[vertexIndex + 3] = tris[i].normal.X; verts[vertexIndex + 4] = tris[i].normal.Y; verts[vertexIndex + 5] = tris[i].normal.Z; verts[vertexIndex + 6] = tris[i].u.X; verts[vertexIndex + 7] = tris[i].u.Y; vertexIndex += vertexElementCount; verts[vertexIndex + 0] = tris[i].y.X; verts[vertexIndex + 1] = tris[i].y.Y; verts[vertexIndex + 2] = tris[i].y.Z; verts[vertexIndex + 3] = tris[i].normal.X; verts[vertexIndex + 4] = tris[i].normal.Y; verts[vertexIndex + 5] = tris[i].normal.Z; verts[vertexIndex + 6] = tris[i].v.X; verts[vertexIndex + 7] = tris[i].v.Y; vertexIndex += vertexElementCount; verts[vertexIndex + 0] = tris[i].z.X; verts[vertexIndex + 1] = tris[i].z.Y; verts[vertexIndex + 2] = tris[i].z.Z; verts[vertexIndex + 3] = tris[i].normal.X; verts[vertexIndex + 4] = tris[i].normal.Y; verts[vertexIndex + 5] = tris[i].normal.Z; verts[vertexIndex + 6] = tris[i].w.X; verts[vertexIndex + 7] = tris[i].w.Y; area += 0.5 * Vector3.Cross(tris[i].y - tris[i].x, tris[i].z - tris[i].x).Length; } }
//Reading from a file constructor (normal meshes) public MeshChunk(BinaryReader file, long dataOffset, BiowareStruct chunkDef, BiowareStruct vertStruct) { uint listLength; long reference; long position = file.BaseStream.Position; materialObjectName = ""; //Read the name file.BaseStream.Seek(chunkDef.fields[0].index + position, SeekOrigin.Begin); name = IOUtilities.readECString(file, dataOffset + file.ReadInt32()); //Seek to vertex size offset and read it file.BaseStream.Seek(chunkDef.fields[1].index + position, SeekOrigin.Begin); vertexSize = (int)file.ReadUInt32(); //Seek to vertex count offset and read it file.BaseStream.Seek(chunkDef.fields[2].index + position, SeekOrigin.Begin); vertexCount = file.ReadUInt32(); verts = new float[vertexCount * vertexElementCount]; //Seek to vertex offset offset and read it file.BaseStream.Seek(chunkDef.fields[7].index + position, SeekOrigin.Begin); vertexOffset = file.ReadUInt32(); //Seek to index count offset and read it file.BaseStream.Seek(chunkDef.fields[3].index + position, SeekOrigin.Begin); indexCount = file.ReadUInt32(); indices = new uint[indexCount]; //Seek to index offset offset and read it file.BaseStream.Seek(chunkDef.fields[10].index + position, SeekOrigin.Begin); startIndex = file.ReadUInt32(); //Seek to vertex declarator offset and read in the list reference file.BaseStream.Seek(chunkDef.fields[13].index + position, SeekOrigin.Begin); reference = file.ReadUInt32(); //Make the triangle array to be filled later tris = new Triangle[indexCount / 3]; //Seek to the list file.BaseStream.Seek(reference + dataOffset, SeekOrigin.Begin); listLength = file.ReadUInt32(); reference = file.BaseStream.Position; usesTwoTexCoords = false; Usage type; //Get the offsets for (int i = 0; i < listLength; i++) { file.BaseStream.Seek(reference + (vertStruct.structSize * i) + vertStruct.fields[3].index, SeekOrigin.Begin); type = (Usage)file.ReadUInt32(); file.BaseStream.Seek(reference + (vertStruct.structSize * i) + vertStruct.fields[1].index, SeekOrigin.Begin); switch (type) { case Usage.POSITION: positionOffset = file.ReadInt32(); break; case Usage.TEXCOORD: int offset = file.ReadInt32(); file.BaseStream.Seek(reference + (vertStruct.structSize * i) + vertStruct.fields[4].index, SeekOrigin.Begin); uint index = file.ReadUInt32(); if (index == 0) { textureOffset = offset; texture2Offset = offset; } else if (index == 1) { texture2Offset = offset; usesTwoTexCoords = true; } break; case Usage.NORMAL: normalOffset = file.ReadInt32(); break; } } }
public override void readData() { int reference, length; long startOfList; //Get the binary reader BinaryReader file = binaryFile.openReader(); //Set up the struct definitions setStructDefinitions(); //Get the sector ID file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[SECTOR_ID_INDEX].index, SeekOrigin.Begin); _sectorID = file.ReadInt32(); name = "Sector " + sectorID; /*----------MAP VERTS----------*/ //Get the reference to the map vertex list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MAP_VERTEX_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the map vertex list with all the map verts length = file.ReadInt32(); _mapVerts = new List<TerrainMapVertex>(length); startOfList = file.BaseStream.Position; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * mapVertexStruct.structSize), SeekOrigin.Begin); //Add it to the list _mapVerts.Add(new TerrainMapVertex(mapVertexStruct, file)); } /*----------MAP EDGES----------*/ //Get the reference to the map edge list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MAP_EDGE_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the map edge list with all the map edges length = file.ReadInt32(); _mapEdges = new List<TerrainMapEdge>(length); startOfList = file.BaseStream.Position; TerrainMapEdge tempEdge; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * mapEdgeStruct.structSize), SeekOrigin.Begin); //Create the edge tempEdge = new TerrainMapEdge(mapEdgeStruct, file); //Get the actual index in the array of the map vert for (int j = 0; j < _mapVerts.Count; j++) { if (_mapVerts[j].id == tempEdge.startVertexIndex) { tempEdge.startVertexIndex = j; break; } } //Add it to the list _mapEdges.Add(tempEdge); } /*----------MAP FACES----------*/ //Get the reference to the map face list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MAP_FACE_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the map face list with all the map faces length = file.ReadInt32(); _mapFaces = new List<TerrainMapFace>(length); startOfList = file.BaseStream.Position; TerrainMapFace tempFace; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * mapFaceStruct.structSize), SeekOrigin.Begin); //Create the face tempFace = new TerrainMapFace(mapFaceStruct, file); //Find the actual edge indices for (int edgeIndex = 0; edgeIndex < 3; edgeIndex++) { for (int j = 0; j < _mapEdges.Count; j++) { if (tempFace[edgeIndex] == _mapEdges[j].id) { tempFace[edgeIndex] = j; break; } } } //Add it to the list _mapFaces.Add(tempFace); } /*----------MESH VERTS----------*/ //Get the reference to the mesh vertex list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MESH_VERTEX_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the vertex list with all the vertex length = file.ReadInt32(); _verts = new List<TerrainMeshVertex>(length); startOfList = file.BaseStream.Position; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * meshVertexStruct.structSize), SeekOrigin.Begin); //Add it to the list _verts.Add(new TerrainMeshVertex(meshVertexStruct, file)); } /*----------MESH EDGES----------*/ //Get the reference to the mesh edge list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MESH_EDGE_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the edge list with all the edge length = file.ReadInt32(); _edges = new List<TerrainMeshEdge>(length); startOfList = file.BaseStream.Position; TerrainMeshEdge tempMeshEdge; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * meshEdgeStruct.structSize), SeekOrigin.Begin); //Create the edge tempMeshEdge = new TerrainMeshEdge(meshEdgeStruct, file); //Get the actual index in the array of the mesh vert for (int j = 0; j < _mapVerts.Count; j++) { if (_verts[j].id == tempMeshEdge.startVertexIndex) { tempMeshEdge.startVertexIndex = j; break; } } //Add it to the list _edges.Add(tempMeshEdge); } /*----------MESH FACES----------*/ //Get the reference to the mesh face list and go there file.BaseStream.Seek(binaryFile.dataOffset + infoStruct.fields[MESH_FACE_LIST_INDEX].index, SeekOrigin.Begin); reference = file.ReadInt32(); file.BaseStream.Seek(binaryFile.dataOffset + reference, SeekOrigin.Begin); //Fill the face list with all the faces length = file.ReadInt32(); _faces = new List<TerrainMeshFace>(length); startOfList = file.BaseStream.Position; TerrainMeshFace tempMeshFace; for (int i = 0; i < length; i++) { //Seek to the next struct in the list file.BaseStream.Seek(startOfList + (i * meshFaceStruct.structSize), SeekOrigin.Begin); //Make the face tempMeshFace = new TerrainMeshFace(meshFaceStruct, file); //Find the actual edge indices for (int edgeIndex = 0; edgeIndex < 3; edgeIndex++) { for (int j = 0; j < _edges.Count; j++) { if (tempMeshFace[edgeIndex] == _edges[j].id) { tempMeshFace[edgeIndex] = j; break; } } } //Find the actual map face index for (int j = 0; j < _mapFaces.Count; j++) { if (tempMeshFace.mapId == _mapFaces[j].id) { tempMeshFace.mapId = j; break; } } //Add it to the list _faces.Add(tempMeshFace); } file.Close(); chunks = new MeshChunk[1]; Triangle[] tris = new Triangle[faces.Count]; //Hold temporary vertex values Vector3 x, y, z; Vector2 u, v, w; //For each faces make a triangle for (int i = 0; i < faces.Count; i++) { //Get the mesh coordinates x = verts[edges[faces[i].edges[0]].startVertexIndex].position; y = verts[edges[faces[i].edges[1]].startVertexIndex].position; z = verts[edges[faces[i].edges[2]].startVertexIndex].position; //Get the map coordinates (lightmap coordinates are the same) u = mapVerts[mapEdges[mapFaces[faces[i].mapId].edges[0]].startVertexIndex].position.Xy; v = mapVerts[mapEdges[mapFaces[faces[i].mapId].edges[1]].startVertexIndex].position.Xy; w = mapVerts[mapEdges[mapFaces[faces[i].mapId].edges[2]].startVertexIndex].position.Xy; //Make the triangle tris[i] = new Triangle(x, y, z, u, v, w, u, v, w); } chunks[0] = new MeshChunk(tris); chunks[0].casts = true; chunks[0].receives = true; }
public void AddTriangle(Triangle tri) { AddTriangle(tri.A, tri.B, tri.C); }
static double findRightAreaMethod(string NameOfGeo, Square square, Rectangle rectangle, ParalelloGram paralelloGram, Trapez trapez, Triangle triangle) { if (NameOfGeo.Equals("Square")) { return(square.AreaForSquare()); } else if (NameOfGeo.Equals("Rectangle")) { return(rectangle.AreaForRectangle()); } else if (NameOfGeo.Equals("paralelloGram")) { return(paralelloGram.AreaForParalleloGram()); } else if (NameOfGeo.Equals("Trapez")) { return(trapez.AreaForTrapez()); } else if (NameOfGeo.Equals("Triangle")) { return(triangle.AreaForTriangle()); } else { return(0); } }
private bool allOnSameSideOfTriangle(Triangle t) { bool topAPositive = Vector3.Dot(t.normal, max - t.x) > 0; bool topBPositive = Vector3.Dot(t.normal, topB - t.x) > 0; bool topCPositive = Vector3.Dot(t.normal, topC - t.x) > 0; bool topDPositive = Vector3.Dot(t.normal, topD - t.x) > 0; bool bottomAPositive = Vector3.Dot(t.normal, bottomA - t.x) > 0; bool bottomBPositive = Vector3.Dot(t.normal, bottomB - t.x) > 0; bool bottomCPositive = Vector3.Dot(t.normal, min - t.x) > 0; bool bottomDPositive = Vector3.Dot(t.normal, bottomD - t.x) > 0; bool allPositive = topAPositive && topBPositive && topCPositive && topDPositive && bottomAPositive && bottomBPositive && bottomCPositive && bottomDPositive; bool allNegative = !(topAPositive || topBPositive || topCPositive || topDPositive || bottomAPositive || bottomBPositive || bottomCPositive || bottomDPositive); return allPositive || allNegative; }
public TrianglePlaneIntersect(Triangle triangle, Plane plane) { // Find the intersections between edges on the triangle and the plane. List<Vector3> vertices = new List<Vector3>(3); List<float> distances = new List<float>(3); List<Vector3> onPlane = new List<Vector3>(); foreach (Vector3 v in triangle.Vertices) { vertices.Add(v); distances.Add(plane.Distance(v)); } for (int i = 0; i < 3; i++) { float d1 = distances[i]; float d2 = distances[(i + 1) % 3]; float d3 = distances[(i + 2) % 3]; Vector3 v1 = vertices[i]; Vector3 v2 = vertices[(i + 1) % 3]; Vector3 v3 = vertices[(i + 2) % 3]; if (d1 * d2 < 0) { // One point on the edge from D1 to D2 Vector3 intersect = new Vector3(v2 * d1 - v1 * d2) / (d1 - d2); onPlane.Add(intersect); if (d3 == 0) { // Other point is on D3 onPlane.Add(v3); break; } else { if (d1 * d3 < 0) { // Intersect with v1 to v3 onPlane.Add(new Vector3(v3 * d1 - v1 * d3) / (d1 - d3)); break; } else if (d2 * d3 < 0) { // Intersect with v2 to v3 onPlane.Add(new Vector3(v3 * d2 - v2 * d3) / (d2 - d3)); break; } else { // how the heck would we get here? } } } if (d1 == 0 && d2 == 0) { if (d3 == 0) { // Triangle intersects perfectly with the plane - need to return all 3 edges here? all_intersect = true; break; } else { onPlane.Add(v1); onPlane.Add(v2); break; } } //if (d1 * d2 < 0) //{ // // Edge of the triangle crosses the plane, interpolate to get the intersection point // Vector3 intersect = new Vector3(v2 * d1 - v1 * d2) / (d1 - d2); // onPlane.Add(intersect); //} } if (onPlane.Count == 2) { pointA = onPlane[0]; pointB = onPlane[1]; intersects = true; } if (intersects) { // Check the vector direction - flip the vertices if necessary if (Vector3.Dot(Vector3.Cross(pointB - pointA, plane.Normal), triangle.Plane.Normal) < 0) { pointA = onPlane[1]; pointB = onPlane[0]; } } }
public void Subdivide(int i=1) { SubPoints = new Vector3[3]; SubPoints[0] = Points[0] + ((Points[1] - Points[0])/2f); SubPoints[1] = Points[1] + ((Points[2] - Points[1])/2f); SubPoints[2] = Points[2] + ((Points[0] - Points[2])/2f); Subs = new Triangle[4]; Subs[0] = new Triangle(Points[0], SubPoints[0], SubPoints[2]); Subs[1] = new Triangle(Points[1], SubPoints[1], SubPoints[0]); Subs[2] = new Triangle(Points[2], SubPoints[2], SubPoints[1]); Subs[3] = new Triangle(SubPoints[0], SubPoints[1], SubPoints[2]); if(i > 1) { foreach(Triangle sub in Subs) { sub.Subdivide(i - 1); } } }
public static void Main(string[] args) { Dot p1 = new Dot(2, 2.5); Dot p2 = new Dot(); Dot p3 = new Dot(p1); Console.WriteLine("{0:N}", p1); Console.WriteLine("{0:N}", p2); Console.WriteLine("{0:N}", p3); Console.WriteLine(); Circle c1 = new Circle(3, 2, 2.5); Circle c2 = new Circle(); Circle c3 = new Circle(6.5, new Dot(10, 12)); Console.WriteLine("{0}", c1); Console.WriteLine("{0}", c2); Console.WriteLine("{0}", c3); Console.WriteLine(); Square s1 = new Square(2.25, 1.0, 0.0); Square s2 = new Square(); Square s3 = new Square(6.0, p1); Console.WriteLine("{0}", s1); Console.WriteLine("{0}", s2); Console.WriteLine("{0}", s3); Console.WriteLine(); Rectangle r1 = new Rectangle(5.0, 9.5, new Dot(8.1, 5.02)); Rectangle r2 = new Rectangle(1.5, 3.9, 2, 2.5); Rectangle r3 = new Rectangle(); Console.WriteLine("{0}\n{1}\n{2}", r1, r2, r3); Console.WriteLine(); Triangle tr1 = new Triangle(8.0, 6.5, 10.0, 15.0, 2.0, 2.5); Triangle tr2 = new Triangle(); Triangle tr3 = new Triangle(12.5, 15.2, p1, p3); Console.WriteLine("{0}\n{1}\n{2}", tr1, tr2, tr3); Console.WriteLine(); Trapezium trap1 = new Trapezium(); Trapezium trap2 = new Trapezium(10.1, 18.25, 20.4, 10.0, 10.0, 4.0, 4.0); Trapezium trap3 = new Trapezium(9.32, 5.2, 6.0, new Dot(15, 12), p1); Console.WriteLine("{0}\n{1}\n{2}", trap1, trap2, trap3); Console.WriteLine(); Console.WriteLine("There are {0} shapes here.", Shape.GetCount()); Console.WriteLine(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public void onKeyPress(object o, KeyPressEventArgs args) { Logger.Log ("key press " + args.Event.Key); if (args.Event.Key == Gdk.Key.p && state != MyState.AddingPoints) { state = MyState.AddingPoints; return; } if (args.Event.Key == Gdk.Key.p && state == MyState.AddingPoints) { foreach (PointD p in demo.points) { VertexStructure.Add (ref vertices,p); } demo.points.Clear (); demo.vertexStructs.Add (vertices); state = MyState.NotAddingPoints; vertices = null; return; } if (args.Event.Key == Gdk.Key.t) { foreach (VertexStructure polygon in demo.vertexStructs) { List<PointD> pointList = Geometry.ComputationalGeometry.GetTriangulateTrianglePoints (polygon,VertexStructure.Count(polygon)); List<PointD>.Enumerator iter = pointList.GetEnumerator(); iter.MoveNext (); do { PointD a = iter.Current; iter.MoveNext (); PointD b = iter.Current; iter.MoveNext (); PointD c = iter.Current; Triangle t = new Triangle (a, b, c); ActiveTriangle nt = new ActiveTriangle (demo,t); demo.addObject(nt); } while(iter.MoveNext ()); Logger.Log ("Iterated"); } demo.vertexStructs.Clear (); return; } if (args.Event.Key == Gdk.Key.e || args.Event.Key == Gdk.Key.n) { List<IGameObject> goList = demo.getObjectsByTag (ActiveTriangle.tag); foreach (IGameObject go in goList){ ActiveTriangle t = (ActiveTriangle) go; switch (args.Event.Key) { case Gdk.Key.e: t.setEvil (); break; case Gdk.Key.n: t.setNeutral (); break; default: t.setNeutral (); break; } } } if (args.Event.Key == Gdk.Key.o) { demo.addObject (new Protagonist (demo)); } }
//public List<Triangles> GetTriangles(Predicate<Triangle> trianglePredicate) //{ // //} // // public List<TriangleMesh> Analyze() { List<TriangleMesh> meshes = new List<TriangleMesh>(); // While there is a triangle on the top: // 1. Find a triangle on the top, remove it // 2. Find all connected triangles, remove them // 3. Add all removed triangles to a new mesh // Find var t = triangles; List<TriangleIndices> ignore = new List<TriangleIndices>(); Func<Triangle, bool> TopTriangleCriteria = triangle => Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ) == 1 && triangle.A.Z == MaxPoint.Z; Func<Triangle, bool> BottomTriangleCriteria = triangle => Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ) == -1 && triangle.A.Z == MinPoint.Z; Func<Triangle, bool> PocketCriteria1 = triangle => !TopTriangleCriteria(triangle) && Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ) > 0; Func<Triangle, bool> PocketCriteria2 = triangle => !TopTriangleCriteria(triangle) && Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ) >= 0; while (t.Count > 0) { var triangleIndices = t[0]; var triangle = new Triangle(vertices[triangleIndices.a], vertices[triangleIndices.b], vertices[triangleIndices.c]); var dot = Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ); Func<float, bool> dotTestTop = dot_value => dot_value == 1; Func<float, bool> dotTestBottom = dot_value => dot_value == -1; if (TopTriangleCriteria(triangle)) { var connected = GetConnectedTriangles(triangleIndices, ignore, TopTriangleCriteria); ignore.AddRange(connected); TriangleMesh mesh = new TriangleMesh(); foreach (var indices in connected) { t.Remove(indices); var newTriangle = new Triangle(vertices[indices.a], vertices[indices.b], vertices[indices.c]); mesh.AddTriangle(newTriangle); } //meshes.Add(mesh); } else if (BottomTriangleCriteria(triangle)) { var connected = GetConnectedTriangles(triangleIndices, ignore, BottomTriangleCriteria); ignore.AddRange(connected); TriangleMesh mesh = new TriangleMesh(); foreach (var indices in connected) { t.Remove(indices); var newTriangle = new Triangle(vertices[indices.a], vertices[indices.b], vertices[indices.c]); mesh.AddTriangle(newTriangle); } //meshes.Add(mesh); } else if (PocketCriteria1(triangle)) { var connected = GetConnectedTriangles(triangleIndices, ignore, PocketCriteria2); ignore.AddRange(connected); TriangleMesh mesh = new TriangleMesh(); foreach (var indices in connected) { t.Remove(indices); var newTriangle = new Triangle(vertices[indices.a], vertices[indices.b], vertices[indices.c]); mesh.AddTriangle(newTriangle); } meshes.Add(mesh); } else { //ignore.Add(t[0]); t.RemoveAt(0); } } return meshes; }
public Boolean triangleIntersects(Triangle t) { //Separating axis theorem return !((t.x.X > max.X && t.y.X > max.X && t.z.X > max.X) || //Max X plane (t.x.Y > max.Y && t.y.Y > max.Y && t.z.Y > max.Y) || //Max Y plane (t.x.Z > max.Z && t.y.Z > max.Z && t.z.Z > max.Z) || //Max Z plane (t.x.X < min.X && t.y.X < min.X && t.z.X < min.X) || //Min X plane (t.x.Y < min.Y && t.y.Y < min.Y && t.z.Y < min.Y) || //Min Y plane (t.x.Z < min.Z && t.y.Z < min.Z && t.z.Z < min.Z) || //Min Z plane allOnSameSideOfTriangle(t)); //Triangle plane }
public TriangleRayIntersect(Triangle triangle, Ray ray) { point = Vector3.Zero; intersects = false; float distance = triangle.Plane.Distance(ray); if (distance <= 0 || float.IsNaN(distance)) { // Plane is behind the ray, no intersection return; } point = ray.Direction * distance + ray.Start; // Make sure the point is within the triangle foreach (Plane p in triangle.EdgePlanes) { if (p.Distance(point) < 0) { // Outside of triangle, no intersection return; } } intersects = true; }
public Boolean containsTriangle(Triangle t) { return containsPoint(t.x) && containsPoint(t.y) && containsPoint(t.z); }
/// <summary> /// Get all connected triangles that satisfy a certain criteria /// </summary> /// <returns></returns> public TriangleIndices[] GetConnectedTriangles(TriangleIndices triangleIndices, List<TriangleIndices> ignoreIndices, Func<Triangle, bool> dotCriteria) { List<TriangleIndices> connected = new List<TriangleIndices>(); connected.Add(triangleIndices); //ignoreIndices.Add(triangleIndices); foreach (var edge in triangleIndices.edges) { foreach (var otherindex in edge.triangles) { if (ignoreIndices.Contains(otherindex) || connected.Contains(otherindex)) { // Don't need to look at this one } else { var triangle = new Triangle(vertices[otherindex.a], vertices[otherindex.b], vertices[otherindex.c]); var dot = Vector3.Dot(triangle.Plane.Normal, Vector3.UnitZ); if (dotCriteria(triangle)) { // Satisfies criteria, keep the triangle List<TriangleIndices> newIgnore = new List<TriangleIndices>(); newIgnore.AddRange(ignoreIndices); newIgnore.AddRange(connected); var moreconnected = GetConnectedTriangles(otherindex, newIgnore, dotCriteria); //ignoreIndices.AddRange(moreconnected); connected.AddRange(moreconnected); } } } } return connected.ToArray(); }