// Rescales the model to size specified by 'sizeInMetres' void RescaleModel(MyVoxelMap voxelMap, MyVoxelImportOptions importOptions) { Vector3 originalSize = m_maxCoord - m_minCoord; Vector3 originalCenter = (m_maxCoord + m_minCoord) / 2.0f; m_minCoord = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); m_maxCoord = new Vector3(float.MinValue, float.MinValue, float.MinValue); // We don't want to touch borders of a voxel map so we need to subtract size of data cell from each side. // I have choosen data cell (not one or two voxels) because LOD - average content of some data cells that were too close to voxel map border // were almost full data cell so when converted as LOD, hole appear there (originaly I was just multiplying by 0.9, but that is wasting in case of large voxel maps). //Vector3 rescaleFactor = voxelMap.SizeInMetres / originalSize * 0.9f; Vector3 rescaleFactor; rescaleFactor.X = (voxelMap.SizeInMetres.X - 2 * MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_METRES) / originalSize.X; rescaleFactor.Y = (voxelMap.SizeInMetres.Y - 2 * MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_METRES) / originalSize.Y; rescaleFactor.Z = (voxelMap.SizeInMetres.Z - 2 * MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_METRES) / originalSize.Z; switch (importOptions) { case MyVoxelImportOptions.KeepAspectRatio: rescaleFactor.X = rescaleFactor.Y = rescaleFactor.Z = Math.Min(Math.Min(rescaleFactor.X, rescaleFactor.Y), rescaleFactor.Z); break; case MyVoxelImportOptions.KeepScale: //Cannot keep scale if the voxel map isn't big enough to contain it, will keep aspect ratio instead if (rescaleFactor.X >= 1 && rescaleFactor.Y >= 1 && rescaleFactor.Z >= 1) { rescaleFactor = Vector3.One; } else { rescaleFactor.X = rescaleFactor.Y = rescaleFactor.Z = Math.Min(Math.Min(rescaleFactor.X, rescaleFactor.Y), rescaleFactor.Z); } break; } for (int i = 0; i < m_triangles.Count; i++) { // Translate vertexes to the origin m_triangles[i].Vertex0 -= originalCenter; m_triangles[i].Vertex1 -= originalCenter; m_triangles[i].Vertex2 -= originalCenter; // Rescale vertexes m_triangles[i].Vertex0 *= rescaleFactor; m_triangles[i].Vertex1 *= rescaleFactor; m_triangles[i].Vertex2 *= rescaleFactor; // Translate vertexes to voxel map position m_triangles[i].Vertex0 += voxelMap.PositionLeftBottomCorner + voxelMap.SizeInMetresHalf; m_triangles[i].Vertex1 += voxelMap.PositionLeftBottomCorner + voxelMap.SizeInMetresHalf; m_triangles[i].Vertex2 += voxelMap.PositionLeftBottomCorner + voxelMap.SizeInMetresHalf; CheckMinMaxCoords(m_triangles[i].Vertex0); CheckMinMaxCoords(m_triangles[i].Vertex1); CheckMinMaxCoords(m_triangles[i].Vertex2); } }
// Use this static method do one-time import of a voxel map public static void Run(MyVoxelMap voxelMap, MyModelObj model, MyVoxelImportOptions importOptions) { MyVoxelImport voxelMapImport = new MyVoxelImport(voxelMap, model, importOptions); }
// Model will be scaled/translated to fit voxel map size (X, Y and Z) MyVoxelImport(MyVoxelMap voxelMap, MyModelObj model, MyVoxelImportOptions importOptions) { // Load model, get triangles LoadModel(model); // Rescale the model so it fits voxelMap (three directions!!!) RescaleModel(voxelMap, importOptions); // Fill lookup array with triangles located at specified voxel positions. Array is 2D. FillTrianglesLookup(voxelMap); // Create XZ map where every voxel center gets list of triangles that lie on its Y line // Do this by iterating over all triangles and making references to them from XZ map Import(voxelMap); }
// Use this static method do one-time import of a voxel map public static void Run(MyVoxelMap voxelMap, string modelName, MyVoxelImportOptions importOptions) { MyVoxelImport voxelMapImport = new MyVoxelImport(voxelMap, modelName, importOptions); }