Example #1
0
 protected BasicPrimitiveTessellator()
 {
     Positions          = new Point3DCollection();
     Normals            = new Vector3DCollection();
     Indices            = new Int32Collection();
     TextureCoordinates = new Point2DCollection();
 }
Example #2
0
 public static void Create(this MeshGeometry3D mesh, out double[] areas, out double[][] centers)
 {
     Point3DCollection pos = mesh.Positions;
     Vector3DCollection vcol = new Vector3DCollection();
     int n = pos.Count / 3;
     areas = new double[n];
     centers = new double[n][];
     for (int i = 0; i < n; i++)
     {
         double[] center = new double[]{0, 0, 0};
         int k = 3 * i;
         Point3D[] p = new Point3D[]{pos[k], pos[k + 1], pos[k + 2]};
         for (int j = 0; j < 3; j++)
         {
             center[0] += p[j].X;
             center[1] += p[j].Y;
             center[2] += p[j].Z;
         }
         for (int j = 0; j < 3; j++)
         {
             center[j] /= 3;
         }
         centers[i] = center;
         System.Windows.Media.Media3D.Vector3D ved;
         areas[i] = p.GetArea(out ved);
         vcol.Add(ved);
     }
     mesh.Normals = vcol;
 }
        public PolyNormalBasedImageSelector(Point3DCollection meshPositions, Vector3DCollection meshNormals, ImageSpecifics[] imageParameters, CameraRatio cameraRatio)
        {
            _meshNormals     = meshNormals;
            _imageParameters = imageParameters;
            _cameraRatio     = cameraRatio;
            _meshPositions   = meshPositions;

            if (_meshNormals == null || _meshNormals.Count != meshPositions.Count)
            {
                throw new Exception("GetTextureCoordinates cannot be called without setting the normals first");
            }

            //calculate and keep ready all the required directions
            _imageIndexAndDirections = new List <ImageIndexAndDirection>();
            for (var ctr = 0; ctr < imageParameters.Count(); ctr++)
            {
                var imageIndexAndDirection = new ImageIndexAndDirection {
                    Index = ctr, Direction = imageParameters[ctr].GetCameraDirection()
                };
                _imageIndexAndDirections.Add(imageIndexAndDirection);
            }

            //Have the transformation objects ready that will be used to transform the selected coordinates to a new coordinate system
            //in which the z direction is the direction towards the camera
            _transformers = imageParameters.Select(x => new SpaceTransformationHandler(x.LookingAt, x.CameraLocation)).ToArray();
        }
Example #4
0
        public static double determinant(Vector3DCollection list)
        {
            Matrix3D m = new Matrix3D();

            for (int i = 0; i < list.Count; i++)
            {
                Vector3D v = list[i];
                if (i == 0)
                {
                    m.M11 = v.X;
                    m.M12 = v.Y;
                    m.M13 = v.Z;
                }
                if (i == 1)
                {
                    m.M21 = v.X;
                    m.M22 = v.Y;
                    m.M23 = v.Z;
                }
                if (i == 2)
                {
                    m.M31 = v.X;
                    m.M32 = v.Y;
                    m.M33 = v.Z;
                }
            }
            return(m.Determinant);
        }
