我需要相对于三角形平移 3d 点,就好像三角形在其他地方一样
我不久前在 Twitter 上发布了这篇文章,但看到我的追随者似乎都不是数学/编程天才,我也将在这里试试运气。我来到这里是因为我发现这个可能包含我的解决方案的一部分。
我在以下 pdf 文档中描述了我的问题,其中包含我的图片试图实现。
为了提供更多细节,我将十二面体的五边形(12 个五边形)分成三角形(每个五边形 5 个,总共 60 个三角形),然后收集与每个三角形相关的一组数据点。
这个想法是为每个单独的三角形生成地形网格。 为此,数据必须以 32K x 32K 的正方形(idTech4 Megatexture)平面表示,
我隐约听说过变换矩阵,如果设置正确,可以实现通过它们传递所有数据点以获取它们的技巧出现在正确的地方。
我查看了这个源代码这里但我不明白我是如何'我应该从那里获取点和/或从那里获取点,更不用说如何进行设置,以便我可以依次呈现每个点并获取结果点。
我很快就找到了属于右后角的点。我所有的 3D 点最初都存储在纬度/经度对中。我这样检索 3D 向量:
coord getcoord(point* p)
{
coord c;
c.x=cos(p->lat*pi/180.l) * cos(p->lon*pi/180.l);
c.y=cos(p->lat*pi/180.l) * sin(p->lon*pi/180.l);
c.z=sin(p->lat*pi/180.l);
return c;
};
我的想法是,如果我能找到三角形的中心,并发现如何偏移角度,以便从球体中心到三角形中间的向量移动到 90N,那么我的点如果我沿着相同的角度旋转它们,它们就已经在正确的平面上了。如果我将它们全部转换为 3d 并从 y 中减去半径,它们也将位于正确的 y 位置。
然后我需要做的就是旋转、缩放和移动到最终位置。
三角形有几种“中心”,我认为我需要的是与三角形的角等距的“中心”(圆周中心?)
但是可能有一种更简单的方法来解决整个问题,所以当我继续我自己的研究,也许你们中的一些人可以帮助我指明正确的方向。
看起来好像一些样本数据是有序的,这里有一些 obj 文件格式的三角形:
v 0.000000 0.000000 3396.000000
v 2061.582356 0.000000 2698.646733
v 637.063983 1960.681333 2698.646733
f 1 2 3
另一个:
v -938.631230 2888.810129 1518.737455
v 637.063983 1960.681333 2698.646733
v 1030.791271 3172.449325 637.064076
f 1 2 3
您会注意到每个点距 0,0,0 的距离为 3396 我提到“在球体上”,意思是远离球体中心的面是转换为正方形时需要成为“顶部”的面。
理论上,所有这些三角形实际上应该具有相同的大小,但由于生成它们的数学中的舍入误差,这可能不完全正确。
如果我没记错的话,我已经采取措施确保您在这里看到的第一个点始终是最长边界对面的点,因此它应该位于最左边的角落(测试上述 2 个样本证实了这一点,但是无论如何我正在测量只是为了确定) 理论上,离开该点的两条腿也应该具有相同的长度,但舍入误差可能会稍微抵消这一点。
如果我做得正确的话,那么较长的边是 2 个较短边的 1,113587 倍。假设它们是相同的,然后在 Excel 中进行一些目标搜索,我可以推断出最终的点,假设我只是平移这个三角形,应该如下所示:
v 16384.000000 0.000000 16384.000000
v -16384.000000 0.000000 9916.165306
v 9916.165306 0.000000 -16384.000000
f 1 2 3
所以我需要设置矩阵来进行此转换,最好使用 4x4 矩阵如下所述。
I posted this on twitter a while ago but seeing how none of my followers appears to be a math/programming genius, I'll try my luck here as well. I got here because I found this which might contain part of my solution.
I described my problem in the following pdf document, containing a picture of what I'm trying to achieve.
To give some more details, I divided the pentagon's of a dodecahedron (12 pentagons) into triangles (5/pentagon, 60 triangles in total), then collected a set of data points relative to each of these triangles.
The idea is to generate terrain meshes for each individual triangle.
To do so, the data must be represented flat, in a 32K x 32K square (idTech4 Megatexture)
I have vaguely heard of transformation matrices, which when set up properly, could do the trick of passing all the data points trough them to have them show up in the right place.
I looked at this source code here but I don't understand how I'm supposed to get the points in and/or out of there, not to mention how to do the setup so I can present each point in turn and get the result point back.
I got as fas as identifying the point that belongs in the back right corner. All my 3D points are originally stored in latitude / longitude pairs. I retrieve the 3D vectors this way:
coord getcoord(point* p)
{
coord c;
c.x=cos(p->lat*pi/180.l) * cos(p->lon*pi/180.l);
c.y=cos(p->lat*pi/180.l) * sin(p->lon*pi/180.l);
c.z=sin(p->lat*pi/180.l);
return c;
};
My thought is that if I can find the center of my triangle, and discover how to offset my angles so the vector from the center of my sphere to the middle of the triangle moves to 90N then my points would already be in the right plane if I rotated them all along the same angles. If I then convert them all to 3d and subtracti the radius from y, they'll be at the correct y position as well.
Then all I'd need to do is the rotation, the scaling, and the moving to the final position.
There are several kinds of 'centers' for a triangle, I think the one I need is the one that is equidistant to the corners of the triangle (Circumcenter?)
But then there might be an easier approach to the whole problem so while I continue my own research, perhaps some of you can help pointing me in the right direction.
It appears as if some sample data is in order, here are a few of these triangles in obj file format:
v 0.000000 0.000000 3396.000000
v 2061.582356 0.000000 2698.646733
v 637.063983 1960.681333 2698.646733
f 1 2 3
And another:
v -938.631230 2888.810129 1518.737455
v 637.063983 1960.681333 2698.646733
v 1030.791271 3172.449325 637.064076
f 1 2 3
You will notice that each point is at a distance of 3396 from 0,0,0
I mentioned 'on the sphere' meaning that the face away from the center of the sphere is the face that needs to become the 'top' when translated into the square.
Theoretically all these triangles should in fact have identical sizes, but due to rounding errors in the math that generated them, this might not be entirely true.
If I'm not mistaken I already took measures to ensure that the first point you see here is always the one opposite the longest border, so it's the one that should go in the far left corner (testing the above 2 samples confirms this, but I'm measuring anyway just to be sure)
Both legs leading away from this point should theoretically have the same length as well, but again rounding errors might slightly offset that.
If I've done it correctly then the longer side is 1,113587 times longer than the 2 shorter sides. Assuming those are identical, then doing some goal seeking in excel, I can deduct that the final points, assuming I was just translating this triangle, should look like:
v 16384.000000 0.000000 16384.000000
v -16384.000000 0.000000 9916.165306
v 9916.165306 0.000000 -16384.000000
f 1 2 3
So I need to setup the matrix to do this transformation, preferably using the 4x4 matrix as explained below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用变换矩阵。 3d 变换矩阵是一个 4x4 的数据结构,它描述平移和旋转(也可能是缩放)。一旦你有了一个矩阵,你就可以像这样变换一个点
这将通过矩阵 m 变换 3D 点 pt 。如果您现在了解一点矩阵数学,您会发现我只是将原点作为向量与矩阵相乘(如果它是倾斜矩阵,则进行一点归一化。)矩阵可以相乘以形成复杂的变换,因此它们非常有用。
有关制作矩阵的详细信息,建议阅读此链接。
http://en.wikipedia.org/wiki/Transformation_matrix
I would recommend using transform matrices. The 3d transform matrix is a 4x4 data structure which describes a translation and rotation (and possibly a scale). Once you have a matrix you can transform a point like so
This will transform the 3D point pt by the matrix m. If you now a little matrix math you'll see i'm just multiplying my origin point as a vector against the matrix (and doing a little normalization if it is a skew matrix.) Matrices can be multiplied together to form complicated transformations so they are very useful.
For details on making matrices suggest reading this link.
http://en.wikipedia.org/wiki/Transformation_matrix