forked from vectorgraphics/asymptote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawpath3.cc
288 lines (236 loc) · 6.55 KB
/
drawpath3.cc
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*****
* drawpath3.cc
*
* Stores a path3 that has been added to a picture.
*****/
#include "drawpath3.h"
#include "drawsurface.h"
#include "material.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
namespace camp {
using vm::array;
using namespace prc;
using gl::modelView;
BezierCurve drawPath3::R;
bool drawPath3::write(prcfile *out, unsigned int *, double, groupsmap&)
{
Int n=g.length();
if(n == 0 || invisible)
return true;
if(straight) {
triple *controls=new(UseGC) triple[n+1];
for(Int i=0; i <= n; ++i)
controls[i]=g.point(i);
out->addLine(n+1,controls,color);
} else {
int m=3*n+1;
triple *controls=new(UseGC) triple[m];
controls[0]=g.point((Int) 0);
controls[1]=g.postcontrol((Int) 0);
size_t k=1;
for(Int i=1; i < n; ++i) {
controls[++k]=g.precontrol(i);
controls[++k]=g.point(i);
controls[++k]=g.postcontrol(i);
}
controls[++k]=g.precontrol(n);
controls[++k]=g.point(n);
out->addBezierCurve(m,controls,color);
}
return true;
}
void drawPath3::render(double size2, const triple& b, const triple& B,
double perspective, bool transparent)
{
#ifdef HAVE_GL
Int n=g.length();
if(n == 0 || invisible || ((color.A < 1.0) ^ transparent))
return;
const bool billboard=interaction == BILLBOARD &&
!settings::getSetting<bool>("offscreen");
triple m,M;
double f,F,s;
if(perspective) {
f=Min.getz()*perspective;
F=Max.getz()*perspective;
m=triple(min(f*b.getx(),F*b.getx()),min(f*b.gety(),F*b.gety()),b.getz());
M=triple(max(f*B.getx(),F*B.getx()),max(f*B.gety(),F*B.gety()),B.getz());
s=max(f,F);
} else {
m=b;
M=B;
s=1.0;
}
const pair size3(s*(B.getx()-b.getx()),s*(B.gety()-b.gety()));
bbox3 box(m,M);
box.transform(modelView.Tinv);
m=box.Min();
M=box.Max();
if(!billboard && (Max.getx() < m.getx() || Min.getx() > M.getx() ||
Max.gety() < m.gety() || Min.gety() > M.gety() ||
Max.getz() < m.getz() || Min.getz() > M.getz()))
return;
RGBAColour Black(0.0,0.0,0.0,color.A);
setcolors(false,Black,Black,color,Black,1.0);
if(billboard) {
for(Int i=0; i < n; ++i) {
triple controls[]={BB.transform(g.point(i)),BB.transform(g.postcontrol(i)),
BB.transform(g.precontrol(i+1)),
BB.transform(g.point(i+1))};
R.queue(controls,straight,size3.length()/size2,m,M);
}
} else {
BB.init(center);
for(Int i=0; i < n; ++i) {
triple controls[]={g.point(i),g.postcontrol(i),g.precontrol(i+1),
g.point(i+1)};
R.queue(controls,straight,size3.length()/size2,m,M);
}
}
R.draw();
#endif
}
drawElement *drawPath3::transformed(const double* t)
{
return new drawPath3(t,this);
}
bool drawNurbsPath3::write(prcfile *out, unsigned int *, double, groupsmap&)
{
if(invisible)
return true;
out->addCurve(degree,n,controls,knots,color,weights);
return true;
}
// Approximate bounds by bounding box of control polyhedron.
void drawNurbsPath3::bounds(const double* t, bbox3& b)
{
double x,y,z;
double X,Y,Z;
triple* Controls;
if(t == NULL) Controls=controls;
else {
Controls=new triple[n];
for(size_t i=0; i < n; i++)
Controls[i]=t*controls[i];
}
boundstriples(x,y,z,X,Y,Z,n,Controls);
b.add(x,y,z);
b.add(X,Y,Z);
if(t == NULL) {
Min=triple(x,y,z);
Max=triple(X,Y,Z);
} else delete[] Controls;
}
drawElement *drawNurbsPath3::transformed(const double* t)
{
return new drawNurbsPath3(t,this);
}
void drawNurbsPath3::ratio(const double* t, pair &b, double (*m)(double, double),
double, bool &first)
{
triple* Controls;
if(t == NULL) Controls=controls;
else {
Controls=new triple[n];
for(size_t i=0; i < n; i++)
Controls[i]=t*controls[i];
}
if(first) {
first=false;
triple v=Controls[0];
b=pair(xratio(v),yratio(v));
}
double x=b.getx();
double y=b.gety();
for(size_t i=0; i < n; ++i) {
triple v=Controls[i];
x=m(x,xratio(v));
y=m(y,yratio(v));
}
b=pair(x,y);
if(t != NULL)
delete[] Controls;
}
void drawNurbsPath3::displacement()
{
#ifdef HAVE_GL
size_t nknots=degree+n+1;
if(Controls == NULL) {
Controls=new(UseGC) GLfloat[(weights ? 4 : 3)*n];
Knots=new(UseGC) GLfloat[nknots];
}
if(weights)
for(size_t i=0; i < n; ++i)
store(Controls+4*i,controls[i],weights[i]);
else
for(size_t i=0; i < n; ++i)
store(Controls+3*i,controls[i]);
for(size_t i=0; i < nknots; ++i)
Knots[i]=knots[i];
#endif
}
void drawNurbsPath3::render(double, const triple&, const triple&,
double, bool transparent)
{
#ifdef HAVE_GL
if(invisible || ((color.A < 1.0) ^ transparent))
return;
GLfloat Diffuse[]={0.0,0.0,0.0,(GLfloat) color.A};
glMaterialfv(GL_FRONT,GL_DIFFUSE,Diffuse);
static GLfloat Black[]={0.0,0.0,0.0,1.0};
glMaterialfv(GL_FRONT,GL_AMBIENT,Black);
GLfloat Emissive[]={(GLfloat) color.R,(GLfloat) color.G,(GLfloat) color.B,
(GLfloat) color.A};
glMaterialfv(GL_FRONT,GL_EMISSION,Emissive);
glMaterialfv(GL_FRONT,GL_SPECULAR,Black);
glMaterialf(GL_FRONT,GL_SHININESS,128.0);
#endif
}
bool drawPixel::write(prcfile *out, unsigned int *, double, groupsmap&)
{
if(invisible)
return true;
out->addPoint(v,color,width);
return true;
}
void drawPixel::render(double size2, const triple& b, const triple& B,
double perspective, bool transparent)
{
#ifdef HAVE_GL
if(invisible || ((color.A < 1.0) ^ transparent)) return;
triple m,M;
double f,F,s;
if(perspective) {
f=Min.getz()*perspective;
F=Max.getz()*perspective;
m=triple(min(f*b.getx(),F*b.getx()),min(f*b.gety(),F*b.gety()),b.getz());
M=triple(max(f*B.getx(),F*B.getx()),max(f*B.gety(),F*B.gety()),B.getz());
s=max(f,F);
} else {
m=b;
M=B;
s=1.0;
}
const pair size3(s*(B.getx()-b.getx()),s*(B.gety()-b.gety()));
bbox3 box(m,M);
box.transform(modelView.Tinv);
m=box.Min();
M=box.Max();
if((Max.getx() < m.getx() || Min.getx() > M.getx() ||
Max.gety() < m.gety() || Min.gety() > M.gety() ||
Max.getz() < m.getz() || Min.getz() > M.getz()))
return;
RGBAColour Black(0.0,0.0,0.0,color.A);
setcolors(false,color,Black,color,Black,1.0);
glPointSize(1.0+width);
R.draw(v);
glPointSize(1.0);
#endif
}
drawElement *drawPixel::transformed(const double* t)
{
return new drawPixel(t*v,p,width,KEY);
}
} //namespace camp