private int drawKoch(SceneBrep scene, Matrix4 m, string param)
        {
            Vector3 A, B, C, D;
            int     one, two, three, four;

            //float a = 1.0f;
            A.X   = 0;
            A.Y   = 0;
            A.Z   = 0;
            one   = scene.AddVertex(Vector3.TransformPosition(A, m));
            B.X   = 1.0f;
            B.Y   = 1.0f;
            B.Z   = 0;
            two   = scene.AddVertex(Vector3.TransformPosition(B, m));
            C.X   = 0;
            C.Y   = 1.0f;
            C.Z   = 1.0f;
            three = scene.AddVertex(Vector3.TransformPosition(C, m));
            D.X   = 1.0f;
            D.Y   = 0;
            D.Z   = 1.0f;
            four  = scene.AddVertex(Vector3.TransformPosition(D, m));

            scene.AddLine(one, two);
            scene.AddLine(two, three);
            scene.AddLine(three, one);
            scene.AddLine(four, one);
            scene.AddLine(four, two);
            scene.AddLine(four, three);

            drawKochRecursive(depthParam, scene, m, param, A, B, C, D);

            return(segments + (int)Math.Pow(4, depthParam) + 1);
        }
Пример #2
0
        private int CreateTetrahedron(SceneBrep scene, Matrix4 m, Vector3 center, float size)
        {
            int[] v = new int[4];
            float z = (float)(size * Math.Sqrt(0.5));

            Vector3 A = new Vector3(size, 0.0f, -z);
            Vector3 B = new Vector3(-size, 0.0f, -z);
            Vector3 C = new Vector3(0.0f, size, z);
            Vector3 D = new Vector3(0.0f, -size, z);

            // vertices:
            v[0] = scene.AddVertex(Vector3.TransformPosition(A, m));
            v[1] = scene.AddVertex(Vector3.TransformPosition(B, m));
            v[2] = scene.AddVertex(Vector3.TransformPosition(C, m));
            v[3] = scene.AddVertex(Vector3.TransformPosition(D, m));

            // normal vectors:
            scene.SetNormal(v[0], Vector3.TransformVector(A, m).Normalized());
            scene.SetNormal(v[1], Vector3.TransformVector(B, m).Normalized());
            scene.SetNormal(v[2], Vector3.TransformVector(C, m).Normalized());
            scene.SetNormal(v[3], Vector3.TransformVector(D, m).Normalized());

            // texture coordinates:
            scene.SetTxtCoord(v[0], new Vector2(1.0f, 0.0f));
            scene.SetTxtCoord(v[1], new Vector2(0.0f, 0.0f));
            scene.SetTxtCoord(v[2], new Vector2(1.0f, 1.0f));
            scene.SetTxtCoord(v[3], new Vector2(0.0f, 1.0f));

            // colors:
            long seed = (long)Math.Min(long.MaxValue, (m.Row3.LengthSquared * 10000.0f));

            seed = RandomStatic.numericRecipes(seed);
            float r = (seed & 255) / 255.0f;

            seed = RandomStatic.numericRecipes(seed);
            float g = (seed & 255) / 255.0f;

            seed = RandomStatic.numericRecipes(seed);
            float b = (seed & 255) / 255.0f;

            scene.SetColor(v[0], new Vector3(r, g, b));
            r = Math.Min(r + 0.2f, 1.0f);
            scene.SetColor(v[1], new Vector3(r, g, b));
            g = Math.Min(g + 0.2f, 1.0f);
            scene.SetColor(v[2], new Vector3(r, g, b));
            b = Math.Min(b + 0.2f, 1.0f);
            scene.SetColor(v[3], new Vector3(r, g, b));

            // triangle faces:
            scene.AddTriangle(v[0], v[1], v[2]);
            scene.AddTriangle(v[2], v[1], v[3]);
            scene.AddTriangle(v[1], v[0], v[3]);
            scene.AddTriangle(v[2], v[3], v[0]);

            return(4);
        }
Пример #3
0
        private void firstCircle(Vector3 v1, Vector3 v2, SceneBrep scene, Matrix4 m)
        {
            Vector3 dif  = (v2 - v1) * size;
            Vector3 rdif = (v2 - v1) * (1 - size);

            last_circle = createCircleAround(v1, v2);
            float s = radius / curve.current_count;

            last_circle     = Transforms.scale(last_circle, s, s, s);
            connect_indices = new int[cdetail];
            Vector3 center = getCenter(last_circle);

            for (int i = 0; i < cdetail; i++)
            {
                connect_indices[i] = scene.AddVertex(Vector3.TransformPosition(last_circle[i] + v1, m));
                scene.SetNormal(connect_indices[i], Vector3.TransformVector(last_circle[i] - center, m).Normalized());
                float r = (last_circle[i].X + v1.X + 1.1f) / 2.5f;
                float g = (last_circle[i].Y + v1.Y + 1.1f) / 2.5f;
                float b = (last_circle[i].Z + v1.Z + 1.1f) / 2.5f;
                scene.SetColor(connect_indices[i], new Vector3(r, g, b));
            }

            for (int i = 0; i < cdetail; i++)
            {
                last_circle[i] += v1 + rdif;
            }
        }
