Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31772

Re: pls help me with this prog

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: pls help me with this prog
Date 2012-10-19 17:40 -0400
Organization > Bestiaria Support Staff <
References <CAFqGZRHxn_-g0tozcHSGv6EXPReMgdLCQiZjYYZa6EB4xFiNhg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2535.1350682847.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, 19 Oct 2012 10:40:40 +0200, inshu chauhan
<insideshoes@gmail.com> declaimed the following in
gmane.comp.python.general:

> in this prog I have written a code to calculate teh centre of a given 3D data..
>
	One: you didn't supply a sample of the data, so none of us can even
attempt to run the code.
 
> but i want to calculate it for every 3 points not the whole data, but
> instead of giving me centre for every 3 data the prog is printing the
> centre 3 times...
>

	What do you mean by the center [American spelling <G>]? After all,
to define a volume in 3D space you need a minimum of FOUR points (of
three [Cartesian] coordinates: x, y, z). Using only three points nets
you only a /plane/. 

> import cv
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, linalg as lin
> 
> 
> 
> 
> def CalcCentre(data):
>     centre = array([0,0,0])
>     count = 0
>     n = 0
>     for p in data[n:n+3]:
>         centre = add(centre, array(p[:3]))
>         count += 1
>         centre = dot(1./count, centre)
>         return centre
>     n += 1
> def ReadPointCloud(filename):
>     f = open(filename)
>     result = []
>     for l in f:
>         sp = l.split()
>         t = tuple(map(float, sp[1:4]))
>         result.append(t)
>     return result
> 
> def main (data):
> 
> 
>     j = 0
>     for  i in data[:3]:
>         while j != 3:
>          centre = CalcCentre(data)
>          j += 1
>          print centre

	Huh... First off, you are looping over the first three elements of
data, extracting each as "i". THEN you totally ignore "i" and do a while
loop of "j" from 0..2, each time passing ALL of "data" to the
calculation routine. When "j" reaches 3, you break out of the while
loop, and grab the next element of "data" into "i" -- since "j" is still
three, you then skip the whole while loop and get another element of
"data"... and skip the while loop again.

	I suspect what you want here, presuming "data" has the form:
[	[x,y,z],
	[x,y,z],
	[x,y,z],
	...
]

	for point in data:
		center = CalcCentre(point)
		print center

OR, if CalcCentre is supposed to receive three points as input... and
you want to process every three contiguous points..

	for i in range(len(data)-2):
		center = CalcCentre(data[i:i+2])
		print center


		

> 
> 
> if __name__ == '__main__':
>     data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')
> 
> main(data)

	This should be indented the same as the previous line. It doesn't
affect running stand-alone, but if you ever try to import this script it
will attempt to execute main() but will have no data to process, and
give you a trace-back.


	This would be much easier to diagnose if you provide a sample of the
input data, and a sample of what you EXPECT to see for that data.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: pls help me with this prog Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-19 17:40 -0400

csiph-web