How to find points that lie inside a 3D boundary - java

I'm currently working on a project in Android that is Java-based. I am using OpenGL-ES 3.0.
In my project, I have a large, complex 3D object (a human head) with 100000+ vertices and 400000+ triangles. The object's vertices are stored in an array. The object can be rotated relative to its centroid.
I am trying to implement a function where the user selects a set of points on the head, and a line is drawn connecting each point (kind of like "connecting the dots"). In other words, the user selects points on the head (p1, p2, p3...pn), and the chosen points will change color. Then, after choosing the last point (pn), an algorithm runs to calculate all the vertices that lie between each point (for example, p1 and p2). Those vertices will then change color, so the user sees a line (or close to a line) between each point they chose.
I have already implemented a way of allowing the user to select the points, and see those points by changing their colors. The difficulty I am having is the implementation of the lines in between the points.
The only idea I currently have to draw these lines is to make use of code I have already programmed. I have code that allows the user to move a ellipsoid (they can choose the dimensions and rotate the ellipsoid). They move the ellipsoid to a location on the head and run an algorithm that calculates all of the points of the head that lie inside the volume of the ellipsoid, and changes those points' colors.
My idea was to take the midpoint of p1 and p2, set that as the center of the ellipsoid, and rotate/stretch the ellipsoid dimensions so that one axis runs from p1 to p2 and the other one is some large value. Basically, it would look like taking a flattened ball-like shape (something like a red blood cell) and putting one end of the ball on p1, and the opposite end on p2. I could then run the algorithm that I already coded to find all the points inside that ellipsoid. Then I can change the color of these points and the user can see a line between each point they selected.
Does anyone have any criticisms of this technique? Are there other ways I could possibly achieve the result I desire?

To check if a point touches or is inside an ellipsoid:
Translate the world by -x, -y, -z (x, y and z being the ellipse's
center) such that (0, 0, 0) becomes the ellipsoid's new center.
Rotate the world so that the ellipsoid makes 0 degrees.
Scale the world by 1/a, 1/b, and 1/c (a, b and c being the length 3
elliptical axes).
if sqroot(a^2 + b^2 + c^2) <= 1, then the point lies
inside the sphere.

Related

Splitting a polygon into parts