Пример #4
0
        private int lastCircle(Vector3 v1, Vector3 v2, SceneBrep scene, Matrix4 m)
        {
            _ = createCircleAround(v1, v2);
            Vector3 center = getCenter(last_circle);
            Vector3 dif    = (v2 - v1) * size;

            _ = (v2 - v1) * (1 - size);
            int[] v = new int[cdetail];

            for (int i = 0; i < cdetail; i++)
            {
                v[i] = scene.AddVertex(Vector3.TransformPosition(last_circle[i] + dif, m));
                scene.SetNormal(v[i], Vector3.TransformVector(last_circle[i] - center, m).Normalized());
                float r = (last_circle[i].X + 1.1f) / 2.5f;
                float g = (last_circle[i].Y + 1.1f) / 2.5f;
                float b = (last_circle[i].Z + 1.1f) / 2.5f;
                scene.SetColor(v[i], new Vector3(r, g, b));
            }

            for (int i = 0; i < cdetail - 1; i++)
            {
                scene.AddTriangle(connect_indices[i], v[i], connect_indices[i + 1]);
                scene.AddTriangle(connect_indices[i + 1], v[i], v[i + 1]);
            }

            scene.AddTriangle(connect_indices[cdetail - 1], v[cdetail - 1], connect_indices[0]);
            scene.AddTriangle(connect_indices[0], v[cdetail - 1], v[0]);

            return(cdetail * 2);
        }
        private int drawRose(SceneBrep scene, Matrix4 m, string param)
        {
            int    number_of_points = segments;
            double period           = Math.PI * d;

            if ((n % 2 == 0) || (d % 2 == 0))
            {
                period *= 2;
            }

            double s  = 0.0;
            double ds = 1.0 / number_of_points;

            double     dtheta = period / number_of_points;
            List <int> points = new List <int>();
            double     k      = (double)n / d;

            for (int i = 0; i < number_of_points; i++)
            {
                double theta = i * dtheta;
                double r     = scale * Math.Cos(k * theta);
                float  x     = (float)(r * Math.Cos(theta * ratioX));
                float  y     = (float)(r * Math.Sin(theta * ratioY));
                float  z     = (float)(r * Math.Cos(theta * i * ratioZ));
                if (noRatioZ)
                {
                    z = (float)(r * Math.Cos(theta));
                }
                if (noRatioX)
                {
                    x = (float)(r * Math.Cos(theta));
                }
                if (noRatioY)
                {
                    y = (float)(r * Math.Sin(theta));
                }


                Vector3 A = new Vector3(x, y, z);
                int     v = scene.AddVertex(Vector3.TransformPosition(A, m));
                points.Add(v);
                scene.SetTxtCoord(v, new Vector2((float)s, (float)s));
                System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0));
                scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f));
                s += ds;
            }

            for (int i = 0; i < number_of_points - 1; i++)
            {
                scene.AddLine(points[i], points[i + 1]);
            }
            scene.AddLine(points[0], points[points.Count - 1]);
            return(number_of_points);
        }
        private int drawTetrahedron(SceneBrep scene, Matrix4 m, string param)
        {
            segments = 4;
            scene.Reserve(segments + (int)Math.Pow(4, depthParam) + 1);

            /*c.Line(0, 0, 0, c.Height);
             * c.Line(0, c.Height, c.Width, c.Height / 2);
             * c.Line(c.Width, c.Height / 2, 0, 0);
             */
            Vector3 A, B, C, D;
            int     one, two, three, four;

            //float a = 1.0f;
            A.X   = 0;
            A.Y   = 0;
            A.Z   = 0;
            one   = scene.AddVertex(Vector3.TransformPosition(A, m));
            B.X   = 1.0f;
            B.Y   = 1.0f;
            B.Z   = 0;
            two   = scene.AddVertex(Vector3.TransformPosition(B, m));
            C.X   = 0;
            C.Y   = 1.0f;
            C.Z   = 1.0f;
            three = scene.AddVertex(Vector3.TransformPosition(C, m));
            D.X   = 1.0f;
            D.Y   = 0;
            D.Z   = 1.0f;
            four  = scene.AddVertex(Vector3.TransformPosition(D, m));

            scene.AddLine(one, two);
            scene.AddLine(two, three);
            scene.AddLine(three, one);
            scene.AddLine(four, one);
            scene.AddLine(four, two);
            scene.AddLine(four, three);

            drawTetrahedronRecursive(depthParam, scene, m, param, A, B, C, D);

            return(segments + (int)Math.Pow(4, depthParam) + 1);
        }
Пример #7
0
 internal void RenderSceneJustCurve(SceneBrep scene)
 {
     for (int i = 0; i < Vertices.Count; i++)
     {
         scene.AddVertex(Vertices[i]);
         if (i > 0)
         {
             scene.AddLine(i - 1, i);
         }
     }
     scene.AddLine(0, Vertices.Count - 1);
 }
        private int drawLineBezier(SceneBrep scene, Matrix4 m, string param, Vector3 P0, Vector3 P1, Vector3 P2)
        {
            int segm = 200;

            float hun = 100.0f;


            List <int> pointsList = new List <int>();

            addCube(scene, m, param);


            //P0 -= delta; P1 -= delta;P2 -= delta;

            for (int i = 0; i <= 100; i++)
            {
                float   t = ((float)(i)) / hun;
                Vector3 P = (1 - t) * ((1 - t) * P0 + t * P1) + t * ((1 - t) * P1 + t * P2);
                //float x = (1-t)*(1-t)*x1 + 2*(1-t)*t*x2+t*t*x3;
                //float y = (1-t)*(1-t)*y1 + 2*(1-t)*t*y2+t*t*y3;
                pointsList.Add(scene.AddVertex(Vector3.TransformPosition(P, m)));
            }

            for (int i = 0; i < pointsList.Count - 1; i++)
            {
                scene.AddLine(pointsList[i], pointsList[i + 1]);
            }

            int p0, p1, p2;

            p0 = scene.AddVertex(Vector3.TransformPosition(P0, m));
            p1 = scene.AddVertex(Vector3.TransformPosition(P1, m));
            p2 = scene.AddVertex(Vector3.TransformPosition(P2, m));

            scene.AddLine(p0, p1);
            scene.AddLine(p1, p2);

            return(segm + 2);
        }
Пример #9
0
        /// <summary>
        /// Construct a new Brep solid (preferebaly closed = regular one).
        /// </summary>
        /// <param name="scene">B-rep scene to be modified</param>
        /// <param name="m">Transform matrix (object-space to world-space)</param>
        /// <param name="param">Shape parameters if needed</param>
        /// <returns>Number of generated faces (0 in case of failure)</returns>
        public int AddMesh(SceneBrep scene, Matrix4 m, string param)
        {
            // {{ TODO: put your Mesh-construction code here

            parseParams(param);

            // If there will be large number of new vertices, reserve space for them to save time.
            scene.Reserve(segments + 1);

            double t  = 0.0;
            double dt = maxT / segments;
            double s  = 0.0; // for both texture coordinate & color ramp
            double ds = 1.0 / segments;

            int     vPrev = 0;
            Vector3 A;

            for (int i = 0; i <= segments; i++)
            {
                // New vertex's coordinates.
                A.X = (float)(radius * Math.Cos(kx * t + dx));
                A.Y = (float)(radius * Math.Cos(ky * t + dy));
                A.Z = (float)(radius * Math.Cos(kz * t + dz));

                // New vertex.
                int v = scene.AddVertex(Vector3.TransformPosition(A, m));

                // Vertex attributes.
                scene.SetTxtCoord(v, new Vector2((float)s, (float)s));
                System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0));
                scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f));

                // New line?
                if (i > 0)
                {
                    scene.AddLine(vPrev, v);
                }

                // Next vertex.
                t    += dt;
                s    += ds;
                vPrev = v;
            }

            // Thick line (for rendering).
            scene.LineWidth = 3.0f;

            return(segments);

            // }}
        }
        private int drawOctahedron(SceneBrep scene, Matrix4 m, string param, float sc, Vector3 color, double s, double theta)
        {
            int     one, two, three, four, five, six;
            float   localScale = (float)sc;
            Vector4 row1       = new Vector4((float)Math.Cos(theta), (float)-Math.Sin(theta), 0, 0);
            Vector4 row2       = new Vector4((float)Math.Sin(theta), (float)Math.Cos(theta), 0, 0);
            Vector4 row3       = new Vector4(0, 0, 1, 0);
            Vector4 row4       = new Vector4(0, 0, 0, 1);

            m = new Matrix4(row1, row2, row3, row4);

            Vector3 A = new Vector3(localScale, 0, 0);

            one   = scene.AddVertex(Vector3.TransformPosition(A, m));
            A     = new Vector3(0, localScale, 0);
            two   = scene.AddVertex(Vector3.TransformPosition(A, m));
            A     = new Vector3(-localScale, 0, 0);
            three = scene.AddVertex(Vector3.TransformPosition(A, m));
            A     = new Vector3(0, -localScale, 0);
            four  = scene.AddVertex(Vector3.TransformPosition(A, m));
            A     = new Vector3(0, 0, localScale);
            five  = scene.AddVertex(Vector3.TransformPosition(A, m));
            A     = new Vector3(0, 0, -localScale);
            six   = scene.AddVertex(Vector3.TransformPosition(A, m));

            scene.SetTxtCoord(one, new Vector2((float)s, (float)s));
            scene.SetTxtCoord(two, new Vector2((float)s, (float)s));
            scene.SetTxtCoord(three, new Vector2((float)s, (float)s));
            scene.SetTxtCoord(four, new Vector2((float)s, (float)s));
            scene.SetTxtCoord(five, new Vector2((float)s, (float)s));
            scene.SetTxtCoord(six, new Vector2((float)s, (float)s));

            scene.SetColor(one, color);
            scene.SetColor(two, color);
            scene.SetColor(three, color);
            scene.SetColor(four, color);
            scene.SetColor(five, color);
            scene.SetColor(six, color);


            scene.AddLine(one, two);
            scene.AddLine(two, three);
            scene.AddLine(three, four);
            scene.AddLine(four, one);

            scene.AddLine(five, one);
            scene.AddLine(five, two);
            scene.AddLine(five, three);
            scene.AddLine(five, four);

            scene.AddLine(six, one);
            scene.AddLine(six, two);
            scene.AddLine(six, three);
            scene.AddLine(six, four);


            return(6);
        }
