Example #1
0
        public void Rebuild()
        {
            system     = null;
            definition = null;
            steps.Clear();

            Build();
        }
Example #2
0
        private void BuildNextStep(bool setCurrentItem)
        {
            if (!InitSystem())
            {
                return;
            }

            // Iterate.
            LSystems.IRewriteRules rules = definition as LSystems.IRewriteRules;
            if (rules != null)
            {
                LSystems.System nextStep = system.Clone();

                switch (rules.RewriteDirection)
                {
                case LSystems.RewriteDirection.LeftToRight:
                    nextStep.RewriteLeftToRight();
                    break;

                case LSystems.RewriteDirection.RightToLeft:
                    nextStep.RewriteRightToLeft();
                    break;

                default:
                    throw new InvalidOperationException("Unknown RewriteDirection");
                }

                nextStep.Decomposite();

                this.steps.Insert(0, new Step()
                {
                    System = nextStep, Name = string.Format("Step {0}", steps.Count)
                });

                system = nextStep;
            }

            if (setCurrentItem)
            {
                if (this.steps.Count > 0)
                {
                    CollectionViewSource.GetDefaultView(this.steps).MoveCurrentTo(this.steps[0]);
                }

                NotifyPropertyChanged("System");
            }
        }
Example #3
0
        private bool InitSystem()
        {
            if (system != null)
            {
                return(true);
            }

            SystemError = null;

            if (!BuildDefinition())
            {
                return(false);
            }

            try
            {
                system = LSystems.SystemBuilder.BuildSystem(definition);
            }
            catch (InvalidOperationException e)
            {
                // Failure.
                SystemError = e.Message;
                return(false);
            }

            LSystems.IRewriteRules rules = definition as LSystems.IRewriteRules;
            if (rules != null)
            {
                system.String = rules.Axiom;
            }

            steps.Clear();
            this.steps.Insert(0, new Step()
            {
                System = system, Name = "Axiom"
            });

            return(true);
        }
