public void AddEquation(ExpVector v) { sourceEquations.Add(v.x); sourceEquations.Add(v.y); sourceEquations.Add(v.z); isDirty = true; }
public static ExpVector ProjectPointToLine(ExpVector p, ExpVector l0, ExpVector l1) { var d = l1 - l0; var t = Dot(d, p - l0) / Dot(d, d); return(l0 + d * t); }
public static ExpVector NormalAtInPlane(this IEntity self, Exp t, IPlane plane) { if (self.plane != null) { var tang = self.TangentAt(t); if (tang == null) { return(null); } var n = ExpVector.Cross(tang, Vector3.forward); if (plane == self.plane) { return(n); } return(plane.DirToFrom(n, self.plane)); } Param p = new Param("pOn"); var pt = self.PointOn(p); var result = new ExpVector(pt.x.Deriv(p).Deriv(p), pt.y.Deriv(p).Deriv(p), pt.z.Deriv(p).Deriv(p)); result.x.Substitute(p, t); result.y.Substitute(p, t); result.z.Substitute(p, t); if (plane == null) { return(result); } return(plane.DirToPlane(result)); }
ExpVector[] GetPointsExp(IPlane plane) { var p = new ExpVector[4]; if (HasEntitiesOfType(IEntityType.Point, 4)) { for (int i = 0; i < 4; i++) { p[i] = GetEntityOfType(IEntityType.Point, i).GetPointAtInPlane(0, plane); } } else if (HasEntitiesOfType(IEntityType.Line, 2)) { var l0 = GetEntityOfType(IEntityType.Line, 0); p[0] = l0.GetPointAtInPlane(0, plane); p[1] = l0.GetPointAtInPlane(1, plane); var l1 = GetEntityOfType(IEntityType.Line, 1); p[2] = l1.GetPointAtInPlane(0, plane); p[3] = l1.GetPointAtInPlane(1, plane); } else if (HasEntitiesOfType(IEntityType.Arc, 1)) { var arc = GetEntityOfType(IEntityType.Arc, 0); p[0] = arc.GetPointAtInPlane(0, plane); p[1] = arc.GetPointAtInPlane(2, plane); p[2] = arc.GetPointAtInPlane(2, plane); p[3] = arc.GetPointAtInPlane(1, plane); } return(p); }
public static ExpVector Cross(ExpVector a, ExpVector b) { return(new ExpVector( a.y * b.z - b.y * a.z, a.z * b.x - b.z * a.x, a.x * b.y - b.x * a.y )); }
public static ExpVector FromPlane(this IPlane plane, ExpVector pt) { if (plane == null) { return(pt); } return(plane.o + (ExpVector)plane.u * pt.x + (ExpVector)plane.v * pt.y + (ExpVector)plane.n * pt.z); }
public static ExpVector DirFromPlane(this IPlane plane, ExpVector dir) { if (plane == null) { return(dir); } return((ExpVector)plane.u * dir.x + (ExpVector)plane.v * dir.y + (ExpVector)plane.n * dir.z); }
public static Exp pointLineDistance(ExpVector p, ExpVector p0, ExpVector p1, bool is3d) { if (is3d) { var d = p0 - p1; return(ExpVector.Cross(d, p0 - p).Magnitude() / d.Magnitude()); } return(((p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y + p0.x * p1.y - p1.x * p0.y) / Exp.Sqrt(Exp.Sqr(p1.x - p0.x) + Exp.Sqr(p1.y - p0.y))); }
public static ExpVector rotateDir2d(ExpVector rv, Exp angle) { var cos = Exp.Cos(angle); var sin = Exp.Sin(angle); return(new ExpVector( cos * rv.x - sin * rv.y, sin * rv.x + cos * rv.y, rv.z )); }
public ExpVector TangentAt(Exp t) { Param p = new Param("pOn"); var pt = PointOn(p); var result = new ExpVector(pt.x.Deriv(p), pt.y.Deriv(p), pt.z.Deriv(p)); result.x.Substitute(p, t); result.y.Substitute(p, t); result.z.Substitute(p, t); return(result); }
public static ExpVector OffsetTangentAt(this IEntity e, Exp t, Exp offset) { Param p = new Param("pOn"); var pt = e.OffsetAt(p, offset); var result = new ExpVector(pt.x.Deriv(p), pt.y.Deriv(p), pt.z.Deriv(p)); result.x.Substitute(p, t); result.y.Substitute(p, t); result.z.Substitute(p, t); return(result); }
public static Exp angle2d(ExpVector d0, ExpVector d1, bool angle360 = false) { Exp nu = d1.x * d0.x + d1.y * d0.y; Exp nv = d0.x * d1.y - d0.y * d1.x; if (angle360) { return(Math.PI - Exp.Atan2(nv, -nu)); } return(Exp.Atan2(nv, nu)); }
public ExpVector GetExpClone(Exp t) { var e = new ExpVector(exp.x.DeepClone(), exp.y.DeepClone(), 0.0); if (t != null) { e.x.Substitute(this.t, t); e.y.Substitute(this.t, t); e.z.Substitute(this.t, t); } return(e); }
public static ExpVector RotateAround(ExpVector point, ExpVector axis, ExpVector origin, Exp angle) { var a = axis.Normalized(); var c = Exp.Cos(angle); var s = Exp.Sin(angle); var u = new ExpVector(c + (1.0 - c) * a.x * a.x, (1.0 - c) * a.y * a.x + s * a.z, (1 - c) * a.z * a.x - s * a.y); var v = new ExpVector((1.0 - c) * a.x * a.y - s * a.z, c + (1.0 - c) * a.y * a.y, (1.0 - c) * a.z * a.y + s * a.x); var n = new ExpVector((1.0 - c) * a.x * a.z + s * a.y, (1.0 - c) * a.y * a.z - s * a.x, c + (1 - c) * a.z * a.z); var p = point - origin; return(p.x * u + p.y * v + p.z * n + origin); }
public static ExpVector DirToPlane(this IPlane plane, ExpVector dir) { if (plane == null) { return(dir); } ExpVector result = new ExpVector(0, 0, 0); result.x = ExpVector.Dot(dir, plane.u); result.y = ExpVector.Dot(dir, plane.v); result.z = ExpVector.Dot(dir, plane.n); return(result); }
public void GenerateEquations(EquationSystem sys) { sys.AddParameters(parameters); sys.AddEquation(u.Magnitude() - 1.0); sys.AddEquation(v.Magnitude() - 1.0); var cross = ExpVector.Cross(u, v); var dot = ExpVector.Dot(u, v); sys.AddEquation(Exp.Atan2(cross.Magnitude(), dot) - Math.PI / 2); sys.AddEquation(n - ExpVector.Cross(u, v)); }
public Vector3 PointOn(float a, Vector3 point) { var ax = GetAxis().Eval(); var axn = ax.normalized; var t = a / (2.0f * Mathf.PI); var o = GetOrigin().Eval(); var prj = ExpVector.ProjectPointToLine(point, o, o + ax); var ra = Mathf.Atan2((float)step.value / 4.0f, (point - prj).magnitude); var res = ExpVector.RotateAround(point, point - prj, o, ra); res = ExpVector.RotateAround(res, ax, o, a); return(res + axn * t * (float)step.value); }
public ExpBasis2d() { px = new Param("ux", 0.0); py = new Param("uy", 0.0); ux = new Param("ux", 1.0); uy = new Param("uy", 0.0); vx = new Param("vx", 0.0); vy = new Param("vy", 1.0); p = new ExpVector(px, py, 0.0); u = new ExpVector(ux, uy, 0.0); v = new ExpVector(vx, vy, 0.0); }
public ExpVector PointOn(Exp a, ExpVector point) { /* * var ax = GetAxis(); * var axn = ax.Normalized(); * var o = GetOrigin(); * var prj = ExpVector.ProjectPointToLine(point, o, o + ax); * var ra = Exp.Atan2(new Exp(step) / 4.0, (point - prj).Magnitude()); */ var ax = GetAxis().Eval(); var axn = ax.normalized; var o = GetOrigin().Eval(); var prj = ExpVector.ProjectPointToLine(point.Eval(), o, o + ax); var t = a / (2.0 * Mathf.PI); var ra = Exp.Atan2(new Exp(step) / 4.0, (point.Eval() - prj).magnitude); var res = ExpVector.RotateAround(point, point - prj, o, ra); res = ExpVector.RotateAround(res, ax, o, a); return(res + (ExpVector)axn * t * step); }
public ExpBasis() { px = new Param("ux", 0.0); py = new Param("uy", 0.0); pz = new Param("uz", 0.0); ux = new Param("ux", 1.0); uy = new Param("uy", 0.0); uz = new Param("uz", 0.0); vx = new Param("vx", 0.0); vy = new Param("vy", 1.0); vz = new Param("vz", 0.0); nx = new Param("nx", 0.0); ny = new Param("ny", 0.0); nz = new Param("nz", 1.0); p = new ExpVector(px, py, pz); u = new ExpVector(ux, uy, uz); v = new ExpVector(vx, vy, vz); n = new ExpVector(nx, ny, nz); }
public static Exp PointLineDistance(ExpVector point, ExpVector l0, ExpVector l1) { var d = l0 - l1; return(Cross(d, l0 - point).Magnitude() / d.Magnitude()); }
/* * protected override void OnDraw(LineCanvas canvas) { * var l0 = GetEntityOfType(IEntityType.Line, 0); * var l1 = GetEntityOfType(IEntityType.Line, 1); * DrawStroke(canvas, l0, 0); * DrawStroke(canvas, l1, 1); * if(DetailEditor.instance.hovered == this) { * DrawReferenceLink(canvas, Camera.main); * } * } */ protected override void OnDraw(LineCanvas canvas) { var line0 = GetEntityOfType(IEntityType.Line, 0); var line1 = GetEntityOfType(IEntityType.Line, 1); ExpVector p0 = null; ExpVector p1 = null; ExpVector p2 = null; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (line0.GetPointAtInPlane(i, null).ValuesEquals(line1.GetPointAtInPlane(j, null), 1e-6)) { p0 = line0.GetPointAtInPlane(i, null); p1 = line0.GetPointAtInPlane((i + 1) % 2, null); p2 = line1.GetPointAtInPlane((j + 1) % 2, null); } } } float pix = getPixelSize(); if (p0 != null) { Vector3 p = p0.Eval(); Vector3 dir1 = p1.Eval() - p; Vector3 dir2 = p2.Eval() - p; dir1 = dir1.normalized * pix * 13f; dir2 = dir2.normalized * pix * 13f; Vector3 corner = p + dir1 + dir2; canvas.DrawLine(p + dir1, corner); canvas.DrawLine(p + dir2, corner); ref_points[0] = ref_points[1] = sketch.plane.ToPlane(corner); } else { for (int i = 0; i < 2; i++) { var line = GetEntityOfType(IEntityType.Line, i); Vector3 a = line.GetPointAtInPlane(0, null).Eval(); Vector3 b = line.GetPointAtInPlane(1, null).Eval(); Vector3 dir = normalize(a - b); Vector3 center = a + (b - a) * 0.5f; Vector3 plane = getVisualPlaneDir(Camera.main.transform.forward); Vector3 perp = normalize(Vector3.Cross(dir, plane)); Vector3 p = center - perp * pix * 8.0f; canvas.DrawLine(p - dir * pix * 8.0f, p + dir * pix * 8.0f); canvas.DrawLine(p, p - perp * pix * 13.0f); ref_points[i] = sketch.plane.ToPlane(p - perp * pix * 6.0f); } if (DetailEditor.instance.hovered == this) { DrawReferenceLink(canvas, Camera.main); } } }
public ExpVector TransformDirection(ExpVector dir) { return(dir.x * u + dir.y * v + dir.z * n); }
public ExpVector TransformPosition(ExpVector pos) { return(pos.x * u + pos.y * v + pos.z * n + p); }
public static Solid CreateSolidRevolve(List <List <Entity> > entitiyLoops, float angle, float helixStep, Vector3 axis, Vector3 origin, float angleStep, UnityEngine.Matrix4x4 tf, IdPath feature) { var ids = new List <List <Id> >(); var polygons = Sketch.GetPolygons(entitiyLoops, ref ids); bool isHelix = (Math.Abs(helixStep) > 1e-6); if (!isHelix && Mathf.Abs(angle) > 360f) { angle = Mathf.Sign(angle) * 360f; } bool inversed = angle < 0f; int subdiv = (int)Mathf.Ceil(Math.Abs(angle) / angleStep); var drot = UnityEngine.Matrix4x4.Translate(origin) * UnityEngine.Matrix4x4.Rotate(Quaternion.AngleAxis(angle / subdiv, axis)) * UnityEngine.Matrix4x4.Translate(-origin); Func <float, Vector3, Vector3> PointOn = (float a, Vector3 point) => { var ax = axis; var axn = axis.normalized; var t = a / 360.0f; var o = origin; var prj = ExpVector.ProjectPointToLine(point, o, o + ax); var ra = Mathf.Atan2(helixStep / 4.0f, (point - prj).magnitude); var res = ExpVector.RotateAround(point, point - prj, o, ra); res = ExpVector.RotateAround(res, ax, o, a * Mathf.PI / 180.0f); return(res + axn * t * helixStep); }; var polys = new List <Polygon>(); Func <Vector3, Vector3> TF = v => tf.MultiplyPoint(v); for (int pi = 0; pi < polygons.Count; pi++) { var p = polygons[pi]; var pid = ids[pi]; var pv = new List <Vector3>(p); var triangles = Triangulation.Triangulate(pv); IdPath polyId = feature.With(new Id(-1)); bool invComp = true; if (Math.Abs(Math.Abs(angle) - 360f) > 1e-6 || isHelix) { float a = 0f; for (int side = 0; side < 2; side++) { for (int i = 0; i < triangles.Count / 3; i++) { var polygonVertices = new List <Vertex>(); for (int j = 0; j < 3; j++) { polygonVertices.Add(PointOn(a, TF(triangles[i * 3 + j])).ToVertex()); } if (inversed == invComp) { polygonVertices.Reverse(); } polys.Add(new Polygon(polygonVertices, polyId)); } polyId = feature.With(new Id(-2)); invComp = false; a = angle; } } Dictionary <Id, IdPath> paths = new Dictionary <Id, IdPath>(); for (int i = 0; i < p.Count; i++) { float a = 0f; float da = angle / subdiv; for (int j = 0; j < subdiv; j++) { var polygonVertices = new List <Vertex>(); polygonVertices.Add(PointOn(a, TF(p[(i + 1) % p.Count])).ToVertex()); polygonVertices.Add(PointOn(a + da, TF(p[(i + 1) % p.Count])).ToVertex()); polygonVertices.Add(PointOn(a + da, TF(p[i])).ToVertex()); polygonVertices.Add(PointOn(a, TF(p[i])).ToVertex()); a += da; if (!inversed) { polygonVertices.Reverse(); } IdPath curPath = null; if (!paths.ContainsKey(pid[i])) { curPath = feature.With(pid[i]); paths.Add(pid[i], curPath); } else { curPath = paths[pid[i]]; } if (isHelix) { var verts = new List <Vertex>(); verts.Add(polygonVertices[0]); verts.Add(polygonVertices[1]); verts.Add(polygonVertices[2]); polys.Add(new Polygon(verts, curPath)); verts = new List <Vertex>(); verts.Add(polygonVertices[0]); verts.Add(polygonVertices[2]); verts.Add(polygonVertices[3]); polys.Add(new Polygon(verts, curPath)); } else { polys.Add(new Polygon(polygonVertices, curPath)); } } } } return(Solid.FromPolygons(polys)); }
public static Exp Dot(ExpVector a, ExpVector b) { return(a.x * b.x + a.y * b.y + a.z * b.z); }
public static ExpVector DirToFrom(this IPlane to, ExpVector pt, IPlane from) { return(to.DirToPlane(from.DirFromPlane(pt))); }
protected override void OnUpdateDirty() { canvas.SetStyle("entities"); var sk = (source as SketchFeature).GetSketch(); bool axisDirectionFound = false; var ax = axis.GetDirectionInPlane(null).Eval(); var o = GetOrigin(null).Eval(); foreach (var e in sk.entityList) { if (e.type != IEntityType.Point) { continue; } var pos = e.PointExpInPlane(null).Eval(); var prj = ExpVector.ProjectPointToLine(pos, o, o + ax); if ((prj - pos).magnitude < 1e-6) { continue; } var ax1 = Vector3.Cross(sketch.plane.n, pos - prj); shouldInvertAxis = (Vector3.Dot(ax, ax1) > 0f); axisDirectionFound = true; break; } // probably, some curved enity, so try actual curve points if (!axisDirectionFound) { foreach (var e in sk.entityList) { if (e.type == IEntityType.Point) { continue; } foreach (var pos in e.SegmentsInPlane(null)) { var prj = ExpVector.ProjectPointToLine(pos, o, o + ax); if ((prj - pos).magnitude < 1e-6) { continue; } var ax1 = Vector3.Cross(sketch.plane.n, pos - prj); shouldInvertAxis = (Vector3.Dot(ax, ax1) > 0f); axisDirectionFound = true; break; } } } foreach (var e in sk.entityList.OfType <PointEntity>()) { var ext = new RevolvedPointEntity(e, this); canvas.DrawSegments(ext.segments); } foreach (var e in sk.entityList) { var ext = new RevolvedEntity(e, this, 0); canvas.DrawSegments(ext.segments); ext = new RevolvedEntity(e, this, 1); canvas.DrawSegments(ext.segments); } }
public ExpVector Transform(ExpVector point, long i) { return(PointOn(new Exp(angle) * i, point)); }
public bool ValuesEquals(ExpVector o, double eps) { return(Math.Abs(x.Eval() - o.x.Eval()) < eps && Math.Abs(y.Eval() - o.y.Eval()) < eps && Math.Abs(z.Eval() - o.z.Eval()) < eps); }