Пример #11
0
        /// <summary>
        /// Do Loop subdivision of the input control triangle mesh.
        /// </summary>
        /// <param name="scene">Input scene (must not be changed).</param>
        /// <param name="epsilon">Reference error toleration (size of undivided triangle).</param>
        /// <param name="normals">Generate normals? (optional)</param>
        /// <param name="colors">Generate colors? (optional)</param>
        /// <param name="txtcoord">Generate texture coordinates? (optional)</param>
        /// <param name="time">Current time for animation (optional)</param>
        /// <param name="param">Optional additional parameters.</param>
        /// <returns>Number of generated points.</returns>
        public int Subdivide(SceneBrep scene, float epsilon, bool normals, bool colors, bool txtcoord, float time, string param)
        {
            result = scene.Clone();

            // !!!{{ TODO: put your Loop subdivision code here

            // pilot: do one (trivial) division
            int triangles = result.Triangles;
            int tr;

            for (tr = 0; tr < triangles; tr++)
            {
                int A, B, C;
                result.GetTriangleVertices(tr, out A, out B, out C);
                Vector3 vA = result.GetVertex(A);
                Vector3 vB = result.GetVertex(B);
                Vector3 vC = result.GetVertex(C);

                Vector3 vA2 = (vB + vC) * 0.5f;
                Vector3 vB2 = (vA + vC) * 0.5f;
                Vector3 vC2 = (vA + vB) * 0.5f;
                int     A2  = result.AddVertex(vA2);
                int     B2  = result.AddVertex(vB2);
                int     C2  = result.AddVertex(vC2);

                result.AddTriangle(B2, C2, A2);
                result.AddTriangle(B2, A2, C);
                result.AddTriangle(C2, B, A2);
                result.SetTriangleVertices(tr, A, C2, B2);
            }

            //result.BuildCornerTable();

            return(result.Vertices);

            // !!!}}
        }
Пример #12
0
        internal void RenderSceneSimpleTube(SceneBrep scene)
        {
            int total = -1;

            for (int i = 0; i < MeshVertices.Count; i++)
            {
                for (int j = 0; j < MeshVertices[i].Count; j++)
                {
                    scene.AddVertex(MeshVertices[i][j]);
                    total++;
                }
                //AddToScene(scene, MeshVertices[i], total);
                if (i > 0)
                {
                    ConnectTwoCircles(scene, MeshVertices, total, i);
                }
            }
            ConnectTwoCircles(scene, MeshVertices, total, 0);
        }
        private void drawTetrahedronRecursive(int depth, SceneBrep scene, Matrix4 m, string param, Vector3 a, Vector3 b, Vector3 c, Vector3 d)
        {
            if (depth == 0)
            {
                return;
            }
            Vector3 stand;

            stand.X = 0;
            stand.Y = 0;
            stand.Z = 0;
            int     ab, ac, ad, bc, bd, cd;
            Vector3 AB = stand, AC = stand, AD = stand;
            Vector3 BC = stand, BD = stand;
            Vector3 CD = stand;

            calculateMiddle(a, b, c, d, ref AB, ref AC, ref AD, ref BC, ref BD, ref CD);

            ab = scene.AddVertex(Vector3.TransformPosition(AB, m));
            ac = scene.AddVertex(Vector3.TransformPosition(AC, m));
            ad = scene.AddVertex(Vector3.TransformPosition(AD, m));
            bc = scene.AddVertex(Vector3.TransformPosition(BC, m));
            bd = scene.AddVertex(Vector3.TransformPosition(BD, m));
            cd = scene.AddVertex(Vector3.TransformPosition(CD, m));

            scene.AddLine(ab, ac);
            scene.AddLine(ac, bc);
            scene.AddLine(bc, ab);

            scene.AddLine(ab, bd);
            scene.AddLine(bd, ad);
            scene.AddLine(ad, ab);

            scene.AddLine(ac, ad);
            scene.AddLine(ad, cd);
            scene.AddLine(cd, ac);

            scene.AddLine(bc, cd);
            scene.AddLine(cd, bd);
            scene.AddLine(bd, bc);

            drawTetrahedronRecursive(depth - 1, scene, m, param, AB, b, BC, BD);
            drawTetrahedronRecursive(depth - 1, scene, m, param, a, AB, AC, AD);
            drawTetrahedronRecursive(depth - 1, scene, m, param, AC, BC, c, CD);
            drawTetrahedronRecursive(depth - 1, scene, m, param, AD, BD, CD, d);
        }
