NCF参数化建筑论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 18636|回复: 16
打印 上一主题 下一主题

[工作记录] 管中窥豹(李思(本版BZ)前辈部分代码)

[复制链接]
跳转到指定楼层
1m
发表于 2010-11-14 09:45:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有些人可能对李思前辈不了解 (其实我这人并不喜欢恭维别人)还是直接看内容吧 这里公布一部分关于矩阵和向量的代码 关于这部分内容的算法 之前我发过更加完整和健壮的代码 李思前辈应该看过了 希望这里能够原谅我公布您的部分东西 主要是让大家了解您 class VectorMath { public static Point3d computeNormal(Point3d vect1, Point3d vect2) { //compute cross product of two vectors //i.e.:axb=(1,2,3) x (4,5,6) =( (2x6 -3x5), (-1x6+3x4),(1x5-2x4)) =(-3,6-3) double x, y, z; double n1, n2, n3, n4, n5, n6; n1 = vect1.X; n2 = vect1.Y; n3 = vect1.Z; n4 = vect2.X; n5 = vect2.Y; n6 = vect2.Z; x = (n2 * n6) - (n3 * n5); y = (-1 * n1 * n6) + (n3 * n4); z = (n1 * n5) - (n2 * n4); return makeUnitVector( new Point3d(x, y, z)); } public static Point3d getVector(Lined line) { double x, y, z; x = line.getPoint(1).X - line.getPoint(0).X; y = line.getPoint(1).Y - line.getPoint(0).Y; z = line.getPoint(1).Z - line.getPoint(0).Z; return new Point3d(x, y, z); } public static double getAngleBetweenVectors(Point3d vect1, Point3d vect2) { double dv1, dv2; double angle; dv1 = System.Math.Sqrt(System.Math.Pow(vect1.X, 2) + System.Math.Pow(vect1.Y, 2) + System.Math.Pow(vect1.Z, 2)); dv2 = System.Math.Sqrt(System.Math.Pow(vect2.X, 2) + System.Math.Pow(vect2.Y, 2) + System.Math.Pow(vect2.Z, 2)); double cos = (double)((vect1.X * vect2.X + vect1.Y * vect2.X + vect1.Z * vect2.Z) / (dv1 * dv2)); angle = System.Math.Acos(cos); angle = ConvertRadianToDregree(angle); return angle; } public static Point3d makeUnitVector(Point3d p) { double length = DistanceBetweenPoints(new Point3d(0, 0, 0), p); return new Point3d(p.X / length, p.Y / length, p.Z / length); } public static Point3d makeVector(Point3d basePoint, Point3d projectPoint) { return projectPoint - basePoint; } public static double DistanceBetweenPoints(Point3d p1, Point3d p2) { return System.Math.Sqrt(System.Math.Pow(p2.X - p1.X, 2) + System.Math.Pow(p2.Y - p1.Y, 2) + System.Math.Pow(p2.Z - p1.Z, 2)); } public static double getAngleBetweenPoints(Point3d p1, Point3d p2) { double dx, dy; dx = p2.X - p1.X; dy = p2.Y - p1.Y; double degree; degree = System.Math.Atan(System.Math.Abs(dy / dx)); if (dx >= 0 && dy >= 0) return ConvertRadianToDregree(degree); else if (dx >= 0 && dy < 0) { //Console.WriteLine("area 4"); return 360 - ConvertRadianToDregree(degree);//v } else if (dx < 0 && dy >= 0) { //Console.WriteLine("area 2"); return 180 - ConvertRadianToDregree(degree); } else if (dx < 0 && dy < 0) { //Console.WriteLine("area 3"); return 180 + ConvertRadianToDregree(degree); } return 0; } public static Point3d rotatePointAroundVector(Point3d point, Point3d axis, double degree) { Matrix16 matrix = Matrix16.makeMatrixRotAroundVector(axis, degree); return matrix.applyTransformationToPoint(point); } public static Point3d PointDistAngle2d(double a, double d) { double[] pair = new double[2]; double signX = 1; double signY = 1; a = a % 360; if (Math.Abs(a) > 90 && Math.Abs(a) < 270) signX = -1; a = ConvertDegreeToRadian(a); pair[1] = signY * Math.Sin(a) * d; pair[0] = signX * Math.Sqrt(Math.Pow(d, 2) - Math.Pow(pair[1], 2)); return new Point3d(pair[0], pair[1],0); } public static double dotProduct3d(Point3d p1, Point3d p2) { return (p1.X * p2.X) + (p1.Y * p2.Y) + (p1.Z * p2.Z); } public static double ConvertDegreeToRadian(double d) { return (d * (Math.PI / 180)); } public static double ConvertRadianToDregree(double d) { return (180 * d / Math.PI); } public static Point3d mouseOnPlane(int mouseX, int mouseY, Plane p) { Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glLoadIdentity(); Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glLoadIdentity(); Gl.glPushMatrix(); //glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); //glGetDoublev(GL_PROJECTION_MATRIX, projectMatrix); //glGetIntegerv(GL_VIEWPORT, viewport); //gluUnProject(mouseLocation.x, mouseLocation.y, 1.0, modelMatrix, projectMatrix, viewport, &objSpaceCoords[0], &objSpaceCoords[1], &objSpaceCoords[2]); int mX = mouseX; int mY = mouseY; double[] modelMatrix=new double[16]; double[] projectMatrix=new double[16]; int[] viewport=new int[4]; double x1,y1,z1,x2,y2,z2; Point3d pt1, pt2; Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX,modelMatrix); Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX,projectMatrix); Gl.glGetIntegerv(Gl.GL_VIEWPORT,viewport); mY = viewport[3] - mY; //Console.WriteLine("viewport=" + viewport[0].ToString() + "," + viewport[1].ToString() + "," + viewport[2].ToString() + "," + viewport[3].ToString()); Glu.gluUnProject(mX, mY, 0f, modelMatrix, projectMatrix, viewport, out x1, out y1, out z1); Glu.gluUnProject(mX, mY, 1f, modelMatrix, projectMatrix, viewport, out x2, out y2, out z2); pt1 = new Point3d(x1, y1, z1); Console.WriteLine("pt1=" + pt1.CeToString()); pt2 = new Point3d(x2, y2, z2); Console.WriteLine("pt2=" + pt1.CeToString()); //Console.WriteLine("unprojected result "+pt1.CeToString() + "|" + pt2.CeToString()); //refer to this address for explaination of the math //http://softsurfer.com/Archive/algorithm_0104/algorithm_0104B.htm Gl.glPopMatrix(); return IntersectVectorAndPlane(pt1, pt2, p); } public static Point3d IntersectVectorAndPlane(Point3d p0, Point3d p1, Plane plane) { Point3d n=plane.getNormal(); p1 = (makeUnitVector(p1 - p0)); double a = dotProduct3d(n, (plane.Origin - p0)); double b = dotProduct3d(n, p1); double s = a / b; p1.scale(s); //Console.WriteLine("return_>" + p1.CeToString()); p1=new Point3d( p1 + p0); //Console.WriteLine("return_>" + p1.CeToString()); Console.WriteLine("unproject reult=" + p1.CeToString()); return new Point3d(p1); 这里要说明的是mouseonplane函数有比较完整java 代码 这里C#引用的是Tao

评分

参与人数 1照度 +50 收起 理由
skywoolf + 50 感谢分享

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
2m
 楼主| 发表于 2010-11-14 09:47:14 | 只看该作者
本帖最后由 panhao1 于 2010-11-14 09:48 编辑 再一次强调 更好用的代码在我那个 processing犀牛化的类 的帖子上 下面是矩阵的 关于4*4矩阵平移 旋转 缩放的东西 class Matrix16 { public double[] grid = new double[16]; public Matrix16() { grid = new double[16]{ 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,0}; } public Matrix16(double[] f) { grid = f; } public Point3d applyTransformationToPoint(Point3d p) { double x, y, z; //multiply 1x3 matrix with 3x3 matrix x = p.X * get(0, 0) + p.Y * get(1, 0) + p.Z * get(2, 0); y = p.X * get(0, 1) + p.Y * get(1, 1) + p.Z * get(2, 1); z = p.X * get(0, 2) + p.Y * get(1, 2) + p.Z * get(2, 2); //translate x, y, z x += get(3, 0); y += get(3, 1); z += get(3, 2); return new Point3d(x, y, z); } public void PrintConsole() { Console.WriteLine(">>"); Console.WriteLine(get(0, 0).ToString() + "/t," + get(1, 0).ToString() + "/t," + get(2, 0).ToString() + "/t," + get(3, 0).ToString()); Console.WriteLine(get(0, 1).ToString() + "/t," + get(1, 1).ToString() + "/t," + get(2, 1).ToString() + "/t," + get(3, 1).ToString()); Console.WriteLine(get(0, 2).ToString() + "/t," + get(1, 2).ToString() + "/t," + get(2, 2).ToString() + "/t," + get(3, 2).ToString()); Console.WriteLine(get(0, 3).ToString() + "/t," + get(1, 3).ToString() + "/t," + get(2, 3).ToString() + "/t," + get(3, 3).ToString()); } public static Matrix16 makeMatrixScale(double s) { return new Matrix16(new double[16]{ s,0,0,0, 0,s,0,0, 0,0,s,0, 0,0,0,1 }); } public static Matrix16 makeMatrixRotAroundVector(Point3d vector, double degree) { double d = degree * ((System.Math.PI * 2) / 360.0); Point3d unitAxis = vector.getUnitVector(); double x = unitAxis.X; double y = unitAxis.Y; double z = unitAxis.Z; double xp = Math.Pow((double)x, 2) - 1; double yp = Math.Pow((double)y, 2) - 1; double zp = Math.Pow((double)z, 2) - 1; double c = Math.Cos(d); double s = Math.Sin(d); return new Matrix16(new double[16]{ 1+((1-c)*xp), ((1-c)*x*y)-(z*s), (1-c)*x*z+y*s, 0, (1-c)*x*y+z*s, 1+(1-c)*yp, (1-c)*y*z-x*s,0, (1-c)*x*z-y*s, (1-c)*y*z+x*s, 1+(1-c)*zp,0, 0,0,0,1}); } public static Matrix16 makeMatrixOriginatePoint(Point3d point) { double dx = -point.X; double dy = -point.Y; double dz = -point.Z; return new Matrix16(new double[16] { 1,0,0,dx, 0,1,0,dy, 0,0,1,dz, 0,0,0,1 }); } public static Matrix16 makeMatrixTranslation(double dx, double dy, double dz) { return new Matrix16(new double[16]{ 1,0,0,dx, 0,1,0,dy, 0,0,1,dz, 0,0,0,1 }); } // the the double value at x,y postion of the matrix 0-3 public double get(int x, int y) { return grid[(y * 4) + x]; } public void set(int x, int y, double value) { grid[(y * 4) + x] = value; } }
3m
 楼主| 发表于 2010-11-14 09:57:19 | 只看该作者
其实我们更多的是用processing做3d图形 因为processing网上可以下载到的 包 更多 这是前辈很久前写的C#的代码 基本上是原创内容 大家要是对图形编程感兴趣 最好是会写一点代码 (代码种类不限) 或是OPEN CV有研究的可以加入40391970 寒假期间在广州做workshop活动,吃住不是问题,希望大家踊跃参加
4m
发表于 2010-11-14 15:45:11 | 只看该作者
为MIT李思前辈顶起!
5m
发表于 2010-11-14 21:11:40 | 只看该作者
好猛!! 收下來慢慢看,喜歡矩陣平移縮放那部份的解釋。
6m
发表于 2010-11-14 21:47:44 | 只看该作者
牛人啊,前辈,我加了{:3_56:}
7m
发表于 2010-11-15 01:16:30 | 只看该作者
不太懂啊 学习下
8m
发表于 2010-11-15 01:23:20 | 只看该作者
不太懂啊 学习下
9m
发表于 2010-11-15 08:28:41 | 只看该作者
{:3_51:}支持了!学习下~
10m
发表于 2010-11-15 11:31:55 | 只看该作者
很不错啊,厉害~~
11m
发表于 2010-11-15 13:39:07 | 只看该作者
很有用啊,李思前辈数学学得真好
12m
发表于 2010-11-16 21:59:51 | 只看该作者
这个……完全看不懂啊,猛
13m
发表于 2011-3-1 17:50:13 | 只看该作者
谢谢分享,支持
14m
发表于 2011-3-1 17:50:29 | 只看该作者
谢谢分享,支持
15m
发表于 2011-7-3 08:43:41 | 只看该作者
这个可以看一看,向李思致敬~
16m
发表于 2014-10-6 23:22:27 | 只看该作者
谢谢楼主分享
17m
发表于 2014-10-19 06:52:39 | 只看该作者
主题是不错,只不过这么po代码没人会看懂啊

小黑屋|手机版|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 )    辽公网安备21021102000973号

GMT+8, 2024-11-22 10:24 , Processed in 0.068241 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表