forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_math.cpp
5347 lines (4835 loc) · 131 KB
/
opennurbs_math.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
/*
Description:
Test math library functions.
Parameters:
function_index - [in] Determines which math library function is called.
1: z = x+y
2: z = x-y
3: z = x*y
4: z = x/y
5: z = fabs(x)
6: z = exp(x)
7: z = log(x)
8: z = log10(x)
9: z = frexp(x)
10: z = pow(x,y)
11: z = sqrt(x)
12: z = sin(x)
13: z = cos(x)
14: z = tan(x)
15: z = sinh(x)
16: z = cosh(x)
17: z = tanh(x)
18: z = asin(x)
19: z = acos(x)
20: z = atan(x)
21: z = atan2(y,x)
22: z = fmod(x,y)
23: z = modf(x,&y)
double x - [in]
double y - [in]
Returns:
Returns the "z" value listed in the function_index parameter
description.
Remarks:
This function is used to test the results of class floating
point functions. It is primarily used to see what happens
when opennurbs is used as a DLL and illegal operations are
performed.
*/
double ON_TestMathFunction( int function_index, double x, double y )
{
// This function is used to test the results of performing operations.
//
// module function
// opennurbs.dll ON_TestMathFunction
// tl.dll TL_TestMathFunction
// rhino.exe Rhino_TestMathFunction
double z = ON_UNSET_VALUE;
int i;
switch(function_index)
{
case 1: // addition
z = x+y;
break;
case 2: // subtraction
z = x-y;
break;
case 3: // multiplication
z = x*y;
break;
case 4: // division
z = x/y;
break;
case 5: // absolute value
z = fabs(x);
break;
case 6: // exp
z = exp(x);
break;
case 7: // log
z = log(x);
break;
case 8: // log10
z = log10(x);
break;
case 9: // frexp
z = frexp(x,&i);
break;
case 10: // pow
z = pow(x,y);
break;
case 11: // square root
z = sqrt(x);
break;
case 12: // sine
z = sin(x);
break;
case 13: // cosine
z = cos(x);
break;
case 14: // tangent
z = tan(x);
break;
case 15: // hyperbolic sine
z = sinh(x);
break;
case 16: // hyperbolic cosine
z = cosh(x);
break;
case 17: // hyperbolic tangent
z = tanh(x);
break;
case 18: // arcsine
z = asin(x);
break;
case 19: // arccosine
z = acos(x);
break;
case 20: // arctangent
z = atan(x);
break;
case 21: // arctangent
z = atan2(y,x);
break;
case 22:
z = fmod(x,y);
break;
case 23:
z = modf(x,&y);
break;
default:
z = 0.0;
break;
}
return z;
}
bool ON_PassesNanTest()
{
bool bPassesAllNanTests = false;
for (;;)
{
const double nan1 = ON_DBL_QNAN;
const double nan2 = ON_DBL_QNAN;
const double zero = 0.0;
const double one = 1.0;
// nan != * and * != nan should always be true
const bool b_NE_test
= nan1 != nan1
&& nan1 != nan2
&& nan1 != zero
&& nan1 != one
&& zero != nan2
&& one != nan2
;
// nan op * and * op nan when op is ==, < > <= >= should all be false
const bool b_EQ_test
= nan1 == nan1
|| nan1 == nan2
|| nan1 == zero
|| nan1 == one
|| zero == nan2
|| one == nan2
;
const bool b_LT_test
= nan1 < nan1
|| nan1 < nan2
|| nan1 < zero
|| nan1 < one
|| zero < nan2
|| one < nan2
;
const bool b_GT_test
= nan1 > nan1
|| nan1 > nan2
|| nan1 > zero
|| nan1 > one
|| zero > nan2
|| one > nan2
;
const bool b_LE_test
= nan1 <= nan1
|| nan1 <= nan2
|| nan1 <= zero
|| nan1 <= one
|| zero <= nan2
|| one <= nan2
;
const bool b_GE_test
= nan1 >= nan1
|| nan1 >= nan2
|| nan1 >= zero
|| nan1 >= one
|| zero >= nan2
|| one >= nan2
;
const bool bPassesIEE754NanCompareTests
= b_NE_test
&& false == b_EQ_test
&& false == b_LT_test
&& false == b_GT_test
&& false == b_LE_test
&& false == b_GE_test
;
if (false == bPassesIEE754NanCompareTests)
{
// some opennurbs code will fail.
ON_ERROR("This compiler does not conform to the IEEE-754 nan compare specification. Some opennurbs code will fail.");
break;
}
const double x[] = {
nan1 + one, one + nan1,
nan1 - one, one - nan1,
nan1 * one, one * nan1,
nan1 / one, one / nan1
};
const size_t xcount = sizeof(x) / sizeof(x[0]);
bool bPassesNanAritmeticTest = true;
for (size_t i = 0; i < xcount && bPassesNanAritmeticTest; ++i)
{
bPassesNanAritmeticTest = x[i] != x[i];
}
if (false == bPassesNanAritmeticTest)
{
// some opennurbs code will fail.
ON_ERROR("This compiler does not conform to the IEEE-754 nan arithmetic specification. Some opennurbs code will fail.");
break;
}
bPassesAllNanTests = true;
break;
}
return bPassesAllNanTests;
}
double ON_DegreesFromRadians(
double angle_in_radians
)
{
if (!ON_IsValid(angle_in_radians))
return angle_in_radians;
double d = angle_in_radians*ON_RADIANS_TO_DEGREES;
const double scale[] = { 1.0, 2.0, 4.0, 8.0, 0.0 };
for (int i = 0; scale[i] > 0.0; i++)
{
double ds = d*scale[i];
double f = floor(ds);
if (f + 0.5 < ds)
f += 1.0;
if (fabs(f - ds) < ON_EPSILON*scale[i])
{
d = f/scale[i];
break;
}
}
return d;
}
double ON_RadiansFromDegrees(
double angle_in_degrees
)
{
return
(ON_IsValid(angle_in_degrees))
? (angle_in_degrees*ON_DEGREES_TO_RADIANS)
: angle_in_degrees;
}
double ON_ArrayDotProduct(int dim, const double* A, const double* B)
{
double AoB;
// do low dimensional cases on one line so we get 80 bit
// intermediate precision in optimized mode.
if (dim==1) return (A[0]*B[0]);
if (dim==2) return (A[0]*B[0] + A[1]*B[1]);
if (dim==3) return (A[0]*B[0] + A[1]*B[1] + A[2]*B[2]);
if (dim==4) return (A[0]*B[0] + A[1]*B[1] + A[2]*B[2] +A[3]*B[3]);
AoB = 0.0;
while (dim--) AoB += *A++ * *B++;
return AoB;
}
double
ON_ArrayDotDifference( int dim, const double* A, const double* B, const double* C )
{
// returns A o ( B - C )
double AoBminusC; // low dim cases inline for better optimization
if (dim==1) return (A[0]*(B[0] - C[0]));
if (dim==2) return (A[0]*(B[0] - C[0]) + A[1]*(B[1] - C[1]));
if (dim==3) return (A[0]*(B[0] - C[0]) + A[1]*(B[1] - C[1]) + A[2]*(B[2] - C[2]));
AoBminusC = 0.0;
while (dim--) AoBminusC += *A++ * (*B++ - *C++);
return AoBminusC;
}
double ON_ArrayDistance(int dim, const double *A, const double *B)
{
// returns sqrt((A-B)o(A-B))
double a, b, c, len;
switch(dim) {
case 1:
len = fabs(*B - *A);
break;
case 2:
a = fabs(*B++ - *A++); b = fabs(*B - *A);
if (a > b)
{b /= a; len = a*sqrt(1.0+b*b);}
else if (b > a)
{a /= b; len = b*sqrt(1.0+a*a);}
else
len = a*ON_SQRT2;
break;
case 3:
a = fabs(*B++ - *A++); b = fabs(*B++ - *A++); c = fabs(*B - *A);
if (a >= b) {
if (a >= c) {
if (a == 0.0) len = 0.0;
else if (a == b && a == c) len = a*ON_SQRT3;
else {b /= a; c /= a; len = a*sqrt(1.0 + (b*b + c*c));}
}
else
{a /= c; b /= c; len = c*sqrt(1.0 + (a*a + b*b));}
}
else if (b >= c)
{a /= b; c /= b; len = b*sqrt(1.0 + (a*a + c*c));}
else
{b /= c; a /= c; len = c*sqrt(1.0 + (a*a + b*b));}
break;
default:
len = 0.0;
while (dim--) {a = *B++ - *A++; len += a*a;}
len = sqrt(len);
break;
}
return len;
}
double ON_ArrayDistanceSquared(int dim, const double* A, const double* B)
{
// returns (A-B)o(A-B)
double x, dist_sq = 0.0;
while (dim--) {
x = (*B++) - (*A++);
dist_sq += x*x;
}
return dist_sq;
}
double ON_ArrayMagnitude(int dim, const double* A)
{
double a, b, c, len;
switch(dim) {
case 1:
len = fabs(*A);
break;
case 2:
a = fabs(*A++); b = fabs(*A);
if (a > b)
{b /= a; len = a*sqrt(1.0+b*b);}
else if (b > a)
{a /= b; len = b*sqrt(1.0+a*a);}
else
len = a*ON_SQRT2;
break;
case 3:
a = fabs(*A++); b = fabs(*A++); c = fabs(*A);
if (a >= b) {
if (a >= c) {
if (a == b && a == c)
len = a*ON_SQRT3;
else
{b /= a; c /= a; len = a*sqrt(1.0 + (b*b + c*c));}
}
else
{a /= c; b /= c; len = c*sqrt(1.0 + (a*a + b*b));}
}
else if (b >= c)
{a /= b; c /= b; len = b*sqrt(1.0 + (a*a + c*c));}
else
{b /= c; a /= c; len = c*sqrt(1.0 + (a*a + b*b));}
break;
default:
len = 0.0;
while (dim--) {a = *A++; len += a*a;}
len = sqrt(len);
break;
}
return len;
}
double ON_ArrayMagnitudeSquared(int dim, const double* A)
{
double x, len_sq=0.0;
while (dim--) {
x = *A++;
len_sq += x*x;
}
return len_sq;
}
void
ON_ArrayScale( int dim, double s, const double* A, double* sA )
{
if ( dim > 0 ) {
while ( dim-- )
*sA++ = s * *A++;
}
}
void
ON_Array_aA_plus_B( int dim, double a, const double* A, const double* B, double* aA_plus_B )
{
if ( dim > 0 ) {
while ( dim-- )
*aA_plus_B++ = a * *A++ + *B++;
}
}
float
ON_ArrayDotProduct( int dim, const float* A, const float* B )
{
float d = 0.0;
if ( dim > 0 ) {
while(dim--)
d += *A++ * *B++;
}
return d;
}
void
ON_ArrayScale( int dim, float s, const float* A, float* sA )
{
if ( dim > 0 ) {
while ( dim-- )
*sA++ = s * *A++;
}
}
void
ON_Array_aA_plus_B( int dim, float a, const float* A, const float* B, float* aA_plus_B )
{
if ( dim > 0 ) {
while ( dim-- )
*aA_plus_B++ = a * *A++ + *B++;
}
}
int
ON_DecomposeVector(
const ON_3dVector& V,
const ON_3dVector& A,
const ON_3dVector& B,
double* x, double* y
)
{
int rank;
double pr;
const double AoV = A*V;
const double BoV = B*V;
const double AoA = A*A;
const double AoB = A*B;
const double BoB = B*B;
rank = ON_Solve2x2( AoA, AoB, AoB, BoB, AoV, BoV, x, y, &pr );
return (rank==2)?true:false;
}
bool
ON_EvJacobian( double ds_o_ds, double ds_o_dt, double dt_o_dt,
double* det_addr )
/* Carefully compute the Jacobian determinant
*
* INPUT:
* ds_o_ds, ds_o_dt, dt_o_dt
* Dot products of the first partial derivatives
* det_addr
* address of an unused double
* OUTPUT:
* *det_addr = ds_o_ds*dt_o_dt - ds_o_dt^2
* ON_EvJacobian()
* 0: successful
* -1: failure
*
* COMMENTS:
* ...
*
* EXAMPLE:
* // ...
*
* REFERENCE:
*
*
* RELATED FUNCTIONS:
* ON_EvBsplineBasis(), ON_EvdeCasteljau(), ON_EvBezier()
*/
{
bool rc = false;
double det, a, b;
a = ds_o_ds*dt_o_dt;
b = ds_o_dt*ds_o_dt;
/* NOTE: a = |Du|^2 * |Dv|^2 and b = (Du o Dv)^2 are always >= 0 */
det = a - b;
if (ds_o_ds <= dt_o_dt* ON_EPSILON || dt_o_dt <= ds_o_ds* ON_EPSILON) {
/* one of the partials is (numerically) zero with respect
* to the other partial - value of det is unreliable
*/
rc = false;
}
else if (fabs(det) <= ((a > b) ? a : b)* ON_SQRT_EPSILON) {
/* Du and Dv are (numerically) (anti) parallel -
* value of det is unreliable.
*/
rc = false;
}
else {
rc = true;
}
if (det_addr) *det_addr = det;
return rc;
}
bool
ON_EvNormalPartials(
const ON_3dVector& ds,
const ON_3dVector& dt,
const ON_3dVector& dss,
const ON_3dVector& dst,
const ON_3dVector& dtt,
ON_3dVector& ns,
ON_3dVector& nt
)
{
bool rc = false;
const double ds_o_ds = ds*ds;
const double ds_o_dt = ds*dt;
const double dt_o_dt = dt*dt;
rc = ON_EvJacobian( ds_o_ds, ds_o_dt, dt_o_dt, nullptr );
if (!rc)
{
// degenerate Jacobian and unit surface normal is not well defined
ns = ON_3dVector::ZeroVector;
nt = ON_3dVector::ZeroVector;
}
else
{
// If V: . -> R^3 is nonzero and C^2 and N = V/|V|, then
//
// V' V o V'
// N' = ----- - ------- * V.
// |V| |V|^3
//
// When a surface has a non-degenerate Jacobian, V = ds X dt
// and the derivatives of N may be computed from the first
// and second partials.
ON_3dVector V = ON_CrossProduct(ds,dt);
double len = V.Length();
double len3 = len*len*len;
if (len < ON_EPSILON)
{
ns = ON_3dVector::ZeroVector;
nt = ON_3dVector::ZeroVector;
return false;
}
ns.x = dss.y*dt.z - dss.z*dt.y + ds.y*dst.z - ds.z*dst.y;
ns.y = dss.z*dt.x - dss.x*dt.z + ds.z*dst.x - ds.x*dst.z;
ns.z = dss.x*dt.y - dss.y*dt.x + ds.x*dst.y - ds.y*dst.x;
nt.x = dst.y*dt.z - dst.z*dt.y + ds.y*dtt.z - ds.z*dtt.y;
nt.y = dst.z*dt.x - dst.x*dt.z + ds.z*dtt.x - ds.x*dtt.z;
nt.z = dst.x*dt.y - dst.y*dt.x + ds.x*dtt.y - ds.y*dtt.x;
ns = ns/len - ((V*ns)/len3)*V;
nt = nt/len - ((V*nt)/len3)*V;
}
return rc;
}
bool
ON_Pullback3dVector( // use to pull 3d vector back to surface parameter space
const ON_3dVector& vector, // 3d vector
double distance, // signed distance from vector location to closet point on surface
// < 0 if point is below with respect to Du x Dv
const ON_3dVector& ds, // surface first partials
const ON_3dVector& dt,
const ON_3dVector& dss, // surface 2nd partials
const ON_3dVector& dst, // (used only when dist != 0)
const ON_3dVector& dtt,
ON_2dVector& pullback // pullback
)
{
bool rc = false;
//int bIsDegenerate = false;
if (distance != 0.0) {
ON_3dVector ns, nt;
rc = ON_EvNormalPartials(ds,dt,dss,dst,dtt,ns,nt);
if ( rc ) {
// adjust ds and dt to take account of offset distance
rc = ON_DecomposeVector( vector, ds + distance*ns, dt + distance*nt, &pullback.x, &pullback.y );
}
}
else {
rc = ON_DecomposeVector( vector, ds, dt, &pullback.x, &pullback.y );
}
return rc;
}
bool
ON_GetParameterTolerance(
double t0, double t1, // domain
double t, // parameter in domain
double* tminus, double* tplus// parameter tolerance (tminus, tplus) returned here
)
{
const bool rc = (t0 < t1) ? true : false;
if ( rc ) {
if ( t < t0 )
t = t0;
else if (t > t1 )
t = t1;
double dt = (t1-t0)*8.0* ON_SQRT_EPSILON + (fabs(t0) + fabs(t1))* ON_EPSILON;
if ( dt >= t1-t0 )
dt = 0.5*(t1-t0);
const double tmin = t-dt;
const double tmax = t+dt;
if ( tminus )
*tminus = tmin;
if ( tplus )
*tplus = tmax;
}
return rc;
}
bool
ON_EvNormal(int limit_dir,
const ON_3dVector& Du, const ON_3dVector& Dv,
const ON_3dVector& Duu, const ON_3dVector& Duv, const ON_3dVector& Dvv,
ON_3dVector& N)
{
const double DuoDu = Du.LengthSquared();
const double DuoDv = Du*Dv;
const double DvoDv = Dv.LengthSquared();
if (ON_EvJacobian(DuoDu, DuoDv, DvoDv, nullptr))
{
N = ON_CrossProduct(Du, Dv);
}
else if (Duu.IsValid() && Duv.IsValid() && Dvv.IsValid())
{
/* degenerate jacobian - try to compute normal using limit
*
* P,Du,Dv,Duu,Duv,Dvv = srf and partials evaluated at (u0,v0).
* Su,Sv,Suu,Suv,Svv = partials evaluated at (u,v).
* Assume that srf : R^2 -> R^3 is analytic near (u0,v0).
*
* srf(u0+u,v0+v) = srf(u0,v0) + u*Du + v*Dv
* + (1/2)*u^2*Duu + u*v*Duv + (1/2)v^2*Dvv
* + cubic and higher order terms.
*
* Su X Sv = Du X Dv + u*(Du X Duv + Duu X Dv) + v*(Du X Dvv + Duv X Dv)
* + quadratic and higher order terms
*
* Set
* (1) A = (Du X Duv + Duu X Dv),
* (2) B = (Du X Dvv + Duv X Dv) and assume
* Du X Dv = 0. Then
*
* |Su X Sv|^2 = u^2*AoA + 2uv*AoB + v^2*BoB + cubic and higher order terms
*
* If u = a*t, v = b*t and (a^2*AoA + 2ab*AoB + b^2*BoB) != 0, then
*
* Su X Sv a*A + b*B
* lim --------- = ----------------------------------
* t->0 |Su X Sv| sqrt(a^2*AoA + 2ab*AoB + b^2*BoB)
*
* All I need is the direction, so I just need to compute a*A + b*B as carefully
* as possible. Note that
* (3) a*A + b*B = Du X (a*Duv + b*Dvv) - Dv X (a*Duu + b*Duv).
* Formula (3) requires fewer flops than using formulae (1) and (2) to
* compute a*A + b*B. In addition, when |Du| << |Dv| or |Du| >> |Dv|,
* formula (3) reduces the number of subtractions between numbers of
* similar size. Since the (nearly) zero first partial is the most common
* is more common than the (nearly) (anti) parallel case, I'll use
* formula (3). If you're reading this because you're not getting
* the right answer and you can't find any bugs, you might want to
* try using formulae (1) and (2).
*
* The limit_dir argument determines which direction is used to compute the
* limit.
* |
* limit_dir == 2 | limit_dir == 1
* \ | /
* \ | /
* \ | /
* \ | /
* \ | /
* \ | /
* \|/
* ---------------*--------------
* /|\
* / | \
* / | \
* / | \
* / | \
* / | \
* / | \
* limit_dir == 3 | limit_dir == 4
* |
*
*/
double a, b;
ON_3dVector V, Au, Av;
switch (limit_dir) {
case 2: /* from 2nd quadrant to point */
a = -1.0; b = 1.0; break;
case 3: /* from 3rd quadrant to point */
a = -1.0; b = -1.0; break;
case 4: /* from 4rth quadrant to point */
a = 1.0; b = -1.0; break;
default: /* from 1rst quadrant to point */
a = 1.0; b = 1.0; break;
}
V = a * Duv + b * Dvv;
Av.x = Du.y * V.z - Du.z * V.y;
Av.y = Du.z * V.x - Du.x * V.z;
Av.z = Du.x * V.y - Du.y * V.x;
V = a * Duu + b * Duv;
Au.x = V.y * Dv.z - V.z * Dv.y;
Au.y = V.z * Dv.x - V.x * Dv.z;
Au.z = V.x * Dv.y - V.y * Dv.x;
N = Av + Au;
}
else
N = ON_3dVector::ZeroVector;
return N.Unitize();
}
bool ON_EvTangent(
const ON_3dVector& D1, // first derivative
const ON_3dVector& D2, // second derivative
ON_3dVector& T // Unit tangent returned here
)
{
// Evaluate unit tangent from first and second derivatives
// T = D1 / |D1|
bool rc = false;
double d1 = D1.Length();
if (d1 == 0.0)
{
// Use L'hopital's rule to show that if the unit tangent
// exists and the 1rst derivative is zero and the 2nd derivative is
// nonzero, then the unit tangent is equal to +/-the unitized
// 2nd derivative. The sign is equal to the sign of D1(s) o D2(s)
// as s approaches the evaluation parameter.
//
d1 = D2.Length();
if (d1 > 0.0)
{
T = D2/d1;
rc = true;
}
else
{
T = ON_3dVector::ZeroVector;
}
}
else
{
T = D1/d1;
rc = true;
}
return rc;
}
bool
ON_EvCurvature(
const ON_3dVector& D1, // first derivative
const ON_3dVector& D2, // second derivative
ON_3dVector& T, // Unit tangent returned here
ON_3dVector& K // Curvature returned here
)
{
// Evaluate unit tangent and curvature from first and second derivatives
// T = D1 / |D1|
// K = ( D2 - (D2 o T)*T )/( D1 o D1)
bool rc = false;
double d1 = D1.Length();
if (d1 == 0.0)
{
// Use L'hopital's rule to show that if the unit tangent
// exists and the 1rst derivative is zero and the 2nd derivative is
// nonzero, then the unit tangent is equal to +/-the unitized
// 2nd derivative. The sign is equal to the sign of D1(s) o D2(s)
// as s approaches the evaluation parameter.
//
d1 = D2.Length();
if (d1 > 0.0) {
T = D2/d1;
}
else
{
T = ON_3dVector::ZeroVector;
}
K = ON_3dVector::ZeroVector;
}
else
{
T = D1/d1;
const double negD2oT = -D2*T;
d1 = 1.0/(d1*d1);
K = d1*( D2 + negD2oT*T );
rc = true;
}
return rc;
}
bool
ON_EvCurvature1Der(
const ON_3dVector& D1, // first derivative
const ON_3dVector& D2, // second derivative
const ON_3dVector& D3, // third derivative
ON_3dVector& T, // Unit tangent returned here
ON_3dVector& K, // curvature vector(k*N). curvature k = K.Length() and Normal N=K.Unitize()
double* kprime, // first derivative of k
double* torsion) // torsion
{
bool rc = false;
double dsdt = D1.Length();
if (dsdt > 0)
{
T = (1 / dsdt) * D1;
// Differentiate the formula k = | q | / |D1|^3, where q = D1 x D2
ON_3dVector q = ON_CrossProduct(D1, D2);
double qlen2 = q.LengthSquared();
double dsdt2 = dsdt * dsdt;
K = (1.0/dsdt2) * (D2 - (D2*T) * T);
if (kprime)
{
ON_3dVector qprime = ON_CrossProduct(D1, D3);
if (qlen2 > 0)
{
*kprime = ((q * qprime) * D1.LengthSquared() - 3 * qlen2 * (D1 * D2)) /
(sqrt(qlen2) * pow(D1.Length(), 5.0));
}
else
*kprime = qprime.Length() / pow(D1.Length(), 3);
rc = true;
}
if (torsion)
{
if (qlen2 > 0)
{
*torsion = q * D3 / qlen2;
rc = true;
}
else
rc = false;
}
}
return rc;
}
bool ON_EvSectionalCurvature(
const ON_3dVector& S10,
const ON_3dVector& S01,
const ON_3dVector& S20,
const ON_3dVector& S11,
const ON_3dVector& S02,
const ON_3dVector& planeNormal,
ON_3dVector& K
)
{
ON_3dVector M, D1, D2;
double a, b, e, pr;
int rank;
// Calculates the curvature of the intersection of the surface
// and plane at the point were the surface partials were evaluated.
// If D1 and D2 are the derivatives of any parametric curve,
// then the curvature is
//
// K = (D2 - (D2oD1)/(D1oD1)*D1)/(D1oD1)
//
// So, the trick is to assign a parameterization to the intersection
// curve and use the surface partials and plane normal
// to calculate the curve's derivatives. For computational reasons,
// I'm choosing the parameterization such that
//
// D1 = (Su X Sv) X sectionNormal.
//
// Then
//
// D2 = ((Suu*u' + Suv*v') X Sv + Su X (Suv*u' + Svv*v')) X sectionNormal,
//
// where the (unknown) intersection curve is srf(u(t),v(t)). But, we
// do know D1 can also be computed as
//
// D1 = Su*u' + Sv*v'
//
// So, if Su and Sv are linearly independent, then we have
// (Su X Sv) X sectionNormal = Su*u' + Sv*v' and can solve for u' and v'.
//
// M = Su X Sv (surface normal = M/|M|)
//M = ON_CrossProduct(S10,S01);
M.x = S10.y*S01.z - S01.y*S10.z;
M.y = S10.z*S01.x - S01.z*S10.x;
M.z = S10.x*S01.y - S01.x*S10.y;
// D1 = 1st derivative of the intersection curve
//D1 = ON_CrossProduct(M,sectionN);
D1.x = M.y*planeNormal.z - planeNormal.y*M.z;
D1.y = M.z*planeNormal.x - planeNormal.z*M.x;
D1.z = M.x*planeNormal.y - planeNormal.x*M.y;
// D1 is tangent to the surface. Find a, b so that D1 = a*Su + b*Sv.
rank = ON_Solve3x2( &S10.x, &S01.x, D1.x, D1.y, D1.z, &a, &b, &e, &pr );
if ( rank < 2 )
{
K.x = 0.0;
K.y = 0.0;
K.z = 0.0;
return false;
}
// M1 = derivative of M = (a*Suu + v*Suv) x Sv + Su x (a*Suv + b*Svv)
//M1 = ON_CrossProduct(a*S20 + b*S11, S01) + ON_CrossProduct(S10, a*S11 + b*S02);
D2.x = a*S20.x + b*S11.x;
D2.y = a*S20.y + b*S11.y;