Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am currently trying to implement a laidlaw-like CSG algorithm.
The essential task is to split polygons so they dont intersect.

Only thing for me left to do now is to split the polygons, on paper I know how to do it, in code it is a little more difficult.

Let's sag we have a single triangle, of course lying on a plane.
Within the triangle there is a (intersection) segment, not touching the boundary or a vertex.

I need two things now:
- An algorithm to get an orthogonal Segment to the existing one so they are forming a cross. I'm doing things a little different than in the Laidlaw-Paper (I didn't understand completely), which is why I need the orthogonal segment.

- An algorithm to determine if a point is on one side of the segment (or infinite line that goes through the segment points), or the other one.
Remember: EVERY point is on the triangle plane. So when I say orthogonal I don't mean pointing out of the plane.

For the last one I already did something but I am not sure its working correctly, it passed the view test I have made, but the results when applying it are a bit confusing.

public static double WichSide(Segment3D seg, Vector3db P)
{
    Vector3db cp1 = Vector3db.Cross(seg.P1 - seg.P0, P - seg.P0);
    return Vector3db.Dot(new Vector3db(1,1,1), cp1);
}

I thought this function gave some number with a negative sign if the point P is on the right, and a positive one if its on the left.
The class Vector3db is pretty similar to any Vector3 class there is.


Now please, I am not good at math, it was took me weeks to get where I am and I still don't understand every algorithm I am using (I try), so try to keep your answers as simple as possible :sigh:

Thanks in advance guys,
Jan

Addition:
Well apparently the only thing unclear was my defintion of segment:
Like in most 3D-Application I am refering to a line segment here which is defined by two three-dimensional points. My structure does not really contain anything else, other than calculating the line-segment's direction.

So in short:
- There is a plane containing a triangle.
- Within this triangle there is a line-segment from P0 to P1
- What I want is an orthognal segment to the line-segment P0-P1.
- Every point of this orthogonal segment has to lie (like every vertex from the triangle and the existing line-segment) on the triangle plane.

I hope this confuses you less, and you have to excuse. I am not really familiar with all the correct mathematical terms in English.

Oh and yes, my Vector class contains everything needed, like cross product, dot product, multiplication with a scalar and the proper operators.

http://imageupload.org/pt-1212944275937.html - Here is a drawing that should help you understand
Posted
Updated 7-Jan-11 10:30am
v7
Comments
Dalek Dave 9-Jan-11 14:32pm    
Well laid out question.

All right, another answer:
From your picture I can see all in one plane. For now, I'll find you only the direction of new segment, you can define its position/dilation by yourself (I asked you about required position of points).

You have triangle defined by 3 3D points: A, B, C, triangle is {A, B, C}.
You have available green segment: { G1, G2 }; G* are 3D points. I will denote segments using curly brackets.

Now, I don't know how your vectors are defined, what are the operation. Let's assume you can construct a vector by two 3D point.

Build 3D vector V1 = new Vector ({A, B}), V2 = new Vector ({B, C}).
This way, V3 = V1×V2 is normal to triangle plane.

Now, have a vector based on available green segment:

Vg = new Vector({G1, G2})

New vector will be orthogonal to a green vector Gv:

Vnew = Vg×V3

As Vg is normal to the triangle place and Vg is in the place, Vnew will be again in the plane but orthogonal to the green Vg.

This is pseudo-code of course. I really need all your 3D classes to show any code.

You got correct direction. I don't know yet the required position and length of new segment, but you can find out yourself, I think.

(If your picture is suggestive and the visual suggestion is correct, you can go to the middle of green segment by averaging of its start and end points, by each of the components in your basis separately. Then this will define a middle of your new segment.)
 
Share this answer
 
v6
Comments
Jan Wolf 8-Jan-11 4:45am    
Last section of your text is correct, this is how I intended it.
I will try the code within the next hours but it looks promising.

Thanks very much in advance, I will mark the answer as accept as soon as I've implemented in.

Pseudo code is okay, someone already gave me a hint to use the triangle normal (which should also be the normal to the plane), so from what I guess now you have given me the solution I wanted :)
There's one question I have:
My constructor for a vector accepts 3 arguments, x,y,z as doubles.
What is a vector constructed by two points? The direction?

