http://www.mathworks.com/matlabcentral/newsreader/view_thread/315624
MATLAB Central Newsreader  Point in Cube Test
Feed for thread: Point in Cube Test
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 01 Jan 2012 16:50:09 +0000
Point in Cube Test
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315624#862566
Deepak
Hi,<br>
<br>
I have the following code for point in cube test. I have a list of point and i need to test each point if it is in a cube. <br>
<br>
clc<br>
clear all<br>
%cube vertices<br>
a = [0 0 0]'; b = [0 1 0]'; c = [1 1 0]'; d = [1 0 0]'; <br>
e = [0 0 1]'; f = [0 1 1]'; g = [1 1 1]'; h = [1 0 1]';<br>
x = [0 0 1 1 0 0 1 1]';<br>
y = [0 1 1 0 0 1 1 0]';<br>
z = [0 0 0 0 1 1 1 1]';<br>
k = convhull(x,y,z)<br>
%query point: [2 2 2]'<br>
x1 = [0 0 1 1 0 0 1 1 2]';<br>
y1 = [0 1 1 0 0 1 1 0 2]';<br>
z1 = [0 0 0 0 1 1 1 1 2]';<br>
l = convhull(x1,y1,z1)<br>
for i = 1:length(l(:,1))<br>
if l(i,:) ~= k(i,:)<br>
ans = ('not in cube')<br>
end<br>
end<br>
<br>
Will this work if the point is on the surface ? Also, is there a set value i can provide at the end of the for loop ? eg:<br>
<br>
If m = 1 point is in cube<br>
if m = 0 point is not in cube <br>
<br>
Thanks <br>

Sun, 01 Jan 2012 18:09:08 +0000
Re: Point in Cube Test
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315624#862579
Matt J
"Deepak " <deepak.tss@gmail.com> wrote in message <jdq2s1$c16$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> <br>
> I have the following code for point in cube test. I have a list of point and i need to test each point if it is in a cube. <br>
> <br>
> clc<br>
> clear all<br>
> %cube vertices<br>
> a = [0 0 0]'; b = [0 1 0]'; c = [1 1 0]'; d = [1 0 0]'; <br>
> e = [0 0 1]'; f = [0 1 1]'; g = [1 1 1]'; h = [1 0 1]';<br>
> x = [0 0 1 1 0 0 1 1]';<br>
> y = [0 1 1 0 0 1 1 0]';<br>
> z = [0 0 0 0 1 1 1 1]';<br>
> k = convhull(x,y,z)<br>
> %query point: [2 2 2]'<br>
> x1 = [0 0 1 1 0 0 1 1 2]';<br>
> y1 = [0 1 1 0 0 1 1 0 2]';<br>
> z1 = [0 0 0 0 1 1 1 1 2]';<br>
> l = convhull(x1,y1,z1)<br>
> for i = 1:length(l(:,1))<br>
> if l(i,:) ~= k(i,:)<br>
> ans = ('not in cube')<br>
> end<br>
> end<br>
> <br>
> Will this work if the point is on the surface ? <br>
<br>
<br>
It will work sometimes, but not reliably. There is no reliable test of whether a point is on the surface of a cube. The test will always be sensitive to numerical precision issues.<br>
Also, the method you're using is not a terribly good one. A more efficient way to test the inclusion of a point in any convex shape is to plug them into the inequalities defining the shape<br>
<br>
A*x<=b<br>
<br>
This test is highly vectorizable over multiple test points, and requires no looping. <br>
One way that you can get this representation is using this tool<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/30892representingpolyhedralconvexhullsbyverticesorinequalities">http://www.mathworks.com/matlabcentral/fileexchange/30892representingpolyhedralconvexhullsbyverticesorinequalities</a>

Mon, 02 Jan 2012 01:01:09 +0000
Re: Point in Cube Test
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315624#862588
Roger Stafford
"Deepak " <deepak.tss@gmail.com> wrote in message <jdq2s1$c16$1@newscl01ah.mathworks.com>...<br>
> I have the following code for point in cube test. I have a list of point and i need to test each point if it is in a cube. <br>
         <br>
With a polyhedron such as a cube which defines a convex set there is no need to call on 'convhull'. You would just get back the same points you started with. (By the way, for 3D points you should call on convhulln.)<br>
<br>
As Matt points out, for such convex volumes the test you need for determining whether a point lies inside the polyhedron is a set of inequalities, one for each face of the polyhedron. If for each face you can determine a vector, n, normal to the face and pointing inside the polyhedron, then an arbitrary point P lies inside the polyhedron if for every face you have dot(PQ,n) > 0 with Q being any one of the polygon vertices on the face.<br>
<br>
If the set of vertices defines a nonconvex volume, the problem is more difficult. Using the above defined inwardpointing face normals and surrounding polygons one can compute the signed solid angle projected onto each polygonal face by the given point. With an inside point these should all sum to a total solid angle of 4*pi and for an outside point it should have a zero sum. For a twodimensional version of this procedure see the thread at:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/315064">http://www.mathworks.com/matlabcentral/newsreader/view_thread/315064</a><br>
<br>
Roger Stafford