Click here to Skip to main content
15,894,646 members
Articles / Multimedia / OpenGL

Universal Framework for Science and Engineering - Part 7: Virtual Reality at Once

Rate me:
Please Sign up or sign in to vote.
4.96/5 (105 votes)
19 Nov 2010CPOL25 min read 187.5K   14.4K   212  
An article on framework applications to virtual reality
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Runtime.Serialization;

using CategoryTheory;

using DiagramUI;


using DataPerformer.Interfaces;

using FormulaEditor.Interfaces;
using FormulaEditor;

namespace DataPerformer
{
    /// <summary>
    /// Formula iterator for filter
    /// </summary>
    [Serializable()]
    public class FormulaFilterIterator : FilterIterator, ISerializable, ICategoryObject, IPostSetArrow
    {
        #region Fields

        /// <summary>
        /// Associated object
        /// </summary>
        protected object obj;

        ObjectFormulaTree tree;

        Dictionary<string, string> variables = new Dictionary<string, string>();

        Dictionary<string, object> constants = new Dictionary<string, object>();

        Dictionary<char, IMeasure> measures = new Dictionary<char,IMeasure>();

        /// <summary>
        /// Formula arguments
        /// </summary>
        private ElementaryObjectArgument arg;


        string formula = "";

        #endregion

        #region Ctor

        /// <summary>
        /// Default constructor
        /// </summary>
        public FormulaFilterIterator()
        {
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        protected FormulaFilterIterator(SerializationInfo info, StreamingContext context)
        {
            variables = info.GetValue("Variables", typeof(Dictionary<string, string>)) as Dictionary<string, string>;
            constants = info.GetValue("Constants", typeof(Dictionary<string, object>)) as Dictionary<string, object>;
            formula = info.GetValue("Formula", typeof(string)) as string;
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Variables", variables, typeof(Dictionary<string, string>));
            info.AddValue("Constants", constants, typeof(Dictionary<string, object>));
            info.AddValue("Formula", formula, typeof(string));
        }

        #endregion

        #region ICategoryObject Members

        ICategory ICategoryObject.Category
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        ICategoryArrow ICategoryObject.Id
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        #region IAssociatedObject Members

        object IAssociatedObject.Object
        {
            get
            {
                return obj;
            }
            set
            {
                obj = value;
            }
        }

        #endregion

        #region IPostSetArrow Members

        void IPostSetArrow.PostSetArrow()
        {
            Set(variables);
        }

        #endregion

        #region Overriden Membres

        /// <summary>
        /// The "allow next" sign
        /// </summary>
        protected override bool? AllowNext
        {
            get 
            {
                if (tree == null)
                {
                    return null;
                }
                foreach (char c in measures.Keys)
                {
                    arg[c] = measures[c].Parameter();
                }
                foreach (string s in constants.Keys)
                {
                    arg[s[0]] = constants[s];
                }
                return (bool?)tree.Result; 
            }
        }

        #endregion

        #region Specific Members

        /// <summary>
        /// All variables of filter
        /// </summary>
        public string AllVariables
        {
            get
            {
                string s = "";
                try
                {
                    MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formula);
                    s = ElementaryObjectDetector.GetVariables(f);
                }
                catch (Exception ex)
                {
                    ex.Log();
                }
                return s;
            }
        }

        /// <summary>
        /// Formula of iterator
        /// </summary>
        public string Formula
        {
            get
            {
                return formula;
            }
            set
            {
                MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, value);
                formula = value;
            }
        }

        /// <summary>
        /// Variables
        /// </summary>
        public string Constants
        {
            get
            {
                try
                {
                    string s = "";
                    MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formula);
                    string str = ElementaryObjectDetector.GetVariables(f);
                    foreach (char c in str)
                    {
                        if (!variables.ContainsKey("" + c) & s.IndexOf(c) < 0)
                        {
                            s += c;
                        }
                    }
                    return s;
                }
                catch (Exception ex)
                {
                    ex.Log();
                }
                return "";
            }
            set
            {
                variables.Clear();
                constants.Clear();
               // string s = "";
                MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formula);
                string str = ElementaryObjectDetector.GetVariables(f);
                double a = 0;
                foreach (char c in str)
                {
                    if (value.IndexOf(c) >= 0)
                    {
                        constants[c + ""] = a;
                    }
                    else
                    {
                        variables[c + ""] = "";
                    }
                }
            }
        }

        /// <summary>
        /// String of variables
        /// </summary>
        public string Variables
        {
            get
            {
                string s = AllVariables;
                string str = "";
                foreach (char c in s)
                {
                    if (!constants.ContainsKey(c + ""))
                    {
                        str += c;
                    }
                }
                return str;
            }
        }

        /// <summary>
        /// Dictionary of variables
        /// </summary>
        public Dictionary<string, string> VariableDictionary
        {
            get
            {
                return variables;
            }
            set
            {
                Set(value);
            }
        }

        /// <summary>
        /// Dictionary of constants
        /// </summary>
        public Dictionary<string, object> ConstDictionary
        {
            get
            {
                return constants;
            }
            set
            {
                constants = value;
            }
        }

        void Set(Dictionary<string, string> variables)
        {
            MathFormula f = MathFormula.FromString(MathSymbolFactory.Sizes, formula);
            measures.Clear();
            Hashtable table = new Hashtable();
            foreach (string key in variables.Keys)
            {
                IMeasure m = this.FindMeasure(variables[key], false);
                measures[key[0]] = m;
                table[key[0]] = m.Type;
            }
            IFormulaObjectCreator creator = VariableDetector.GetCreator(table);
            f = f.FullTransform(null);
            tree = ObjectFormulaTree.CreateTree(f, creator);
            arg = new ElementaryObjectArgument();
            arg.Add(tree);
            this.variables = variables;
        }
    
        #endregion


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions