-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathPolyhedra.cs
142 lines (125 loc) · 5.04 KB
/
Polyhedra.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* PathCAM - Toolpath generation software for CNC manufacturing machines
* Copyright (C) 2013 Benjamin R. Porter https://github.com/xenovacivus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace GUI
{
public class Polyhedra
{
private static Vector3 GetPerpendicular(Vector3 normal)
{
Vector3 c = new Vector3(0, 1, 0);
if (Math.Abs(normal.Y) > 0.5)
{
c = new Vector3(1, 0, 0);
}
return c;
}
public static void DrawCylinderWireMesh(Vector3 from, Vector3 to, float radius, int sides = 24)
{
Vector3 direction = to - from;
direction.Normalize();
Vector3 c = GetPerpendicular(direction);
Vector3 perp1 = Vector3.Cross(direction, c);
perp1.Normalize();
Vector3 perp2 = Vector3.Cross(perp1, direction);
GL.Disable(EnableCap.Lighting);
GL.Begin(PrimitiveType.Lines);
for (float theta = 0; theta < OpenTK.MathHelper.TwoPi; theta += OpenTK.MathHelper.TwoPi / sides)
{
float thetaNext = theta + OpenTK.MathHelper.TwoPi / sides;
Vector3 a = ((float)Math.Sin(theta) * perp1 + (float)Math.Cos(theta) * perp2) * radius;
Vector3 b = ((float)Math.Sin(thetaNext) * perp1 + (float)Math.Cos(thetaNext) * perp2) * radius;
GL.Vertex3(to);
GL.Vertex3(to + a);
GL.Vertex3(from);
GL.Vertex3(from + a);
GL.Vertex3(to + b);
GL.Vertex3(to + a);
//GL.Vertex3(to + b);
GL.Vertex3(from + b);
GL.Vertex3(from + a);
//GL.Vertex3(from + b);
GL.Vertex3(a + from);
GL.Vertex3(a + to);
}
GL.End();
GL.Enable(EnableCap.Lighting);
}
public static void DrawCylinder(Vector3 from, Vector3 to, float radius, int sides = 24)
{
Vector3 direction = to - from;
direction.Normalize();
Vector3 c = GetPerpendicular(direction);
Vector3 perp1 = Vector3.Cross(direction, c);
perp1.Normalize();
Vector3 perp2 = Vector3.Cross(perp1, direction);
GL.Begin(PrimitiveType.Triangles);
for (float theta = 0; theta < OpenTK.MathHelper.TwoPi; theta += OpenTK.MathHelper.TwoPi / sides)
{
float thetaNext = theta + OpenTK.MathHelper.TwoPi / sides;
Vector3 a = ((float)Math.Sin(theta) * perp1 + (float)Math.Cos(theta) * perp2) * radius;
Vector3 b = ((float)Math.Sin(thetaNext) * perp1 + (float)Math.Cos(thetaNext) * perp2) * radius;
GL.Normal3(direction);
GL.Vertex3(to);
GL.Vertex3(to + a);
GL.Vertex3(to + b);
GL.Normal3(-direction);
GL.Vertex3(from);
GL.Vertex3(from + a);
GL.Vertex3(from + b);
GL.Normal3(a);
GL.Vertex3(a + from);
GL.Normal3(b);
GL.Vertex3(b + from);
GL.Vertex3(b + to);
GL.Normal3(a);
GL.Vertex3(a + from);
GL.Vertex3(a + to);
GL.Normal3(b);
GL.Vertex3(b + to);
}
GL.End();
}
public static void DrawCone(Vector3 from, Vector3 to, float radius, int sides = 24)
{
Vector3 direction = to - from;
direction.Normalize();
Vector3 c = GetPerpendicular(direction);
Vector3 perp1 = Vector3.Cross(direction, c);
perp1.Normalize();
Vector3 perp2 = Vector3.Cross(perp1, direction);
GL.Begin(PrimitiveType.TriangleFan);
GL.Normal3(direction);
GL.Vertex3(to);
for (float theta = 0; theta < OpenTK.MathHelper.TwoPi; theta += OpenTK.MathHelper.TwoPi/sides)
{
float x = (float)Math.Sin(theta);
float y = (float)Math.Cos(theta);
Vector3 tail = (perp1 * x + perp2 * y) * radius + from;
GL.Normal3(perp1 * x + perp2 * y);
GL.Vertex3(tail);
}
GL.End();
}
}
}