English 中文(简体)
Calculate the angle between two triangles in CUDA
原标题:

I wanted to calculate the angle between two triangles in 3D space. The two triangles will always share exactly two points. e.g.

Triangle 1:

Point1 (x1, y1, z1),  
Point2 (x2, y2, z2),   
Point3 (x3, y3, z3).  

Triangle 2:

Point1 (x1, y1, z1),  
Point2 (x2, y2, z2),  
Point4 (x4, y4, z4).

Is there a way to calculate the angle between them efficiently in CUDA?

最佳回答

For each plane, you need to construct it s normal vector (perpendicular to all lines in that plane). The simple way to do that is to take the cross-product of two non-parallel lines in the triangle. (ex (P3-P1) X (P2-P1) and (P4-P1) X (P2-P1).

Normalize those.

The dot product of those two direction vectors gives you the cosine of the angle.

The tricky bit is to watch out for degenerate triangles! If all 3 points defining either triangle are colinear, (that triangle is just a line) then what you re asking for is undefined, and the cross-product will divide by zero. You need to decide what you re going to do in that case.

Since you re trying to do this on a GPU, you ll ideally want to write this function without any branches, if you re concerned about efficiency. That would mean instead of testing for degenerate triangles with an if clause, you should try and do it with a ternary A ? B : C

问题回答

The angle between the triangles is the same as the angle between the planes defined by the three points of each triangle.

Since both Point 1 or Point 2 lie in both planes, figure out the direction cosines from one of those points to Point 3, and then to Point 4. Then, the cosine of the angle between these two lines is just the sum of the products of the corresponding direction cosines.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签