last day (15 days later) » 

5:10 PM
0
A: Calculating surface normal in Python using Newell's Method

dblcliksorry, I can't COMMENT, so I'll post this here in hopes of helping. First, when you run your code as-is, do you get any errors? I copy/pasted your code exactly and got a Division By Zero error. Also, when you say that all of your polygons will be planar, do mean they'll always be 2-dimensional...

 
Yes, I get the divide by zero - because I'm trying to normalise [0,0,0] which isn't a valid vector. That shouldn't be the value at that point in the code. And by planar, I mean the first one. A 2D polygon in 3D space.
 
Okay, I got that too (I avoided it by using this--normalised = [i/sum(n) if sum(n)!=0.0 else 0 for i in n]--just to get past it). Will your polygons always be convex, or is it possible to have ones that are not?
 
They could be any shape, though not self-intersecting. And your workaround might fail, for example with the vector [-0.5, 0.0, 0.5]. I was using try: except: block but it shouldn't be necessary if the algorithm is working correctly.
 
I'm still going to toy with getting this to work, but your point about failing for some cases is correct. This should work in all cases, just to have in your code as quick avoidance to Div-by-Zero: normalised = [i/sum(n) if len(list(set(n)))!=1 or sum(n)!=0 else 0.0 for i in n]
 
What's wrong with a try, except? It's more readable and avoids testing as it's not necessary.
 
5:10 PM
I saw your EDIT, but wanted to make one call out, JIC. I was going to recommend the Cross Product approach but I believe it only works for convex polygons, which is why I asked about that above. You may want to test it on a concave polygon to make sure you're not going to get an error in those cases.
Nothing's wrong with a try, except :) I use them all of the time! I was just giving a one line solution to fit your existing code format
 
Hi, thanks for helping out. This is dragging out a bit in comments though.
I tried with a concave polygon (a bowtie shape) and it gives me the same result as Wolfram Alpha
poly = [Point3D(0.0, 0.0, 0.0),
        Point3D(2.0, 1.0, 0.0),
        Point3D(4.0, 0.0, 0.0),
        Point3D(4.0, 3.0, 0.0),
        Point3D(2.0, 2.0, 0.0),
        Point3D(0.0, 3.0, 0.0)]
Both give [0,0,-4]
But that seems wrong to me!
 
Hey, no problem, happy to help what I can
let me look at the WA output
 
Is it just that it needs normalising?
 
Possibly, I'm wondering if WA is also using the cross-product method for this problem too
 
No, it looks like it just needs normalising. See this:
 
5:17 PM
yep, it looks like that's it
since your shapes appear to always be planar, I think you'll be fine with the cross-product method
 
great. by the way, while i've got your ear, i have a much more involved question, about rotating my 3d shapes into 2D so i can use pyclipper for clipping operations. that's why i'm looking for the normal vector.
just wondering if you know of any useful libraries/algorithms for that?
my first few tries have been fairly disastrous!
 
yeah, that can be a messy problem indeed!
so your issues are with the rotating into 2d, or using pyclipper?
 
pyclipper's fine. it's the rotating i'm having trouble with
 
understandable--so I'm up to speed, are you wanting to create 2d projections of each face, or the 2d projection of the shape onto the (x,y,0) space?
your day sounds worlds more interesting than mine, btw!
 
so the problem is i have 2 walls in a building that are overlapping (and are not necessarily vertical). i need to convert that into three wall sections, one for the walla.intersect(wallB), one for wallA.diff(wallB), and one for wallB.diff(wallA). so what i think i need to to do is rotate them both into 2D space, without changing their overlaps, perform the clipping to identify the diffs and intersect, then rotate the new walls back into the original plane.
I wish it was just my day - i've been struggling with this since Sunday!
 
5:30 PM
ouch! I remember times like that when I was doing my MS in Applied Math--days and days spent on one problem alone!
okay, so I think I can more clearly see what you're trying to do
 
final year of my EngD in building simulation here - it's getting a bit close!
 
good for you! that's exciting
I personally don't know any algorithms off-hand, but I'm wondering if this problem hasn't come up in game design--seems like a common issue that would have to be dealt with since it's inevitable for shapes to render with overlap
 
no worries. that snippet of explanation above has helped me formulate a question at least.
thanks for your help!
 
ah, but the issue with game design is those are two objects and you'd just do a collision-detection...you're needing to separate out 2 objects
no problem! I'll keep my eye out for anything of use for you :)
 
:-)
 
5:34 PM
best of luck with your last year for the EngD!
 
thanks. and if you want to convert your comment/answer into something about cross-product method i'll accept it.
get you some internet points for your trouble!
 
well thanks! I'll make the edit--much appreciated!
 

  last day (15 days later) »