Cheers,
Jan
Sergey Alexandrovich Kryukov 8-Jan-11 5:45am    
Vector constructor? Well, this is way too easy (but I really don't know your interpretation of vectors). I will figure this, see below.

This is not about direction; a vector is vector. Ok, if you have separate vector class and 3D point class, they are both vectors, essentially. I don't know the difference in your classes, probably only their operation sets. A constructor Vector(double, double, double) is equivalent to Vector(Point3D), that is, a Point3D is interpreted as a vector starting from the (0, 0, 0) in your basis to (x, y, z). The constructor I mean in pseudo-code is the vector between two points: Point3D P1 and P2. This is, essentially, either P1 - P2 or (the opposite, does not matter), P2 - P1, where "-" is a vector subtraction. If you don't have vector subtraction for 3D point, this is Vector(P1)-Vector(P2) or the negation of that. As I say, your vectors and 3D points could be the same class. In other words, vector subtraction is Vector(x1-x2, y1-y2, z1-z2) or the vector going from P2 = {x2, x2, x2} to P1 = {x1, x1, x1}. I trust you can translate vector to the point you need and dilate to the required length and deduce 2 points needed to form your new segment.
Jan Wolf 8-Jan-11 8:34am    
Well the direction of a ray / line segment that is defined by two points is P2-P1, so my assumption is not completely wrong.
The Vector between two points is the direction of the line/line segment/ray (that goes through those points).

I have all the necessary vector operations needed, addition, substraction, cross product, dot product.


In theory I know the difference between a point and a vector, nevertheless, I use the vector class for points and vectors, like I am used to (because SlimDX for example always takes it's Vector3 class for points and vectors).

Constructing the points I need for my orthogonal segments is something I can do, thanks to your explanation. While I'm not really good at math, I'm luckily not a complete idiot :D

Thanks again SAKryukov for your detailled explanation and help.
It's really appreciated

P.s.:
Since it was asked. The Laidlaw paper for CSG Operations wouldn't really help here, because Laidlaw does the triangle splitting a little different. I want to achieve that the triangle is split so that the given intersection line segment does not cut the triangle anymore. Therefore I have to break it in five pieces.. Difficult stuff.
Sergey Alexandrovich Kryukov 8-Jan-11 14:10pm    
No, your assumption wasn't wrong. I only say again vector is not direction (there is theory using pure directions, particular important for liquid crystals (of stick-like molecules; so the object is called "director"). Using the same class for a vector and a point is quite adequate (do you know the Occam razor?). Sufficient introduction: http://en.wikipedia.org/wiki/Euclidean_vector

About "laidlaw-like CSG algorithm", "CSG algorithm" -- I just wanted to know what is that. Do you know any references?

Good luck.
Espen Harlinn 9-Jan-11 9:03am    
5+ for the effort I would have liked to give a 10+ (at least) :-)
If you have a plane defined by two vectors A and B; they essentially define a plane orientation only if the angle between them, and the length of each are not too close to zero. In this case, you can always get a vector product of the two:

C = A×B

C calculated this way is orthogonal to both A and B and to the plane; using this technique you can do further calculations to build vectors with orientation to your requirements.
 
Share this answer
 
v4
Comments
Jan Wolf 7-Jan-11 14:07pm    
I hope I have clarified the confusing parts in the last sections I've added.
Sergey Alexandrovich Kryukov 7-Jan-11 16:11pm    
I'm looking, thanks. What's the "laidlaw-like CSG algorithm"? Do you have a reference?
Sergey Alexandrovich Kryukov 7-Jan-11 16:29pm    
Ok, you described the orientation of new orthogonal segment. Forget vector product -- I thought "orthogonal to the triangle plane", now you say "orthogonal to P1-P2 line". Now, where should be the end points of the new segment?
Sergey Alexandrovich Kryukov 8-Jan-11 15:30pm    
The Answer is updated based on your clarifications, reasoning irrelevant to your problem removed, so now it is both correct and relevant.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900