Example #1
0
        private void buttonBuildCell_Click(object sender, EventArgs e)
        {
            if (m_Prim_CellFaces != null)
            {
                m_Prim_CellFaces.Dispose();
                m_Prim_CellEdges.Dispose();
                m_Prim_CellFaces = null;
                m_Prim_CellEdges = null;
            }

            float AreaThresholdLow  = m_AreaMin + 0.1f * (m_AreaAvg - m_AreaMin);
            float AreaThresholdHigh = m_AreaMax - 0.1f * (m_AreaMax - m_AreaAvg);
//          float		AreaThresholdLow = m_AreaAvg / 1.5f;
//          float		AreaThresholdHigh = 1.5f * m_AreaAvg;

            List <VertexP3N3> vertices      = new List <VertexP3N3>();
            List <uint>       indices_Faces = new List <uint>();
            List <uint>       Indices_Edges = new List <uint>();

            float AreaMin   = float.MaxValue;
            float AreaMax   = -float.MaxValue;
            float TotalArea = 0.0f;

            for (int FaceIndex = 0; FaceIndex < m_neighborPositions.Length; FaceIndex++)
            {
                float3 P = m_neighborPositions[FaceIndex];
                float3 N = -P.Normalized;                       // Pointing inward

                // Build the polygon by cutting it with all other neighbors
                CellPolygon Polygon = new CellPolygon(P, N);
                for (int NeighborIndex = 0; NeighborIndex < m_neighborPositions.Length; NeighborIndex++)
                {
                    if (NeighborIndex != FaceIndex)
                    {
                        float3 Pn = m_neighborPositions[NeighborIndex];
                        float3 Nn = -Pn.Normalized;                             // Pointing inward
                        Polygon.Cut(Pn, Nn);
                    }
                }

                // Append vertices & indices for both faces & edges
                uint VertexOffset  = (uint)vertices.Count;
                uint VerticesCount = (uint)Polygon.m_Vertices.Length;
                if (VerticesCount > 0)
                {
                    float PolygonArea = 0.0f;
                    for (uint FaceTriangleIndex = 0; FaceTriangleIndex < VerticesCount - 2; FaceTriangleIndex++)
                    {
                        indices_Faces.Add(VertexOffset + 0);
                        indices_Faces.Add(VertexOffset + 1 + FaceTriangleIndex);
                        indices_Faces.Add(VertexOffset + 2 + FaceTriangleIndex);

                        float Area = 0.5f * (Polygon.m_Vertices[2 + FaceTriangleIndex] - Polygon.m_Vertices[0]).Cross(Polygon.m_Vertices[1 + FaceTriangleIndex] - Polygon.m_Vertices[0]).Length;
                        PolygonArea += Area;
                    }
                    AreaMin    = Math.Min(AreaMin, PolygonArea);
                    AreaMax    = Math.Max(AreaMax, PolygonArea);
                    TotalArea += PolygonArea;

                    for (uint VertexIndex = 0; VertexIndex < VerticesCount; VertexIndex++)
                    {
                        Indices_Edges.Add(VertexOffset + VertexIndex);
                        Indices_Edges.Add(VertexOffset + (VertexIndex + 1) % VerticesCount);
                    }

                    float3 Color = PolygonArea <AreaThresholdLow ? new float3(1, 0, 0) : PolygonArea> AreaThresholdHigh ? new float3(0, 1, 0) : new float3(1, 1, 0);

                    foreach (float3 Vertex in Polygon.m_Vertices)
                    {
                        vertices.Add(new VertexP3N3()
                        {
                            P = Vertex, N = Color
                        });
                    }
                }
            }

            m_AreaMin       = AreaMin;
            m_AreaMax       = AreaMax;
            m_AreaAvg       = TotalArea / m_neighborPositions.Length;
            labelStats.Text = "Area min: " + m_AreaMin + "\r\n"
                              + "Area max: " + m_AreaMax + "\r\n"
                              + "Area average: " + m_AreaAvg + "\r\n"
                              + "Area total: " + TotalArea + "\r\n";

            m_Prim_CellFaces = new Primitive(m_Device, (uint)vertices.Count, VertexP3N3.FromArray(vertices.ToArray()), indices_Faces.ToArray(), Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3);
            m_Prim_CellEdges = new Primitive(m_Device, (uint)vertices.Count, VertexP3N3.FromArray(vertices.ToArray()), Indices_Edges.ToArray(), Primitive.TOPOLOGY.LINE_LIST, VERTEX_FORMAT.P3N3);
        }
