Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Point in Cube Test
Date: Sun, 1 Jan 2012 18:09:08 +0000 (UTC)
Organization: Xoran Technologies
Lines: 37
Message-ID: <jdq7g4$opv$1@newscl01ah.mathworks.com>
References: <jdq2s1$c16$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1325441348 25407 172.30.248.37 (1 Jan 2012 18:09:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 1 Jan 2012 18:09:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:753517

"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