Example #4
0
        public void SetSystem(LSystems.System system)
        {
            this.system = system;

            // Rebuild system.
            Clear(ref this.vertexesPtr);
            Clear(ref this.colorsPtr);
            Clear(ref this.normalsPtr);
            Clear(ref this.triVertexesPtr);
            Clear(ref this.triColorsPtr);
            Clear(ref this.triNormalsPtr);

            this.numQuadVertexes = 0;
            this.numTriVertexes  = 0;

            this.vertexes.Clear();
            this.normals.Clear();
            this.colors.Clear();
            this.triVertexes.Clear();
            this.triNormals.Clear();
            this.triColors.Clear();

            this.helperLines.Clear();

            ResetState();

            this.Push();

            if (this.system != null)
            {
                //Debug.WriteLine("---------------------");
                //foreach (object obj in (IEnumerable)this.system.String)
                //{
                //    Debug.WriteLine(string.Format("{0}", obj.GetType()));
                //}

                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition) this.system.Definition).Turtle = this;
                }

                var interpreter = new LSystems.Turtle.Interpreter();
                interpreter.RegisterExternalFunctions(this.system.Definition);
                interpreter.Interpret(this, this.system.String as IEnumerable);
                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition) this.system.Definition).Turtle = null;
                }
            }

            this.Pop();

            // Generate normals.
            for (int i = 0; i < vertexes.Count; i += 12)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(vertexes[i], vertexes[i + 1], vertexes[i + 2]),
                    new OpenTK.Vector3(vertexes[i + 3], vertexes[i + 4], vertexes[i + 5]),
                    new OpenTK.Vector3(vertexes[i + 6], vertexes[i + 7], vertexes[i + 8]));

                for (int j = 0; j < 4; ++j)
                {
                    Add(normals, normal);
                }
            }

            for (int i = 0; i < triVertexes.Count; i += 9)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(triVertexes[i], triVertexes[i + 1], triVertexes[i + 2]),
                    new OpenTK.Vector3(triVertexes[i + 3], triVertexes[i + 4], triVertexes[i + 5]),
                    new OpenTK.Vector3(triVertexes[i + 6], triVertexes[i + 7], triVertexes[i + 8]));

                for (int j = 0; j < 3; ++j)
                {
                    Add(triNormals, normal);
                }
            }

            // Copy arrays in non-managed memory.
            Create(this.vertexes, ref this.vertexesPtr);
            Create(this.colors, ref this.colorsPtr);
            Create(this.normals, ref this.normalsPtr);

            this.numQuadVertexes = this.vertexes.Count / 3;

            Create(this.triVertexes, ref this.triVertexesPtr);
            Create(this.triColors, ref this.triColorsPtr);
            Create(this.triNormals, ref this.triNormalsPtr);

            this.numTriVertexes = this.triVertexes.Count / 3;

            this.toolStripLabelStatistics.Text =
                string.Format("{0} quads, {1} triangles.", this.numQuadVertexes / 4, this.numTriVertexes / 3);

            // Force redraw.
            glControl1.Invalidate();
        }
        public void SetSystem(LSystems.System system)
        {
            this.system = system;

            // Rebuild system.
            Clear(ref this.vertexesPtr);
            Clear(ref this.colorsPtr);
            Clear(ref this.normalsPtr);
            Clear(ref this.triVertexesPtr);
            Clear(ref this.triColorsPtr);
            Clear(ref this.triNormalsPtr);

            this.numQuadVertexes = 0;
            this.numTriVertexes = 0;

            this.vertexes.Clear();
            this.normals.Clear();
            this.colors.Clear();
            this.triVertexes.Clear();
            this.triNormals.Clear();
            this.triColors.Clear();

            this.helperLines.Clear();

            ResetState();

            this.Push();

            if (this.system != null)
            {
                //Debug.WriteLine("---------------------");
                //foreach (object obj in (IEnumerable)this.system.String)
                //{
                //    Debug.WriteLine(string.Format("{0}", obj.GetType()));
                //}

                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition)this.system.Definition).Turtle = this;
                }

                var interpreter = new LSystems.Turtle.Interpreter();
                interpreter.RegisterExternalFunctions(this.system.Definition);
                interpreter.Interpret(this, this.system.String as IEnumerable);
                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition)this.system.Definition).Turtle = null;
                }
            }

            this.Pop();

            // Generate normals.
            for (int i = 0; i < vertexes.Count; i += 12)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(vertexes[i], vertexes[i + 1], vertexes[i + 2]),
                    new OpenTK.Vector3(vertexes[i + 3], vertexes[i + 4], vertexes[i + 5]),
                    new OpenTK.Vector3(vertexes[i + 6], vertexes[i + 7], vertexes[i + 8]));

                for (int j = 0; j < 4; ++j) { Add(normals, normal); }
            }

            for (int i = 0; i < triVertexes.Count; i += 9)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(triVertexes[i], triVertexes[i + 1], triVertexes[i + 2]),
                    new OpenTK.Vector3(triVertexes[i + 3], triVertexes[i + 4], triVertexes[i + 5]),
                    new OpenTK.Vector3(triVertexes[i + 6], triVertexes[i + 7], triVertexes[i + 8]));

                for (int j = 0; j < 3; ++j) { Add(triNormals, normal); }
            }

            // Copy arrays in non-managed memory.
            Create(this.vertexes, ref this.vertexesPtr);
            Create(this.colors, ref this.colorsPtr);
            Create(this.normals, ref this.normalsPtr);

            this.numQuadVertexes = this.vertexes.Count / 3;

            Create(this.triVertexes, ref this.triVertexesPtr);
            Create(this.triColors, ref this.triColorsPtr);
            Create(this.triNormals, ref this.triNormalsPtr);

            this.numTriVertexes = this.triVertexes.Count / 3;

            this.toolStripLabelStatistics.Text =
                string.Format("{0} quads, {1} triangles.", this.numQuadVertexes / 4, this.numTriVertexes / 3);

            // Force redraw.
            glControl1.Invalidate();
        }
        private bool InitSystem()
        {
            if (system != null)
            {
                return true;
            }

            SystemError = null;

            if (!BuildDefinition())
            {
                return false;
            }

            try
            {
                system = LSystems.SystemBuilder.BuildSystem(definition);
            }
            catch (InvalidOperationException e)
            {
                // Failure.
                SystemError = e.Message;
                return false;
            }

            LSystems.IRewriteRules rules = definition as LSystems.IRewriteRules;
            if (rules != null)
            {
                system.String = rules.Axiom;
            }

            steps.Clear();
            this.steps.Insert(0, new Step() { System = system, Name = "Axiom" });

            return true;
        }
        private void BuildNextStep(bool setCurrentItem)
        {
            if (!InitSystem())
            {
                return;
            }

            // Iterate.
            LSystems.IRewriteRules rules = definition as LSystems.IRewriteRules;
            if (rules != null)
            {
                LSystems.System nextStep = system.Clone();

                switch (rules.RewriteDirection)
                {
                    case LSystems.RewriteDirection.LeftToRight:
                        nextStep.RewriteLeftToRight();
                        break;

                    case LSystems.RewriteDirection.RightToLeft:
                        nextStep.RewriteRightToLeft();
                        break;

                    default:
                        throw new InvalidOperationException("Unknown RewriteDirection");
                }

                nextStep.Decomposite();

                this.steps.Insert(0, new Step() { System = nextStep, Name = string.Format("Step {0}", steps.Count) });

                system = nextStep;
            }

            if (setCurrentItem)
            {
                if (this.steps.Count > 0)
                {
                    CollectionViewSource.GetDefaultView(this.steps).MoveCurrentTo(this.steps[0]);
                }

                NotifyPropertyChanged("System");
            }
        }
        public void Rebuild()
        {
            system = null;
            definition = null;
            steps.Clear();

            Build();
        }