public void SetPrintLevelingEquation(Vector3 position0, Vector3 position1, Vector3 position2, Vector2 bedCenter) { if (position0 == position1 || position1 == position2 || position2 == position0) { return; } Plane planeOfPoints = new Plane(position0, position1, position2); Ray ray = new Ray(new Vector3(bedCenter, 0), Vector3.UnitZ); bool inFront; double distanceToPlaneAtBedCenter = planeOfPoints.GetDistanceToIntersection(ray, out inFront); Matrix4X4 makePointsFlatMatrix = Matrix4X4.CreateTranslation(-bedCenter.x, -bedCenter.y, -distanceToPlaneAtBedCenter); makePointsFlatMatrix *= Matrix4X4.CreateRotation(planeOfPoints.planeNormal, Vector3.UnitZ); makePointsFlatMatrix *= Matrix4X4.CreateTranslation(bedCenter.x, bedCenter.y, 0);//distanceToPlaneAtBedCenter); bedLevelMatrix = Matrix4X4.Invert(makePointsFlatMatrix); { // test that the points come back as 0 zs Vector3 outPosition0 = Vector3.TransformPosition(position0, makePointsFlatMatrix); Vector3 outPosition1 = Vector3.TransformPosition(position1, makePointsFlatMatrix); Vector3 outPosition2 = Vector3.TransformPosition(position2, makePointsFlatMatrix); Vector3 printPosition0 = new Vector3(LevelWizardBase.GetPrintLevelPositionToSample(0), 0); Vector3 printPosition1 = new Vector3(LevelWizardBase.GetPrintLevelPositionToSample(1), 0); Vector3 printPosition2 = new Vector3(LevelWizardBase.GetPrintLevelPositionToSample(2), 0); Vector3 leveledPositon0 = Vector3.TransformPosition(printPosition0, bedLevelMatrix); Vector3 leveledPositon1 = Vector3.TransformPosition(printPosition1, bedLevelMatrix); Vector3 leveledPositon2 = Vector3.TransformPosition(printPosition2, bedLevelMatrix); } }
public void SetPrintLevelingEquation(Vector3 position0, Vector3 position1, Vector3 position2, Vector2 bedCenter) { if (position0 == position1 || position1 == position2 || position2 == position0) { return; } Plane planeOfPoints = new Plane(position0, position1, position2); Ray ray = new Ray(new Vector3(bedCenter, 0), Vector3.UnitZ); bool inFront; double distanceToPlaneAtBedCenter = planeOfPoints.GetDistanceToIntersection(ray, out inFront); Matrix4X4 makePointsFlatMatrix = Matrix4X4.CreateTranslation(-bedCenter.x, -bedCenter.y, -distanceToPlaneAtBedCenter); makePointsFlatMatrix *= Matrix4X4.CreateRotation(planeOfPoints.PlaneNormal, Vector3.UnitZ); makePointsFlatMatrix *= Matrix4X4.CreateTranslation(bedCenter.x, bedCenter.y, 0);//distanceToPlaneAtBedCenter); bedLevelMatrix = Matrix4X4.Invert(makePointsFlatMatrix); }
public Vector3 GetPositionWithZOffset(Vector3 currentDestination) { if (LevelingData.SampledPositions.Count == NumberOfRadialSamples+1) { Vector2 destinationFromCenter = new Vector2(currentDestination) - BedCenter; double angleToPoint = Math.Atan2(destinationFromCenter.y, destinationFromCenter.x); if (angleToPoint < 0) { angleToPoint += MathHelper.Tau; } double oneSegmentAngle = MathHelper.Tau / NumberOfRadialSamples; int firstIndex = (int)(angleToPoint / oneSegmentAngle); int lastIndex = firstIndex + 1; if (lastIndex == NumberOfRadialSamples) { lastIndex = 0; } Plane currentPlane = new Plane(LevelingData.SampledPositions[firstIndex], LevelingData.SampledPositions[lastIndex], LevelingData.SampledPositions[NumberOfRadialSamples]); double hitDistance = currentPlane.GetDistanceToIntersection(new Vector3(currentDestination.x, currentDestination.y, 0), Vector3.UnitZ); currentDestination.z += hitDistance; } return currentDestination; }
public void GetPerimetersForAllLayers(Mesh meshToSlice, double firstLayerHeight, double otherLayerHeights) { AllLayers.Clear(); AxisAlignedBoundingBox meshBounds = meshToSlice.GetAxisAlignedBoundingBox(); double heightWithoutFirstLayer = meshBounds.ZSize - firstLayerHeight; int layerCount = (int)((heightWithoutFirstLayer / otherLayerHeights) + .5); double currentZ = otherLayerHeights; if (firstLayerHeight > 0) { layerCount++; currentZ = firstLayerHeight; } for (int i = 0; i < layerCount; i++) { allLayers.Add(new SliceLayer(currentZ)); currentZ += otherLayerHeights; } foreach (Face face in meshToSlice.Faces) { double minZ = double.MaxValue; double maxZ = double.MinValue; foreach (FaceEdge faceEdge in face.FaceEdges()) { minZ = Math.Min(minZ, faceEdge.firstVertex.Position.z); maxZ = Math.Max(maxZ, faceEdge.firstVertex.Position.z); } for (int layerIndex = 0; layerIndex < layerCount; layerIndex++) { SliceLayer layer = allLayers[layerIndex]; double zHeight = layer.ZHeight; if (zHeight < minZ) { // not up to the start of the face yet continue; } if (zHeight > maxZ) { // done with this face break; } Plane cutPlane = new Plane(Vector3.UnitZ, zHeight); Vector3 start; Vector3 end; if (face.GetCutLine(cutPlane, out start, out end)) { layer.UnorderedSegments.Add(new SliceLayer.Segment(new Vector2(start.x, start.y), new Vector2(end.x, end.y))); } } } }
public bool GetCutLine(Plane cutPlane, out Vector3 start, out Vector3 end) { start = new Vector3(); end = new Vector3(); int splitCount = 0; FaceEdge prevEdge = null; bool prevInFront = false; bool first = true; FaceEdge firstEdge = null; bool firstInFront = false; foreach (FaceEdge faceEdge in FaceEdges()) { if (first) { prevEdge = faceEdge; prevInFront = cutPlane.GetDistanceFromPlane(prevEdge.firstVertex.Position) > 0; first = false; firstEdge = prevEdge; firstInFront = prevInFront; } else { FaceEdge curEdge = faceEdge; bool curInFront = cutPlane.GetDistanceFromPlane(curEdge.firstVertex.Position) > 0; if (prevInFront != curInFront) { // we crossed over the cut line Vector3 directionNormal = (curEdge.firstVertex.Position - prevEdge.firstVertex.Position).GetNormal(); Ray edgeRay = new Ray(prevEdge.firstVertex.Position, directionNormal); double distanceToHit; bool hitFrontOfPlane; if (cutPlane.RayHitPlane(edgeRay, out distanceToHit, out hitFrontOfPlane)) { splitCount++; if (splitCount == 1) { start = edgeRay.origin + edgeRay.directionNormal * distanceToHit; } else { end = edgeRay.origin + edgeRay.directionNormal * distanceToHit; } } } prevEdge = curEdge; prevInFront = curInFront; if (splitCount == 2) { break; } } } if (splitCount == 1 && prevInFront != firstInFront) { // we crossed over the cut line Vector3 directionNormal = (firstEdge.firstVertex.Position - prevEdge.firstVertex.Position).GetNormal(); Ray edgeRay = new Ray(prevEdge.firstVertex.Position, directionNormal); double distanceToHit; bool hitFrontOfPlane; if (cutPlane.RayHitPlane(edgeRay, out distanceToHit, out hitFrontOfPlane)) { splitCount++; end = edgeRay.origin + edgeRay.directionNormal * distanceToHit; } } if (splitCount == 2) { return true; } return false; }
public static Vector3 GetPositionWithZOffset(Vector3 currentDestination, PrintLevelingData levelingData) { Vector2 destinationFromCenter = new Vector2(currentDestination) - ActiveSliceSettings.Instance.BedCenter; double angleToPoint = Math.Atan2(destinationFromCenter.y, destinationFromCenter.x); if (angleToPoint < 0) { angleToPoint += MathHelper.Tau; } double oneSegmentAngle = MathHelper.Tau / 6; int firstIndex = (int)(angleToPoint / oneSegmentAngle); int lastIndex = firstIndex + 1; if (lastIndex == 6) { lastIndex = 0; } Plane currentPlane = new Plane(levelingData.SampledPositions[firstIndex], levelingData.SampledPositions[lastIndex], levelingData.SampledPositions[6]); double hitDistance = currentPlane.GetDistanceToIntersection(new Vector3(currentDestination.x, currentDestination.y, 0), Vector3.UnitZ); currentDestination.z += hitDistance; return currentDestination; }