public void Read(double[] values, IEnumerable <SnappedPrimitive> primitives)
        {
            var resultReader = new VectorsReader(values);

            foreach (var snappedCylinder in primitives.OfType <SnappedCylinder>())
            {
                resultReader.Read(snappedCylinder);
            }

            foreach (var snappedCone in primitives.OfType <SnappedCone>())
            {
                resultReader.Read(snappedCone);
            }

            foreach (var snappedSphere in primitives.OfType <SnappedSphere>())
            {
                resultReader.Read(snappedSphere);
            }

            foreach (var snappedSgc in primitives.OfType <SnappedStraightGenCylinder>())
            {
                resultReader.Read(snappedSgc);
            }

            foreach (var snappedCuboid in primitives.OfType <SnappedCuboid>())
            {
                resultReader.Read(snappedCuboid);
            }

            foreach (var snappedBgc in primitives.OfType <SnappedBendedGenCylinder>())
            {
                resultReader.Read(snappedBgc);
            }
        }
Esempio n. 2
0
        private void OptimizeAll()
        {
            #region Write all variables and their current values to a vector

            var variablesWriter   = new VariableVectorsWriter();
            var startVectorWriter = new VectorsWriter();

            // write cylinders
            foreach (var snappedCylinder in sessionData.SnappedPrimitives.OfType <SnappedCylinder>())
            {
                variablesWriter.Write(snappedCylinder);
                startVectorWriter.Write(snappedCylinder);
            }

            // write cones
            foreach (var snappedCone in sessionData.SnappedPrimitives.OfType <SnappedCone>())
            {
                variablesWriter.Write(snappedCone);
                startVectorWriter.Write(snappedCone);
            }

            #endregion

            // all objective functions. Will be summed eventually to form one big objective.
            var objectives = new List <Term>();

            // all equality constraints.
            var constraints = new List <Term>();

            var curvesToAnnotations = new Dictionary <FeatureCurve, ISet <Annotation> >();

            #region Get mapping of curves to annotations

            foreach (var fc in sessionData.FeatureCurves)
            {
                curvesToAnnotations[fc] = new HashSet <Annotation>();
            }

            foreach (var annotation in sessionData.Annotations)
            {
                IEnumerable <FeatureCurve> curves = null;
                annotation.MatchClass <Parallelism>(pa => curves  = pa.Elements);
                annotation.MatchClass <Coplanarity>(ca => curves  = ca.Elements);
                annotation.MatchClass <Cocentrality>(ca => curves = ca.Elements);
                Debug.Assert(curves != null);
                foreach (var fc in curves)
                {
                    curvesToAnnotations[fc].Add(annotation);
                }
            }

            #endregion

            #region get objectives and constraints for primitives

            foreach (var snappedPrimitive in sessionData.SnappedPrimitives)
            {
                var objectiveAndConstraints = snappersManager.Reconstruct(snappedPrimitive, curvesToAnnotations);
                objectives.Add(objectiveAndConstraints.Item1);
                constraints.AddRange(objectiveAndConstraints.Item2);
            }

            #endregion

            #region get constraints for annotations

            foreach (var annotation in sessionData.Annotations)
            {
                var constraintTerms = GetAnnotationConstraints(annotation);
                constraints.AddRange(constraintTerms);
            }

            #endregion

            #region perform optimization

            var finalObjective = TermUtils.SafeSum(objectives);
            var vars           = variablesWriter.ToArray();
            var vals           = startVectorWriter.ToArray();

            var optimum = Optimizer.MinAugmentedLagrangian(finalObjective, constraints.ToArray(), vars, vals, mu: 10, tolerance: 1E-5);

            #endregion

            #region read data back from the optimized vector

            var resultReader = new VectorsReader(optimum);
            foreach (var snappedCylinder in sessionData.SnappedPrimitives.OfType <SnappedCylinder>())
            {
                resultReader.Read(snappedCylinder);
            }

            foreach (var snappedCone in sessionData.SnappedPrimitives.OfType <SnappedCone>())
            {
                resultReader.Read(snappedCone);
            }

            #endregion

            #region Update feature curves

            foreach (var snappedPrimitive in sessionData.SnappedPrimitives)
            {
                snappedPrimitive.UpdateFeatureCurves();
            }

            #endregion
        }