public static bool hitMainBodyBefore(double[] rayOrigin, double[] rayDirection, double[] hitPointToCompareTo) { List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])mainBody.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(mainBody.Transform2); bodies.Add(tempBody); } int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)rayOrigin, (object)rayDirection, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS | swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double).0000001, (double).0000001); double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); if (numIntersectionsFound == 0) { return(false); } int lengthOfOneReturn = 9; for (int i = 0; i < numIntersectionsFound; i++) { byte intersectionType = (byte)horrifyingReturn[i * lengthOfOneReturn + 2]; if ((intersectionType & (byte)swRayPtsResults_e.swRayPtsResultsENTER) == 0) { // we need it to be just entry rays continue; } double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; if (distanceAlongRay(rayOrigin, new double[] { x, y, z }) < distanceAlongRay(rayOrigin, hitPointToCompareTo)) { return(true); } } return(false); }
public CylinderParams GetBoundingCylinder(IBody2 body, double[] dir) { double[] xAxis = new double[] { 1, 0, 0 }; double[] yAxis = new double[] { 0, 1, 0 }; double[] zAxis = new double[] { 0, 0, 1 }; bool isAligned = m_MathHelper.ArrayEqual(dir, yAxis); IMathTransform alignTransform = null; if (!isAligned) { alignTransform = m_MathHelper.GetTransformBetweenVectorsAroundPoint( dir, yAxis, new double[] { 0, 0, 0 }); IBody2 bodyCopy = body.ICopy(); bodyCopy.ApplyTransform(alignTransform as MathTransform); body = bodyCopy; } double[] rootPt; double[] endPt; GetExtremePoints(body, yAxis, out rootPt, out endPt); double height = Math.Abs(endPt[1] - rootPt[1]); dir = new double[] { 0, endPt[1] - rootPt[1], 0 }; List <double[]> perPoints = GetPerimeterPoints(body, xAxis, zAxis); List <Point> points = new List <Point>(); foreach (double[] pt in perPoints) { points.Add(new Point(pt[0], pt[2])); } Circle cir = SmallestEnclosingCircle.MakeCircle(points); double[] circCenter = new double[] { cir.c.x, rootPt[1], cir.c.y }; if (!isAligned) { circCenter = m_MathHelper.TransformPoint(circCenter, alignTransform.IInverse()); dir = m_MathHelper.TransformVector(dir, alignTransform.IInverse()); } double radius = cir.r; return(new CylinderParams(height, circCenter, dir, radius)); }
public static double[] whereRayHitsComponent(Component2 component, MathPoint rayOrigin, MathVector rayDirection) { List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])component.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(component.Transform2); bodies.Add(tempBody); } int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)rayOrigin.ArrayData, (object)rayDirection.ArrayData, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS), (double).0000001, (double).0000001); if (numIntersectionsFound == 0) { return(null); } double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); int lengthOfOneReturn = 9; for (int i = 0; i < numIntersectionsFound; i++) { double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; double[] hitPoint = new double[] { x, y, z }; if (!hitMainBodyBefore(rayOrigin.ArrayData, rayDirection.ArrayData, hitPoint)) { return(hitPoint); } } return(null); }
public static bool ApplyTransform(this IBody2 body, Matrix4x4 t) { var transform = SwAddinBase.Active.Math.ToSwMatrix(t); return(body.ApplyTransform(transform)); }
public static bool rawRaysSeeComponentInCurrentConfig(Component2 component) { /* * see https://forum.solidworks.com/message/348151 */ List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])component.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(component.Transform2); bodies.Add(tempBody); } double[] rayVectorOrigins = camera.rayVectorOrigins(); double[] rayVectorDirections = camera.rayVectorDirections(); int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)rayVectorOrigins, (object)rayVectorDirections, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS), (double).0000001, (double).0000001); if (numIntersectionsFound == 0) { return(false); } double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); for (int i = 0; i < horrifyingReturn.Length; i += 9) { double[] pt = new double[] { horrifyingReturn[i + 3], horrifyingReturn[i + 4], horrifyingReturn[i + 5] }; } int lengthOfOneReturn = 9; for (int i = 0; i < numIntersectionsFound; i++) { int rayIndex = (int)horrifyingReturn[i * lengthOfOneReturn + 1]; double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; double[] rayOrigin = camera.rayVectorSources().ElementAt(rayIndex).ArrayData; double[] rayDirection = camera.rayVectors().ElementAt(rayIndex).ArrayData; double[] hitPoint = new double[] { x, y, z }; if (!hitMainBodyBefore(rayOrigin, rayDirection, hitPoint) && !hitSomethingElseBefore(rayOrigin, rayDirection, hitPoint)) { if (visualize) { startSketch(); visualizeRay(mathUtils.CreateVector(rayDirection), mathUtils.CreatePoint(rayOrigin)); finishSketch(); } return(true); } } return(false); }
public static ReflectionPoint reflectedRayCanSeeComponent(Component2 component) { List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])mainBody.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(mainBody.Transform2); bodies.Add(tempBody); } double[] rayOrigins = camera.rayVectorOrigins(); double[] rayDirections = camera.rayVectorDirections(); int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)rayOrigins, (object)rayDirections, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS | swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double).0000001, (double).0000001); double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); if (numIntersectionsFound == 0) { return(null); } int lengthOfOneReturn = 9; ReflectionPoint reflectionWorksAt = null; if (visualize) { startSketch(); } for (int i = 0; i < numIntersectionsFound; i++) { byte intersectionType = (byte)horrifyingReturn[i * lengthOfOneReturn + 2]; if ((intersectionType & (byte)swRayPtsResults_e.swRayPtsResultsENTER) == 0) { // we need it to be just entry rays continue; } int rayIndex = (int)horrifyingReturn[i * lengthOfOneReturn + 1]; double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; double nx = horrifyingReturn[i * lengthOfOneReturn + 6]; double ny = horrifyingReturn[i * lengthOfOneReturn + 7]; double nz = horrifyingReturn[i * lengthOfOneReturn + 8]; ReflectionPoint rp = new ReflectionPoint(mathUtils, new double[] { x, y, z }, new double[] { nx, ny, nz }, component); double[] reflectionDir = camera.calculateReflectionDir(camera.rayVectors().ElementAt(rayIndex).ArrayData, rp.nxnynz); MathVector reflectedRay = mathUtils.CreateVector(reflectionDir); if (rayHitsComponent(component, rp.location, reflectedRay)) { swDoc.SketchManager.CreatePoint(x, y, z); if (visualize) { visualizeRay(camera.centreOfVision, mathUtils.CreateVector(new double[] { rayDirections[rayIndex * 3], rayDirections[rayIndex * 3 + 1], rayDirections[rayIndex * 3 + 2] })); visualizeRay(rp.location, reflectedRay); } reflectionWorksAt = rp; } } if (visualize) { finishSketch("reflections for " + component.Name2); } return(reflectionWorksAt); }
public double distanceFromFlagToCamera() { if (Utilities.distanceBetween(belongsTo, camera.fieldOfView) < 0) { return(Utilities.distanceBetween(belongsTo, camera.fieldOfView)); } swFeatureMgr.EditRollback((int)swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, ""); swAssembly.EditRebuild(); IMathPoint centreOfFlagBase = Utilities.getNamedPoint("centre of flag base", belongsTo); IMathPoint centreOfFlagTip = Utilities.getNamedPoint("centre of flag top", belongsTo); double[] dataBase = centreOfFlagBase.ArrayData; double[] dataTip = centreOfFlagTip.ArrayData; double[] flagDir = new double[] { dataTip[0] - dataBase[0], dataTip[1] - dataBase[1], dataTip[2] - dataBase[2] }; List <IBody2> bodies = new List <IBody2>(); object vBodyInfo; object[] componentBodies = (object[])camera.fieldOfView.GetBodies3((int)swBodyType_e.swSolidBody, out vBodyInfo); for (int i = 0; i < componentBodies.Length; i++) { IBody2 tempBody = ((Body2)componentBodies[i]).ICopy(); tempBody.ApplyTransform(camera.fieldOfView.Transform2); bodies.Add(tempBody); } int numIntersectionsFound = (int)swDoc.RayIntersections((object)bodies.ToArray(), (object)dataTip, (object)flagDir, (int)(swRayPtsOpts_e.swRayPtsOptsTOPOLS | swRayPtsOpts_e.swRayPtsOptsNORMALS | swRayPtsOpts_e.swRayPtsOptsENTRY_EXIT), (double).0000001, (double).0000001); if (numIntersectionsFound == 0) { return(0); } double[] horrifyingReturn = (double[])swDoc.GetRayIntersectionsPoints(); int lengthOfOneReturn = 9; double[] intersectionPoint = new double[3]; for (int i = 0; i < numIntersectionsFound; i++) { byte intersectionType = (byte)horrifyingReturn[i * lengthOfOneReturn + 2]; if ((intersectionType & (byte)swRayPtsResults_e.swRayPtsResultsENTER) == 0) { // we need it to be just entry rays continue; } double x = horrifyingReturn[i * lengthOfOneReturn + 3]; double y = horrifyingReturn[i * lengthOfOneReturn + 4]; double z = horrifyingReturn[i * lengthOfOneReturn + 5]; // check to see if the distance along the ray is shorter? we want the shortest distance! intersectionPoint[0] = x; intersectionPoint[1] = y; intersectionPoint[2] = z; break; } return(Utilities.distanceFormula(intersectionPoint, dataTip)); }