Skip to content

Highschool algebra applied in 3D automation and modeling

January 16, 2016

In high-school, I always considered myself average in math, and I never took much interest in it.

In fact, I can’t say I took an interest in any of the classes taught except history occasionally (at least there were nice stories there). It’s not that I wasn’t interested in the subjects themselves (as I would later discover), it’s just, well… that they were taught in a dull and utterly unimaginative manner. This, unfortunately, is likely the norm in most schools.

It would take me nearly 10 years from the time I was graduated from high-school, to really appreciate how useful math could be. It was during my time as a graduate student, performing research in microbiology that required analyzing large amounts of data. At the end of that period, statistics – probably the least favorite topic for most undergrads, would become one I’m actually rather fond of! Later still, I would heavily regret my earlier disinterest in math, and learn how useful it is for my day to day work in 3D and image processing.

I believe that math is normally taught in a criminally bad way in school. Being a field full of abstractions, it’s commonly taught in an abstract, detached manner, without giving students any real indication to its applications. Our brain is a pretty economical organ, which often refuses to learn things that seem utterly useless, particularly when they require as much effort and repetitive exercise as math often does. I think this is at the heart of the modern math education crisis.

If it was up to me, I’d intertwine math with other subjects from the start. Programming specifically, but also physics, chemistry, economics and biology. Heck, why not a combination of programming and math heavy science? Let kids write code that solves their homework for them, and they will have to understand both the science and the math involved, and learn to code along the way. However its implemented, math should ALWAYS be taught alongside its applications. Optimally, interesting and compelling applications (like video games).

Of course this might not apply to the truly abstract nature of some of the higher level, academic math topics, but it does and should for middle and high-school, and most college level courses.

 

Intersecting Edges

intersect_edges

Lately I’ve seen an intersting question on the Blender StackExchange website: how to add a vertex at the point of intersection of two edges. That’s a pretty useful thing, especially if your 3D geometry is distorted and noisy, a common issue with 3D scans.

There were already several answers to this question, describing a number of efficient techniques for fixing this issue. One of which, by the almighty Zeffii (who maintains the Blender Python blog), included an automated, scripted solution that employs the function geometry.intersect_line_line(v1, v2, v3, v4) to find the intersection and adds a vertex there.

It does not, however, split the edges at this point, so this vert is floating above.

That’s one reason I wanted to write a different solution. The 2nd reason is that I didn’t want to depend on the built-in intersection function. Intersecting two straight lines is a pretty trivial mathematical problem, and I wanted to see if I can revive enough of my basic math skills to solve it.

 

Solving it

The first step is to generate a parametric line equation for each of the two edges. This nice page showed me that a 3D line equation could be represented thus:

(x,y,z) = (x0, y0, z0) + t(a,b,c) = ( x0 + t·a, y0 + t·b, z0 + t·c )

With x0, y0, z0 being the XYZ coordinates of the edge’s first point (let’s call it p1),

and a, b, c being the XYZ values of the edge direction vector, which is calculated by subtracting the edge’s first point (p1) from the 2nd point (p2).

And finally, t is the line’s parameter, with which we can calculate any point on that line.

This could also be broken down to equations for any particular axis’ coordinate:

x = x0 + t·a
y = y0 + t·b
z = z0 + t·c

 

With this in mind we can reconstruct both edge’s line equations. And the intersection point between both lines, is the point where the x,y,z values will be the identical in both lines. I later found an answer in the Math StackExchange website describing this solution in a much prettier manner.

 

So, since I know the first points of each vector and the direction vector’s of both edges, all I needed to do is to reshuffle the variables a bit until I had an equation for one line equation’s parameter t, then plug that value in the line equation to find the intersection point’s coordinate. Here’s the python code for that bit:


globCo = lambda co: o.matrix_world * co</pre>
def find_intersection( edges, o ):
    # Calculate global locations of edge verts
    p1, p2 = [ globCo( v.co ) for v in edges[0].verts ]
    p3, p4 = [ globCo( v.co ) for v in edges[1].verts ]

    v1 = p2 - p1 # Edge 1 line vector
    v2 = p4 - p3 # Edge 2 line vector

    # Calcualte intersection
    # Based on paramteric line equation: (x,y,z) = p0 + t * lineVec
    numer = v2.y * ( p3.x - p1.x ) + v2.x * ( p1.y - p3.y )
    denom = v1.x * v2.y - v1.y * v2.x
    t1 = numer / denom

    co = Vector( [ getattr( p1, i ) + t1 * getattr( v1, i ) for i in 'xyz' ] )

    return co

The entire script can be found on GitHub.

 

Hopes and Regrets

If only someone told me the algebra and calculus that dulled my senses in school (and more advanced topics) can be used to generate amazing 3D structures, power game engines and simulate interesting phenomena in the real world, I certainly would have paid more attention and worked harder to master the material and learn more. One day, when I have a bit more free time, I hope to try and develop curricula that integrates math into programming and other scientific subjects from the get-go, preferably with gamification employed as well. If any educators are reading this, I’d be happy to hear your thoughts on the matter.

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: