-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCutter.cs
165 lines (137 loc) · 4.06 KB
/
Cutter.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Side.cs
//
// Author:
// Jean-Philippe Bruyère <[email protected]>
//
// Copyright (c) 2015 jp
//
// 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 OpenTK;
using System.Collections.Generic;
using GGL;
using OpenTK.Graphics.OpenGL;
using System.Linq;
using System.Diagnostics;
namespace Opuz2015
{
public enum CutType{
Simple,
Curvy,
Diamond,
}
public class Cutter
{
static Random rnd = new Random();
public bool TutIsInside = false;
int resolution = 20;
float tutWidth = 0.10f;
float tutWidthVariance = 0.3f;
float tutHeight = 0.3f;
float tutHeightVariance = 0.5f;
float tutPosVariance = 0.3f;
public int NbPoints {
get {
switch (CutType) {
case CutType.Simple:
return resolution + 1;
case CutType.Curvy:
return (resolution-1) * 3;
case CutType.Diamond:
return resolution + 1;
}
return -1;
}
}
CutType CutType = CutType.Simple;
public Cutter(CutType _type = CutType.Simple){
CutType = _type;
}
public Vector3[] Cut (float startPos, float length)
{
float tw = length * tutWidth;
float th = length * tutHeight;
float tp = length / 2f;
tw = tw + (float)(0.5 - rnd.NextDouble())*tw*tutWidthVariance;
th = th + (float)(0.5 - rnd.NextDouble())*th*tutHeightVariance;
tp = tp + (float)(0.5 - rnd.NextDouble())*length*tutPosVariance;
if (rnd.NextDouble () < 0.5) {
th = -th;
TutIsInside = true;
}
List<Vector3> points = new List<Vector3> ();
Vector3[] p = null;
switch (CutType) {
case CutType.Simple:
p = new Vector3[] {
new Vector3 (startPos, 0, 0),
new Vector3 (startPos + tp - tw, 0, 0),
new Vector3 (startPos + tp - tw * 3.0f, th, 0),
new Vector3 (startPos + tp + tw * 3.0f, th, 0),
new Vector3 (startPos + tp + tw, 0, 0),
new Vector3 (startPos + length, 0, 0)
};
points.Add (p [0]);
for (int i = 0; i < resolution; i++) {
float t = i / (float)(resolution-1);
points.Add(CalculateBezierPoint(t,p[1],p[2],p[3],p[4]));
}
break;
case CutType.Curvy:
p = new Vector3[] {
new Vector3 (startPos, 0, 0),
new Vector3 (startPos + tp * 0.3f, 0, 0),
new Vector3 (startPos + tp - tw * 0.3f, -th * 0.3f, 0),
new Vector3 (startPos + tp - tw, 0, 0),
new Vector3 (startPos + tp - tw * 3.0f, th, 0),
new Vector3 (startPos + tp + tw * 3.0f, th, 0),
new Vector3 (startPos + tp + tw, 0, 0),
new Vector3 (startPos + tp + tw * 0.3f, -th * 0.3f, 0),
new Vector3 (startPos + length - (length - tp) * 0.3f, 0, 0),
new Vector3 (startPos + length, 0, 0)
};
for (int i = 0; i < resolution-1; i++) {
float t = i / (float)(resolution-1);
points.Add(CalculateBezierPoint(t,p[0],p[1],p[2],p[3]));
}
for (int i = 0; i < resolution-1; i++) {
float t = i / (float)(resolution-1);
points.Add(CalculateBezierPoint(t,p[3],p[4],p[5],p[6]));
}
for (int i = 0; i < resolution-1; i++) {
float t = i / (float)(resolution-1);
points.Add(CalculateBezierPoint(t,p[6],p[7],p[8],p[9]));
}
break;
case CutType.Diamond:
break;
}
return points.ToArray ();
}
static Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * tt * p2;
p += ttt * p3;
return p;
}
}
}