Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Point in Cube Test

Subject: Point in Cube Test

From: Deepak

Date: 1 Jan, 2012 16:50:09

Message: 1 of 3

Hi,

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.

clc
clear all
%cube vertices
a = [0 0 0]'; b = [0 1 0]'; c = [1 1 0]'; d = [1 0 0]';
e = [0 0 1]'; f = [0 1 1]'; g = [1 1 1]'; h = [1 0 1]';
x = [0 0 1 1 0 0 1 1]';
y = [0 1 1 0 0 1 1 0]';
z = [0 0 0 0 1 1 1 1]';
k = convhull(x,y,z)
%query point: [2 2 2]'
x1 = [0 0 1 1 0 0 1 1 2]';
y1 = [0 1 1 0 0 1 1 0 2]';
z1 = [0 0 0 0 1 1 1 1 2]';
l = convhull(x1,y1,z1)
for i = 1:length(l(:,1))
    if l(i,:) ~= k(i,:)
        ans = ('not in cube')
    end
end

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:

If m = 1 point is in cube
if m = 0 point is not in cube

Thanks
        

Subject: Point in Cube Test

From: Matt J

Date: 1 Jan, 2012 18:09:08

Message: 2 of 3

"Deepak " <deepak.tss@gmail.com> wrote in message <jdq2s1$c16$1@newscl01ah.mathworks.com>...
> Hi,
>
> 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.
>
> clc
> clear all
> %cube vertices
> a = [0 0 0]'; b = [0 1 0]'; c = [1 1 0]'; d = [1 0 0]';
> e = [0 0 1]'; f = [0 1 1]'; g = [1 1 1]'; h = [1 0 1]';
> x = [0 0 1 1 0 0 1 1]';
> y = [0 1 1 0 0 1 1 0]';
> z = [0 0 0 0 1 1 1 1]';
> k = convhull(x,y,z)
> %query point: [2 2 2]'
> x1 = [0 0 1 1 0 0 1 1 2]';
> y1 = [0 1 1 0 0 1 1 0 2]';
> z1 = [0 0 0 0 1 1 1 1 2]';
> l = convhull(x1,y1,z1)
> for i = 1:length(l(:,1))
> if l(i,:) ~= k(i,:)
> ans = ('not in cube')
> end
> end
>
> Will this work if the point is on the surface ?


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.
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

A*x<=b

This test is highly vectorizable over multiple test points, and requires no looping.
One way that you can get this representation is using this tool

http://www.mathworks.com/matlabcentral/fileexchange/30892-representing-polyhedral-convex-hulls-by-vertices-or-inequalities

Subject: Point in Cube Test

From: Roger Stafford

Date: 2 Jan, 2012 01:01:09

Message: 3 of 3

"Deepak " <deepak.tss@gmail.com> wrote in message <jdq2s1$c16$1@newscl01ah.mathworks.com>...
> 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.
- - - - - - - - - -
  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.)

  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(P-Q,n) > 0 with Q being any one of the polygon vertices on the face.

  If the set of vertices defines a non-convex volume, the problem is more difficult. Using the above defined inward-pointing 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 two-dimensional version of this procedure see the thread at:

 http://www.mathworks.com/matlabcentral/newsreader/view_thread/315064

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us