-
Notifications
You must be signed in to change notification settings - Fork 30
/
splineCurveCubic.m
243 lines (175 loc) · 5.01 KB
/
splineCurveCubic.m
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
addpath ../C_files/
addpath ../fem_util/
addpath ../examples/
addpath ../meshing/
addpath ../nurbs-util/
clear all
% backtracking parameters
alpha = 0.1;
beta = 0.7;
% initial curve
knotVec = [0 0 0 1 1 1];
controlPts = [0 0; -0.5 -4;3 -1];
weights = [1 1 1]; % b-spline curves
p = 2;
noCtrPts = size(controlPts,1);
cp = zeros(4,noCtrPts);
cp(1:2,:) = controlPts';
cp(4,:) = weights;
originalCurve = nrbmak(cp,knotVec);
figure
hold on
nrbctrlplot(originalCurve);
axis equal
axis off
opts = struct('Color','rgb','Bounds','tight','FontMode','fixed','FontSize',20);
%exportfig(gcf,'splinecurve.eps',opts)
% generating points on offset curve
thickness = -1.;
% computing offset points
noPts=100;
xi=linspace(0,1,noPts);
offsetPts = zeros(noPts,2);
for i=1:length(xi)
xii = xi(i);
[N dNdxi] = NURBS1DBasisDers(xii,p,knotVec,weights);
s = findspan(noCtrPts-1,p,xii,knotVec);
pts = controlPts(s-p 1:s-p 1 p,:);
x = N * pts;
dx = dNdxi* pts;
dx = dx/norm(dx);
offsetPts(i,:) = x - [-dx(2) dx(1)]*thickness;
end
figure
hold on
nrbctrlplot(originalCurve);
axis equal
axis off
plot(offsetPts(:,1),offsetPts(:,2),'r*-','LineWidth',1.3);
%% gradient descent method
% initial guess curve = a line
cpoints = controlPts(:,1:2);
cpoints(1, :) = offsetPts(1,:);
cpoints(end,:) = offsetPts(end,:);
vec = cpoints(end,:) - cpoints(1,:);
dlam = 1/(noCtrPts-1);
for i=1:noCtrPts-2
cpoints(1 i,:) = cpoints(1,:) dlam*i*vec;
end
cpoints0 = cpoints;
plot(cpoints0(:,1),cpoints0(:,2),'r-o',...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
samPts = computePoints(knotVec,weights,cpoints,xi);
figure
hold on
axis equal
axis off
plot(offsetPts(:,1),offsetPts(:,2),'r*-','LineWidth',1.3);
plot(samPts(:,1),samPts(:,2),'bs-','LineWidth',1.2);
energy = getEnergy(offsetPts,samPts);
% disturbe the guess curve to compute numerically the gradient of the
% energy
h = 1e-8;
eps = 0.1;
eps1 = 0.01;
error = 10;
error1 = 10;
dgamma = 0.0001;
gamma = 0;
iiter = 1;
while error > eps
samPts = computePoints(knotVec,weights,cpoints,xi);
f = getEnergy(offsetPts,samPts);
% determine the gradients numerically
for i = 1:noCtrPts-2
cpoints(1 i,:) = cpoints(1 i,:) [h 0];
samPts = computePoints(knotVec,weights,cpoints,xi);
energy1x = getEnergy(offsetPts,samPts);
cpoints(1 i,:) = cpoints(1 i,:) - [h 0] [0 h];
samPts = computePoints(knotVec,weights,cpoints,xi);
energy1y = getEnergy(offsetPts,samPts);
dir = 1/h*[energy1x-energy energy1y-energy];
grad(i,:) = dir;
cpoints(i 1,:) = cpoints0(i 1,:);
end
s = 1;
for k=1:10
cpnew = cpoints0;
cpnew(2:end-1,:) = cpnew(2:end-1,:) - grad*s;
samPts = computePoints(knotVec,weights,cpnew,xi);
fnew = getEnergy(offsetPts,samPts);
if (fnew < f s*alpha*(-grad)'*grad)
break;
else
s = s*beta;
end
end
cpoints0(2:end-1,:) = cpoints0(2:end-1,:) - grad*s;
cpoints = cpoints0;
samPts = computePoints(knotVec,weights,cpoints,xi);
energy = getEnergy(offsetPts,samPts);
error = energy;
disp (sprintf(' %s %i %s %5.4e ', 'Iter',iiter, ':', error) );
iiter = iiter 1;
end
%% plot result
figure
hold on
plot(offsetPts(:,1),offsetPts(:,2),'r--','LineWidth',1.3);
%plot(samPts(:,1), samPts(:,2),'cy--','LineWidth',1.3);
nrbctrlplot(originalCurve);
axis equal
cp = zeros(4,size(controlPts,1));
cp(1:2,:) = cpoints0';
cp(4,:) = weights;
offsetCurve = nrbmak(cp,knotVec);
nrbctrlplot(offsetCurve);
%%
% extruded surface
srf1 = nrbextrude(originalCurve, [0,0,1]);
srf2 = nrbextrude(offsetCurve, [0,0,1]);
figure
hold on
nrbctrlplot(srf1);
nrbctrlplot(srf2);
% make a volume from two surfaces
volumePts = zeros(4,noCtrPts,2,2);
volumePts(1:4,:,1,1) = srf1.coefs(:,:,1);
volumePts(1:4,:,2,1) = srf1.coefs(:,:,2);
volumePts(1:4,:,1,2) = srf2.coefs(:,:,1);
volumePts(1:4,:,2,2) = srf2.coefs(:,:,2);
uKnot = knotVec;
vKnot = [0 0 1 1];
wKnot = [0 0 1 1];
vol = nrbmak(volumePts,{uKnot vKnot wKnot});
nrbplot(vol,[20 2 2])
axis off
%% continuum elements in between the curves
solidPts = zeros(4,noCtrPts,2);
solidPts(1:2,:,1) = controlPts';
solidPts(1:2,:,2) = cpoints';
solidPts(4,:) = 1;
uKnot = knotVec;
vKnot = [0 0 1 1];
solid = nrbmak(solidPts,{uKnot vKnot});
figure
hold on
nrbctrlplot(solid)
axis off
refineLevel = 3;
for i=1:refineLevel
uKnotVectorU = unique(uKnot);
uKnotVectorV = unique(vKnot);
% new knots along two directions
newKnotsX = uKnotVectorU(1:end-1) 0.5*diff(uKnotVectorU);
newKnotsY = uKnotVectorV(1:end-1) 0.5*diff(uKnotVectorV);
newKnots = {newKnotsX []};
solid = nrbkntins(solid,newKnots);
uKnot = cell2mat(solid.knots(1));
vKnot = cell2mat(solid.knots(2));
end
%%
convert2DNurbs
plotMesh (controlPts,weights,uKnot,vKnot,p,q,100,'r--','try.eps');