/// <summary> /// create square model /// </summary> /// <param name="modelColor">model color</param> /// <param name="points">model points</param> /// <returns>model3d group</returns> public Model3DGroup Create(Color modelColor, Point3D[] points) { try { Point[] texturepoint0 = { new Point(0, 0), new Point(0, 1), new Point(1, 0) }; Point[] texturepoint1 = { new Point(1, 0), new Point(0, 1), new Point(1, 1) }; SolidColorBrush modelbrush = new SolidColorBrush(modelColor); modelbrush.Freeze(); Model3DGroup bottomside = new Model3DGroup(); bottomside.Children.Add(CreateGeoModel3D(points[2], points[3], points[0], modelbrush)); bottomside.Children.Add(CreateGeoModel3D(points[2], points[0], points[1], modelbrush)); bottomside.Freeze(); Model3DGroup topside = new Model3DGroup(); topside.Children.Add(CreateGeoModel3D(points[7], points[6], points[5], modelbrush)); topside.Children.Add(CreateGeoModel3D(points[7], points[5], points[4], modelbrush)); topside.Freeze(); Model3DGroup rightside = new Model3DGroup(); rightside.Children.Add(CreateGeoModel3D(points[2], points[1], points[5], modelbrush)); rightside.Children.Add(CreateGeoModel3D(points[2], points[5], points[6], modelbrush)); rightside.Freeze(); Model3DGroup leftside = new Model3DGroup(); leftside.Children.Add(CreateGeoModel3D(points[0], points[3], points[7], modelbrush)); leftside.Children.Add(CreateGeoModel3D(points[0], points[7], points[4], modelbrush)); leftside.Freeze(); Model3DGroup frontside = new Model3DGroup(); frontside.Children.Add(CreateGeoModel3D(points[3], points[2], points[6], modelbrush)); frontside.Children.Add(CreateGeoModel3D(points[3], points[6], points[7], modelbrush)); frontside.Freeze(); Model3DGroup backside = new Model3DGroup(); backside.Children.Add(CreateGeoModel3D(points[0], points[4], points[5], modelbrush)); backside.Children.Add(CreateGeoModel3D(points[1], points[0], points[5], modelbrush)); backside.Freeze(); Model3DGroup cube = new Model3DGroup(); cube.Children.Add(bottomside); cube.Children.Add(topside); cube.Children.Add(rightside); cube.Children.Add(leftside); cube.Children.Add(frontside); cube.Children.Add(backside); return cube; } catch (Exception ex) { throw ex; } }
// static constructor static Transition3D() { var defaultLight = new Model3DGroup(); var direction = new Vector3D(1, 1, 1); direction.Normalize(); byte ambient = 108; // 108 is minimum for directional to be < 256 (for direction = [1,1,1]) byte directional = (byte)Math.Min((255 - ambient) / Vector3D.DotProduct(direction, new Vector3D(0, 0, 1)), 255); defaultLight.Children.Add(new AmbientLight(Color.FromRgb(ambient, ambient, ambient))); defaultLight.Children.Add(new DirectionalLight(Color.FromRgb(directional, directional, directional), direction)); defaultLight.Freeze(); LightProperty = DependencyProperty.Register("Light", typeof(Model3D), typeof(Transition3D), new UIPropertyMetadata(defaultLight)); }
/// <summary> /// Builds the model. /// </summary> /// <returns>The model.</returns> public Model3DGroup ToModel3D() { Model3DGroup modelGroup = null; this.Dispatch( () => { modelGroup = new Model3DGroup(); int i = 0; foreach (var mesh in this.Meshes) { var gm = new GeometryModel3D { Geometry = mesh.ToMesh(), Material = this.Materials[i], BackMaterial = this.Materials[i] }; if (this.Freeze) { gm.Freeze(); } modelGroup.Children.Add(gm); i++; } if (this.Freeze) { modelGroup.Freeze(); } }); return modelGroup; }
/// <summary> /// Creates a <see cref="Model3DGroup" /> from the loaded file. /// </summary> /// <returns>A <see cref="Model3DGroup" />.</returns> public Model3DGroup CreateModel3D() { Model3DGroup modelGroup = null; this.Dispatch( () => { modelGroup = new Model3DGroup(); var g = this.CreateMeshGeometry3D(); var gm = new GeometryModel3D { Geometry = g, Material = this.DefaultMaterial }; gm.BackMaterial = gm.Material; if (this.Freeze) { gm.Freeze(); } modelGroup.Children.Add(gm); if (this.Freeze) { modelGroup.Freeze(); } }); return modelGroup; }
private Model3DGroup CreateMockModel(ImageSource image, double width, double height) { //Создаем фон Point3D p1 = new Point3D(0, 0, 0); Point3D p2 = new Point3D(0, width, 0); Point3D p3 = new Point3D(height, width, 0); Point3D p4 = new Point3D(height, 0, 0); ImageBrush brush = new ImageBrush(image); //добавляем фон в группу Model3DGroup background = new Model3DGroup(); background.Children.Add(CreateBackgroundModelGroup(p1, p2, p3, p4, brush)); background.Freeze(); return background; }
public Model3DGroup GetNewStitching() { //if (!m_stitchingHasChanged || !m_hasStitching) // return null; m_stitchingHasChanged = false; Model3DGroup model = new Model3DGroup(); for (int i = 0; i < 3; i++) { if (m_stitchingGeometry[i] != null) model.Children.Add(m_stitchingGeometry[i]); } model.Freeze(); return model; }
private void UpdateCurrentTile(PointCloudTile tile) { if (tile == null) return; List<PointCloudTile> tilesToLoad = new List<PointCloudTile>(); int pointsToLoad = 0; Model3DGroup emptyModelGroup = new Model3DGroup(); emptyModelGroup.Freeze(); bool isDirty = false; int radius = 2; int xMin = Math.Max(0, tile.Col - radius); int xMax = Math.Min(tile.Col + radius + 1, CurrentTileSource.TileSet.Cols); int yMin = Math.Max(0, tile.Row - radius); int yMax = Math.Min(tile.Row + radius + 1, CurrentTileSource.TileSet.Rows); for (int x = xMin; x < xMax; x++) { for (int y = yMin; y < yMax; y++) { PointCloudTile currentTile = CurrentTileSource.TileSet.GetTile(y, x); if (currentTile != null) { if (!m_loadedTiles.ContainsKey(currentTile)) { tilesToLoad.Add(currentTile); pointsToLoad += currentTile.PointCount; isDirty = true; } } } } PointCloudTile[] loadedTiles = m_loadedTiles.Keys.ToArray(); SortByDistanceFromTile(loadedTiles, tile); Array.Reverse(loadedTiles); // drop loaded tiles that are the farthest from the center int totalAllowedPoints = MAX_BUFFER_SIZE_BYTES / CurrentTileSource.PointSizeBytes; int loadedPoints = loadedTiles.Sum(t => t.PointCount); int potentialTotalPoints = loadedPoints + pointsToLoad; Dictionary<PointCloudTile, TileInfo3D> alteredTiles = new Dictionary<PointCloudTile, TileInfo3D>(); if (potentialTotalPoints > totalAllowedPoints) { int pointsToDrop = potentialTotalPoints - totalAllowedPoints; int i = 0; while (pointsToDrop > 0) { PointCloudTile currentTile = loadedTiles[i]; TileInfo3D tileInfo = m_tileInfo[currentTile]; GeometryModel3D model = m_loadedTiles[currentTile]; m_meshTileMap.Remove(model); m_loadedTiles.Remove(currentTile); //m_loadedTileBuffers.Remove(currentTile); // replace high-res tile with low-res geometry int modelIndex = tileInfo.Tile.ValidIndex; m_tileModelCollection[modelIndex] = tileInfo.LowResGeometry; // clear stitching m_stitchingModelCollection[modelIndex] = emptyModelGroup; tileInfo.ClearGeometry(); alteredTiles.Add(currentTile, tileInfo); pointsToDrop -= currentTile.PointCount; ++i; } } Jacere.Core.Geometry.Point3D centerOfMass = CurrentTileSource.CenterOfMass; PointCloudTile[] tilesToLoadArray = tilesToLoad.ToArray(); #warning sort so that disk reads are in order? or make a tile cache SortByDistanceFromTile(tilesToLoadArray, tile); foreach (PointCloudTile currentTile in tilesToLoadArray) { TileInfo3D tileInfo = m_tileInfo[currentTile]; CurrentTileSource.LoadTileGrid(currentTile, m_buffer, m_gridHighRes, m_quantizedGridHighRes); if (ENABLE_HEIGHT_EXAGGERATION) m_gridHighRes.Multiply(m_heightExaggerationFactor, (float)centerOfMass.Z); Jacere.Core.Geometry.Extent3D tileExtent = currentTile.Extent; MeshGeometry3D mesh = CurrentTileSource.GenerateMesh(m_gridHighRes, tileExtent); DiffuseMaterial material = new DiffuseMaterial(); if (USE_HIGH_RES_TEXTURE) { material.Brush = m_overviewTextureBrush; mesh.TextureCoordinates = MeshUtils.GeneratePlanarTextureCoordinates(mesh, m_overallCenteredExtent, MathUtils.ZAxis); } else { material.Brush = m_solidBrush; } material.Freeze(); GeometryModel3D geometryModel = new GeometryModel3D(mesh, material); geometryModel.Freeze(); // replace low-res tile with high-res geometry int modelIndex = tileInfo.Tile.ValidIndex; m_tileModelCollection[modelIndex] = geometryModel; // clear stitching m_stitchingModelCollection[modelIndex] = emptyModelGroup; tileInfo.UpdateGeometry(geometryModel, m_gridHighRes); alteredTiles.Add(currentTile, tileInfo); m_meshTileMap.Add(geometryModel, currentTile); m_loadedTiles.Add(currentTile, geometryModel); //m_loadedTileBuffers.Add(currentTile, inputBuffer); } // in the future, I could have a list of which tiles need to be checked for stitching updates // go through the stitching groups and replace any empty ones with the appropriate stitching if (ENABLE_STITCHING && isDirty) { PointCloudTile[] alteredTileArray = alteredTiles.Keys.ToArray(); foreach (PointCloudTile currentTile in alteredTileArray) { // this amount of clearing is excessive. I only want to clear one of the edges if (currentTile.Col < CurrentTileSource.TileSet.Cols - 1) { PointCloudTile adjacentTile = CurrentTileSource.TileSet.GetTile(currentTile.Row, currentTile.Col + 1); if (adjacentTile != null && m_tileInfo.ContainsKey(adjacentTile)) { TileInfo3D adjacentTileInfo = m_tileInfo[adjacentTile]; if (!alteredTiles.ContainsKey(adjacentTile)) alteredTiles.Add(adjacentTile, adjacentTileInfo); m_stitchingModelCollection[adjacentTileInfo.Tile.ValidIndex] = emptyModelGroup; adjacentTileInfo.UpdateStitching(null, null, TileStitchingEdge.Left); } } if (currentTile.Row < CurrentTileSource.TileSet.Rows - 1) { PointCloudTile adjacentTile = CurrentTileSource.TileSet.GetTile(currentTile.Row + 1, currentTile.Col); if (adjacentTile != null && m_tileInfo.ContainsKey(adjacentTile)) { TileInfo3D adjacentTileInfo = m_tileInfo[adjacentTile]; if (!alteredTiles.ContainsKey(adjacentTile)) alteredTiles.Add(adjacentTile, adjacentTileInfo); m_stitchingModelCollection[adjacentTileInfo.Tile.ValidIndex] = emptyModelGroup; adjacentTileInfo.UpdateStitching(null, null, TileStitchingEdge.Top); } } if (currentTile.Col < CurrentTileSource.TileSet.Cols - 1 && currentTile.Row < CurrentTileSource.TileSet.Rows - 1) { PointCloudTile adjacentTile = CurrentTileSource.TileSet.GetTile(currentTile.Row + 1, currentTile.Col + 1); if (adjacentTile != null && m_tileInfo.ContainsKey(adjacentTile)) { TileInfo3D adjacentTileInfo = m_tileInfo[adjacentTile]; if (!alteredTiles.ContainsKey(adjacentTile)) alteredTiles.Add(adjacentTile, adjacentTileInfo); m_stitchingModelCollection[adjacentTileInfo.Tile.ValidIndex] = emptyModelGroup; adjacentTileInfo.UpdateStitching(null, null, TileStitchingEdge.TopLeft); } } } foreach (KeyValuePair<PointCloudTile, TileInfo3D> kvp in alteredTiles) { int i = kvp.Value.Tile.ValidIndex; Model3DGroup stitchingGroup = m_stitchingModelCollection[i] as Model3DGroup; if (stitchingGroup.Children.Count == 0) { GeometryModel3D geometryModel = m_tileModelCollection[i] as GeometryModel3D; Model3DGroup newStitchingGroup = GenerateTileStitching(CurrentTileSource, kvp.Value); if (newStitchingGroup.Children.Count > 0) m_stitchingModelCollection[i] = newStitchingGroup; } } //for (int i = 0; i < m_stitchingModelCollection.Count; i++) //{ // Model3DGroup stitchingGroup = m_stitchingModelCollection[i] as Model3DGroup; // // this is an incorrect condition, but it won't be apparent until I get multi-res stitching // if (stitchingGroup.Children.Count < 3) // { // GeometryModel3D geometryModel = m_tileModelCollection[i] as GeometryModel3D; // PointCloudTile currentTile = m_meshTileMap[geometryModel]; // TileInfo3D currentTileInfo = m_tileInfo[currentTile]; // Model3DGroup newStitchingGroup = GenerateTileStitching(CurrentTileSource, currentTileInfo); // if (newStitchingGroup.Children.Count > 0) // m_stitchingModelCollection[i] = newStitchingGroup; // } //} } }
/// <summary> /// Builds the model. /// </summary> /// <returns>A Model3D object.</returns> private Model3DGroup BuildModel() { Model3DGroup modelGroup = null; this.Dispatch(() => { modelGroup = new Model3DGroup(); foreach (var g in this.Groups) { foreach (var gm in g.CreateModels()) { if (this.Freeze) { gm.Freeze(); } modelGroup.Children.Add(gm); } } if (this.Freeze) { modelGroup.Freeze(); } }); return modelGroup; }
private void Paint3DScena() { //Создаем фон int width = scenario.map.GetMap().GetLength(0), height = scenario.map.GetMap().GetLength(1); Point3D p1 = new Point3D(-width / 2, 0, -height / 2); Point3D p2 = new Point3D(width / 2, 0, -height / 2); Point3D p3 = new Point3D(width / 2, 0, height / 2); Point3D p4 = new Point3D(-width / 2, 0, height / 2); ImageBrush brush; if (File.Exists(Properties.Settings.Default.ScenarioPath + "image.jpg")) { brush = new ImageBrush(new BitmapImage(new Uri(Properties.Settings.Default.ScenarioPath + "image.jpg"))); } else { brush = new ImageBrush(scenario.Image); } //добавляем фон в группу Model3DGroup background = new Model3DGroup(); background.Children.Add(CreateBackgroundModelGroup(p1, p2, p3, p4, brush)); background.Freeze(); ModelVisual3D backgroundModel = new ModelVisual3D(); backgroundModel.Content = background; //Заполняем ViewPort фоном mainViewport.Children.Add(backgroundModel); }
/// <summary> /// create square model /// </summary> /// <param name="pictureName">picture file path name</param> /// <param name="points">model points</param> /// <returns>model3d group</returns> public Model3DGroup Create(string pictureName, Point3D[] points, bool isFreeze) { try { ImageBrush imagebrush = CreateImageBrush(pictureName, isFreeze); Point[] ptexture0 = { new Point(0, 1), new Point(0, 0), new Point(1, 0) }; Point[] ptexture1 = { new Point(0, 1), new Point(1, 0), new Point(1, 1) }; Model3DGroup bottomside = new Model3DGroup(); bottomside.Children.Add(CreateGeoModel3D(points[2], points[3], points[0], ptexture0, imagebrush)); bottomside.Children.Add(CreateGeoModel3D(points[2], points[0], points[1], ptexture1, imagebrush)); Model3DGroup topside = new Model3DGroup(); topside.Children.Add(CreateGeoModel3D(points[7], points[6], points[5], ptexture0, imagebrush)); topside.Children.Add(CreateGeoModel3D(points[7], points[5], points[4], ptexture1, imagebrush)); Model3DGroup rightside = new Model3DGroup(); rightside.Children.Add(CreateGeoModel3D(points[2], points[1], points[5], ptexture0, imagebrush)); rightside.Children.Add(CreateGeoModel3D(points[2], points[5], points[6], ptexture1, imagebrush)); Model3DGroup leftside = new Model3DGroup(); leftside.Children.Add(CreateGeoModel3D(points[0], points[3], points[7], ptexture0, imagebrush)); leftside.Children.Add(CreateGeoModel3D(points[0], points[7], points[4], ptexture1, imagebrush)); Model3DGroup frontside = new Model3DGroup(); frontside.Children.Add(CreateGeoModel3D(points[3], points[2], points[6], ptexture0, imagebrush)); frontside.Children.Add(CreateGeoModel3D(points[3], points[6], points[7], ptexture1, imagebrush)); Model3DGroup backside = new Model3DGroup(); backside.Children.Add(CreateGeoModel3D(points[5], points[1], points[0], ptexture0, imagebrush)); backside.Children.Add(CreateGeoModel3D(points[5], points[0], points[4], ptexture1, imagebrush)); if (isFreeze) { bottomside.Freeze(); topside.Freeze(); rightside.Freeze(); leftside.Freeze(); frontside.Freeze(); backside.Freeze(); } Model3DGroup cube = new Model3DGroup(); cube.Children.Add(bottomside); cube.Children.Add(topside); cube.Children.Add(rightside); cube.Children.Add(leftside); cube.Children.Add(frontside); cube.Children.Add(backside); return cube; } catch (Exception ex) { throw ex; } }
/// <summary> /// create square 2D /// </summary> /// <param name="pictureName"></param> /// <param name="points"></param> /// <returns></returns> public Model3DGroup CreateSquare2D(string pictureName, Point3D[] points) { try { Uri inpuri = new Uri(@pictureName, UriKind.Relative); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = inpuri; bi.EndInit(); ImageBrush imagebrush = new ImageBrush(bi); imagebrush.Opacity = 100; imagebrush.Freeze(); Point[] ptexture0 = { new Point(0, 0), new Point(0, 1), new Point(1, 0) }; Point[] ptexture1 = { new Point(1, 0), new Point(0, 1), new Point(1, 1) }; Model3DGroup square = new Model3DGroup(); square.Children.Add(CreateGeoModel2D(points[0], points[3], points[1], ptexture0, imagebrush)); square.Children.Add(CreateGeoModel2D(points[1], points[3], points[2], ptexture1, imagebrush)); square.Freeze(); return square; } catch (Exception ex) { throw ex; } }
/// <summary> /// create square 2D /// </summary> /// <param name="pictureName"></param> /// <param name="points"></param> /// <returns></returns> public Model3DGroup CreateSquare2D(Color modelColor, Point3D[] points) { try { SolidColorBrush modelbrush = new SolidColorBrush(modelColor); modelbrush.Freeze(); Model3DGroup square = new Model3DGroup(); square.Children.Add(CreateGeoModel2D(points[0], points[3], points[1], modelbrush)); square.Children.Add(CreateGeoModel2D(points[1], points[3], points[2], modelbrush)); square.Freeze(); return square; } catch (Exception ex) { throw ex; } }
/// <summary> /// create square model /// </summary> /// <param name="pictureName">picture file path name</param> /// <param name="points">model points</param> /// <returns>model3d group</returns> public Model3DGroup Create(string[] pictureNames, Point3D[] points, bool isFreeze) { try { //0=front,1=back,2=left,3=right,4=top,5=bottom ImageBrush[] imgbrushs = new ImageBrush[6]; for (int i = 0; i < pictureNames.Length; i++) { imgbrushs[i] = CreateImageBrush(pictureNames[i], isFreeze); } Point[] ptexture0 = { new Point(0, 1), new Point(1, 1), new Point(1, 0) }; Point[] ptexture1 = { new Point(0, 1), new Point(1, 0), new Point(0, 0) }; Model3DGroup bottomside = new Model3DGroup(); bottomside.Children.Add(CreateGeoModel3D(points[2], points[3], points[0], ptexture0, imgbrushs[5])); bottomside.Children.Add(CreateGeoModel3D(points[2], points[0], points[1], ptexture1, imgbrushs[5])); Model3DGroup topside = new Model3DGroup(); topside.Children.Add(CreateGeoModel3D(points[7], points[6], points[5], ptexture0, imgbrushs[4])); topside.Children.Add(CreateGeoModel3D(points[7], points[5], points[4], ptexture1, imgbrushs[4])); Model3DGroup rightside = new Model3DGroup(); rightside.Children.Add(CreateGeoModel3D(points[2], points[1], points[5], ptexture0, imgbrushs[3])); rightside.Children.Add(CreateGeoModel3D(points[2], points[5], points[6], ptexture1, imgbrushs[3])); Model3DGroup leftside = new Model3DGroup(); leftside.Children.Add(CreateGeoModel3D(points[0], points[3], points[7], ptexture0, imgbrushs[2])); leftside.Children.Add(CreateGeoModel3D(points[0], points[7], points[4], ptexture1, imgbrushs[2])); Model3DGroup frontside = new Model3DGroup(); frontside.Children.Add(CreateGeoModel3D(points[3], points[2], points[6], ptexture0, imgbrushs[0])); frontside.Children.Add(CreateGeoModel3D(points[3], points[6], points[7], ptexture1, imgbrushs[0])); Model3DGroup backside = new Model3DGroup(); backside.Children.Add(CreateGeoModel3D(points[1], points[0], points[4], ptexture0, imgbrushs[1])); backside.Children.Add(CreateGeoModel3D(points[1], points[4], points[5], ptexture1, imgbrushs[1])); if (isFreeze) { bottomside.Freeze(); topside.Freeze(); rightside.Freeze(); leftside.Freeze(); frontside.Freeze(); backside.Freeze(); } Model3DGroup cube = new Model3DGroup(); cube.Children.Add(bottomside); cube.Children.Add(topside); cube.Children.Add(rightside); cube.Children.Add(leftside); cube.Children.Add(frontside); cube.Children.Add(backside); return cube; } catch (Exception ex) { throw ex; } }
/// <summary> /// Reads the model from the specified stream. /// </summary> /// <param name="s">The stream.</param> /// <returns>The model.</returns> public override Model3DGroup Read(Stream s) { using (var reader = new BinaryReader(s)) { long length = reader.BaseStream.Length; // http://gpwiki.org/index.php/Loading_3ds_files // http://www.flipcode.com/archives/3DS_File_Loader.shtml // http://sandy.googlecode.com/svn/trunk/sandy/as3/branches/3.0.2/src/sandy/parser/Parser3DS.as var headerId = this.ReadChunkId(reader); if (headerId != ChunkID.MAIN3DS) { throw new FileFormatException("Unknown file"); } int headerSize = this.ReadChunkSize(reader); if (headerSize != length) { throw new FileFormatException("Incomplete file (file length does not match header)"); } while (reader.BaseStream.Position < reader.BaseStream.Length) { var id = this.ReadChunkId(reader); int size = this.ReadChunkSize(reader); switch (id) { case ChunkID.EDIT_MATERIAL: this.ReadMaterial(reader, size); break; case ChunkID.EDIT_OBJECT: this.ReadObject(reader, size); break; case ChunkID.EDIT3DS: case ChunkID.OBJ_CAMERA: case ChunkID.OBJ_LIGHT: case ChunkID.OBJ_TRIMESH: // don't read the whole chunk, read the sub-defines... break; default: // download the whole chunk this.ReadData(reader, size - 6); break; } } Model3DGroup mg = null; this.Dispatch( () => { mg = new Model3DGroup(); foreach (var m in this.meshes) { var model = m.CreateModel(); if (this.Freeze) { model.Freeze(); } mg.Children.Add(model); } if (this.Freeze) { mg.Freeze(); } }); return mg; } }