summaryrefslogtreecommitdiff
path: root/graphics/asymptote/runpath3d.in
blob: 4246c68424e942e618a5631ab16cba9cb9db882d (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*****
 * runpath3.in
 *
 * Runtime functions for path3 operations.
 *
 *****/

pair     => primPair()
triple   => primTriple()
path3     => primPath3()
boolarray* => booleanArray()
realarray* => realArray()
realarray2* => realArray2()
triplearray* => tripleArray()
triplearray2* => tripleArray2()

#include "path3.h"
#include "array.h"
#include "drawsurface.h"
#include "predicates.h"

using namespace camp;
using namespace vm;

typedef array boolarray;
typedef array realarray;
typedef array realarray2;
typedef array triplearray;
typedef array triplearray2;

using types::booleanArray;
using types::realArray;
using types::realArray2;
using types::tripleArray;
using types::tripleArray2;

// Autogenerated routines:


path3 path3(triplearray *pre, triplearray *point, triplearray *post,
            boolarray *straight, bool cyclic)
{
  size_t n=checkArrays(pre,point);
  checkEqual(n,checkArray(post));
  checkEqual(n,checkArray(straight));
  mem::vector<solvedKnot3> nodes(n);
  for(size_t i=0; i < n; ++i) {
    nodes[i].pre=read<triple>(pre,i);
    nodes[i].point=read<triple>(point,i);
    nodes[i].post=read<triple>(post,i);
    nodes[i].straight=read<bool>(straight,i);
  }

  return path3(nodes,(Int) n,cyclic);
}

path3 :nullPath3()
{
  return nullpath3;
}

bool ==(path3 a, path3 b)
{
  return a == b;
}

bool !=(path3 a, path3 b)
{
  return !(a == b);
}

triple point(path3 p, Int t)
{
  return p.point((Int) t);
}

triple point(path3 p, real t)
{
  return p.point(t);
}

triple precontrol(path3 p, Int t)
{
  return p.precontrol((Int) t);
}

triple precontrol(path3 p, real t)
{
  return p.precontrol(t);
}

triple postcontrol(path3 p, Int t)
{
  return p.postcontrol((Int) t);
}

triple postcontrol(path3 p, real t)
{
  return p.postcontrol(t);
}

triple dir(path3 p, Int t, Int sign=0, bool normalize=true)
{
  return p.dir(t,sign,normalize);
}

triple dir(path3 p, real t, bool normalize=true)
{
  return p.dir(t,normalize);
}

triple accel(path3 p, Int t, Int sign=0)
{
  return p.accel(t,sign);
}

triple accel(path3 p, real t)
{
  return p.accel(t);
}

real radius(path3 p, real t)
{
  triple v=p.dir(t,false);
  triple a=p.accel(t);
  real d=dot(a,v);
  real v2=v.abs2();
  real a2=a.abs2();
  real denom=v2*a2-d*d;
  real r=v2*sqrt(v2);
  return denom > 0 ? r/sqrt(denom) : 0.0;
}

real radius(triple z0, triple c0, triple c1, triple z1, real t)
{
  triple v=(3.0*(z1-z0)+9.0*(c0-c1))*t*t+(6.0*(z0+c1)-12.0*c0)*t+3.0*(c0-z0);
  triple a=6.0*(z1-z0+3.0*(c0-c1))*t+6.0*(z0+c1)-12.0*c0;
  real d=dot(a,v);
  real v2=v.abs2();
  real a2=a.abs2();
  real denom=v2*a2-d*d;
  real r=v2*sqrt(v2);
  return denom > 0 ? r/sqrt(denom) : 0.0;
}

path3 reverse(path3 p)
{
  return p.reverse();
}

path3 subpath(path3 p, Int a, Int b)
{
  return p.subpath((Int) a, (Int) b);
}

path3 subpath(path3 p, real a, real b)
{
  return p.subpath(a,b);
}

Int length(path3 p)
{
  return p.length();
}

bool cyclic(path3 p)
{
  return p.cyclic();
}

bool straight(path3 p, Int t)
{
  return p.straight(t);
}

path3 unstraighten(path3 p)
{
  return p.unstraighten();
}

// return the maximum distance squared of points c0 and c1 from
// the respective internal control points of z0--z1.
real straightness(triple z0, triple c0, triple c1, triple z1)
{
  return Straightness(z0,c0,c1,z1);
}

// return the straightness of segment i of path3 g.
real straightness(path3 p, Int t)
{
  if(p.straight(t)) return 0;
  return Straightness(p.point(t),p.postcontrol(t),p.precontrol(t+1),
                      p.point(t+1));
}

bool piecewisestraight(path3 p)
{
  return p.piecewisestraight();
}

real arclength(path3 p)
{
  return p.arclength();
}

real arclength(triple z0, triple c0, triple c1, triple z1)
{
  return arcLength(z0,c0,c1,z1);
}

real arctime(path3 p, real dval)
{
  return p.arctime(dval);
}

realarray* intersect(path3 p, path3 q, real fuzz=-1)
{
  bool exact=fuzz <= 0.0;
  if(fuzz < 0)
    fuzz=BigFuzz*::max(::max(length(p.max()),length(p.min())),
                       ::max(length(q.max()),length(q.min())));

  std::vector<real> S,T;
  real s,t;
  if(intersections(s,t,S,T,p,q,fuzz,true,exact)) {
    array *V=new array(2);
    (*V)[0]=s;
    (*V)[1]=t;
    return V;
  } else
    return new array(0);
}

realarray2* intersections(path3 p, path3 q, real fuzz=-1)
{
  bool exact=fuzz <= 0.0;
  if(fuzz < 0)
    fuzz=BigFuzz*::max(::max(length(p.max()),length(p.min())),
                       ::max(length(q.max()),length(q.min())));
  bool single=!exact;

  real s,t;
  std::vector<real> S,T;
  bool found=intersections(s,t,S,T,p,q,fuzz,single,exact);
  if(!found) return new array(0);
  array *V;
  if(single) {
    V=new array(1);
    array *Vi=new array(2);
    (*V)[0]=Vi;
    (*Vi)[0]=s;
    (*Vi)[1]=t;
  } else {
    size_t n=S.size();
    V=new array(n);
    for(size_t i=0; i < n; ++i) {
      array *Vi=new array(2);
      (*V)[i]=Vi;
      (*Vi)[0]=S[i];
      (*Vi)[1]=T[i];
    }
  }
  stable_sort(V->begin(),V->end(),run::compare2<real>());
  return V;
}

realarray* intersect(path3 p, triplearray2 *P, real fuzz=-1)
{
  triple *A;
  copyArray2C(A,P,true,4);
  if(fuzz <= 0) fuzz=BigFuzz*::max(::max(length(p.max()),length(p.min())),
                                   norm(A,16));
  std::vector<real> T,U,V;
  bool found=intersections(T,U,V,p,A,fuzz,true);
  delete[] A;
  if(found) {
    array *W=new array(3);
    (*W)[0]=T[0];
    (*W)[1]=U[0];
    (*W)[2]=V[0];
    return W;
  } else
    return new array(0);
}

realarray2* intersections(path3 p, triplearray2 *P, real fuzz=-1)
{
  triple *A;
  copyArray2C(A,P,true,4);
  if(fuzz <= 0) fuzz=BigFuzz*::max(::max(length(p.max()),length(p.min())),
                                   norm(A,16));
  std::vector<real> T,U,V;
  intersections(T,U,V,p,A,fuzz,false);
  delete[] A;
  size_t n=T.size();
  array *W=new array(n);
  for(size_t i=0; i < n; ++i) {
    array *Wi=new array(3);
    (*W)[i]=Wi;
    (*Wi)[0]=T[i];
    (*Wi)[1]=U[i];
    (*Wi)[2]=V[i];
  }
  return W; // Sorting will done in asy.
}

Int size(path3 p)
{
  return p.size();
}

path3 &(path3 p, path3 q)
{
  return camp::concat(p,q);
}

triple min(path3 p)
{
  return p.min();
}

triple max(path3 p)
{
  return p.max();
}

realarray *mintimes(path3 p)
{
  array *V=new array(3);
  triple v=p.mintimes();
  (*V)[0]=v.getx();
  (*V)[1]=v.gety();
  (*V)[2]=v.getz();
  return V;
}

realarray *maxtimes(path3 p)
{
  array *V=new array(3);
  triple v=p.maxtimes();
  (*V)[0]=v.getx();
  (*V)[1]=v.gety();
  (*V)[2]=v.getz();
  return V;
}

path3 Operator *(realarray2 *t, path3 g)
{
  return transformed(*t,g);
}

pair minratio(path3 g)
{
  return g.ratio(::min);
}

pair maxratio(path3 g)
{
  return g.ratio(::max);
}

// Return a negative (positive) value if a--b--c--cycle is oriented
// counterclockwise (clockwise) when viewed from d or zero if all four
// points are coplanar.
// The value returned is the determinant
// |a.x a.y a.z 1|
// |b.x b.y b.z 1|
// |c.x c.y c.z 1|
// |d.x d.y d.z 1|
real orient(triple a, triple b, triple c, triple d)
{
  real A[]={a.getx(),a.gety(),a.getz()};
  real B[]={b.getx(),b.gety(),b.getz()};
  real C[]={c.getx(),c.gety(),c.getz()};
  real D[]={d.getx(),d.gety(),d.getz()};
  return orient3d(A,B,C,D);
}

// Return a positive (negative) value if e lies inside (outside)
// the sphere passing through the points a,b,c,d oriented so that
// a--b--c--cycle appears in clockwise order when viewed from d
// or zero if all five points are cospherical.
// The value returned is the determinant
// |a.x a.y a.z a.x^2+a.y^2+a.z^2 1|
// |b.x b.y b.z b.x^2+b.y^2+b.z^2 1|
// |c.x c.y c.z c.x^2+c.y^2+c.z^2 1|
// |d.x d.y d.z d.x^2+d.y^2+d.z^2 1|
// |e.x e.y e.z e.x^2+e.y^2+e.z^2 1|
real insphere(triple a, triple b, triple c, triple d, triple e)
{
  real A[]={a.getx(),a.gety(),a.getz()};
  real B[]={b.getx(),b.gety(),b.getz()};
  real C[]={c.getx(),c.gety(),c.getz()};
  real D[]={d.getx(),d.gety(),d.getz()};
  real E[]={e.getx(),e.gety(),e.getz()};
  return insphere(A,B,C,D,E);
}