Пример #14
0
        /// <summary>
        /// Construct a new control mesh for subdivision (formally the Brep solid..).
        /// </summary>
        /// <param name="scene">B-rep scene to be modified</param>
        /// <param name="m">Transform matrix (object-space to world-space)</param>
        /// <param name="time">Current time in seconds</param>
        /// <param name="param">Shape parameters if needed</param>
        public void ControlMesh(SceneBrep scene, Matrix4 m, float time, string param)
        {
            // !!!{{ TODO: put your Control-mesh-construction code here

            List <int> upper = new List <int>();

            upper.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(0, 1, 0), m)));
            upper.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(1, 1, 0), m)));
            upper.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(1, 1, 1), m)));
            upper.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(0, 1, 1), m)));

            List <int> lower = new List <int>();

            lower.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(0, 0, 0), m)));
            lower.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(1, 0, 0), m)));
            lower.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(1, 0, 1), m)));
            lower.Add(scene.AddVertex(Vector3.TransformPosition(new Vector3(0, 0, 1), m)));

            // Lower face
            scene.AddTriangle(lower[0], lower[1], lower[2]);
            scene.AddTriangle(lower[2], lower[3], lower[0]);

            // Upper face
            scene.AddTriangle(upper[2], upper[1], upper[0]);
            scene.AddTriangle(upper[0], upper[3], upper[2]);

            // Side faces
            for (int i = 0; i < upper.Count; i++)
            {
                int j = i < (upper.Count - 1) ? i + 1 : 0;
                scene.AddTriangle(upper[i], upper[j], lower[i]);
                scene.AddTriangle(lower[i], upper[j], lower[j]);
            }

            // !!!}}
        }
        private int drawSpiral(SceneBrep scene, Matrix4 m, string param)
        {
            int number_of_points = segments;

            double     period = Math.PI * scale;
            double     dtheta = period / number_of_points;
            List <int> points = new List <int>();
            double     ds     = 1.0 / (double)number_of_points;
            double     s      = 0.0;

            for (int i = 0; i < number_of_points; i++)
            {
                double theta = i * dtheta;
                float  x     = (float)(s * s * Math.Cos(theta * ratioX));
                float  y     = (float)(s * s * Math.Sin(theta * ratioY));
                float  z     = (float)(s * s);



                Vector3 A = new Vector3(x, y, z);
                int     v = scene.AddVertex(Vector3.TransformPosition(A, m));

                scene.SetTxtCoord(v, new Vector2((float)s, (float)s));
                System.Drawing.Color c = Raster.Draw.ColorRamp(0.5 * (s + 1.0));
                scene.SetColor(v, new Vector3(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f));

                points.Add(v);
                s += ds;
            }

            for (int i = 0; i < number_of_points - 1; i++)
            {
                scene.AddLine(points[i], points[i + 1]);
            }
            //scene.AddLine(points[0], points[points.Count - 1]);
            return(number_of_points);
        }
Пример #16
0
        private int createBentPipe(SceneBrep scene, Matrix4 m, Vector3 v1, Vector3 v2, Vector3 v3)
        {
            Vector3[] centers    = new Vector3[bdetail];
            Vector3   oned       = (v1 - v2) * size;
            Vector3   twod       = (v3 - v2) * size;
            Vector3   rtwod      = (v3 - v2) * (1 - size);
            Vector3   rot_point  = v2 + oned + twod;
            Vector3   test_point = oned + twod;

            test_point.X = Math.Abs(test_point.X);
            test_point.Y = Math.Abs(test_point.Y);
            test_point.Z = Math.Abs(test_point.Z);
            double angle = -(Math.PI / 2) / (bdetail - 1);
            char   axis;

            if (test_point.Y < test_point.X)
            {
                axis = (test_point.Z < test_point.Y) ? 'z' : 'y';
            }
            else
            {
                axis = (test_point.Z < test_point.X) ? 'z' : 'x';
            }

            MyMatrix4 mat = MyMatrix4.getSingleRotationMatrix(angle, axis);
            float     l   = longestSide(last_circle[0] - v1);
            Vector3   sp  = rotateAroundPoint(last_circle[0], rot_point, mat);
            float     l2  = longestSide(sp - v1);

            if (l2 < l)
            {
                mat = MyMatrix4.getSingleRotationMatrix(-angle, axis);
            }
            Vector3[] field = new Vector3[bdetail * cdetail];
            Array.Copy(last_circle, field, cdetail);
            centers[0] = getCenter(last_circle);
            Vector3[] new_circle = new Vector3[cdetail];

            for (int i = 1; i < bdetail; i++)
            {
                Array.ConstrainedCopy(field, (i - 1) * cdetail, new_circle, 0, cdetail);
                new_circle = rotateAroundPoint(new_circle, rot_point, mat);
                Array.ConstrainedCopy(new_circle, 0, field, i * cdetail, cdetail);
                centers[i] = getCenter(new_circle);
            }

            Array.ConstrainedCopy(field, (bdetail - 1) * cdetail, last_circle, 0, cdetail);
            for (int i = 0; i < cdetail; i++)
            {
                last_circle[i] += rtwod - twod;
            }
            int[] v = new int[field.Length];

            for (int i = 0; i < field.Length; i++)
            {
                v[i] = scene.AddVertex(Vector3.TransformPosition(field[i], m));
                scene.SetNormal(v[i], Vector3.TransformVector(field[i] - centers[i / cdetail], m).Normalized());
                float r = (field[i].X + 1.1f) / 2.5f;
                float g = (field[i].Y + 1.1f) / 2.5f;
                float b = (field[i].Z + 1.1f) / 2.5f;
                scene.SetColor(v[i], new Vector3(r, g, b));
            }

            for (int i = 0; i < cdetail - 1; i++)
            {
                scene.AddTriangle(connect_indices[i], v[i], connect_indices[i + 1]);
                scene.AddTriangle(connect_indices[i + 1], v[i], v[i + 1]);
            }

            scene.AddTriangle(connect_indices[cdetail - 1], v[cdetail - 1], connect_indices[0]);
            scene.AddTriangle(connect_indices[0], v[cdetail - 1], v[0]);

            for (int i = 0; i < (bdetail - 1); i++)
            {
                for (int j = 0; j < cdetail - 1; j++)
                {
                    int a = i * cdetail + j;
                    scene.AddTriangle(v[a], v[a + cdetail], v[a + 1]);
                    scene.AddTriangle(v[a + 1], v[a + cdetail], v[a + cdetail + 1]);
                }
                int b = i * cdetail + cdetail - 1;
                scene.AddTriangle(v[b], v[b + cdetail], v[b - cdetail + 1]);
                scene.AddTriangle(v[b - cdetail + 1], v[b + cdetail], v[b + 1]);
            }
            Array.ConstrainedCopy(v, (bdetail - 1) * cdetail, connect_indices, 0, cdetail);

            return(bdetail * cdetail * 2 + cdetail * 2);
        }
