Ejemplo n.º 1
0
 private Selection(SelectionType type, TemplateRef selectedTemplate, int?selectedEdgeId, PatchRef selectedPatch)
 {
     _selectedTemplate = selectedTemplate;
     _selectedPatch    = selectedPatch;
     _selectedEdgeId   = selectedEdgeId;
     _type             = type;
 }
Ejemplo n.º 2
0
 public void AddElement(TemplateRef tmpl, Visual3D element)
 {
     if (!elements.ContainsKey(tmpl))
     {
         elements[tmpl] = new List <Visual3D>();
     }
     elements[tmpl].Add(element);
 }
Ejemplo n.º 3
0
 public Vector3D this[TemplateRef tmpl] {
     get {
         if (translations.ContainsKey(tmpl))
         {
             return(translations[tmpl]);
         }
         return(new Vector3D(0, 0, 0));
     }
 }
Ejemplo n.º 4
0
    //edit by czq end
    public static void GenWaperAPI()
    {
        try
        {
            TemplateRef template_ref = ScriptableObject.CreateInstance <TemplateRef>();
            if (template_ref.LuaClassWrap == null)
            {
                return;
            }

            Generator.GetGenConfig(Utils.GetAllTypes());
            ExportLuaApi(Generator.LuaCallCSharp);
        }
        catch
        {
        }
    }
Ejemplo n.º 5
0
 public void Translate(TemplateRef tmpl, Vector3D by)
 {
     if (!translations.ContainsKey(tmpl))
     {
         translations[tmpl] = new Vector3D(0, 0, 0);
     }
     translations[tmpl] += by;
     foreach (var visual in elements[tmpl])
     {
         //Console.WriteLine("AAAAA: " + tmpl.Debuggable.getDebugInfo().shortDescription);
         Vector3D offset = this[tmpl];
         if (offset.Y + lowestZ[tmpl] < 0)
         {
             offset.Y = -lowestZ[tmpl];
         }
         visual.Transform = new TranslateTransform3D(offset);
     }
 }
Ejemplo n.º 6
0
 public static Selection Patch(TemplateRef template, PatchRef patchRef)
 {
     return(new Selection(SelectionType.Patch, template, null, patchRef));
 }
Ejemplo n.º 7
0
 public static Selection Edge(TemplateRef template, int edgeId)
 {
     return(new Selection(SelectionType.Edge, template, edgeId, PatchRef.Null));
 }
Ejemplo n.º 8
0
 public static Selection Template(TemplateRef template)
 {
     return(new Selection(SelectionType.Template, template, null, PatchRef.Null));
 }
Ejemplo n.º 9
0
        public void Execute(string command)
        {
            if (command.StartsWith("rotate "))
            {
                string[]   split = command.Split(' ');
                Vector3D   axis  = new Vector3D(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]));
                Quaternion q     = new Quaternion(axis, double.Parse(split[4]));
                var        tmpl  = UiStates.Selection.SelectedTemplate;
                if (!tmpl.IsNull)
                {
                    UiInstance.Rotate(tmpl, q, tmpl.Center);
                }
                else
                {
                    Console.WriteLine("No template selected.");
                }
            }
            else if (command.StartsWith("translate "))
            {
                string[] split = command.Split(' ');
                Vector3D trans = double.Parse(split[4]) * new Vector3D(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]));

                var tmpl = UiStates.Selection.SelectedTemplate;
                if (!tmpl.IsNull)
                {
                    UiInstance.Translate(tmpl, trans);
                }
                else
                {
                    Console.WriteLine("No template selected.");
                }
            }
            else if (command.StartsWith("scad example"))
            {
                UiInstance.makeExampleScadTemplate();
            }
            else if (command == "reflect on")
            {
                OverlapPreventionHack.SetAlwaysReflect(true);
            }
            else if (command == "reflect off")
            {
                OverlapPreventionHack.SetAlwaysReflect(false);
            }
            else if (command == "test label selection")
            {
                JointLayout layout = new JointLayout();
                layout.RobotShape = new List <Point> {
                    new Point(0, 10), new Point(10, 10), new Point(10, 0), new Point(0, 0)
                };
                layout.Joints = new List <Point> {
                    new Point(0, 10), new Point(10, 10), new Point(10, 0), new Point(0, 0)
                };
                List <JointLabeling> labelings = new List <JointLabeling> {
                    new JointLabeling {
                        JointLabels = new List <int> {
                            1, 3, 2, 4
                        }
                    },
                    new JointLabeling {
                        JointLabels = new List <int> {
                            1, 2, 2, 4
                        }
                    },
                    new JointLabeling {
                        JointLabels = new List <int> {
                            1, 1, 2, 4
                        }
                    },
                    new JointLabeling {
                        JointLabels = new List <int> {
                            1, 1, 3, 4
                        }
                    }
                };
                //MainWindow.configurationChooser.Choose(UiInstance, layout, labelings, 1, 40);
            }
            else if (command.StartsWith("add constraint "))
            {
                // Example:
                // add constraint 2(#0) + -3(#1) + (#3) = 0
                // Means that
                // Suppose the selected template has parameters defined as:
                //   param #0 = width, param #1 = height, param #2 = depth, param #3 = radius
                // Then this means 2 * width - 3 * height + radius = 0

                Regex  regex             = new Regex("^(.*)([=<>])(.+)$");
                string exprWithoutSpaces = command.Substring("add constraint ".Length).Replace(" ", "");
                Match  match             = regex.Match(exprWithoutSpaces);
                if (match.Success)
                {
                    string   expr      = match.Groups[1].Value;
                    string   oper      = match.Groups[2].Value;
                    string   constant  = match.Groups[3].Value;
                    string[] terms     = expr.Split('+');
                    Regex    termRegex = new Regex(@"^(.*)[(]#(\d+)[)]$");
                    try
                    {
                        Tuple <double, int>[] parsedTerms = terms.Select(term =>
                        {
                            Match termMatch = termRegex.Match(term);
                            if (termMatch.Success)
                            {
                                string coeffStr = termMatch.Groups[1].Value;
                                string indexStr = termMatch.Groups[2].Value;
                                double coeff    = coeffStr == "" ? 1 : coeffStr == "-" ? -1 : double.Parse(coeffStr);
                                int index       = int.Parse(indexStr);
                                return(Tuple.Create(coeff, index));
                            }
                            else
                            {
                                throw new ArgumentException("Invalid term: " + term);
                            }
                        }).ToArray();

                        bool   isEquality   = oper == "=";
                        double constantTerm = double.Parse(constant);
                        if (oper == ">")
                        {
                            parsedTerms  = parsedTerms.Select(t => Tuple.Create(-t.Item1, t.Item2)).ToArray();
                            constantTerm = -constantTerm;
                        }
                        TemplateRef selectedTemplate = UiStates.Selection.SelectedTemplate;
                        if (selectedTemplate.IsNull)
                        {
                            MessageBox.Show("Must select a template first.");
                            return;
                        }
                        selectedTemplate.AddConstraint(parsedTerms, constantTerm, isEquality);
                    }
                    catch (ArgumentException ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Invalid command");
                }
            }
        }