In my application I work with OpenGL and large arrays of data.
One of the things I need to do is I receive multiple "simple" polygons that can be either convex or concave. By simply I mean geometrical definition - polygons without holes and intersecting edges.
I receive this poligons in the form of linked loop of points (vertices), where each Vi is connected to Vi-1 and Vi+1 points and nothing else. The last point is also connected to the first, giving us a loop as a result.
I will say right now that I figured out polygon triangulation, and it works like a charm (I used ear-clipping method for now).
But, I need to build LOTS of those polygons, millions of them. To minimize the load I decided to split my world into "chunks", squares like in Minecraft. I then use frustum culling and other optimizations methods to either render or discard/simplify these chunks and meshes in them.
So, what I want to do is: if the polygon I received lies in several different chunks - then I want to split it into meshes for each of those chunks. Basically, I want to create multiple polygons that are divided on chunk borders, and THEN triangulate them and create meshes for rendering.
As a result I want to have one (or several) contours that I can then triangulate.
here's a picture of my task:
I made an algorythm for splitting it myself that I thought would work, but I found that it only works if the contour in any given chunk doesn't break up, like you see in the example (in other words if there's only one figure in any given chunk).
I thought to ask here if anyone knows any good algorythms for that task? I'll probably come up with something myself, but practice shows that there's almost always a better and simpler ready-made solution out there. I'd really appreciated it if someone could give me a useful link or an article, if not with a solution itself, then something that could give ideas.
I'm not at work at the moment, because it's weekend, so I'll be trying/testing the things on Monday.
What I came up with at the moment, however, is a very simple solution.
test all points in the contour.
If point i and point i+1 don't belong to the same chunk (which I can test easily):
then find an intersection between chunk border and the line made by these two points.
add this new point to the contour between the original two points.
When every edge of the polygon was tested like that - triangulate it.
because we have points on the edges of the chunks - then during triangulation the triangles will fit exactly into chunk borders
for each triangle, decide which chunk it belongs to and generate the mesh in THAT chunk.
I won't go into details of some optimization ideas - like not needing to evaluate resulting triangles if the entire figure fits within the same chunk.
This is a stab in the dark at writing some pseudo code that may work. Feel free to implement it and select your own answer with the actual code if it works.
First, convert your vertex array to a double linked list that loops from the last element to the first element. Or it may be better to model it as an undirected graph instead, because a point may have missing neighbours.
Then apply the algorithm below for each quadrant, starting with the full polygon each time. You can cut down the size of that polygon by culling vertices that are outside of the quadrant and at least 1-neighbour away from edges that cross the cutting lines.
// We need to keep track of the points that we insert on the cutting lines
let LIX = List of X-Cut-Line Intersection Points
let LIY = List of Y-Cut-Line Intersection Points
foreach Edge AB in Poly where A is inside the quadrant, B outside
// Insert points into the polygon that intersect the X-Cut-Line
if AB Intersects X-Cut-Line
let Ix = Intersection Point
Insert Ix between AB so that A<->B becomes A<->Ix
B can be removed from the polygon now
Add Ix to LIX
// Insert points into the polygon that intersect the Y-Cut-Line
if AB.Intersects Y-Cut-Line
let Iy = Intersection Point
Insert Iy between AB so that A<->B becomes A<->Iy
B can be removed from the polygon now
Add Iy to LIY
// Then iterate pairs of points along each cutting line to join them
sort LIX by X Ascending
while (LIX.Length > 0)
let P0 = LIX[0]
let P1 = LIX[1]
Link P0 and P1
Remove P0 and P1 from LIX
sort LIY by Y Ascending
while (LIY.Length > 0)
let P0 = LIY[0]
let P1 = LIY[1]
Link P0 and P1
Remove P0 and P1 from LIY
Finally, you can look for cycles in the resulting linked list/graph to find the new polygons. Each cycle should in theory not contain points from the other cycles already detected so it is an individual polygon.

Rotation around a specific point (eg, rotate around 0,0,0)

I've been searching a lot on this problem, but I couldn't really find an answer that would fit.
I need to rotate a cylinder around a given point (eg, 0,0,0), but the pivot of the cylinder is given by default. How do i change that?
I found this topic, and it's quite what I would want to do, but I don't know how to do it with java.
To explain better what I would like to do, I'll show 3 images.(v)
imageshack.us/photo/my-images/259/aintgood.jpg
imageshack.us/photo/my-images/840/whatineed.jpg
imageshack.us/photo/my-images/705/nogoodn.jpg
So, the first image shows my basic problem, the cylinder should be positioned with the end at the center of the sphere, let's say (0,0,0). The user gives two angles. The first one is for a rotX command, the second one for a rotZ one. The pivot of the cylinder is at its center, so, as image 3 shows, even if i translate the cylinder so its end is at the center of the sphere, when it rotates, the whole thing ruins.
Image 2 shows what the cylinder-sphere group should look like, regardless the given angles.
The image is not obtained based on an algorithm, but based on calculus, and mouserotated.
The general procedure for rotation about an arbitrary point P is:
Translate by -P (so P is at (0, 0, 0))
Rotate around the origin
Translate by P (to bring the origin back to the original location of P)
The easiest way to do this is to represent everything in homogeneous coordinates and represent translations and rotations by matrices. Composing the above three transformations (translate-rotate-translate) is done by matrix multiplication. If the rotation is composed of two or more simpler rotations, then the rotation matrix itself is a product of the matrices for the simpler rotations.

How do I stop one line segment from intersecting another?