Пример #17
0
        /// <summary>
        /// Reads one 3D scene from a given stream (containing text variant of Wavefront OBJ format).
        /// </summary>
        /// <param name="reader">Already open text reader</param>
        /// <param name="scene">Scene to be modified</param>
        /// <param name="scene">Matrix for instancing</param>
        /// <returns>Number of faces read</returns>
        public int ReadBrep( StreamReader reader, SceneBrep scene, Matrix4 m )
        {
            if ( reader == null ) return SceneBrep.NULL;

              Debug.Assert( scene != null );
              int v0 = scene.Vertices;
              int lastVertex = v0 - 1;

              int faces = 0;
              List<Vector3> normals = new List<Vector3>( 256 );
              int lastNormal = -1;
              int[] f = new int[ 3 ];

              do
              {
            string line = reader.ReadLine();
            if ( line == null ) break;

            int commentPos = line.IndexOf( COMMENT );
            if ( commentPos >= 0 )
              line = line.Substring( 0, commentPos );

            string[] tokens = line.Split( DELIMITERS , StringSplitOptions.RemoveEmptyEntries );
            if ( tokens.Length < 1 ) continue;

            switch ( tokens[ 0 ] )
            {
              case VERTEX:
            if ( tokens.Length < 4 ) continue;

            Vector3 coord;
            if ( !float.TryParse( tokens[ 1 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.X ) ||
                 !float.TryParse( tokens[ 2 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Y ) ||
                 !float.TryParse( tokens[ 3 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Z ) )
              continue;

            if ( MirrorConversion )
              coord.Z = -coord.Z;
            lastVertex = scene.AddVertex( Vector3.Transform( coord, m ) );
            break;

              case VERTEX_NORMAL:
            if ( tokens.Length < 4 ) continue;

            Vector3 norm;
            if ( !float.TryParse( tokens[ 1 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.X ) ||
                 !float.TryParse( tokens[ 2 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Y ) ||
                 !float.TryParse( tokens[ 3 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Z ) )
              continue;

            if ( MirrorConversion )
              norm.Z = -norm.Z;
            normals.Add( Vector3.TransformNormal( norm, m ) );
            lastNormal++;
            break;

              case FACE:
            if ( tokens.Length < 4 ) continue;
            int N = tokens.Length - 1;
            if ( f.Length < N )
              f = new int[ N ];
            int i;

            for ( i = 0; i < N; i++ )       // read indices for one vertex
            {
              string[] vt = tokens[ i + 1 ].Split( '/' );
              int ti, ni;
              ti = ni = 0;                  // 0 => value not present

              // 0 .. vertex coord index
              if ( !int.TryParse( vt[ 0 ], out f[ i ] ) ||
                   f[ i ] == 0 )
                break;

              if ( f[ i ] > 0 )
                f[ i ] = v0 + f[ i ] - 1;
              else
                f[ i ] = lastVertex + 1 - f[ i ];

              if ( vt.Length > 1 )
              {
                // 1 .. texture coord index (not yet)
                if ( !int.TryParse( vt[ 1 ], out ti ) ) ti = 0;

                if ( vt.Length > 2 )
                {
                  // 2 .. normal vector index
                  if ( !int.TryParse( vt[ 2 ], out ni ) ) ni = 0;
                }
              }
              // there was a normal..
              if ( ni != 0 )
              {
                if ( ni > 0 )
                  ni--;
                else
                  ni = lastNormal + 1 - ni;
                if ( ni >= 0 && ni < normals.Count )
                  scene.SetNormal( f[ i ], normals[ ni ] );
              }
            }

            N = i;
            for ( i = 1; i < N - 1; i++ )
            {
              scene.AddTriangle( f[ 0 ], f[ i ], f[ i + 1 ] );
              faces++;
            }

            break;
            }
              }
              while ( !reader.EndOfStream );

              return faces;
        }
        private int dragonCurve(SceneBrep scene, Matrix4 m, string param)
        {
            int        retValue     = 1;
            List <int> turnSequence = new List <int>();

            for (int i = 0; i < depthParam; i++)
            {
                var copy = new List <int>(turnSequence);
                copy.Reverse();
                turnSequence.Add(1);
                foreach (int turn in copy)
                {
                    turnSequence.Add(-turn);
                }
            }
            bool    increasez = false;
            int     one, two;
            double  z2;
            double  startingAngle = -depthParam * (Math.PI / 4);
            double  side = 400 / Math.Pow(2, depthParam / 2.0);
            double  angle = startingAngle;
            int     x1 = 230, y1 = 350;
            int     x2 = x1 + (int)(Math.Cos(angle) * side);
            int     y2 = y1 + (int)(Math.Sin(angle) * side);
            Vector3 A  = new Vector3(x1, y1, 0);

            one = scene.AddVertex(Vector3.TransformPosition(A, m));
            A   = new Vector3(x2, y2, 0);
            two = scene.AddVertex(Vector3.TransformPosition(A, m));
            scene.AddLine(one, two);
            x1  = x2;
            y1  = y2;
            one = two;
            int turns = turnSequence.Count / 100;
            int j     = 0;

            z2 = 1;
            int direction = 0;

            foreach (int turn in turnSequence)
            {
                angle += turn * (Math.PI / 2);
                x2     = x1 + (int)(Math.Cos(angle) * side);
                y2     = y1 + (int)(Math.Sin(angle) * side);
                if (j >= turns)
                {
                    j         = 0;
                    increasez = !increasez;
                    if (increasez)
                    {
                        direction++;
                    }
                }
                if (increasez)
                {
                    if (direction % 2 == 0)
                    {
                        z2 = z2 + Math.Abs((int)(Math.Cos(angle) * side));
                    }
                    else
                    {
                        z2 = z2 - Math.Abs((int)(Math.Cos(angle) * side));
                    }
                }

                A   = new Vector3(x2, y2, (float)z2);
                two = scene.AddVertex(Vector3.TransformPosition(A, m));
                scene.AddLine(one, two);
                x1  = x2;
                y1  = y2;
                one = two;
                retValue++;
                j++;
            }
            return(retValue);
        }
        private void addCube(SceneBrep scene, Matrix4 m, string param)
        {
            Vector3 P0, P1, P2, P3;
            Vector3 D0, D1, D2, D3;

            P0.X = 0;
            P0.Y = 0;
            P0.Z = 0;

            P1.X = 1;
            P1.Y = 0;
            P1.Z = 0;

            P2.X = 1;
            P2.Y = 1;
            P2.Z = 0;

            P3.X = 0;
            P3.Y = 1;
            P3.Z = 0;

            D0.X = 0;
            D0.Y = 0;
            D0.Z = 1;

            D1.X = 1;
            D1.Y = 0;
            D1.Z = 1;

            D2.X = 1;
            D2.Y = 1;
            D2.Z = 1;

            D3.X = 0;
            D3.Y = 1;
            D3.Z = 1;

            /*Vector3 delta;
             * delta.X = 0.5f;
             * delta.Y = 0.5f;
             * delta.Z = 0.5f;
             *
             * P0 -= delta;P1 -= delta;P2 -= delta;P3 -= delta;
             * D0 -= delta;
             * D1 -= delta;
             * D2 -= delta;
             * D3 -= delta;*/

            int p0 = scene.AddVertex(Vector3.TransformPosition(P0, m));
            int p1 = scene.AddVertex(Vector3.TransformPosition(P1, m));
            int p2 = scene.AddVertex(Vector3.TransformPosition(P2, m));
            int p3 = scene.AddVertex(Vector3.TransformPosition(P3, m));

            int d0 = scene.AddVertex(Vector3.TransformPosition(D0, m));
            int d1 = scene.AddVertex(Vector3.TransformPosition(D1, m));
            int d2 = scene.AddVertex(Vector3.TransformPosition(D2, m));
            int d3 = scene.AddVertex(Vector3.TransformPosition(D3, m));

            scene.AddLine(p0, p1);
            scene.AddLine(p1, p2);
            scene.AddLine(p2, p3);
            scene.AddLine(p3, p0);

            scene.AddLine(d0, d1);
            scene.AddLine(d1, d2);
            scene.AddLine(d2, d3);
            scene.AddLine(d3, d0);

            scene.AddLine(p0, d0);
            scene.AddLine(p1, d1);
            scene.AddLine(p2, d2);
            scene.AddLine(p3, d3);
        }
Пример #20
0
        /// <summary>
        /// Reads one 3D scene from a given stream (containing text variant of Wavefront OBJ format).
        /// </summary>
        /// <param name="reader">Already open text reader</param>
        /// <param name="scene">Scene to be modified</param>
        /// <param name="scene">Matrix for instancing</param>
        /// <returns>Number of faces read</returns>
        public int ReadBrep(StreamReader reader, SceneBrep scene, Matrix4 m)
        {
            if (reader == null)
            {
                return(SceneBrep.NULL);
            }

            Debug.Assert(scene != null);
            int v0         = scene.Vertices;
            int lastVertex = v0 - 1;

            int faces = 0;

            List <Vector2> txtCoords    = new List <Vector2>(256);
            List <Vector3> normals      = new List <Vector3>(256);
            int            lastTxtCoord = -1;
            int            lastNormal   = -1;

            int[] f = new int[3];

            do
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }

                int commentPos = line.IndexOf(COMMENT);
                if (commentPos >= 0)
                {
                    line = line.Substring(0, commentPos);
                }

                string[] tokens = line.Split(DELIMITERS, StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Length < 1)
                {
                    continue;
                }

                switch (tokens[0])
                {
                case VERTEX:
                    if (tokens.Length < 4)
                    {
                        continue;
                    }

                    Vector3 coord;
                    if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.X) ||
                        !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Y) ||
                        !float.TryParse(tokens[3], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Z))
                    {
                        continue;
                    }

                    if (MirrorConversion)
                    {
                        coord.Z = -coord.Z;
                    }
                    lastVertex = scene.AddVertex(Vector3.TransformPosition(coord, m));
                    break;

                case VERTEX_TEXTURE:
                    if (tokens.Length < 3)
                    {
                        continue;
                    }

                    Vector2 txtCoord;
                    if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out txtCoord.X) ||
                        !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out txtCoord.Y))
                    {
                        continue;
                    }

                    if (TextureUpsideDown)
                    {
                        txtCoord.Y = 1.0f - txtCoord.Y;
                    }

                    txtCoords.Add(txtCoord);
                    lastTxtCoord++;
                    break;

                case VERTEX_NORMAL:
                    if (tokens.Length < 4)
                    {
                        continue;
                    }

                    Vector3 norm;
                    if (!float.TryParse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.X) ||
                        !float.TryParse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Y) ||
                        !float.TryParse(tokens[3], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Z))
                    {
                        continue;
                    }

                    if (MirrorConversion)
                    {
                        norm.Z = -norm.Z;
                    }
                    normals.Add(Vector3.TransformNormal(norm, m));
                    lastNormal++;
                    break;

                case FACE:
                    if (tokens.Length < 4)
                    {
                        continue;
                    }
                    int N = tokens.Length - 1;
                    if (f.Length < N)
                    {
                        f = new int[N];
                    }
                    int i;

                    for (i = 0; i < N; i++) // read indices for one vertex
                    {
                        string[] vt = tokens[i + 1].Split('/');
                        int      ti, ni;
                        ti = ni = 0; // 0 => value not present

                        // 0 .. vertex coord index
                        if (!int.TryParse(vt[0], out f[i]) ||
                            f[i] == 0)
                        {
                            break;
                        }

                        if (f[i] > 0)
                        {
                            f[i] = v0 + f[i] - 1;
                        }
                        else
                        {
                            f[i] = lastVertex + 1 - f[i];
                        }

                        if (vt.Length > 1)
                        {
                            // 1 .. texture coord index (not yet)
                            if (!int.TryParse(vt[1], out ti))
                            {
                                ti = 0;
                            }

                            if (vt.Length > 2)
                            {
                                // 2 .. normal vector index
                                if (!int.TryParse(vt[2], out ni))
                                {
                                    ni = 0;
                                }
                            }
                        }

                        // there was a texture coord..
                        if (ti != 0)
                        {
                            if (ti > 0)
                            {
                                ti--;
                            }
                            else
                            {
                                ti = lastTxtCoord + 1 - ti;
                            }
                            if (ti >= 0 && ti < txtCoords.Count)
                            {
                                scene.SetTxtCoord(f[i], txtCoords[ti]);
                            }
                        }

                        // there was a normal..
                        if (ni != 0)
                        {
                            if (ni > 0)
                            {
                                ni--;
                            }
                            else
                            {
                                ni = lastNormal + 1 - ni;
                            }
                            if (ni >= 0 && ni < normals.Count)
                            {
                                scene.SetNormal(f[i], normals[ni]);
                            }
                        }
                    }

                    N = i;
                    for (i = 1; i < N - 1; i++)
                    {
                        scene.AddTriangle(f[0], f[i], f[i + 1]);
                        faces++;
                    }

                    break;
                }
            } while (!reader.EndOfStream);

            return(faces);
        }
