-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcylinder.cpp
154 lines (135 loc) · 3.94 KB
/
cylinder.cpp
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
#include "cylinder.h"
#include <maya/MMatrix.h>
#include <math.h>
MPointArray CylinderMesh::gPoints;
MVectorArray CylinderMesh::gNormals;
MIntArray CylinderMesh::gFaceCounts;
MIntArray CylinderMesh::gFaceConnects;
CylinderMesh::CylinderMesh(
const MPoint& start, const MPoint& end, double _r) :
mStart(start), mEnd(end), r(_r)
{
if (gPoints.length() == 0)
{
initCylinderMesh(r);
}
}
CylinderMesh::~CylinderMesh(){}
void CylinderMesh::transform(MPointArray& points, MVectorArray& normals)
{
MVector forward = mEnd - mStart;
double s = forward.length();
forward.normalize();
MVector left = MVector(0,0,1)^forward;
MVector up;
if (left.length() < 0.0001)
{
up = forward^MVector(0,1,0);
left = up^forward;
}
else
{
up = forward^left;
}
MMatrix mat;
mat[0][0] = forward[0]; mat[0][1] = left[0]; mat[0][2] = up[0]; mat[0][3] = 0;
mat[1][0] = forward[1]; mat[1][1] = left[1]; mat[1][2] = up[1]; mat[1][3] = 0;
mat[2][0] = forward[2]; mat[2][1] = left[2]; mat[2][2] = up[2]; mat[2][3] = 0;
mat[3][0] = 0; mat[3][1] = 0; mat[3][2] = 0; mat[3][3] = 1;
mat = mat.transpose();
for (int i = 0; i < gPoints.length(); i++)
{
MPoint p = gPoints[i];
p.x = p.x * s; // scale
p = p * mat + mStart; // transform
points.append(p);
MVector n = gNormals[i] * mat;
n.normalize();
normals.append(n);
}
}
void CylinderMesh::appendToMesh(
MPointArray& points,
MIntArray& faceCounts,
MIntArray& faceConnects)
{
MPointArray cpoints;
MVectorArray cnormals;
transform(cpoints, cnormals);
int startIndex = points.length(); // offset for indexes
for (int i = 0; i < cpoints.length(); i++)
{
points.append(cpoints[i]);
}
for (int i = 0; i < gFaceCounts.length(); i++)
{
faceCounts.append(gFaceCounts[i]);
}
for (int i = 0; i < gFaceConnects.length(); i++)
{
faceConnects.append(gFaceConnects[i]+startIndex);
}
}
void CylinderMesh::getMesh(
MPointArray& points,
MIntArray& faceCounts,
MIntArray& faceConnects)
{
MVectorArray cnormals;
transform(points, cnormals);
faceCounts = gFaceCounts;
faceConnects = gFaceConnects;
}
void CylinderMesh::initCylinderMesh(double r)
{
int numslices = 10;
double angle = M_PI*2/numslices;
// Add points and normals
gPoints.clear();
gNormals.clear();
gFaceCounts.clear();
gFaceConnects.clear();
for (int i = 0; i < numslices; i++)
{
gPoints.append(MPoint(0,r*cos(angle*i), r*sin(angle*i)));
gNormals.append(MVector(0,r*cos(angle*i), r*sin(angle*i)));
}
for (int i = 0; i < numslices; i++)
{
gPoints.append(MPoint(1,r*cos(angle*i), r*sin(angle*i)));
gNormals.append(MVector(0,r*cos(angle*i), r*sin(angle*i)));
}
// endcap 1
gPoints.append(MPoint(0,0,0));
gNormals.append(MVector(-1,0,0));
// endcap 2
gPoints.append(MPoint(1,0,0));
gNormals.append(MVector(1,0,0));
// Set indices for endcap 1
for (int i = 0; i < numslices; i++)
{
gFaceCounts.append(3); // append triangle
gFaceConnects.append(2*numslices);
gFaceConnects.append((i+1)%numslices);
gFaceConnects.append(i);
}
// Set indices for endcap 2
for (int i = numslices; i < 2*numslices; i++)
{
gFaceCounts.append(3); // append triangle
gFaceConnects.append(2*numslices+1);
gFaceConnects.append(i);
int next = i+1;
if (next >= 2*numslices) next = numslices;
gFaceConnects.append(next);
}
// Set indices for middle
for (int i = 0; i < numslices; i++)
{
gFaceCounts.append(4); // append quad
gFaceConnects.append(i);
gFaceConnects.append((i+1)%numslices);
gFaceConnects.append((i+1)%numslices+numslices);
gFaceConnects.append(i+numslices);
}
}