Example #5
0
        /// <summary>Initializes the boid.</summary>
        /// <param name="colors">The boids color's, Item1 for Material and Item2 for BackMaterial.</param>
        public Boid(Tuple<Color,Color> colors)
        {
            if (colors == null) throw new ArgumentNullException("colors");

            // Store the colors
            _colors = colors;
            _materialBrush = new SolidColorBrush(colors.Item1);
            _backmaterialBrush = new SolidColorBrush(colors.Item2);

            // Set up the boid's model
            base.Content = new GeometryModel3D()
            {
                Material = new DiffuseMaterial(_materialBrush),
                BackMaterial = new DiffuseMaterial(_backmaterialBrush),
                Geometry = new MeshGeometry3D()
                {
                    // Two perpendicular triangles pointing up
                    Positions = Point3DCollection.Parse("0 1 0  1 -1 0  -1 -1 0  0 1 0  0 -1 1  0 -1 -1"), 
                    Normals = Vector3DCollection.Parse("0 0 -1  1 0 0"),
                    TriangleIndices = Int32Collection.Parse("0 1 2  3 4 5")
                }
            };

            // Initialize its rotation and translation
            _rotation = new AxisAngleRotation3D(UNIT_Y, 0);
            _translation = new TranslateTransform3D(new Vector3D());

            // Add all of the necessary transforms
            var t = new Transform3DGroup();
            t.Children.Add(new ScaleTransform3D(MODEL_SCALE, MODEL_SCALE, MODEL_SCALE));
            t.Children.Add(new RotateTransform3D(_rotation));
            t.Children.Add(_translation);
            base.Transform = t;
        }
Example #6
0
        private Model3DGroup CopyToWPFThread(Model3DGroup src_model)
        {
            /** disassemble */
            var geometrymodel = src_model.Children[0] as GeometryModel3D;
            var geometry      = geometrymodel.Geometry as MeshGeometry3D;

            string str_vertices = geometry.Positions.ToString();
            string str_normals  = geometry.Normals.ToString();
            string str_indices  = geometry.TriangleIndices.ToString();
            string str_textures = geometry.TextureCoordinates.ToString();

            Model3DGroup result_model = null;

            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
            {
                result_model = new Model3DGroup();
                result_model.Children.Add(new GeometryModel3D()
                {
                    Geometry = new MeshGeometry3D()
                    {
                        /** re-assemble */
                        Positions          = Point3DCollection.Parse(str_vertices),
                        Normals            = Vector3DCollection.Parse(str_normals),
                        TriangleIndices    = Int32Collection.Parse(str_indices),
                        TextureCoordinates = PointCollection.Parse(str_textures)
                    },
                    Material = new DiffuseMaterial(new ImageBrush(new BitmapImage(new Uri(@"image/marble.jpg", UriKind.Relative))))
                });
            }));

            return(result_model);
        }
Example #7
0
        public static Vector3DCollection CalculateNormals(IList <Point3D> positions, IList <int> triangleIndices)
        {
            var normals = new Vector3DCollection(positions.Count);

            for (int i = 0; i < positions.Count; i++)
            {
                normals.Add(new Vector3D());
            }

            for (int i = 0; i < triangleIndices.Count; i += 3)
            {
                int      index0 = triangleIndices[i];
                int      index1 = triangleIndices[i + 1];
                int      index2 = triangleIndices[i + 2];
                var      p0     = positions[index0];
                var      p1     = positions[index1];
                var      p2     = positions[index2];
                Vector3D u      = p1 - p0;
                Vector3D v      = p2 - p0;
                Vector3D w      = Vector3D.CrossProduct(u, v);
                w.Normalize();
                normals[index0] += w;
                normals[index1] += w;
                normals[index2] += w;
            }

            for (int i = 0; i < normals.Count; i++)
            {
                var w = normals[i];
                w.Normalize();
                normals[i] = w;
            }

            return(normals);
        }
Example #8
0
        public MeshGeometry3D createAlternativeMesh()
        {
            //this.mesh is the relevant mesh that we are setting.

            //Define list of triangle indices over the whole model.
            var triangleIndices = new List <int>();

            //Define Vector3D normals.
            Vector3DCollection myNormalCollection = new Vector3DCollection();

            myNormalCollection.Add(new Vector3D(0, 0, 1));
            myNormalCollection.Add(new Vector3D(0, 1, 1));
            myNormalCollection.Add(new Vector3D(1, 1, 1));

            //iterate over each 3 points and create a triangle with the relevant indices.
            for (int i = 0; i < this.cloudList[0].Count - 3; i += 3)
            {
                this.mesh.Positions.Add(this.cloudList[0][i]);
                this.mesh.Positions.Add(this.cloudList[0][i + 1]);
                this.mesh.Positions.Add(this.cloudList[0][i + 2]);

                //set triangle normals on the mesh.
                triangleIndices.Add(0);
                triangleIndices.Add(1);
                triangleIndices.Add(2);
            }

            return(new MeshGeometry3D()
            {
                Positions = new Point3DCollection(this.cloudList[0]),
                //TextureCoordinates = new System.Windows.Media.PointCollection(this.textureCoordinates),
                TriangleIndices = new Int32Collection(triangleIndices),
                Normals = myNormalCollection
            });
        }