Пример #21
0
        public int ReadBrep(string fileName, SceneBrep scene)
        {
            Debug.Assert(scene != null);

            this.scene = scene;

            scene.Reset();

            if (fileName == null || fileName.Length == 0)
            {
                throw new IOException("Invalid file name");
            }

            if (fileName.EndsWith(".gz"))
            {
                headerReader = new StreamReader(new GZipStream(new FileStream(fileName, FileMode.Open), CompressionMode.Decompress));
            }
            else
            {
                var fs = new FileStream(fileName, FileMode.Open);
                headerReader = new StreamReader(fs);
            }


            // prepare buffers for data filling
            if (!ParseHeader())
            {
                return(-1);
            }

            // read vertices
            var vertexReader = GetReader("vertex");
            var element      = vertexReader.ReadElement();

            List <Vector2> txtCoords = new List <Vector2>(256);

            int[] f          = new int[3];
            int   v0         = scene.Vertices;
            int   lastVertex = v0 - 1;

            while (element != null)
            {
                lastVertex = scene.AddVertex(Vector3.TransformPosition(element.GetVertex(), matrix));

                if (DoNormals)
                {
                    Vector3.TransformNormal(element.GetNormal(), matrix);

                    scene.SetNormal(lastVertex, element.GetNormal());
                }

                if (DoTxtCoords)
                {
                    scene.SetTxtCoord(lastVertex, element.GetTextureCoordinate());
                }

                if (DoColors)
                {
                    scene.SetColor(lastVertex, element.GetVertexColor());
                }

                element = vertexReader.ReadElement();
            }

            // read triangles
            var faceReader = GetReader("face");

            element = faceReader.ReadElement();

            while (element != null)
            {
                int A, B, C;
                element.GetTriangleVertices(out A, out B, out C);

                scene.AddTriangle(A, B, C);

                element = faceReader.ReadElement();
            }

            headerReader.Close();
            vertexReader.Close();
            faceReader.Close();

            return(scene.Triangles);
        }
