public cSectionGCD() { csMode = CsMode.None; ready = false; sloxelResolution = Vector3.one * (float)InspectorT.slicerForm.ResUpDown.Value; increment = 1.0f / sloxelResolution.x; csPlaneEqn = new Vector4(0f, 1f, 0f, 0f); triangles.Clear(); layers.Clear(); layerHeights.Clear(); voxelHeights.Clear(); highlights.Clear(); foreach (var voxel in voxels) { MonoBehaviour.Destroy(voxel.Value.Voxel); } voxels.Clear(); topLayerHeight = -1000000; bottomLayerHeight = 1000000; tlhGrid = -1000000; blhGrid = 1000000; voxelHeight = 0; voxelLayers = 0; minLineSepSloxels = 1000000; maxLineSepSloxels = -1; minLineCountSloxels = 1000000; maxLineCountSloxels = -1; InspectorR.voxelsFitted = false; foreach (var l in LoadFile.gcdLines) { if (!layers.ContainsKey(l.p1.y)) { var newLayer = new CrossSectionLayer(l.p1.y); newLayer.Layer = layers.Count; layers.Add(l.p1.y, newLayer); layerHeights.Add(l.p1.y); if (l.p1.y < bottomLayerHeight) { bottomLayerHeight = l.p1.y; blhGrid = Mathf.Floor(bottomLayerHeight * sloxelResolution.x) * increment - increment / 2.0f; } if (l.p1.y > topLayerHeight) { topLayerHeight = l.p1.y; tlhGrid = Mathf.Ceil(topLayerHeight * sloxelResolution.x) * increment + increment / 2.0f; } } layers[l.p1.y].gcdLines.Add(l); } var modelHeight = topLayerHeight - bottomLayerHeight; var sloxelHeight = (modelHeight / layers.Count); var sloxelsPerVoxel = Mathf.RoundToInt(increment / sloxelHeight); voxelHeight = sloxelHeight * sloxelsPerVoxel;// layers.Count / sloxelsPerVoxel;// (topLayerHeight - bottomLayerHeight + increment) / sloxelsPerVoxel; for (float i = bottomLayerHeight - voxelHeight; i <= topLayerHeight + voxelHeight; i += voxelHeight) { if (!voxelHeights.Contains(i)) voxelHeights.Add(i); } var verts = new List<Vector3>(); foreach (var _v in MakeMesh.verts) { verts.Add(_v); } var count = (int)(verts.Count / 3); for (int i = 0; i < count; i++) { var newTriangle = new Triangle(); for (int j = 0; j <= 2; j++) { if (verts[3 * i + j].y > newTriangle.Max) newTriangle.Max = verts[3 * i + j].y; if (verts[3 * i + j].y < newTriangle.Min) newTriangle.Min = verts[3 * i + j].y; newTriangle.Points[j] = verts[3 * i + j]; newTriangle.Normal = MakeMesh.normals[3 * i + j]; } newTriangle.PlaneEquation = GetPlane(newTriangle.Points); triangles.Add(newTriangle); } foreach (var layer in layers) { csPlaneEqn.w = layer.Key; CrossSection(); } //var wt = new CheckWalls(); foreach (var layer in layers) { var min = layer.Value.Min; var max = layer.Value.Max; foreach (var line in layer.Value.border) { if (line.Endpoint0.x < min.x) min.x = line.Endpoint0.x; if (line.Endpoint1.x < min.x) min.x = line.Endpoint1.x; if (line.Endpoint0.x > max.x) max.x = line.Endpoint0.x; if (line.Endpoint1.x > max.x) max.x = line.Endpoint1.x; if (line.Endpoint0.z < min.z) min.z = line.Endpoint0.z; if (line.Endpoint1.z < min.z) min.z = line.Endpoint1.z; if (line.Endpoint0.z > max.z) max.z = line.Endpoint0.z; if (line.Endpoint1.z > max.z) max.z = line.Endpoint1.z; } layer.Value.Min = min; layer.Value.Max = max; } GetSloxels(); //getXLines(); //getZLines(); ready = true; if (InspectorT.slicerForm != null) InspectorT.slicerForm.LayerTrackbar.Maximum = cSectionGCD.layers.Count - 1; InspectorR.voxelsLoaded = true; InspectorT.slicerForm.panel1.Invalidate(); }
public void AddSloxel(Vector3 centroid, CrossSectionLayer layer) { if (layer.SloxPositions.Contains(centroid)) return; layer.SloxPositions.Add(centroid); var slox = new Sloxel(); slox.Layer = layer.Layer; slox.Position = centroid; slox.Dim = sloxelResolution.x; slox.Id = layer.Sloxels.Count; layer.Sloxels.Add(slox); var inc = 1 / sloxelResolution.x; foreach (var voxelLayer in voxelHeights) { if (Mathf.Abs(centroid.y - voxelLayer) <= voxelHeight / 2) { var voxPos = new Vector3(centroid.x, voxelLayer, centroid.z); if (!voxels.ContainsKey(voxPos)) { var voxObj = MonoBehaviour.Instantiate(GameObject.Find("VOXELIZER").GetComponent<MeshVoxelizer>().voxel, voxPos, Quaternion.identity) as GameObject; voxObj.transform.SetParent(GameObject.Find("VOXELIZER").transform); voxObj.GetComponent<SingleCube>().Voxel(); voxObj.GetComponent<SingleCube>().Id = voxels.Count; voxObj.GetComponent<SingleCube>().Origin = voxPos; var scale = increment * 0.9f; voxObj.transform.localScale = new Vector3(scale, 0.9f * voxelHeight, scale); var small = SingleCube.scale; voxObj.GetComponent<BoxCollider>().size = new Vector3(small, small, small); var newVox = new VoxelClass(); newVox.Id = (float)voxels.Count; newVox.Origin = voxPos; newVox.singleCube = voxObj.GetComponent<SingleCube>(); voxObj.GetComponent<SingleCube>().voxelClass = newVox; newVox.Voxel = voxObj; voxels.Add(voxPos, newVox); highlights.Add(voxPos); InspectorR.highlightVector = voxPos; } foreach (var line in layer.gcdLines) { if (line.MovesInX) { if (line.p1.z >= slox.Position.z - inc / 2 && line.p1.z <= slox.Position.z + inc / 2) { var m = line.p2 - line.p1; var sloxMinX = slox.Position.x - inc / 2; var sloxMaxX = slox.Position.x + inc / 2; var factorMinX = (sloxMinX - line.p1.x) / m.x; var factorMaxX = (sloxMaxX - line.p1.x) / m.x; if ((factorMinX >= 0 && factorMinX <= 1) || (factorMaxX >= 0 && factorMaxX <= 1)) { if (line.step < slox.MinLineNumber) slox.MinLineNumber = line.step; if (line.step > slox.MaxLineNumber) slox.MaxLineNumber = line.step; if (!slox.IntersectedByLines.Contains(line)) { foreach (var cb in line.CrazyBalls) { if (cb.t >= factorMinX && cb.t < factorMaxX) { slox.CrazyBalls.Add(cb); } } slox.IntersectedByLines.Add(line); } } } } else if (line.MovesInZ) { if (line.p1.x >= slox.Position.x - inc / 2 && line.p1.x <= slox.Position.x + inc / 2) { var m = line.p2 - line.p1; var sloxMinZ = slox.Position.z - inc / 2; var sloxMaxZ = slox.Position.z + inc / 2; var factorMinZ = (sloxMinZ - line.p1.z) / m.z; var factorMaxZ = (sloxMaxZ - line.p1.z) / m.z; if ((factorMinZ >= 0 && factorMinZ <= 1) || (factorMaxZ >= 0 && factorMaxZ <= 1)) { if (line.step < slox.MinLineNumber) slox.MinLineNumber = line.step; if (line.step > slox.MaxLineNumber) slox.MaxLineNumber = line.step; if (!slox.IntersectedByLines.Contains(line)) { foreach (var cb in line.CrazyBalls) { if (cb.t >= factorMinZ && cb.t < factorMaxZ) { slox.CrazyBalls.Add(cb); } } slox.IntersectedByLines.Add(line); } } } } } var v = voxels[voxPos]; if (slox.MinLineNumber < v.MinLine) v.MinLine = slox.MinLineNumber; if (slox.MaxLineNumber > v.MaxLine) v.MaxLine = slox.MaxLineNumber; slox.Voxel = v; if (!layer.Voxels.Contains(v)) { layer.Voxels.Add(v); } slox.VoxelOrigin = voxPos; foreach (var cb in slox.CrazyBalls) { v.CrazyBalls.Add(cb); } v.Sloxels.Add(slox); } } }