Example #2
0
        private void buttonBuildCell_Click( object sender, EventArgs e )
        {
            if ( m_Prim_CellFaces != null ) {
                m_Prim_CellFaces.Dispose();
                m_Prim_CellEdges.Dispose();
                m_Prim_CellFaces = null;
                m_Prim_CellEdges = null;
            }

            float		AreaThresholdLow = m_AreaMin + 0.1f * (m_AreaAvg - m_AreaMin);
            float		AreaThresholdHigh = m_AreaMax - 0.1f * (m_AreaMax - m_AreaAvg);
            // 			float		AreaThresholdLow = m_AreaAvg / 1.5f;
            // 			float		AreaThresholdHigh = 1.5f * m_AreaAvg;

            List<VertexP3N3>	Vertices = new List<VertexP3N3>();
            List<uint>			Indices_Faces = new List<uint>();
            List<uint>			Indices_Edges = new List<uint>();

            float	AreaMin = float.MaxValue;
            float	AreaMax = -float.MaxValue;
            float	TotalArea = 0.0f;

            for ( int FaceIndex=0; FaceIndex < m_NeighborPositions.Length; FaceIndex++ ) {
                float3	P = m_NeighborPositions[FaceIndex];
                float3	N = -P.Normalized;	// Pointing inward

                // Build the polygon by cutting it with all other neighbors
                CellPolygon	Polygon = new CellPolygon( P, N );
                for ( int NeighborIndex=0; NeighborIndex < m_NeighborPositions.Length; NeighborIndex++ )
                    if ( NeighborIndex != FaceIndex ) {
                        float3	Pn = m_NeighborPositions[NeighborIndex];
                        float3	Nn = -Pn.Normalized;	// Pointing inward
                        Polygon.Cut( Pn, Nn );
                    }

                // Append vertices & indices for both faces & edges
                uint	VertexOffset = (uint) Vertices.Count;
                uint	VerticesCount = (uint) Polygon.m_Vertices.Length;

                float	PolygonArea = 0.0f;
                for ( uint FaceTriangleIndex=0; FaceTriangleIndex < VerticesCount-2; FaceTriangleIndex++ ) {
                    Indices_Faces.Add( VertexOffset + 0 );
                    Indices_Faces.Add( VertexOffset + 1 + FaceTriangleIndex );
                    Indices_Faces.Add( VertexOffset + 2 + FaceTriangleIndex );

                    float	Area = 0.5f * (Polygon.m_Vertices[2 + FaceTriangleIndex] - Polygon.m_Vertices[0]).Cross( Polygon.m_Vertices[1 + FaceTriangleIndex] - Polygon.m_Vertices[0] ).Length;
                    PolygonArea += Area;
                }
                AreaMin = Math.Min( AreaMin, PolygonArea );
                AreaMax = Math.Max( AreaMax, PolygonArea );
                TotalArea += PolygonArea;

                for ( uint VertexIndex=0; VertexIndex < VerticesCount; VertexIndex++ ) {
                    Indices_Edges.Add( VertexOffset + VertexIndex );
                    Indices_Edges.Add( VertexOffset + (VertexIndex+1) % VerticesCount );
                }

                float3	Color = PolygonArea < AreaThresholdLow ? new float3( 1, 0, 0 ) : PolygonArea > AreaThresholdHigh ? new float3( 0, 1, 0 ) : new float3( 1, 1, 0 );

                foreach ( float3 Vertex in Polygon.m_Vertices ) {
                    Vertices.Add( new VertexP3N3() { P = Vertex, N = Color } );
                }

            }

            m_AreaMin = AreaMin;
            m_AreaMax = AreaMax;
            m_AreaAvg = TotalArea / m_NeighborPositions.Length;
            labelStats.Text = "Area min: " + m_AreaMin + "\r\n"
                            + "Area max: " + m_AreaMax + "\r\n"
                            + "Area average: " + m_AreaAvg + "\r\n"
                            + "Area total: " + TotalArea + "\r\n";

            m_Prim_CellFaces = new Primitive( m_Device, Vertices.Count, VertexP3N3.FromArray( Vertices.ToArray() ), Indices_Faces.ToArray(), Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3 );
            m_Prim_CellEdges = new Primitive( m_Device, Vertices.Count, VertexP3N3.FromArray( Vertices.ToArray() ), Indices_Edges.ToArray(), Primitive.TOPOLOGY.LINE_LIST, VERTEX_FORMAT.P3N3 );
        }