Example #9
0
        [FriendAccessAllowed] // Built into Core, also used by Framework.
#endif
        internal static bool SerializeVector3D(BinaryWriter writer, string stringValues)
        {
#if PBTCOMPILER
            List <ThreeDoublesMarkup> points = ParseThreeDoublesCollection(stringValues, TypeConverterHelper.InvariantEnglishUS);
            ThreeDoublesMarkup        curPoint;
#else
            Vector3DCollection points = Vector3DCollection.Parse(stringValues);
            Vector3D           curPoint;
#endif

            // Write out the size.
            writer.Write(( uint )points.Count);

            // Write out the doubles.
            for (int i = 0; i < points.Count; i++)
            {
                curPoint = points[i];

                WriteDouble(writer, curPoint.X);
                WriteDouble(writer, curPoint.Y);
                WriteDouble(writer, curPoint.Z);
            }

            return(true);
        }
Example #10
0
        public static MeshGeometry3D CreateCylinder(double radius, double height, int steps)
        {
            var geometry  = new MeshGeometry3D();
            var positions = new Point3DCollection();
            var indicies  = new Int32Collection();
            var normals   = new Vector3DCollection();

            // center
            positions.Add(new Point3D(0, height, 0));
            normals.Add(new Vector3D(0, 1, 0));

            var delta = 360.0 / steps;

            for (int i = 0; i < steps; ++i)
            {
                var rad = Helper.DegreesToRadians * (i * delta);
                var v1  = new Point3D(Math.Sin(rad) * radius, 0, Math.Cos(rad) * radius);
                var v2  = new Point3D(Math.Sin(rad) * radius, height, Math.Cos(rad) * radius);
                var n   = new Vector3D(Math.Sin(rad), 0, Math.Cos(rad));
                positions.Add(v1);
                normals.Add(n);
                positions.Add(v2);
                normals.Add(n);
            }

            // top disc

            for (int i = 0; i < steps; ++i)
            {
                indicies.Add(0);
                int j = i;
                int k = j + 1;
                j %= steps;
                k %= steps;
                indicies.Add((j + 1) * 2);
                indicies.Add((k + 1) * 2);
            }

            // border quads

            for (int i = 0; i < steps; ++i)
            {
                int j = i;
                int k = j + 1;
                j %= steps;
                k %= steps;
                indicies.Add(((j + 1) * 2) - 1);
                indicies.Add(((k + 1) * 2) + 0);
                indicies.Add(((j + 1) * 2) + 0);

                indicies.Add(((k + 1) * 2) - 1);
                indicies.Add(((k + 1) * 2) + 0);
                indicies.Add(((j + 1) * 2) - 1);
            }

            geometry.TriangleIndices = indicies;
            geometry.Positions       = positions;
            geometry.Normals         = normals;
            return(geometry);
        }
 protected BasicPrimitiveTessellator()
 {
     Positions = new Point3DCollection();
     Normals = new Vector3DCollection();
     Indices = new Int32Collection();
     TextureCoordinates = new Point2DCollection();
 }