Ejemplo n.º 10
0
        public static void Draw(UIStates uiStates, Dictionary <TemplateRef, Drawing> drawings, Canvas canvas, TemplateRef selectedTemplate, Action <TemplateRef> selectionCallback)
        {
            canvas.Children.Clear();

            //finding bounding box
            double        xmin, xmax, ymin, ymax;
            List <double> Xs = new List <double>();
            List <double> Ys = new List <double>();

            foreach (var drawing in drawings.Values)
            {
                foreach (var face in drawing.faces)
                {
                    foreach (var point in face.points)
                    {
                        Xs.Add(point.X);
                        Ys.Add(point.Y);
                    }
                }
            }
            if (Xs.Count == 0 || Ys.Count == 0)
            {
                return;
            }
            xmin = Xs.Min();
            xmax = Xs.Max();
            ymin = Ys.Min();
            ymax = Ys.Max();

            double width       = canvas.ActualWidth;
            double height      = canvas.ActualHeight;
            double marginRatio = 0.1;
            double scale       = Math.Min(width * (1 - marginRatio) / (xmax - xmin), height * (1 - marginRatio) / (ymax - ymin));
            Vector pCenter     = 0.5 * new Vector(xmax + xmin, ymax + ymin);
            Vector center      = 0.5 * new Vector(width, height);
            Vector translate   = center - pCenter;
            Matrix matrix      = Matrix.Identity;

            matrix.Translate(-pCenter.X, -pCenter.Y);
            matrix.Scale(scale, scale);
            matrix.Translate(pCenter.X, pCenter.Y);

            matrix.Translate(translate.X, translate.Y);
            //matrix.Scale(scale, scale);

            // adding polygons
            Dictionary <TemplateRef, Polygon> polygons = new Dictionary <TemplateRef, Polygon>();

            int seletedIndex = 0;

            foreach (var kvp in drawings)
            {
                var template = kvp.Key;
                var drawing  = kvp.Value;
                foreach (var face in drawing.faces)
                {
                    if (template.Equals(selectedTemplate))
                    {
                        seletedIndex = polygons.Count;
                    }
                    Polygon polygon = new Polygon();
                    foreach (var point in face.points)
                    {
                        polygon.Points.Add(point * matrix);
                    }
                    polygons[template] = polygon;
                    canvas.Children.Add(polygon);
                }
            }

            Vector vec = new Vector(0, 0);

            foreach (var kvp in polygons)
            {
                TemplateRef template = kvp.Key;
                Polygon     A        = kvp.Value;

                bool intersect = false;
                foreach (var B in polygons.Values)
                {
                    if (A != B)
                    {
                        if (uiStates.ShowOverlaps && CollisionDetector.PolygonCollision(A, B, vec).Intersect == true)
                        {
                            intersect = true;
                        }
                    }
                }
                if (selectedTemplate.IsAncestorOf(template))
                {
                    A.Fill   = selectedFill;
                    A.Stroke = Brushes.Black;
                }
                else if (intersect == true)
                {
                    A.Stroke          = Brushes.Red;
                    A.StrokeThickness = 2;
                    A.Fill            = polygonFill;
                }
                else
                {
                    A.Stroke = Brushes.Black;
                    A.Fill   = Brushes.Linen;
                }

                //adding mouse click event handler
                A.MouseDown += (sender, args) =>
                {
                    selectionCallback(template);
                };

                Brush old = null;
                A.MouseEnter += (sender, args) =>
                {
                    old    = A.Fill;
                    A.Fill = hoverFill;
                };
                A.MouseLeave += (sender, args) =>
                {
                    A.Fill = old;
                };
            }
        }