Geometry code gets tiresome after a while, but I want to finish this library, so here goes.
Basically, what's the most efficient way to move one line segment, A, so that it no longer intersects with another, B?
Both line segments are defined with a start point (x, y) and a vector describing how the segment extends from that point (eX, eY). An example of how the line segment is described is below:
The solution I'm looking for is where the line segment is moved (its extent is not modified in any way) to the nearest location where it does not intersect. An example:
What is the most efficient way to get this result?
EDIT: People have asked what I mean by "move" - I mean change the (x, y) coordinate of the line segment start point. This will translate the entire segment.
And the line segments exist on the Cartesian plane, and any x/y movement is allowed.
How about this: find the four vectors: two from red line's endpoints going perpendicularly to the black line, and two going perpendicularly from the red line to the black line's endpoints. Take the shortest of these vectors and move the red line along it.
Since you don't specify in which dimension you are free to move, I will assume that any are fine.
I assume your red line is characterized by a starting point (x,y), and a vector from there to the endpoint (eX,eY). Any point on the line is thus [0,1]*(eX,eY)+(x,y).
Lets find the point where lines cross. That's where a*(eX1,eY1)+(x1,y1) = (eX2,eY2)+(x2,y2) with a in [0,1].
If this crossing exists, you can just move the line so that it ends at this crosspoint, with a being the length you have to move.
(x1',y1') = (x1,y1) - a*(eX1,eY1)
This way, you move the starting point away until the crossing point you found before is the touching point of the two lines.

Calculating every cartesian point in a moving cicrle

I have an array: int[][] lawn = new int[980][1280];
wich stores the values of the height of blades in the virtual “lawn”.
In my simulation i have a robot that goes around the lawn and cuts the blades.
My robot has the form of a circle with a diameter (rDiameter). The coordinate system is done with Double and my lawn is in Integer.
I have to develop an algorithm that puts to 0 all the cells touched by the robot when it moves around.
I have the start and end point of the movement, which are stored in a Line2D.Double form and I want to set on 0 all the cells touched by robot (image).
Any ideas?
(Here my previous question on the same argument every cartesian point in a circle
Break the problem in 3 parts. Part 1 is to set to 0 all points in a semi-circle. Part 2 is to set to 0 all points in a rectangle. Part 3 is to break the path into two semi-circles (at the ends) and a rectangle (joining them).
Note that the semi-circles and the rectangles have, in general, lines that are not axis aligned. There are plenty of references out there on rasterizing polygons and circles. You could look up Jack Bresenham's algorithms. Or you could flip open any classic computer graphics text.

Calculate shape orientation in Java (Image analysis)

I have an image such as this:
and I need to calculate the orientation of it. In this case the shape is pointing towards the top left of the screen. Accuracy isn't hugely important as long as 3 or 4 calculations average out to within 5 degrees or so of the actual orientation (it will be moving slightly).
Can anyone point me towards an algorithm to do this? I don't mind if the orientation is returned as a double or as a vector.
If the image is always T-shaped, you can simply get the furthest pair of pixels, then find the furthest pair from either both of those (the edges of the T), find which is further from the other two, draw a line from that one to the middle point of those two.
You can further refine it by then finding the base of the T by comparing the middle line with the edges of the base, and adjusting the angle and offset until it is actually in the middle.
The definitive solution is impossible I guess, since requires image recognition. I would project the 2D image onto axis, i.e. obtain the width and height of the image and get direction vector from these values taking them as components.
First, a couple of assumptions:
The center and centroid are "close"
The descending bar of the T is longer than the cross-bar
First, determine the bounding rectangle of the image and find the points of the image that lie along this rectangle. For points that lie along the line and are a certain distance from one another (say 5 pixels to pick a value) you'll need to only take 1 point from that cluster. At the end of this you should have 3 points, i.e. a triangle. The shortest side of the triangle should be the cross-bar (from assumption 2), i.e. find the two points closest to each other. The line that is perpendicular to the line crossing those two points is then your orientation line, i.e. find the angle between it and the horizontal axis.
I would try morphological skeletonization to simplify the image, followed by some straightforward algorithm to determine the orientation of the longer leg of the skeleton.
The solution in the end was to use a Convex Hull Algorithm, which finds the minimum number of points needed to enclose a shape with a bound.

Categories

Resources