Example #12
0
        private void SendGraphicsToView(Point3DCollection points, Point3DCollection pointsSelected,
                                        Point3DCollection lines, Point3DCollection linesSelected, Point3DCollection redLines, Point3DCollection greenLines,
                                        Point3DCollection blueLines, Point3DCollection verts, Vector3DCollection norms, Int32Collection tris,
                                        Point3DCollection vertsSel, Vector3DCollection normsSel, Int32Collection trisSel, MeshGeometry3D mesh,
                                        MeshGeometry3D meshSel, List <BillboardTextItem> text)
        {
            Points                  = points;
            PointsSelected          = pointsSelected;
            Lines                   = lines;
            LinesSelected           = linesSelected;
            XAxes                   = redLines;
            YAxes                   = greenLines;
            ZAxes                   = blueLines;
            mesh.Positions          = verts;
            mesh.Normals            = norms;
            mesh.TriangleIndices    = tris;
            meshSel.Positions       = vertsSel;
            meshSel.Normals         = normsSel;
            meshSel.TriangleIndices = trisSel;
            Mesh         = mesh;
            MeshSelected = meshSel;
            Text         = text;

            // Send property changed notifications for everything
            NotifyPropertyChanged(string.Empty);
        }
Example #13
0
        public static MeshGeometry3D CreateMesh(int xVertices, int yVertices)
        {
            Vector3DCollection normals    = new Vector3DCollection();
            PointCollection    textCoords = new PointCollection();

            for (int y = 0; y < yVertices; y++)
            {
                for (int x = 0; x < xVertices; x++)
                {
                    // Normals
                    Vector3D n1 = new Vector3D(0, 0, 1);
                    normals.Add(n1);

                    // Texture Coordinates
                    textCoords.Add(GetTextureCoordinate(xVertices, yVertices, y, x));
                }
            }
            Int32Collection indices = GetTriangleIndices(xVertices, yVertices);

            MeshGeometry3D mesh = new MeshGeometry3D();

            mesh.Normals            = normals;
            mesh.TriangleIndices    = indices;
            mesh.TextureCoordinates = textCoords;
            return(mesh);
        }