Пример #22
0
        /// <summary>
        /// Construct a new Brep solid (preferebaly closed = regular one).
        /// </summary>
        /// <param name="scene">B-rep scene to be modified</param>
        /// <param name="m">Transform matrix (object-space to world-space)</param>
        /// <param name="param">Shape parameters if needed</param>
        /// <returns>Number of generated faces (0 in case of failure)</returns>
        public int AddMesh(SceneBrep scene, Matrix4 m, string param)
        {
            parseParams(param);
            scene.LineWidth = lineWidth;

            // create shape
            // create vertices in 3D local space
            Vector3[] shapeVerticesLocal = new Vector3[]
            {
                new Vector3(0, 1, depth),
                new Vector3(0.866025f, 0.5f, depth),
                new Vector3(0.866025f, -0.5f, depth),
                new Vector3(0, -1, depth),
                new Vector3(-0.866025f, -0.5f, depth),
                new Vector3(-0.866025f, 0.5f, depth),
                new Vector3(0, 0, depth),

                new Vector3(0, 1, -depth),
                new Vector3(0.866025f, 0.5f, -depth),
                new Vector3(0.866025f, -0.5f, -depth),
                new Vector3(0, -1, -depth),
                new Vector3(-0.866025f, -0.5f, -depth),
                new Vector3(-0.866025f, 0.5f, -depth),
                new Vector3(0, 0, -depth),
            };

            // push vertices to scene and get theirs indexes
            int[] shapeVerticesIndex = new int[shapeVerticesLocal.Length];
            for (int i = 0; i < shapeVerticesLocal.Length; i++)
            {
                int vertexIndex = scene.AddVertex(Vector3.TransformPosition(shapeVerticesLocal[i], m));
                shapeVerticesIndex[i] = vertexIndex;
                scene.SetColor(vertexIndex, new Vector3(0, 0, 0));
            }

            Vector3[] edgeColors = new Vector3[] { new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 0) };

            // { vertex, vertex, color }
            List <int[, ]> shapeEdges = new List <int[, ]>();

            shapeEdges.Add(new int[2, 3] {
                { 0, 1, 0 }, { 8, 9, 0 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 1, 2, 1 }, { 9, 13, 1 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 2, 6, 1 }, { 10, 9, 1 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 2, 3, 0 }, { 10, 11, 0 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 3, 4, 1 }, { 11, 13, 1 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 4, 6, 1 }, { 12, 11, 1 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 4, 5, 0 }, { 12, 7, 0 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 5, 0, 1 }, { 7, 13, 1 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 0, 6, 1 }, { 8, 7, 1 }
            });

            shapeEdges.Add(new int[2, 3] {
                { 6, 0, 2 }, { 9, 13, 2 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 6, 2, 2 }, { 11, 13, 2 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 6, 4, 2 }, { 7, 13, 2 }
            });

            shapeEdges.Add(new int[2, 3] {
                { 0, 1, 3 }, { 7, 8, 3 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 1, 2, 3 }, { 8, 9, 3 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 2, 3, 3 }, { 9, 10, 3 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 3, 4, 3 }, { 10, 11, 3 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 4, 5, 3 }, { 11, 12, 3 }
            });
            shapeEdges.Add(new int[2, 3] {
                { 5, 0, 3 }, { 12, 7, 3 }
            });



            // push edges to scene
            foreach (int[,] group in shapeEdges)
            {
                scene.AddLine(shapeVerticesIndex[group[0, 0]], shapeVerticesIndex[group[0, 1]]);
                scene.AddLine(shapeVerticesIndex[group[1, 0]], shapeVerticesIndex[group[1, 1]]);
            }


            //push faces to scene
            foreach (int[,] group in shapeEdges)
            {
                for (int i = 0; i < splits; i++)
                {
                    Vector3 beginDiff = shapeVerticesLocal[group[0, 0]] - shapeVerticesLocal[group[0, 1]];
                    beginDiff /= splits;
                    Vector3 begin = shapeVerticesLocal[group[0, 0]] - beginDiff * i;

                    Vector3 endDiff = shapeVerticesLocal[group[1, 0]] - shapeVerticesLocal[group[1, 1]];
                    endDiff /= splits;
                    Vector3 end = shapeVerticesLocal[group[1, 0]] - endDiff * i;


                    int beginIndex = scene.AddVertex(Vector3.TransformPosition(begin, m));
                    scene.SetColor(beginIndex, edgeColors[group[0, 2]]);

                    int endIndex = scene.AddVertex(Vector3.TransformPosition(end, m));
                    scene.SetColor(endIndex, edgeColors[group[1, 2]]);

                    scene.AddLine(beginIndex, endIndex);
                }
            }
            return(1); // some magic number
        }
        private void drawKochRecursive(int depth, SceneBrep scene, Matrix4 m, string param, Vector3 a, Vector3 b, Vector3 c, Vector3 d)
        {
            if (depth == 0)
            {
                return;
            }

            Vector3 stand;

            stand.X = 0;
            stand.Y = 0;
            stand.Z = 0;
            int     ab, ac, ad, bc, bd, cd, headabc, headadb, headacd, headbdc;
            Vector3 AB = stand, AC = stand, AD = stand;
            Vector3 BC = stand, BD = stand;
            Vector3 CD = stand;

            calculateMiddle(a, b, c, d, ref AB, ref AC, ref AD, ref BC, ref BD, ref CD);
            float lengthOfSide = (float)Math.Sqrt((AB.X - BC.X) * (AB.X - BC.X) + (AB.Y - BC.Y) * (AB.Y - BC.Y) + (AB.Z - BC.Z) * (AB.Z - BC.Z));
            float height       = lengthOfSide * (float)Math.Sqrt(6) / 3.0f;

            Vector3 headABC = calculateHeightVertex(a, b, c, height);
            Vector3 headADB = calculateHeightVertex(a, d, b, height);
            Vector3 headACD = calculateHeightVertex(a, c, d, height);
            Vector3 headBDC = calculateHeightVertex(b, d, c, height);


            ab      = scene.AddVertex(Vector3.TransformPosition(AB, m));
            ac      = scene.AddVertex(Vector3.TransformPosition(AC, m));
            ad      = scene.AddVertex(Vector3.TransformPosition(AD, m));
            bc      = scene.AddVertex(Vector3.TransformPosition(BC, m));
            bd      = scene.AddVertex(Vector3.TransformPosition(BD, m));
            cd      = scene.AddVertex(Vector3.TransformPosition(CD, m));
            headabc = scene.AddVertex(Vector3.TransformPosition(headABC, m));
            headadb = scene.AddVertex(Vector3.TransformPosition(headADB, m));
            headacd = scene.AddVertex(Vector3.TransformPosition(headACD, m));
            headbdc = scene.AddVertex(Vector3.TransformPosition(headBDC, m));


            scene.AddLine(ab, ac);
            scene.AddLine(ac, bc);
            scene.AddLine(bc, ab);
            scene.AddLine(ab, headabc);
            scene.AddLine(ac, headabc);
            scene.AddLine(bc, headabc);


            scene.AddLine(ab, bd);
            scene.AddLine(bd, ad);
            scene.AddLine(ad, ab);
            scene.AddLine(ab, headadb);
            scene.AddLine(bd, headadb);
            scene.AddLine(ad, headadb);


            scene.AddLine(ac, ad);
            scene.AddLine(ad, cd);
            scene.AddLine(cd, ac);
            scene.AddLine(ac, headacd);
            scene.AddLine(ad, headacd);
            scene.AddLine(cd, headacd);


            scene.AddLine(bc, cd);
            scene.AddLine(cd, bd);
            scene.AddLine(bd, bc);
            scene.AddLine(bc, headbdc);
            scene.AddLine(bd, headbdc);
            scene.AddLine(cd, headbdc);

            drawKochRecursive(depth - 1, scene, m, param, AB, AC, BC, headABC);
            drawKochRecursive(depth - 1, scene, m, param, AB, BD, AD, headADB);
            drawKochRecursive(depth - 1, scene, m, param, AD, CD, AC, headACD);
            drawKochRecursive(depth - 1, scene, m, param, BD, BC, CD, headBDC);
        }
