QT C++ code samples
- Mike_Eitel
- Offline
- Platinum Member
Less
More
- Posts: 1159
- Thank you received: 185
21 Jul 2019 08:41 #140057
by Mike_Eitel
Replied by Mike_Eitel on topic QT C++ code samples
Hi Grotius
I'm no more in coding but remember strange results by "compiler optimization".
All sorts of.
M5C
Mike
I'm no more in coding but remember strange results by "compiler optimization".
All sorts of.
M5C
Mike
Please Log in or Create an account to join the conversation.
22 Jul 2019 18:07 - 22 Jul 2019 19:37 #140209
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi Mike,
Thanks for your reply.
I'm no more in coding but remember strange results by "compiler optimization".
I solved it by making the formula outside of OpenGl. No complex issue. It can be a compiler problem. Short after my post
i got a QT update message. Maybe they spotted the same thing... If the problem still exist's it's not my problem anymore.
So i was a bit fast to publish this mathematical behavioure.
3d view (rotate) is implemented, was not hard, but i trapped myself... again .. Have to solve another thing.. I know what i did wrong...
My worst fault this week is to make visualisation's based on the sort array instead of the base array....
The base array has all the raw incoming data. This base array is the holy grail of the program. Without trusting the base array
the program is worth nothing...
So my vision is to alway's view first the raw dxf data (base array) to the user with OpenGL... This is very important.
In other word's... Show the base (auto)cad input to the user screen without any code manipulation.
Hmmm...
My stupid thing... Always the stupid tiny things...
I will change the code to fully 3d (it is already, but the array view has to change) and to base input code class again. ( tiny operation )
After that the classes (plasma or 3d milling) are free to use the code....
So you see Mike, every day we are learning about our code. You are a respected member over here !!
Thanks for your reply.
I'm no more in coding but remember strange results by "compiler optimization".
I solved it by making the formula outside of OpenGl. No complex issue. It can be a compiler problem. Short after my post
i got a QT update message. Maybe they spotted the same thing... If the problem still exist's it's not my problem anymore.
So i was a bit fast to publish this mathematical behavioure.
3d view (rotate) is implemented, was not hard, but i trapped myself... again .. Have to solve another thing.. I know what i did wrong...
My worst fault this week is to make visualisation's based on the sort array instead of the base array....
The base array has all the raw incoming data. This base array is the holy grail of the program. Without trusting the base array
the program is worth nothing...
So my vision is to alway's view first the raw dxf data (base array) to the user with OpenGL... This is very important.
In other word's... Show the base (auto)cad input to the user screen without any code manipulation.
Hmmm...
My stupid thing... Always the stupid tiny things...
I will change the code to fully 3d (it is already, but the array view has to change) and to base input code class again. ( tiny operation )
After that the classes (plasma or 3d milling) are free to use the code....
So you see Mike, every day we are learning about our code. You are a respected member over here !!
Last edit: 22 Jul 2019 19:37 by Grotius.
The following user(s) said Thank You: tommylight
Please Log in or Create an account to join the conversation.
25 Jul 2019 20:46 - 25 Jul 2019 22:04 #140565
by Grotius
Replied by Grotius on topic QT C++ code samples
Little project update :
Added 3d rotate zoom, now this works we can connect it to the right mouse button. Mouse rotate for x and z seem's good enough.
Added 3d path direction arrow's and start points.
Learned more about openGL this day. I started with line arrow's, but a cone looks nicer.
1. Draw a cone with the opengl triangle-strip
2. Move the cone to the position in the middle of the line
3. Rotate the cone within the direction of the line
4. Fabricate the next cone in the array for loop.
A raw code example for making a cone, it places the cone where you want, and it set's the cone direction :
The axis gui :
I am thinking adapting cad function's into the cam program. To drawing lines, arc and circles. To make some fun with cad - cam !
And finally write a exiting file output for the users.
A clean dxf output combined with a ngc output. More languages and data pressed into one exiting file...
No one does this, because their program's are not written to read in multi levels. No one thought about it.
Added 3d rotate zoom, now this works we can connect it to the right mouse button. Mouse rotate for x and z seem's good enough.
Added 3d path direction arrow's and start points.
Learned more about openGL this day. I started with line arrow's, but a cone looks nicer.
1. Draw a cone with the opengl triangle-strip
2. Move the cone to the position in the middle of the line
3. Rotate the cone within the direction of the line
4. Fabricate the next cone in the array for loop.
A raw code example for making a cone, it places the cone where you want, and it set's the cone direction :
Warning: Spoiler!
//make directon cone for line
if(int_sort_array[i][0][0] == 1){
double midpoint_x = (double_sort_array[i][0][0]+double_sort_array[i][0][3])/2;
double midpoint_y = (double_sort_array[i][0][1]+double_sort_array[i][0][4])/2;
double midpoint_z = (double_sort_array[i][0][2]+double_sort_array[i][0][5])/2;
double startpoint_x = double_sort_array[i][0][0];
double startpoint_y = double_sort_array[i][0][1];
double endpoint_x = double_sort_array[i][0][3];
double endpoint_y = double_sort_array[i][0][4];
double C = sqrt( pow((endpoint_x-startpoint_x),2) + pow((endpoint_y-startpoint_y),2) );
double A = endpoint_x-startpoint_x;
double B = endpoint_y-startpoint_y;
double vertex = acos(A/C) * 180.0 / M_PI; //0 to 180 degrees
if(B<0){vertex = vertex *-1;} //180 to 360 degrees
//3d arrow of triangle_strip
double x;
double y;
int height = 5;
int radius = 2;
double angle = (2*M_PI)/8;
glPushMatrix(); //add invidual item
glTranslated(midpoint_x,midpoint_y,midpoint_z);
glRotated(90,0,1,0);
glRotated(-vertex,1,0,0);
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_TRIANGLE_STRIP);
for(int sides = 0; sides <= 8 ; sides++ ) {
x = radius * cos(angle);
y = radius * sin(angle);
angle = angle + (2*M_PI)/8;
glVertex3d(x, y , 0);
glVertex3d(0, 0 , height);
}
glEnd();
glPopMatrix(); //pop invidual item
}
The axis gui :
I am thinking adapting cad function's into the cam program. To drawing lines, arc and circles. To make some fun with cad - cam !
And finally write a exiting file output for the users.
A clean dxf output combined with a ngc output. More languages and data pressed into one exiting file...
No one does this, because their program's are not written to read in multi levels. No one thought about it.
Attachments:
Last edit: 25 Jul 2019 22:04 by Grotius.
Please Log in or Create an account to join the conversation.
26 Jul 2019 07:48 #140631
by sivaraj
Replied by sivaraj on topic QT C++ code samples
The following user(s) said Thank You: Grotius
Please Log in or Create an account to join the conversation.
26 Jul 2019 21:51 - 26 Jul 2019 23:23 #140692
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi Sivaraj,
I did not see that. But i know where to find this problem. It's in the contour recognize class.
The problem has to do with swapping the direction of the little arc at your red arrow line.
Later on i will improve this class.
The blue direction lines are telling the truth. Thanks for your spot !
Today i added :
- dimensions, see left corner at bottom. It's called the statusbar. This statusbar is useless at this moment.
This statusbar must be hacked up into several useful pieces.
It works with zoom, movement's etc. This was a hard piece to understand.
If you click on the screen, it will give you the actual cad coordinates. And later on it will give you actual line lenght.
Something that draftsight is not doing... When you draw a line in drafsight, you don't see the lenght at your mouse point...
- click the line near the start point (can be expanded to more item's like endpoint or window select) and the line is selected,
see the color change to orange. ( now we have the basics of a interactive opengl screen )
- changed line thickness in opengl, this was easy.
- added the 3d rotate to the right mouse button, this was more difficult, but works very nice.
DxfDecoder gui :
For the rest i spotted a G41.1 / G42.1 issue. At least, that is a issue for me for this moment.
Later on i will post more about this.
The inner circle is with G41.1 D0, tool diameter 0 no offset. This goes perfect !!
The outher circle is with G41.1 D1, tool diameter 1 offset left of path.
The problem is with the outher circle. At lead in and lead out, the product is not finisched. Is there a overcut G code command?
At the fire spot.. This material will stay on the cutted product (circle). That is a problem. This non cutted material is between the start radius lead in and the end radius lead out, wich is in this case 0.
I did not see that. But i know where to find this problem. It's in the contour recognize class.
The problem has to do with swapping the direction of the little arc at your red arrow line.
Later on i will improve this class.
The blue direction lines are telling the truth. Thanks for your spot !
Today i added :
- dimensions, see left corner at bottom. It's called the statusbar. This statusbar is useless at this moment.
This statusbar must be hacked up into several useful pieces.
It works with zoom, movement's etc. This was a hard piece to understand.
If you click on the screen, it will give you the actual cad coordinates. And later on it will give you actual line lenght.
Something that draftsight is not doing... When you draw a line in drafsight, you don't see the lenght at your mouse point...
- click the line near the start point (can be expanded to more item's like endpoint or window select) and the line is selected,
see the color change to orange. ( now we have the basics of a interactive opengl screen )
- changed line thickness in opengl, this was easy.
- added the 3d rotate to the right mouse button, this was more difficult, but works very nice.
DxfDecoder gui :
For the rest i spotted a G41.1 / G42.1 issue. At least, that is a issue for me for this moment.
Later on i will post more about this.
The inner circle is with G41.1 D0, tool diameter 0 no offset. This goes perfect !!
The outher circle is with G41.1 D1, tool diameter 1 offset left of path.
The problem is with the outher circle. At lead in and lead out, the product is not finisched. Is there a overcut G code command?
At the fire spot.. This material will stay on the cutted product (circle). That is a problem. This non cutted material is between the start radius lead in and the end radius lead out, wich is in this case 0.
Last edit: 26 Jul 2019 23:23 by Grotius.
The following user(s) said Thank You: tommylight, bkt
Please Log in or Create an account to join the conversation.
27 Jul 2019 20:06 - 27 Jul 2019 20:08 #140764
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi dear liinuxcnc users..
We have a little project update today.
Added button's to the toolbar. It are dockable toolbars, splitted up into more little movable toolbar's just like in cad.
Added the axis origin arrow's for xyz. You can disable them with a toolbar select button.
To test this release :
we.tl/t-Km5u0JAgJE
To start, unzip and type in terminal : ./DxfDecoder
I think this release will work now on a standard debian installation. It's no longer depending of QT open libraries.
Not tested this on standard debian, ubuntu or wathever, but i expect this release is going to work for linux.
No longer needed dependencies :
<QOpenGLWidget>
<QtOpenGL>
We have a little project update today.
Added button's to the toolbar. It are dockable toolbars, splitted up into more little movable toolbar's just like in cad.
Added the axis origin arrow's for xyz. You can disable them with a toolbar select button.
To test this release :
we.tl/t-Km5u0JAgJE
To start, unzip and type in terminal : ./DxfDecoder
I think this release will work now on a standard debian installation. It's no longer depending of QT open libraries.
Not tested this on standard debian, ubuntu or wathever, but i expect this release is going to work for linux.
No longer needed dependencies :
<QOpenGLWidget>
<QtOpenGL>
Attachments:
Last edit: 27 Jul 2019 20:08 by Grotius.
The following user(s) said Thank You: tommylight
Please Log in or Create an account to join the conversation.
30 Jul 2019 13:39 #140950
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi,
Today i started with calculating the outside contour offset of a product. To compensate kerf width for plasma
and milling. Calculating the inside offset has the same principle. This example is for lines at this moment.
Finding circle and arc intersection's is the next level to do.
Attached i a picture of a product 100x100mm that have 2 lines that are offset 5mm from the product.
To find the new closed contour point, see the yellow circle, we have to do a calculation for finding this point. It's called intersection point.
The C++ code sample :
The tested console output :
determinant is : -2495
intersection point is x : 52.097 y : 105
So nice that this works !!
Attached the QT intersection project in zip format.
Today i started with calculating the outside contour offset of a product. To compensate kerf width for plasma
and milling. Calculating the inside offset has the same principle. This example is for lines at this moment.
Finding circle and arc intersection's is the next level to do.
Attached i a picture of a product 100x100mm that have 2 lines that are offset 5mm from the product.
To find the new closed contour point, see the yellow circle, we have to do a calculation for finding this point. It's called intersection point.
The C++ code sample :
Warning: Spoiler!
//find intersection point of 2 lines.. www.geeksforgeeks.org/program-for-point-...ection-of-two-lines/
//line 1 to compare :
double startpoint_x0 = 0;
double startpoint_y0 = 105;
double endpoint_x0 = 50;
double endpoint_y0 = 105;
//line 2 to compare :
double startpoint_x1 = 53.6;
double startpoint_y1 = 103.5;
double endpoint_x1 = 103.6;
double endpoint_y1 = 53.6;
//line 1
double delta_y0 = endpoint_y0 - startpoint_y0;
double delta_x0 = startpoint_x0 - endpoint_x0;
double c0 = delta_y0 * startpoint_x0 + delta_x0 * startpoint_y0;
//line 2
double delta_y1 = endpoint_y1 - startpoint_y1;
double delta_x1 = startpoint_x1 - endpoint_x1;
double c1 = delta_y1 * startpoint_x1 + delta_x1 * startpoint_y1;
double determinant = delta_y0*delta_x1 - delta_y1*delta_x0;
qDebug() << "determinant is : " << determinant;
if (determinant == 0) // The lines are parallel.
{
qDebug() << "the lines are parallel";
}
else
{
double x = (delta_x1*c0 - delta_x0*c1)/determinant;
double y = (delta_y0*c1 - delta_y1*c0)/determinant;
qDebug() << " intersection point is x : " << x << " y : " << y;
}
//line 1 to compare :
double startpoint_x0 = 0;
double startpoint_y0 = 105;
double endpoint_x0 = 50;
double endpoint_y0 = 105;
//line 2 to compare :
double startpoint_x1 = 53.6;
double startpoint_y1 = 103.5;
double endpoint_x1 = 103.6;
double endpoint_y1 = 53.6;
//line 1
double delta_y0 = endpoint_y0 - startpoint_y0;
double delta_x0 = startpoint_x0 - endpoint_x0;
double c0 = delta_y0 * startpoint_x0 + delta_x0 * startpoint_y0;
//line 2
double delta_y1 = endpoint_y1 - startpoint_y1;
double delta_x1 = startpoint_x1 - endpoint_x1;
double c1 = delta_y1 * startpoint_x1 + delta_x1 * startpoint_y1;
double determinant = delta_y0*delta_x1 - delta_y1*delta_x0;
qDebug() << "determinant is : " << determinant;
if (determinant == 0) // The lines are parallel.
{
qDebug() << "the lines are parallel";
}
else
{
double x = (delta_x1*c0 - delta_x0*c1)/determinant;
double y = (delta_y0*c1 - delta_y1*c0)/determinant;
qDebug() << " intersection point is x : " << x << " y : " << y;
}
The tested console output :
determinant is : -2495
intersection point is x : 52.097 y : 105
So nice that this works !!
Attached the QT intersection project in zip format.
The following user(s) said Thank You: chimeno, tommylight
Please Log in or Create an account to join the conversation.
30 Jul 2019 19:14 - 30 Jul 2019 19:48 #140971
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi,
Thanks to chimeno & tommylight !
Me was lucky today... Made the circle and line intersection code ready quite quickly...
Attached a QT project for searching the intersecting point of a line and a circle.
This basic code can be a template for intersecting lines with arc's too.
The cad drawing with a line and a circle. The red dimension lines are calculated by the C++ code.
The C++ code :
The console output of this example :
Two solutions x1 : 9.55418 y1 : 5.75232
Two solutions x2 : 3.14394 y2 : 2.19108
This Circle and line intersecting QT C++ project is attached for downloading.
Another offset example :
In this example we got a 5mm outside offset for the product.
The green lines are done by the line-line intersection methode in my previous post today.
If we see a line followed by an arc in the memory array, we do a line-circle intersection like below.
Offset of the arc is 5mm.
Code :
Console output :
Two solutions x1 : 90.4489 y1 : 66.7792
Two solutions x2 : 66.6837 y2 : 90.3977
We need the closest solution. The closest founded intersection point related to the line. We can make a little formula for that with if, else.
The yellow circle is the calculated intersection point of a line collision with an arc.
Thanks to chimeno & tommylight !
Me was lucky today... Made the circle and line intersection code ready quite quickly...
Attached a QT project for searching the intersecting point of a line and a circle.
This basic code can be a template for intersecting lines with arc's too.
The cad drawing with a line and a circle. The red dimension lines are calculated by the C++ code.
The C++ code :
Warning: Spoiler!
float circle_center_x = 8;
float circle_center_y = 1;
float radius = 5;
float line_x1 = 1;
float line_y1 = 1;
float line_x2 = 10;
float line_y2 = 6;
float dx, dy, A, B, C, det, t;
dx = line_x2 - line_x1;
dy = line_y2 - line_y1;
A = dx * dx + dy * dy;
B = 2 * (dx * (line_x1 - circle_center_x) + dy * (line_y1 - circle_center_y));
C = (line_x1 - circle_center_x) * (line_x1 - circle_center_x) + (line_y1 - circle_center_y) * (line_y1 - circle_center_y) - radius * radius;
det = B * B - 4 * A * C;
if ((A <= 0.0000001) || (det < 0))
{
qDebug() << "No real solutions";
}
else if (det == 0)
{
t = -B / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
qDebug() << "One solution x1 :" << intersection_x1 << "y1 :" << intersection_y1;
}
else
{
t = (-B + sqrt(det)) / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
t = (-B - sqrt(det)) / (2 * A);
float intersection_x2 = (line_x1 + t * dx);
float intersection_y2 = (line_y1 + t * dy);
qDebug() << "Two solutions x1 :" << intersection_x1 << "y1 :" << intersection_y1;
qDebug() << "Two solutions x2 :" << intersection_x2 << "y2 :" << intersection_y2;
}
float circle_center_y = 1;
float radius = 5;
float line_x1 = 1;
float line_y1 = 1;
float line_x2 = 10;
float line_y2 = 6;
float dx, dy, A, B, C, det, t;
dx = line_x2 - line_x1;
dy = line_y2 - line_y1;
A = dx * dx + dy * dy;
B = 2 * (dx * (line_x1 - circle_center_x) + dy * (line_y1 - circle_center_y));
C = (line_x1 - circle_center_x) * (line_x1 - circle_center_x) + (line_y1 - circle_center_y) * (line_y1 - circle_center_y) - radius * radius;
det = B * B - 4 * A * C;
if ((A <= 0.0000001) || (det < 0))
{
qDebug() << "No real solutions";
}
else if (det == 0)
{
t = -B / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
qDebug() << "One solution x1 :" << intersection_x1 << "y1 :" << intersection_y1;
}
else
{
t = (-B + sqrt(det)) / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
t = (-B - sqrt(det)) / (2 * A);
float intersection_x2 = (line_x1 + t * dx);
float intersection_y2 = (line_y1 + t * dy);
qDebug() << "Two solutions x1 :" << intersection_x1 << "y1 :" << intersection_y1;
qDebug() << "Two solutions x2 :" << intersection_x2 << "y2 :" << intersection_y2;
}
The console output of this example :
Two solutions x1 : 9.55418 y1 : 5.75232
Two solutions x2 : 3.14394 y2 : 2.19108
This Circle and line intersecting QT C++ project is attached for downloading.
Another offset example :
In this example we got a 5mm outside offset for the product.
The green lines are done by the line-line intersection methode in my previous post today.
If we see a line followed by an arc in the memory array, we do a line-circle intersection like below.
Offset of the arc is 5mm.
Code :
Warning: Spoiler!
float circle_center_x = 75;
float circle_center_y = 75;
float radius = 17.5;
float line_x1 = 53.5;
float line_y1 = 103.5;
float line_x2 = 69.7;
float line_y2 = 87.4;
float dx, dy, A, B, C, det, t;
dx = line_x2 - line_x1;
dy = line_y2 - line_y1;
A = dx * dx + dy * dy;
B = 2 * (dx * (line_x1 - circle_center_x) + dy * (line_y1 - circle_center_y));
C = (line_x1 - circle_center_x) * (line_x1 - circle_center_x) + (line_y1 - circle_center_y) * (line_y1 - circle_center_y) - radius * radius;
det = B * B - 4 * A * C;
if ((A <= 0.0000001) || (det < 0))
{
qDebug() << "No real solutions";
}
else if (det == 0)
{
t = -B / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
qDebug() << "One solution x1 :" << intersection_x1 << "y1 :" << intersection_y1;
}
else
{
t = (-B + sqrt(det)) / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
t = (-B - sqrt(det)) / (2 * A);
float intersection_x2 = (line_x1 + t * dx);
float intersection_y2 = (line_y1 + t * dy);
qDebug() << "Two solutions x1 :" << intersection_x1 << "y1 :" << intersection_y1;
qDebug() << "Two solutions x2 :" << intersection_x2 << "y2 :" << intersection_y2;
}
float circle_center_y = 75;
float radius = 17.5;
float line_x1 = 53.5;
float line_y1 = 103.5;
float line_x2 = 69.7;
float line_y2 = 87.4;
float dx, dy, A, B, C, det, t;
dx = line_x2 - line_x1;
dy = line_y2 - line_y1;
A = dx * dx + dy * dy;
B = 2 * (dx * (line_x1 - circle_center_x) + dy * (line_y1 - circle_center_y));
C = (line_x1 - circle_center_x) * (line_x1 - circle_center_x) + (line_y1 - circle_center_y) * (line_y1 - circle_center_y) - radius * radius;
det = B * B - 4 * A * C;
if ((A <= 0.0000001) || (det < 0))
{
qDebug() << "No real solutions";
}
else if (det == 0)
{
t = -B / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
qDebug() << "One solution x1 :" << intersection_x1 << "y1 :" << intersection_y1;
}
else
{
t = (-B + sqrt(det)) / (2 * A);
float intersection_x1 = (line_x1 + t * dx);
float intersection_y1 = (line_y1 + t * dy);
t = (-B - sqrt(det)) / (2 * A);
float intersection_x2 = (line_x1 + t * dx);
float intersection_y2 = (line_y1 + t * dy);
qDebug() << "Two solutions x1 :" << intersection_x1 << "y1 :" << intersection_y1;
qDebug() << "Two solutions x2 :" << intersection_x2 << "y2 :" << intersection_y2;
}
Console output :
Two solutions x1 : 90.4489 y1 : 66.7792
Two solutions x2 : 66.6837 y2 : 90.3977
We need the closest solution. The closest founded intersection point related to the line. We can make a little formula for that with if, else.
The yellow circle is the calculated intersection point of a line collision with an arc.
Last edit: 30 Jul 2019 19:48 by Grotius.
Please Log in or Create an account to join the conversation.
30 Jul 2019 20:48 - 30 Jul 2019 21:32 #140976
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi,
The last item to investegate was find the intersecting point of a circle with a circle.
Cad drawing of product with a contour offset 5mm. Find where the 2 circle's collide..
Attached QT C++ project find intersection circle to circle.
console output :
intersection x1 : 74.0697 y1 : 57.5247
intersection x2 : 92.4753 y2 : 75.9303
We have 2 solution's. We calculate wich one we need with a if else statement.
What do we do with the tiny line in within the red circle? Delete it automaticly?
The QT C++ code for intersecting circle to circle :
Okey we have to go on with the next level.
Add contour offset's to the product.....
The swap class needs some improvement's, but i will try add some contour offsets first.
We have classes for line-line, line-circle and arc-arc interceptions. Without that we where lost.
But now we have more power to make offsets.
Glad to be back to the relaxed DxfDecoder screen.
The last item to investegate was find the intersecting point of a circle with a circle.
Cad drawing of product with a contour offset 5mm. Find where the 2 circle's collide..
Attached QT C++ project find intersection circle to circle.
console output :
intersection x1 : 74.0697 y1 : 57.5247
intersection x2 : 92.4753 y2 : 75.9303
We have 2 solution's. We calculate wich one we need with a if else statement.
What do we do with the tiny line in within the red circle? Delete it automaticly?
The QT C++ code for intersecting circle to circle :
Warning: Spoiler!
double cx0 = 75;
double cy0 = 75;
double radius0 = 17.5;
double cx1 = 100;
double cy1 = 50;
double radius1 = 27;
// Find the distance between the centers.
double dx = cx0 - cx1;
double dy = cy0 - cy1;
double dist = sqrt(dx * dx + dy * dy);
// See how many solutions there are.
if (dist > radius0 + radius1)
{
qDebug() << "No solution";
}
else if (dist < abs(radius0 - radius1))
{
qDebug() << "No solutions, one circle contains the other";
}
else if ((dist == 0) && (radius0 == radius1))
{
qDebug() << "No solutions, the circles coincide";
}
else
{
// Find a and h.
double a = (radius0 * radius0 -
radius1 * radius1 + dist * dist) / (2 * dist);
double h = sqrt(radius0 * radius0 - a * a);
// Find P2.
double cx2 = cx0 + a * (cx1 - cx0) / dist;
double cy2 = cy0 + a * (cy1 - cy0) / dist;
// Get the points P3.
double intersection_x1((cx2 + h * (cy1 - cy0) / dist));
double intersection_y1((cy2 - h * (cx1 - cx0) / dist));
double intersection_x2 = (cx2 - h * (cy1 - cy0) / dist);
double intersection_y2 = ((cy2 + h * (cx1 - cx0) / dist));
qDebug() << "intersection x1 : " << intersection_x1 << " y1 : " << intersection_y1;
qDebug() << "intersection x2 : " << intersection_x2 << " y2 : " << intersection_y2;
}
double cy0 = 75;
double radius0 = 17.5;
double cx1 = 100;
double cy1 = 50;
double radius1 = 27;
// Find the distance between the centers.
double dx = cx0 - cx1;
double dy = cy0 - cy1;
double dist = sqrt(dx * dx + dy * dy);
// See how many solutions there are.
if (dist > radius0 + radius1)
{
qDebug() << "No solution";
}
else if (dist < abs(radius0 - radius1))
{
qDebug() << "No solutions, one circle contains the other";
}
else if ((dist == 0) && (radius0 == radius1))
{
qDebug() << "No solutions, the circles coincide";
}
else
{
// Find a and h.
double a = (radius0 * radius0 -
radius1 * radius1 + dist * dist) / (2 * dist);
double h = sqrt(radius0 * radius0 - a * a);
// Find P2.
double cx2 = cx0 + a * (cx1 - cx0) / dist;
double cy2 = cy0 + a * (cy1 - cy0) / dist;
// Get the points P3.
double intersection_x1((cx2 + h * (cy1 - cy0) / dist));
double intersection_y1((cy2 - h * (cx1 - cx0) / dist));
double intersection_x2 = (cx2 - h * (cy1 - cy0) / dist);
double intersection_y2 = ((cy2 + h * (cx1 - cx0) / dist));
qDebug() << "intersection x1 : " << intersection_x1 << " y1 : " << intersection_y1;
qDebug() << "intersection x2 : " << intersection_x2 << " y2 : " << intersection_y2;
}
Okey we have to go on with the next level.
Add contour offset's to the product.....
The swap class needs some improvement's, but i will try add some contour offsets first.
We have classes for line-line, line-circle and arc-arc interceptions. Without that we where lost.
But now we have more power to make offsets.
Glad to be back to the relaxed DxfDecoder screen.
Last edit: 30 Jul 2019 21:32 by Grotius.
The following user(s) said Thank You: tommylight, emilvv
Please Log in or Create an account to join the conversation.
01 Aug 2019 14:14 - 01 Aug 2019 18:03 #141110
by Grotius
Replied by Grotius on topic QT C++ code samples
Hi,
Today i have a C++ example for finding a new xy offset point of a certain line. In this example the offset is 10mm.
We have the x0,y0 (0,0) and x1,y1 (200,100) points. This are the points that form the blue product line C.
We have to calculate the offset point of the blue line, see yellow circle.
Visualisation of the calculation..
Attached the QT project for this example, the zip includes the cad drawing.
The C++ code spoiler down here has some updated code... Don't forget this...
Console output :
this calculation is between 0 and 180 degrees, the new x is : 204.472 new y is : 91.0557 (alpha 3)
The output in DxfDecoder. Tested in all quadrants. The calculated point is the endpoint of the short blue line wich has
the offset lenght of 10mm in this case. The blue line is perpendicular to the base line. The yellow lines are opposites of the blue line.
The yellow line was founded by only a + and - calculation related to the blue line.
This is nice !!
We can now draw a offset line with opengl.
After that, we can do the intersection calculation for line-line to close the contour... Yes yes..
These separate software step's (tutorials) combined (in total) will result in the base code for a complex contour offset algoritme.
Don't forget the power of Linux !! It's all in the game...
C++ code: (angle1 = alpha1 and so on)
For a quick test we add the offset lines to the opengl class :
Later on we will copy this test to a new class and keep the opengl as clean as possible.
Next item is to execute the intersection line-line class forum.linuxcnc.org/41-guis/36768-qt-c-co...ples?start=60#140950 to find all the intersection point's... This we will do tomorrow !
If i look at the current output, we can use this for generating press brake products..
Today i have a C++ example for finding a new xy offset point of a certain line. In this example the offset is 10mm.
We have the x0,y0 (0,0) and x1,y1 (200,100) points. This are the points that form the blue product line C.
We have to calculate the offset point of the blue line, see yellow circle.
Visualisation of the calculation..
Attached the QT project for this example, the zip includes the cad drawing.
The C++ code spoiler down here has some updated code... Don't forget this...
Console output :
this calculation is between 0 and 180 degrees, the new x is : 204.472 new y is : 91.0557 (alpha 3)
The output in DxfDecoder. Tested in all quadrants. The calculated point is the endpoint of the short blue line wich has
the offset lenght of 10mm in this case. The blue line is perpendicular to the base line. The yellow lines are opposites of the blue line.
The yellow line was founded by only a + and - calculation related to the blue line.
This is nice !!
We can now draw a offset line with opengl.
After that, we can do the intersection calculation for line-line to close the contour... Yes yes..
These separate software step's (tutorials) combined (in total) will result in the base code for a complex contour offset algoritme.
Don't forget the power of Linux !! It's all in the game...
C++ code: (angle1 = alpha1 and so on)
Warning: Spoiler!
//test product offset at endpoint
if(int_sort_array[0][0] == 1){
double startpoint_x = double_sort_array[0][0];
double startpoint_y = double_sort_array[0][1];
double startpoint_z = double_sort_array[0][2];
double endpoint_x = double_sort_array[0][3];
double endpoint_y = double_sort_array[0][4];
double endpoint_z = double_sort_array[0][5];
double A = endpoint_x-startpoint_x; //triangle horizontal base
double B = endpoint_y-startpoint_y; //triangle vertical
double C = sqrt( pow(A,2) + pow(B,2) ); //orginal cad line, triangle slope side
double angle = acos(A/C) * 180.0 / M_PI; //0 to 180 degrees
if(angle==0){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F*-1 , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y*-1 , endpoint_z);
glEnd();
}
if(angle==180){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y , endpoint_z);
glEnd();
}
if(B>0){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y , endpoint_z);
glEnd();
}
if(B<0){
angle = (angle -180) *-1; //180 to 360 degrees
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E*-1, startpoint_y + F*-1 , endpoint_z);
glEnd();
//add startpoint :
double p4_x_dist = (A - E*-1)*-1 ;
double p4_y_dist = (B - F*-1)*-1 ;
qDebug() << "startpoint p4_x_dist = " << p4_x_dist;
qDebug() << "startpoint p4_y_dist = " << p4_y_dist;
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x_dist, startpoint_y + p4_y_dist , endpoint_z);
glEnd();
}
if(int_sort_array[0][0] == 1){
double startpoint_x = double_sort_array[0][0];
double startpoint_y = double_sort_array[0][1];
double startpoint_z = double_sort_array[0][2];
double endpoint_x = double_sort_array[0][3];
double endpoint_y = double_sort_array[0][4];
double endpoint_z = double_sort_array[0][5];
double A = endpoint_x-startpoint_x; //triangle horizontal base
double B = endpoint_y-startpoint_y; //triangle vertical
double C = sqrt( pow(A,2) + pow(B,2) ); //orginal cad line, triangle slope side
double angle = acos(A/C) * 180.0 / M_PI; //0 to 180 degrees
if(angle==0){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F*-1 , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y*-1 , endpoint_z);
glEnd();
}
if(angle==180){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y , endpoint_z);
glEnd();
}
if(B>0){
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E, startpoint_y + F , endpoint_z);
glEnd();
//add startpoint :
double p4_x = startpoint_x + ( E - endpoint_x );
double p4_y = startpoint_y - ( endpoint_y - F );
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x, startpoint_y + p4_y , endpoint_z);
glEnd();
}
if(B<0){
angle = (angle -180) *-1; //180 to 360 degrees
double offset = 10;
double D = sqrt( pow(C,2) + pow(offset,2) );
double angle1 = acos(C/D) * 180.0 / M_PI; //0 to 180 degrees
double angle2 = angle - angle1;
double E = cos(angle2*(M_PI/180)) * D ; //x value p3
double F = sqrt( pow(D,2) - pow(E,2) ); //y value p3
glColor3f(0.0f, 0.0f, 1.0f); //blue
glBegin(GL_LINES);
glVertex3d(endpoint_x, endpoint_y , endpoint_z);
glVertex3d(startpoint_x + E*-1, startpoint_y + F*-1 , endpoint_z);
glEnd();
//add startpoint :
double p4_x_dist = (A - E*-1)*-1 ;
double p4_y_dist = (B - F*-1)*-1 ;
qDebug() << "startpoint p4_x_dist = " << p4_x_dist;
qDebug() << "startpoint p4_y_dist = " << p4_y_dist;
glColor4f(1.0f, 1.0f, 0.0f, 0.0f); //yellow
glBegin(GL_LINES);
glVertex3d(startpoint_x, startpoint_y , startpoint_z);
glVertex3d(startpoint_x + p4_x_dist, startpoint_y + p4_y_dist , endpoint_z);
glEnd();
}
For a quick test we add the offset lines to the opengl class :
Later on we will copy this test to a new class and keep the opengl as clean as possible.
Next item is to execute the intersection line-line class forum.linuxcnc.org/41-guis/36768-qt-c-co...ples?start=60#140950 to find all the intersection point's... This we will do tomorrow !
If i look at the current output, we can use this for generating press brake products..
Attachments:
Last edit: 01 Aug 2019 18:03 by Grotius.
The following user(s) said Thank You: phillc54, tommylight
Please Log in or Create an account to join the conversation.
Time to create page: 0.597 seconds