Example #14
0
        protected override void Triangulate(DependencyPropertyChangedEventArgs args,
                                            Point3DCollection vertices,
                                            Vector3DCollection normals,
                                            Int32Collection indices,
                                            PointCollection textures)
        {
            vertices.Clear();
            normals.Clear();
            indices.Clear();
            textures.Clear();

            MeshGeometry3D mesh = MeshGenerator.Geometry;

            foreach (Point3D vertex in mesh.Positions)
            {
                vertices.Add(vertex);
            }

            foreach (Vector3D normal in mesh.Normals)
            {
                normals.Add(normal);
            }

            foreach (int index in mesh.TriangleIndices)
            {
                indices.Add(index);
            }

            foreach (Point texture in mesh.TextureCoordinates)
            {
                textures.Add(texture);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        protected virtual void PropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            // Get the MeshGeometry3D for local convenience.
            MeshGeometry3D mesh = Geometry;

            // Obtain the four collection of the MeshGeometry3D.
            Point3DCollection  vertices = mesh.Positions;
            Vector3DCollection normals  = mesh.Normals;
            Int32Collection    indices  = mesh.TriangleIndices;
            PointCollection    textures = mesh.TextureCoordinates;

            // Set the MeshGeometry3D collections to null while updating.
            mesh.Positions          = null;
            mesh.Normals            = null;
            mesh.TriangleIndices    = null;
            mesh.TextureCoordinates = null;

            // Call the abstract method to fill the collections.
            Triangulate(args, vertices, normals, indices, textures);

            // Set the updated collections to the MeshGeometry3D.
            mesh.TextureCoordinates = textures;
            mesh.TriangleIndices    = indices;
            mesh.Normals            = normals;
            mesh.Positions          = vertices;
        }
        private void SetNormals()
        {
            var normals = new Vector3DCollection()
            {
                new Vector3D(0, 0, -1),
                new Vector3D(0, 0, 1),
                new Vector3D(1, 0, 0),
                new Vector3D(-1, 0, 0),
                new Vector3D(0, 1, 0),
                new Vector3D(0, -1, 0)
            };
            var n2 = new Vector3DCollection()
            {
                normals[0] + normals[3] + normals[5],
                normals[0] + normals[2] + normals[5],
                normals[0] + normals[3] + normals[4],
                normals[0] + normals[2] + normals[4],
                normals[1] + normals[5] + normals[3],
                normals[1] + normals[2] + normals[5],
                normals[1] + normals[3] + normals[4],
                normals[2] + normals[4] + normals[1],
            };

            n2.ToList().ForEach(x => x.Normalize());
            D.Normals = n2;
        }
        /// <summary>
        /// Converts <paramref name="skn"/> to a list of <see cref="MeshGeometry3D"/>
        /// </summary>
        /// <param name="skn">The <see cref="SKNFile"/> which should get converted to a <c>Tuple{string, MeshGeometry3D}(submeshName, submeshData)</c></param>
        /// <returns>A collection of converted <see cref="SKNSubmesh"/></returns>
        /// <remarks>Normals do not get converted</remarks>
        public static IEnumerable <Tuple <string, MeshGeometry3D> > ConvertSKN(SKNFile skn)
        {
            foreach (SKNSubmesh submesh in skn.Submeshes)
            {
                MeshGeometry3D mesh = new MeshGeometry3D();

                Int32Collection    indices  = new Int32Collection(submesh.Indices.Select(x => (int)x));
                Point3DCollection  vertices = new Point3DCollection();
                Vector3DCollection normals  = new Vector3DCollection();
                PointCollection    uvs      = new PointCollection();
                for (int i = 0; i < submesh.Vertices.Count; i++)
                {
                    SKNVertex vertex = submesh.Vertices[i];
                    vertices.Add(new Point3D(vertex.Position.X, vertex.Position.Y, vertex.Position.Z));
                    normals.Add(new Vector3D(vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z));
                    uvs.Add(new Point(vertex.UV.X, vertex.UV.Y));
                }

                mesh.TextureCoordinates = uvs;
                mesh.Positions          = vertices;
                mesh.Normals            = normals;
                mesh.TriangleIndices    = indices;

                yield return(new Tuple <string, MeshGeometry3D>(submesh.Name, mesh));
            }
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            _stream.Position = 0;
            switch (_converterId)
            {
            case Baml2006SchemaContext.KnownTypes.XamlBrushSerializer:
                return(System.Windows.Media.SolidColorBrush.DeserializeFrom(_reader,
                                                                            new DeferredBinaryDeserializerExtensionContext(serviceProvider, _freezer, _canFreeze)));

            case Baml2006SchemaContext.KnownTypes.XamlPathDataSerializer:
                return(Parsers.DeserializeStreamGeometry(_reader));

            case Baml2006SchemaContext.KnownTypes.XamlPoint3DCollectionSerializer:
                return(Point3DCollection.DeserializeFrom(_reader));

            case Baml2006SchemaContext.KnownTypes.XamlPointCollectionSerializer:
                return(PointCollection.DeserializeFrom(_reader));

            case Baml2006SchemaContext.KnownTypes.XamlVector3DCollectionSerializer:
                return(Vector3DCollection.DeserializeFrom(_reader));

            default:
                throw new NotImplementedException();
            }
        }
Example #19
0
 public Mesh()
 {
     Positions = new Point3DCollection();
     Normals = new Vector3DCollection();
     TextureCoordinates = new Point2DCollection();
     Indices = new IndexCollection();
     Material = new Material();
 }
Example #20
0
 public Mesh()
 {
     Positions          = new Point3DCollection();
     Normals            = new Vector3DCollection();
     TextureCoordinates = new Point3DCollection();
     Indices            = new Int32Collection();
     Transform          = new MatrixTransform(Matrix3D.Identity);
 }
Example #21
0
 public Mesh()
 {
     Positions          = new Point3DCollection();
     Normals            = new Vector3DCollection();
     TextureCoordinates = new Point2DCollection();
     Indices            = new IndexCollection();
     Material           = new Material();
 }
 public static void AssertContains(this Vector3DCollection collection, params double[][] points)
 {
     Assert.AreEqual(points.Length, collection.Count, "Expected to find {0} points in collection", points.Length);
     foreach (var point in points)
     {
         Assert.IsTrue(collection.Contains(new Vector3D(point[0], point[1], point[2])), "Expected collection to contain point [{0},{1},{2}]", point[0], point[1], point[2]);
     }
 }
Example #23
0
 public Mesh()
 {
     Positions = new Point3DCollection();
     Normals = new Vector3DCollection();
     TextureCoordinates = new Point3DCollection();
     Indices = new Int32Collection();
     Transform = new MatrixTransform(Matrix3D.Identity);
 }
        static Visual3D GetMesh(XmlElement element)
        {
            ModelVisual3D  mod  = new ModelVisual3D();
            MeshGeometry3D mesh = new MeshGeometry3D();
            Material       mat  = null;
            //mesh.Positions = e.GetChild("vertices").ToPoint3DCollection();
            XmlElement poly = element.GetChild("polylist");

            if (poly != null)
            {
                List <int[]> ind = poly.ToInt3Array();
                mat = Find <Material>(poly.GetAttribute("material").ToIdName());
                if (mat == null)
                {
                    return(null);
                }
                XmlNodeList nl = poly.GetElementsByTagName("input");
                Dictionary <string, object> d = poly.FindInputs();
                List <Point3D>     vertices   = (d["VERTEX"] as double[]).ToPoint3DList();
                List <Vector3D>    norm       = (d["NORMAL"] as double[]).ToVector3DList();
                List <Point>       textures   = (d["TEXCOORD"] as double[]).ToPointList();
                Point3DCollection  vert       = new Point3DCollection();
                PointCollection    textc      = new PointCollection();
                Int32Collection    index      = new Int32Collection();
                Vector3DCollection norms      = new Vector3DCollection();

                /* foreach (Point3D p in vertices)
                 * {
                 *   vert.Add(p);
                 * }*/
                Vector3D[] nt = new Vector3D[ind.Count];
                Point[]    pt = new Point[ind.Count];

                /* foreach (int[] i in ind)
                 * {
                 *    index.Add(i[0]);
                 *    norms.Add(norm[i[1]]);
                 *    textc.Add(textures[i[2]]);
                 * }
                 */
                for (int i = 0; i < ind.Count; i++)
                {
                    norms.Add(norm[i]);
                    textc.Add(textures[i]);
                    vert.Add(vertices[ind[i][0]]);
                }
                mesh.Positions          = vert;
                mesh.Normals            = norms;
                mesh.TextureCoordinates = textc;
                mesh.TriangleIndices    = index;
            }
            GeometryModel3D geom = new GeometryModel3D();

            geom.Geometry = mesh;
            geom.Material = mat;
            mod.Content   = geom;
            return(mod);
        }
        private static GeometryModel3D GetCmpModel(MeshGroup meshGroup, int meshIndex)
        {
            VMeshData.MeshHeader mesh      = meshGroup.Mesh.Meshes[meshIndex];
            Point3DCollection    positions = new Point3DCollection();
            Int32Collection      indices   = new Int32Collection();
            Vector3DCollection   normals   = new Vector3DCollection();

            // PointCollection texture = new PointCollection();
            int vertexCount = meshGroup.MeshReference.VertexStart + mesh.EndVertex + 1;

            if (meshGroup.Mesh.Vertices.Length < vertexCount)
            {
                return(null);
            }

            for (int i = meshGroup.MeshReference.VertexStart + mesh.StartVertex; i < vertexCount; ++i)
            {
                positions.Add(meshGroup.Mesh.Vertices[i].Position);
                normals.Add(meshGroup.Mesh.Vertices[i].Normal);

                // texture.Add(new Point
                // {
                // X = vMesh.Vertices[i].S,
                // Y = vMesh.Vertices[i].T
                // });
            }

            int triangleCount = (mesh.TriangleStart + mesh.NumRefVertices) / 3;

            if (meshGroup.Mesh.Triangles.Length < triangleCount)
            {
                return(null);
            }

            for (int i = mesh.TriangleStart / 3; i < triangleCount; ++i)
            {
                indices.Add(meshGroup.Mesh.Triangles[i].Vertex1);
                indices.Add(meshGroup.Mesh.Triangles[i].Vertex2);
                indices.Add(meshGroup.Mesh.Triangles[i].Vertex3);
            }

            GeometryModel3D gm = new GeometryModel3D
            {
                Geometry = new MeshGeometry3D
                {
                    Positions       = positions,
                    TriangleIndices = indices,
                    Normals         = normals,

                    // TextureCoordinates = texture
                },
                Material  = SharedMaterials.CmpModel,
                Transform = new MatrixTransform3D(meshGroup.Transform * ConversionMatrix)
            };

            gm.Freeze();
            return(gm);
        }
Example #26
0
        public ObjReader()
        {
            Points    = new Point3DCollection();
            TexCoords = new PointCollection();
            Normals   = new Vector3DCollection();

            Groups    = new Collection <Group>();
            Materials = new Dictionary <string, MaterialDefinition>();
        }
Example #27
0
        /// <summary>
        /// Grows a collection by the required size
        /// </summary>
        /// <param name="pointColl"></param>
        /// <param name="growSize"></param>
        public static Vector3DCollection GrowBy(this Vector3DCollection vecColl, int growSize)
        {
            Vector3DCollection grown = new Vector3DCollection(vecColl.Count + growSize);

            foreach (var v in vecColl)
            {
                grown.Add(v);
            }
            return(grown);
        }
Example #28
0
        public static Vector3DCollection GetVectorCollection(List <XbimVector3D> points)
        {
            var ret = new Vector3DCollection(points.Count);

            foreach (var enumPoint in points)
            {
                ret.Add(new Vector3D(enumPoint.X, enumPoint.Y, enumPoint.Z));
            }
            return(ret);
        }
        public WpfVector3DCollection(IEnumerable <XbimVector3D> xbimVectors)
        {
            var realVectors = xbimVectors as IList <XbimVector3D>
                              ?? xbimVectors.ToList();

            _wpfVectors = new Vector3DCollection(realVectors.Count);
            foreach (var vec in realVectors)
            {
                _wpfVectors.Add(new Vector3D(vec.X, vec.Y, vec.Z));
            }
        }
        /// <summary>
        /// Creates a new mesh where no vertices are shared.
        /// </summary>
        /// <param name="input">
        /// The input mesh.
        /// </param>
        /// <returns>
        /// A new mesh.
        /// </returns>
        public static MeshGeometry3D NoSharedVertices(MeshGeometry3D input)
        {
            var p  = new Point3DCollection();
            var ti = new Int32Collection();
            Vector3DCollection n = null;

            if (input.Normals != null)
            {
                n = new Vector3DCollection();
            }

            PointCollection tc = null;

            if (input.TextureCoordinates != null)
            {
                tc = new PointCollection();
            }

            for (int i = 0; i < input.TriangleIndices.Count; i += 3)
            {
                int i0     = i;
                int i1     = i + 1;
                int i2     = i + 2;
                int index0 = input.TriangleIndices[i0];
                int index1 = input.TriangleIndices[i1];
                int index2 = input.TriangleIndices[i2];
                var p0     = input.Positions[index0];
                var p1     = input.Positions[index1];
                var p2     = input.Positions[index2];
                p.Add(p0);
                p.Add(p1);
                p.Add(p2);
                ti.Add(i0);
                ti.Add(i1);
                ti.Add(i2);
                if (n != null)
                {
                    n.Add(input.Normals[index0]);
                    n.Add(input.Normals[index1]);
                    n.Add(input.Normals[index2]);
                }

                if (tc != null)
                {
                    tc.Add(input.TextureCoordinates[index0]);
                    tc.Add(input.TextureCoordinates[index1]);
                    tc.Add(input.TextureCoordinates[index2]);
                }
            }

            return(new MeshGeometry3D {
                Positions = p, TriangleIndices = ti, Normals = n, TextureCoordinates = tc
            });
        }
Example #31
0
 /// <summary>
 /// Converts a string into a Vector3DCollection.
 /// </summary>
 public override object ConvertFromString(string value, IValueSerializerContext context)
 {
     if (value != null)
     {
         return(Vector3DCollection.Parse(value));
     }
     else
     {
         return(base.ConvertFromString(value, context));
     }
 }
        private void DrawFacet(NodeModel node, object obj, string tag, RenderDescription rd,
                               Octree.OctreeSearch.Octree octree)
        {
            var facet = obj as Facet;

            if (facet == null)
            {
                return;
            }

            var builder = new MeshBuilder();
            var points  = new Point3DCollection();
            var tex     = new PointCollection();
            var norms   = new Vector3DCollection();
            var tris    = new List <int>();

            var a = facet.Points[0];
            var b = facet.Points[1];
            var c = facet.Points[2];

            var side1 = (b - a).Normalize();
            var side2 = (c - a).Normalize();
            var norm  = side1.CrossProduct(side2);

            int count = 0;

            foreach (var pt in facet.Points)
            {
                points.Add(new Point3D(pt.X, pt.Y, pt.Z));
                tex.Add(new System.Windows.Point(0, 0));
                tris.Add(count);
                norms.Add(new Vector3D(norm.X, norm.Y, norm.Z));
                count++;
            }

            builder.Append(points, tris, norms, tex);

            if (node.IsSelected)
            {
                rd.SelectedMeshes.Add(builder.ToMesh(true));
            }
            else
            {
                rd.Meshes.Add(builder.ToMesh(true));
            }

            if (node.DisplayLabels)
            {
                var cp = (a + b + c) / 3;
                rd.Text.Add(new BillboardTextItem {
                    Text = tag, Position = new Point3D(cp.X, cp.Y, cp.Z)
                });
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        /// <param name="vertices"></param>
        /// <param name="normals"></param>
        /// <param name="indices"></param>
        /// <param name="textures"></param>
        protected override void Triangulate(DependencyPropertyChangedEventArgs args,
                                            Point3DCollection vertices,
                                            Vector3DCollection normals,
                                            Int32Collection indices,
                                            PointCollection textures)
        {
            // Clear all four collections.
            vertices.Clear();
            normals.Clear();
            indices.Clear();
            textures.Clear();

            // Fill the vertices, normals, and textures collections.
            for (int stack = 0; stack <= Stacks; stack++)
            {
                double phi   = Math.PI / 2 - stack * Math.PI / Stacks;
                double y     = Radius * Math.Sin(phi);
                double scale = -Radius *Math.Cos(phi);

                for (int slice = 0; slice <= Slices; slice++)
                {
                    double theta = slice * 2 * Math.PI / Slices;
                    double x     = scale * Math.Sin(theta);
                    double z     = scale * Math.Cos(theta);

                    Vector3D normal = new Vector3D(x, y, z);
                    normals.Add(normal);
                    vertices.Add(normal.ToPoint3D());
                    textures.Add(new Point((double)slice / Slices,
                                           (double)stack / Stacks));
                }
            }

            // Fill the indices collection.
            for (int stack = 0; stack < Stacks; stack++)
            {
                for (int slice = 0; slice < Slices; slice++)
                {
                    if (stack != 0)
                    {
                        indices.Add((stack + 0) * (Slices + 1) + slice);
                        indices.Add((stack + 1) * (Slices + 1) + slice);
                        indices.Add((stack + 0) * (Slices + 1) + slice + 1);
                    }

                    if (stack != Stacks - 1)
                    {
                        indices.Add((stack + 0) * (Slices + 1) + slice + 1);
                        indices.Add((stack + 1) * (Slices + 1) + slice);
                        indices.Add((stack + 1) * (Slices + 1) + slice + 1);
                    }
                }
            }
        }