Пример #24
0
        /// <summary>
        /// Reads one 3D scene from a given stream (containing text variant of Wavefront OBJ format).
        /// </summary>
        /// <param name="reader">Already open text reader</param>
        /// <param name="scene">Scene to be modified</param>
        /// <param name="scene">Matrix for instancing</param>
        /// <returns>Number of faces read</returns>
        public int ReadBrep( StreamReader reader, SceneBrep scene, Matrix4 m )
        {
            if ( reader == null ) return SceneBrep.NULL;

              Debug.Assert( scene != null );
              int v0 = scene.Vertices;

              int faces = 0;
              List<Vector3> normals = new List<Vector3>( 256 );
              int[] f = new int[ 3 ];

              do
              {
            string line = reader.ReadLine();
            if ( line == null ) break;

            int commentPos = line.IndexOf( COMMENT );
            if ( commentPos >= 0 )
              line = line.Substring( 0, commentPos );

            string[] tokens = line.Split( ' ' );

            switch ( tokens[ 0 ] )
            {
              case VERTEX:
            if ( tokens.Length < 4 ) continue;

            Vector3 coord;
            if ( !float.TryParse( tokens[ 1 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.X ) ||
                 !float.TryParse( tokens[ 2 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Y ) ||
                 !float.TryParse( tokens[ 3 ], NumberStyles.Float, CultureInfo.InvariantCulture, out coord.Z ) )
              continue;

            if ( MirrorConversion )
              coord.Z = -coord.Z;
            scene.AddVertex( Vector3.Transform( coord, m ) );
            break;

              case VERTEX_NORMAL:
            if ( tokens.Length < 4 ) continue;

            Vector3 norm;
            if ( !float.TryParse( tokens[ 1 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.X ) ||
                 !float.TryParse( tokens[ 2 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Y ) ||
                 !float.TryParse( tokens[ 3 ], NumberStyles.Float, CultureInfo.InvariantCulture, out norm.Z ) )
              continue;

            if ( MirrorConversion )
              norm.Z = -norm.Z;
            normals.Add( Vector3.TransformNormal( norm, m ) );
            break;

              case FACE:
            if ( tokens.Length < 4 ) continue;
            int i;

            for ( i = 0; i < 3; i++ )
            {
              string[] vt = tokens[ i + 1 ].Split( '/' );
              int ti, ni;
              ti = ni = SceneBrep.NULL;
              // 0 .. vertex coord index
              if ( !int.TryParse( vt[ 0 ], out f[ i ] ) ) break;
              f[ i ] = v0 + f[ i ] - 1;
              if ( vt.Length >= 2 )
              {
                // 1 .. texture coord index (not yet)
                int.TryParse( vt[ 1 ], out ti );
                ti--;
                if ( vt.Length >= 3 )
                {
                  // 2 .. normal vector index
                  int.TryParse( vt[ 2 ], out ni );
                  ni--;
                }
              }
              if ( ni >= 0 && ni < normals.Count )
                scene.SetNormal( f[ i ], normals[ ni ] );
            }

            if ( i >= 3 )
            {
              scene.AddTriangle( f[ 0 ], f[ 1 ], f[ 2 ] );
              faces++;
            }

            break;
            }
              }
              while ( !reader.EndOfStream );

              return faces;
        }
Пример #25
0
        /// <summary>
        /// Construct a new Brep solid (preferebaly closed = regular one).
        /// </summary>
        /// <param name="scene">B-rep scene to be modified</param>
        /// <param name="m">Transform matrix (object-space to world-space)</param>
        /// <param name="param">Shape parameters if needed</param>
        /// <returns>Number of generated faces (0 in case of failure)</returns>
        public int AddMesh(SceneBrep scene, Matrix4 m, string param)
        {
            parseParams(param);

            int elements = 6 + (depth > 0 ? 8 * (int)Math.Pow(3, depth - 1) : 0) + (center ? 1 : 0);

            scene.Reserve(elements);

            int middle = scene.AddVertex(Vector3.TransformPosition(new Vector3(0, 0, 0), m));

            GenerateBaseVertices();
            GenerateSubVertices();

            // Generate int positions
            {
                Queue <Vertex> currentVertices = new Queue <Vertex>(baseVertices);

                while (currentVertices.Count > 0)
                {
                    Vertex currentVertex = currentVertices.Dequeue();
                    currentVertex.Position = scene.AddVertex(Vector3.TransformPosition(currentVertex.Vector3, m));

                    for (int i = 0; i < currentVertex.SubVertices.Count; i++)
                    {
                        currentVertices.Enqueue(currentVertex.SubVertices[i]);
                    }
                }
            }


            if (neighbors)
            {
                Queue <Vertex> currentVertices = new Queue <Vertex>(baseVertices);

                while (currentVertices.Count > 0)
                {
                    Vertex currentVertex = currentVertices.Dequeue();
                    for (int i = 0; i < currentVertex.Neighbors.Count; i++)
                    {
                        scene.AddLine(currentVertex.Position, currentVertex.Neighbors[i].Position);
                    }

                    for (int i = 0; i < currentVertex.SubVertices.Count; i++)
                    {
                        currentVertices.Enqueue(currentVertex.SubVertices[i]);
                    }
                }
            }

            if (center)
            {
                Queue <Vertex> currentVertices = new Queue <Vertex>(baseVertices);

                while (currentVertices.Count > 0)
                {
                    Vertex currentVertex = currentVertices.Dequeue();
                    scene.AddLine(currentVertex.Position, middle);

                    for (int i = 0; i < currentVertex.SubVertices.Count; i++)
                    {
                        currentVertices.Enqueue(currentVertex.SubVertices[i]);
                    }
                }
            }

            scene.LineWidth = 3.0f;

            return(elements);
        }